source: trunk/ConfigApps/MainFormUnit.pas@ 413

Last change on this file since 413 was 413, checked in by ataylor, 7 years ago

Revamp of build system, and added ConfigApps to repository.

File size: 10.0 KB
Line 
1Unit MainFormUnit;
2
3Interface
4
5Uses
6 Classes, Forms, Graphics, StdCtrls, Buttons, MultiColumnListBox, Dialogs;
7
8const
9 AppVersion = 'V1.1.1'; // $SS_REQUIRE_NEW_VERSION$
10
11type
12 TProgramType =
13 (
14 idBrowser,
15 idMail,
16 idNews,
17 idFTP,
18 idIRC
19 );
20
21Type
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
61Var
62 MainForm: TMainForm;
63
64Implementation
65
66Uses
67 PMWin, PMSHL,
68 SysUtils,
69 ACLFileUtility, ACLUtility, ACLStringUtility,
70 ControlsUtility, ACLDialogs;
71
72type
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
87const
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
117var
118 CurrentSettings: array[ Low( TProgramType ).. High( TProgramType ) ] of TCurrentSettings;;
119
120Procedure SetProgramPath( P: TProgramSettings;
121 const V: string );
122begin
123 SetUserProfileString( 'WPURLDEFAULTSETTINGS',
124 P.PathKey,
125 V );
126end;
127
128Procedure SetWorkingDirectory( P: TProgramSettings;
129 const V: string );
130begin
131 SetUserProfileString( 'WPURLDEFAULTSETTINGS',
132 P.WorkingDirectoryKey,
133 V );
134end;
135
136Procedure SetParameters( P: TProgramSettings;
137 Const V: string );
138begin
139 SetUserProfileString( 'WPURLDEFAULTSETTINGS',
140 P.ParametersKey,
141 V );
142end;
143
144Function GetProgramPath( P: TProgramSettings ): string;
145begin
146 Result := GetUserProfileString( 'WPURLDEFAULTSETTINGS',
147 P.PathKey,
148 '' );
149end;
150
151Function GetWorkingDirectory( P: TProgramSettings ): string;
152begin
153 Result := GetUserProfileString( 'WPURLDEFAULTSETTINGS',
154 P.WorkingDirectoryKey,
155 '' );
156end;
157
158Function GetParameters( P: TProgramSettings ): string;
159begin
160 Result := GetUserProfileString( 'WPURLDEFAULTSETTINGS',
161 P.ParametersKey,
162 '' );
163end;
164
165Procedure TMainForm.ParametersEditOnChange (Sender: TObject);
166Begin
167 if not Displaying then
168 Changed := true;
169End;
170
171Procedure TMainForm.RestoreButtonOnClick (Sender: TObject);
172Begin
173 LoadCurrentSettings;
174End;
175
176Procedure TMainForm.MainFormOnCloseQuery (Sender: TObject;
177 Var CanClose: Boolean);
178Begin
179 if Changed then
180 if not DoConfirmDlg( 'Discard changes?',
181 'Exit and discard changes?' ) then
182 CanClose := false;
183
184End;
185
186Procedure TMainForm.WorkingDirectoryEditOnChange (Sender: TObject);
187Begin
188 if not Displaying then
189 Changed := true;
190
191End;
192
193Procedure TMainForm.ApplyButtonOnClick (Sender: TObject);
194var
195 ProgramType: TProgramType;
196 P: TProgramSettings;
197 FoundPath: string; // dummy
198Begin
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;
251End;
252
253Procedure TMainForm.SelectDirectoryButtonOnClick (Sender: TObject);
254Begin
255 ChangeDirDialog.Directory := WorkingDirectoryEdit.Text;
256 if not ChangeDirDialog.Execute then
257 exit;
258 WorkingDirectoryEdit.Text := ChangeDirDialog.Directory;
259End;
260
261Procedure TMainForm.BrowsePathButtonOnClick (Sender: TObject);
262Begin
263 OpenDialog.Filename := ExtractFilePath( PathEdit.Text ) + '*.exe' ;
264 if not OpenDialog.Execute then
265 exit;
266
267 PathEdit.Text := OpenDialog.FileName;
268End;
269
270Procedure TMainForm.PathEditOnChange (Sender: TObject);
271Begin
272 if not Displaying then
273 begin
274 StoreEdits( SelectedType );
275 Changed := true;
276 end;
277End;
278
279Function TMainForm.SelectedType: TProgramType;
280begin
281 Result := TProgramType( ItemsListBox.Items.Objects[ ItemsListBox.ItemIndex ] );
282end;
283
284Procedure TMainForm.StoreEdits( P: TProgramType );
285var
286 i: longint;
287begin
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;
297end;
298
299Procedure TMainForm.ItemsListboxOnItemFocus (Sender: TObject; Index: LongInt);
300Begin
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;
314End;
315
316Procedure TMainForm.MainFormOnCreate (Sender: TObject);
317Begin
318 Font := GetNiceDefaultFont;
319
320 VersionLabel.Caption := AppVersion;
321 LoadCurrentSettings;
322end;
323
324Procedure TMainForm.LoadCurrentSettings;
325var
326 ProgramType: TProgramType;
327 P: TProgramSettings;
328begin
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;
347End;
348
349Initialization
350 RegisterClasses ([TMainForm, TEdit, TButton, TMultiColumnListBox, TLabel,
351 TSystemOpenDialog, TChangeDirDialog]);
352End.
Note: See TracBrowser for help on using the repository browser.