UnitInitialization: Move to the new folder system.
[WineLauncher.git] / Functions / MainBackend / UnitMainBackend.pas
blob40dc16455d48348aaf5c75f67867eccb7f8e17de
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 UnitMainBackend;
18 {$mode objfpc}{$H+}
20 interface
22 uses
23 Classes, SysUtils, XMLCfg, Process, BaseUnix;
25 procedure SaveLastUsedConfig();
26 procedure LoadLastUsedConfig();
27 procedure OnWineVersionChange();
29 function RunWineCheck(ProgramNameOverRide:string; ProgramFlagsOverRide:string):boolean;
30 function ListProgramsOnDisc(ModeMe:TStrings; DriveChar:char):boolean;
32 var
33 XmlLastUsedConfig:TXMLConfig;
34 LastUsedConfigLoading:boolean;
36 {The next three are used by ListProgramsOnDisc.}
37 LastReadLinkPath:string;
38 DiscErrorText:string;
39 OldPath:string;
41 implementation
42 uses
43 UnitInitialization,
44 UnitMain,
45 UnitMisc,
46 UnitSettings,
47 UnitProgramsList;
49 procedure SaveLastUsedConfig();
50 begin
51 if LastUsedConfigLoading = true then exit;
52 Try
53 {1}
54 XmlLastUsedConfig.SetValue('Version', '2');
55 {Was WineVersionInfo/DistributionName,DistributionVersion,Architecture.}
56 XmlLastUsedConfig.SetValue('WineVersionInfo/WineVersion', UnitMain.form1.ComboBox_WineVersion.Text);
57 {2}
58 XmlLastUsedConfig.SetValue('WineVersionInfo/UsesTerminal', UnitMain.form1.Check_Terminal.Checked);
59 XmlLastUsedConfig.SetValue('WineVersionInfo/UsesSoundWrapper', UnitMain.form1.CheckBox_UseSoundWrapper.Checked);
61 {Save}
62 XmlLastUsedConfig.Filename := (ConfigPath + '/LastUsedConfig.xml');
63 Finally
64 {Clean up}
65 XmlLastUsedConfig.Flush;
66 end;
67 end;
69 procedure LoadLastUsedConfig();
70 var
71 LastUsedConfigVersion:string;
72 begin
73 {Only run this ones wine has being scanned for.}
74 LastUsedConfigLoading := true;
75 XmlLastUsedConfig:= TXMLConfig.Create(nil);
77 Try
78 XmlLastUsedConfig.Filename := (ConfigPath + '/LastUsedConfig.xml');
79 {Get the version of the config file.}
80 LastUsedConfigVersion := XmlLastUsedConfig.GetValue('Version','');
81 {$IFDEF LogVar}
82 LogVar(LastUsedConfigVersion,'LastUsedConfigVersion');
83 {$ENDIF}
85 {Make sure the version value exists.}
86 if LastUsedConfigVersion = '-1' then
87 begin
88 Log(3, Channel, 'Invalided file version, never mind we ignore it and wirte over it.');
89 Exit();
90 end;
91 {1}
92 {Setting ItemIndex does not trigger OnEditingDone.}
93 {So we run Enter to update the list then we run EditingDone to check it}
95 SelectItem(UnitMain.form1.ComboBox_WineVersion, XmlLastUsedConfig.GetValue('WineVersionInfo/WineVersion', ''));
96 UnitMain.form1.ComboBox_WineVersionEnter(form1);
97 UnitMain.form1.ComboBox_WineVersionEditingDone(form1);
99 {2}
100 UnitMain.form1.Check_Terminal.Checked := XmlLastUsedConfig.GetValue('WineVersionInfo/UsesTerminal', false);
101 UnitMain.form1.CheckBox_UseSoundWrapper.Checked := XmlLastUsedConfig.GetValue('WineVersionInfo/UsesSoundWrapper', false);
103 Finally
104 XmlLastUsedConfig.Flush;
105 LastUsedConfigLoading := false;
106 end;
107 end;
109 function RunWineCheck(ProgramNameOverRide:string; ProgramFlagsOverRide:string):boolean;
111 loop:integer;
112 SoundWrapper:string;
113 temp00:integer;
114 WinePreFixRaw:string;
115 WinePreFix:string;
116 ChannelLocal:string;
117 ProgramFlags:string;
118 ProgramName:string;
119 WorkDir:string;
120 UnixPathWithOutFile:string;
121 WinePath:string; {~/.local/wine/[Wine Version]"}
122 begin
123 ChannelLocal := ('RunWineCheck');
125 WinePath := (PathToWine + UnitMain.form1.ComboBox_WineVersion.Text);
127 {Get wine version info}
128 if FileExistsAndIsExecutable((WinePath + '/wine'), false) = true then
129 begin
130 wine_version_full := (WinePath + '/wine');
131 {$IFDEF LogVar}
132 LogVar(wine_version_full, 'wine_version_full');
133 {$ENDIF}
135 else
136 if FileExistsAndIsExecutable((WinePath + '/bin/wine'), false) = true then
137 begin
138 wine_version_full := (WinePath + '/bin/wine');
139 {$IFDEF LogVar}
140 LogVar(wine_version_full, 'wine_version_full');
141 {$ENDIF}
143 else
144 begin
145 Log(0, ChannelLocal, 'Can not find wine');
146 exit(false);
147 end;
149 {Get Terminal info}
150 if UnitMain.form1.Check_Terminal.Checked = true then
151 begin
152 if UnitMain.form1.ComboBox_TerminalName.Text <> '' then
153 begin
154 temp00 := UnitMain.form1.ComboBox_TerminalName.ItemIndex;
155 RunIn := (UnitSettings.Form4.StringGrid_TerminalSettings.Cells[1,temp00] + ' ' + UnitSettings.Form4.StringGrid_TerminalSettings.Cells[2,temp00]);
156 Log(0, ChannelLocal, ('Using Terminal' + Wrap(RunIn)) );
157 end;
159 else
160 begin
161 {After the Program is closed the child process is still listed.}
162 RunIn := UnitSettings.Form4.ComboBox_ShellPath.text;
163 Log(0, ChannelLocal, ('Using' + Wrap(RunIn)) );
164 end;
166 {PreFix}
167 WinePreFixRaw := (PreFixList[1].Strings[UnitMain.form1.ComboBox_PreFix.ItemIndex]);
168 WinePreFix := ('env WINEPREFIX="' + WinePreFixRaw + '"');
169 {$IFDEF LogVar}
170 LogVar(WinePreFix, 'WinePreFix');
171 {$ENDIF}
174 {Sound Wrapper}
175 if UnitMain.form1.CheckBox_UseSoundWrapper.Checked = true then
176 begin
177 if FileExists(UnitMain.form1.ComboBox_SoundWrapper.Text) = true then
178 begin
179 SoundWrapper := (UnitMain.form1.ComboBox_SoundWrapper.Text + ' ');
180 Log(0, ChannelLocal, ('File' + Wrap(UnitMain.form1.ComboBox_SoundWrapper.Text) + 'exists'));
182 else
183 begin
184 Log(1, ChannelLocal, ('File' + Wrap(UnitMain.form1.ComboBox_SoundWrapper.Text) + 'does not exists'));
185 exit(false);
186 end;
188 else
189 begin
190 Log(0, ChannelLocal, ('No sound wrapper will be used'));
191 end;
193 {Set program flags}
194 if ProgramNameOverRide = '' then
195 begin
196 if UnitMain.form1.EditBox_ProgramPath.Text <> '' then
197 begin
198 ProgramFlags := '';
199 for loop := 0 to (UnitMain.form1.CheckListBox_Flags.Items.Count) do
200 begin
201 if UnitMain.form1.CheckListBox_Flags.Checked[loop] = true then
202 begin
203 ProgramFlags := (ProgramFlags + ' ' + UnitMain.form1.CheckListBox_Flags.Items.ValueFromIndex[loop]);
204 end;
205 end;
206 {$IFDEF LogVar}
207 LogVar(ProgramFlags, 'ProgramFlags' );
208 {$ENDIF}
209 ProgramName := (' ' + '"' + UnitMain.form1.EditBox_ProgramPath.Text + '"' + ProgramFlags + ' ');
211 else
212 begin
213 Log(1, ChannelLocal, ('Please select a program.'));
214 exit(false);
215 end;
217 else
218 begin
219 ProgramName := (' ' + '"' + ProgramNameOverRide + '"' + ProgramFlagsOverRide + ' ');
220 end;
222 {Set the WorkDir}
223 UnixPathWithOutFile := ExtractFilePath(WinToUnixPath(UnitMain.form1.EditBox_ProgramPath.Text));
224 if ProgramNameOverRide = '' then
225 begin
226 if DoesFolderExists(ExtractFilePath(UnixPathWithOutFile), ExtractFileName(UnixPathWithOutFile)) then
227 begin
228 WorkDir := WorkDirTemplate(GetUnixDirPath(WinToUnixPath(UnitMain.form1.EditBox_ProgramPath.Text)) );
230 else
231 begin
232 WorkDir := WorkDirTemplate(WinePreFixRaw + '/dosdevices/c:/windows');
233 end;
235 else
236 begin
237 WorkDir := WorkDirTemplate(WinePreFixRaw + '/dosdevices/c:/windows');
238 end;
240 {Setup start_S1}
241 start_S1 := ('#! /bin/sh' + {linebrake}#10 + WorkDir {Line brake done in template.} + WinePreFix + SoundWrapper + ' ' + wine_version_full + ' ' + ProgramName + ';');
242 {$IFDEF LogVar}
243 LogVar(start_S1, 'start_S1');
244 {$ENDIF}
246 {Create Script file}
247 if MakeFile((ConfigPath + '/1'), start_S1) = false then
248 begin
249 log(1, ChannelLocal, ('Can not make file' + wrap(ConfigPath + '/1')) );
250 exit(false);
251 end;
253 {Set executable flag}
254 if SetExecutableFlag(ConfigPath + '/1') = false then
255 begin
256 log(1, ChannelLocal, ('Can not set executable flag on file'+ wrap(ConfigPath + '/1')) );
257 exit(false)
258 end;
260 {Execute script one}
261 Log(0, ChannelLocal, ('Script One has being executed.'));
262 AProcess.CommandLine := (RunIn + ' ' + ConfigPath +'/1');
263 {After the Program is closed the child process is still listed.}
264 AProcess.Execute;
265 Result := true;
266 end;
268 function ListProgramsOnDisc(ModeMe:TStrings; DriveChar:char):boolean;
270 script:string;
271 Path:string;
272 Info:TSearchRec;
273 Count:Longint;
274 FindPath:string;
275 begin
276 {TODO : This function needs to check the drive not just the folders.}
277 Path := (PathToPrefix + UnitMain.Form1.ComboBox_PreFix.Text + '/dosdevices/' + AnsiLowerCase(DriveChar));
279 {Do not rescan if the path is the same as the last one.}
280 {Note if you change disks it will not rescan it.}
281 if OldPath = Path then exit(false);
283 if AsyncProcessScan.Active = false then
284 begin
285 OldPath := Path;
286 ModeMe.Clear;
287 LastReadLinkPath := fpReadLink(Path + ':');
289 else exit(false);
291 if LastReadLinkPath = '' then
292 begin
293 log(4, '',('Link ' + Wrap(Path + ':') + 'does not exists.'));
294 LastReadLinkPath := '';
295 DiscErrorText := 'Please map' + wrap(DriveChar + ':') + 'to your cd / dvd drive, You can do that in winecfg.';
296 exit(false);
297 end;
299 {Reset count.}
300 Count := 0;
301 if FindFirst((Path + ':/*'), faAnyFile and faDirectory, Info) = 0 then
302 begin
303 Repeat
304 Inc(Count);
305 Until FindNext(info) <> 0;
306 if Count < 3 then
307 begin
308 log(0,'', Info.Name);
309 DiscErrorText := 'You have no disc in your drive.';
310 LastReadLinkPath := '';
311 exit(false);
312 end;
313 end;
314 FindClose(Info);
316 if LastReadLinkPath = '/' then
317 begin
318 DiscErrorText := 'You can not map '+ DriveChar + ': to root.';
319 LastReadLinkPath := '';
320 exit(false);
321 end;
323 if LastReadLinkPath = GetEnvironmentVariable('HOME') then
324 begin
325 DiscErrorText := 'You can not map '+ DriveChar + ': to your home folder.';
326 LastReadLinkPath := '';
327 exit(false);
328 end;
330 FindPath := SearchForBin('find');
331 if FindPath = '' then
332 begin
333 DiscErrorText := ('Can not find the program called' + Wrap('find'));
334 LastReadLinkPath := '';
335 exit(false);
336 end;
338 script := (FindPath + ' -H ' + Path + ': -iname *.exe');
339 Log(0, '', 'Script Scan has being executed.');
340 AsyncProcessScan.CommandLine := script;
341 AsyncProcessScan.Execute;
342 DiscErrorText := 'Scaning';
343 Timer.Enabled := true;
345 Result := true;
347 end;
349 procedure OnWineVersionChange();
350 begin
351 if UnitMain.form1.ComboBox_WineVersion.Text <> '' then
352 begin
353 UnitMain.form1.Btn_Run.Enabled := true;
355 else
356 UnitMain.form1.Btn_Run.Enabled := false;
357 end;
359 end.