-
Notifications
You must be signed in to change notification settings - Fork 6
/
settingsHelper.pas
323 lines (293 loc) · 11.2 KB
/
settingsHelper.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
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
unit settingsHelper;
interface
uses
FireDAC.Phys.SQLite, Generics.Collections, Classes, SysUtils, JSON,
FireDAC.Comp.Client, FireDAC.Stan.Param, FireDAC.Stan.Error,
FireDAC.Stan.Intf, FireDAC.Stan.Option, Winapi.ShellAPI,
FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def, FireDAC.Stan.Pool,
FireDAC.Stan.Async, FireDAC.Phys, FireDAC.VCLUI.Wait, FireDAC.DatS,
FireDAC.DApt.Intf, FireDAC.DApt, FireDAC.Comp.DataSet;
type
TSite = class
private
FId: Integer;
FName: string;
FUrl: string;
FAltUrl: string;
FIcon: string;
FPosition: Integer;
FUserScript: string;
FUserStyle: string;
FUserScriptEnabled: Boolean;
FUserStyleEnabled: Boolean;
FEnabled: Boolean;
FUA: string;
published
property Id: Integer read FId write FId;
property Name: string read FName write FName;
property Url: string read FUrl write FUrl;
property AltUrl: string read FAltUrl write FAltUrl;
property Icon: string read FIcon write FIcon;
property Position: Integer read FPosition write FPosition;
property UserStyle: string read FUserStyle write FUserStyle;
property UserScript: string read FUserScript write FUserScript;
property UserStyleEnabled: Boolean read FUserStyleEnabled write FUserStyleEnabled;
property UserScriptEnabled: Boolean read FUserScriptEnabled write FUserScriptEnabled;
property Enabled: Boolean read FEnabled write FEnabled;
property UA: string read FUA write FUA;
end;
TSettings = class
private
FDB: TFDConnection;
FSites: TObjectList<TSite>;
// app settings
FSettingsPath: string;
FDatabaseName: string;
FInifileName: string;
FAutoHide: Boolean;
FAutoStart: Boolean;
FDetectClipboardText: Boolean;
FDetectClipboardImage: Boolean;
FDisableOnFullScreen: Boolean;
FDisableOnFullScreenDirectX: Boolean;
FGlobalHotkey: string;
FRequireWinkey: Boolean;
FProxy: string;
FBarPosition: Integer; //ABE_RIGHT
FDarkMode: Boolean;
public
procedure CreateTables;
constructor Create(const settingsPath: string);
destructor Destroy; override;
procedure AddSites(const name, url, alturl, svgicon, uscript, ustyle: string;
uscriptOn, ustyleOn, enabled: Boolean; position: Integer; const UA: string);
procedure ReadSites;
procedure SaveSettings;
procedure LoadSettings;
procedure UpdateSite(id: Integer; const name, url, alturl, svgicon, uscript, ustyle: string;
uscriptOn, ustyleOn, enabled: Boolean; position: Integer; const UA: string);
procedure DeleteSite(id: Integer);
property DB: TFDConnection read FDB;
property Sites: TObjectList<TSite> read FSites write FSites;
property AutoHide: Boolean read FAutoHide write FAutoHide;
property AutoStart: Boolean read FAutoStart write FAutoStart;
property DetectClipboardText: Boolean read FDetectClipboardText write FDetectClipboardText;
property DetectClipboardImage: Boolean read FDetectClipboardImage write FDetectClipboardImage;
property DisableOnFullScreen: Boolean read FDisableOnFullScreen write FDisableOnFullScreen;
property DisableOnFullScreenDirectX: Boolean read FDisableOnFullScreenDirectX write FDisableOnFullScreenDirectX;
property GlobalHotkey: string read FGlobalHotkey write FGlobalHotkey;
property RequireWinKey: Boolean read FRequireWinkey write FRequireWinkey;
property Proxy: string read FProxy write FProxy;
property BarPosition: Integer read FBarPosition write FBarPosition;
property DarkMode: Boolean read FDarkMode write FDarkMode;
end;
implementation
uses
System.IniFiles;
{ TSettings }
procedure TSettings.AddSites(const name, url, alturl, svgicon, uscript,
ustyle: string; uscriptOn, ustyleOn, enabled: Boolean; position: Integer; const UA: string);
var
q: TFDQuery;
begin
q := TFDQuery.Create(nil);
try
q.Connection := FDB;
q.SQL.Text := 'INSERT OR IGNORE INTO settings (name, url, alturl, svgIcon,' +
'userscript, userscriptactive,' +
'userstyle, userstyleactive,' +
'enabled, position, ua) VALUES (:name, :url, :alturl, :svgIcon,' +
':userscript, :userscriptactive,' +
':userstyle, :userstyleactive,' +
':enabled, :position, :ua)';
q.Params.ParamByName('name').AsWideString := name;
q.Params.ParamByName('url').AsWideString := url;
q.Params.ParamByName('alturl').AsWideString := alturl;
q.Params.ParamByName('svgIcon').AsWideString := svgicon;
q.Params.ParamByName('userscript').AsWideString := uscript;
q.Params.ParamByName('userscriptactive').AsBoolean := uscriptOn;
q.Params.ParamByName('userstyle').AsWideString := ustyle;
q.Params.ParamByName('userstyleactive').AsBoolean := ustyleOn;
q.Params.ParamByName('enabled').AsBoolean := enabled;
q.Params.ParamByName('position').AsInteger := position;
q.Params.ParamByName('ua').AsWideString := UA;
q.ExecSQL;
finally
q.Free;
end;
end;
constructor TSettings.Create(const settingsPath: string);
var
FileInfo: TSearchRec;
begin
FSettingsPath := ExtractFilePath(settingsPath);
FDatabaseName := ExtractFileName(settingsPath);
FInifileName := StringReplace(FDatabaseName, ExtractFileExt(FDatabaseName), '.ini', [rfIgnoreCase]);
FDB := TFDConnection.Create(nil);
FDB.Params.DriverID := 'SQLite';
FDB.Params.Database := settingsPath;
FDB.Open;
if FindFirst(settingsPath, faAnyFile, FileInfo) = 0 then
begin
try
if (FileInfo.Size = 0) then
CreateTables;
finally
FindClose(FileInfo);
end;
end;
FSites := TObjectList<TSite>.Create;
end;
procedure TSettings.CreateTables;
var
qr: TFDQuery;
begin
qr := TFDQuery.Create(nil);
try
qr.Connection := FDB;
qr.SQL.Text := 'CREATE TABLE IF NOT EXISTS settings(id INTEGER PRIMARY KEY,' +
'name TEXT, url TEXT, alturl TEXT, svgIcon TEXT,' +
'userscript TEXT, userscriptactive INTEGER,' +
'userstyle TEXT, userstyleactive INTEGER,' +
'enabled INTEGER, position INTEGER, ua TEXT)';
qr.ExecSQL;
qr.SQL.Text := 'CREATE UNIQUE INDEX IF NOT EXISTS name_index on settings(name)';
qr.ExecSQL;
finally
qr.Free;
end;
end;
procedure TSettings.DeleteSite(id: Integer);
var
q: TFDQuery;
begin
q := TFDQuery.Create(nil);
try
q.Connection := FDB;
q.SQL.Text := 'DELETE from settings WHERE id = :id';
q.Params.ParamByName('id').AsInteger := id;
q.ExecSQL;
finally
q.Free;
end;
end;
destructor TSettings.Destroy;
begin
inherited;
FSites.Free;
FDB.CloneConnection;
FDB.Free;
end;
procedure TSettings.LoadSettings;
var
ini: TIniFile;
begin
ini := TIniFile.Create(FSettingsPath + FInifileName);
try
FAutoHide := ini.ReadBool('settings', 'autohide', True);
FAutoStart := ini.ReadBool('settings', 'autostart', False);
FDetectClipboardText := ini.ReadBool('settings', 'cliptext', False);
FDetectClipboardImage := ini.ReadBool('settings', 'clipimg', False);
FDisableOnFullScreen := ini.ReadBool('settings', 'notonfs', True);
FDisableOnFullScreenDirectX := ini.ReadBool('settings', 'notonfs3d', True);
FGlobalHotkey := ini.ReadString('settings', 'hotkey', '');
FRequireWinkey := ini.ReadBool('settings', 'requirewinkey', False);
FProxy := ini.ReadString('settings', 'proxy', 'localhost:8080');
FBarPosition := ini.ReadInteger('settings', 'position', ABE_RIGHT);
FDarkMode := ini.ReadBool('settings', 'darkmode', True);
finally
ini.Free;
end;
end;
procedure TSettings.ReadSites;
var
q: TFDQuery;
begin
q := TFDQuery.Create(nil);
try
q.Connection := FDB;
q.SQL.Text := 'SELECT * FROM settings';
q.Open;
// clear current FSites if it is filled already
FSites.Clear;
while not q.Eof do
begin
var site := TSite.Create;
try
with site do
begin
FId := q.FieldByName('id').AsInteger;
FName := q.FieldByName('name').AsWideString;
FUrl := q.FieldByName('url').AsWideString;
FAltUrl := q.FieldByName('alturl').AsWideString;
FIcon := q.FieldByName('svgIcon').AsWideString;
FUserScript := q.FieldByName('userscript').AsWideString;
FUserScriptEnabled := Boolean(q.FieldByName('userscriptactive').AsInteger);
FUserStyle := q.FieldByName('userstyle').AsWideString;
FUserStyleEnabled := Boolean(q.FieldByName('userstyleactive').AsInteger);
FEnabled := Boolean(q.FieldByName('enabled').AsInteger);
FPosition := q.FieldByName('position').AsInteger;
FUA := q.FieldByName('ua').AsWideString;
end;
FSites.Add(site);
finally
//site.Free; <// do not free, otherwise it won't be accessible anyumore, clear or destroy handles it
end;
q.Next;
end;
finally
q.Free;
end;
end;
procedure TSettings.SaveSettings;
var
ini: TIniFile;
begin
ini := TIniFile.Create(FSettingsPath + FInifileName);
try
ini.WriteBool('settings', 'autohide', FAutoHide);
ini.WriteBool('settings', 'autostart', FAutoStart);
ini.WriteBool('settings', 'cliptext', FDetectClipboardText);
ini.WriteBool('settings', 'clipimg', FDetectClipboardImage);
ini.WriteBool('settings', 'notonfs', FDisableOnFullScreen);
ini.WriteBool('settings', 'notonfs3d', FDisableOnFullScreenDirectX);
ini.WriteString('settings', 'hotkey', FGlobalHotkey);
ini.WriteBool('settings', 'requirewinkey', FRequireWinkey);
ini.WriteString('settings', 'proxy', FProxy);
ini.WriteInteger('settings', 'position', FBarPosition);
ini.WriteBool('settings', 'darkmode', FDarkMode);
finally
ini.Free;
end;
end;
procedure TSettings.UpdateSite(id: Integer; const name, url, alturl, svgicon,
uscript, ustyle: string; uscriptOn, ustyleOn, enabled: Boolean;
position: Integer; const UA: string);
var
q: TFDQuery;
begin
q := TFDQuery.Create(nil);
try
q.Connection := FDB;
q.SQL.Text := 'UPDATE settings SET name = :name, url = :url, alturl = :alturl, svgIcon = :svgIcon,' +
'userscript = :userscript, userscriptactive = :userscriptactive,' +
'userstyle = :userstyle, userstyleactive = :userstyleactive,' +
'enabled = :enabled, position = :position, ua = :ua WHERE id = :id';
q.Params.ParamByName('id').AsInteger := id;
q.Params.ParamByName('name').AsWideString := name;
q.Params.ParamByName('url').AsWideString := url;
q.Params.ParamByName('alturl').AsWideString := alturl;
q.Params.ParamByName('svgIcon').AsWideString := svgicon;
q.Params.ParamByName('userscript').AsWideString := uscript;
q.Params.ParamByName('userscriptactive').AsBoolean := uscriptOn;
q.Params.ParamByName('userstyle').AsWideString := ustyle;
q.Params.ParamByName('userstyleactive').AsBoolean := ustyleOn;
q.Params.ParamByName('enabled').AsBoolean := enabled;
q.Params.ParamByName('position').AsInteger := position;
q.Params.ParamByName('ua').AsWideString := UA;
q.ExecSQL;
finally
q.Free;
end;
end;
end.