This repository has been archived by the owner on Jun 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
/
MeConnectForm.pas
305 lines (274 loc) · 9.41 KB
/
MeConnectForm.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
unit MeConnectForm;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, TypInfo, FireDAC.Comp.Client;
type
TMeConnectForm = class(TForm)
btConnect: TButton;
btCancel: TButton;
btDetails: TButton;
gbAdvanced: TGroupBox;
btClear: TButton;
gbConnect: TGroupBox;
edUsername: TEdit;
lbUsername: TLabel;
lbPassword: TLabel;
edPassword: TEdit;
edServer: TComboBox;
lbServer: TLabel;
lbPort: TLabel;
edPort: TEdit;
edmDatabase: TComboBox;
lbmDatabase: TLabel;
cbSavePassword: TCheckBox;
Image1: TImage;
lbCharSet: TLabel;
edCharSet: TEdit;
edcDatabase: TComboBox;
lbcDatabase: TLabel;
edrDatabase: TComboBox;
lbrDatabase: TLabel;
procedure btConnectClick(Sender: TObject);
procedure edServerDropDown(Sender: TObject);
procedure GetDataBases(Sender: TObject);
procedure edExit(Sender: TObject);
procedure btDetailsClick(Sender: TObject);
procedure btClearClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure edServerChange(Sender: TObject);
private
FListGot : boolean;
procedure SaveSettings;
procedure LoadSettings;
procedure SavePassword;
procedure LoadPassword;
procedure GetServerList(Items: TStrings);
protected
procedure DoInit;
procedure DoConnect;
end;
implementation
uses MainUnit, MyDataModule, Functions, Registry;
{$R *.dfm}
procedure TMeConnectForm.DoInit;
begin
FListGot := false;
if (edUsername.Text <> '') and (edPassword.Text = '') then
ActiveControl := edPassword;
LoadSettings;
if cbSavePassword.Checked then begin
LoadPassword;
end;
end;
procedure TMeConnectForm.DoConnect;
begin
edExit(nil);
MainForm.CharDBName := edcDatabase.Text;
MainForm.RealmDBName := edrDatabase.Text;
try
if (MainForm.MyTrinityConnection.Connected=true) then
MainForm.MyTrinityConnection.Close;
MainForm.MyTrinityConnection.Open;
if Trim(edCharSet.Text) <> '' then
begin
MainForm.MyTempQuery.SQL.Text := Format('SET NAMES %s',[edCharSet.Text]);
MainForm.MyTempQuery.ExecSQL;
end;
ModalResult := mrOk;
except
ModalResult := mrNone;
raise;
end;
end;
procedure TMeConnectForm.btConnectClick(Sender: TObject);
begin
SaveSettings;
DoConnect;
if cbSavePassword.Checked then begin
SavePassword;
end;
end;
procedure TMeConnectForm.edServerDropDown(Sender: TObject);
var
OldCursor: TCursor;
begin
if FListGot then
Exit;
FListGot := True;
OldCursor := Screen.Cursor;
Screen.Cursor := crSQLWait;
try
GetServerList(edServer.Items);
finally
Screen.Cursor := OldCursor;
end;
end;
procedure TMeConnectForm.GetServerList(Items: TStrings);
begin
with TRegistry.Create do
try
RootKey:= HKEY_CURRENT_USER;
if OpenKey('Software\' + Trim(ProgramName) + '\servers', false) then
GetKeyNames(Items);
finally
Free;
end;
end;
procedure TMeConnectForm.GetDataBases(Sender: TObject);
var
OldCursor: TCursor;
WasConnected: Boolean;
begin
edmDatabase.Items.Clear;
edcDatabase.Items.Clear;
edrDatabase.Items.Clear;
OldCursor := Screen.Cursor;
Screen.Cursor := crSQLWait;
try
WasConnected := (MainForm.MyTrinityConnection.Connected=True);
if (WasConnected=False) then
MainForm.MyTrinityConnection.Open;
MainForm.MyTrinityConnection.GetCatalogNames('', edmDatabase.Items);
edcDatabase.Items.AddStrings(edmDatabase.Items);
edrDatabase.Items.AddStrings(edmDatabase.Items);
if (WasConnected=False) then
MainForm.MyTrinityConnection.Close;
finally
Screen.Cursor := OldCursor;
end;
end;
procedure TMeConnectForm.edExit(Sender: TObject);
begin
try
MainForm.MyTrinityConnection.Params.Clear;
MainForm.MyTrinityConnection.DriverName:='MySQL';
MainForm.MyTrinityConnection.Params.AddPair('Server', edServer.Text);
MainForm.MyTrinityConnection.Params.AddPair('Port', edPort.Text);
MainForm.MyTrinityConnection.Params.AddPair('Database', edmDatabase.Text);
MainForm.MyTrinityConnection.Params.AddPair('User_Name', edUsername.Text);
MainForm.MyTrinityConnection.Params.AddPair('Password', edPassword.Text);
except
ActiveControl := Sender as TWinControl;
raise;
end;
end;
procedure TMeConnectForm.LoadPassword;
begin
edPassword.Text:=ReadFromRegistry(CurrentUser, '', 'Password', tpString, '');
end;
procedure TMeConnectForm.SavePassword;
begin
WriteToRegistry(CurrentUser, '', 'Password', tpString, edPassword.Text);
WriteToRegistry(CurrentUser, 'servers\' + edServer.Text, 'Password', tpString, edPassword.Text);
end;
procedure TMeConnectForm.LoadSettings;
begin
cbSavePassword.Checked:=ReadFromRegistry(CurrentUser, '', 'SavePass', tpBool, false);
edServer.Text:=ReadFromRegistry(CurrentUser, '', 'Server', tpString, '');
edUsername.Text:=ReadFromRegistry(CurrentUser, '', 'Username', tpString, '');
edPort.Text:=ReadFromRegistry(CurrentUser, '', 'Port', tpString, '');
edCharSet.Text:=ReadFromRegistry(CurrentUser, '', 'Charset', tpString, '');
end;
procedure TMeConnectForm.SaveSettings;
begin
WriteToRegistry(CurrentUser, '', 'SavePass', tpBool, cbSavePassword.Checked);
WriteToRegistry(CurrentUser, '', 'Server', tpString, edServer.Text);
WriteToRegistry(CurrentUser, '', 'Username', tpString, edUsername.Text);
WriteToRegistry(CurrentUser, '', 'cDatabase', tpString, edcDatabase.Text);
WriteToRegistry(CurrentUser, '', 'mDatabase', tpString, edmDatabase.Text);
WriteToRegistry(CurrentUser, '', 'rDatabase', tpString, edrDatabase.Text);
WriteToRegistry(CurrentUser, '', 'Port', tpString, edPort.Text);
WriteToRegistry(CurrentUser, '', 'Charset', tpString, edCharSet.Text);
WriteToRegistry(CurrentUser, 'servers\' + edServer.Text, 'SavePass', tpBool, cbSavePassword.Checked);
WriteToRegistry(CurrentUser, 'servers\' + edServer.Text, 'Username', tpString, edUsername.Text);
WriteToRegistry(CurrentUser, 'servers\' + edServer.Text, 'mDatabase', tpString, edmDatabase.Text);
WriteToRegistry(CurrentUser, 'servers\' + edServer.Text, 'cDatabase', tpString, edcDatabase.Text);
WriteToRegistry(CurrentUser, 'servers\' + edServer.Text, 'rDatabase', tpString, edrDatabase.Text);
WriteToRegistry(CurrentUser, 'servers\' + edServer.Text, 'Port', tpString, edPort.Text);
WriteToRegistry(CurrentUser, 'servers\' + edServer.Text, 'Charset', tpString, edCharSet.Text);
end;
procedure TMeConnectForm.btDetailsClick(Sender: TObject);
const
mH = 80;
begin
if pos('>>', btDetails.Caption)>0 then
begin
ClientHeight:=ClientHeight+gbAdvanced.Height+10;
btDetails.Caption:=dmMain.Text[77]; //'<< Details';
gbAdvanced.Visible:=true;
end
else
begin
ClientHeight:=ClientHeight-gbAdvanced.Height-10;
btDetails.Caption:=dmMain.Text[78]; //'Details >>';
gbAdvanced.Visible:=false;
end;
end;
procedure TMeConnectForm.btClearClick(Sender: TObject);
begin
edUsername.Clear;
edPassword.Clear;
edServer.Clear;
edPort.Clear;
edmDatabase.Clear;
edcDatabase.Clear;
edrDatabase.Clear;
edCharSet.Clear;
end;
procedure TMeConnectForm.FormCreate(Sender: TObject);
begin
ClientHeight:=292;
DoInit;
Caption := Format('Truice %s',[VERSION_EXE]);
dmMain.Translate.CreateDefaultTranslation(TForm(Self));
end;
procedure TMeConnectForm.FormShow(Sender: TObject);
var
mDBname, cDBname, rDBname, sDBname: string;
AC: TWinControl;
begin
dmMain.Translate.TranslateForm(TForm(Self));
Caption := Format('Truice %s',[VERSION_EXE]);
mDBname:=ReadFromRegistry(CurrentUser, '', 'mDatabase', tpString, '');
cDBname:=ReadFromRegistry(CurrentUser, '', 'cDatabase', tpString, '');
rDBname:=ReadFromRegistry(CurrentUser, '', 'rDatabase', tpString, '');
sDBname:=ReadFromRegistry(CurrentUser, '', 'sDatabase', tpString, '');
edExit(Sender);
try
GetDataBases(Sender);
if mDBname<>'' then
edmDatabase.ItemIndex:=edmDatabase.Items.IndexOf(mDBName);
if cDBname<>'' then
edcDatabase.ItemIndex:=edcDatabase.Items.IndexOf(cDBName);
if rDBname<>'' then
edrDatabase.ItemIndex:=edrDatabase.Items.IndexOf(rDBName);
except
if edmDatabase.Text='' then
edmDatabase.Text:=mDBName;
if edcDatabase.Text='' then
edcDatabase.Text:=cDBName;
if edrDatabase.Text='' then
edrDatabase.Text:=rDBName;
end;
AC := edServer;
if edServer.Text <> '' then AC := edPort;
if edPort.Text <> '' then AC := edUsername;
if edUsername.Text <> '' then AC := edPassword;
if edPassword.Text <> '' then AC := btConnect;
FocusControl(AC);
end;
procedure TMeConnectForm.edServerChange(Sender: TObject);
begin
cbSavePassword.Checked:=ReadFromRegistry(CurrentUser, 'servers\' + edServer.Text, 'SavePass', tpBool, false);
if cbSavePassword.Checked then
edPassword.Text:=ReadFromRegistry(CurrentUser, 'servers\' + edServer.Text, 'Password', tpString, '');
edUsername.Text:=ReadFromRegistry(CurrentUser, 'servers\' + edServer.Text, 'Username', tpString, '');
edmDatabase.Text:=ReadFromRegistry(CurrentUser, 'servers\' + edServer.Text, 'mDatabase', tpString, '');
edcDatabase.Text:=ReadFromRegistry(CurrentUser, 'servers\' + edServer.Text, 'cDatabase', tpString, '');
edrDatabase.Text:=ReadFromRegistry(CurrentUser, 'servers\' + edServer.Text, 'rDatabase', tpString, '');
edPort.Text:=ReadFromRegistry(CurrentUser, 'servers\' + edServer.Text, 'Port', tpString, '');
edCharSet.Text:=ReadFromRegistry(CurrentUser,'servers\' + edServer.Text, 'Charset', tpString, '');
end;
end.