Changeset 14 for trunk/UCore.pas
- Timestamp:
- Feb 4, 2018, 2:01:05 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UCore.pas
r8 r14 12 12 type 13 13 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 14 24 { TCore } 15 25 16 26 TCore = class(TDataModule) 17 27 AAbout: TAction; 28 AFileMerge: TAction; 18 29 ASettings: TAction; 19 30 AFileOpenRecent: TAction; … … 36 47 procedure AAboutExecute(Sender: TObject); 37 48 procedure AExitExecute(Sender: TObject); 49 procedure AFileMergeExecute(Sender: TObject); 38 50 procedure AFileNewExecute(Sender: TObject); 39 51 procedure AFileOpenExecute(Sender: TObject); … … 64 76 procedure FileOpen(FileName: string); 65 77 procedure FileClose; 78 function FileMerge(FileName: string): TMergeResult; 66 79 procedure Initialize; 67 80 procedure UpdateInterface; … … 83 96 SAppExitQuery = 'File was modified. Do you want to save it before exit?'; 84 97 SFileNotFound = 'File ''%s'' not found.'; 98 SMergedContacts = 'Contacts merged. Loaded: %d, New: %d, Updated: %d'; 99 100 { TMergeResult } 101 102 procedure TMergeResult.Clear; 103 begin 104 Loaded := 0; 105 New := 0; 106 Updated := 0; 107 end; 108 109 class operator TMergeResult.Add(const A, B: TMergeResult): TMergeResult; 110 begin 111 Result.Loaded := A.Loaded + B.Loaded; 112 Result.New := A.New + B.New; 113 Result.Updated := A.Updated + B.Updated; 114 end; 85 115 86 116 { TCore } … … 91 121 end; 92 122 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); 123 procedure TCore.AFileMergeExecute(Sender: TObject); 134 124 var 135 125 TempFile: TDataFile; 126 I: Integer; 127 MergeResult: TMergeResult; 128 TotalMergeResult: TMergeResult; 136 129 begin 137 130 TempFile := DefaultDataFileClass.Create; … … 146 139 OpenDialog1.FileName := ExtractFileName(DataFile.FileName); 147 140 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; 152 end; 153 154 procedure TCore.AAboutExecute(Sender: TObject); 155 begin 156 FormAbout := TFormAbout.Create(nil); 157 try 158 FormAbout.ApplicationInfo := ApplicationInfo1; 159 FormAbout.ShowModal; 160 finally 161 FormAbout.Free; 162 end; 163 end; 164 165 procedure TCore.AFileCloseExecute(Sender: TObject); 166 begin 167 FileClose; 168 UpdateFile; 169 end; 170 171 procedure TCore.AHomePageExecute(Sender: TObject); 172 begin 173 OpenWebPage(ApplicationInfo1.HomePage); 174 end; 175 176 procedure TCore.ASettingsExecute(Sender: TObject); 177 begin 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; 186 end; 187 188 procedure TCore.AFileNewExecute(Sender: TObject); 189 begin 190 FileNew; 191 UpdateFile; 192 end; 193 194 procedure TCore.AFileOpenExecute(Sender: TObject); 195 var 196 TempFile: TDataFile; 197 begin 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]; 148 210 if OpenDialog1.Execute then begin 149 211 FileOpen(OpenDialog1.FileName); … … 241 303 end; 242 304 305 function TCore.FileMerge(FileName: string): TMergeResult; 306 var 307 TempFile: TContactsFile; 308 NewContact: TContact; 309 I: Integer; 310 CountNew: Integer; 311 CountUpdated: Integer; 312 begin 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])); 337 end; 338 243 339 procedure TCore.FileNew; 244 340 begin … … 257 353 FormContacts.Contacts := TContactsFile(DataFile).Contacts 258 354 else FormContacts.Contacts := nil; 355 FormContacts.ReloadList; 259 356 end; 260 357
Note:
See TracChangeset
for help on using the changeset viewer.