UnitProgramsList: Fix memory leaks.
[WineLauncher.git] / Functions / ProgramsList / UnitProgramsList.pas
bloba3187557e008dd70d7b8772867145b802a1ac9a9
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 UnitProgramsList;
18 {$mode objfpc}{$H+}
21 interface
23 uses
24 Classes, SysUtils, XMLCfg, Grids;
26 function ProgramsListSavePrefix(PrefixName:string):boolean;
27 function ProgramsListLoadPrefix(PrefixName:string):boolean;
28 function SetUpGrid(A:integer):boolean;
29 function SetUpArray():boolean;
31 type
32 DataStore = record
33 PrefixName:string;
34 Grid:TStringGrid;
35 end;
37 var
38 PreFixList:array of Tstrings;
39 CdData:Tstrings;
40 CdDataName:Tstrings;
41 Data:array of DataStore;
43 const
44 DataNameCol = 0;
45 DataPathCol = 1;
46 DataFlagsCol = 2;
47 PrefixDataName = 'PrefixData.xml';
49 implementation
50 uses
51 UnitInitialization,
52 UnitSettings,
53 UnitMisc,
54 UnitMain;
56 function ProgramsListSavePrefix(PrefixName:string):boolean;
57 var
58 PathWithName:string;
59 FullPathWithName:string;
60 loop:integer;
61 PrefixInt:integer;
62 SaveConfig:TXMLConfig;
63 begin
64 PrefixInt := FindPrefixInt(PrefixName);
66 {Stop if the prefix does not exist.}
67 if PrefixInt = -1 then exit;
69 {Get the prefix path.}
70 PathWithName := (PreFixList[1].Strings[PrefixInt]);
71 {Stop if the prefix path does not exist.}
72 if DoesFolderExists(PathWithName) = false then exit(false);
74 FullPathWithName := (PathWithName + '/' + PrefixDataName);
76 {Delete the old file.}
77 if FileExists(FullPathWithName) then DeleteFile(FullPathWithName);
78 SaveConfig := TXMLConfig.Create(nil);
79 Try
80 SaveConfig.FileName := FullPathWithName;
82 SaveConfig.SetValue('Version', 6);
83 SaveConfig.SetValue('PrefixName', PrefixName);
85 {The frist row is the header}
86 for loop := 1 to (data[PrefixInt].Grid.Cols[DataNameCol].Count -1) do
87 begin
88 {Do not save blank items.}
89 if data[PrefixInt].Grid.Cells[DataNameCol,loop] = '' then exit;
91 {The items need to start at 0.}
92 SaveConfig.SetValue('Item' + InttoStr(loop - 1) + '/Name', data[PrefixInt].Grid.Cells[DataNameCol, loop]);
93 SaveConfig.SetValue('Item' + InttoStr(loop - 1) + '/Path', data[PrefixInt].Grid.Cells[DataPathCol, loop]);
95 if data[PrefixInt].Grid.Cells[DataFlagsCol, loop] <> '' then
96 begin
97 SaveConfig.SetValue('Item' + InttoStr(loop - 1) + '/Flags', data[PrefixInt].Grid.Cells[DataFlagsCol,loop] );
98 end;
100 end;
102 Finally
103 SaveConfig.Flush;
104 SaveConfig.Free;
105 end;
107 end;
109 function ProgramsListLoadPrefix(PrefixName:string):boolean;
111 PathWithName:string;
112 FullPathWithName:string;
113 LoopCount:integer;
114 Done:boolean;
115 PrefixInt:integer;
116 LoadConfig:TXMLConfig;
117 begin
118 PrefixInt := FindPrefixInt(PrefixName);
120 {Stop if the prefix does not exist.}
121 if PrefixInt = -1 then exit;
123 {Get the prefix path.}
124 PathWithName := (PreFixList[1].Strings[PrefixInt]);
126 FullPathWithName := (PathWithName + '/' + PrefixDataName);
128 LoadConfig := TXMLConfig.Create(nil);
130 if FileExists(FullPathWithName) = true then
131 begin
133 LoadConfig.FileName := FullPathWithName;
135 {The items start at 0.}
136 LoopCount := 0;
137 Done := false;
139 if LoadConfig.getValue('Version', -1) = 6 then
140 begin
141 while Done = false do
142 begin
143 {Do not load blank items.}
144 if LoadConfig.GetValue('Item' + InttoStr(LoopCount) + '/Name', '') <> '' then
145 begin
146 {Leave room for the header.}
147 data[PrefixInt].Grid.Cells[DataNameCol, LoopCount +1] := LoadConfig.GetValue('Item' + InttoStr(LoopCount) + '/Name', '');
148 data[PrefixInt].Grid.Cells[DataPathCol, LoopCount +1] := LoadConfig.GetValue('Item' + InttoStr(LoopCount) + '/Path', '');
150 if LoadConfig.GetValue('Item' + InttoStr(LoopCount) + '/Flags', '') <> '' then
151 begin
152 data[PrefixInt].Grid.Cells[DataFlagsCol, LoopCount +1] := LoadConfig.GetValue('Item' + InttoStr(LoopCount) + '/Flags', '');
153 end;
154 inc(LoopCount);
156 else
157 begin
158 Done := true;
159 end;
160 end;
161 end;
163 Finally
164 LoadConfig.Flush;
165 LoadConfig.Free;
166 end;
167 end;
168 end;
170 function SetUpGrid(A:integer):boolean;
171 begin
172 {This can not be nil or the debugger will get upset when you close the program.}
173 Data[A].Grid := TStringGrid.Create(UnitSettings.Form4);
174 Data[A].Grid.RowCount := 101;
175 Data[A].Grid.ColCount := 3;
176 Data[A].PrefixName := PreFixList[0][A];
178 {Fill in the header for the grid.}
179 {Cells[{Col},{Row}]}
180 Data[A].Grid.Cells[DataNameCol,0] := 'Name';
181 Data[A].Grid.Cells[DataPathCol,0] := 'Path';
182 Data[A].Grid.Cells[DataFlagsCol,0] := 'Flags';
183 end;
185 function SetUpArray():boolean;
187 loop:integer;
188 begin
189 {Lay out for Data: array of DataStore}
190 {Type DataStore: string, TStringGrid}
192 SetLength(Data, PreFixList[0].count);
193 for loop := 0 to (PreFixList[0].count -1) do
194 begin
195 SetUpGrid(loop);
196 end;
198 {Load from file(s) into the array}
199 for loop := 0 to (PreFixList[0].Count -1) do
200 begin
201 ProgramsListLoadPrefix(PreFixList[0][loop]);
202 end;
204 {Force the Grid to update, if we do not you can get data lost.}
205 UnitSettings.form4.ComboBox_PreFixChange(UnitSettings.form4);
206 {Force the Prefix Combobox to update, or the ProgramsList comobox will not list anything.}
207 UnitMain.form1.ComboBox_PreFixChange(UnitMain.form1);
208 end;
210 end.