-
Notifications
You must be signed in to change notification settings - Fork 3
/
PrFormUnit.pas
333 lines (289 loc) · 8.79 KB
/
PrFormUnit.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
unit PrFormUnit;
interface
uses
Winapi.Windows, Winapi.Messages, System.StrUtils, System.SysUtils,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms,
Vcl.Dialogs, nppplugin, NppForms, System.Win.Registry,
NppDockingForms, Vcl.StdCtrls, ConstUnit, Vcl.Clipbrd,
Buttons, ExtCtrls, ActnList, Vcl.Menus, System.Actions,
{$IFNDEF NPPCONNECTIONS}diaConstUnit,{$ENDIF}
CustomDialogUnit;
type
TPrForm = class(TCustomDialogForm)
PopupMenu1: TPopupMenu;
N1: TMenuItem;
N2: TMenuItem;
N3: TMenuItem;
N4: TMenuItem;
HistoryPopupMenu: TPopupMenu;
Password: TEdit;
cmdEdit: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
HistorySpeedButton: TSpeedButton;
SpeedButton1: TSpeedButton;
PathEdit: TEdit;
procedure FormCreate(Sender: TObject);
procedure PasswordChange(Sender: TObject);
procedure SpeedButton1Click(Sender: TObject);
procedure OkBtnClick(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure HistorySpeedButtonClick(Sender: TObject);
private
FPathHistory: TStringList;
FExternalServer: String;
FExternalBase: String;
FPath: String;
FÑonnectString: String;
FExternalPort: String;
function GetFileName: String;
function GetTargetPath: TPathName;
function GetFullFileName: String;
procedure SaveSettings;
procedure RestoreSettings;
procedure SetExternalServer(const Value: String);
procedure SetExternalBase(const Value: String);
function GetConnectString: String;
procedure SetExternalPort(const Value: String);
protected
procedure OnClickHistoryPopupMenu(Sender: TObject);
procedure OnClearHistoryPopupMenu(Sender: TObject);
procedure ChangeColorMode(Sender: TObject); override;
property TargetPath: TPathName read GetTargetPath;
property FullFileName: String read GetFullFileName;
public
function DoForm: TModalResult; override;
procedure InitConnectionString;
property FileName: String read GetFileName;
property ConnectString: String read GetConnectString;
property Path: String read FPath;
property ExternalServer: String read FExternalServer write SetExternalServer;
property ExternalPort: String read FExternalPort write SetExternalPort;
property ExternalBase: String read FExternalBase write SetExternalBase;
end;
implementation
uses GetFolderDialogUnit;
{$R *.dfm}
procedure TPrForm.FormCreate(Sender: TObject);
begin
inherited;
FPathHistory := TStringList.Create;
end;
function TPrForm.GetConnectString: String;
begin
Result := FÑonnectString;
end;
function TPrForm.GetFileName: String;
begin
Npp.GetFileName(Result);
end;
function TPrForm.GetFullFileName: String;
begin
Npp.GetFullFileName(Result);
end;
procedure TPrForm.InitConnectionString;
begin
if Password.Text = '' then Password.Text := 'dca 123456';
FÑonnectString := ChangeFileExt(FileName,'');
FÑonnectString := FÑonnectString + ' ' + FExternalServer;
if FExternalPort <> '' then {Äëÿ PostgreSQL}
FÑonnectString := FÑonnectString + ':' + FExternalPort;
FÑonnectString := FÑonnectString + ' ' + FExternalBase;
FÑonnectString := Trim(FÑonnectString + ' ' + Password.Text);
cmdEdit.Text := '..\serv ' + FÑonnectString;
end;
procedure TPrForm.PasswordChange(Sender: TObject);
begin
inherited;
InitConnectionString;
end;
procedure TPrForm.SpeedButton1Click(Sender: TObject);
var sFolder: String;
begin
sFolder:= 'c:\';
if GetFolderDialog(Application.Handle, Label3.Caption, sFolder) then
begin
PathEdit.Text := sFolder;
end;
end;
procedure TPrForm.OkBtnClick(Sender: TObject);
function CopyFileTo(const Source,Target: TPathName): boolean;
begin
Result := True;
if Source <> Target then
begin
if FileExists(Target) then
begin
if FileIsReadOnly(Target) then FileSetReadOnly(Target,False);
end;
Result := CopyFile(nppPChar(Source),nppPChar(Target),False);
end;
end;
var Target: TPathName;
Succ: boolean;
begin
inherited;
Self.Npp.SaveCurrentFile;
SaveSettings;
FPath := TargetPath;
if FPath = '' then
begin
FPath := ExtractFilePath(FullFileName);
Succ := True;
end
else
begin
Target := IncludeTrailingPathDelimiter(FPath) + FileName;
Succ := CopyFileTo(FullFileName,Target);
if not Succ then MessageError(Format(cnstFileCopyError,[Target]),cnstFileCopyErrorCaption);
end;
ModalResult := cnstModalResultArray[Succ];
end;
function TPrForm.GetTargetPath: TPathName;
begin
Result := PathEdit.Text;
end;
procedure TPrForm.HistorySpeedButtonClick(Sender: TObject);
var aPoint: TPoint;
i: integer;
NewItem: TMenuItem;
begin
inherited;
HistoryPopupMenu.Items.Clear;
for i := 0 to FPathHistory.Count - 1 do
begin
NewItem := TMenuItem.Create(Self);
NewItem.Caption := FPathHistory[i];
NewItem.Hint := FPathHistory[i];
NewItem.OnClick := OnClickHistoryPopupMenu;
HistoryPopupMenu.Items.Add(NewItem);
end;
if FPathHistory.Count > 3 then
begin
NewItem := TMenuItem.Create(Self);
NewItem.Caption := '-';
HistoryPopupMenu.Items.Add(NewItem);
NewItem := TMenuItem.Create(Self);
NewItem.Caption := cnstPrClearMenuItemCaption;
NewItem.OnClick := OnClearHistoryPopupMenu;
HistoryPopupMenu.Items.Add(NewItem);
end;
aPoint := PathEdit.ClientToScreen(Point(PathEdit.Width,HistorySpeedButton.Height));
HistoryPopupMenu.Popup(aPoint.X,aPoint.Y);
end;
procedure TPrForm.ChangeColorMode(Sender: TObject);
begin
inherited;
if DarkMode then
begin
Password.BorderStyle := bsNone;
cmdEdit.BorderStyle := bsNone;
PathEdit.BorderStyle := bsNone;
end;
end;
function TPrForm.DoForm: TModalResult;
begin
RestoreSettings;
InitConnectionString;
Result := inherited DoForm;
end;
procedure TPrForm.SaveSettings;
var Registry: TRegistry;
i: integer;
S: TPathName;
begin
inherited;
Registry := TRegistry.Create(KEY_ALL_ACCESS);
try
Registry.RootKey := HKEY_CURRENT_USER;
if not Registry.KeyExists(cnstDllKey) then Registry.CreateKey(cnstDllKey);
if Registry.OpenKey(cnstDllKey + '\' + cnstPrFormKey, True) then
begin
Registry.WriteString('Password',Trim(Password.Text));
Registry.WriteString('PathMemo',Trim(PathEdit.Text));
Registry.CloseKey;
end;
S := Trim(PathEdit.Text);
if (S<>'') and (FPathHistory.IndexOf(S)<0) then FPathHistory.Add(S);
if FPathHistory.Count = 0 then
Registry.DeleteKey(cnstDllKey + '\' + cnstPrHistKey)
else
if Registry.OpenKey(cnstDllKey + '\' + cnstPrHistKey, True) then
begin
for i := 0 to FPathHistory.Count - 1 do
Registry.WriteString('Item' + IntToStr(i),FPathHistory[i]);
Registry.CloseKey;
end;
finally
Registry.Free;
end;
end;
procedure TPrForm.RestoreSettings;
var Registry: TRegistry;
Server,Base: String;
i: integer;
begin
Registry := TRegistry.Create(KEY_READ);
try
Registry.RootKey := HKEY_CURRENT_USER;
if Registry.OpenKey(cnstDllKey + '\' + cnstPrFormKey, False) then
begin
Password.Text := Registry.ReadString('Password');
PathEdit.Text := Registry.ReadString('PathMemo');
Registry.CloseKey;
end;
if Registry.OpenKey(cnstDllKey + '\' + cnstPrHistKey, False) then
begin
Registry.GetValueNames(FPathHistory);
for i := 0 to FPathHistory.Count - 1 do
FPathHistory[i] := Registry.ReadString(FPathHistory[i]);
Registry.CloseKey;
end;
finally
Registry.Free;
end;
end;
procedure TPrForm.FormDestroy(Sender: TObject);
begin
inherited;
FPathHistory.Free;
end;
procedure TPrForm.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key = VK_RETURN then OkBtn.Click;
if Key = VK_ESCAPE then CancelBtn.Click;
end;
procedure TPrForm.OnClickHistoryPopupMenu(Sender: TObject);
begin
PathEdit.Text := TMenuItem(Sender).Hint;
end;
procedure TPrForm.OnClearHistoryPopupMenu(Sender: TObject);
var
Registry : TRegistry;
begin
HistoryPopupMenu.Items.Clear;
FPathHistory.Clear;
Registry := TRegistry.Create(KEY_ALL_ACCESS);
try
Registry.RootKey := HKEY_CURRENT_USER;
Registry.DeleteKey(cnstDllKey + '\' + cnstPrHistKey);
finally
Registry.Free;
end;
end;
procedure TPrForm.SetExternalServer(const Value: String);
begin
FExternalServer := Value;
end;
procedure TPrForm.SetExternalBase(const Value: String);
begin
FExternalBase := Value;
end;
procedure TPrForm.SetExternalPort(const Value: String);
begin
FExternalPort := Value;
end;
end.