forked from artem78/AutoScreenshot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patholdscreenshotcleaner.pas
217 lines (174 loc) · 4.94 KB
/
oldscreenshotcleaner.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
unit OldScreenshotCleaner;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, ExtCtrls;
type
TIntervalUnit = (iuHours, iuDays, iuWeeks, iuMonths);
TInterval = record
Val: Cardinal;
Unit_: TIntervalUnit;
end;
TOldScreenshotCleanerChangeCallback = procedure of object;
{ TOldScreenshotCleaner }
TOldScreenshotCleaner = class
private
FMaxAge: TInterval;
FOnChangeCallback: TOldScreenshotCleanerChangeCallback;
Timer: TTimer;
procedure SetMaxAge(AMaxAge: TInterval);
procedure SetActive(AActive: Boolean);
function GetActive: Boolean;
procedure DoOnTimer(ASender: TObject);
procedure UpdateUI;
public
constructor Create;
destructor Destroy; override;
property MaxAge: TInterval read FMaxAge write SetMaxAge;
property Active: Boolean read GetActive write SetActive;
property OnChangeCallback: TOldScreenshotCleanerChangeCallback
read FOnChangeCallback write FOnChangeCallback;
procedure Start;
procedure Stop;
end;
operator explicit (const AInterval: TInterval): String;
operator explicit (const AStr: String): TInterval;
operator - (ADateTime: TDateTime; AInterval: TInterval): TDateTime;
implementation
uses LazLoggerBase, uUtils, ScreenGrabber, DateUtils,
///////////
uAutoScreen, Forms
///////////
;
const
RunInterval: Integer =
{$IFOPT D+}
5 * MSecsPerSec;
{$Else}
30 * MSecsPerSec * SecsPerMin; // 30 minutes
{$ENDIF}
operator explicit (const AInterval: TInterval): String;
var
UnitShortName: Char;
begin
case AInterval.Unit_ of
iuHours: UnitShortName := 'h';
iuDays: UnitShortName := 'd';
iuWeeks: UnitShortName := 'w';
iuMonths: UnitShortName := 'm';
end;
Result := IntToStr(AInterval.Val) + UnitShortName;
end;
operator explicit (const AStr: String): TInterval;
var
UnitShortName: Char;
begin
if AStr.IsEmpty then
raise Exception.Create('Empty string given');
UnitShortName := AStr[Length(AStr)];
case UnitShortName of
'h': Result.Unit_ := iuHours;
'd': Result.Unit_ := iuDays;
'w': Result.Unit_ := iuWeeks;
'm': Result.Unit_ := iuMonths;
else raise Exception.CreateFmt('Unknown unit character ''%s''', [UnitShortName]);
end;
Result.Val := StrToInt(Copy(AStr, 1, Length(AStr) - 1));
end;
operator - (ADateTime: TDateTime; AInterval: TInterval): TDateTime;
begin
case AInterval.Unit_ of
iuHours: Result := IncHour(ADateTime, -AInterval.Val);
iuDays: Result := IncDay(ADateTime, -AInterval.Val);
iuWeeks: Result := IncDay(ADateTime, -AInterval.Val * 7);
iuMonths: Result := IncMonth(ADateTime, -AInterval.Val);
end;
end;
{ TOldScreenshotCleaner }
procedure TOldScreenshotCleaner.SetMaxAge(AMaxAge: TInterval);
begin
//if (FMaxAge.Unit_ = AMaxAge.Unit_) and (FMaxAge.Val = AMaxAge.Val) then
// Exit;
FMaxAge := AMaxAge;
if Assigned(FOnChangeCallback) then
FOnChangeCallback;
end;
procedure TOldScreenshotCleaner.SetActive(AActive: Boolean);
begin
//if FActive = AActive then
// Exit;
if AActive then
Start
else
Stop;
if Assigned(FOnChangeCallback) then
FOnChangeCallback;
end;
function TOldScreenshotCleaner.GetActive: Boolean;
begin
Result := Timer.Enabled;
end;
procedure TOldScreenshotCleaner.DoOnTimer(ASender: TObject);
var
MaxDateTime: TDateTime;
ImgExts: TStringList;
ImgFmt: TImageFormat;
Dir: String;
begin
// Set normal timer interval at first run
if Timer.Interval <> RunInterval then
Timer.Interval := RunInterval;
MaxDateTime := Now - MaxAge;
//DebugLn('MaxDateTime=', DateTimeToStr(MaxDateTime));
ImgExts := TStringList.Create;
try
for ImgFmt in TImageFormat do
ImgExts.Append(ImageFormatInfoArray[ImgFmt].Extension);
//DebugLn('ImgExts=', ImgExts.CommaText);
Dir := MainForm.OutputDirEdit.Directory {TODO: remake this!};
Assert(not Dir.IsEmpty, 'Wrong path!');
Assert(Dir <> '/', 'Wrong path!');
DebugLn('Start clearing old screenshots until ', DateTimeToStr(MaxDateTime));
DebugLnEnter;
DeleteOldFiles(Dir, MaxDateTime, True, ImgExts.ToStringArray, True, @UpdateUI);
finally
ImgExts.Free;
DebugLnExit;
DebugLn('Old files cleaning finished');
end;
end;
procedure TOldScreenshotCleaner.UpdateUI;
begin
Application.ProcessMessages;
end;
constructor TOldScreenshotCleaner.Create;
begin
Timer := TTimer.Create(Nil);
Timer.Enabled := False;
Timer.OnTimer := @DoOnTimer;
end;
destructor TOldScreenshotCleaner.Destroy;
begin
Stop;
Timer.Free;
inherited Destroy;
end;
procedure TOldScreenshotCleaner.Start;
begin
if not Timer.Enabled then
begin
Timer.Interval := 1 * MSecsPerSec; // To start immediately, will be
// increased later after first run
Timer.Enabled := True;
DebugLn('Old screenshot cleaner started');
end;
end;
procedure TOldScreenshotCleaner.Stop;
begin
if Timer.Enabled then
begin
Timer.Enabled := False;
DebugLn('Old screenshot cleaner stopped');
end;
end;
end.