-
Notifications
You must be signed in to change notification settings - Fork 1
/
Mainform.pas
356 lines (323 loc) · 10.1 KB
/
Mainform.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
unit Mainform;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, ValEdit, StdCtrls, ComCtrls, cxGraphics, cxControls,
cxLookAndFeels, cxLookAndFeelPainters, cxContainer, cxEdit, cxTextEdit,
cxMemo, cxStyles, cxVGrid, cxInplaceContainer, cxCheckBox, cxDropDownEdit,
Menus, cxButtons, cxExportVGLink, cxListView, cefvcl, cxCustomData, cxTL,
cxTLdxBarBuiltInMenu, cxButtonEdit, cxMaskEdit, Buttons, ExtCtrls,
acClasses, acCaptionButton, acTrayIcon, AppEvnts, HotKeyManager,
LMDCustomComponent, LMDObjectStorage;
type
THostmanForm = class(TForm)
cxTreeList1: TcxTreeList;
cxTreeList1ColumnIP: TcxTreeListColumn;
cxTreeList1ColumnHost: TcxTreeListColumn;
PopupMenu1: TPopupMenu;
Add1: TMenuItem;
Delete1: TMenuItem;
cxTreeList1ColumnComment: TcxTreeListColumn;
cxTreeList1ColumnEnable: TcxTreeListColumn;
AddSubitem1: TMenuItem;
Bevel1: TBevel;
ExitButton: TBitBtn;
ApplyButton: TBitBtn;
Saveas1: TMenuItem;
N1: TMenuItem;
Load1: TMenuItem;
AboutButton: TBitBtn;
EditHostsButton: TButton;
acTrayIcon1: TacTrayIcon;
PopupMenuTray: TPopupMenu;
E1: TMenuItem;
Aboutt1: TMenuItem;
ApplicationEvents1: TApplicationEvents;
Apply1: TMenuItem;
SaveButton: TButton;
HotKeyManager1: THotKeyManager;
HotKey1: THotKey;
ObjectStorage1: TLMDObjectStorage;
Label1: TLabel;
Label2: TLabel;
HotKey2: THotKey;
procedure Add1Click(Sender: TObject);
procedure Delete1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure ExitButtonClick(Sender: TObject);
procedure ApplyButtonClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure AddSubitem1Click(Sender: TObject);
procedure Saveas1Click(Sender: TObject);
procedure Load1Click(Sender: TObject);
procedure cxTreeList1DblClick(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure AboutButtonClick(Sender: TObject);
procedure EditHostsButtonClick(Sender: TObject);
procedure acTrayIcon1DblClick(Sender: TObject);
procedure ApplicationEvents1Minimize(Sender: TObject);
procedure cxTreeList1Edited(Sender: TcxCustomTreeList;
AColumn: TcxTreeListColumn);
procedure PopupMenu1Popup(Sender: TObject);
procedure SaveButtonClick(Sender: TObject);
procedure HotKeyManager1HotKeyPressed(HotKey: Cardinal; Index: Word);
procedure HotKey1Exit(Sender: TObject);
private
function generateHostFile():TStringList;
procedure adjustListViewHeader();
procedure parseHostFile();
procedure AddHotkey();
public
{ Public declarations }
end;
var
HostmanForm: THostmanForm;
prevHotKey:Cardinal;
const
hostPath = 'C:\Windows\System32\drivers\etc\hosts';
hostPathEscaped = 'C:\\Windows\\System32\\drivers\\etc\\hosts';
implementation
uses RegExpr, ShellAPI;
{$R *.dfm}
procedure THostmanForm.parseHostFile();
var matched,lines:TStringList;
line:string;
i:integer;
begin
lines := TStringList.Create();
lines.LoadFromFile(hostPathEscaped);
for i := 0 to lines.Count -1 do begin
line := lines[i];
if not ExecRegExpr('^\s*#',line) then begin
matched := TStringList.Create;
SplitRegExpr('(\s+)|(\s+#)',line,matched);
line := matched.Text;
if matched.Count>= 2 then begin
with cxTreeList1.Root.AddChild do begin
Texts[0] := matched[0];
Texts[1] := matched[1];
if matched.Count > 2 then Texts[2] := StringReplace(matched[2],'#','',[]);
end;
end;
end
end;
end;
procedure THostmanForm.Add1Click(Sender: TObject);
var node:TcxTreeListNode;
begin
node := cxTreeList1.Root.AddChild;
node.Selected := True;
end;
procedure THostmanForm.Delete1Click(Sender: TObject);
begin
cxTreeList1.Selections[0].Delete;
end;
procedure THostmanForm.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
cxTreeList1.SaveToFile('default.host');
Action := caNone;
acTrayIcon1.MinimizeToTray;
end;
procedure THostmanForm.ExitButtonClick(Sender: TObject);
begin
cxTreeList1.SaveToFile('default.host');
ObjectStorage1.Save;
Application.Terminate;
end;
function THostmanForm.generateHostFile():TStringList;
var sl:TStringList;
i,j:Integer;
node:TcxTreeListNode;
host, comment,ip: string;
function fn(ip,host,comment:string; commentout:string = 'false'):string;
var fmt:string;
begin
if CompareText(commentout,'false') = 0 then begin
fmt := '#%s %s #%s';
end else begin
fmt := '%s %s #%s';
end;
Result := Format(fmt,[ip,host,comment]);
end;
begin
sl := TStringList.Create;
sl.Append('#Generated by Hostman');
for i := 0 to cxTreeList1.Root.Count - 1 do
begin
node := cxTreeList1.Root.Items[i];
ip := node.Texts[0];
sl.Append(fn(ip,node.Texts[1],node.Texts[2],node.Texts[3]));
for j:=0 to node.Count -1 do
begin
host := node.Items[j].Texts[1];
comment := node.Items[j].Texts[2];
sl.Append(fn(ip,host,comment,node.Items[j].Texts[3]));
end;
end;
Result := sl;
end;
procedure THostmanForm.ApplyButtonClick(Sender: TObject);
var sl :TStringList;
begin
if Application.MessageBox(PChar(Format('Overwrite %s ?',[hostPath])),
'Warnning', MB_OKCANCEL + MB_ICONQUESTION) = IDOK then begin
sl := self.generateHostFile();
try
sl.SaveToFile(hostPathEscaped);
cxTreeList1.SaveToFile('default.host');
Application.MessageBox(PChar('Done!'), 'Done', MB_OK +
MB_ICONINFORMATION);
except
Application.MessageBox('Overwrite '+ hostPath +' Failed!',
'Error', MB_OKCANCEL + MB_ICONSTOP);
end;
sl.Free;
end
end;
procedure THostmanForm.adjustListViewHeader();
var w : integer;
begin
cxTreeList1ColumnIP.ApplyBestFit;
cxTreeList1ColumnEnable.ApplyBestFit;
w := (cxTreeList1.Width - cxTreeList1ColumnIP.Width - cxTreeList1ColumnEnable.Width - 2 ) div 2;
cxTreeList1ColumnHost.Width := w;
cxTreeList1ColumnComment.Width := w;
end;
procedure THostmanForm.FormCreate(Sender: TObject);
begin
adjustListViewHeader;
if(FileExists('default.host')) then begin
cxTreeList1.LoadFromFile('default.host')
end else begin
parseHostFile();
end;
prevHotKey := 0;
ObjectStorage1.Load;
AddHotkey;
end;
procedure THostmanForm.AddSubitem1Click(Sender: TObject);
var node,child : TcxTreeListNode;
begin
node := cxTreeList1.Selections[0];
child := node.AddChild;
node.Expand(True);
child.Selected := true;
end;
procedure THostmanForm.Saveas1Click(Sender: TObject);
var dlg:TSaveDialog;
begin
dlg := TSaveDialog.Create(self);
dlg.InitialDir := ExtractFileDir(Application.ExeName);
dlg.Filter := '*.host|*.*';
dlg.DefaultExt := 'host';
if dlg.Execute then begin
try
cxTreeList1.SaveToFile(ChangeFileExt(dlg.FileName,'.host'));
except
Application.MessageBox('Save Host Settings Failed!', 'Error', MB_OK +
MB_ICONSTOP);
end
end;
end;
procedure THostmanForm.Load1Click(Sender: TObject);
var dlg: TOpenDialog;
begin
dlg := TOpenDialog.Create(self);
dlg.InitialDir := ExtractFileDir(Application.ExeName);
dlg.DefaultExt := 'host';
dlg.Filter := '*.host|*.*';
if dlg.Execute then begin
try
cxTreeList1.LoadFromFile(dlg.FileName);
except
Application.MessageBox('Load Host Settings Failed!', 'Error', MB_OK +
MB_ICONSTOP);
end
end;
end;
procedure THostmanForm.cxTreeList1DblClick(Sender: TObject);
begin
cxTreeList1.OptionsBehavior.ImmediateEditor := True;
end;
procedure THostmanForm.FormResize(Sender: TObject);
begin
adjustListViewHeader;
end;
procedure THostmanForm.AboutButtonClick(Sender: TObject);
begin
Application.MessageBox('Hostman' + #13#10 + ' Hosts file manager' + #13#10 +
' A NuTs Gizmo', 'About', MB_OK + MB_ICONINFORMATION);
end;
procedure THostmanForm.EditHostsButtonClick(Sender: TObject);
var a :Cardinal;
begin
ShellExecute(Application.Handle,'open','c:\\windows\\system32\\drivers\\etc\\hosts',0,nil,SW_SHOW);
a := GetLastError;
if a <> 0 then begin
ShellExecute(Application.Handle,'open','notepad','c:\\windows\\system32\\drivers\\etc\\hosts',nil,SW_SHOW);
end;
end;
procedure THostmanForm.acTrayIcon1DblClick(Sender: TObject);
begin
HostmanForm.Show;
end;
procedure THostmanForm.ApplicationEvents1Minimize(Sender: TObject);
begin
acTrayIcon1.MinimizeToTray;
end;
procedure THostmanForm.cxTreeList1Edited(Sender: TcxCustomTreeList;
AColumn: TcxTreeListColumn);
begin
cxTreeList1.OptionsBehavior.ImmediateEditor := False;
end;
procedure THostmanForm.PopupMenu1Popup(Sender: TObject);
begin
if cxTreeList1.SelectionCount = 1 then begin
AddSubitem1.Enabled := (cxTreeList1.Selections[0].Parent = cxTreeList1.Root);
end;
end;
procedure THostmanForm.SaveButtonClick(Sender: TObject);
begin
try
cxTreeList1.SaveToFile('default.host');
Application.MessageBox('Done!', 'About', MB_OK + MB_ICONINFORMATION);
except
Application.MessageBox('Save Failed!', 'About', MB_OK + MB_ICONSTOP);
end
end;
procedure THostmanForm.AddHotkey();
var hk1,hk2:Cardinal;
begin
try
hk1 := HotKey1.HotKey;
hk2 := HotKey2.HotKey;
HotKeyManager1.ClearHotKeys;
HotKeyManager1.AddHotKey(hk1);
HotKeyManager1.AddHotKey(hk2);
except
Application.MessageBox('Register Hotkey Failed!', 'Error', MB_OK +
MB_ICONSTOP);
end;
end;
procedure THostmanForm.HotKeyManager1HotKeyPressed(HotKey: Cardinal;
Index: Word);
var sl :TStringList;
begin
if HotKey = HotKey1.HotKey then begin
if self.Visible then acTrayIcon1.MinimizeToTray else acTrayIcon1.RestoreFromTray;
end else if HotKey = HotKey2.HotKey then begin
try
sl := self.generateHostFile();
sl.SaveToFile(hostPathEscaped);
except
Application.MessageBox('Overwrite '+ hostPath +' Failed!',
'Error', MB_OKCANCEL + MB_ICONSTOP);
end;
end;
end;
procedure THostmanForm.HotKey1Exit(Sender: TObject);
begin
AddHotkey();
end;
end.