UnitMain: Fixed more leaks.
[WineLauncher.git] / UnitMain.pas
bloba2ad3498db0074671de21bd201b453fb0913426a
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 {Made with Lazarus 0.9.26.3 fixes branch.}
18 {Free Pascal Compiler version 2.2.5.}
20 unit UnitMain;
22 {$mode objfpc}{$H+}
24 interface
26 uses
27 Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls,
28 DbCtrls, BaseUnix, Process, CheckLst, ExtCtrls, ComCtrls, UnitCreatePrefix, UnitAbout, Errors;
30 type
32 { TForm1 }
34 TForm1 = class(TForm)
35 Btn_Run: TButton;
36 Btn_DebugChannelsConfig: TButton;
37 Btn_Settings: TButton;
38 Btn_StopScan: TButton;
39 Button_Regedit: TButton;
40 Button_Winecfg: TButton;
41 CheckBox_UseSoundWrapper: TCheckBox;
42 CheckBox_DebugChannelUse: TCheckBox;
43 CheckListBox_Flags: TCheckListBox;
44 Check_Terminal: TCheckBox;
45 ComboBox_WineVersion: TComboBox;
46 ComboBox_ProgramsList: TComboBox;
47 ComboBox_PreFix: TComboBox;
48 ComboBox_SoundWrapper: TComboBox;
49 ComboBox_TerminalName: TComboBox;
50 EditBox_ProgramPath: TEdit;
51 Label_WineVersion: TLabel;
52 Label_Flags: TLabel;
53 Label_ProgramPath: TLabel;
54 Label_ProgramsList: TLabel;
55 Label_WinePreFix: TLabel;
56 Memo_LogOutPut: TMemo;
57 PageControl1: TPageControl;
58 Tab_Log: TTabSheet;
59 TogBtn_Listdrive: TToggleBox;
61 procedure Btn_DebugChannelsConfigClick(Sender: TObject);
62 procedure Btn_RunClick(Sender: TObject);
63 procedure Btn_SettingsClick(Sender: TObject);
64 procedure Btn_StopScanClick(Sender: TObject);
65 procedure Button_RegeditClick(Sender: TObject);
66 procedure Button_WinecfgClick(Sender: TObject);
67 procedure ComboBox_PreFixChange(Sender: TObject);
68 procedure ComboBox_ProgramsListChange(Sender: TObject);
69 procedure ComboBox_WineVersionEditingDone(Sender: TObject);
70 procedure ComboBox_WineVersionEnter(Sender: TObject);
71 procedure EditBox_ProgramPathChange(Sender: TObject);
72 procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
73 procedure FormCreate(Sender: TObject);
74 procedure FormDestroy(Sender: TObject);
75 procedure TogBtn_ListdriveChange(Sender: TObject);
76 procedure TimerOnTimer(Sender: TObject);
78 private { private declarations }
80 public { public declarations }
82 end;
84 var
85 Form1: TForm1;
86 start_S1:string; { Script to write to the file. }
87 RunIn:string; { terminal or shell set here }
88 NoError:Boolean; { Tbh this should not be here but im not sure need to check. }
89 Sh_Path:string; { Path for the shell }
90 GetCurrentDir:string;
91 Channel:string; { Debug channel name }
92 UnixDirPath:string; { Needed for setting the working dir at 'Run'. }
93 Mode:boolean;
94 Timer:TTimer; {The timer is a workaround.}
95 {Frames}
96 FrameCreatePrefix:TFrameCreatePrefix;
97 FrameAbout:TFrameAbout;
99 implementation
100 uses
101 UnitInitialization,
102 UnitMainBackend,
103 UnitMisc,
104 UnitDebugConfig,
105 UnitSettings,
106 UnitProgramsList;
108 procedure TForm1.FormCreate(Sender: TObject);
109 begin
110 {The timer is a workaround.}
111 Timer := TTimer.Create(nil) ;
112 Timer.OnTimer := @TimerOnTimer;
113 Timer.Enabled := false;
114 Timer.Interval := 100;
116 {Scan for wine.}
117 ComboBox_WineVersionEnter(Self);
119 {Select first item.}
120 if ComboBox_SoundWrapper.items.Count <> 0 then ComboBox_SoundWrapper.Text := ComboBox_SoundWrapper.Items[0];
121 if ComboBox_TerminalName.items.Count <> 0 then ComboBox_TerminalName.Text := ComboBox_TerminalName.Items[0];
122 if ComboBox_WineVersion.Items.Count <> 0 then ComboBox_WineVersion.Text := ComboBox_WineVersion.Items[0];
124 {Frame Create prefix.}
125 AddTabToPageControl(PageControl1, 'Create prefix');
126 FrameCreatePrefix := TFrameCreatePrefix.Create(Self);
127 EmbedFrame(FrameCreatePrefix, PageControl1.PageCount -1);
128 {Frame about.}
129 AddTabToPageControl(PageControl1, 'About');
130 FrameAbout := TFrameAbout.Create(Self);
131 EmbedFrame(FrameAbout, PageControl1.PageCount -1);
133 FrameAbout.Memo_About.Lines.Add('');
134 {When the is a new release update the version number in the file called 'VERSION.inc'.}
135 FrameAbout.Memo_About.Lines.Add('WineLauncher Version: ' + WineLauncherVersion);
136 FrameAbout.Memo_About.Lines.Add(BuiltWithFpcVersion);
138 {Load LastUsedConfig.}
139 LoadLastUsedConfig();
141 {$IFDEF GuiTest}
142 Btn_StopScan.Visible := true;
143 {$ENDIF}
144 end;
146 procedure TForm1.FormDestroy(Sender: TObject);
147 begin
148 Timer.Destroy;
149 XmlLastUsedConfig.Destroy;
150 SetLength(TabSheets, 0);
151 SetLength(Data, 0);
152 FreeAndNil(CdData);
153 FreeAndNil(CdDataName);
155 {stuff configed in UnitInitialization.}
156 AProcess.Destroy;
157 CreatePrefixProcess.Destroy;
158 AsyncProcessScan.Destroy;
159 end;
161 procedure TForm1.TimerOnTimer(Sender: TObject);
163 TempList:Tstrings;
164 begin
165 {We should not use a timer to check this. The is a bug in lazarus, 'OnTerminate' of AsyncProcess never runs.}
166 if AsyncProcessScan.Active = true then
167 begin
168 Timer.Enabled := true;
169 Btn_StopScan.Visible := true;
171 else
172 begin
173 Btn_StopScan.Visible := false;
174 TempList := TStringlist.Create;
175 Timer.Enabled := false;
176 TempList.LoadFromStream(AsyncProcessScan.OutPut);
178 if TempList.Count = 0 then
179 begin
180 DiscErrorText := 'Drive' + wrap('D' + ':') + 'has no executable files.';
181 LastReadLinkPath := '';
183 else
184 begin
185 DiscErrorText := '';
186 end;
188 while TempList.Count <> 0 do
189 begin
190 CdData.Add(UnixToWinPath(TempList.Strings[0],'d'));
191 TempList.Delete(0);
192 end;
194 ComboBox_PreFixChange(self);
195 TempList.Destroy;
197 CheckListBox_Flags.Clear;
198 end;
199 end;
201 procedure TForm1.TogBtn_ListdriveChange(Sender: TObject);
202 begin
203 if Mode = true then
204 begin
205 Mode := false;
206 AsyncProcessScan.Active := false;
207 Btn_StopScan.Visible := false;
208 CheckListBox_Flags.Enabled := true;
209 EditBox_ProgramPath.Clear;
211 FreeAndNil(CdData);
212 FreeAndNil(CdDataName);
214 ComboBox_PreFixChange(self);
215 ComboBox_ProgramsListChange(self);
217 else
218 begin
219 Mode := true;
220 CdData := TStringlist.Create;
221 CdDataName := TStringlist.Create;
223 log(0,'', LastReadLinkPath);
224 ComboBox_PreFixChange(self);
225 CheckListBox_Flags.Clear;
226 EditBox_ProgramPath.Clear;
227 end;
228 end;
233 procedure TForm1.Btn_RunClick(Sender: TObject);
234 begin
235 RunWineCheck( '', '' );
236 end;
238 procedure TForm1.Btn_SettingsClick(Sender: TObject);
239 begin
240 UnitSettings.Form4.Show ;
241 end;
243 procedure TForm1.Btn_StopScanClick(Sender: TObject);
244 begin
245 AsyncProcessScan.Active := false;
246 end;
248 procedure TForm1.Button_RegeditClick(Sender: TObject);
249 begin
250 RunWineCheck( 'Regedit', '' );
251 end;
253 procedure TForm1.Button_WinecfgClick(Sender: TObject);
254 begin
255 RunWineCheck( 'winecfg', '' );
256 end;
259 procedure TForm1.ComboBox_PreFixChange(Sender: TObject);
261 myloop:integer;
262 begin
263 ComboBox_ProgramsList.Items.Clear;
265 EditBox_ProgramPath.Enabled := true;
266 ComboBox_ProgramsList.Enabled := true;
268 if Mode = false then
269 begin
270 for myloop := 1 to (Data[ComboBox_PreFix.ItemIndex].Grid.RowCount -1) do
271 begin
272 if Data[ComboBox_PreFix.ItemIndex].Grid.Cells[DataNameCol, myloop] <> '' then
273 begin
274 ComboBox_ProgramsList.Items.Add(Data[ComboBox_PreFix.ItemIndex].Grid.Cells[DataNameCol, myloop]);
276 else
277 begin
278 break;
279 end;
280 end;
282 if ComboBox_ProgramsList.Items.Count <> 0 then
283 begin
284 ComboBox_ProgramsList.ItemIndex := 0;
285 EditBox_ProgramPath.Enabled := true;
286 ComboBox_ProgramsList.Enabled := true;
287 ComboBox_ProgramsListChange(Self);
289 else
290 begin
291 ComboBox_ProgramsList.Text := '';
292 EditBox_ProgramPath.Text := '';
293 EditBox_ProgramPath.Enabled := false;
294 ComboBox_ProgramsList.Enabled := false;
295 end;
298 else
299 begin
300 ListProgramsOnDisc(CdData, 'D');
301 if CdData.Text <> '' then
302 begin
303 CdDataName.Clear;
304 CdDataName.AddStrings(CdData);
306 {Write some code to clean up the names, keep then usefull and unique.}
308 ComboBox_ProgramsList.Items := CdDataName;
309 if ComboBox_ProgramsList.Items.Count <> 0 then
310 begin
311 ComboBox_ProgramsList.Text := ComboBox_ProgramsList.Items[0];
312 EditBox_ProgramPath.Enabled := true;
313 ComboBox_ProgramsList.Enabled := true;
314 ComboBox_ProgramsListChange(self);
315 end;
318 else
319 begin
320 ComboBox_ProgramsList.Items.Add(DiscErrorText);
321 ComboBox_ProgramsList.ItemIndex := 0;
322 EditBox_ProgramPath.Text := '';
323 EditBox_ProgramPath.Enabled := false;
324 ComboBox_ProgramsList.Enabled := false;
325 CheckListBox_Flags.Clear;
326 end;
328 end;
329 end;
331 procedure TForm1.ComboBox_ProgramsListChange(Sender: TObject);
333 ProgramsListSelectedItem:integer;
334 PrefixInt:integer;
335 begin
336 if Mode = false then
337 begin
338 PrefixInt := FindPrefixInt(ComboBox_PreFix.Items.Strings[ComboBox_PreFix.ItemIndex]);
339 EditBox_ProgramPath.Enabled := true;
340 {Plus one to line it up with the grid}
341 ProgramsListSelectedItem := (Data[PrefixInt].Grid.cols[DataNameCol].IndexOf(Data[PrefixInt].Grid.cols[DataNameCol].Strings[ComboBox_ProgramsList.ItemIndex]) +1);
342 {Cells[Col, Row]}
343 EditBox_ProgramPath.Text := Data[PrefixInt].Grid.Cells[DataPathCol, ProgramsListSelectedItem] ;
344 CutUpFlags(Data[PrefixInt].Grid.Cells[DataFlagsCol, ProgramsListSelectedItem]);
346 else
347 begin
348 if CdData.Text <> '' then
349 begin
350 EditBox_ProgramPath.Text := CdData.Strings[CdDataName.IndexOf(ComboBox_ProgramsList.Text)] ;
351 end;
352 end;
353 end;
355 procedure TForm1.ComboBox_WineVersionEditingDone(Sender: TObject);
356 begin
357 OnWineVersionChange();
358 end;
360 procedure TForm1.ComboBox_WineVersionEnter(Sender: TObject);
362 a:string;
363 SystemSidePath:string;
364 begin
365 {Note FpSymLink does not have a string version with fpc 2.2.5.}
366 {This is not best way adding the system side wine but it works.}
367 SystemSidePath := PathToWine + '/System Side Wine';
368 {link wine.}
369 a := SearchForBin('wine');
370 if SearchForBin(a) <> '' then
371 begin
372 if DirExistsIfNotMakeIt(SystemSidePath) = true then
373 begin
374 if DirExistsIfNotMakeIt(SystemSidePath + '/bin') = true then
375 begin
376 SystemSidePath := (SystemSidePath + '/bin');
377 FpUnlink(SystemSidePath + '/wine');
378 if FpSymLink(PChar(a), PChar(SystemSidePath + '/wine') ) <> 0 then
379 begin
380 log(1,'','Can not create symlink: ' + StrError(fpgeterrno));
381 end;
382 {link wineprefixcreate.}
383 a := SearchForBin('wineprefixcreate');
384 if SearchForBin(a) <> '' then
385 begin
386 FpUnlink(SystemSidePath + '/wineprefixcreate');
387 if FpSymLink(PChar(a), PChar(SystemSidePath + '/wineprefixcreate') ) <> 0 then
388 begin
389 log(1,'','Can not create symlink: ' + StrError(fpgeterrno));
390 end;
391 end;
392 end;
393 end;
394 end;
396 FolderScan(ComboBox_WineVersion, PathToWine);
397 OnWineVersionChange();
398 end;
400 procedure TForm1.EditBox_ProgramPathChange(Sender: TObject);
401 begin
402 ColourCheck(EditBox_ProgramPath);
403 end;
405 procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
406 begin
407 SaveLastUsedConfig();
408 end;
411 procedure TForm1.Btn_DebugChannelsConfigClick(Sender: TObject);
412 begin
413 UnitDebugConfig.Form2.Show ;
414 end;
419 initialization
420 {$I UnitMain.lrs}
422 end.