-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnonvisualctrlsdm.pas
428 lines (373 loc) · 11.2 KB
/
nonvisualctrlsdm.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
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
427
428
unit NonVisualCtrlsDM;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, XMLConf, Controls, ActnList, ExtCtrls, Menus, Models,
PeriodEditFrm, Utils, TrayIconEx;
type
{ TNonVisualCtrlsDataModule }
TNonVisualCtrlsDataModule = class(TDataModule)
LanguageIcons: TImageList;
ShowDoneTasksAction: TAction;
BackupDatabaseAction: TAction;
ExportDatabaseAction: TAction;
DeletePeriodAction: TAction;
EditPeriodAction: TAction;
CreatePeriodAction: TAction;
DeleteTaskAction: TAction;
EditTaskAction: TAction;
CreateTaskAction: TAction;
ActionList: TActionList;
DelimiterMenuItem: TMenuItem;
ExitAction: TAction;
ExitMenuItem: TMenuItem;
Icons: TImageList;
MarkTaskAsDoneAction: TAction;
ServiceMenuItem: TMenuItem;
ExportMenuItem: TMenuItem;
StartTimeTrackingMenuItem: TMenuItem;
StartTimeTrackingAction: TAction;
StopTimeTrackingAction: TAction;
StopTimeTrackingMenuItem: TMenuItem;
TrayIcon: TTrayIconEx;
TrayPopupMenu: TPopupMenu;
UnMarkTaskAsDoneAction: TAction;
XMLConfig: TXMLConfig;
procedure BackupDatabaseActionExecute(Sender: TObject);
procedure CreatePeriodActionExecute(Sender: TObject);
procedure CreateTaskActionExecute(Sender: TObject);
procedure DataModuleCreate(Sender: TObject);
procedure DeletePeriodActionExecute(Sender: TObject);
procedure DeleteTaskActionExecute(Sender: TObject);
procedure EditPeriodActionExecute(Sender: TObject);
procedure EditTaskActionExecute(Sender: TObject);
procedure ExitActionExecute(Sender: TObject);
procedure ExportDatabaseActionExecute(Sender: TObject);
procedure MarkTaskAsDoneActionExecute(Sender: TObject);
procedure ShowDoneTasksActionExecute(Sender: TObject);
procedure StartTimeTrackingActionExecute(Sender: TObject);
procedure StopTimeTrackingActionExecute(Sender: TObject);
procedure TrayIconDblClick(Sender: TObject);
procedure TrayIconMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure UnMarkTaskAsDoneActionExecute(Sender: TObject);
{procedure UniqueInstance1OtherInstance(Sender: TObject;
ParamCount: Integer; const Parameters: array of String);}
private
procedure LoadIconsFromResources;
public
procedure RunningTaskUpdated;
end;
const
// Indexes for icons in image list
NoIconIdx = -1;
StartIconIdx = 0;
StopIconIdx = 1;
TickIconIdx = 2;
TickGrayIconIdx = 3;
CreateIconIdx = 4;
DeleteIconIdx = 5;
EditIconIdx = 6;
ExitIconIdx = 7;
EnFlagIconIdx = 0;
RuFlagIconIdx = 1;
var
NonVisualCtrlsDataModule: TNonVisualCtrlsDataModule;
implementation
uses
DatabaseDM, main, taskedit, Dialogs;
resourcestring
RSDone = 'Done!';
RSConfirmDeletePeriod = 'Are you sure to delete period?';
RSSavedIn = 'Saved in "%s"';
RSConfirmDeleteTask = 'Are you sure to delete task "%s"?';
{$R *.lfm}
{ TNonVisualCtrlsDataModule }
procedure TNonVisualCtrlsDataModule.StartTimeTrackingActionExecute(
Sender: TObject);
var
Task: TTask;
begin
Task := TTask.GetById(DatabaseDataModule.TasksSQLQuery.FieldByName('id').AsInteger);
Task.Start;
Task.Free;
RunningTaskUpdated;
end;
procedure TNonVisualCtrlsDataModule.ExitActionExecute(Sender: TObject);
begin
MainForm.Close;
end;
procedure TNonVisualCtrlsDataModule.ExportDatabaseActionExecute(Sender: TObject
);
begin
with TSaveDialog.Create(nil) do
begin
try
Filter := 'XML|*.xml';
DefaultExt := 'xml';
FileName := Format('export %s.xml', [FormatDateTime('yyyy-mm-dd hh-nn-ss', Now)]);
if Execute then
begin
DatabaseDataModule.ExportDatabase(FileName);
ShowMessage(RSDone);
end;
finally
Free;
end;
end;
end;
procedure TNonVisualCtrlsDataModule.CreateTaskActionExecute(Sender: TObject);
begin
with DatabaseDataModule.TasksSQLQuery do
begin
Append;
if TaskEditForm.ShowModal = mrOK then
begin
FieldByName('created').AsDateTime := Now;
Post;
ApplyUpdates;
DatabaseDataModule.SQLTransaction1.CommitRetaining;
end
else
Cancel;
end;
end;
procedure TNonVisualCtrlsDataModule.DataModuleCreate(Sender: TObject);
begin
LoadIconsFromResources;
end;
procedure TNonVisualCtrlsDataModule.DeletePeriodActionExecute(Sender: TObject);
var
Msg: String;
begin
with DatabaseDataModule.PeriodsSQLQuery do
begin
if IsEmpty then
Exit;
Msg := RSConfirmDeletePeriod;
if MessageDlg(Msg, mtConfirmation, mbYesNo, 0) = mrYes then
begin
Delete;
ApplyUpdates;
DatabaseDataModule.SQLTransaction1.CommitRetaining;
end;
end;
end;
procedure TNonVisualCtrlsDataModule.CreatePeriodActionExecute(Sender: TObject);
begin
with DatabaseDataModule.PeriodsSQLQuery do
begin
Append;
FieldByName('begin').AsDateTime := Now;
FieldByName('end').AsDateTime := Now;
FieldByName('task_id').AsInteger := DatabaseDataModule.TasksSQLQuery.FieldByName('id').AsInteger;
FieldByName('is_manually_added').AsBoolean := True;
if PeriodEditForm.ShowModal = mrOK then
begin
Post;
ApplyUpdates;
DatabaseDataModule.SQLTransaction1.CommitRetaining;
end
else
Cancel;
end;
end;
procedure TNonVisualCtrlsDataModule.BackupDatabaseActionExecute(Sender: TObject
);
var
BackupFileName: String;
begin
try
BackupFileName := DatabaseDataModule.SaveDatabaseBackup;
MessageDlg(Format(RSSavedIn, [BackupFileName]), mtInformation, [mbOk] , 0);
except
on E: Exception do
MessageDlg(E.Message, {mtWarning} mtError, [mbOk], 0);
end;
end;
procedure TNonVisualCtrlsDataModule.DeleteTaskActionExecute(Sender: TObject);
var
Msg: String;
begin
with DatabaseDataModule.TasksSQLQuery do
begin
if IsEmpty then
Exit;
Msg := Format(RSConfirmDeleteTask, [DatabaseDataModule.TasksSQLQuery.FieldByName('name').AsString]);
if MessageDlg(Msg, mtConfirmation, [mbYes, mbNo], 0) = mrYes then
begin
Delete;
ApplyUpdates;
DatabaseDataModule.SQLTransaction1.CommitRetaining;
// Refresh periods list after cascade delete
DatabaseDataModule.PeriodsSQLQuery.Refresh;
end;
end;
end;
procedure TNonVisualCtrlsDataModule.EditPeriodActionExecute(Sender: TObject);
begin
with DatabaseDataModule.PeriodsSQLQuery do
begin
if IsEmpty then
Exit;
Edit;
if PeriodEditForm.ShowModal = mrOK then
begin
FieldByName('is_manually_added').AsBoolean := True;
Post;
ApplyUpdates;
DatabaseDataModule.SQLTransaction1.CommitRetaining;
end
else
Cancel;
end;
end;
procedure TNonVisualCtrlsDataModule.EditTaskActionExecute(Sender: TObject);
begin
with DatabaseDataModule.TasksSQLQuery do
begin
if IsEmpty then
Exit;
Edit;
if TaskEditForm.ShowModal = mrOK then
begin
FieldByName('created').AsDateTime :=
TTask.GetById(FieldByName('id').AsInteger).Created; // Fix for date gets lost
FieldByName('modified').AsDateTime := Now;
Post;
ApplyUpdates;
DatabaseDataModule.SQLTransaction1.CommitRetaining;
end
else
Cancel;
end;
end;
procedure TNonVisualCtrlsDataModule.MarkTaskAsDoneActionExecute(Sender: TObject
);
begin
with DatabaseDataModule.TasksSQLQuery do
begin
// Stop selected task if it is active
if FieldByName('is_active').AsBoolean then
begin
StopTimeTrackingAction.Execute;
end;
Edit;
FieldByName('done').AsBoolean := True;
FieldByName('modified').AsDateTime := Now;
Post;
ApplyUpdates;
end;
DatabaseDataModule.SQLTransaction1.CommitRetaining;
end;
procedure TNonVisualCtrlsDataModule.ShowDoneTasksActionExecute(Sender: TObject);
begin
ShowDoneTasksAction.Checked := not ShowDoneTasksAction.Checked;
DatabaseDataModule.DoneTasksFilter := ShowDoneTasksAction.Checked;
end;
procedure TNonVisualCtrlsDataModule.StopTimeTrackingActionExecute(
Sender: TObject);
begin
TTask.Stop;
DatabaseDataModule.PeriodsSQLQuery.Refresh;
RunningTaskUpdated;
end;
procedure TNonVisualCtrlsDataModule.TrayIconDblClick(Sender: TObject);
begin
MainForm.RestoreFromTray;
end;
procedure TNonVisualCtrlsDataModule.TrayIconMouseMove(Sender: TObject;
Shift: TShiftState; X, Y: Integer);
var
ActivePeriod: TPeriod;
HintText: String;
begin
// Update tray icon hint text
HintText := 'Task Organizer';
ActivePeriod := TPeriod.GetActive;
if Assigned(ActivePeriod) then
begin
HintText := Format('%s - %s (%s)', [HintText, ActivePeriod.GetTask.Name,
DurationToStr(ActivePeriod.Duration)]);
end;
TrayIcon.Hint := HintText;
end;
procedure TNonVisualCtrlsDataModule.UnMarkTaskAsDoneActionExecute(
Sender: TObject);
begin
with DatabaseDataModule.TasksSQLQuery do
begin
Edit;
FieldByName('done').AsBoolean := False;
FieldByName('modified').AsDateTime := Now;
Post;
ApplyUpdates;
end;
DatabaseDataModule.SQLTransaction1.CommitRetaining;
end;
procedure TNonVisualCtrlsDataModule.LoadIconsFromResources;
begin
with Icons do
begin
Clear;
AddResourceName(HINSTANCE, 'START');
AddResourceName(HINSTANCE, 'STOP');
AddResourceName(HINSTANCE, 'TICK');
AddResourceName(HINSTANCE, 'TICK_GRAY');
AddResourceName(HINSTANCE, 'CREATE');
AddResourceName(HINSTANCE, 'DELETE');
AddResourceName(HINSTANCE, 'EDIT');
AddResourceName(HINSTANCE, 'EXIT');
end;
with TrayIcon.Icons do
begin
Clear;
AddResourceName(HINSTANCE, 'CLOCK_ANIMATION_1');
AddResourceName(HINSTANCE, 'CLOCK_ANIMATION_2');
AddResourceName(HINSTANCE, 'CLOCK_ANIMATION_3');
AddResourceName(HINSTANCE, 'CLOCK_ANIMATION_4');
AddResourceName(HINSTANCE, 'CLOCK_ANIMATION_5');
AddResourceName(HINSTANCE, 'CLOCK_ANIMATION_6');
AddResourceName(HINSTANCE, 'CLOCK_ANIMATION_7');
AddResourceName(HINSTANCE, 'CLOCK_ANIMATION_8');
end;
with TrayIcon do
begin
Icon.SetSize(16, 16); // To load icon with correct size
Icon.LoadFromResourceName(HINSTANCE, 'MAINICON');
DefaultIcon.SetSize(16, 16);
DefaultIcon.LoadFromResourceName(HINSTANCE, 'MAINICON');
end;
with LanguageIcons do
begin
Clear;
AddResourceName(HINSTANCE, 'FLAG_EN');
AddResourceName(HINSTANCE, 'FLAG_RU');
end;
end;
procedure TNonVisualCtrlsDataModule.RunningTaskUpdated;
var
IsActive: Boolean;
begin
IsActive := TTask.HasActive;
StartTimeTrackingAction.Enabled:=(not DatabaseDataModule.TasksSQLQuery.FieldByName('done').AsBoolean) and (not IsActive);
StopTimeTrackingAction.Enabled:=IsActive;
if IsActive then
begin
TrayIcon.Animate := True;
end
else
begin
TrayIcon.Animate := False;
TrayIcon.RestoreIcon;
end;
DatabaseDataModule.PeriodsDataSource.DataSet.Last;
end;
{procedure TDataModule1.UniqueInstance1OtherInstance(Sender: TObject;
ParamCount: Integer; const Parameters: array of String);
begin
//MainForm.Show;
//MainForm.BringToFront;
Application.BringToFront;
end; }
end.