1 | unit Core;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, FileUtil, Controls, ActnList, Forms, Dialogs, ExtCtrls,
|
---|
7 | LastOpenedList, ApplicationInfo, PersistentForm, ScaleDPI, Common,
|
---|
8 | Translator, DataFile, VCard, Menus, RegistryEx, Theme, FormMain,
|
---|
9 | Registry, VCardFile, FormKeyShortcuts;
|
---|
10 |
|
---|
11 | type
|
---|
12 |
|
---|
13 | { TCore }
|
---|
14 |
|
---|
15 | TCore = class(TDataModule)
|
---|
16 | AAbout: TAction;
|
---|
17 | AKeyShortcuts: TAction;
|
---|
18 | AFullScreen: TAction;
|
---|
19 | AFileImport: TAction;
|
---|
20 | AFileExport: TAction;
|
---|
21 | ANormalize: TAction;
|
---|
22 | ARemoveExactDuplicates: TAction;
|
---|
23 | AFileCompare: TAction;
|
---|
24 | AViewSource: TAction;
|
---|
25 | ATest: TAction;
|
---|
26 | AFind: TAction;
|
---|
27 | AFileSplit: TAction;
|
---|
28 | AGenerate: TAction;
|
---|
29 | AFindDuplicate: TAction;
|
---|
30 | AFileCombine: TAction;
|
---|
31 | ASettings: TAction;
|
---|
32 | AFileOpenRecent: TAction;
|
---|
33 | AHomePage: TAction;
|
---|
34 | AFileClose: TAction;
|
---|
35 | AFileSaveAs: TAction;
|
---|
36 | AFileSave: TAction;
|
---|
37 | AFileOpen: TAction;
|
---|
38 | AFileNew: TAction;
|
---|
39 | AExit: TAction;
|
---|
40 | ActionList1: TActionList;
|
---|
41 | ApplicationInfo1: TApplicationInfo;
|
---|
42 | SelectDirectoryDialog1: TSelectDirectoryDialog;
|
---|
43 | Translator: TTranslator;
|
---|
44 | ImageList1: TImageList;
|
---|
45 | LastOpenedList1: TLastOpenedList;
|
---|
46 | OpenDialog1: TOpenDialog;
|
---|
47 | PersistentForm1: TPersistentForm;
|
---|
48 | SaveDialog1: TSaveDialog;
|
---|
49 | ScaleDPI1: TScaleDPI;
|
---|
50 | ThemeManager1: TThemeManager;
|
---|
51 | procedure AAboutExecute(Sender: TObject);
|
---|
52 | procedure AExitExecute(Sender: TObject);
|
---|
53 | procedure AFileCombineExecute(Sender: TObject);
|
---|
54 | procedure AFileCompareExecute(Sender: TObject);
|
---|
55 | procedure AFileExportExecute(Sender: TObject);
|
---|
56 | procedure AFileImportExecute(Sender: TObject);
|
---|
57 | procedure AFileNewExecute(Sender: TObject);
|
---|
58 | procedure AFileOpenExecute(Sender: TObject);
|
---|
59 | procedure AFileOpenRecentExecute(Sender: TObject);
|
---|
60 | procedure AFileSaveExecute(Sender: TObject);
|
---|
61 | procedure AFileSaveAsExecute(Sender: TObject);
|
---|
62 | procedure AFileCloseExecute(Sender: TObject);
|
---|
63 | procedure AFileSplitExecute(Sender: TObject);
|
---|
64 | procedure AFindDuplicateExecute(Sender: TObject);
|
---|
65 | procedure AFindExecute(Sender: TObject);
|
---|
66 | procedure AFullScreenExecute(Sender: TObject);
|
---|
67 | procedure AGenerateExecute(Sender: TObject);
|
---|
68 | procedure AHomePageExecute(Sender: TObject);
|
---|
69 | procedure AKeyShortcutsExecute(Sender: TObject);
|
---|
70 | procedure ANormalizeExecute(Sender: TObject);
|
---|
71 | procedure ARemoveExactDuplicatesExecute(Sender: TObject);
|
---|
72 | procedure ASettingsExecute(Sender: TObject);
|
---|
73 | procedure ATestExecute(Sender: TObject);
|
---|
74 | procedure AViewSourceExecute(Sender: TObject);
|
---|
75 | procedure DataModuleCreate(Sender: TObject);
|
---|
76 | procedure DataModuleDestroy(Sender: TObject);
|
---|
77 | procedure LastOpenedList1Change(Sender: TObject);
|
---|
78 | procedure TranslatorTranslate(Sender: TObject);
|
---|
79 | private
|
---|
80 | FOnDataFileChange: TNotifyEvent;
|
---|
81 | FOnLastOpenedListChange: TNotifyEvent;
|
---|
82 | InitializeStarted: Boolean;
|
---|
83 | InitializeFinished: Boolean;
|
---|
84 | LoadErrors: string;
|
---|
85 | ProfileImage: TImage;
|
---|
86 | LastSplitDir: string;
|
---|
87 | ProfilePhotoFileName: string;
|
---|
88 | RecentFileRegistryContext: TRegistryContext;
|
---|
89 | FormMain: TFormMain;
|
---|
90 | FormKeyShortcuts: TFormKeyShortcuts;
|
---|
91 | procedure FileModified(Sender: TObject);
|
---|
92 | function FindFirstNonOption: string;
|
---|
93 | procedure LoadConfig;
|
---|
94 | procedure SaveConfig;
|
---|
95 | procedure DoError(Text: string; Line: Integer);
|
---|
96 | procedure AddItemToLastOpenedList(FileName: string);
|
---|
97 | procedure DoDataFileChange;
|
---|
98 | function GetDefaultCompareTool: string;
|
---|
99 | public
|
---|
100 | DefaultDataFileClass: TDataFileClass;
|
---|
101 | DataFile: TDataFile;
|
---|
102 | FileClosed: Boolean;
|
---|
103 | ReopenLastFileOnStart: Boolean;
|
---|
104 | LastContactTabIndex: Integer;
|
---|
105 | LastContactFileName: string;
|
---|
106 | LastPhotoFileName: string;
|
---|
107 | LastQrCodeFileName: string;
|
---|
108 | LastPropertyValueFileName: string;
|
---|
109 | MapUrl: string;
|
---|
110 | GenerateCount: Integer;
|
---|
111 | ToolbarVisible: Boolean;
|
---|
112 | DefaultVcardVersion: string;
|
---|
113 | DefaultPhoneCountryCode: string;
|
---|
114 | DefaultInternationalCallPrefix: string;
|
---|
115 | CompareTool: string;
|
---|
116 | procedure UpdateFile;
|
---|
117 | function GetProfileImage: TImage;
|
---|
118 | function GetTempDir: string;
|
---|
119 | procedure FileNew;
|
---|
120 | procedure FileOpen(FileName: string);
|
---|
121 | procedure FileClose;
|
---|
122 | procedure Initialize;
|
---|
123 | procedure UpdateInterface;
|
---|
124 | property OnDataFileChange: TNotifyEvent read FOnDataFileChange
|
---|
125 | write FOnDataFileChange;
|
---|
126 | property OnLastOpenedListChange: TNotifyEvent read FOnLastOpenedListChange
|
---|
127 | write FOnLastOpenedListChange;
|
---|
128 | end;
|
---|
129 |
|
---|
130 | var
|
---|
131 | Core: TCore;
|
---|
132 |
|
---|
133 |
|
---|
134 | implementation
|
---|
135 |
|
---|
136 | {$R *.lfm}
|
---|
137 |
|
---|
138 | uses
|
---|
139 | FormSettings, FormFindDuplicity, FormCompare, TestCase, FormNormalize,
|
---|
140 | FormGenerate, FormError, FormFind, FormSource, TestCases, FormExport, FormEx,
|
---|
141 | FormAbout, FormTests, FormImport;
|
---|
142 |
|
---|
143 | resourcestring
|
---|
144 | SAppExit = 'Application exit';
|
---|
145 | SAppExitQuery = 'File was modified. Do you want to save it before exit?';
|
---|
146 | SFileSplit = 'Contacts split';
|
---|
147 | SFileSplitFinishedOpenDirectory = 'Total %d contact files saved. Do you want to open the directory %s?';
|
---|
148 | SFileNotFound = 'File ''%s'' not found.';
|
---|
149 | SCombinedContacts = 'Combined %d contact files.';
|
---|
150 | SLine = 'Line %d: %s';
|
---|
151 | SRemovedDuplicates = 'Removed %d duplicates.';
|
---|
152 |
|
---|
153 | { TCore }
|
---|
154 |
|
---|
155 | procedure TCore.AExitExecute(Sender: TObject);
|
---|
156 | begin
|
---|
157 | FormMain.Close;
|
---|
158 | Application.Terminate;
|
---|
159 | end;
|
---|
160 |
|
---|
161 | procedure TCore.AFileCombineExecute(Sender: TObject);
|
---|
162 | var
|
---|
163 | TempFile: TDataFile;
|
---|
164 | I: Integer;
|
---|
165 | LoadedFiles: Integer;
|
---|
166 | begin
|
---|
167 | TempFile := DefaultDataFileClass.Create(nil);
|
---|
168 | try
|
---|
169 | OpenDialog1.Filter := TempFile.GetFileFilter;
|
---|
170 | finally
|
---|
171 | TempFile.Free;
|
---|
172 | end;
|
---|
173 | OpenDialog1.DefaultExt := '';
|
---|
174 | if Assigned(DataFile) then begin
|
---|
175 | OpenDialog1.InitialDir := ExtractFileDir(DataFile.FileName);
|
---|
176 | OpenDialog1.FileName := ExtractFileName(DataFile.FileName);
|
---|
177 | end;
|
---|
178 | OpenDialog1.Options := OpenDialog1.Options + [ofAllowMultiSelect];
|
---|
179 | if OpenDialog1.Execute then begin
|
---|
180 | LoadedFiles := 0;
|
---|
181 | for I := 0 to OpenDialog1.Files.Count - 1 do begin
|
---|
182 | if FileExists(OpenDialog1.Files[I]) then begin
|
---|
183 | TempFile := TVCardFile.Create(nil);
|
---|
184 | try
|
---|
185 | TempFile.LoadFromFile(OpenDialog1.Files[I]);
|
---|
186 | TVCardFile(DataFile).VCard.Contacts.AddContacts(TVCardFile(TempFile).VCard.Contacts);
|
---|
187 | Inc(LoadedFiles);
|
---|
188 | finally
|
---|
189 | TempFile.Free;
|
---|
190 | end;
|
---|
191 | end;
|
---|
192 | end;
|
---|
193 | if LoadedFiles > 0 then TVCardFile(DataFile).Modified := True;
|
---|
194 | ShowMessage(Format(SCombinedContacts, [LoadedFiles]));
|
---|
195 | UpdateFile;
|
---|
196 | end;
|
---|
197 | end;
|
---|
198 |
|
---|
199 | procedure TCore.AFileCompareExecute(Sender: TObject);
|
---|
200 | var
|
---|
201 | FormCompare: TFormCompare;
|
---|
202 | begin
|
---|
203 | FormCompare := TFormCompare.Create(nil);
|
---|
204 | try
|
---|
205 | FormCompare.ShowModal;
|
---|
206 | finally
|
---|
207 | FreeAndNil(FormCompare);
|
---|
208 | end;
|
---|
209 | end;
|
---|
210 |
|
---|
211 | procedure TCore.AFileExportExecute(Sender: TObject);
|
---|
212 | var
|
---|
213 | FormExport: TFormExport;
|
---|
214 | begin
|
---|
215 | FormExport := TFormExport.Create(nil);
|
---|
216 | try
|
---|
217 | FormExport.ShowModal;
|
---|
218 | finally
|
---|
219 | FormExport.Free;
|
---|
220 | end;
|
---|
221 | end;
|
---|
222 |
|
---|
223 | procedure TCore.AFileImportExecute(Sender: TObject);
|
---|
224 | var
|
---|
225 | FormImport: TFormImport;
|
---|
226 | begin
|
---|
227 | FormImport := TFormImport.Create(nil);
|
---|
228 | try
|
---|
229 | if FormImport.ShowModal = mrOK then begin
|
---|
230 | TVCardFile(DataFile).Modified := True;
|
---|
231 | UpdateFile;
|
---|
232 | end;
|
---|
233 | finally
|
---|
234 | FormImport.Free;
|
---|
235 | end;
|
---|
236 | end;
|
---|
237 |
|
---|
238 | procedure TCore.AAboutExecute(Sender: TObject);
|
---|
239 | var
|
---|
240 | FormAbout: TFormAbout;
|
---|
241 | begin
|
---|
242 | FormAbout := TFormAbout.Create(nil);
|
---|
243 | FormAbout.ApplicationInfo := ApplicationInfo1;
|
---|
244 | FormAbout.ShowModal;
|
---|
245 | FreeAndNil(FormAbout);
|
---|
246 | end;
|
---|
247 |
|
---|
248 | procedure TCore.AFileCloseExecute(Sender: TObject);
|
---|
249 | begin
|
---|
250 | FileClose;
|
---|
251 | UpdateFile;
|
---|
252 | end;
|
---|
253 |
|
---|
254 | procedure TCore.AFileSplitExecute(Sender: TObject);
|
---|
255 | var
|
---|
256 | I: Integer;
|
---|
257 | C: Integer;
|
---|
258 | FileName: string;
|
---|
259 | ModalResult: TModalResult;
|
---|
260 | begin
|
---|
261 | C := 0;
|
---|
262 | SelectDirectoryDialog1.FileName := LastSplitDir;
|
---|
263 | if SelectDirectoryDialog1.Execute then begin
|
---|
264 | LastSplitDir := SelectDirectoryDialog1.FileName;
|
---|
265 | with TVCardFile(DataFile).VCard.Contacts do
|
---|
266 | for I := 0 to Count - 1 do begin
|
---|
267 | if Items[I].Fields[cfFullName] <> '' then begin
|
---|
268 | FileName := SelectDirectoryDialog1.FileName + DirectorySeparator +
|
---|
269 | Items[I].FullNameToFileName + VCardFileExt;
|
---|
270 | Items[I].SaveToFile(FileName);
|
---|
271 | Inc(C);
|
---|
272 | end;
|
---|
273 | end;
|
---|
274 | ModalResult := MessageDlg(SFileSplit,
|
---|
275 | Format(SFileSplitFinishedOpenDirectory, [C,
|
---|
276 | SelectDirectoryDialog1.FileName]), mtConfirmation, [mbYes, mbNo], 0);
|
---|
277 | if ModalResult = mrYes then begin
|
---|
278 | {$IFDEF WINDOWS}
|
---|
279 | ExecuteProgram('explorer.exe', ['"' + SelectDirectoryDialog1.FileName + '"'], []);
|
---|
280 | {$ENDIF}
|
---|
281 | {$IFDEF UNIX}
|
---|
282 | ExecuteProgram('/usr/bin/xdg-open', [SelectDirectoryDialog1.FileName], []);
|
---|
283 | {$ENDIF}
|
---|
284 | end;
|
---|
285 | end;
|
---|
286 | end;
|
---|
287 |
|
---|
288 | procedure TCore.AFindDuplicateExecute(Sender: TObject);
|
---|
289 | begin
|
---|
290 | with TFormFindDuplicity.Create(nil) do
|
---|
291 | try
|
---|
292 | Contacts := TVCardFile(DataFile).VCard.Contacts;
|
---|
293 | ShowModal;
|
---|
294 | DoDataFileChange;
|
---|
295 | finally
|
---|
296 | Free;
|
---|
297 | end;
|
---|
298 | end;
|
---|
299 |
|
---|
300 | procedure TCore.AFindExecute(Sender: TObject);
|
---|
301 | begin
|
---|
302 | with TFormFind.Create(nil) do
|
---|
303 | try
|
---|
304 | Contacts := TVCardFile(DataFile).VCard.Contacts;
|
---|
305 | ShowModal;
|
---|
306 | DoDataFileChange;
|
---|
307 | finally
|
---|
308 | Free;
|
---|
309 | end;
|
---|
310 | end;
|
---|
311 |
|
---|
312 | procedure TCore.AFullScreenExecute(Sender: TObject);
|
---|
313 | begin
|
---|
314 | FormMain.ToggleFullScreen;
|
---|
315 | end;
|
---|
316 |
|
---|
317 | procedure TCore.AGenerateExecute(Sender: TObject);
|
---|
318 | begin
|
---|
319 | with TFormGenerate.Create(nil) do
|
---|
320 | try
|
---|
321 | Contacts := TVCardFile(DataFile).VCard.Contacts;
|
---|
322 | if ShowModal = mrOk then begin
|
---|
323 | DataFile.Modified := True;
|
---|
324 | DoDataFileChange;
|
---|
325 | end;
|
---|
326 | finally
|
---|
327 | Free;
|
---|
328 | end;
|
---|
329 | end;
|
---|
330 |
|
---|
331 | procedure TCore.AHomePageExecute(Sender: TObject);
|
---|
332 | begin
|
---|
333 | OpenWebPage(ApplicationInfo1.HomePage);
|
---|
334 | end;
|
---|
335 |
|
---|
336 | procedure TCore.AKeyShortcutsExecute(Sender: TObject);
|
---|
337 | begin
|
---|
338 | if not Assigned(FormKeyShortcuts) then
|
---|
339 | FormKeyShortcuts := TFormKeyShortcuts.Create(nil);
|
---|
340 | FormKeyShortcuts.ImageList := ImageList1;
|
---|
341 | FormKeyShortcuts.MainForm := FormMain;
|
---|
342 | FormKeyShortcuts.SourceComponents.Clear;
|
---|
343 | FormKeyShortcuts.SourceComponents.Add(Self);
|
---|
344 | FormKeyShortcuts.Show;
|
---|
345 | end;
|
---|
346 |
|
---|
347 | procedure TCore.ANormalizeExecute(Sender: TObject);
|
---|
348 | var
|
---|
349 | FormNormalize: TFormNormalize;
|
---|
350 | begin
|
---|
351 | FormNormalize := TFormNormalize.Create(nil);
|
---|
352 | try
|
---|
353 | FormNormalize.ShowModal;
|
---|
354 | finally
|
---|
355 | FreeAndNil(FormNormalize);
|
---|
356 | end;
|
---|
357 | end;
|
---|
358 |
|
---|
359 | procedure TCore.ARemoveExactDuplicatesExecute(Sender: TObject);
|
---|
360 | var
|
---|
361 | RemovedCount: Integer;
|
---|
362 | begin
|
---|
363 | RemovedCount := TVCardFile(DataFile).VCard.Contacts.RemoveExactDuplicates;
|
---|
364 | ShowMessage(Format(SRemovedDuplicates, [RemovedCount]));
|
---|
365 | if RemovedCount > 0 then begin
|
---|
366 | DataFile.Modified := True;
|
---|
367 | UpdateFile;
|
---|
368 | end;
|
---|
369 | end;
|
---|
370 |
|
---|
371 | procedure TCore.ASettingsExecute(Sender: TObject);
|
---|
372 | begin
|
---|
373 | with TFormSettings.Create(nil) do
|
---|
374 | try
|
---|
375 | LoadData;
|
---|
376 | if ShowModal = mrOK then begin
|
---|
377 | SaveData;
|
---|
378 | FormMain.SettingsChanged;
|
---|
379 | end;
|
---|
380 | finally
|
---|
381 | Free;
|
---|
382 | end;
|
---|
383 | end;
|
---|
384 |
|
---|
385 | procedure TCore.ATestExecute(Sender: TObject);
|
---|
386 | begin
|
---|
387 | with TFormTests.Create(nil) do
|
---|
388 | try
|
---|
389 | TestCases := InitTestCases;
|
---|
390 | try
|
---|
391 | ShowModal;
|
---|
392 | finally
|
---|
393 | TestCases.Free;
|
---|
394 | end;
|
---|
395 | finally
|
---|
396 | Free;
|
---|
397 | end;
|
---|
398 | end;
|
---|
399 |
|
---|
400 | procedure TCore.AViewSourceExecute(Sender: TObject);
|
---|
401 | begin
|
---|
402 | with TFormSource.Create(nil) do
|
---|
403 | try
|
---|
404 | Source := TVCardFile(DataFile).VCard.AsString;
|
---|
405 | if ShowModal = mrOk then begin
|
---|
406 | TVCardFile(DataFile).VCard.AsString := Source;
|
---|
407 | UpdateFile;
|
---|
408 | end;
|
---|
409 | finally
|
---|
410 | Free;
|
---|
411 | end;
|
---|
412 | end;
|
---|
413 |
|
---|
414 | procedure TCore.AFileNewExecute(Sender: TObject);
|
---|
415 | begin
|
---|
416 | FileNew;
|
---|
417 | UpdateFile;
|
---|
418 | end;
|
---|
419 |
|
---|
420 | procedure TCore.AFileOpenExecute(Sender: TObject);
|
---|
421 | var
|
---|
422 | TempFile: TDataFile;
|
---|
423 | begin
|
---|
424 | TempFile := DefaultDataFileClass.Create(nil);
|
---|
425 | try
|
---|
426 | OpenDialog1.Filter := TempFile.GetFileFilter;
|
---|
427 | finally
|
---|
428 | TempFile.Free;
|
---|
429 | end;
|
---|
430 | OpenDialog1.DefaultExt := '';
|
---|
431 | if Assigned(DataFile) then begin
|
---|
432 | OpenDialog1.InitialDir := ExtractFileDir(DataFile.FileName);
|
---|
433 | OpenDialog1.FileName := ExtractFileName(DataFile.FileName);
|
---|
434 | end;
|
---|
435 | OpenDialog1.Options := OpenDialog1.Options - [ofAllowMultiSelect];
|
---|
436 | if OpenDialog1.Execute then begin
|
---|
437 | FileOpen(OpenDialog1.FileName);
|
---|
438 | UpdateFile;
|
---|
439 | end;
|
---|
440 | end;
|
---|
441 |
|
---|
442 | procedure TCore.AFileOpenRecentExecute(Sender: TObject);
|
---|
443 | begin
|
---|
444 | FileOpen(TMenuItem(Sender).Caption);
|
---|
445 | UpdateFile;
|
---|
446 | end;
|
---|
447 |
|
---|
448 | procedure TCore.AFileSaveAsExecute(Sender: TObject);
|
---|
449 | begin
|
---|
450 | SaveDialog1.DefaultExt := DataFile.GetFileExt;
|
---|
451 | SaveDialog1.Filter := DataFile.GetFileFilter;
|
---|
452 | SaveDialog1.InitialDir := ExtractFileDir(DataFile.FileName);
|
---|
453 | SaveDialog1.FileName := ExtractFileName(DataFile.FileName);
|
---|
454 | if SaveDialog1.Execute then begin
|
---|
455 | DataFile.SaveToFile(SaveDialog1.FileName);
|
---|
456 | AddItemToLastOpenedList(SaveDialog1.FileName);
|
---|
457 | UpdateFile;
|
---|
458 | end;
|
---|
459 | end;
|
---|
460 |
|
---|
461 | procedure TCore.AFileSaveExecute(Sender: TObject);
|
---|
462 | begin
|
---|
463 | if FileExists(DataFile.FileName) then begin
|
---|
464 | DataFile.SaveToFile(DataFile.FileName);
|
---|
465 | AddItemToLastOpenedList(DataFile.FileName);
|
---|
466 | UpdateFile;
|
---|
467 | end else AFileSaveAs.Execute;
|
---|
468 | end;
|
---|
469 |
|
---|
470 | procedure TCore.DataModuleCreate(Sender: TObject);
|
---|
471 | {$IFDEF UNIX}
|
---|
472 | const
|
---|
473 | LinuxDataFilesDir = '/usr/share/vCardStudio';
|
---|
474 | LinuxLanguagesDir = LinuxDataFilesDir + '/Languages';
|
---|
475 | LinuxImagesDir = LinuxDataFilesDir + '/Images';
|
---|
476 | {$ENDIF}
|
---|
477 | begin
|
---|
478 | ProfilePhotoFileName := 'Images/Profile.png';
|
---|
479 | {$IFDEF UNIX}
|
---|
480 | // If installed in Linux system then use installation directory for po files
|
---|
481 | if not DirectoryExists(Translator.POFilesFolder) and DirectoryExists(LinuxLanguagesDir) then begin
|
---|
482 | Translator.POFilesFolder := LinuxLanguagesDir;
|
---|
483 | end;
|
---|
484 | // If installed in Linux system then use installation directory for images files
|
---|
485 | if not DirectoryExists('Images') and DirectoryExists(LinuxImagesDir) then begin
|
---|
486 | ProfilePhotoFileName := LinuxImagesDir + DirectorySeparator + 'Profile.png';
|
---|
487 | end;
|
---|
488 | {$ENDIF}
|
---|
489 |
|
---|
490 | DataFile := nil;
|
---|
491 | DefaultDataFileClass := TVCardFile;
|
---|
492 | FileClosed := True;
|
---|
493 | RecentFileRegistryContext := TRegistryContext.Create(ApplicationInfo1.RegistryRoot,
|
---|
494 | ApplicationInfo1.RegistryKey + '\RecentFiles');
|
---|
495 | LoadConfig;
|
---|
496 |
|
---|
497 | TFormEx.ScaleDPI := ScaleDPI1;
|
---|
498 | TFormEx.Translator := Translator;
|
---|
499 | TFormEx.ThemeManager := ThemeManager1;
|
---|
500 | TFormEx.PersistentForm := PersistentForm1;
|
---|
501 |
|
---|
502 | Application.CreateForm(TFormMain, FormMain);
|
---|
503 | FormMain.Show;
|
---|
504 | end;
|
---|
505 |
|
---|
506 | procedure TCore.DataModuleDestroy(Sender: TObject);
|
---|
507 | begin
|
---|
508 | FileClose;
|
---|
509 | SaveConfig;
|
---|
510 | if Assigned(ProfileImage) then
|
---|
511 | FreeAndNil(ProfileImage);
|
---|
512 | if Assigned(FormKeyShortcuts) then
|
---|
513 | FreeAndNil(FormKeyShortcuts);
|
---|
514 | end;
|
---|
515 |
|
---|
516 | procedure TCore.LastOpenedList1Change(Sender: TObject);
|
---|
517 | begin
|
---|
518 | if Assigned(FOnLastOpenedListChange) then
|
---|
519 | FOnLastOpenedListChange(Self);
|
---|
520 | end;
|
---|
521 |
|
---|
522 | procedure TCore.TranslatorTranslate(Sender: TObject);
|
---|
523 | begin
|
---|
524 | TestCase.Translate;
|
---|
525 | end;
|
---|
526 |
|
---|
527 | procedure TCore.FileModified(Sender: TObject);
|
---|
528 | begin
|
---|
529 | UpdateFile;
|
---|
530 | end;
|
---|
531 |
|
---|
532 | procedure TCore.FileOpen(FileName: string);
|
---|
533 | var
|
---|
534 | FormError: TFormError;
|
---|
535 | begin
|
---|
536 | if FileExists(FileName) then begin
|
---|
537 | FileClose;
|
---|
538 | if FileClosed then begin
|
---|
539 | FileNew;
|
---|
540 | LoadErrors := '';
|
---|
541 | DataFile.LoadFromFile(FileName);
|
---|
542 | AddItemToLastOpenedList(FileName);
|
---|
543 | if LoadErrors <> '' then begin
|
---|
544 | FormError := TFormError.Create(nil);
|
---|
545 | FormError.MemoErrors.Text := LoadErrors;
|
---|
546 | FormError.ShowModal;
|
---|
547 | FreeAndNil(FormError);
|
---|
548 | end;
|
---|
549 | end;
|
---|
550 | end else ShowMessage(Format(SFileNotFound, [FileName]));
|
---|
551 | end;
|
---|
552 |
|
---|
553 | procedure TCore.FileClose;
|
---|
554 | var
|
---|
555 | ModalResult: TModalResult;
|
---|
556 | DoClose: Boolean;
|
---|
557 | begin
|
---|
558 | DoClose := False;
|
---|
559 | if Assigned(DataFile) then begin
|
---|
560 | if DataFile.Modified then begin
|
---|
561 | ModalResult := MessageDlg(SAppExit, SAppExitQuery,
|
---|
562 | mtConfirmation, [mbYes, mbNo, mbCancel], 0);
|
---|
563 | if ModalResult = mrYes then begin
|
---|
564 | AFileSave.Execute;
|
---|
565 | DoClose := True;
|
---|
566 | end
|
---|
567 | else if ModalResult = mrNo then begin
|
---|
568 | DoClose := True;
|
---|
569 | end else FileClosed := False;
|
---|
570 | end else DoClose := True;
|
---|
571 | end else DoClose := True;
|
---|
572 | if DoClose then begin
|
---|
573 | if Assigned(DataFile) then FreeAndNil(DataFile);
|
---|
574 | FileClosed := True;
|
---|
575 | UpdateFile;
|
---|
576 | end;
|
---|
577 | end;
|
---|
578 |
|
---|
579 | procedure TCore.FileNew;
|
---|
580 | begin
|
---|
581 | FileClose;
|
---|
582 | if FileClosed then begin
|
---|
583 | DataFile := DefaultDataFileClass.Create(nil);
|
---|
584 | DataFile.OnModify := FileModified;
|
---|
585 | TVCardFile(DataFile).VCard.OnError := DoError;
|
---|
586 | end;
|
---|
587 | end;
|
---|
588 |
|
---|
589 | procedure TCore.UpdateFile;
|
---|
590 | begin
|
---|
591 | UpdateInterface;
|
---|
592 | DoDataFileChange;
|
---|
593 | end;
|
---|
594 |
|
---|
595 | procedure TCore.LoadConfig;
|
---|
596 | begin
|
---|
597 | PersistentForm1.RegistryContext := ApplicationInfo1.GetRegistryContext;
|
---|
598 | LastOpenedList1.LoadFromRegistry(RecentFileRegistryContext);
|
---|
599 |
|
---|
600 | with TRegistryEx.Create do
|
---|
601 | try
|
---|
602 | CurrentContext := ApplicationInfo1.GetRegistryContext;
|
---|
603 | if ValueExists('LanguageCode') then
|
---|
604 | Translator.Language := Translator.Languages.SearchByCode(ReadStringWithDefault('LanguageCode', ''))
|
---|
605 | else Translator.Language := Translator.Languages.SearchByCode('');
|
---|
606 | if ValueExists('Theme') then
|
---|
607 | ThemeManager1.Theme := ThemeManager1.Themes.FindByName(ReadStringWithDefault('Theme', 'System'))
|
---|
608 | else ThemeManager1.Theme := ThemeManager1.Themes.FindByName('System');
|
---|
609 | ReopenLastFileOnStart := ReadBoolWithDefault('ReopenLastFileOnStart', True);
|
---|
610 | LastContactTabIndex := ReadIntegerWithDefault('LastContactTabIndex', 0);
|
---|
611 | LastContactFileName := ReadStringWithDefault('LastContactFileName', '');
|
---|
612 | LastSplitDir := ReadStringWithDefault('LastSplitDir', '');
|
---|
613 | LastPropertyValueFileName := ReadStringWithDefault('LastPropertyValueFileName', '');
|
---|
614 | GenerateCount := ReadIntegerWithDefault('GenerateCount', 1);
|
---|
615 | DefaultVcardVersion := ReadStringWithDefault('DefaultVcardVersion', '2.1');
|
---|
616 | MapUrl := ReadStringWithDefault('MapUrl', 'https://www.openstreetmap.org/search?query=');
|
---|
617 | LastPhotoFileName := ReadStringWithDefault('LastPhotoFileName', '');
|
---|
618 | LastQrCodeFileName := ReadStringWithDefault('LastQrCodeFileName', '');
|
---|
619 | CompareTool := ReadStringWithDefault('CompareTool', GetDefaultCompareTool);
|
---|
620 | DefaultPhoneCountryCode := ReadStringWithDefault('DefaultPhoneCountryPrefix', DefaultPhoneCountryCode);
|
---|
621 | DefaultInternationalCallPrefix := ReadStringWithDefault('DefaultInternationalCallPrefix', DefaultInternationalCallPrefix);
|
---|
622 | finally
|
---|
623 | Free;
|
---|
624 | end;
|
---|
625 | end;
|
---|
626 |
|
---|
627 | procedure TCore.SaveConfig;
|
---|
628 | begin
|
---|
629 | with TRegistryEx.Create do
|
---|
630 | try
|
---|
631 | CurrentContext := ApplicationInfo1.GetRegistryContext;
|
---|
632 | if Assigned(Translator.Language) and (Translator.Language.Code <> '') then
|
---|
633 | WriteString('LanguageCode', Translator.Language.Code)
|
---|
634 | else DeleteValue('LanguageCode');
|
---|
635 | if Assigned(ThemeManager1.Theme) and (ThemeManager1.Theme.Name <> '') then
|
---|
636 | WriteString('Theme', ThemeManager1.Theme.Name)
|
---|
637 | else DeleteValue('Theme');
|
---|
638 | WriteBool('ReopenLastFileOnStart', ReopenLastFileOnStart);
|
---|
639 | WriteInteger('LastContactTabIndex', LastContactTabIndex);
|
---|
640 | WriteString('LastContactFileName', LastContactFileName);
|
---|
641 | WriteString('LastSplitDir', LastSplitDir);
|
---|
642 | WriteString('LastPropertyValueFileName', LastPropertyValueFileName);
|
---|
643 | WriteInteger('GenerateCount', GenerateCount);
|
---|
644 | WriteString('DefaultVcardVersion', DefaultVcardVersion);
|
---|
645 | WriteString('MapUrl', MapUrl);
|
---|
646 | WriteString('LastPhotoFileName', LastPhotoFileName);
|
---|
647 | WriteString('LastQrCodeFileName', LastQrCodeFileName);
|
---|
648 | WriteString('CompareTool', CompareTool);
|
---|
649 | WriteString('DefaultPhoneCountryPrefix', DefaultPhoneCountryCode);
|
---|
650 | WriteString('DefaultInternationalCallPrefix', DefaultInternationalCallPrefix);
|
---|
651 | finally
|
---|
652 | Free;
|
---|
653 | end;
|
---|
654 | end;
|
---|
655 |
|
---|
656 | procedure TCore.DoError(Text: string; Line: Integer);
|
---|
657 | begin
|
---|
658 | LoadErrors := LoadErrors + Format(SLine, [Line, Text]) + LineEnding;
|
---|
659 | end;
|
---|
660 |
|
---|
661 | procedure TCore.AddItemToLastOpenedList(FileName: string);
|
---|
662 | begin
|
---|
663 | with LastOpenedList1 do begin
|
---|
664 | LoadFromRegistry(RecentFileRegistryContext);
|
---|
665 | AddItem(FileName);
|
---|
666 | SaveToRegistry(RecentFileRegistryContext);
|
---|
667 | end;
|
---|
668 | end;
|
---|
669 |
|
---|
670 | procedure TCore.DoDataFileChange;
|
---|
671 | begin
|
---|
672 | if Assigned(FOnDataFileChange) then
|
---|
673 | FOnDataFileChange(Self);
|
---|
674 | end;
|
---|
675 |
|
---|
676 | function TCore.GetDefaultCompareTool: string;
|
---|
677 | const
|
---|
678 | KDiff3 = '/usr/bin/kdiff3';
|
---|
679 | Kompare = '/usr/bin/kompare';
|
---|
680 | Meld = '/usr/bin/meld';
|
---|
681 | Diffuse = '/usr/bin/diffuse';
|
---|
682 | BeyondCompareLin = '/usr/bin/bcompare';
|
---|
683 | BeyondCompareWin3 = 'c:\Program Files\Beyond Compare 3\BCompare.exe';
|
---|
684 | BeyondCompareWin4 = 'c:\Program Files\Beyond Compare 4\BCompare.exe';
|
---|
685 | BeyondCompareWin5 = 'c:\Program Files\Beyond Compare 5\BCompare.exe';
|
---|
686 | begin
|
---|
687 | if FileExists(KDiff3) then Result := KDiff3
|
---|
688 | else if FileExists(Meld) then Result := Meld
|
---|
689 | else if FileExists(Kompare) then Result := Kompare
|
---|
690 | else if FileExists(Diffuse) then Result := Diffuse
|
---|
691 | else if FileExists(BeyondCompareLin) then Result := BeyondCompareLin
|
---|
692 | else if FileExists(BeyondCompareWin5) then Result := BeyondCompareWin5
|
---|
693 | else if FileExists(BeyondCompareWin4) then Result := BeyondCompareWin4
|
---|
694 | else if FileExists(BeyondCompareWin3) then Result := BeyondCompareWin3
|
---|
695 | else Result := '';
|
---|
696 | end;
|
---|
697 |
|
---|
698 | function TCore.GetProfileImage: TImage;
|
---|
699 | begin
|
---|
700 | if not Assigned(ProfileImage) then begin
|
---|
701 | ProfileImage := TImage.Create(nil);
|
---|
702 | if FileExists(ProfilePhotoFileName) then
|
---|
703 | ProfileImage.Picture.LoadFromFile(ProfilePhotoFileName);
|
---|
704 | end;
|
---|
705 | Result := ProfileImage;
|
---|
706 | end;
|
---|
707 |
|
---|
708 | function TCore.GetTempDir: string;
|
---|
709 | begin
|
---|
710 | Result := SysUtils.GetTempDir + Application.Title;
|
---|
711 | end;
|
---|
712 |
|
---|
713 | procedure TCore.UpdateInterface;
|
---|
714 | begin
|
---|
715 | AFileSave.Enabled := Assigned(DataFile) and DataFile.Modified;
|
---|
716 | AFileSaveAs.Enabled := Assigned(DataFile);
|
---|
717 | AFileClose.Enabled := Assigned(DataFile);
|
---|
718 | AFileSplit.Enabled := Assigned(DataFile);
|
---|
719 | AFileCombine.Enabled := Assigned(DataFile);
|
---|
720 | AFileCompare.Enabled := Assigned(DataFile);
|
---|
721 | AFind.Enabled := Assigned(DataFile);
|
---|
722 | AFindDuplicate.Enabled := Assigned(DataFile);
|
---|
723 | AGenerate.Enabled := Assigned(DataFile);
|
---|
724 | {$IFOPT D+}
|
---|
725 | ATest.Enabled := True;
|
---|
726 | {$ENDIF}
|
---|
727 | {$IFOPT D-}
|
---|
728 | ATest.Enabled := False;
|
---|
729 | {$ENDIF}
|
---|
730 | ATest.Visible := ATest.Enabled;
|
---|
731 | AFileCompare.Visible := AFileCompare.Enabled;
|
---|
732 | end;
|
---|
733 |
|
---|
734 | procedure TCore.Initialize;
|
---|
735 | var
|
---|
736 | FileNameOption: string;
|
---|
737 | begin
|
---|
738 | if not InitializeStarted then begin
|
---|
739 | InitializeStarted := True;
|
---|
740 |
|
---|
741 | FileNameOption := FindFirstNonOption;
|
---|
742 | if FileNameOption <> '' then begin
|
---|
743 | // Open file specified as command line parameter
|
---|
744 | AFileNew.Execute;
|
---|
745 | DataFile.LoadFromFile(FileNameOption);
|
---|
746 | AddItemToLastOpenedList(FileNameOption);
|
---|
747 | end else
|
---|
748 | if ReopenLastFileOnStart and (LastOpenedList1.Items.Count > 0) and FileExists(LastOpenedList1.Items[0]) then begin
|
---|
749 | // Open last opened file
|
---|
750 | AFileNew.Execute;
|
---|
751 | DataFile.LoadFromFile(LastOpenedList1.Items[0])
|
---|
752 | end else AFileNew.Execute;
|
---|
753 |
|
---|
754 | UpdateFile;
|
---|
755 | InitializeFinished := True;
|
---|
756 | end;
|
---|
757 | end;
|
---|
758 |
|
---|
759 | function TCore.FindFirstNonOption: string;
|
---|
760 | var
|
---|
761 | S: string;
|
---|
762 | I: Integer;
|
---|
763 | begin
|
---|
764 | Result := '';
|
---|
765 | for I := 1 to Application.ParamCount do begin
|
---|
766 | S := Application.Params[I];
|
---|
767 | if S[1] = Application.OptionChar then Continue;
|
---|
768 | Result := S;
|
---|
769 | Break;
|
---|
770 | end;
|
---|
771 | end;
|
---|
772 |
|
---|
773 | end.
|
---|
774 |
|
---|