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