WineLauncher: Fixed an error in the license headers.
[WineLauncher.git] / Form / Settings / UnitSettings.pas
blobfde08d2db72a38bef537810e1eda1cd539cfb449
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, either version 3 of the License, or
6 (at your option) any later version.
8 WineLauncher is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with WineLauncher. If not, see <http://www.gnu.org/licenses/>.
17 unit UnitSettings;
19 {$mode objfpc}{$H+}
21 interface
23 uses
24 Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls, BaseUnix,
25 Grids, strutils;
27 type
29 { TForm4 }
32 TForm4 = class(TForm)
33 ComboBox_PreFix: TComboBox;
34 ComboBox_ShellPath: TComboBox;
35 Label1: TLabel;
36 Label8: TLabel;
37 StringGrid_TerminalSettings: TStringGrid;
38 StringGrid_ProgramsList: TStringGrid;
40 procedure ComboBox_PreFixChange(Sender: TObject);
41 procedure ComboBox_ShellPathEditingDone(Sender: TObject);
42 procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
43 procedure FormCreate(Sender: TObject);
44 procedure FormDestroy(Sender: TObject);
45 procedure StringGrid_ProgramsListDrawCell(Sender: TObject; aCol,
46 aRow: Integer; aRect: TRect; aState: TGridDrawState);
47 procedure StringGrid_ProgramsListEditingDone(Sender: TObject);
48 procedure StringGrid_TerminalSettingsEditingDone(Sender: TObject);
50 function PreFixExists():boolean;
51 private
53 procedure ProgramsUpdate();
55 public
56 { public declarations }
57 end;
59 var
60 Form4: TForm4;
61 TerminalPathClean:Tstrings;
62 TerminalPathFlags:Tstrings;
63 TerrminalSettingsIsFine:boolean;
64 ShellPathIsFine:boolean;
65 ProgramsList:Tstrings;
67 implementation
68 uses
69 UnitInitialization,
70 UnitMain,
71 UnitProgramsList,
72 UnitMisc;
74 procedure TForm4.FormCreate(Sender: TObject);
75 var
76 LocalChannel:string;
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.FormDestroy(Sender: TObject);
132 begin
133 PreFixList[1].Destroy;
134 PreFixList[0].Destroy;
135 TerminalPathClean.Destroy;
136 ProgramsList.Destroy;
137 end;
139 procedure TForm4.StringGrid_ProgramsListDrawCell(Sender: TObject; aCol,
140 aRow: Integer; aRect: TRect; aState: TGridDrawState);
141 begin
142 StringGrid_ProgramsList.AutoSizeColumns;
143 end;
145 procedure TForm4.StringGrid_ProgramsListEditingDone(Sender: TObject);
147 loop:integer;
148 LocalChannel:string;
149 begin
150 LocalChannel := 'StringGrid_ProgramsListEditingDone';
152 {TODO Went Editing this runs two times, abit pointless and not CPU friendy}
153 {TODO may add some checks to the flags. Can that be done ?}
155 {log EditingDone}
156 Log(5, LocalChannel, ('-----------------'));
157 Log(3, LocalChannel, ( StringGrid_ProgramsList.Name + ' editing has finished.' ));
159 for loop := 1 to StringGrid_ProgramsList.Cols[DataNameCol].Count -1 do
160 begin
161 if (StringGrid_ProgramsList.Cells[DataNameCol,loop]) = '' then
162 begin
163 StringGrid_ProgramsList.MoveColRow(false,loop, (StringGrid_ProgramsList.Cols[DataNameCol].Count -1)) ;
164 StringGrid_ProgramsList.Rows[StringGrid_ProgramsList.Cols[DataNameCol].Count -1].Clear ;
165 end;
166 end;
168 {Fill it}
169 ProgramsUpdate();
170 end;
172 procedure TForm4.ProgramsUpdate();
174 B:integer;
175 PrefixInt:integer;
176 begin
177 {Fill array}
178 PrefixInt := FindPrefixInt(ComboBox_PreFix.Items.Strings[ComboBox_PreFix.ItemIndex]);
179 for B := 0 to Data[PrefixInt].Grid.RowCount -1 do
180 begin
181 Data[PrefixInt].Grid.Rows[B].Text := StringGrid_ProgramsList.Rows[B].text;
182 end;
183 end;
185 function TForm4.PreFixExists():boolean;
187 PreFixFolders:Tstrings;
188 CheckList:Tstrings;
189 loop:integer;
190 ChannelLocal:string;
191 begin
192 ChannelLocal := ('PreFixExists');
193 { TODO : procedure for telling you a procedure has being called. }
194 Log(5, '', ('-----------------'));
195 Log(0, ChannelLocal, ('PreFixExists has being Called.'));
197 CheckList := TStringlist.Create;
198 PreFixFolders := TStringlist.Create;
200 CheckList.Clear;
201 CheckList.Add('/dosdevices') ;
202 CheckList.Add('/drive_c') ;
204 { TODO : ListFileDir can only handly one path at ones }
206 ListFileDir({0} PathToPrefix, {1} PreFixFolders, {2} nil, {3} nil, {4} nil, {5} false);
208 for loop := 0 to (PreFixFolders.Count -1) do
209 begin
210 if AnsiEndsText('~', PreFixFolders.Strings[loop]) = false then
211 begin
212 { Folder and file check. }
214 if DoesFoldersExists((PathToPrefix + PreFixFolders.Strings[loop]), CheckList ) = true then
215 begin
216 if FileExistsAndIsExecutable(PathToPrefix + PreFixFolders.Strings[loop] + '/system.reg',true) = true then
217 begin
218 if FileExistsAndIsExecutable(PathToPrefix + PreFixFolders.Strings[loop] + '/user.reg', true) = true then
219 begin
220 PreFixList[0].add(PreFixFolders.Strings[loop]);
221 PreFixList[1].add(PathToPrefix + PreFixFolders.Strings[loop]);
222 end;
223 end;
224 end;
225 end;
227 end;
229 if PreFixList[0].Count <> 0 then
230 begin
231 {Sych Tstrings with Comobox.}
232 UnitMain.Form1.ComboBox_PreFix.Items := PreFixList[0];
233 ComboBox_PreFix.Items := PreFixList[0];
235 {Cheep hack to stop crashs.}
236 UnitMain.Form1.ComboBox_PreFix.Text := UnitMain.Form1.ComboBox_PreFix.Items[0];
237 ComboBox_PreFix.Text := ComboBox_PreFix.Items[0];
238 Result := true;
240 else
241 begin
242 Result := false;
243 end;
246 {Clean up.}
247 CheckList.Destroy;
248 PreFixFolders.Destroy;
250 end;
253 procedure TForm4.FormClose(Sender: TObject; var CloseAction: TCloseAction);
255 LocalChannel:string;
256 TempInt:Integer;
257 loop:Integer;
258 begin
259 { TODO : Set LocalChannel. }
260 LocalChannel := '';
262 Log(5, LocalChannel, ('-----------------'));
263 Log(3, LocalChannel, ( Form4.Name + ' close has happen.' ));
265 {Save Prefix data.}
266 for loop := 0 to (PreFixList[0].Count -1) do
267 begin
268 ProgramsListSavePrefix(PreFixList[0][loop]);
269 end;
272 {Temp save of StringGrid_TerminalSettings}
273 if TerrminalSettingsIsFine = true then
274 begin
275 Log(3, LocalChannel, ( 'Saving' + Wrap(StringGrid_TerminalSettings.Name) + 'to' + Wrap(ConfigPath + '/StringGrid_TerminalSettings') ));
276 StringGrid_TerminalSettings.SaveOptions := [soDesign,{soAttributes,}soContent{,soPosition}] ;
277 {TODO error checking for save.}
278 StringGrid_TerminalSettings.SaveToFile( ConfigPath + '/StringGrid_TerminalSettings' );
280 else
281 begin
282 Log(1, LocalChannel, ('Not saving' + Wrap(StringGrid_TerminalSettings.Name) + 'because the is something wrong with it.'));
283 end;
285 if Mode = false then
286 begin
287 {Refresh form1.}
288 {Remember the selected item.}
289 TempInt := UnitMain.Form1.ComboBox_ProgramsList.ItemIndex;
290 {Update ComboBox_PreFix, This clears ComboBox_ProgramsList.}
291 UnitMain.Form1.ComboBox_PreFixChange(self);
292 {Write back the remembered selected item.}
293 UnitMain.Form1.ComboBox_ProgramsList.ItemIndex := TempInt;
294 {This updates EditBox_ProgramPath.}
295 UnitMain.Form1.ComboBox_ProgramsListChange(self);
297 {Note flag settings are losted.}
298 end;
300 end;
302 procedure TForm4.ComboBox_ShellPathEditingDone(Sender: TObject);
304 LocalChannel:string;
305 begin
306 { TODO : Set LocalChannel. }
307 LocalChannel := '';
309 Log(5, LocalChannel, ('-----------------'));
310 Log(3, LocalChannel, ( ComboBox_ShellPath.Name + ' editing has finished.' ));
312 if FileExistsAndIsExecutable( {FullPath:string}ComboBox_ShellPath.text, {JustCheckExists:boolean}false ) = true then
313 begin
314 ShellPathIsFine := true;
315 { TODO -cColour : Work out why "clwindow" does not work. }
316 ComboBox_ShellPath.Color := clwhite;
318 else
319 begin
320 ShellPathIsFine := false;
321 ComboBox_ShellPath.Color := clred;
322 end;
324 end;
326 procedure TForm4.ComboBox_PreFixChange(Sender: TObject);
328 B:integer;
329 PrefixInt:integer;
330 begin
331 StringGrid_ProgramsList.Clean ;
332 StringGrid_ProgramsList.FixedRows := 1 ;
333 PrefixInt := FindPrefixInt(ComboBox_PreFix.Items.Strings[ComboBox_PreFix.ItemIndex]);
335 for B := 0 to 100 do
336 begin
337 StringGrid_ProgramsList.Rows[B].text := Data[PrefixInt].Grid.Rows[B].Text;
338 end;
339 end;
342 procedure TForm4.StringGrid_TerminalSettingsEditingDone(Sender: TObject);
344 LocalChannel:string;
345 loop:integer;
346 loop2:integer;
347 IsFine:boolean;
348 begin
349 { TODO : Went Editing this runs two times, abit pointless and not CPU friendy}
350 { TODO : May add some checks to the flags. Can that be done ?}
351 { TODO : Set LocalChannel. }
352 LocalChannel := '';
353 IsFine := true;
356 Log(5, LocalChannel, ('-----------------'));
357 Log(3, LocalChannel, ( StringGrid_TerminalSettings.Name + ' editing has finished.'));
359 {USEFUL INFO: stringGrid.Cells[col,row] }
361 {Check the no row is blank other then the last one}
362 if IsFine = true then
363 begin
364 for loop := 0 to ( StringGrid_TerminalSettings.RowCount -2 )do {Ignore last row}
365 begin
366 {TODO : check the whole row not just the frist cell }
367 if StringGrid_TerminalSettings.Cells[0,loop] = '' then {delete it!}
368 begin
369 StringGrid_TerminalSettings.DeleteColRow(false,loop);
370 Log(4, LocalChannel, ('Not Fine deleting row.'));
371 exit; {exit or out of range error}
373 else
374 begin
376 end;
377 end; {end loop}
378 end;
381 {Check the Names are unique or you can get the wrong path}
382 if IsFine = true then
383 begin
384 //UnitMain.form1.log('Running Check the Names are unique');
385 for loop := 1 to ( StringGrid_TerminalSettings.RowCount -1 )do
386 begin
387 for loop2 := 0 to ( StringGrid_TerminalSettings.RowCount -1 )do
388 begin
389 if StringGrid_TerminalSettings.cells[0,loop] = StringGrid_TerminalSettings.cells[0,loop2] then
390 begin
391 if loop2 <> loop then
392 begin
393 { Error }
394 Log(4, LocalChannel, ( 'Col(0)Row(' + inttostr(loop) + ') has the same name as Col(0)Row(' + inttostr(loop2) + ').'));
395 IsFine := false;
397 else
398 begin
399 { All fine }
400 end;
401 end;
402 end; {loop2 end}
403 end;{loop end}
404 end;
407 {Check that Path does exists and is executable.}
408 if IsFine = true then
409 begin
410 for loop := 0 to ( StringGrid_TerminalSettings.RowCount -1 )do
411 begin
412 if loop = ( StringGrid_TerminalSettings.RowCount -1 ) then
413 begin
414 if StringGrid_TerminalSettings.Cells[0,loop] <> '' then
415 begin
416 if FileExists( StringGrid_TerminalSettings.Cells[1,loop] ) then
417 begin
418 Log(3, LocalChannel, ('Col(1)Row(' + inttostr(loop) + ') file ( ' + StringGrid_TerminalSettings.Cells[1,loop] + ' ) does exists.'));
419 if FpAccess( StringGrid_TerminalSettings.Cells[1,loop] , X_OK {Exe flag check}) = 0 then
420 begin
421 Log(1, LocalChannel, ('Col(1)Row(' + inttostr(loop) + ') file ( ' + StringGrid_TerminalSettings.Cells[1,loop] + ' ) is executable.'));
423 else
424 begin
425 { else FpAccess }
426 IsFine := false;
427 Log(1, LocalChannel, ('Col(1)Row(' + inttostr(loop) + ') file (' + StringGrid_TerminalSettings.Cells[1,loop] + ' ) is not executable.'));
428 end;
430 else
431 begin
432 { else FileExists }
433 IsFine := false;
434 Log(1, LocalChannel, ('Col(1)Row(' + inttostr(loop) + ') file ( ' + StringGrid_TerminalSettings.Cells[1,loop] + ' ) does not exists.'));
435 end;
436 end; { StringGrid_TerminalSettings.Cells[0,loop] <> '' END }
438 else { ELSE loop = ( StringGrid_TerminalSettings.RowCount -1 ) }
439 begin
441 end;
443 end; {loop end}
444 end;
446 {if last row is used then add a new one}
447 if IsFine = true then
448 begin
449 if StringGrid_TerminalSettings.cells[0,(StringGrid_TerminalSettings.RowCount -1)] <> '' then
450 begin
451 StringGrid_TerminalSettings.RowCount := ( StringGrid_TerminalSettings.RowCount + 1 );
452 end;
453 end;
455 {Report!}
456 if Isfine = true then
457 begin
458 TerrminalSettingsIsFine := true;
459 Log(0, LocalChannel, ('Terminal settings are fine.'));
461 else
462 begin
463 TerrminalSettingsIsFine := false;
464 Log(1, LocalChannel, ('The is an error in the terminal settings.'));
465 end;
468 {sych}
469 if Isfine = true then
470 begin
471 TerminalPathClean.Clear;
472 for loop := 0 to ( StringGrid_TerminalSettings.RowCount -2 )do {Last row is ignore WILL be blank}
473 begin
474 TerminalPathClean.Add(StringGrid_TerminalSettings.Cells[0,loop] );
475 end;
477 UnitMain.Form1.ComboBox_TerminalName.Items := TerminalPathClean;
478 {INFO this will change your seleted item back to the first item, not the worlds best code but it stops crashs}
479 UnitMain.Form1.ComboBox_TerminalName.Text := UnitMain.Form1.ComboBox_TerminalName.Items[0];
480 end;
482 end;
487 initialization
488 {$I UnitSettings.lrs}
490 end.