-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathUnit_HistoryManager.pas
More file actions
426 lines (381 loc) · 11 KB
/
Copy pathUnit_HistoryManager.pas
File metadata and controls
426 lines (381 loc) · 11 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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
unit Unit_HistoryManager;
{$I OllmaClient_Defines.inc}
interface
uses
Winapi.Windows,
Winapi.Messages,
System.SysUtils,
System.Variants,
System.Classes,
System.SyncObjs,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.StdCtrls,
Vcl.ExtCtrls,
Vcl.ComCtrls;
type
THisObject = class
ho_Session: Integer;
ho_Filename: string;
constructor Create(const AFileName: string);
end;
type
THistoryManager = class
private
FHistoryFile: string;
FLinkedFile: string;
FListBox: TListBox;
FLinkedFileList : TStringList;
FCriticalSection: TCriticalSection;
procedure FreeListObjects(const AClearFlag: Boolean = False);
procedure UpdateHistory(const AFlag: Integer = 0);
procedure Clearance_HistoryFiles_All(const AFlag: Integer = 0);
function Get_OverwriteFlag(const ASubject: string): Boolean;
procedure Clearance_ListBox;
procedure Lock();
procedure Unlock();
public
constructor Create(AListBox: TListBox);
destructor Destroy; override;
//
function AddToHistory(const AFlag: Integer; const ASubject, AFile: string): Integer;
function DeleteHistory(const AIndex: Integer): Boolean;
procedure Load_HstoryList(const AListfile: string = '');
procedure Clear_ViewAll(const AFlag: Integer = 0);
procedure Clear_ListData(const AFlag: Integer = 0);
function Clearance_HistoryFiles(const AFlag: Integer = 0): Integer;
procedure SetSelectionOfSubject(const ASubject: string);
function Is_HistorySubject(const ASubject: string): Integer;
function Get_HistoryCurrent(): string;
function Get_HistoryFile(const ASubject: string; var VOvereriteFlag: Boolean): string; overload;
function Get_HistoryFile(const AIndex: Integer): string; overload;
function Get_HistoryFileSession(const AIndex: Integer; var VSession: Integer): string;
end;
implementation
uses
System.Math,
System.IOUtils,
Unit_Common;
{ THisObject }
constructor THisObject.Create(const AFileName: string);
begin
ho_Session := RandomRange(1000000, 9999999);
ho_Filename := AFileName;
end;
{ THistory_Manager ... }
constructor THistoryManager.Create(AListBox: TListBox);
begin
Randomize;
FHistoryFile := CV_HisPath+'history.lst';
FLinkedFile := CV_HisPath+'attached.txt'; // in preparation for a very long string of history subject ...
FListBox := AListBox;
FCriticalSection := TCriticalSection.Create;
FLinkedFileList := TStringList.Create;
Load_HstoryList();
end;
procedure THistoryManager.Load_HstoryList(const AListfile: string='');
begin
if FileExists(FHistoryFile) and FileExists(FLinkedFile) then
begin
Screen.Cursor := crAppStart;
var _HistoryList := TStringList.Create;
_HistoryList.LoadFromFile(FHistoryFile);
FLinkedFileList.Clear;
FLinkedFileList.LoadFromFile(FLinkedFile);
try
if _HistoryList.Count <= FLinkedFileList.Count then // To cope with an error situation
FListBox.Clear;
FListBox.Items.BeginUpdate;
with FListBox do
try
var _hisObject: THisObject := nil;
var _Subject: string := '';
var _File: string := '';
for var _i := 0 to _HistoryList.Count-1 do
begin
_Subject := _HistoryList.Strings[_i];
_File := FLinkedFileList.Strings[_i];
_hisObject := THisObject.Create(_File);
AddItem(_Subject, TObject(_hisObject));
end;
finally
Items.EndUpdate;
end;
finally
_HistoryList.Free;
Screen.Cursor := crDefault;
end;
end;
end;
procedure THistoryManager.SetSelectionOfSubject(const ASubject: string);
begin
var _index := FListBox.Items.IndexOf(ASubject);
if _index >= 0 then
begin
FListBox.ClearSelection;
FListBox.Selected[_index] := True;
end;
end;
procedure THistoryManager.Lock;
begin
FCriticalSection.Enter();
end;
procedure THistoryManager.Unlock;
begin
FCriticalSection.Leave();
end;
procedure THistoryManager.UpdateHistory(const AFlag: Integer = 0);
begin
if AFlag > 0 then Lock();
try
FLinkedFileList.Clear;
var _file: string := '';
with FListBox.Items do
for var _idx := 0 to Count -1 do
begin
_file := THisObject(Objects[_idx]).ho_Filename;
FLinkedFileList.Add(_file);
end;
if AFlag = 0 then
begin
FListBox.Items.SaveToFile(FHistoryFile); // OverWrite ...
FLinkedFileList.SaveToFile(FLinkedFile); // OverWrite ...
end;
finally
if AFlag > 0 then UnLock();
end;
end;
destructor THistoryManager.Destroy;
begin
var _dlinkedlist := TStringList.Create;
if FListBox.Items.Count > HIS_MAX_ITEMS then
begin
FListBox.Items.BeginUpdate;
with FListBox.Items do
for var _i := Count-1 downto HIS_MAX_ITEMS do
try
_dlinkedlist.Add(THisObject(Objects[_i]).ho_Filename);
THisObject(Objects[_i]).Free;
Objects[_i] := nil;
finally
Delete(_i);
end;
FListBox.Items.EndUpdate;
end;
try
if _dlinkedlist.Count > 0 then
begin
with _dlinkedlist do
for var _i := 0 to Count-1 do
Safety_DeleteFile(Strings[_i]);
end;
finally
_dlinkedlist.Free;
end;
if FListBox.Items.Count = 0 then
begin
Safety_DeleteFile(FHistoryFile);
Safety_DeleteFile(FLinkedFile);
end
else
begin
UpdateHistory(0); // SaveToFile ...
FreeListObjects();
end;
FLinkedFileList.Free;
FCriticalSection.Free;
inherited;
end;
function THistoryManager.Get_OverwriteFlag(const ASubject: string): Boolean;
begin
Result := FListBox.Items.IndexOf(ASubject) >= 0;
end;
function THistoryManager.Is_HistorySubject(const ASubject: string): Integer;
begin
Result := FListBox.Items.IndexOf(ASubject);
end;
function THistoryManager.AddToHistory(const AFlag: Integer; const ASubject, AFile: string): Integer;
begin
Result := -1;
var _index := FListBox.Items.IndexOf(ASubject);
FListBox.Items.BeginUpdate;
with FListBox do
try
if _index < 0 then
begin
var _hisObject: THisObject := THisObject.Create(AFile);
if Items.Count = 0 then
AddItem(ASubject, TObject(_hisObject))
else
Items.InsertObject(0, ASubject, TObject(_hisObject));
Result := 0;
end
else
begin
Result := _index;
THisObject(Items.Objects[_index]).ho_Filename := AFile;
end;
finally
Items.EndUpdate;
end;
UpdateHistory(2);
end;
procedure THistoryManager.FreeListObjects(const AClearFlag: Boolean = False);
begin
FListBox.Items.BeginUpdate;
with FListBox do
try
for var _idx := Count -1 downto 0 do
begin
THisObject(Items.Objects[_idx]).Free;
Items.Objects[_idx] := nil;
end;
finally
if AClearFlag then
Clear;
end;
FListBox.Items.EndUpdate;
end;
function THistoryManager.Get_HistoryFile(const ASubject: string; var VOvereriteFlag: Boolean): string;
begin
Result := '';
VOvereriteFlag := False;
var _index := FListBox.Items.IndexOf(ASubject);
if _index >= 0 then
begin
VOvereriteFlag := True;
Result := THisObject(FListBox.Items.Objects[_index]).ho_Filename;
end;
end;
function THistoryManager.Get_HistoryFile(const AIndex: Integer): string;
begin
Result := '';
if FListBox.Count > 0 then
begin
if (AIndex >= 0) and (AIndex < FListBox.Count) then
Result := THisObject(FListBox.Items.Objects[AIndex]).ho_Filename;
end;
end;
function THistoryManager.Get_HistoryFileSession(const AIndex: Integer; var VSession: Integer): string;
begin
Result := '';
if FListBox.Count > 0 then
begin
if (AIndex >= 0) and (AIndex < FListBox.Count) then
begin
VSession := THisObject(FListBox.Items.Objects[AIndex]).ho_Session;
Result := THisObject(FListBox.Items.Objects[AIndex]).ho_Filename;
end;
end;
end;
function THistoryManager.Get_HistoryCurrent: string;
begin
Result := '';
if (FListBox.Count > 0) and (FListBox.ItemIndex >= 0) then
begin
Result := FListBox.Items.Strings[FListBox.ItemIndex];
end;
end;
function THistoryManager.DeleteHistory(const AIndex: Integer): Boolean;
begin
Result := False;
if (FListBox.Count > 0) and
((AIndex >= 0) and (AIndex <= FListBox.Count-1)) then
begin
var _hfile := Get_HistoryFile(AIndex);
FListBox.Items.BeginUpdate;
with FListBox.Items do
try
THisObject(Objects[AIndex]).Free;
Objects[AIndex] := nil;
Delete(AIndex);
finally
EndUpdate;
end;
Safety_DeleteFile(_hfile);
Result := not FileExists(_hfile);
UpdateHistory(1);
end;
end;
procedure THistoryManager.Clearance_ListBox();
begin
FListBox.Items.BeginUpdate;
with FListBox do
try
var _ofilename: string := '';
var _delindex: Integer := -1;
for var _idx := Count -1 downto 0 do
begin
_ofilename := THisObject(Items.Objects[_idx]).ho_Filename;
if not FileExists(_ofilename) then
begin
THisObject(Items.Objects[_idx]).Free;
Items.Objects[_idx] := nil;
Items.Delete(_idx);
end;
end;
finally
Items.EndUpdate;
end;
UpdateHistory(3);
end;
function THistoryManager.Clearance_HistoryFiles(const AFlag: Integer = 0): Integer;
begin
Result := 0;
Clearance_ListBox();
if FLinkedFileList.Count < 1 then Exit;
var _srec: TSearchRec;
if FindFirst(CV_HisPath + '*.*', faAnyFile, _srec) = 0 then
begin
var _fname := '';
var _ext := '.dat';
try
repeat
_fname := CV_HisPath + _srec.Name;
_ext := ExtractFileExt(_srec.Name);
if SameText(_ext, '.dat') and (FLinkedFileList.IndexOf(_fname) < 0) then
if Safety_DeleteFile(_fname) then
Inc(Result);;
until FindNext(_srec) <> 0;
finally
FindClose(_srec);
end;
end;
end;
procedure THistoryManager.Clearance_HistoryFiles_All(const AFlag: Integer = 0);
begin
var _srec: TSearchRec;
if FindFirst(CV_HisPath + '*.*', faAnyFile, _srec) = 0 then
begin
var _fname := '';
var _ext := '.dat';
try
repeat
_fname := CV_HisPath + _srec.Name;
_ext := ExtractFileExt(_fname);
if SameText(_ext, '.dat') then
TFile.Delete(_fname);
until FindNext(_srec) <> 0;
finally
FindClose(_srec);
end;
end;
end;
procedure THistoryManager.Clear_ListData(const AFlag: Integer = 0);
begin
FreeListObjects(True);
Safety_DeleteFile(FHistoryFile);
Safety_DeleteFile(FLinkedFile);
Clearance_HistoryFiles_All(0);
end;
procedure THistoryManager.Clear_ViewAll(const AFlag: Integer = 0);
begin
UpdateHistory(0);
FreeListObjects(True);
end;
{ Referance ...
TFileStream.WriteComponent(ListBox_History);
TFileStream.ReadComponent(ListBox_History);
... Referance }
end.