1 | Unit MainFormUnit;
|
---|
2 |
|
---|
3 | Interface
|
---|
4 |
|
---|
5 | Uses
|
---|
6 | Classes, Forms, Graphics, StdCtrls, Buttons, MultiColumnListBox, Dialogs;
|
---|
7 |
|
---|
8 | const
|
---|
9 | AppVersion = 'V1.1.1'; // $SS_REQUIRE_NEW_VERSION$
|
---|
10 |
|
---|
11 | type
|
---|
12 | TProgramType =
|
---|
13 | (
|
---|
14 | idBrowser,
|
---|
15 | idMail,
|
---|
16 | idNews,
|
---|
17 | idFTP,
|
---|
18 | idIRC
|
---|
19 | );
|
---|
20 |
|
---|
21 | Type
|
---|
22 | TMainForm = Class (TForm)
|
---|
23 | PathEdit: TEdit;
|
---|
24 | BrowsePathButton: TButton;
|
---|
25 | SelectDirectoryButton: TButton;
|
---|
26 | WorkingDirectoryEdit: TEdit;
|
---|
27 | ItemsListbox: TMultiColumnListBox;
|
---|
28 | ApplyButton: TButton;
|
---|
29 | CloseButton: TButton;
|
---|
30 | PathLabel: TLabel;
|
---|
31 | WorkingDirectoryLabel: TLabel;
|
---|
32 | OpenDialog: TSystemOpenDialog;
|
---|
33 | ChangeDirDialog: TChangeDirDialog;
|
---|
34 | RestoreButton: TButton;
|
---|
35 | ParametersEdit: TEdit;
|
---|
36 | ParametersLabel: TLabel;
|
---|
37 | VersionLabel: TLabel;
|
---|
38 | Procedure ParametersEditOnChange (Sender: TObject);
|
---|
39 | Procedure RestoreButtonOnClick (Sender: TObject);
|
---|
40 | Procedure MainFormOnCloseQuery (Sender: TObject; Var CanClose: Boolean);
|
---|
41 | Procedure WorkingDirectoryEditOnChange (Sender: TObject);
|
---|
42 | Procedure ApplyButtonOnClick (Sender: TObject);
|
---|
43 | Procedure SelectDirectoryButtonOnClick (Sender: TObject);
|
---|
44 | Procedure BrowsePathButtonOnClick (Sender: TObject);
|
---|
45 | Procedure PathEditOnChange (Sender: TObject);
|
---|
46 | Procedure ItemsListboxOnItemFocus (Sender: TObject; Index: LongInt);
|
---|
47 | Procedure MainFormOnCreate (Sender: TObject);
|
---|
48 | Private
|
---|
49 | {Insert private declarations here}
|
---|
50 | FLastItem: TProgramType;
|
---|
51 | FLastItemSet: boolean;
|
---|
52 | Displaying: boolean;
|
---|
53 | Changed: boolean;
|
---|
54 | Public
|
---|
55 | {Insert public declarations here}
|
---|
56 | Function SelectedType: TProgramType;
|
---|
57 | Procedure StoreEdits( P: TProgramType );
|
---|
58 | Procedure LoadCurrentSettings;
|
---|
59 | End;
|
---|
60 |
|
---|
61 | Var
|
---|
62 | MainForm: TMainForm;
|
---|
63 |
|
---|
64 | Implementation
|
---|
65 |
|
---|
66 | Uses
|
---|
67 | PMWin, PMSHL,
|
---|
68 | SysUtils,
|
---|
69 | ACLFileUtility, ACLUtility, ACLStringUtility,
|
---|
70 | ControlsUtility, ACLDialogs;
|
---|
71 |
|
---|
72 | type
|
---|
73 | TProgramSettings = record
|
---|
74 | ProgramType: TProgramType;
|
---|
75 | Name: string;
|
---|
76 | PathKey: string;
|
---|
77 | WorkingDirectoryKey: string;
|
---|
78 | ParametersKey: string;
|
---|
79 | end;
|
---|
80 |
|
---|
81 | TCurrentSettings = record
|
---|
82 | Path: string;
|
---|
83 | WorkingDirectory: string;
|
---|
84 | Parameters: string;
|
---|
85 | end;
|
---|
86 |
|
---|
87 | const
|
---|
88 | Programs: array[ Low( TProgramType ).. High( TProgramType ) ] of TProgramSettings =
|
---|
89 | (
|
---|
90 | ( ProgramType: idBrowser;
|
---|
91 | Name: 'Web Browser';
|
---|
92 | PathKey: 'DefaultBrowserExe';
|
---|
93 | WorkingDirectoryKey: 'DefaultWorkingDir';
|
---|
94 | ParametersKey: 'DefaultParameters' ),
|
---|
95 | ( ProgramType: idMail;
|
---|
96 | Name: 'Email';
|
---|
97 | PathKey: 'DefaultMailExe';
|
---|
98 | WorkingDirectoryKey: 'DefaultMailWorkingDir';
|
---|
99 | ParametersKey: 'DefaultMailParameters' ),
|
---|
100 | ( ProgramType: idNews;
|
---|
101 | Name: 'Newsgroups';
|
---|
102 | PathKey: 'DefaultNewsExe';
|
---|
103 | WorkingDirectoryKey: 'DefaultNewsWorkingDir';
|
---|
104 | ParametersKey: 'DefaultNewsParameters' ),
|
---|
105 | ( ProgramType: idFTP;
|
---|
106 | Name: 'FTP';
|
---|
107 | PathKey: 'DefaultFTPExe';
|
---|
108 | WorkingDirectoryKey: 'DefaultFTPWorkingDir';
|
---|
109 | ParametersKey: 'DefaultFTPParameters' ),
|
---|
110 | ( ProgramType: idIRC;
|
---|
111 | Name: 'IRC';
|
---|
112 | PathKey: 'DefaultIRCExe';
|
---|
113 | WorkingDirectoryKey: 'DefaultIRCWorkingDir';
|
---|
114 | ParametersKey: 'DefaultIRCParameters' )
|
---|
115 | );
|
---|
116 |
|
---|
117 | var
|
---|
118 | CurrentSettings: array[ Low( TProgramType ).. High( TProgramType ) ] of TCurrentSettings;;
|
---|
119 |
|
---|
120 | Procedure SetProgramPath( P: TProgramSettings;
|
---|
121 | const V: string );
|
---|
122 | begin
|
---|
123 | SetUserProfileString( 'WPURLDEFAULTSETTINGS',
|
---|
124 | P.PathKey,
|
---|
125 | V );
|
---|
126 | end;
|
---|
127 |
|
---|
128 | Procedure SetWorkingDirectory( P: TProgramSettings;
|
---|
129 | const V: string );
|
---|
130 | begin
|
---|
131 | SetUserProfileString( 'WPURLDEFAULTSETTINGS',
|
---|
132 | P.WorkingDirectoryKey,
|
---|
133 | V );
|
---|
134 | end;
|
---|
135 |
|
---|
136 | Procedure SetParameters( P: TProgramSettings;
|
---|
137 | Const V: string );
|
---|
138 | begin
|
---|
139 | SetUserProfileString( 'WPURLDEFAULTSETTINGS',
|
---|
140 | P.ParametersKey,
|
---|
141 | V );
|
---|
142 | end;
|
---|
143 |
|
---|
144 | Function GetProgramPath( P: TProgramSettings ): string;
|
---|
145 | begin
|
---|
146 | Result := GetUserProfileString( 'WPURLDEFAULTSETTINGS',
|
---|
147 | P.PathKey,
|
---|
148 | '' );
|
---|
149 | end;
|
---|
150 |
|
---|
151 | Function GetWorkingDirectory( P: TProgramSettings ): string;
|
---|
152 | begin
|
---|
153 | Result := GetUserProfileString( 'WPURLDEFAULTSETTINGS',
|
---|
154 | P.WorkingDirectoryKey,
|
---|
155 | '' );
|
---|
156 | end;
|
---|
157 |
|
---|
158 | Function GetParameters( P: TProgramSettings ): string;
|
---|
159 | begin
|
---|
160 | Result := GetUserProfileString( 'WPURLDEFAULTSETTINGS',
|
---|
161 | P.ParametersKey,
|
---|
162 | '' );
|
---|
163 | end;
|
---|
164 |
|
---|
165 | Procedure TMainForm.ParametersEditOnChange (Sender: TObject);
|
---|
166 | Begin
|
---|
167 | if not Displaying then
|
---|
168 | Changed := true;
|
---|
169 | End;
|
---|
170 |
|
---|
171 | Procedure TMainForm.RestoreButtonOnClick (Sender: TObject);
|
---|
172 | Begin
|
---|
173 | LoadCurrentSettings;
|
---|
174 | End;
|
---|
175 |
|
---|
176 | Procedure TMainForm.MainFormOnCloseQuery (Sender: TObject;
|
---|
177 | Var CanClose: Boolean);
|
---|
178 | Begin
|
---|
179 | if Changed then
|
---|
180 | if not DoConfirmDlg( 'Discard changes?',
|
---|
181 | 'Exit and discard changes?' ) then
|
---|
182 | CanClose := false;
|
---|
183 |
|
---|
184 | End;
|
---|
185 |
|
---|
186 | Procedure TMainForm.WorkingDirectoryEditOnChange (Sender: TObject);
|
---|
187 | Begin
|
---|
188 | if not Displaying then
|
---|
189 | Changed := true;
|
---|
190 |
|
---|
191 | End;
|
---|
192 |
|
---|
193 | Procedure TMainForm.ApplyButtonOnClick (Sender: TObject);
|
---|
194 | var
|
---|
195 | ProgramType: TProgramType;
|
---|
196 | P: TProgramSettings;
|
---|
197 | FoundPath: string; // dummy
|
---|
198 | Begin
|
---|
199 | StoreEdits( SelectedType );
|
---|
200 | // validate.
|
---|
201 | for ProgramType := Low( TProgramType ) to High( TProgramType ) do
|
---|
202 | begin
|
---|
203 | p := Programs[ ProgramType ];
|
---|
204 |
|
---|
205 | with CurrentSettings[ ProgramType ] do
|
---|
206 | begin
|
---|
207 | if Trim( Path ) <> '' then
|
---|
208 | begin
|
---|
209 | if ExtractFilePath( Path ) <> '' then
|
---|
210 | begin
|
---|
211 | // they specified a path...
|
---|
212 | if not FileExists( Path ) then
|
---|
213 | if not DoConfirmDlg( p.Name,
|
---|
214 | 'File does not exist: ' + EndLine
|
---|
215 | + Path + EndLine
|
---|
216 | + 'Save anyway?' ) then
|
---|
217 | exit;
|
---|
218 | end
|
---|
219 | else
|
---|
220 | begin
|
---|
221 | // no directory, search path
|
---|
222 | if not SearchPath( 'PATH',
|
---|
223 | Path,
|
---|
224 | FoundPath ) then
|
---|
225 | if not DoConfirmDlg( p.Name,
|
---|
226 | 'Program not found in path: ' + EndLine
|
---|
227 | + Path + EndLine
|
---|
228 | + 'Save anyway?' ) then
|
---|
229 | exit;
|
---|
230 | end;
|
---|
231 | if not DirectoryExists( WorkingDirectory ) then
|
---|
232 | if not DoConfirmDlg( p.Name,
|
---|
233 | 'Directory does not exist: '
|
---|
234 | + WorkingDirectory
|
---|
235 | + '. Save anyway?' ) then
|
---|
236 | exit;
|
---|
237 | end;
|
---|
238 | end;
|
---|
239 | end;
|
---|
240 |
|
---|
241 | for ProgramType := Low( TProgramType ) to High( TProgramType ) do
|
---|
242 | begin
|
---|
243 | p := Programs[ ProgramType ];
|
---|
244 |
|
---|
245 | SetProgramPath( P, CurrentSettings[ ProgramType ].Path );
|
---|
246 | SetWorkingDirectory( P, CurrentSettings[ ProgramType ].WorkingDirectory );
|
---|
247 | SetParameters( p, CurrentSettings[ ProgramType ].Parameters );
|
---|
248 | end;
|
---|
249 |
|
---|
250 | Changed := false;
|
---|
251 | End;
|
---|
252 |
|
---|
253 | Procedure TMainForm.SelectDirectoryButtonOnClick (Sender: TObject);
|
---|
254 | Begin
|
---|
255 | ChangeDirDialog.Directory := WorkingDirectoryEdit.Text;
|
---|
256 | if not ChangeDirDialog.Execute then
|
---|
257 | exit;
|
---|
258 | WorkingDirectoryEdit.Text := ChangeDirDialog.Directory;
|
---|
259 | End;
|
---|
260 |
|
---|
261 | Procedure TMainForm.BrowsePathButtonOnClick (Sender: TObject);
|
---|
262 | Begin
|
---|
263 | OpenDialog.Filename := ExtractFilePath( PathEdit.Text ) + '*.exe' ;
|
---|
264 | if not OpenDialog.Execute then
|
---|
265 | exit;
|
---|
266 |
|
---|
267 | PathEdit.Text := OpenDialog.FileName;
|
---|
268 | End;
|
---|
269 |
|
---|
270 | Procedure TMainForm.PathEditOnChange (Sender: TObject);
|
---|
271 | Begin
|
---|
272 | if not Displaying then
|
---|
273 | begin
|
---|
274 | StoreEdits( SelectedType );
|
---|
275 | Changed := true;
|
---|
276 | end;
|
---|
277 | End;
|
---|
278 |
|
---|
279 | Function TMainForm.SelectedType: TProgramType;
|
---|
280 | begin
|
---|
281 | Result := TProgramType( ItemsListBox.Items.Objects[ ItemsListBox.ItemIndex ] );
|
---|
282 | end;
|
---|
283 |
|
---|
284 | Procedure TMainForm.StoreEdits( P: TProgramType );
|
---|
285 | var
|
---|
286 | i: longint;
|
---|
287 | begin
|
---|
288 | CurrentSettings[ P ].Path := PathEdit.Text;
|
---|
289 | CurrentSettings[ P ].WorkingDirectory := WorkingDirectoryEdit.Text;
|
---|
290 | CurrentSettings[ P ].Parameters := ParametersEdit.Text;
|
---|
291 |
|
---|
292 | for i := 0 to ItemsListBox.Items.Count - 1 do
|
---|
293 | if TProgramType( ItemsListBox.Items.Objects[ i ] ) = p then
|
---|
294 | ItemsListBox.Items[ i ] := Programs[ p ].Name
|
---|
295 | + #9
|
---|
296 | + PathEdit.Text;
|
---|
297 | end;
|
---|
298 |
|
---|
299 | Procedure TMainForm.ItemsListboxOnItemFocus (Sender: TObject; Index: LongInt);
|
---|
300 | Begin
|
---|
301 | if FLastItemSet then
|
---|
302 | begin
|
---|
303 | StoreEdits( FLastItem );
|
---|
304 | end;
|
---|
305 |
|
---|
306 | Displaying := true;
|
---|
307 | PathEdit.Text := CurrentSettings[ SelectedType ].Path;
|
---|
308 | WorkingDirectoryEdit.Text := CurrentSettings[ SelectedType ].WorkingDirectory;
|
---|
309 | ParametersEdit.Text := CurrentSettings[ SelectedType ].Parameters;
|
---|
310 |
|
---|
311 | Displaying := false;
|
---|
312 | FLastItem := SelectedType;
|
---|
313 | FLastItemSet := true;
|
---|
314 | End;
|
---|
315 |
|
---|
316 | Procedure TMainForm.MainFormOnCreate (Sender: TObject);
|
---|
317 | Begin
|
---|
318 | Font := GetNiceDefaultFont;
|
---|
319 |
|
---|
320 | VersionLabel.Caption := AppVersion;
|
---|
321 | LoadCurrentSettings;
|
---|
322 | end;
|
---|
323 |
|
---|
324 | Procedure TMainForm.LoadCurrentSettings;
|
---|
325 | var
|
---|
326 | ProgramType: TProgramType;
|
---|
327 | P: TProgramSettings;
|
---|
328 | begin
|
---|
329 | ItemsListBox.Items.Clear;
|
---|
330 | for ProgramType := Low( TProgramType ) to High( TProgramType ) do
|
---|
331 | begin
|
---|
332 | p := Programs[ ProgramType ];
|
---|
333 | CurrentSettings[ ProgramType ].Path := GetProgramPath( p );
|
---|
334 | CurrentSettings[ ProgramType ].WorkingDirectory := GetWorkingDirectory( p );
|
---|
335 | CurrentSettings[ ProgramType ].Parameters := GetParameters( p );
|
---|
336 |
|
---|
337 | ItemsListBox.Items.AddObject( P.Name
|
---|
338 | + #9
|
---|
339 | + CurrentSettings[ ProgramType ].Path,
|
---|
340 | TObject( P.ProgramType ) );
|
---|
341 | end;
|
---|
342 |
|
---|
343 | FLastItemSet := false;
|
---|
344 | Displaying := false;
|
---|
345 | Changed := false;
|
---|
346 | ItemsListBox.ItemIndex := 0;
|
---|
347 | End;
|
---|
348 |
|
---|
349 | Initialization
|
---|
350 | RegisterClasses ([TMainForm, TEdit, TButton, TMultiColumnListBox, TLabel,
|
---|
351 | TSystemOpenDialog, TChangeDirDialog]);
|
---|
352 | End.
|
---|