1 | unit ULoaderForm;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
|
---|
7 | Dialogs, ComCtrls, StdCtrls, Menus, UHostAddressList;
|
---|
8 |
|
---|
9 | type
|
---|
10 | TLoaderForm = class(TForm)
|
---|
11 | Label1: TLabel;
|
---|
12 | Edit1: TEdit;
|
---|
13 | Label2: TLabel;
|
---|
14 | Edit2: TEdit;
|
---|
15 | Label3: TLabel;
|
---|
16 | ButtonConnect: TButton;
|
---|
17 | CheckBox1: TCheckBox;
|
---|
18 | CheckBox2: TCheckBox;
|
---|
19 | CheckBox3: TCheckBox;
|
---|
20 | Label4: TLabel;
|
---|
21 | Edit4: TEdit;
|
---|
22 | Edit3: TEdit;
|
---|
23 | ListView1: TListView;
|
---|
24 | ButtonSave: TButton;
|
---|
25 | ButtonRemove: TButton;
|
---|
26 | ButtonTools: TButton;
|
---|
27 | PopupMenu1: TPopupMenu;
|
---|
28 | OpenDialog1: TOpenDialog;
|
---|
29 | SaveDialog1: TSaveDialog;
|
---|
30 | RemoveAllAddresses1: TMenuItem;
|
---|
31 | ClearCache1: TMenuItem;
|
---|
32 | N1: TMenuItem;
|
---|
33 | ExportAddresses1: TMenuItem;
|
---|
34 | ImportAddresses1: TMenuItem;
|
---|
35 | procedure FormCreate(Sender: TObject);
|
---|
36 | procedure ExportAddresses1Click(Sender: TObject);
|
---|
37 | procedure FormDestroy(Sender: TObject);
|
---|
38 | procedure ListView1SelectItem(Sender: TObject; Item: TListItem;
|
---|
39 | Selected: Boolean);
|
---|
40 | procedure ListView1Data(Sender: TObject; Item: TListItem);
|
---|
41 | procedure ButtonRemoveClick(Sender: TObject);
|
---|
42 | procedure ButtonSaveClick(Sender: TObject);
|
---|
43 | procedure ButtonToolsClick(Sender: TObject);
|
---|
44 | procedure ImportAddresses1Click(Sender: TObject);
|
---|
45 | procedure RemoveAllAddresses1Click(Sender: TObject);
|
---|
46 | procedure ButtonConnectClick(Sender: TObject);
|
---|
47 | private
|
---|
48 | HostAddressList: THostAddressList;
|
---|
49 | RegistryKey: string;
|
---|
50 | procedure LoadFromRegistry;
|
---|
51 | procedure SaveToRegistry;
|
---|
52 | procedure UpdateListView1;
|
---|
53 | procedure LoadFormPosition(Form: TForm);
|
---|
54 | procedure SaveFormPosition(Form: TForm);
|
---|
55 | public
|
---|
56 | { Public declarations }
|
---|
57 | end;
|
---|
58 |
|
---|
59 | var
|
---|
60 | LoaderForm: TLoaderForm;
|
---|
61 |
|
---|
62 | implementation
|
---|
63 |
|
---|
64 | uses
|
---|
65 | UApplicationInfo, URegistry, Registry, IdTCPClient, IdTCPConnection,
|
---|
66 | UMainForm;
|
---|
67 |
|
---|
68 | {$R *.dfm}
|
---|
69 |
|
---|
70 | { TLoaderForm }
|
---|
71 |
|
---|
72 | procedure TLoaderForm.ButtonSaveClick(Sender: TObject);
|
---|
73 | var
|
---|
74 | I: Integer;
|
---|
75 | FoundIndex: Integer;
|
---|
76 | NewHostAddress: THostAddress;
|
---|
77 | begin
|
---|
78 | FoundIndex := -1;
|
---|
79 | for I := 0 to HostAddressList.Count - 1 do begin
|
---|
80 | if (THostAddress(HostAddressList[I]).Address = Edit1.Text) and (THostAddress(HostAddressList[I]).User = Edit2.Text) then begin
|
---|
81 | FoundIndex := I;
|
---|
82 | Break;
|
---|
83 | end;
|
---|
84 | end;
|
---|
85 | if FoundIndex = -1 then begin
|
---|
86 | NewHostAddress := THostAddress.Create;
|
---|
87 | with NewHostAddress do begin
|
---|
88 | Address := Edit1.Text;
|
---|
89 | User := Edit2.Text;
|
---|
90 | Note := Edit4.Text;
|
---|
91 | if KeepPassword then Password := Edit3.Text
|
---|
92 | else Password := '';
|
---|
93 | KeepPassword := CheckBox1.Checked;
|
---|
94 | SecureMode := CheckBox2.Checked;
|
---|
95 | end;
|
---|
96 | HostAddressList.Add(NewHostAddress);
|
---|
97 | end else begin
|
---|
98 | with THostAddress(HostAddressList[FoundIndex]) do begin
|
---|
99 | if KeepPassword then Password := Edit3.Text
|
---|
100 | else Password := '';
|
---|
101 | Note := Edit4.Text;
|
---|
102 | KeepPassword := CheckBox1.Checked;
|
---|
103 | SecureMode := CheckBox2.Checked;
|
---|
104 | end;
|
---|
105 | end;
|
---|
106 | UpdateListView1;
|
---|
107 | end;
|
---|
108 |
|
---|
109 | procedure TLoaderForm.ButtonToolsClick(Sender: TObject);
|
---|
110 | var
|
---|
111 | PopupPoint: TPoint;
|
---|
112 | begin
|
---|
113 | PopupPoint := Point(ButtonTools.Left, ButtonTools.Top + ButtonTools.Height);
|
---|
114 | PopupPoint := ClientToScreen(PopupPoint);
|
---|
115 | PopupMenu1.Popup(PopupPoint.X, PopupPoint.Y);
|
---|
116 | end;
|
---|
117 |
|
---|
118 | procedure TLoaderForm.ButtonConnectClick(Sender: TObject);
|
---|
119 | begin
|
---|
120 | MainForm.Show;
|
---|
121 | MainForm.RouterOS.Connect(Edit1.Text, Edit2.Text, Edit3.Text);
|
---|
122 | Hide;
|
---|
123 | end;
|
---|
124 |
|
---|
125 | procedure TLoaderForm.ButtonRemoveClick(Sender: TObject);
|
---|
126 | begin
|
---|
127 | if Assigned(ListView1.Selected) then begin
|
---|
128 | HostAddressList.Delete(ListView1.Selected.Index);
|
---|
129 | UpdateListView1;
|
---|
130 | end;
|
---|
131 | end;
|
---|
132 |
|
---|
133 | procedure TLoaderForm.ExportAddresses1Click(Sender: TObject);
|
---|
134 | begin
|
---|
135 | if SaveDialog1.Execute then begin
|
---|
136 | HostAddressList.SaveToXML(SaveDialog1.FileName);
|
---|
137 | end;
|
---|
138 | end;
|
---|
139 |
|
---|
140 | procedure TLoaderForm.FormCreate(Sender: TObject);
|
---|
141 | begin
|
---|
142 | HostAddressList := THostAddressList.Create;
|
---|
143 | RegistryKey := '\Software\' + ApplicationInfo.CompanyName + '\' + ApplicationInfo.Name;
|
---|
144 | LoadFromRegistry;
|
---|
145 | UpdateListView1;
|
---|
146 | end;
|
---|
147 |
|
---|
148 | procedure TLoaderForm.FormDestroy(Sender: TObject);
|
---|
149 | begin
|
---|
150 | SaveToRegistry;
|
---|
151 | HostAddressList.Free;
|
---|
152 | end;
|
---|
153 |
|
---|
154 | procedure TLoaderForm.ImportAddresses1Click(Sender: TObject);
|
---|
155 | begin
|
---|
156 | if OpenDialog1.Execute then begin
|
---|
157 | HostAddressList.LoadFromXML(OpenDialog1.FileName);
|
---|
158 | UpdateListView1;
|
---|
159 | end;
|
---|
160 | end;
|
---|
161 |
|
---|
162 | procedure TLoaderForm.ListView1Data(Sender: TObject; Item: TListItem);
|
---|
163 | begin
|
---|
164 | if Item.Index < HostAddressList.Count then
|
---|
165 | with Item, THostAddress(HostAddressList[Item.Index]) do begin
|
---|
166 | Caption := Address;
|
---|
167 | with SubItems do begin
|
---|
168 | Add(User);
|
---|
169 | Add(Note);
|
---|
170 | end;
|
---|
171 | end;
|
---|
172 | end;
|
---|
173 |
|
---|
174 | procedure TLoaderForm.ListView1SelectItem(Sender: TObject; Item: TListItem;
|
---|
175 | Selected: Boolean);
|
---|
176 | begin
|
---|
177 | ButtonRemove.Enabled := Assigned(ListView1.Selected);
|
---|
178 | if Assigned(ListView1.Selected) then
|
---|
179 | with THostAddress(HostAddressList[ListView1.Selected.Index]) do begin
|
---|
180 | Edit1.Text := Address;
|
---|
181 | Edit2.Text := User;
|
---|
182 | Edit3.Text := Password;
|
---|
183 | Edit4.Text := Note;
|
---|
184 | CheckBox1.Checked := KeepPassword;
|
---|
185 | CheckBox2.Checked := SecureMode;
|
---|
186 | end;
|
---|
187 | end;
|
---|
188 |
|
---|
189 | procedure TLoaderForm.LoadFormPosition(Form: TForm);
|
---|
190 | begin
|
---|
191 | with Form, TRegistryEx.Create do
|
---|
192 | try
|
---|
193 | RootKey := HKEY_CURRENT_USER;
|
---|
194 | OpenKey(RegistryKey + '\Forms\' + Form.Name, True);
|
---|
195 | Width := ReadIntegerWithDefault('Width', Width);
|
---|
196 | Height := ReadIntegerWithDefault('Height', Height);
|
---|
197 | Top := ReadIntegerWithDefault('Top', (Screen.Height - Height) div 2);
|
---|
198 | Left := ReadIntegerWithDefault('Left', (Screen.Width - Width) div 2);
|
---|
199 | if Left < 0 then Left := 0;
|
---|
200 | if Left > (Screen.Width - 50) then Left := Screen.Width - 50;
|
---|
201 | if Top < 0 then Top := 0;
|
---|
202 | if Top > (Screen.Height - 50) then Top := Screen.Height - 50;
|
---|
203 | if ReadBoolWithDefault('Maximized', False) then begin
|
---|
204 | WindowState := wsMaximized;
|
---|
205 | end;
|
---|
206 | finally
|
---|
207 | Free;
|
---|
208 | end;
|
---|
209 | end;
|
---|
210 |
|
---|
211 | procedure TLoaderForm.LoadFromRegistry;
|
---|
212 | begin
|
---|
213 | with TRegistryEx.Create do try
|
---|
214 | RootKey := HKEY_CURRENT_USER;
|
---|
215 | OpenKey(RegistryKey, True);
|
---|
216 | CheckBox3.Checked := ReadBoolWithDefault('LoadPreviousSession', True);
|
---|
217 | CheckBox2.Checked := ReadBoolWithDefault('SecureMode', True);
|
---|
218 | CheckBox1.Checked := ReadBoolWithDefault('KeepPassword', False);
|
---|
219 | if CheckBox3.Checked then begin
|
---|
220 | Edit1.Text := ReadStringWithDefault('Address', '');
|
---|
221 | Edit2.Text := ReadStringWithDefault('User', '');
|
---|
222 | Edit3.Text := ReadStringWithDefault('Password', '');
|
---|
223 | Edit4.Text := ReadStringWithDefault('Note', '');
|
---|
224 | end else
|
---|
225 | Edit2.Text := 'admin';
|
---|
226 | finally
|
---|
227 | Free;
|
---|
228 | end;
|
---|
229 | HostAddressList.LoadFromRegistry(RegistryKey + '\HostAddressList');
|
---|
230 | LoadFormPosition(Self);
|
---|
231 | end;
|
---|
232 |
|
---|
233 | procedure TLoaderForm.RemoveAllAddresses1Click(Sender: TObject);
|
---|
234 | begin
|
---|
235 | HostAddressList.Clear;
|
---|
236 | UpdateListView1;
|
---|
237 | end;
|
---|
238 |
|
---|
239 | procedure TLoaderForm.SaveFormPosition(Form: TForm);
|
---|
240 | var
|
---|
241 | Pl : TWindowPlacement; // used for API call
|
---|
242 | R: TRect; // used for wdw pos
|
---|
243 | begin
|
---|
244 | {Calculate window's normal size and position using
|
---|
245 | Windows API call - the form's Width, Height, Top and
|
---|
246 | Left properties will give maximized window size if
|
---|
247 | form is maximised, which is not what we want here}
|
---|
248 | Pl.Length := SizeOf(TWindowPlacement);
|
---|
249 | GetWindowPlacement(Form.Handle, @Pl);
|
---|
250 | R := Pl.rcNormalPosition;
|
---|
251 |
|
---|
252 | with Form, TRegistryEx.Create do
|
---|
253 | try
|
---|
254 | RootKey := HKEY_CURRENT_USER;
|
---|
255 | OpenKey(RegistryKey + '\Forms\' + Form.Name, True);
|
---|
256 | WriteInteger('Width', R.Right - R.Left);
|
---|
257 | WriteInteger('Height', R.Bottom - R.Top);
|
---|
258 | WriteInteger('Top', R.Top);
|
---|
259 | WriteInteger('Left', R.Left);
|
---|
260 | WriteBool('Maximized', WindowState = wsMaximized);
|
---|
261 | finally
|
---|
262 | Free;
|
---|
263 | end;
|
---|
264 | end;
|
---|
265 |
|
---|
266 | procedure TLoaderForm.SaveToRegistry;
|
---|
267 | begin
|
---|
268 | with TRegistryEx.Create do try
|
---|
269 | RootKey := HKEY_CURRENT_USER;
|
---|
270 | OpenKey(RegistryKey, True);
|
---|
271 |
|
---|
272 | WriteString('Address', Edit1.Text);
|
---|
273 | WriteString('User', Edit2.Text);
|
---|
274 | WriteString('Note', Edit4.Text);
|
---|
275 | WriteBool('KeepPassword', CheckBox1.Checked);
|
---|
276 | if CheckBox1.Checked then WriteString('Password', Edit3.Text)
|
---|
277 | else WriteString('Password', '');
|
---|
278 | WriteBool('SecureMode', CheckBox2.Checked);
|
---|
279 | WriteBool('LoadPreviouisSession', CheckBox3.Checked);
|
---|
280 | finally
|
---|
281 | Free;
|
---|
282 | end;
|
---|
283 | HostAddressList.SaveToRegistry(RegistryKey + '\HostAddressList');
|
---|
284 | SaveFormPosition(Self);
|
---|
285 | end;
|
---|
286 |
|
---|
287 | procedure TLoaderForm.UpdateListView1;
|
---|
288 | begin
|
---|
289 | with ListView1, Items do begin
|
---|
290 | Count := HostAddressList.Count;
|
---|
291 | Refresh;
|
---|
292 | end;
|
---|
293 | end;
|
---|
294 |
|
---|
295 | end.
|
---|