-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUPreferenceData.pas
287 lines (263 loc) · 9.86 KB
/
UPreferenceData.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
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
{* Copyright (C) 2009-2011 Karl-Michael Schindler
*
* This file is part of Heat Wizard.
*
* Heat Wizard is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* Heat Wizard is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Heat Wizard; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/> or write to the
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301, USA
*
* $URL$
* $Id$
*}
unit UPreferenceData;
{$mode objfpc}
{$H+}
interface
uses
SysUtils;
type
TPreferenceData = class
FileVersion: string;
Language: string;
FormsPosition: record
Top, Left: integer
end;
constructor Create;
procedure Save;
procedure Read;
end;
var
PreferenceData: TPreferenceData;
implementation
uses
StrUtils,
{$IF Defined(DARWIN)}
MacOSAll,
{$ELSE}
XMLConf,
{$IFEND}
Ulog;
constructor TPreferenceData.Create;
begin
Inherited;
Logger.Output('UPreferenceData', 'Create');
end;
procedure TPreferenceData.Read;
var
Error: record
NoFileversion: boolean;
NoLanguage: boolean;
NoFormsTopPosition: boolean;
NoFormsLeftPosition: boolean;
end;
{$IF Defined(DARWIN)}
var
StringConversionSuccess: boolean;
DataString: CFPropertyListRef;
FileVersionBuffer: str255;
LanguageBuffer: str255;
FormsPositionTopBuffer: str255;
FormsPositionLeftBuffer: str255;
TopCFString: CFStringRef;
LeftCFString: CFStringRef;
begin
Logger.Output('UPreferenceData', 'Enter Read');
DataString := CFPreferencesCopyAppValue(CFStr('FileVersion'), kCFPreferencesCurrentApplication);
if not (DataString = NIL) then
begin
StringConversionSuccess := CFStringGetPascalString(DataString,
@FileVersionBuffer,
Length(FileVersionBuffer),
kCFStringEncodingUTF8);
if not StringConversionSuccess then
begin
Logger.Output('UPreferenceData', 'Error reading Fileversion from file. Continue with Default');
Error.NoFileversion := true;
end
else
begin
Logger.Output('UPreferenceData', 'Fileversion Buffer as read from file: ' + FileVersionBuffer);
Error.NoFileversion := false;
end;
end
else
begin
Logger.Output('UPreferenceData', 'Error reading Fileversion from file. Continue with Default');
Error.NoFileversion := true;
end;
DataString := CFPreferencesCopyAppValue(CFStr('Language'), kCFPreferencesCurrentApplication);
if not (DataString = NIL) then
begin
StringConversionSuccess := CFStringGetPascalString(DataString,
@LanguageBuffer,
Length(LanguageBuffer),
kCFStringEncodingUTF8);
if not StringConversionSuccess then
begin
Logger.Output('UPreferenceData', 'Error reading Language from file. Continue with Default');
Error.NoLanguage := true;
end
else
begin
Logger.Output('UPreferenceData', 'Language Buffer as read from file: ' + LanguageBuffer);
Error.NoLanguage := false;
end;
end
else
begin
Logger.Output('UPreferenceData', 'Error reading Language from file. Continue with Default');
Error.NoFileversion := true;
end;
DataString := CFPreferencesCopyAppValue(CFStr('FormsPosition'), kCFPreferencesCurrentApplication);
if not (DataString = NIL) then
begin
TopCFString := CFDictionaryGetValue (DataString, CFStr('Top'));
StringConversionSuccess := CFStringGetPascalString(TopCFString,
@FormsPositionTopBuffer,
Length(FormsPositionTopBuffer),
kCFStringEncodingUTF8);
if StringConversionSuccess then
begin
LeftCFString := CFDictionaryGetValue (DataString, CFStr('Left'));
StringConversionSuccess := CFStringGetPascalString(LeftCFString,
@FormsPositionLeftBuffer,
Length(FormsPositionLeftBuffer),
kCFStringEncodingUTF8);
end;
if not StringConversionSuccess then
begin
Logger.Output('UPreferenceData', 'Error reading FormsPosition from file. Continue with Default');
Error.NoFormsTopPosition := true;
end
else
begin
Logger.Output('UPreferenceData', 'FormsPosition Buffer Top as read from file: ' + FormsPositionTopBuffer);
Logger.Output('UPreferenceData', 'FormsPosition Buffer Left as read from file: ' + FormsPositionLeftBuffer);
Error.NoFormsTopPosition := false;
end;
end
else
begin
Logger.Output('UPreferenceData', 'Error reading FormsPosition from file. Continue with Default');
Error.NoFormsTopPosition := true;
end;
{$ELSE}
var
FileVersionBuffer: string;
LanguageBuffer: string;
FormsPositionTopBuffer: string;
FormsPositionLeftBuffer: string;
Preferences: TXMLConfig;
begin
Logger.Output('UPreferenceData', 'Create PreferenceDoc');
{$IF not defined(Windows)}
PreferenceDirName := GetEnvironmentVariable('HOME') + PreferenceDirName;
if not DirectoryExists(PreferenceDirName) then
begin
Logger.Output('UPreferenceData', 'Application directory not found. Trying to create: ' + PreferenceDirName);
mkdir(PreferenceDirName);
end;
PreferenceFileName := PreferenceDirName + PreferenceFileName;
{$IFEND}
if not fileExists(PreferenceFileName) then
begin
Logger.Output('UPreferenceData', 'Preference file not found. Trying to create: ' + PreferenceFileName);
FileVersionBuffer := '1.0.0';
LanguageBuffer := 'en';
FormsPosition.Top := 130;
FormsPosition.Left := 400;
end
else
begin
Preferences := TXMLConfig.Create(nil);
Try
Preferences.Filename := PreferenceFileName;
except // probably an errorneous preference file
Preferences.StartEmpty := true;
Preferences.Clear;
Preferences.Filename := PreferenceFileName;
end;
FileVersionBuffer := Preferences.GetValue('FileVersion','1.0.0');
Error.NoFileVersion := false;
LanguageBuffer := Preferences.GetValue('Language', 'en');
Error.NoLanguage := false;
FormsPositionTopBuffer := intToStr(Preferences.GetValue('FormsPosition/Top', 130));
Error.NoFormsTopPosition := false;
FormsPositionLeftBuffer := intToStr(Preferences.GetValue('FormsPosition/Left', 400));
Error.NoFormsLeftPosition := false;
Preferences.Destroy;
end;
{$IFEND}
Logger.Output('UPreferenceData', 'Check: FileversionBuffer: ' + FileversionBuffer + ' LanguageBuffer: ' + LanguageBuffer);
if (not Error.NoFileVersion) and AnsiMatchText(FileVersionBuffer, ['1.0.0']) then
FileVersion := FileVersionBuffer
else
FileVersion := '1.0.0';
if (not Error.NoLanguage) and AnsiMatchText(LanguageBuffer, ['en','de','fi', 'fr']) then
Language := LanguageBuffer
else
Language := 'en';
FormsPosition.Top := StrToIntDef(FormsPositionTopBuffer, 130);
FormsPosition.Left := StrToIntDef(FormsPositionLeftBuffer, 400);;
PreferenceData.Save;
Logger.Output('UPreferenceData', 'Leaving Read. Fileversion: ' + Fileversion + ' Language: ' + Language);
end;
procedure TPreferenceData.Save;
{$IF Defined(DARWIN)}
var
FileVersionData: CFPropertyListRef;
LanguageData: CFPropertyListRef;
PositionData: CFPropertyListRef;
keys: array [1..2] of CFStringRef;
vals: array [1..2] of CFStringRef;
begin
Logger.Output('UPreferenceData', 'Enter Save');
Logger.Output('UPreferenceData', 'Fileversion: ' + FileVersion);
Logger.Output('UPreferenceData', 'Language: ' + Language);
Logger.Output('UPreferenceData', 'FormsPosition: Top: ' + intToStr(FormsPosition.Top) + ' Left: ' + intToStr(FormsPosition.Left));
FileVersionData := CFStringCreateWithPascalString(kCFAllocatorDefault, FileVersion, kCFStringEncodingUTF8);
CFPreferencesSetAppValue(CFStr('FileVersion'), FileVersionData, kCFPreferencesCurrentApplication);
LanguageData := CFStringCreateWithPascalString(kCFAllocatorDefault, Language, kCFStringEncodingUTF8);
CFPreferencesSetAppValue(CFStr('Language'), LanguageData, kCFPreferencesCurrentApplication);
keys[1] := CFStr('Top');
vals[1] := CFStringCreateWithPascalString(kCFAllocatorDefault, intToStr(FormsPosition.Top), kCFStringEncodingUTF8);
keys[2] := CFStr('Left');
vals[2] := CFStringCreateWithPascalString(kCFAllocatorDefault, intToStr(FormsPosition.Left), kCFStringEncodingUTF8);
PositionData := CFDictionaryCreate(kCFAllocatorDefault, @keys, @vals, 2, Nil, Nil);
CFPreferencesSetAppValue(CFStr('FormsPosition'), PositionData, kCFPreferencesCurrentApplication);
CFPreferencesAppSynchronize(kCFPreferencesCurrentApplication);
{$ELSE}
var
Preferences: TXMLConfig;
begin
Logger.Output('UPreferenceData', 'Enter Save');
Logger.Output('UPreferenceData', 'Fileversion: ' + FileVersion);
Logger.Output('UPreferenceData', 'Language: ' + Language);
Logger.Output('UPreferenceData', 'FormsPosition: Top: ' + intToStr(FormsPosition.Top) + ' Left: ' + intToStr(FormsPosition.Left));
Preferences := TXMLConfig.Create(nil);
Preferences.StartEmpty := true;
Preferences.Clear;
Preferences.Filename := PreferenceFileName;
Preferences.SetValue('FileVersion', FileVersion);
Preferences.SetValue('Language', Language);
Preferences.SetValue('FormsPosition/Top', intToStr(FormsPosition.Top));
Preferences.SetValue('FormsPosition/Left', intToStr(FormsPosition.Left));
Preferences.Flush;
Preferences.Destroy;
{$IFEND}
end;
initialization
end.