Changeset 14 for trunk/UCore.pas


Ignore:
Timestamp:
Feb 4, 2018, 2:01:05 PM (7 years ago)
Author:
chronos
Message:
  • Added: New File menu action Merge. It can merge multiple vcard files and update values for items with same full name.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UCore.pas

    r8 r14  
    1212type
    1313
     14  { TMergeResult }
     15
     16  TMergeResult = record
     17    Loaded: Integer;
     18    New: Integer;
     19    Updated: Integer;
     20    procedure Clear;
     21    class operator Add(const A, B: TMergeResult): TMergeResult;
     22  end;
     23
    1424  { TCore }
    1525
    1626  TCore = class(TDataModule)
    1727    AAbout: TAction;
     28    AFileMerge: TAction;
    1829    ASettings: TAction;
    1930    AFileOpenRecent: TAction;
     
    3647    procedure AAboutExecute(Sender: TObject);
    3748    procedure AExitExecute(Sender: TObject);
     49    procedure AFileMergeExecute(Sender: TObject);
    3850    procedure AFileNewExecute(Sender: TObject);
    3951    procedure AFileOpenExecute(Sender: TObject);
     
    6476    procedure FileOpen(FileName: string);
    6577    procedure FileClose;
     78    function FileMerge(FileName: string): TMergeResult;
    6679    procedure Initialize;
    6780    procedure UpdateInterface;
     
    8396  SAppExitQuery = 'File was modified. Do you want to save it before exit?';
    8497  SFileNotFound = 'File ''%s'' not found.';
     98  SMergedContacts = 'Contacts merged. Loaded: %d, New: %d, Updated: %d';
     99
     100{ TMergeResult }
     101
     102procedure TMergeResult.Clear;
     103begin
     104  Loaded := 0;
     105  New := 0;
     106  Updated := 0;
     107end;
     108
     109class operator TMergeResult.Add(const A, B: TMergeResult): TMergeResult;
     110begin
     111  Result.Loaded := A.Loaded + B.Loaded;
     112  Result.New := A.New + B.New;
     113  Result.Updated := A.Updated + B.Updated;
     114end;
    85115
    86116{ TCore }
     
    91121end;
    92122
    93 procedure TCore.AAboutExecute(Sender: TObject);
    94 begin
    95   FormAbout := TFormAbout.Create(nil);
    96   try
    97     FormAbout.ApplicationInfo := ApplicationInfo1;
    98     FormAbout.ShowModal;
    99   finally
    100     FormAbout.Free;
    101   end;
    102 end;
    103 
    104 procedure TCore.AFileCloseExecute(Sender: TObject);
    105 begin
    106   FileClose;
    107   UpdateFile;
    108 end;
    109 
    110 procedure TCore.AHomePageExecute(Sender: TObject);
    111 begin
    112   OpenWebPage(ApplicationInfo1.HomePage);
    113 end;
    114 
    115 procedure TCore.ASettingsExecute(Sender: TObject);
    116 begin
    117   FormSettings := TFormSettings.Create(nil);
    118   try
    119     FormSettings.LoadData;
    120     if FormSettings.ShowModal = mrOK then
    121       FormSettings.SaveData;
    122   finally
    123     FormSettings.Free;
    124   end;
    125 end;
    126 
    127 procedure TCore.AFileNewExecute(Sender: TObject);
    128 begin
    129   FileNew;
    130   UpdateFile;
    131 end;
    132 
    133 procedure TCore.AFileOpenExecute(Sender: TObject);
     123procedure TCore.AFileMergeExecute(Sender: TObject);
    134124var
    135125  TempFile: TDataFile;
     126  I: Integer;
     127  MergeResult: TMergeResult;
     128  TotalMergeResult: TMergeResult;
    136129begin
    137130  TempFile := DefaultDataFileClass.Create;
     
    146139    OpenDialog1.FileName := ExtractFileName(DataFile.FileName);
    147140  end;
     141  OpenDialog1.Options := OpenDialog1.Options + [ofAllowMultiSelect];
     142  if OpenDialog1.Execute then begin
     143    TotalMergeResult.Clear;
     144    for I := 0 to OpenDialog1.Files.Count - 1 do begin
     145      MergeResult := FileMerge(OpenDialog1.Files[I]);
     146      TotalMergeResult := TotalMergeResult + MergeResult;
     147    end;
     148    ShowMessage(Format(SMergedContacts, [TotalMergeResult.Loaded,
     149      TotalMergeResult.New, TotalMergeResult.Updated]));
     150    UpdateFile;
     151  end;
     152end;
     153
     154procedure TCore.AAboutExecute(Sender: TObject);
     155begin
     156  FormAbout := TFormAbout.Create(nil);
     157  try
     158    FormAbout.ApplicationInfo := ApplicationInfo1;
     159    FormAbout.ShowModal;
     160  finally
     161    FormAbout.Free;
     162  end;
     163end;
     164
     165procedure TCore.AFileCloseExecute(Sender: TObject);
     166begin
     167  FileClose;
     168  UpdateFile;
     169end;
     170
     171procedure TCore.AHomePageExecute(Sender: TObject);
     172begin
     173  OpenWebPage(ApplicationInfo1.HomePage);
     174end;
     175
     176procedure TCore.ASettingsExecute(Sender: TObject);
     177begin
     178  FormSettings := TFormSettings.Create(nil);
     179  try
     180    FormSettings.LoadData;
     181    if FormSettings.ShowModal = mrOK then
     182      FormSettings.SaveData;
     183  finally
     184    FormSettings.Free;
     185  end;
     186end;
     187
     188procedure TCore.AFileNewExecute(Sender: TObject);
     189begin
     190  FileNew;
     191  UpdateFile;
     192end;
     193
     194procedure TCore.AFileOpenExecute(Sender: TObject);
     195var
     196  TempFile: TDataFile;
     197begin
     198  TempFile := DefaultDataFileClass.Create;
     199  try
     200    OpenDialog1.Filter := TempFile.GetFileFilter;
     201  finally
     202    TempFile.Free;
     203  end;
     204  OpenDialog1.DefaultExt := '';
     205  if Assigned(DataFile) then begin
     206    OpenDialog1.InitialDir := ExtractFileDir(DataFile.FileName);
     207    OpenDialog1.FileName := ExtractFileName(DataFile.FileName);
     208  end;
     209  OpenDialog1.Options := OpenDialog1.Options - [ofAllowMultiSelect];
    148210  if OpenDialog1.Execute then begin
    149211    FileOpen(OpenDialog1.FileName);
     
    241303end;
    242304
     305function TCore.FileMerge(FileName: string): TMergeResult;
     306var
     307  TempFile: TContactsFile;
     308  NewContact: TContact;
     309  I: Integer;
     310  CountNew: Integer;
     311  CountUpdated: Integer;
     312begin
     313  Result.Clear;
     314  if FileExists(FileName) then begin
     315    TempFile := TContactsFile.Create;
     316    try
     317      TempFile.LoadFromFile(FileName);
     318      Result.Loaded := TempFile.Contacts.Count;
     319      for I := 0 to TempFile.Contacts.Count - 1 do begin
     320        NewContact := TContactsFile(DataFile).Contacts.Search(TContact(TempFile.Contacts[I]).FullName);
     321        if not Assigned(NewContact) then begin
     322          NewContact := TContact.Create;
     323          NewContact.Parent := TContactsFile(DataFile);
     324          NewContact.Assign(TContact(TempFile.Contacts[I]));
     325          TContactsFile(DataFile).Contacts.Add(NewContact);
     326          Inc(Result.New);
     327        end else begin
     328          if NewContact.UpdateFrom(TContact(TempFile.Contacts[I])) then
     329            Inc(Result.Updated);
     330        end;
     331      end;
     332      TContactsFile(DataFile).Modified := True;
     333    finally
     334      TempFile.Free;
     335    end;
     336  end else ShowMessage(Format(SFileNotFound, [FileName]));
     337end;
     338
    243339procedure TCore.FileNew;
    244340begin
     
    257353    FormContacts.Contacts := TContactsFile(DataFile).Contacts
    258354    else FormContacts.Contacts := nil;
     355  FormContacts.ReloadList;
    259356end;
    260357
Note: See TracChangeset for help on using the changeset viewer.