-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdMain.pas
More file actions
235 lines (207 loc) · 5.48 KB
/
dMain.pas
File metadata and controls
235 lines (207 loc) · 5.48 KB
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
unit dMain;
interface
uses
System.SysUtils, System.Classes, FireDAC.Stan.Intf, FireDAC.Stan.Option,
FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def,
FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Phys, FireDAC.Phys.SQLite,
FireDAC.Phys.SQLiteDef, FireDAC.Stan.ExprFuncs, FireDAC.VCLUI.Wait,
FireDAC.Comp.UI, Data.DB, FireDAC.Comp.Client, FireDAC.Stan.Param,
FireDAC.DatS, FireDAC.DApt.Intf, FireDAC.DApt, FireDAC.Comp.DataSet,
uGlobals, System.IOUtils, FireDAC.Comp.ScriptCommands, FireDAC.Stan.Util,
FireDAC.Comp.Script, Vcl.ExtCtrls, System.Hash, System.SyncObjs;
type
TdmMain = class(TDataModule)
conMain: TFDConnection;
fdPhysSQLite1: TFDPhysSQLiteDriverLink;
fdCursor1: TFDGUIxWaitCursor;
qFolders: TFDQuery;
qFiles: TFDQuery;
tmrCalcSha: TTimer;
qSha: TFDQuery;
procedure DataModuleCreate(Sender: TObject);
procedure conMainAfterConnect(Sender: TObject);
procedure tmrCalcShaTimer(Sender: TObject);
procedure DataModuleDestroy(Sender: TObject);
private
function CreateDB(const DBPath: string): Boolean;
procedure OpenDB(const DBPath: string);
procedure CalcNextSha;
procedure DoWriteSha(const FileID: Int64; Sha: string);
procedure DoCalcNextSha(const FileID: Int64; const FilePath: string);
procedure DoDeleteFile(const FileID: Int64);
public
function CreateQuery: TFDQuery;
procedure RefreshFolders;
procedure RefreshFiles(FolderID: Int64);
end;
procedure ShaLock;
function ShaTryLock: boolean;
procedure ShaUnlock;
var
dmMain: TdmMain;
implementation
var
CSSha: TCriticalSection;
procedure ShaLock;
begin
if dmMain.tmrCalcSha.Enabled then
dmMain.tmrCalcSha.Enabled := False;
CSSha.Acquire;
end;
function ShaTryLock: boolean;
begin
Result := CSSha.TryEnter;
end;
procedure ShaUnlock;
begin
CSSha.Release;
end;
{%CLASSGROUP 'Vcl.Controls.TControl'}
{$R *.dfm}
procedure TdmMain.CalcNextSha;
begin
qSha.Active := True;
if qSha.RecordCount > 0 then
begin
tmrCalcSha.Interval := 100;
DoCalcNextSha(qSha.FieldByName('id').AsLargeInt,
TGlobals.Implode(qSha.FieldByName('path_base').AsString,
qSha.FieldByName('path').AsString,
qSha.FieldByName('name').AsString,
qSha.FieldByName('ext').AsString));
end else
begin
tmrCalcSha.Interval := 1000;
tmrCalcSha.Enabled := True;
end;
qSha.Active := False;
end;
procedure TdmMain.conMainAfterConnect(Sender: TObject);
begin
qFolders.Active := True;
qFiles.Active := True;
tmrCalcSha.Enabled := True;
end;
function TdmMain.CreateDB(const DBPath: string): Boolean;
var
con: TFDConnection;
scr: TFDScript;
s: TResourceStream;
begin
con := TFDConnection.Create(nil);
scr := TFDScript.Create(nil);
s := TResourceStream.Create(HInstance, 'RES_DB', 'TEXT');
try
con.DriverName := 'SQLite';
con.Params.Database := DBPath;
con.Params.Values['OpenMode'] := 'CreateUTF16';
con.Connected := True;
scr.Connection := con;
s.Seek(0, soFromBeginning);
scr.SQLScripts.Add.SQL.LoadFromStream(s);
Result := scr.ValidateAll and
scr.ExecuteAll;
finally
s.Free;
scr.Free;
con.Free;
end;
end;
function TdmMain.CreateQuery: TFDQuery;
begin
Result := TFDQuery.Create(nil);
Result.Connection := conMain;
end;
procedure TdmMain.DataModuleCreate(Sender: TObject);
var
fname: string;
begin
// Open database. Create if necessary.
fname := TGlobals.GetDBPath;
if TFile.Exists(fname) then
OpenDB(fname)
else
if CreateDB(fname) then
OpenDB(fname)
else
TGlobals.ShowWarning('Create database error.');
end;
procedure TdmMain.DataModuleDestroy(Sender: TObject);
begin
tmrCalcSha.Enabled := False;
end;
procedure TdmMain.DoCalcNextSha(const FileID: Int64; const FilePath: string);
begin
if TFile.Exists(FilePath) then
begin
TThread.CreateAnonymousThread(procedure
var
sha: string;
begin
if not ShaTryLock then
Exit;
sha := THashSHA2.GetHashStringFromFile(FilePath, THashSHA2.TSHA2Version.SHA256);
TThread.Synchronize(TThread.Current, procedure
begin
DoWriteSha(FileID, sha);
tmrCalcSha.Enabled := True;
end);
ShaUnlock;
end).Start;
end else
begin
DoDeleteFile(FileID);
ShaUnlock;
end;
end;
procedure TdmMain.DoDeleteFile(const FileID: Int64);
begin
with CreateQuery do
try
SQL.Text := 'DELETE FROM files WHERE id=:ID';
ParamByName('ID').AsLargeInt := FileID;
ExecSQL;
finally
Free;
end;
end;
procedure TdmMain.DoWriteSha(const FileID: Int64; Sha: string);
begin
with CreateQuery do
try
SQL.Text := 'UPDATE files SET sha=:SHA WHERE id=:ID';
ParamByName('SHA').AsString := Sha;
ParamByName('ID').AsLargeInt := FileID;
ExecSQL;
finally
Free;
end;
end;
procedure TdmMain.OpenDB(const DBPath: string);
begin
conMain.Params.Database := DBPath;
conMain.Connected := True;
end;
procedure TdmMain.RefreshFiles(FolderID: Int64);
begin
qFiles.Active := False;
qFiles.ParamByName('ID').AsLargeInt := FolderID;
qFiles.Active := True;
end;
procedure TdmMain.RefreshFolders;
begin
if qFolders.Active then
qFolders.Refresh
else
qFolders.Active := True;
end;
procedure TdmMain.tmrCalcShaTimer(Sender: TObject);
begin
tmrCalcSha.Enabled := False;
CalcNextSha;
end;
initialization
CSSha := TCriticalSection.Create;
finalization
CSSha.Free;
end.