-
Notifications
You must be signed in to change notification settings - Fork 32
/
Quick.ORM.App.Config.pas
132 lines (106 loc) · 3.24 KB
/
Quick.ORM.App.Config.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
{ ***************************************************************************
Copyright (c) 2016-2019 Kike Pérez
Unit : Quick.ORM.App.Config
Description : Load/Save config from/to JSON file
Author : Kike Pérez
Version : 1.2
Created : 26/01/2017
Modified : 08/05/2019
This file is part of QuickORM: https://github.com/exilon/QuickORM
Uses Synopse mORMot framework. Copyright (C) 2017 Arnaud Bouchez
Synopse Informatique - https://synopse.info
***************************************************************************
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*************************************************************************** }
unit Quick.ORM.App.Config;
{$i QuickORM.inc}
interface
uses
Classes,
SysUtils,
mORMot,
SynCommons,
SynCrypto;
type
TORMAppConfig = class(TSQLRecord)
private
fConfigFile : RawUTF8;
fConfigEncrypted : Boolean;
fConfigPassword : RawUTF8;
public
property ConfigFile : RawUTF8 read fConfigFile write fConfigFile;
property ConfigEncrypted : Boolean read fConfigEncrypted write fConfigEncrypted;
property ConfigPassword : RawUTF8 read fConfigPassword write fConfigPassword;
function Load(CreateIfNotExists : Boolean = False) : Boolean;
function Save : Boolean;
end;
{Usage: create a descend class from TORMAppConfig and add published properties to be loaded/saved
TMyConfig = class(TORMAppConfig)
private
fName : RawUTF8;
fSurname : RawUTF8;
fStatus : Integer;
published
property Name : RawUTF8 read fName write fName;
property SurName : RawUTF8 read fSurname write fSurname;
property Status : Integer read fStatus write fStatus;
end;
}
implementation
{ TORMAppConfig }
function TORMAppConfig.Load(CreateIfNotExists : Boolean = False) : Boolean;
var
tmp : RawUTF8;
begin
if (CreateIfNotExists) and (not FileExists(fConfigFile)) then
begin
Self.Save;
Result := False;
end;
try
if fConfigEncrypted then
begin
tmp := AnyTextFileToRawUTF8(fConfigFile,true);
if tmp <> '' then
begin
tmp := AESSHA256(tmp,fConfigPassword,False);
RemoveCommentsFromJSON(pointer(tmp));
JSONToObject(Self,pointer(tmp),result,nil,[]);
end;
end
else
begin
Result := JSONFileToObject(fConfigFile,Self);
end;
except
on e : Exception do raise e;
end;
end;
function TORMAppConfig.Save : Boolean;
var
json : RawUTF8;
begin
try
if fConfigEncrypted then
begin
json := ObjectToJSON(Self);
json := AESSHA256(json,fConfigPassword,True);
Result := FileFromString(json,fConfigFile);
end
else
begin
Result := ObjectToJSONFile(Self,fConfigFile);
end;
except
on e : Exception do raise e;
end;
end;
end.