forked from ahausladen/DDevExtensions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPluginConfig.pas
More file actions
393 lines (339 loc) · 10.8 KB
/
Copy pathPluginConfig.pas
File metadata and controls
393 lines (339 loc) · 10.8 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
{******************************************************************************}
{* *}
{* DDevExtensions *}
{* *}
{* (C) 2006-2007 Andreas Hausladen *}
{* *}
{******************************************************************************}
unit PluginConfig;
{$I DelphiExtension.inc}
interface
uses
{$IFDEF COMPILER6_UP}
Variants,
{$ENDIF COMPILER6_UP}
SysUtils, Classes, SimpleXmlIntf, SimpleXmlImport, FrmTreePages, TypInfo;
type
TPluginConfig = class(TComponent)
private
FFilename: string;
FRootNodeName: string;
FLoading: Boolean;
procedure LoadFromFile(const Filename: string);
protected
function GetOptionPages: TTreePage; virtual;
procedure Init; virtual;
procedure RegisterOptionPages; virtual;
property Loading: Boolean read FLoading;
public
constructor Create(const AFilename, ARootNodeName: string); reintroduce;
procedure SaveToXml(Node: IXmlNode); virtual;
procedure LoadFromXml(Node: IXmlNode); virtual;
procedure Save;
property RootNodeName: string read FRootNodeName;
end;
TConfiguration = class(TObject)
private
FModified: Boolean;
FFilename: string;
FUpdateLock: Integer;
FXml: IXmlDocument;
public
constructor Create;
destructor Destroy; override;
function HasNode(const NodeName: string): Boolean;
function FindNode(const NodeName: string): IXmlNode;
function GetNode(const NodeName: string): IXmlNode; overload;
class function GetNode(ParentNode: IXmlNode; const NodeName: string): IXmlNode; overload;
procedure Modified;
procedure SaveToFile(const AFilename: string);
procedure LoadFromFile(const AFilename: string);
procedure Save;
procedure BeginUpdate;
procedure EndUpdate;
property Filename: string read FFilename;
end;
function Configuration: TConfiguration;
implementation
uses
FrmDDevExtOptions, IDEUtils, IDEHooks, Main;
var
GlobalConfiguration: TConfiguration;
function Configuration: TConfiguration;
begin
if GlobalConfiguration = nil then
begin
if AppDataDirectory = '' then
InitAppDataDirectory;
GlobalConfiguration := TConfiguration.Create;
GlobalConfiguration.LoadFromFile(AppDataDirectory + '\DDevExtensions' + DelphiVersion + '.xml');
end;
Result := GlobalConfiguration;
end;
{$IFDEF COMPILER5}
function GetPropList(TypeInfo: PTypeInfo; out PropList: PPropList): Integer; overload;
begin
Result := GetTypeData(TypeInfo)^.PropCount;
if Result > 0 then
begin
GetMem(PropList, Result * SizeOf(Pointer));
GetPropInfos(TypeInfo, PropList);
end;
end;
function GetPropList(AObject: TObject; out PropList: PPropList): Integer; overload;
begin
Result := GetPropList(PTypeInfo(AObject.ClassInfo), PropList);
end;
{$ENDIF COMPILER5}
{ TPluginConfig }
constructor TPluginConfig.Create(const AFilename, ARootNodeName: string);
begin
inherited Create(nil);
FFilename := ChangeFileExt(AFilename, '') + DelphiVersion + ExtractFileExt(AFilename);
FRootNodeName := ARootNodeName;
Init;
if Configuration.HasNode(RootNodeName) then
LoadFromXml(Configuration.GetNode(RootNodeName))
else
if FileExists(FFilename) then
try
LoadFromFile(FFilename);
Save; // save to Configuration
DeleteFile(FFilename);
except
end;
RegisterOptionPages;
end;
function TPluginConfig.GetOptionPages: TTreePage;
begin
Result := nil;
end;
procedure TPluginConfig.Init;
begin
end;
procedure TPluginConfig.LoadFromFile(const Filename: string);
var
Doc: IXmlDocument;
begin
Doc := LoadXmlDocument(Filename);
LoadFromXml(Doc.DocumentElement);
end;
procedure TPluginConfig.RegisterOptionPages;
begin
TFormDDevExtOptions.RegisterPages(GetOptionPages);
end;
procedure TPluginConfig.Save;
begin
Configuration.GetNode(FRootNodeName).ChildNodes.Clear;
SaveToXml(Configuration.GetNode(FRootNodeName));
Configuration.Modified;
end;
procedure TPluginConfig.SaveToXml(Node: IXmlNode);
var
i: Integer;
PropList: PPropList;
Info: PPropInfo;
Count: Integer;
HasActive: Boolean;
Obj: TObject;
PropName: string;
begin
HasActive := False;
Info := GetPropInfo(Self, 'Active', tkProperties);
if (Info <> nil) and (Info.PropType^.Kind = tkEnumeration) then
begin
Node.Attributes['Active'] := GetEnumProp(Self, Info);
HasActive := True;
end;
Count := GetPropList(ClassInfo, PropList);
try
for i := 0 to Count - 1 do
begin
PropName := string(PropList[i].Name);
if ((AnsiCompareText(PropName, 'Active') <> 0) or not HasActive) and
(AnsiCompareText(PropName, 'Tag') <> 0) and
(AnsiCompareText(PropName, 'Name') <> 0) then
begin
Node.ChildNodes.DeleteNodes(PropName);;
case PropList[i].PropType^.Kind of
tkInteger:
TConfiguration.GetNode(Node, PropName).Attributes['Value'] := GetOrdProp(Self, PropList[i]);
tkString, tkUString, tkLString:
TConfiguration.GetNode(Node, PropName).NodeValue := GetStrProp(Self, PropList[i]);
tkWString:
TConfiguration.GetNode(Node, PropName).NodeValue := GetWideStrProp(Self, PropList[i]);
tkVariant:
TConfiguration.GetNode(Node, PropName).NodeValue := GetVariantProp(Self, PropList[i]);
tkFloat:
TConfiguration.GetNode(Node, PropName).Attributes['Value'] := GetFloatProp(Self, PropList[i]);
tkEnumeration:
TConfiguration.GetNode(Node, PropName).Attributes['Value'] := GetEnumProp(Self, PropList[i]);
tkClass:
begin
Obj := GetObjectProp(Self, PropList[i]);
if Obj is TStrings then
TConfiguration.GetNode(Node, PropName).NodeValue := TStrings(Obj).CommaText;
end;
end;
end;
end;
finally
if Assigned(PropList) then
FreeMem(PropList);
end;
end;
procedure TPluginConfig.LoadFromXml(Node: IXmlNode);
var
i: Integer;
PropList: PPropList;
Info: PPropInfo;
Count: Integer;
HasActive: Boolean;
Xml, N: IXmlNode;
Obj: TObject;
PropName: string;
begin
FLoading := True;
try
if Node <> nil then
begin
Info := GetPropInfo(Self, 'Active', tkProperties);
HasActive := (Info <> nil) and (Info.PropType^.Kind = tkEnumeration) and (Node.Attributes['Active'] <> Null);
Count := GetPropList(ClassInfo, PropList);
try
for i := 0 to Count - 1 do
begin
PropName := string(PropList[i].Name);
if ((AnsiCompareText(PropName, 'Active') <> 0) or not HasActive) and
(AnsiCompareText(PropName, 'Tag') <> 0) and
(AnsiCompareText(PropName, 'Name') <> 0) then
begin
Xml := Node.ChildNodes.FindNode(PropName);
if (Xml <> nil) and ((PropList[i].PropType^.Kind in [tkString, tkUString, tkLString, tkWString, tkVariant]) or (Xml.Attributes['Value'] <> Null)) then
begin
case PropList[i].PropType^.Kind of
tkInteger:
SetOrdProp(Self, PropList[i], Xml.Attributes['Value']);
tkString, tkLString, tkUString:
SetStrProp(Self, PropList[i], VarToStr(Xml.NodeValue));
tkWString:
SetWideStrProp(Self, PropList[i], VarToWideStr(Xml.NodeValue));
tkVariant:
SetVariantProp(Self, PropList[i], Xml.NodeValue);
tkFloat:
SetFloatProp(Self, PropList[i], Xml.Attributes['Value']);
tkEnumeration:
if VarToStr(Xml.Attributes['Value']) <> '' then
begin
try
SetEnumProp(Self, PropList[i], VarToStr(Xml.Attributes['Value']));
except
// ignore exception if the enumeration item doesn't exist (anymore)
end;
end;
tkClass:
begin
Obj := GetObjectProp(Self, PropList[i]);
if Obj is TStrings then
begin
N := Node.ChildNodes.FindNode(PropName);
if N <> nil then
TStrings(Obj).CommaText := N.NodeValue;
end;
end;
end;
end;
end;
end;
if HasActive and (VarToStr(Node.Attributes['Active']) <> '') then
SetEnumProp(Self, Info, VarToStr(Node.Attributes['Active']));
finally
if Assigned(PropList) then
FreeMem(PropList);
end;
end;
finally
FLoading := False;
Loaded;
end;
end;
{ TConfiguration }
constructor TConfiguration.Create;
begin
inherited Create;
FXml := NewXmlDocument;
FXml.DocumentElement := FXml.CreateElement('DDevExtensions', '');
end;
destructor TConfiguration.Destroy;
begin
FXml := nil;
inherited Destroy;
end;
procedure TConfiguration.BeginUpdate;
begin
Inc(FUpdateLock);
end;
procedure TConfiguration.EndUpdate;
begin
Assert(FUpdateLock > 0, 'Unpaired call of EndUpdate');
Dec(FUpdateLock);
if (FUpdateLock = 0) and FModified then
Save;
end;
function TConfiguration.GetNode(const NodeName: string): IXMLNode;
begin
Result := GetNode(FXml.DocumentElement, NodeName);
end;
class function TConfiguration.GetNode(ParentNode: IXmlNode; const NodeName: string): IXmlNode;
begin
Result := ParentNode.ChildNodes.FindNode(NodeName);
if Result = nil then
Result := ParentNode.AddChild(NodeName);
end;
function TConfiguration.HasNode(const NodeName: string): Boolean;
begin
Result := FXml.DocumentElement.ChildNodes.FindNode(NodeName) <> nil;
end;
function TConfiguration.FindNode(const NodeName: string): IXmlNode;
begin
Result := FXml.DocumentElement.ChildNodes.FindNode(NodeName);
end;
procedure TConfiguration.LoadFromFile(const AFilename: string);
begin
FFilename := AFilename;
try
if FileExists(Filename) then
FXml := LoadXmlDocument(AFilename)
else
begin
FXml := NewXmlDocument;
FXml.DocumentElement := FXml.CreateElement('DDevExtensions', '');
end;
FXml.Options := FXml.Options + [doNodeAutoIndent];
except
if Assigned(ApplicationHandleException) then
ApplicationHandleException(Self);
end;
FModified := False;
end;
procedure TConfiguration.Modified;
begin
FModified := True;
if (Filename <> '') and (FUpdateLock = 0) then
Save;
end;
procedure TConfiguration.Save;
begin
Assert(Filename <> '');
ForceDirectories(ExtractFileDir(Filename));
FXml.SaveToFile(Filename);
FModified := False;
end;
procedure TConfiguration.SaveToFile(const AFilename: string);
begin
FXml.SaveToFile(AFilename);
end;
initialization
finalization
FreeAndNil(GlobalConfiguration);
end.