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