-
Notifications
You must be signed in to change notification settings - Fork 5
/
UConfig.pas
255 lines (208 loc) · 6.06 KB
/
UConfig.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
unit UConfig;
interface
uses System.Generics.Collections;
type
TDefinition = class
Name: string;
Source: string;
Destination: string;
Inclusions: string;
Exclusions: string;
HiddenFiles: Boolean;
Recursive: Boolean;
Delete: Boolean;
LastUpdate: TDateTime;
Checked: Boolean;
end;
TDefinitionList = class(TObjectList<TDefinition>);
TMasksTable = class
Name: string;
Masks: string;
end;
TMasksTableList = class(TObjectList<TMasksTable>);
TConfig = class
private
ConfigFile: string;
Loaded: Boolean;
procedure Load;
procedure Save;
public
CheckForNewVersion: Boolean;
WriteLogFile: Boolean;
SecureMode: Boolean;
Definitions: TDefinitionList;
MasksTables: TMasksTableList;
constructor Create;
destructor Destroy; override;
function FindMasksTable(const Name: string): TMasksTable;
end;
var Config: TConfig;
implementation
uses System.Classes, System.SysUtils, System.IOUtils,
Xml.XmlDoc, Xml.XMLIntf, Soap.XSBuiltIns, System.Variants,
Vcl.Forms;
constructor TConfig.Create;
begin
inherited;
Definitions := TDefinitionList.Create;
MasksTables := TMasksTableList.Create;
ConfigFile := TPath.Combine(ExtractFilePath(Application.ExeName), 'Config.xml');
//default values
CheckForNewVersion := True;
WriteLogFile := True;
SecureMode := True;
Load;
Loaded := True;
end;
destructor TConfig.Destroy;
begin
if Loaded then //avoid losing config file
Save;
Definitions.Free;
MasksTables.Free;
inherited;
end;
{$REGION 'Utils'}
const STR_ENTER = #13#10;
function PipeToEnter(const Text: string): string;
begin
Result := Text.Replace('|', STR_ENTER);
end;
function EnterToPipe(const Text: string): string;
begin
Result := Text.Replace(STR_ENTER, '|');
end;
function XMLStrToTimestamp(V: string): TDateTime;
begin
if not V.IsEmpty then
Result := XMLTimeToDateTime(V)
else
Result := 0;
end;
function TimestampToXMLStr(V: TDateTime): string;
begin
if V>0 then
Result := DateTimeToXMLTime(V)
else
Result := string.Empty;
end;
function GetNode(Parent: IXMLNode; const Name: string): IXMLNode;
begin
Result := Parent.ChildNodes.FindNode(Name);
if Result=nil then
raise Exception.CreateFmt('Node %s\%s not found on config file', [Parent.NodeName, Name]);
end;
type
TNodeValueKind = (nvkString, nvkBoolean);
function GetNodeValue(Parent: IXMLNode; const Name: string; Kind: TNodeValueKind): Variant;
var V: OleVariant;
begin
V := GetNode(Parent, Name).NodeValue;
if (Kind=nvkString) and (V=null) then
Result := string.Empty
else
Result := V;
end;
{$ENDREGION}
procedure TConfig.Load;
var
XML: TXMLDocument;
Root, XDefs, XMsks, N: IXMLNode;
I: Integer;
D: TDefinition;
M: TMasksTable;
begin
if not TFile.Exists(ConfigFile) then Exit;
XML := TXMLDocument.Create(Application);
try
XML.LoadFromFile(ConfigFile);
Root := XML.DocumentElement;
CheckForNewVersion := GetNodeValue(Root, 'CheckForNewVersion', nvkBoolean);
WriteLogFile := GetNodeValue(Root, 'WriteLogFile', nvkBoolean);
SecureMode := GetNodeValue(Root, 'SecureMode', nvkBoolean);
XDefs := GetNode(Root, 'Definitions');
for I := 0 to XDefs.ChildNodes.Count-1 do
begin
N := XDefs.ChildNodes[I];
if N.NodeName='Definition' then
begin
D := TDefinition.Create;
Definitions.Add(D);
D.Name := GetNodeValue(N, 'Name', nvkString);
D.Source := GetNodeValue(N, 'Source', nvkString);
D.Destination := GetNodeValue(N, 'Destination', nvkString);
D.Inclusions := PipeToEnter(GetNodeValue(N, 'Inclusions', nvkString));
D.Exclusions := PipeToEnter(GetNodeValue(N, 'Exclusions', nvkString));
D.HiddenFiles := GetNodeValue(N, 'HiddenFiles', nvkBoolean);
D.Recursive := GetNodeValue(N, 'Recursive', nvkBoolean);
D.Delete := GetNodeValue(N, 'Delete', nvkBoolean);
D.LastUpdate := XMLStrToTimestamp(GetNodeValue(N, 'LastUpdate', nvkString));
D.Checked := GetNodeValue(N, 'Checked', nvkBoolean);
end;
end;
XMsks := GetNode(Root, 'MasksTables');
for I := 0 to XMsks.ChildNodes.Count-1 do
begin
N := XMsks.ChildNodes[I];
if N.NodeName='MasksTable' then
begin
M := TMasksTable.Create;
MasksTables.Add(M);
M.Name := GetNodeValue(N, 'Name', nvkString);
M.Masks := PipeToEnter(GetNodeValue(N, 'Masks', nvkString));
end;
end;
finally
XML.Free;
end;
end;
procedure TConfig.Save;
var
XML: TXMLDocument;
Root, XDefs, XMsks, N: IXMLNode;
D: TDefinition;
M: TMasksTable;
begin
XML := TXMLDocument.Create(Application);
try
XML.Active := True;
Root := XML.AddChild('Config');
Root.AddChild('CheckForNewVersion').NodeValue := CheckForNewVersion;
Root.AddChild('WriteLogFile').NodeValue := WriteLogFile;
Root.AddChild('SecureMode').NodeValue := SecureMode;
XDefs := Root.AddChild('Definitions');
for D in Definitions do
begin
N := XDefs.AddChild('Definition');
N.AddChild('Name').NodeValue := D.Name;
N.AddChild('Source').NodeValue := D.Source;
N.AddChild('Destination').NodeValue := D.Destination;
N.AddChild('Inclusions').NodeValue := EnterToPipe(D.Inclusions);
N.AddChild('Exclusions').NodeValue := EnterToPipe(D.Exclusions);
N.AddChild('HiddenFiles').NodeValue := D.HiddenFiles;
N.AddChild('Recursive').NodeValue := D.Recursive;
N.AddChild('Delete').NodeValue := D.Delete;
N.AddChild('LastUpdate').NodeValue := TimestampToXMLStr(D.LastUpdate);
N.AddChild('Checked').NodeValue := D.Checked;
end;
XMsks := Root.AddChild('MasksTables');
for M in MasksTables do
begin
N := XMsks.AddChild('MasksTable');
N.AddChild('Name').NodeValue := M.Name;
N.AddChild('Masks').NodeValue := EnterToPipe(M.Masks);
end;
XML.SaveToFile(ConfigFile);
finally
XML.Free;
end;
end;
function TConfig.FindMasksTable(const Name: string): TMasksTable;
var
M: TMasksTable;
begin
for M in MasksTables do
if SameText(M.Name, Name) then Exit(M);
Exit(nil);
end;
end.