-
Notifications
You must be signed in to change notification settings - Fork 6
/
uUsersLoader.pas
371 lines (338 loc) · 15.2 KB
/
uUsersLoader.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
unit uUsersLoader;
{******************************************************************************}
{* Users Loader Unit *}
{* Revolutionary Confederation of Anarcho Syndicalists *}
{* Written by: black.rabbit 2011 *}
{******************************************************************************}
interface
{$I 'std.inc'}
uses
Windows, SysUtils, Variants, Classes, Graphics,
Controls, ComCtrls, CommCtrl, Gauges,
ImgList, acAlphaImageList, jpeg, pngimage, acPNG,
{ utils }
DateUtils, Utils, Strings, VarRecs, Versions, EClasses,
DllThreads,
{ kernel }
Kernel, ProtoClasses, CryptoClasses, MetaClasses, ParaClasses,
HypoClasses, HyperClasses,
{ engine }
Engine,
{ SQLite }
SQLite3, SQLite3DLL, SQLiteTable3;
type
{ çàãðóç÷èê ïîëüçîâàòåëåé }
{$M+}
CUsersLoader = class of TUsersLoader;
PUsersLoader = ^TUsersLoader;
TUsersLoader = class (TDllThread)
private
f_DBFileName: String; { ôàéë áàçû äàííûõ }
f_DB: TSQLiteDatabase; { îáúåêò áàçû äàííûõ }
f_TreeNodes: TTreeNodes; { äåðåâî }
f_Images: TsAlphaImageList; { íàáîð ïèêòîãðàìì }
f_Gauge: TGauge; { ïîêàçàòåëü ïðîãðåññà çàãðóçêè }
f_StatusPanel: TStatusPanel; { ñòðîêà ñîñòîÿíèÿ çàãðóçêè }
f_ParentNode: TTreeNode; { íà÷àëüíûé óçåë }
f_Users: TUsers; { ñïèñîê ïîëüçîâàòåëåé }
f_UserIndex: LongInt; { ïîçèöèÿ â îáõîäå ñïèñêà }
public
constructor Create (anArgs: array of const); override;
destructor Destroy; override;
public
procedure Main; override;
procedure Return; override;
protected
procedure WriteStatus (const aMessage: String); overload;
procedure WriteStatus (const aMessage: String;
aParams: array of const); overload;
public
property DBFileName: String read f_DBFileName;
property DB: TSQLiteDatabase read f_DB write f_DB;
property TreeNodes: TTreeNodes read f_TreeNodes write f_TreeNodes;
property Images: TsAlphaImageList read f_Images write f_Images;
property Gauge: TGauge read f_Gauge write f_Gauge;
property StatusPanel: TStatusPanel read f_StatusPanel write f_StatusPanel;
property ParentNode: TTreeNode read f_ParentNode write f_ParentNode;
property Users: TUsers read f_Users write f_Users;
property UserIndex: LongInt read f_UserIndex write f_UserIndex;
end;
{$M-}
{ TUsersLoader Errors }
resourcestring
ERR_TUSERSLOADER_INCORRECT_DATABASE = 'Íå èíèöèàëèçèðîâàí îáúåêò ÁÄ!';
ERR_TUSERSLOADER_INCORRECT_NODES = 'Îáúåêò äåðåâà íå èíèöèàëèçèðîâàí!';
ERR_TUSERSLOADER_INCORRECT_NODE = 'Îáúåêò óçëà äåðåâà íå èíèöèàëèçèðîâàí!';
ERR_TUSERSLOADER_INCORRECT_ID = 'Óçåë äåðåâà ñîäåðæèò íåêîððåêòíûé èäåíòèôèêàòîð!';
ERR_TUSERSLOADER_INCORRECT_IMAGES = 'Êîíòåéíåð äëÿ èçîáðàæåíèé íå èíèöèàëèçèðîâàí!';
ERR_TUSERSLOADER_INCORRECT_GAUGE = 'Íå èíèöèàëèçèðîâàí ýëåìåíò êîíòðîëÿ ïðîãðåññà!';
ERR_TUSERSLOADER_INCORRECT_STATUS_PANEL = 'Íå èíèöèàëèçèðîâàíà ñòðîêà ñîñòîÿíèÿ!';
ERR_TUSERSLOADER_CREATE = 'Îøèáêà ñîçäàíèÿ ïîòîêà çàãðóçêè ñïèñêà ïîëüçîâàòåëåé!';
ERR_TUSERSLOADER_DESTROY = 'Îøèáêà óíè÷òîæåíèÿ ïîòîêà çàãðóçêè ñïèñêà ïîëüçîâàòåëåé!';
ERR_TUSERSLOADER_MAIN = 'Îøèáêà ãëàâíîé ôóíêöèè ïîòîêà!';
ERR_TUSERSLOADER_RETURN = 'Îøèáêà ôóíêöèè âîçâðàòà ïîòîêà!';
ERR_TUSERSLOADER_IMAGE_LOAD_FAILED = 'Îøèáêà çàãðóçêè èçîáðàæåíèÿ: %s';
ERR_TUSERSLOADER_WRITE_STATUS = 'Îøèáêà îòîáðàæåíèÿ ñòàòóñà!';
{ TUsersLoader Hints }
resourcestring
MSG_TUSERSLOADER_LOAD_OBJECT = 'Çàãðóçêà ''%s'' ...';
MSG_TUSERSLOADER_LOAD_OBJECT_PROPERTY = 'Çàãðóçêà ''%s'' ... %s';
implementation
{ TUsersLoader }
constructor TUsersLoader.Create (anArgs: array of const);
var
I : Integer;
args : array_of_const;
OBJ : TObject;
begin
try
{ ïåðåäàåì ïàðàìåòðû ñîçäàíèÿ áàçîâîãî ïîòîêà,
íà÷èíàÿ ñ ñåäüìîãî àðãóìåíòà }
if ( High (anArgs) >= 6 ) then
begin
SetLength ( Args, High (anArgs)-6 +1 );
for I := 6 to High (anArgs) do
args [I-6] := anArgs [I];
end
else
args := _array_of_const ([]);
inherited Create (args);
{ ÿâíî óêàçûâàåì íà íåîáõîäèìîñòü óíè÷òîæåíèÿ ïîòîêà ïî îêîí÷àíèþ ðàáîòû }
FreeOnTerminate := TRUE;
{ èìÿ ïîòîêà }
Name := ClassName;
{ ïåðâûé ïàðàìåòð - ôàéë ÁÄ }
f_DBFileName := '';
f_DB := NIL;
if notEmpty (0,anArgs) then
begin
f_DBFileName := toString (anArgs [0]);
f_DB := TSQLiteDatabase.Create (f_DBFileName);
end;
if ( not Assigned (f_DB) ) then
raise Exception.Create (ERR_TUSERSLOADER_INCORRECT_DATABASE);
{ âòîðîé ïàðàìåòð - TreeNodes
äåðåâî, êóäà áóäóò çàãðóæàòüñÿ ïîëüçîâàòåëè }
f_TreeNodes := NIL;
if notEmpty (1,anArgs) then
begin
OBJ := toObject (anArgs [1]);
if ( Assigned (OBJ) and OBJ.InheritsFrom (TTreeNodes) ) then
f_TreeNodes := TTreeNodes (OBJ);
end;
if ( not Assigned (f_TreeNodes) ) then
raise Exception.Create (ERR_TUSERSLOADER_INCORRECT_NODES);
{ òðåòèé ïàðàìåòð - ParentNode
êîðíåâîé óçåë }
f_ParentNode := NIL;
if notEmpty (2,anArgs) then
begin
OBJ := toObject (anArgs [2]);
if ( Assigned (OBJ) and OBJ.InheritsFrom (TTreeNode) ) then
f_ParentNode := TTreeNode (OBJ);
end;
{ ÷åòâåðòûé ïàðàìåòð - Images
êîíòåéíåð äëÿ àâàòàðîê ïîëüçîâàòåëåé }
f_Images := NIL;
if notEmpty (3,anArgs) then
begin
OBJ := toObject (anArgs [3]);
if ( Assigned (OBJ) and OBJ.InheritsFrom (TsAlphaImageList) ) then
f_Images := TsAlphaImageList (OBJ);
end;
{if ( not Assigned (f_Images) ) then
raise Exception.Create (ERR_TUSERSLOADER_INCORRECT_IMAGES);}
{if ( not Assigned (f_ParentNode) ) then
raise Exception.Create (ERR_TUSERSLOADER_INCORRECT_NODE);}
{ ïÿòûé ïàðàìåòð - Gauge
ïîêàçàòåëü ïðîãðåññà çàãðóçêè }
f_Gauge := NIL;
if notEmpty (4,anArgs) then
begin
OBJ := toObject (anArgs [4]);
if ( Assigned (OBJ) and OBJ.InheritsFrom (TControl) ) then
f_Gauge := TGauge (OBJ);
end;
{if ( not Assigned (f_Gauge) ) then
raise Exception.Create (ERR_TUSERSLOADER_INCORRECT_GAUGE);}
{ øåñòîé ïàðàìåòð - StatusPanel
ïîêàçàòåëü ïðîãðåññà çàãðóçêè }
f_StatusPanel := NIL;
if notEmpty (5,anArgs) then
begin
OBJ := toObject (anArgs [5]);
if ( Assigned (OBJ) and OBJ.InheritsFrom (TStatusPanel) ) then
f_StatusPanel := TStatusPanel (OBJ);
end;
{if ( not Assigned (f_StatusPanel) ) then
raise Exception.Create (ERR_TUSERSLOADER_INCORRECT_STATUS_PANEL);}
f_Users := NIL;
f_UserIndex := -1;
except on E: Exception do
_raise ([ 'Create', ERR_TUSERSLOADER_CREATE, E, Exception (FatalException) ],
['{09C45EE2-687D-48FA-9983-6EC266F4DC8F}']);
end;
end;
destructor TUsersLoader.Destroy;
begin
try
FreeAndNil (f_Users);
FreeAndNil (f_DB);
inherited Destroy;
except on E: Exception do
_raise ([ 'Destroy', ERR_TUSERSLOADER_DESTROY, E, Exception (FatalException) ],
['{4B2C7575-FFC3-4104-B9F7-D9D756D1D6B2}']);
end;
end;
procedure TUsersLoader.Main;
begin
try
if Terminated then Exit;
inherited Main;
if not Assigned (Users) then
f_Users := TUsers.Load (DB,[ _([]),
_([]),
_([USER_ID]) ]) as TUsers;
except on E: Exception do
_raise ([ 'Main', ERR_TUSERSLOADER_MAIN, E, Exception (FatalException) ],
['{87F29C26-7A30-4714-B7B5-0D9BECD76150}']);
end;
end;
procedure TUsersLoader.Return;
var
Node : TTreeNode;
Bmp : TBitMap;
begin
try
if Terminated then Exit;
if ( not Assigned (Users) or (Users.Count <= 0) ) then
begin
SetTabStatus (tabUsers,tbsLoaded);
Terminate;
end
else if UserIndex >= Users.Count-1 then
begin
SetTabStatus (tabUsers,tbsLoaded);
Terminate;
end
else
try
inherited Return;
UserIndex := UserIndex + 1;
if Assigned (StatusPanel) then
StatusPanel.Text := Format (MSG_TUSERSLOADER_LOAD_OBJECT,
[ Users.ItemAt [UserIndex].Login ]);
Node := TreeNodes.AddChild ( ParentNode,
{Format (' %s '#13#10' %s ',
[ Users.ItemAt [UserIndex].Login,
Users.ItemAt [UserIndex].Description ])}
''
);
Node.ImageIndex := -1;
Node.SelectedIndex := -1;
Node.Data := CreateUserNode;
with PUserNode (Node.Data)^ do
begin
//WriteStatus (MSG_TUSERSLOADER_LOAD_OBJECT_PROPERTY,
// [ Users.ItemAt [UserIndex].Login,
// Users.ItemAt [UserIndex].PropAt [_usr_id].Caption ]);
ID := Users.ItemAt [UserIndex].ID;
//WriteStatus (MSG_TUSERSLOADER_LOAD_OBJECT_PROPERTY,
// [ Users.ItemAt [UserIndex].Login,
// Users.ItemAt [UserIndex].PropAt [_usr_id_pic].Caption ]);
IDPic := Users.ItemAt [UserIndex].IDPic;
//WriteStatus (MSG_TUSERSLOADER_LOAD_OBJECT_PROPERTY,
// [ Users.ItemAt [UserIndex].Login,
// Users.ItemAt [UserIndex].PropAt [_usr_login].Caption ]);
Login := Users.ItemAt [UserIndex].Login;
//WriteStatus (MSG_TUSERSLOADER_LOAD_OBJECT_PROPERTY,
// [ Users.ItemAt [UserIndex].Login,
// Users.ItemAt [UserIndex].PropAt [_usr_description].Caption ]);
Description := Users.ItemAt [UserIndex].Description;
//WriteStatus (MSG_TUSERSLOADER_LOAD_OBJECT_PROPERTY,
// [ Users.ItemAt [UserIndex].Login,
// Users.ItemAt [UserIndex].PropAt [_usr_public_key].Caption ]);
PublicKey := Users.ItemAt [UserIndex].PublicKey;
//WriteStatus (MSG_TUSERSLOADER_LOAD_OBJECT_PROPERTY,
// [ Users.ItemAt [UserIndex].Login,
// Users.ItemAt [UserIndex].PropAt [_usr_time_stamp_create].Caption ]);
TimeStampCreate := Users.ItemAt [UserIndex].TimeStampCreate;
//WriteStatus (MSG_TUSERSLOADER_LOAD_OBJECT_PROPERTY,
// [ Users.ItemAt [UserIndex].Login,
// Users.ItemAt [UserIndex].PropAt [_usr_time_stamp_modify].Caption ]);
TimeStampModify := Users.ItemAt [UserIndex].TimeStampModify;
//WriteStatus (MSG_TUSERSLOADER_LOAD_OBJECT_PROPERTY,
// [ Users.ItemAt [UserIndex].Login,
// Users.ItemAt [UserIndex].PropAt [_usr_time_stamp_public].Caption ]);
TimeStampPublic := Users.ItemAt [UserIndex].TimeStampPublic;
//WriteStatus (MSG_TUSERSLOADER_LOAD_OBJECT_PROPERTY,
// [ Users.ItemAt [UserIndex].Login,
// Users.ItemAt [UserIndex].PropAt [_usr_key_hash].Caption ]);
KeyHash := Users.ItemAt [UserIndex].KeyHash;
//WriteStatus (MSG_TUSERSLOADER_LOAD_OBJECT_PROPERTY,
// [ Users.ItemAt [UserIndex].Login,
// Users.ItemAt [UserIndex].PropAt [_usr_data_hash].Caption ]);
DataHash := Users.ItemAt [UserIndex].DataHash;
end;
if Assigned (Images) and
( Users.ItemAt [UserIndex].IDPic > 0 ) then
try
//WriteStatus (MSG_TUSERSLOADER_LOAD_OBJECT_PROPERTY,
// [ Users.ItemAt [UserIndex].Login,
// Users.ItemAt [UserIndex].Pic.GetClassName ]);
Images.AcBeginUpdate;
Bmp := TBitMap.Create;
try
if Assigned ( Users.ItemAt [UserIndex].Pic.Picture ) and
( (Users.ItemAt [UserIndex].Pic.Picture.Width > 0) or
(Users.ItemAt [UserIndex].Pic.Picture.Height > 0) ) then
Bmp.Assign (Users.ItemAt [UserIndex].Pic.Bitmap)
{else if Assigned (MainForm) then
Bmp.Assign (MainForm.imgAnonymous.Picture.Graphic)};
ResizeImage (Bmp,Images.Width,Images.Height,2,Images.BkColor);
Images.Add (Bmp,NIL);
Node.ImageIndex := UserIndex;
Node.SelectedIndex := UserIndex;
finally
FreeAndNil (Bmp);
Images.AcEndUpdate;
end;
except on E: Exception do begin
Node.ImageIndex := -1;
Node.SelectedIndex := -1;
WriteStatus (ERR_TUSERSLOADER_IMAGE_LOAD_FAILED,[E.Message]);
end; end;
if Assigned (Gauge) then
Gauge.Progress := Gauge.Progress + 1;
except on E: Exception do
WriteStatus (E.Message);
end;
except on E: Exception do
_raise ([ 'Return', ERR_TUSERSLOADER_RETURN, E, Exception (FatalException) ],
['{A99D218D-5E59-4A6E-9E8A-B2B41D785941}']);
end;
end;
procedure TUsersLoader.WriteStatus (const aMessage: String);
begin
try
if Assigned (StatusPanel) then
begin
StatusPanel.Text := aMessage;
ProcessMessages;
end;
except on E: Exception do
_raise (['WriteStatus',ERR_TUSERSLOADER_WRITE_STATUS,E],
['{09F81E23-AE5B-4876-8D81-288D2418E508}']);
end;
end;
procedure TUsersLoader.WriteStatus (const aMessage: String;
aParams: array of const);
begin
try
WriteStatus ( Format (aMessage,aParams) );
except on E: Exception do
_raise (['WriteStatus',ERR_TUSERSLOADER_WRITE_STATUS,E],
['{E5135175-7326-4CC4-8B15-1E2424B1F2B4}']);
end;
end;
end.