| 1 | unit UFormLoginProfile;
|
|---|
| 2 |
|
|---|
| 3 | {$mode delphi}
|
|---|
| 4 |
|
|---|
| 5 | interface
|
|---|
| 6 |
|
|---|
| 7 | uses
|
|---|
| 8 | Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
|
|---|
| 9 | ExtCtrls, Spin, SpecializedList, URegistry, Registry;
|
|---|
| 10 |
|
|---|
| 11 | type
|
|---|
| 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 |
|
|---|
| 76 | resourcestring
|
|---|
| 77 | SProfile = 'Profile';
|
|---|
| 78 | SProtocolXMLRPC = 'XML-RPC';
|
|---|
| 79 | SProtocolMySQL = 'MySQL';
|
|---|
| 80 | SProtocolDirect = 'Direct';
|
|---|
| 81 |
|
|---|
| 82 | var
|
|---|
| 83 | LoginProfileForm: TLoginProfileForm;
|
|---|
| 84 | ProtocolTypeText: array[TConnectProtocol] of string;
|
|---|
| 85 |
|
|---|
| 86 | procedure UpdateTranslation;
|
|---|
| 87 |
|
|---|
| 88 | implementation
|
|---|
| 89 |
|
|---|
| 90 | {$R *.lfm}
|
|---|
| 91 |
|
|---|
| 92 | uses
|
|---|
| 93 | UCore, UFormMain;
|
|---|
| 94 |
|
|---|
| 95 | { TConnectProfile }
|
|---|
| 96 |
|
|---|
| 97 | constructor TConnectProfile.Create;
|
|---|
| 98 | begin
|
|---|
| 99 | Port := 8090;
|
|---|
| 100 | HostName := 'localhost';
|
|---|
| 101 | UserName := 'admin';
|
|---|
| 102 | Name := SProfile;
|
|---|
| 103 | Database := 'database';
|
|---|
| 104 | end;
|
|---|
| 105 |
|
|---|
| 106 | procedure TConnectProfile.Assign(Source: TConnectProfile);
|
|---|
| 107 | begin
|
|---|
| 108 | Name := Source.Name;
|
|---|
| 109 | UserName := Source.UserName;
|
|---|
| 110 | Database := Source.Database;
|
|---|
| 111 | Port := Source.Port;
|
|---|
| 112 | Protocol := Source.Protocol;
|
|---|
| 113 | HostName := Source.HostName;
|
|---|
| 114 | end;
|
|---|
| 115 |
|
|---|
| 116 | { TLoginProfileForm }
|
|---|
| 117 |
|
|---|
| 118 | procedure TLoginProfileForm.FormCreate(Sender: TObject);
|
|---|
| 119 | begin
|
|---|
| 120 | ProfileList := TProfileList.Create;
|
|---|
| 121 | end;
|
|---|
| 122 |
|
|---|
| 123 | procedure TLoginProfileForm.ButtonOkClick(Sender: TObject);
|
|---|
| 124 | begin
|
|---|
| 125 | Core.Profiles.Assign(ProfileList);
|
|---|
| 126 | Core.Profiles.SaveToRegistry(Core.RegistryRootKey, Core.RegistryKey);
|
|---|
| 127 | end;
|
|---|
| 128 |
|
|---|
| 129 | procedure TLoginProfileForm.ComboBoxProtocolChange(Sender: TObject);
|
|---|
| 130 | begin
|
|---|
| 131 | if Assigned(SelectedProfile) then
|
|---|
| 132 | SelectedProfile.Protocol :=
|
|---|
| 133 | TConnectProtocol(ComboBoxProtocol.Items.Objects[ComboBoxProtocol.ItemIndex]);
|
|---|
| 134 | end;
|
|---|
| 135 |
|
|---|
| 136 | procedure TLoginProfileForm.EditNameChange(Sender: TObject);
|
|---|
| 137 | begin
|
|---|
| 138 | if Assigned(SelectedProfile) then begin
|
|---|
| 139 | SelectedProfile.Name := EditName.Text;
|
|---|
| 140 | ListBoxProfiles.Items[ListBoxProfiles.ItemIndex] := EditName.Text;
|
|---|
| 141 | end;
|
|---|
| 142 | end;
|
|---|
| 143 |
|
|---|
| 144 | procedure TLoginProfileForm.EditDatabaseChange(Sender: TObject);
|
|---|
| 145 | begin
|
|---|
| 146 | if Assigned(SelectedProfile) then
|
|---|
| 147 | SelectedProfile.Database := EditDatabase.Text;
|
|---|
| 148 | end;
|
|---|
| 149 |
|
|---|
| 150 | procedure TLoginProfileForm.EditServerChange(Sender: TObject);
|
|---|
| 151 | begin
|
|---|
| 152 | if Assigned(SelectedProfile) then
|
|---|
| 153 | SelectedProfile.HostName := EditServer.Text;
|
|---|
| 154 | end;
|
|---|
| 155 |
|
|---|
| 156 | procedure TLoginProfileForm.FormClose(Sender: TObject;
|
|---|
| 157 | var CloseAction: TCloseAction);
|
|---|
| 158 | begin
|
|---|
| 159 | MainForm.PersistentForm.Save(Self);
|
|---|
| 160 | Core.Profiles.SaveToRegistry(Core.RegistryRootKey, Core.RegistryKey);
|
|---|
| 161 | end;
|
|---|
| 162 |
|
|---|
| 163 | procedure TLoginProfileForm.ButtonAddClick(Sender: TObject);
|
|---|
| 164 | begin
|
|---|
| 165 | ProfileList.AddNew(TConnectProfile.Create);
|
|---|
| 166 | ListBoxProfiles.Items.AddObject(TConnectProfile(ProfileList.Last).Name,
|
|---|
| 167 | TConnectProfile(ProfileList.Last));
|
|---|
| 168 | end;
|
|---|
| 169 |
|
|---|
| 170 | procedure TLoginProfileForm.ButtonDeleteClick(Sender: TObject);
|
|---|
| 171 | begin
|
|---|
| 172 | if ListBoxProfiles.ItemIndex <> - 1 then begin
|
|---|
| 173 | ProfileList.Delete(ListBoxProfiles.ItemIndex);
|
|---|
| 174 | ListBoxProfiles.Items.Delete(ListBoxProfiles.ItemIndex);
|
|---|
| 175 | end;
|
|---|
| 176 | end;
|
|---|
| 177 |
|
|---|
| 178 | procedure TLoginProfileForm.FormDestroy(Sender: TObject);
|
|---|
| 179 | begin
|
|---|
| 180 | ProfileList.Free;
|
|---|
| 181 | end;
|
|---|
| 182 |
|
|---|
| 183 | procedure TLoginProfileForm.FormShow(Sender: TObject);
|
|---|
| 184 | var
|
|---|
| 185 | I: Integer;
|
|---|
| 186 | begin
|
|---|
| 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;
|
|---|
| 200 | end;
|
|---|
| 201 |
|
|---|
| 202 | procedure TLoginProfileForm.ListBoxProfilesSelectionChange(Sender: TObject;
|
|---|
| 203 | User: boolean);
|
|---|
| 204 | begin
|
|---|
| 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;
|
|---|
| 216 | end;
|
|---|
| 217 |
|
|---|
| 218 | procedure TLoginProfileForm.SpinEditPortChange(Sender: TObject);
|
|---|
| 219 | begin
|
|---|
| 220 | if Assigned(SelectedProfile) then
|
|---|
| 221 | SelectedProfile.Port := SpinEditPort.Value;
|
|---|
| 222 | end;
|
|---|
| 223 |
|
|---|
| 224 | procedure TLoginProfileForm.UpdateInterface;
|
|---|
| 225 | begin
|
|---|
| 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;
|
|---|
| 231 | end;
|
|---|
| 232 |
|
|---|
| 233 | procedure TProfileList.LoadFromRegistry(ARootKey: HKEY; AKey: string);
|
|---|
| 234 | var
|
|---|
| 235 | KeyInfo: TRegKeyInfo;
|
|---|
| 236 | I: Integer;
|
|---|
| 237 | begin
|
|---|
| 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;
|
|---|
| 258 | end;
|
|---|
| 259 |
|
|---|
| 260 | procedure TProfileList.SaveToRegistry(ARootKey: HKEY; AKey: string);
|
|---|
| 261 | var
|
|---|
| 262 | KeyInfo: TRegKeyInfo;
|
|---|
| 263 | I: Integer;
|
|---|
| 264 | begin
|
|---|
| 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;
|
|---|
| 283 | end;
|
|---|
| 284 |
|
|---|
| 285 | procedure TProfileList.Assign(Source: TProfileList);
|
|---|
| 286 | var
|
|---|
| 287 | I: Integer;
|
|---|
| 288 | begin
|
|---|
| 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;
|
|---|
| 295 | end;
|
|---|
| 296 |
|
|---|
| 297 | procedure TProfileList.FillStrings(Strings: TStrings);
|
|---|
| 298 | var
|
|---|
| 299 | I: Integer;
|
|---|
| 300 | begin
|
|---|
| 301 | Strings.Clear;
|
|---|
| 302 | for I := 0 to Count - 1 do
|
|---|
| 303 | Strings.AddObject(TConnectProfile(Items[I]).Name, Items[I]);
|
|---|
| 304 | end;
|
|---|
| 305 |
|
|---|
| 306 | procedure UpdateTranslation;
|
|---|
| 307 | begin
|
|---|
| 308 | ProtocolTypeText[cpXMLRPC] := SProtocolXMLRPC;
|
|---|
| 309 | ProtocolTypeText[cpMySQL] := SProtocolMySQL;
|
|---|
| 310 | ProtocolTypeText[cpDirect] := SProtocolDirect;
|
|---|
| 311 | end;
|
|---|
| 312 |
|
|---|
| 313 | end.
|
|---|
| 314 |
|
|---|