forked from vladk1973/npp.connections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BDLoginUnit.pas
566 lines (513 loc) · 14.7 KB
/
BDLoginUnit.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
unit BDLoginUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, NppForms, StdCtrls, Buttons, ExtCtrls, ConstUnit, regUnit,
IniFiles, Registry, System.Actions, Vcl.ActnList, Data.DB, Vcl.Grids,
{$IFNDEF NPPCONNECTIONS}diaConstUnit,{$ENDIF}
Vcl.DBGrids, Data.Win.ADODB, CustomDialogUnit;
type
TBDLoginForm = class(TCustomDialogForm)
ActionList1: TActionList;
OkAction: TAction;
customAction: TAction;
Image1: TImage;
SLabel: TLabel;
ServerList: TComboBox;
cbCustom: TCheckBox;
Port: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Password: TEdit;
Label4: TLabel;
Login: TEdit;
procedure ServerListDrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
procedure OkActionExecute(Sender: TObject);
procedure ServerListChange(Sender: TObject);
procedure OkActionUpdate(Sender: TObject);
procedure customActionUpdate(Sender: TObject);
procedure customActionExecute(Sender: TObject);
procedure CancelPanelClick(Sender: TObject);
procedure OkPanelClick(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure CancelPanelMouseEnter(Sender: TObject);
private
{ Private declarations }
FBDType: TBdType;
procedure FillDSNList(Strings: TStrings);
const cnstMSSqlServersKey0 = 'SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo';
const cnstMSSqlServersKey1 = 'SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo';
const cnstItem = '%s|Server=%s|Port=%s';
const cnstData = '|Login=%s|Password=%s|BdType=%d';
const cnstSrcSybase = 'SQL.INI';
const cnstSybaseName = 'SYBASE';
const cnstSybaseRoot = 'SYBROOT';
const cnstNothing = 'Пусто';
procedure FillNothing;
procedure FillServerList;
procedure FillServerListMS;
procedure FillServerListSyb;
procedure FillServerListCustom;
procedure FillODBCSources;
protected
procedure ChangeColorMode(Sender: TObject); override;
public
procedure Get(var aDataSource,aServer,aPort,aLogin,aPassword: string);
procedure SaveCurrentDataSourceToRegistry;
function Data: TCommand;
function DoForm: TModalResult; override;
property BDType: TBdType read FBDType write FBDType;
end;
implementation
function SQLDataSources(EnvironmentHandle:Pointer;Direction:Smallint;
ServerName:PAnsiChar;BufferLength1:Smallint;NameLength1Ptr:PSmallInt;
Description:PAnsiChar;BufferLength2:Smallint;NameLength2Ptr:PSmallInt): Smallint; stdcall; external 'odbc32.dll';
function SQLAllocHandle(HandleType:Smallint;InputHandle:Smallint;
OutputHandlePtr:Pointer): Smallint; stdcall; external 'odbc32.dll';
function SQLSetEnvAttr(EnvironmentHandle:Pointer;Attribute:Integer;
ValuePtr:Pointer;StringLength:Integer): Smallint; stdcall; external 'odbc32.dll';
function SQLFreeHandle(HandleType:Smallint;Handle:Pointer): Smallint; stdcall; external 'odbc32.dll';
function SQLAllocEnv(var phenv: Pointer):Smallint; stdcall; external 'odbc32.dll';
function SQLAllocConnect(henv: Pointer;var phdbc: Pointer):Smallint; stdcall; external 'odbc32.dll';
{$R *.dfm}
procedure TBDLoginForm.FillDSNList(Strings: TStrings);
const
MAX_BUF=1024;
SQL_HANDLE_ENV=1;
SQL_NULL_HENV=0;
SQL_ATTR_ODBC_VERSION=200;
SQL_IS_INTEGER=-6;
SQL_FETCH_FIRST_USER=31;
SQL_FETCH_FIRST=2;
SQL_FETCH_NEXT=1;
SQL_OV_ODBC3=3;
SQL_NO_DATA=100;
var
datasrc,descrip:array [0..MAX_BUF-1] of AnsiChar;
direction:Smallint;
rdsrc,rdesc,res:SmallInt;
hEnv:Pointer;
begin
rdsrc:=0;
rdesc:=0;
datasrc[0]:=#0;
descrip[0]:=#0;
res := SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HENV, @hEnv);
SQLSetEnvAttr(hEnv, SQL_ATTR_ODBC_VERSION,Pointer(SQL_OV_ODBC3),SQL_IS_INTEGER);
direction:=SQL_FETCH_FIRST;
Strings.Clear;
while (SQLDataSources(hEnv,direction,@datasrc[0],MAX_BUF,@rdsrc,@descrip[0],MAX_BUF,@rdesc)<>SQL_NO_DATA) do
begin
Strings.Add(string(datasrc)+ '||');
direction:=SQL_FETCH_NEXT;
end;
SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
end;
procedure TBDLoginForm.CancelPanelClick(Sender: TObject);
begin
CancelBtn.Click;
end;
procedure TBDLoginForm.CancelPanelMouseEnter(Sender: TObject);
begin
inherited;
TPanel(Sender).Color := RGB(GetRValue(Color)+10,GetGValue(Color)+10,GetBValue(Color)+10)
end;
procedure TBDLoginForm.ChangeColorMode(Sender: TObject);
begin
inherited;
if DarkMode then
begin
Port.BorderStyle := bsNone;
Password.BorderStyle := bsNone;
ServerList.BevelKind := bkFlat;
Login.BorderStyle := bsNone;
end;
end;
procedure TBDLoginForm.customActionExecute(Sender: TObject);
begin
FillServerList;
end;
procedure TBDLoginForm.customActionUpdate(Sender: TObject);
begin
TAction(Sender).Enabled := FBDType in [bdMSSQL,bdSybase];
end;
function TBDLoginForm.Data: TCommand;
var
i: Integer;
begin
Result := '';
i := ServerList.ItemIndex;
if cbCustom.Checked then
begin
if (ServerList.Text <> '') and (Port.Text <> '') then
begin
Result := Format(cnstItem,[ServerList.Text,ServerList.Text,Port.Text]) +
Format(cnstData,[Login.Text,Password.Text,Integer(FBdType)])
end;
end
else
if i >= 0 then
Result := ServerList.Items[ServerList.ItemIndex] +
Format(cnstData,[Login.Text,Password.Text,Integer(FBdType)])
end;
function TBDLoginForm.DoForm: TModalResult;
begin
Caption := constLoginBDCaption + constLoginBDCaptionArray[FBDType];
TopPanel.Caption := Caption;
SLabel.Caption := constDSBDCaptionArray[FBDType];
if (FBDType = bdPostgreSQL) then cbCustom.Checked := True;
FillServerList;
Result := inherited DoForm;
end;
procedure TBDLoginForm.FillNothing;
begin
ServerList.Clear;
ServerList.AddItem(cnstNothing,Pointer(1));
ServerList.ItemIndex := 0;
ServerList.Enabled := False;
end;
procedure TBDLoginForm.FillODBCSources;
begin
ServerList.clear;
FillDSNList(ServerList.Items);
if ServerList.Items.Count = 0 then
begin
FillNothing;
Exit;
end
else
ServerList.ItemIndex := 0;
end;
procedure TBDLoginForm.FillServerListMS;
var
Registry: TRegistry;
Servers,Servers32 : TStringList;
i,j: integer;
S,FDataSource,FServer,FPort: string;
begin
Registry := TRegistry.Create(KEY_READ);
Servers := TStringList.Create;
Servers.Duplicates := dupIgnore;
Servers32 := TStringList.Create;
Servers32.Duplicates := dupIgnore;
try
try
Registry.RootKey := HKEY_LOCAL_MACHINE;
if Registry.OpenKey(cnstMSSqlServersKey0, False) then
Registry.GetValueNames(Servers);
if Registry.OpenKey(cnstMSSqlServersKey1, False) then
Registry.GetValueNames(Servers32);
Servers.AddStrings(Servers32);
if Servers.Count = 0 then
begin
FillNothing;
Exit;
end;
Servers.Sorted := True;
for i := 0 to Servers.Count - 1 do
begin
S := Registry.ReadString(Servers[i]); //DBMSSOCN,Asql,2012
FDataSource := Servers[i];
j := Pos(',',S);
if j > 0 then
begin
FServer := Copy(S,j+1,MaxInt);
j := Pos(',',FServer);
if j > 0 then
begin
FPort := Copy(FServer,j+1,MaxInt);
FServer := Copy(FServer,1,j-1);
end;
end;
ServerList.AddItem(Format(cnstItem,[FDataSource,FServer,FPort]),nil);
end;
finally
Registry.CloseKey;
end;
finally
Registry.Free;
Servers.Free;
Servers32.Free;
if ServerList.Items.Count > 0 then
begin
ServerList.ItemIndex := 0;
ServerListChange(ServerList);
end;
end;
end;
procedure TBDLoginForm.FillServerList;
begin
ServerList.Clear;
Port.Text := '';
if cbCustom.Checked then
begin
Port.Enabled := True;
Port.NumbersOnly := True;
ServerList.Style := csDropDown;
ServerList.OnDrawItem := nil;
ServerList.Enabled := True;
FillServerListCustom;
end
else
begin
Port.Enabled := False;
Port.NumbersOnly := False;
ServerList.Style := csOwnerDrawFixed;
ServerList.OnDrawItem := ServerListDrawItem;
if FBDType = bdMSSQL then
FillServerListMS
else
if FBDType = bdSybase then
FillServerListSyb
else
if FBDType = bdPostgreSQL then
FillServerListCustom
else
if FBDType = bdODBC then
begin
FillODBCSources;
OkAction.OnUpdate := nil;
end;
end;
end;
procedure TBDLoginForm.FillServerListCustom;
var
Options: TOptionsReg;
Servers : TStringList;
i,j,FPort: integer;
S,FServer: string;
begin
Options := TOptionsReg.Create;
try
Servers := Options.GetConnectToList(FBDType);
if Servers.Count = 0 then Exit;
for i := 0 to Servers.Count - 1 do
begin
S := Servers[i]; //eisrko,5432
j := Pos(',',S);
if j > 0 then
begin
FPort := StrToInt(Copy(S,j+1,MaxInt));
FServer := Copy(S,1,j-1);
ServerList.AddItem(FServer,Pointer(FPort));
end;
end;
finally
Options.Free;
Servers.Free;
if ServerList.Items.Count > 0 then
begin
ServerList.ItemIndex := 0;
ServerListChange(ServerList);
end;
end;
end;
procedure TBDLoginForm.FillServerListSyb;
procedure GetEnvironmentStrings(Vars: TStrings);
var
ptr: LPWSTR;
s: string;
Done: boolean;
begin
Vars.Clear;
s := '';
Done := FALSE;
ptr := windows.GetEnvironmentStrings;
while Done = false do begin
if ptr^ = #0 then begin
inc(ptr);
if ptr^ = #0 then Done := TRUE
else Vars.Add(s);
s := ptr^;
end else s := s + ptr^;
inc(ptr);
end;
end;
var
Strings: TStringList;
i,j: integer;
FIni: TIniFile;
S,FDataSource,FServer,FPort,FName: string;
begin
Strings := TStringList.Create;
try
FName := '';
GetEnvironmentStrings(Strings);
for i := 0 to Strings.Count-1 do
if (Strings.Names[i] = cnstSybaseName) or (Strings.Names[i] = cnstSybaseRoot) then
begin
FName := Strings.Values[Strings.Names[i]];
break;
end;
if FName = '' then
begin
FillNothing;
Exit;
end;
FName := IncludeTrailingPathDelimiter(FName) + 'ini\' + cnstSrcSybase;
if FileExists(FName) then
begin
FIni := TIniFile.Create(FName);
try
Strings.Clear;
FIni.ReadSections(Strings);
Strings.Sorted := True;
for i := 0 to Strings.Count - 1 do
begin
FDataSource := Strings[i];
S := FIni.ReadString(FDataSource,'query','');
j := Pos(',',S);
if j > 0 then
begin
FServer := Copy(S,j+1,MaxInt);
j := Pos(',',FServer);
if j > 0 then
begin
FPort := Copy(FServer,j+1,MaxInt);
FServer := Copy(FServer,1,j-1);
end;
end;
ServerList.AddItem(Format(cnstItem,[FDataSource,FServer,FPort]),nil);
end;
finally
FIni.Free;
end;
end
else
begin
FillNothing;
Exit;
end;
finally
Strings.Free;
if ServerList.Items.Count > 0 then
begin
ServerList.ItemIndex := 0;
ServerListChange(ServerList);
end;
end;
end;
procedure TBDLoginForm.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
inherited;
if Key = VK_RETURN then
begin
OkAction.Update;
if OkAction.Enabled then OkBtn.Click;
end;
if Key = VK_ESCAPE then CancelBtn.Click;
end;
procedure TBDLoginForm.Get(var aDataSource, aServer, aPort, aLogin,
aPassword: string);
var
Strings: TStringList;
i: Integer;
begin
i := ServerList.ItemIndex;
if i >= 0 then
begin
Strings := TStringList.Create;
try
Strings.Delimiter := '|';
Strings.DelimitedText := ServerList.Items[ServerList.ItemIndex];
aDataSource := Strings[0];
aServer := Strings[1];
aPort := Strings[2];
aLogin := Login.Text;
aPassword := Password.Text;
finally
Strings.Free;
end;
end;
end;
procedure TBDLoginForm.SaveCurrentDataSourceToRegistry;
var
Options: TOptionsReg;
begin
if cbCustom.Checked then
begin
Options := TOptionsReg.Create;
try
Options.SaveConnectToString(FBDType,ServerList.Text,ServerList.Text+','+Port.Text);
finally
Options.Free;
end;
end;
end;
procedure TBDLoginForm.ServerListChange(Sender: TObject);
var
S: string;
iPos,iIndex: Integer;
begin
inherited;
iIndex := (Sender as TComboBox).ItemIndex;
if iIndex>=0 then
begin
S := (Sender as TComboBox).Items[iIndex];
iPos := Pos('|Port=',S);
if iPos > 0 then
begin
S := Copy(S,iPos+6,MaxInt);
Port.Text := S;
end
else
begin
iPos := Integer((Sender as TComboBox).Items.Objects[iIndex]);
if iPos > 0 then
Port.Text := IntToStr(iPos);
end;
end;
end;
procedure TBDLoginForm.ServerListDrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
var
Bitmap: TBitmap;
Offset: Integer;
S: string;
begin
with (Control as TComboBox).Canvas do { Draw on control canvas, not on the form. }
begin
S := (Control as TComboBox).Items[Index];
FillRect(Rect); { Clear the rectangle. }
Offset := 2; { Provide default offset. }
if Pos('|',S) = 0 then
Offset := 6
else
begin
Bitmap := Image1.Picture.Bitmap;
if Bitmap <> nil then
begin
//DrawIcon(Handle, Rect.Left + Offset, Rect.Top, Icon.Handle);
BrushCopy(
Bounds(Rect.Left + Offset, Rect.Top+2, Bitmap.Width, Bitmap.Height),
Bitmap,
Bounds(0, 0, Bitmap.Width, Bitmap.Height),
clWhite); {render bitmap}
Offset := Bitmap.width + 6; { Add four pixels between bitmap and text. }
end;
S := Copy(S,1,Pos('|',S)-1);
end;
TextOut(Rect.Left + Offset, Rect.Top+1, S);
end;
end;
procedure TBDLoginForm.OkActionExecute(Sender: TObject);
begin
OkBtn.Enabled := False;
Self.Refresh;
end;
procedure TBDLoginForm.OkActionUpdate(Sender: TObject);
begin
OkAction.Enabled := (Login.Text <> '') and
(Password.Text <> '') and
(Port.Text <> '') and
(ServerList.Text <> '');
end;
procedure TBDLoginForm.OkPanelClick(Sender: TObject);
begin
OkAction.Update;
if OkAction.Enabled then OkBtn.Click;
end;
end.