-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.pas
executable file
·402 lines (338 loc) · 10.4 KB
/
main.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
unit main;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
StdCtrls, DBGrids, Menus, TasksFrame, DatabaseDM,
PeriodsFrame, NonVisualCtrlsDM, ReportFrm, LocalizedForms;
type
{ TMainForm }
TMainForm = class(TLocalizedForm)
BackupDBMenuItem: TMenuItem;
GoToTaskMenuItem: TMenuItem;
MenuItem1: TMenuItem;
LanguageMenuItem: TMenuItem;
MenuItem2: TMenuItem;
PeriodsFrame1: TPeriodsFrame;
ReportFrame1: TReportFrame;
ShowDoneTasksMenuItem: TMenuItem;
TasksFrame1: TTasksFrame;
ViewMenuItem: TMenuItem;
MainMenu: TMainMenu;
ExportMenuItem: TMenuItem;
ServiceMenuItem: TMenuItem;
LogsMemo: TMemo;
PageControl1: TPageControl;
LogsTabSheet: TTabSheet;
ReportTabSheet: TTabSheet;
TasksTabSheet: TTabSheet;
PeriodsTabSheet: TTabSheet;
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);
procedure FormCreate(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure GoToTaskMenuItemClick(Sender: TObject);
procedure ReportTabSheetShow(Sender: TObject);
procedure TasksTabSheetShow(Sender: TObject);
private
FirstShow: Boolean; // Indicates if FormShow event runs first time
procedure ApplicationMinimize(Sender: TObject);
procedure GoToTaskMenuItemClick2(Sender: TObject); // ToDo: Better name
procedure StoreFormState;
procedure RestoreFormState;
procedure StoreSettings;
procedure RestoreSettings;
procedure FillLanguagesList;
procedure OnLanguageMenuClick(ASender: TObject);
procedure SetLanguage(ALang: String);
function GetLanguage: String;
procedure UpdateTranslation(ALang: String); override;
public
procedure MinimizeToTray;
procedure RestoreFromTray;
property Language: String read GetLanguage write SetLanguage;
end;
var
MainForm: TMainForm;
implementation
uses
Models, StrUtils, TypInfo, LCLTranslator, LazFileUtils
{$IFOPT D+}
,LazLogger
{$Else}
,LazLoggerDummy
{$EndIf}
;
resourcestring
RSRunningTaskNotification = 'You have running task. Are you sure to exit?';
RSLibraryNotFound = 'Library "%s" not found in program directory!';
{$I revision.inc}
{$R *.lfm}
{ TMainForm }
procedure TMainForm.FormCreate(Sender: TObject);
const
SqliteLibName = 'sqlite3.dll';
begin
FillLanguagesList;
Language := '';
FirstShow := True;
if not FileExists(SqliteLibName) then
begin
MessageDlg(Format(RSLibraryNotFound, [SqliteLibName]), mtError, [mbOK], 0);
//Close;
Application.Terminate;
end;
PageControl1.ActivePageIndex:=0;
{$IFOPT D-}
LogsTabSheet.TabVisible := False;
{$EndIf}
//TasksFrame1.RefreshStartStopBtnsVisibility;
Application.OnMinimize := @ApplicationMinimize; // Minimize to tray
end;
procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: boolean);
begin
if TTask.HasActive then
begin
CanClose := MessageDlg(RSRunningTaskNotification,
mtConfirmation, mbYesNo, 0) = mrYes;
end;
end;
procedure TMainForm.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
StoreSettings;
end;
procedure TMainForm.FormShow(Sender: TObject);
begin
NonVisualCtrlsDataModule.RunningTaskUpdated;
if FirstShow then
RestoreSettings;
FirstShow := False;
end;
procedure TMainForm.GoToTaskMenuItemClick(Sender: TObject);
const
Limit = 12;
var
MenuItem: TMenuItem;
Tasks: TTasks;
Task: TTask;
begin
GoToTaskMenuItem.Clear;
Tasks := TTask.GetLastActive(Limit, not DatabaseDataModule.DoneTasksFilter);
for Task in Tasks do
begin
MenuItem := TMenuItem.Create(GoToTaskMenuItem);
MenuItem.Caption := Task.Name;
MenuItem.Name := Format('GoToTask%dMenuItem', [Task.Id]);
MenuItem.OnClick := @GoToTaskMenuItemClick2;
GoToTaskMenuItem.Add(MenuItem);
end;
Tasks.Free;
end;
procedure TMainForm.ReportTabSheetShow(Sender: TObject);
begin
//DatabaseDataModule.StatsSQLQuery.Refresh;
//StatsDBGrid.Refresh;
ReportFrame1.OnShow;
end;
procedure TMainForm.TasksTabSheetShow(Sender: TObject);
begin
//TasksFrame1.RefreshStartStopBtnsVisibility;
end;
procedure TMainForm.ApplicationMinimize(Sender: TObject);
begin
MinimizeToTray;
end;
procedure TMainForm.GoToTaskMenuItemClick2(Sender: TObject);
var
S: String;
TaskId: Integer;
begin
S := (Sender as TMenuItem).Name;
TaskId := StrToInt(S.Substring(8, S.Length - 16));
TasksFrame1.SelectTask(TaskId);
TasksFrame1.TasksDBGrid.SetFocus;
end;
procedure TMainForm.StoreFormState;
begin
with NonVisualCtrlsDataModule.XMLConfig do
begin
SetValue('MainWindow/Left', Left);
SetValue('MainWindow/Top', Top);
SetValue('MainWindow/Width', Width);
SetValue('MainWindow/Height', Height);
SetValue('MainWindow/RestoredLeft', RestoredLeft);
SetValue('MainWindow/RestoredTop', RestoredTop);
SetValue('MainWindow/RestoredWidth', RestoredWidth);
SetValue('MainWindow/RestoredHeight', RestoredHeight);
SetValue('MainWindow/WindowState', GetEnumName(TypeInfo(TWindowState), Ord(WindowState)));
end;
end;
procedure TMainForm.StoreSettings;
begin
StoreFormState;
with NonVisualCtrlsDataModule.XMLConfig do
begin
SetValue('View/ShowDoneTasks', NonVisualCtrlsDataModule.ShowDoneTasksAction.Checked);
SetValue('SelectedTask', TasksFrame1.TasksDBGrid.DBOptions.DataSource.DataSet.FieldByName('id').AsInteger);
SetValue('Language', Language);
end;
end;
procedure TMainForm.RestoreFormState;
var
LastWindowState: TWindowState;
LastWindowStateStr: String;
begin
with NonVisualCtrlsDataModule.XMLConfig do
begin
LastWindowStateStr := GetEnumName(TypeInfo(TWindowState), Ord(WindowState));
LastWindowStateStr := GetValue('MainWindow/WindowState', LastWindowStateStr);
LastWindowState := TWindowState(GetEnumValue(TypeInfo(TWindowState), LastWindowStateStr));;
if LastWindowState = wsMaximized then
begin
WindowState := wsNormal;
BoundsRect := Bounds(
GetValue('MainWindow/RestoredLeft', RestoredLeft),
GetValue('MainWindow/RestoredTop', RestoredTop),
GetValue('MainWindow/RestoredWidth', RestoredWidth),
GetValue('MainWindow/RestoredHeight', RestoredHeight));
WindowState := wsMaximized;
end
else
begin
WindowState := wsNormal;
BoundsRect := Bounds(
GetValue('MainWindow/Left', Left),
GetValue('MainWindow/Top', Top),
GetValue('MainWindow/Width', Width),
GetValue('MainWindow/Height', Height));
end;
end;
end;
procedure TMainForm.RestoreSettings;
begin
RestoreFormState;
with NonVisualCtrlsDataModule.XMLConfig do
begin
// ToDo: Remake this
with NonVisualCtrlsDataModule.ShowDoneTasksAction do
begin
Checked := not GetValue('View/ShowDoneTasks', False);
Execute;
end;
TasksFrame1.TasksDBGrid.DBOptions.DataSource.DataSet.Locate(
'id', GetValue('SelectedTask', -1), []);
//TasksFrame1.SelectTask(GetValue('SelectedTask', -1));
Language := GetValue('Language', '');
end;
end;
procedure TMainForm.FillLanguagesList;
function ExtractLangCodeFromFileName(AFileName: String): String;
var
Idx: Integer;
begin
Result := ExtractFileNameOnly(AFileName);
Idx := WordCount(Result, ['.']);
Result := ExtractWord(Idx, Result, ['.']);
Result := LowerCase(Result);
end;
var
Files, Langs: TStringList;
FileName, LangCode, LangName: String;
MenuItem: TMenuItem;
begin
LanguageMenuItem.Clear;
Files := TStringList.Create;
Langs := TStringList.Create;
try
Langs.Sorted := True;
Langs.Duplicates := dupIgnore;
FindAllFiles(Files, ConcatPaths([ProgramDirectory, 'languages']), '*.po;*.mo', False);
for FileName in Files do
begin
Langs.Add(ExtractLangCodeFromFileName(FileName));
end;
Langs.Add('en');
for LangCode in Langs do
begin
MenuItem := TMenuItem.Create(LanguageMenuItem);
case LangCode of
'en':
begin
LangName := 'English';
MenuItem.ImageIndex := EnFlagIconIdx;
end;
'ru':
begin
LangName := 'Russian (Русский)';
MenuItem.ImageIndex := RuFlagIconIdx;
end
else
begin
LangName := LangCode;
MenuItem.ImageIndex := NoIconIdx;
end;
end;
MenuItem.Caption := LangName;
MenuItem.Name := LangCode + '_LangMenuItem';
MenuItem.OnClick := @OnLanguageMenuClick;
MenuItem.RadioItem := True;
MenuItem.AutoCheck := {True} False;
LanguageMenuItem.Add(MenuItem);
end;
finally
Langs.Free;
Files.Free;
end;
end;
procedure TMainForm.OnLanguageMenuClick(ASender: TObject);
begin
Language := ExtractWord(1, (ASender as TMenuItem).Name, ['_']);
end;
procedure TMainForm.SetLanguage(ALang: String);
var
ResLang: String;
begin
ResLang := SetDefaultLang(ALang);
UpdateTranslation(ResLang);
end;
function TMainForm.GetLanguage: String;
begin
Result := GetDefaultLang;
end;
procedure TMainForm.UpdateTranslation(ALang: String);
var
LangMenuItem: TMenuItem;
begin
inherited;
// Update some labels
if LanguageMenuItem.Caption <> 'Language' then
LanguageMenuItem.Caption := LanguageMenuItem.Caption + ' / Language';
//FillLanguagesList;
Caption := Caption + Format(' %s %s'{$IFOPT D+} + ' [Debug Build]'{$EndIf}, [GitRevisionStr, {$I %DATE%}]);
// Update selected language in main menu
LangMenuItem := (LanguageMenuItem.FindComponent(ALang + '_LangMenuItem') as TMenuItem);
if Assigned(LangMenuItem) then
LangMenuItem.Checked := True;
//ShowMessage('lang changed');
end;
procedure TMainForm.MinimizeToTray;
begin
StoreFormState;
WindowState := wsMinimized;
Hide;
NonVisualCtrlsDataModule.TrayIcon.Show;
end;
procedure TMainForm.RestoreFromTray;
begin
NonVisualCtrlsDataModule.TrayIcon.Hide;
//WindowState := wsNormal;
Show;
RestoreFormState;
end;
initialization
{$IFOPT D+}
DeleteFile('log.txt');
DebugLogger.LogName := 'log.txt';
DebugLn('Logging started');
{$EndIf}
end.