UnitSettings: Disable the run button when the is no prefix.
[WineLauncher.git] / Form / Settings / UnitSettings.pas
blob4e5ecb12a29ee8c3eaaa54f71eb4bdbb158a4c53
1 { This file is part of WineLauncher.
3 WineLauncher is free software: you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation, version 3 of the License.
7 WineLauncher is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with WineLauncher. If not, see <http://www.gnu.org/licenses/>.
16 unit UnitSettings;
18 {$mode objfpc}{$H+}
20 interface
22 uses
23 Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls, BaseUnix,
24 Grids, strutils;
26 type
28 { TForm4 }
31 TForm4 = class(TForm)
32 ComboBox_PreFix: TComboBox;
33 ComboBox_ShellPath: TComboBox;
34 Label1: TLabel;
35 Label8: TLabel;
36 StringGrid_TerminalSettings: TStringGrid;
37 StringGrid_ProgramsList: TStringGrid;
39 procedure ComboBox_PreFixChange(Sender: TObject);
40 procedure ComboBox_ShellPathEditingDone(Sender: TObject);
41 procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
42 procedure FormCreate(Sender: TObject);
43 procedure StringGrid_ProgramsListDrawCell(Sender: TObject; aCol,
44 aRow: Integer; aRect: TRect; aState: TGridDrawState);
45 procedure StringGrid_ProgramsListEditingDone(Sender: TObject);
46 procedure StringGrid_TerminalSettingsEditingDone(Sender: TObject);
48 function PreFixExists():boolean;
49 private
51 procedure ProgramsUpdate();
53 public
54 { public declarations }
55 end;
57 var
58 Form4: TForm4;
59 TerminalPathClean:Tstrings;
60 TerminalPathFlags:Tstrings;
61 TerrminalSettingsIsFine:boolean;
62 ShellPathIsFine:boolean;
63 ProgramsList:Tstrings;
65 implementation
66 uses
67 UnitInitialization,
68 UnitMain,
69 UnitProgramsList,
70 UnitMisc;
72 procedure TForm4.FormCreate(Sender: TObject);
73 var
74 LocalChannel:string;
75 A,B:integer;
76 loop:integer;
77 begin
78 LocalChannel := '';
80 TerminalPathClean := TStringlist.Create;
81 ProgramsList := TStringlist.Create;
83 StringGrid_ProgramsList.SaveOptions := [soDesign,{soAttributes,}soContent{,soPosition}] ;
86 {Cheep Hack to check the StringGrid and sych the combobox. }
87 StringGrid_TerminalSettingsEditingDone(self);
89 {Crash fix}
90 ComboBox_ShellPath.Text := ComboBox_ShellPath.Items[0] ;
92 {Load StringGrid_TerminalSettings}
94 if FileExists ( ConfigPath + '/StringGrid_TerminalSettings') = true then
95 begin
96 {NOTE StringGrid.LoadFromFile has no error checking? http://www.freepascal.org/docs-html/lcl/grids/tcustomgrid.loadfromfile.html}
97 StringGrid_TerminalSettings.LoadFromFile( ConfigPath + '/StringGrid_TerminalSettings' );
98 Log(3, LocalChannel, ( ConfigPath + '/StringGrid_TerminalSettings' + ' ) has being loaded.'));
99 end
100 else
101 begin
102 Log(4, LocalChannel, ( ConfigPath + '/StringGrid_TerminalSettings' + ' ) does not exists.'));
103 end;
105 {Lay out for PreFixList: array of Tstrings}
106 {The first Tstrings is for prefix names}
107 {The second Tstrings is for the paths of the prefix's}
108 SetLength(PreFixList, 2);
109 PreFixList[0] := TStringlist.Create;
110 PreFixList[1] := TStringlist.Create;
112 if PreFixExists() = true then
113 begin
114 SetUpArray();
116 else
117 begin
118 UnitMain.form1.Btn_Settings.Enabled := false;
119 UnitMain.form1.Button_Winecfg.Enabled := false;
120 UnitMain.form1.Button_Regedit.Enabled := false;
121 UnitMain.form1.Btn_Run.Enabled := false;
122 UnitMain.form1.TogBtn_Listdrive.Enabled := false;
123 UnitMain.form1.EditBox_ProgramPath.Text := '';
124 UnitMain.form1.EditBox_ProgramPath.Enabled := false;
125 NoPrefix := true;
126 MessageDlg('Please create a wine prefix', 'You need to create a wine prefix see the create prefix tab.', mtWarning, [mbOK], '');
127 end;
129 end;
131 procedure TForm4.StringGrid_ProgramsListDrawCell(Sender: TObject; aCol,
132 aRow: Integer; aRect: TRect; aState: TGridDrawState);
133 begin
134 StringGrid_ProgramsList.AutoSizeColumns;
135 end;
137 procedure TForm4.StringGrid_ProgramsListEditingDone(Sender: TObject);
139 loop:integer;
140 B:integer;
141 LocalChannel:string;
142 begin
143 LocalChannel := 'StringGrid_ProgramsListEditingDone';
145 {TODO Went Editing this runs two times, abit pointless and not CPU friendy}
146 {TODO may add some checks to the flags. Can that be done ?}
148 {log EditingDone}
149 Log(5, LocalChannel, ('-----------------'));
150 Log(3, LocalChannel, ( StringGrid_ProgramsList.Name + ' editing has finished.' ));
152 for loop := 1 to StringGrid_ProgramsList.Cols[DataNameCol].Count -1 do
153 begin
154 if (StringGrid_ProgramsList.Cells[DataNameCol,loop]) = '' then
155 begin
156 StringGrid_ProgramsList.MoveColRow(false,loop, (StringGrid_ProgramsList.Cols[DataNameCol].Count -1)) ;
157 StringGrid_ProgramsList.Rows[StringGrid_ProgramsList.Cols[DataNameCol].Count -1].Clear ;
158 end;
159 end;
161 {Fill it}
162 ProgramsUpdate();
163 end;
165 procedure TForm4.ProgramsUpdate();
167 B:integer;
168 PrefixInt:integer;
169 begin
170 {Fill array}
171 PrefixInt := FindPrefixInt(ComboBox_PreFix.Items.Strings[ComboBox_PreFix.ItemIndex]);
172 for B := 0 to Data[PrefixInt].Grid.RowCount -1 do
173 begin
174 Data[PrefixInt].Grid.Rows[B].Text := StringGrid_ProgramsList.Rows[B].text;
175 end;
176 end;
178 function TForm4.PreFixExists():boolean;
180 PreFixFolders:Tstrings;
181 CheckList:Tstrings;
182 loop:integer;
183 ChannelLocal:string;
184 begin
185 ChannelLocal := ('PreFixExists');
186 { TODO : procedure for telling you a procedure has being called. }
187 Log(5, '', ('-----------------'));
188 Log(0, ChannelLocal, ('PreFixExists has being Called.'));
190 CheckList := TStringlist.Create;
191 PreFixFolders := TStringlist.Create;
193 CheckList.Clear;
194 CheckList.Add('/dosdevices') ;
195 CheckList.Add('/drive_c') ;
197 { TODO : ListFileDir can only handly one path at ones }
199 ListFileDir({0} PathToPrefix, {1} PreFixFolders, {2} nil, {3} nil, {4} nil, {5} false);
201 for loop := 0 to (PreFixFolders.Count -1) do
202 begin
203 if AnsiEndsText('~', PreFixFolders.Strings[loop]) = false then
204 begin
205 { Folder and file check. }
207 if DoesFoldersExists((PathToPrefix + PreFixFolders.Strings[loop]), CheckList ) = true then
208 begin
209 if FileExistsAndIsExecutable(PathToPrefix + PreFixFolders.Strings[loop] + '/system.reg',true) = true then
210 begin
211 if FileExistsAndIsExecutable(PathToPrefix + PreFixFolders.Strings[loop] + '/user.reg', true) = true then
212 begin
213 PreFixList[0].add(PreFixFolders.Strings[loop]);
214 PreFixList[1].add(PathToPrefix + PreFixFolders.Strings[loop]);
215 end;
216 end;
217 end;
218 end;
220 end;
222 if PreFixList[0].Count <> 0 then
223 begin
224 {Sych Tstrings with Comobox.}
225 UnitMain.Form1.ComboBox_PreFix.Items := PreFixList[0];
226 ComboBox_PreFix.Items := PreFixList[0];
228 {Cheep hack to stop crashs.}
229 UnitMain.Form1.ComboBox_PreFix.Text := UnitMain.Form1.ComboBox_PreFix.Items[0];
230 ComboBox_PreFix.Text := ComboBox_PreFix.Items[0];
231 Result := true;
233 else
234 begin
235 Result := false;
236 end;
239 {Clean up.}
240 CheckList.Destroy;
241 PreFixFolders.Destroy;
243 end;
246 procedure TForm4.FormClose(Sender: TObject; var CloseAction: TCloseAction);
248 LocalChannel:string;
249 TempInt:Integer;
250 loop:Integer;
251 begin
252 { TODO : Set LocalChannel. }
253 LocalChannel := '';
255 Log(5, LocalChannel, ('-----------------'));
256 Log(3, LocalChannel, ( Form4.Name + ' close has happen.' ));
258 {Save Prefix data.}
259 for loop := 0 to (PreFixList[0].Count -1) do
260 begin
261 ProgramsListSavePrefix(PreFixList[0][loop]);
262 end;
265 {Temp save of StringGrid_TerminalSettings}
266 if TerrminalSettingsIsFine = true then
267 begin
268 Log(3, LocalChannel, ( 'Saving' + Wrap(StringGrid_TerminalSettings.Name) + 'to' + Wrap(ConfigPath + '/StringGrid_TerminalSettings') ));
269 StringGrid_TerminalSettings.SaveOptions := [soDesign,{soAttributes,}soContent{,soPosition}] ;
270 {TODO error checking for save.}
271 StringGrid_TerminalSettings.SaveToFile( ConfigPath + '/StringGrid_TerminalSettings' );
273 else
274 begin
275 Log(1, LocalChannel, ('Not saving' + Wrap(StringGrid_TerminalSettings.Name) + 'because the is something wrong with it.'));
276 end;
278 if Mode = false then
279 begin
280 {Refresh form1.}
281 {Remember the selected item.}
282 TempInt := UnitMain.Form1.ComboBox_ProgramsList.ItemIndex;
283 {Update ComboBox_PreFix, This clears ComboBox_ProgramsList.}
284 UnitMain.Form1.ComboBox_PreFixChange(self);
285 {Write back the remembered selected item.}
286 UnitMain.Form1.ComboBox_ProgramsList.ItemIndex := TempInt;
287 {This updates EditBox_ProgramPath.}
288 UnitMain.Form1.ComboBox_ProgramsListChange(self);
290 {Note flag settings are losted.}
291 end;
293 end;
295 procedure TForm4.ComboBox_ShellPathEditingDone(Sender: TObject);
297 LocalChannel:string;
298 begin
299 { TODO : Set LocalChannel. }
300 LocalChannel := '';
302 Log(5, LocalChannel, ('-----------------'));
303 Log(3, LocalChannel, ( ComboBox_ShellPath.Name + ' editing has finished.' ));
305 if FileExistsAndIsExecutable( {FullPath:string}ComboBox_ShellPath.text, {JustCheckExists:boolean}false ) = true then
306 begin
307 ShellPathIsFine := true;
308 { TODO -cColour : Work out why "clwindow" does not work. }
309 ComboBox_ShellPath.Color := clwhite;
311 else
312 begin
313 ShellPathIsFine := false;
314 ComboBox_ShellPath.Color := clred;
315 end;
317 end;
319 procedure TForm4.ComboBox_PreFixChange(Sender: TObject);
321 B:integer;
322 PrefixInt:integer;
323 begin
324 StringGrid_ProgramsList.Clean ;
325 StringGrid_ProgramsList.FixedRows := 1 ;
326 PrefixInt := FindPrefixInt(ComboBox_PreFix.Items.Strings[ComboBox_PreFix.ItemIndex]);
328 for B := 0 to 100 do
329 begin
330 StringGrid_ProgramsList.Rows[B].text := Data[PrefixInt].Grid.Rows[B].Text;
331 end;
332 end;
335 procedure TForm4.StringGrid_TerminalSettingsEditingDone(Sender: TObject);
337 LocalChannel:string;
338 loop:integer;
339 loop2:integer;
340 IsFine:boolean;
341 begin
342 { TODO : Went Editing this runs two times, abit pointless and not CPU friendy}
343 { TODO : May add some checks to the flags. Can that be done ?}
344 { TODO : Set LocalChannel. }
345 LocalChannel := '';
346 IsFine := true;
349 Log(5, LocalChannel, ('-----------------'));
350 Log(3, LocalChannel, ( StringGrid_TerminalSettings.Name + ' editing has finished.'));
352 {USEFUL INFO: stringGrid.Cells[col,row] }
354 {Check the no row is blank other then the last one}
355 if IsFine = true then
356 begin
357 for loop := 0 to ( StringGrid_TerminalSettings.RowCount -2 )do {Ignore last row}
358 begin
359 {TODO : check the whole row not just the frist cell }
360 if StringGrid_TerminalSettings.Cells[0,loop] = '' then {delete it!}
361 begin
362 StringGrid_TerminalSettings.DeleteColRow(false,loop);
363 Log(4, LocalChannel, ('Not Fine deleting row.'));
364 exit; {exit or out of range error}
366 else
367 begin
369 end;
370 end; {end loop}
371 end;
374 {Check the Names are unique or you can get the wrong path}
375 if IsFine = true then
376 begin
377 //UnitMain.form1.log('Running Check the Names are unique');
378 for loop := 1 to ( StringGrid_TerminalSettings.RowCount -1 )do
379 begin
380 for loop2 := 0 to ( StringGrid_TerminalSettings.RowCount -1 )do
381 begin
382 if StringGrid_TerminalSettings.cells[0,loop] = StringGrid_TerminalSettings.cells[0,loop2] then
383 begin
384 if loop2 <> loop then
385 begin
386 { Error }
387 Log(4, LocalChannel, ( 'Col(0)Row(' + inttostr(loop) + ') has the same name as Col(0)Row(' + inttostr(loop2) + ').'));
388 IsFine := false;
390 else
391 begin
392 { All fine }
393 end;
394 end;
395 end; {loop2 end}
396 end;{loop end}
397 end;
400 {Check that Path does exists and is executable.}
401 if IsFine = true then
402 begin
403 for loop := 0 to ( StringGrid_TerminalSettings.RowCount -1 )do
404 begin
405 if loop = ( StringGrid_TerminalSettings.RowCount -1 ) then
406 begin
407 if StringGrid_TerminalSettings.Cells[0,loop] <> '' then
408 begin
409 if FileExists( StringGrid_TerminalSettings.Cells[1,loop] ) then
410 begin
411 Log(3, LocalChannel, ('Col(1)Row(' + inttostr(loop) + ') file ( ' + StringGrid_TerminalSettings.Cells[1,loop] + ' ) does exists.'));
412 if FpAccess( StringGrid_TerminalSettings.Cells[1,loop] , X_OK {Exe flag check}) = 0 then
413 begin
414 Log(1, LocalChannel, ('Col(1)Row(' + inttostr(loop) + ') file ( ' + StringGrid_TerminalSettings.Cells[1,loop] + ' ) is executable.'));
416 else
417 begin
418 { else FpAccess }
419 IsFine := false;
420 Log(1, LocalChannel, ('Col(1)Row(' + inttostr(loop) + ') file (' + StringGrid_TerminalSettings.Cells[1,loop] + ' ) is not executable.'));
421 end;
423 else
424 begin
425 { else FileExists }
426 IsFine := false;
427 Log(1, LocalChannel, ('Col(1)Row(' + inttostr(loop) + ') file ( ' + StringGrid_TerminalSettings.Cells[1,loop] + ' ) does not exists.'));
428 end;
429 end; { StringGrid_TerminalSettings.Cells[0,loop] <> '' END }
431 else { ELSE loop = ( StringGrid_TerminalSettings.RowCount -1 ) }
432 begin
434 end;
436 end; {loop end}
437 end;
439 {if last row is used then add a new one}
440 if IsFine = true then
441 begin
442 if StringGrid_TerminalSettings.cells[0,(StringGrid_TerminalSettings.RowCount -1)] <> '' then
443 begin
444 StringGrid_TerminalSettings.RowCount := ( StringGrid_TerminalSettings.RowCount + 1 );
445 end;
446 end;
448 {Report!}
449 if Isfine = true then
450 begin
451 TerrminalSettingsIsFine := true;
452 Log(0, LocalChannel, ('Terminal settings are fine.'));
454 else
455 begin
456 TerrminalSettingsIsFine := false;
457 Log(1, LocalChannel, ('The is an error in the terminal settings.'));
458 end;
461 {sych}
462 if Isfine = true then
463 begin
464 TerminalPathClean.Clear;
465 for loop := 0 to ( StringGrid_TerminalSettings.RowCount -2 )do {Last row is ignore WILL be blank}
466 begin
467 TerminalPathClean.Add(StringGrid_TerminalSettings.Cells[0,loop] );
468 end;
470 UnitMain.Form1.ComboBox_TerminalName.Items := TerminalPathClean;
471 {INFO this will change your seleted item back to the first item, not the worlds best code but it stops crashs}
472 UnitMain.Form1.ComboBox_TerminalName.Text := UnitMain.Form1.ComboBox_TerminalName.Items[0];
473 end;
475 end;
480 initialization
481 {$I UnitSettings.lrs}
483 end.