-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettings.pas
197 lines (164 loc) · 4.56 KB
/
Settings.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
unit Settings;
interface
uses
Classes, System.SysUtils, Rest.json, Generics.Collections;
type
TCodeEditor = class
private
FName: String;
FPath: String;
FParam: String;
FEnabled: Boolean;
public
constructor Create(const AName, APath, AParam: String);
destructor Destroy(); override;
property Name: String read FName write FName;
property Path: String read FPath write FPath;
property Param: String read FParam write FParam;
property Enabled: Boolean read FEnabled write FEnabled;
end;
TCodeEditors = class
private
FDefault: Extended;
FItems: TArray<TCodeEditor>;
public
property default: Extended read FDefault write FDefault;
property items: TArray<TCodeEditor> read FItems write FItems;
destructor Destroy; override;
procedure Append(item: TCodeEditor);
procedure Remove(index: Integer);
function Count: Integer;
end;
TSettings = class(TObject)
private
FCodeEditors: TCodeEditors;
FShowSplash: Boolean;
FPort: Integer;
FUseSSL: Boolean;
FTestSite: String;
FCodeEditor: String;
FPHPVersion: String;
FHTTPServer: String;
FMonitorPort: Integer;
public
constructor Create;
destructor Destroy; override;
property ShowSplash: Boolean read FShowSplash write FShowSplash;
property Port: Integer read FPort write FPort;
property UseSSL: Boolean read FUseSSL write FUseSSL;
property TestSite: String read FTestSite write FTestSite;
property DefaultCodeEditor: String read FCodeEditor write FCodeEditor;
property PHPVersion: String read FPHPVersion write FPHPVersion;
property HTTPServer: String read FHTTPServer write FHTTPServer;
property MonitorPort: Integer read FMonitorPort write FMonitorPort;
property CodeEditors: TCodeEditors read FCodeEditors write FCodeEditors;
end;
TSettingsHandler = class(TObject)
private
public
class function LoadSettings(fsettings: String = ''):TSettings;
class procedure SaveSettings(settings: TSettings; fsettings: String = '');
class function IsPortable: Boolean;
end;
implementation
{ TSettings }
constructor TSettings.Create;
begin
inherited;
FCodeEditors := TCodeEditors.Create();
end;
destructor TSettings.Destroy;
begin
FCodeEditors.Free;
inherited;
end;
{ TSettingsHandler }
// if `data` directory is found, then ignore %APPDATA%\lara directory
// and turn it portable mode
class function TSettingsHandler.IsPortable: Boolean;
begin
Result := DirectoryExists(ExtractFilePath(ParamStr(0)+'data'));
end;
class function TSettingsHandler.LoadSettings(fSettings: String): TSettings;
var
strings: TStrings;
begin
strings := TStringList.Create;
try
if fsettings = '' then
fsettings := ExtractFilePath(ParamStr(0)) + 'settings.json';
if FileExists(fsettings) then
begin
strings.LoadFromFile(fsettings);
//Result := TSettings.Create();
Result := TJson.JsonToObject<TSettings>(strings.Text);//.Create();
end
else
begin
Result := TSettings.Create();
end;
finally
strings.Free;
end;
end;
class procedure TSettingsHandler.SaveSettings(settings: TSettings;
fsettings: String);
var
strings: TStrings;
json: String;
begin
if fsettings = '' then
fsettings := ExtractFilePath(ParamStr(0)) + 'settings.json';
strings := TStringList.Create;
try
//json := TJSON.ObjectToJsonString(settings,[joIgnoreEmptyArrays]);
json := TJSON.ObjectToJsonString(settings);
strings.Add(json);
strings.SaveToFile(fsettings);
finally
strings.Free;
end;
end;
{ TCodeEditor }
constructor TCodeEditor.Create(const AName, APath, AParam: String);
begin
FName := AName;
FPath := APath;
FParam := AParam;
FEnabled := True;
end;
destructor TCodeEditor.Destroy;
begin
inherited;
end;
{ TCodeEditors }
procedure TCodeEditors.Append(item: TCodeEditor);
begin
SetLength(FItems, Length(FItems) +1);
Fitems[High(Fitems)]:= item;
end;
function TCodeEditors.Count: Integer;
begin
Result := Length(FItems);
end;
destructor TCodeEditors.Destroy;
var
LitemsItem: TCodeEditor;
begin
for LitemsItem in FItems do
LitemsItem.free;
inherited;
end;
procedure TCodeEditors.Remove(index: Integer);
var
I: Integer;
begin
if (Count > 0) and (index < Count)then
begin
FItems[index].Free;
for I := index + 1 to Count - 1 do
FItems[I - 1] := FItems[I];
SetLength(FItems, Count - 1);
end;
end;
end.