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