source: branches/test1/Client/Forms/UFormLoginProfile.pas

Last change on this file was 52, checked in by chronos, 14 years ago
  • Modified: Code related to persistence layer abstraction moved to separated package PersistentData.
File size: 8.8 KB
Line 
1unit UFormLoginProfile;
2
3{$mode delphi}
4
5interface
6
7uses
8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
9 ExtCtrls, Spin, SpecializedList, URegistry, Registry;
10
11type
12 TConnectProtocol = (cpXMLRPC, cpDirect, cpMySQL, cpINI, cpRegistry, cpSQLLite);
13
14 { TConnectProfile }
15
16 TConnectProfile = class
17 Name: string;
18 UserName: string;
19 HostName: string;
20 Protocol: TConnectProtocol;
21 Database: string;
22 Port: Word;
23 constructor Create;
24 procedure Assign(Source: TConnectProfile);
25 end;
26
27 { TProfileList }
28
29 TProfileList = class(TListObject)
30 procedure LoadFromRegistry(ARootKey: HKEY; AKey: string);
31 procedure SaveToRegistry(ARootKey: HKEY; AKey: string);
32 procedure Assign(Source: TProfileList);
33 procedure FillStrings(Strings: TStrings);
34 end;
35
36 { TLoginProfileForm }
37
38 TLoginProfileForm = class(TForm)
39 ButtonCancel: TButton;
40 ButtonAdd: TButton;
41 ButtonDelete: TButton;
42 ButtonOk: TButton;
43 ComboBoxProtocol: TComboBox;
44 EditName: TEdit;
45 EditDatabase: TEdit;
46 EditServer: TEdit;
47 Label1: TLabel;
48 Label2: TLabel;
49 Label3: TLabel;
50 Label4: TLabel;
51 Label5: TLabel;
52 LabelServer: TLabel;
53 ListBoxProfiles: TListBox;
54 Panel1: TPanel;
55 SpinEditPort: TSpinEdit;
56 procedure ButtonAddClick(Sender: TObject);
57 procedure ButtonDeleteClick(Sender: TObject);
58 procedure ButtonOkClick(Sender: TObject);
59 procedure ComboBoxProtocolChange(Sender: TObject);
60 procedure EditNameChange(Sender: TObject);
61 procedure EditDatabaseChange(Sender: TObject);
62 procedure EditServerChange(Sender: TObject);
63 procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
64 procedure FormCreate(Sender: TObject);
65 procedure FormDestroy(Sender: TObject);
66 procedure FormShow(Sender: TObject);
67 procedure ListBoxProfilesSelectionChange(Sender: TObject; User: boolean);
68 procedure SpinEditPortChange(Sender: TObject);
69 private
70 SelectedProfile: TConnectProfile;
71 ProfileList: TProfileList;
72 public
73 procedure UpdateInterface;
74 end;
75
76resourcestring
77 SProfile = 'Profile';
78 SProtocolXMLRPC = 'XML-RPC';
79 SProtocolMySQL = 'MySQL';
80 SProtocolDirect = 'Direct';
81
82var
83 LoginProfileForm: TLoginProfileForm;
84 ProtocolTypeText: array[TConnectProtocol] of string;
85
86procedure UpdateTranslation;
87
88implementation
89
90{$R *.lfm}
91
92uses
93 UCore, UFormMain;
94
95{ TConnectProfile }
96
97constructor TConnectProfile.Create;
98begin
99 Port := 8090;
100 HostName := 'localhost';
101 UserName := 'admin';
102 Name := SProfile;
103 Database := 'database';
104end;
105
106procedure TConnectProfile.Assign(Source: TConnectProfile);
107begin
108 Name := Source.Name;
109 UserName := Source.UserName;
110 Database := Source.Database;
111 Port := Source.Port;
112 Protocol := Source.Protocol;
113 HostName := Source.HostName;
114end;
115
116{ TLoginProfileForm }
117
118procedure TLoginProfileForm.FormCreate(Sender: TObject);
119begin
120 ProfileList := TProfileList.Create;
121end;
122
123procedure TLoginProfileForm.ButtonOkClick(Sender: TObject);
124begin
125 Core.Profiles.Assign(ProfileList);
126 Core.Profiles.SaveToRegistry(Core.RegistryRootKey, Core.RegistryKey);
127end;
128
129procedure TLoginProfileForm.ComboBoxProtocolChange(Sender: TObject);
130begin
131 if Assigned(SelectedProfile) then
132 SelectedProfile.Protocol :=
133 TConnectProtocol(ComboBoxProtocol.Items.Objects[ComboBoxProtocol.ItemIndex]);
134end;
135
136procedure TLoginProfileForm.EditNameChange(Sender: TObject);
137begin
138 if Assigned(SelectedProfile) then begin
139 SelectedProfile.Name := EditName.Text;
140 ListBoxProfiles.Items[ListBoxProfiles.ItemIndex] := EditName.Text;
141 end;
142end;
143
144procedure TLoginProfileForm.EditDatabaseChange(Sender: TObject);
145begin
146 if Assigned(SelectedProfile) then
147 SelectedProfile.Database := EditDatabase.Text;
148end;
149
150procedure TLoginProfileForm.EditServerChange(Sender: TObject);
151begin
152 if Assigned(SelectedProfile) then
153 SelectedProfile.HostName := EditServer.Text;
154end;
155
156procedure TLoginProfileForm.FormClose(Sender: TObject;
157 var CloseAction: TCloseAction);
158begin
159 MainForm.PersistentForm.Save(Self);
160 Core.Profiles.SaveToRegistry(Core.RegistryRootKey, Core.RegistryKey);
161end;
162
163procedure TLoginProfileForm.ButtonAddClick(Sender: TObject);
164begin
165 ProfileList.AddNew(TConnectProfile.Create);
166 ListBoxProfiles.Items.AddObject(TConnectProfile(ProfileList.Last).Name,
167 TConnectProfile(ProfileList.Last));
168end;
169
170procedure TLoginProfileForm.ButtonDeleteClick(Sender: TObject);
171begin
172 if ListBoxProfiles.ItemIndex <> - 1 then begin
173 ProfileList.Delete(ListBoxProfiles.ItemIndex);
174 ListBoxProfiles.Items.Delete(ListBoxProfiles.ItemIndex);
175 end;
176end;
177
178procedure TLoginProfileForm.FormDestroy(Sender: TObject);
179begin
180 ProfileList.Free;
181end;
182
183procedure TLoginProfileForm.FormShow(Sender: TObject);
184var
185 I: Integer;
186begin
187 ComboBoxProtocol.Clear;
188 for I := 0 to Integer(High(ProtocolTypeText)) do
189 ComboBoxProtocol.Items.AddObject(ProtocolTypeText[TConnectProtocol(I)],
190 Pointer(I));
191
192 MainForm.PersistentForm.Load(Self);
193 ProfileList.Assign(Core.Profiles);
194 ProfileList.FillStrings(ListBoxProfiles.Items);
195 if Core.LastProfile < ListBoxProfiles.Count then
196 ListBoxProfiles.ItemIndex := Core.LastProfile;
197 if (ListBoxProfiles.Items.Count > 0) and
198 (ListBoxProfiles.ItemIndex = -1) then ListBoxProfiles.ItemIndex := 0;
199 UpdateInterface;
200end;
201
202procedure TLoginProfileForm.ListBoxProfilesSelectionChange(Sender: TObject;
203 User: boolean);
204begin
205 if ListBoxProfiles.ItemIndex <> -1 then
206 with TConnectProfile(ProfileList[ListBoxProfiles.ItemIndex]) do begin
207 SelectedProfile := nil;
208 EditServer.Text := HostName;
209 EditDatabase.Text := Database;
210 ComboBoxProtocol.ItemIndex := ComboBoxProtocol.Items.IndexOfObject(Pointer(Protocol));
211 SpinEditPort.Value := Port;
212 EditName.Text := Name;
213 SelectedProfile := TConnectProfile(ProfileList[ListBoxProfiles.ItemIndex]);
214 UpdateInterface;
215 end;
216end;
217
218procedure TLoginProfileForm.SpinEditPortChange(Sender: TObject);
219begin
220 if Assigned(SelectedProfile) then
221 SelectedProfile.Port := SpinEditPort.Value;
222end;
223
224procedure TLoginProfileForm.UpdateInterface;
225begin
226 EditName.Enabled := ListBoxProfiles.ItemIndex <> -1;
227 EditServer.Enabled := ListBoxProfiles.ItemIndex <> -1;
228 EditDatabase.Enabled := ListBoxProfiles.ItemIndex <> -1;
229 SpinEditPort.Enabled := ListBoxProfiles.ItemIndex <> -1;
230 ComboBoxProtocol.Enabled := ListBoxProfiles.ItemIndex <> -1;
231end;
232
233procedure TProfileList.LoadFromRegistry(ARootKey: HKEY; AKey: string);
234var
235 KeyInfo: TRegKeyInfo;
236 I: Integer;
237begin
238 with TRegistryEx.Create do
239 try
240 RootKey := ARootKey;
241 OpenKey(AKey + '\ConnectProfile', True);
242 GetKeyInfo(KeyInfo);
243 for I := 0 to KeyInfo.NumSubKeys - 1 do
244 if KeyExists(AKey + '\ConnectProfile\' + IntToStr(I)) then begin
245 OpenKey(AKey + '\ConnectProfile\' + IntToStr(I), True);
246 with TConnectProfile(AddNew(TConnectProfile.Create)) do begin
247 Name := ReadStringWithDefault('Name', Name);
248 HostName := ReadStringWithDefault('HostName', HostName);
249 Port := ReadIntegerWithDefault('Port', Port);
250 Database := ReadStringWithDefault('Database', Database);
251 Protocol := TConnectProtocol(ReadIntegerWithDefault('Protocol', Integer(Protocol)));
252 UserName := ReadStringWithDefault('UserName', UserName);
253 end;
254 end;
255 finally
256 Free;
257 end;
258end;
259
260procedure TProfileList.SaveToRegistry(ARootKey: HKEY; AKey: string);
261var
262 KeyInfo: TRegKeyInfo;
263 I: Integer;
264begin
265 with TRegistryEx.Create do
266 try
267 RootKey := ARootKey;
268 OpenKey(AKey + '\ConnectProfile', True);
269 for I := 0 to Count - 1 do begin
270 OpenKey(AKey + '\ConnectProfile\' + IntToStr(I), True);
271 with TConnectProfile(Items[I]) do begin
272 WriteString('Name', Name);
273 WriteString('HostName', HostName);
274 WriteInteger('Port', Port);
275 WriteString('Database', Database);
276 WriteInteger('Protocol', Integer(Protocol));
277 WriteString('UserName', UserName);
278 end;
279 end;
280 finally
281 Free;
282 end;
283end;
284
285procedure TProfileList.Assign(Source: TProfileList);
286var
287 I: Integer;
288begin
289 Clear;
290 Count := Source.Count;
291 for I := 0 to Source.Count - 1 do begin
292 Items[I] := TConnectProfile.Create;
293 TConnectProfile(Items[I]).Assign(TConnectProfile(Source.Items[I]));
294 end;
295end;
296
297procedure TProfileList.FillStrings(Strings: TStrings);
298var
299 I: Integer;
300begin
301 Strings.Clear;
302 for I := 0 to Count - 1 do
303 Strings.AddObject(TConnectProfile(Items[I]).Name, Items[I]);
304end;
305
306procedure UpdateTranslation;
307begin
308 ProtocolTypeText[cpXMLRPC] := SProtocolXMLRPC;
309 ProtocolTypeText[cpMySQL] := SProtocolMySQL;
310 ProtocolTypeText[cpDirect] := SProtocolDirect;
311end;
312
313end.
314
Note: See TracBrowser for help on using the repository browser.