-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusettingsrec.pas
89 lines (77 loc) · 1.68 KB
/
usettingsrec.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
unit uSettingsRec;
{$mode ObjFPC}{$H+}
{$modeswitch advancedrecords}
interface
uses
Classes, SysUtils, LCLIntf, LazFileUtils,
// JSON units for settings
FPJson, JsonParser,
// ca stuff
caDbg;
const
SETTINGS_JSON = 'settings.json';
type
TSettings = record
private
FDeviceName: string;
// private methods
function GetAppConfigPath: string;
function GetConfigFileName: string;
public
property DeviceName: string read FDeviceName write FDeviceName;
procedure Read;
procedure Save;
end;
implementation
function TSettings.GetAppConfigPath: string;
var
Path: string;
begin
Path := IncludeTrailingPathDelimiter(GetUserDir) + '.config' + PathDelim + 'OsmosePresets';
ForceDirectories(Path);
Result := Path;
end;
function TSettings.GetConfigFileName:string;
begin
Result := GetAppConfigPath + PathDelim + SETTINGS_JSON;
end;
procedure TSettings.Read;
var
JSONData: TJSONData;
JSONObj: TJSONObject;
begin
JSONData := GetJSON(TFileStream.Create(GetConfigFileName, fmOpenRead));
try
if JSONData is TJSONObject then
begin
JSONObj := TJSONObject(JSONData);
Self.DeviceName := JSONObj.Get('DeviceName', '');
db('Self.DeviceName', Self.DeviceName);
end
else
raise Exception.Create('Invalid JSON format');
finally
JSONData.Free;
end;
end;
procedure TSettings.Save;
var
JSONObj: TJSONObject;
SL: TStrings;
begin
JSONObj := TJSONObject.Create;
try
JSONObj.Add('Device Name', DeviceName);
SL := TStringList.Create;
try
SL.Text := JSONObj.FormatJSON;
db(SL.Text);
SL.SaveToFile(GetConfigFileName);
finally
SL.Free;
end;
finally
JSONObj.Free;
end;
end;
end.