-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilesmonitoru.pas
218 lines (192 loc) · 4.48 KB
/
filesmonitoru.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
unit FilesMonitorU;
{
Thread files monitoring
(c) Miguel A. Risco Castillo
Version: 0.2
Date: 2018/04/23
Based on ATFileNotif by Alexey Torgashin
}
{$mode objfpc}{$H+}
interface
uses
Forms, Dialogs, SysUtils, Classes, ExtCtrls, LazFileUtils,
DebugU;
type
{ TFilesMon }
TFilesMon = class(TThread)
private
FEnabled: Boolean;
FFiles: TStringList;
FInterval: Integer;
FOnChanged: TNotifyEvent;
fEvent:PRTLEvent;
procedure SetEnabled(AValue: Boolean);
procedure DoChanged;
procedure SetInterval(AValue: Integer);
protected
procedure Execute; override;
public
FileChanged:String;
constructor Create(AEnable:Boolean=false; AOnChanged:TNotifyEvent=nil; AInterval: Integer=1000);
destructor Destroy; override;
procedure Terminate;
procedure AddFile(const AValue: string);
procedure DelFile(const AValue: string);
procedure UpdateFile(const AValue: string);
published
property Files: TStringList read FFiles;
property OnChanged: TNotifyEvent read FOnChanged write FOnChanged;
property Enabled:Boolean read FEnabled write SetEnabled;
property Interval:Integer read FInterval write SetInterval;
end;
implementation
type
TFileSRec = class(TObject)
FExist: Boolean;
FSize: Int64;
FAge: Longint;
end;
{ Helper functions }
procedure FGetFileRec(const FileName: string; var Rec: TFileSRec);
begin
Rec.FExist:= FileExistsUTF8(FileName);
Rec.FSize:= FileSizeUtf8(FileName);
Rec.FAge:= FileAgeUTF8(FileName);
end;
function CheckFileChanged(const FileName: string; var OldRec: TFileSRec): Boolean;
var
NewRec: TFileSRec;
begin
NewRec:=TFileSRec.Create;
try
FGetFileRec(FileName, NewRec);
Result:=
(OldRec.FExist <> NewRec.FExist) or
(OldRec.FSize <> NewRec.FSize) or
(OldRec.FAge <> NewRec.FAge);
if Result then
begin
OldRec.FExist:=NewRec.FExist;
OldRec.FSize:=NewRec.FSize;
OldRec.FAge:=NewRec.FAge;
end;
finally
NewRec.Free;
end;
end;
{ TFilesMon }
constructor TFilesMon.Create(AEnable: Boolean; AOnChanged: TNotifyEvent;
AInterval: Integer);
begin
FFiles:=TStringList.Create;
FFiles.OwnsObjects:=true;
FileChanged:='';
FInterval:= AInterval;
FEnabled:= AEnable;
FOnChanged:=AOnChanged;
fEvent := RTLEventCreate;
inherited Create(true);
FreeOnTerminate := True;
Start;
end;
destructor TFilesMon.Destroy;
begin
FEnabled:= False;
Terminate;
if assigned(FFiles) then FreeAndNil(FFiles);
RTLeventdestroy(fEvent);
inherited Destroy;
end;
procedure TFilesMon.Terminate;
begin
FEnabled:=false;
inherited Terminate;
RTLeventSetEvent(fEvent);
end;
procedure TFilesMon.AddFile(const AValue: string);
var
En: Boolean;
FFileRec: TFileSRec;
begin
Info('TFilesMon.AddFile',AValue);
En:= FEnabled;
FEnabled:= False;
FFileRec:=TFileSRec.Create;
FGetFileRec(AValue, FFileRec);
if (AValue <> '') and (not FFileRec.FExist) then
begin
FFileRec.Free;
raise Exception.Create('File to watch doesn''t exist')
end else
if FFiles.IndexOf(AValue)=-1 then
FFiles.AddObject(AValue,FFileRec)
else
FFileRec.Free;
FEnabled:= En;
end;
procedure TFilesMon.DelFile(const AValue: string);
var
En: Boolean;
i:integer;
begin
Info('TFilesMon.DelFile',AValue);
En:= FEnabled;
FEnabled:= False;
i:=FFiles.IndexOf(AValue);
if i>=0 then FFiles.Delete(i);
FEnabled:= En;
end;
procedure TFilesMon.UpdateFile(const AValue: string);
var
En: Boolean;
i:integer;
FileRec:TFileSRec;
begin
Info('TFilesMon.UpdateFile',AValue);
En:= FEnabled;
FEnabled:= False;
i:=FFiles.IndexOf(AValue);
if i>=0 then
begin
FileRec:=TFileSRec(FFiles.Objects[i]);
FGetFileRec(AValue,FileRec);
end;
FEnabled:= En;
end;
procedure TFilesMon.SetEnabled(AValue: Boolean);
begin
if FEnabled=AValue then Exit;
FEnabled:=AValue;
end;
procedure TFilesMon.Execute;
var
i:integer;
FileSRec:TFileSRec;
begin
While not Terminated do
begin
For i:=0 to FFiles.Count-1 do
begin
if Terminated or not FEnabled then break;
FileSRec:=TFileSRec(FFiles.Objects[i]);
if CheckFileChanged(FFiles[i],FileSRec) then
begin
FileChanged:=FFiles[i];
Synchronize(@DoChanged);
end;
end;
RTLeventWaitFor(fEvent,FInterval);
RTLeventResetEvent(fEvent);
end;
end;
procedure TFilesMon.DoChanged;
begin
if Assigned(FOnChanged) then
FOnChanged(Self);
end;
procedure TFilesMon.SetInterval(AValue: Integer);
begin
if FInterval=AValue then Exit;
FInterval:=AValue;
end;
end.