| 1 | unit UFormContacts;
|
|---|
| 2 |
|
|---|
| 3 | interface
|
|---|
| 4 |
|
|---|
| 5 | uses
|
|---|
| 6 | Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
|
|---|
| 7 | Menus, ActnList, UVCard, UListViewSort, LazUTF8, Clipbrd, URegistry,
|
|---|
| 8 | Generics.Collections, Types;
|
|---|
| 9 |
|
|---|
| 10 | type
|
|---|
| 11 |
|
|---|
| 12 | { TFormContacts }
|
|---|
| 13 |
|
|---|
| 14 | TFormContacts = class(TForm)
|
|---|
| 15 | AAdd: TAction;
|
|---|
| 16 | AClone: TAction;
|
|---|
| 17 | ACopy: TAction;
|
|---|
| 18 | AColumns: TAction;
|
|---|
| 19 | ACut: TAction;
|
|---|
| 20 | APaste: TAction;
|
|---|
| 21 | ALoadFromFile: TAction;
|
|---|
| 22 | ASaveToFile: TAction;
|
|---|
| 23 | ASelectAll: TAction;
|
|---|
| 24 | ARemove: TAction;
|
|---|
| 25 | AModify: TAction;
|
|---|
| 26 | ActionList1: TActionList;
|
|---|
| 27 | ListView1: TListView;
|
|---|
| 28 | ListViewFilter1: TListViewFilter;
|
|---|
| 29 | ListViewSort1: TListViewSort;
|
|---|
| 30 | MenuItem1: TMenuItem;
|
|---|
| 31 | MenuItem10: TMenuItem;
|
|---|
| 32 | MenuItem11: TMenuItem;
|
|---|
| 33 | MenuItem12: TMenuItem;
|
|---|
| 34 | Separator1: TMenuItem;
|
|---|
| 35 | MenuItemColumns: TMenuItem;
|
|---|
| 36 | MenuItem2: TMenuItem;
|
|---|
| 37 | MenuItem3: TMenuItem;
|
|---|
| 38 | MenuItem4: TMenuItem;
|
|---|
| 39 | MenuItem5: TMenuItem;
|
|---|
| 40 | MenuItem6: TMenuItem;
|
|---|
| 41 | MenuItem7: TMenuItem;
|
|---|
| 42 | MenuItem8: TMenuItem;
|
|---|
| 43 | MenuItem9: TMenuItem;
|
|---|
| 44 | OpenDialog1: TOpenDialog;
|
|---|
| 45 | PopupMenuContact: TPopupMenu;
|
|---|
| 46 | SaveDialog1: TSaveDialog;
|
|---|
| 47 | StatusBar1: TStatusBar;
|
|---|
| 48 | ToolBar1: TToolBar;
|
|---|
| 49 | ToolButton1: TToolButton;
|
|---|
| 50 | ToolButton2: TToolButton;
|
|---|
| 51 | ToolButton3: TToolButton;
|
|---|
| 52 | ToolButton4: TToolButton;
|
|---|
| 53 | ToolButton5: TToolButton;
|
|---|
| 54 | ToolButton6: TToolButton;
|
|---|
| 55 | ToolButton7: TToolButton;
|
|---|
| 56 | procedure AAddExecute(Sender: TObject);
|
|---|
| 57 | procedure ACloneExecute(Sender: TObject);
|
|---|
| 58 | procedure AColumnsExecute(Sender: TObject);
|
|---|
| 59 | procedure ACopyExecute(Sender: TObject);
|
|---|
| 60 | procedure ACutExecute(Sender: TObject);
|
|---|
| 61 | procedure ALoadFromFileExecute(Sender: TObject);
|
|---|
| 62 | procedure AModifyExecute(Sender: TObject);
|
|---|
| 63 | procedure APasteExecute(Sender: TObject);
|
|---|
| 64 | procedure ARemoveExecute(Sender: TObject);
|
|---|
| 65 | procedure ASaveToFileExecute(Sender: TObject);
|
|---|
| 66 | procedure ASelectAllExecute(Sender: TObject);
|
|---|
| 67 | procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
|
|---|
| 68 | procedure FormCreate(Sender: TObject);
|
|---|
| 69 | procedure FormDestroy(Sender: TObject);
|
|---|
| 70 | procedure FormResize(Sender: TObject);
|
|---|
| 71 | procedure FormShow(Sender: TObject);
|
|---|
| 72 | procedure ListView1Data(Sender: TObject; Item: TListItem);
|
|---|
| 73 | procedure ListView1DblClick(Sender: TObject);
|
|---|
| 74 | procedure ListView1SelectItem(Sender: TObject; Item: TListItem;
|
|---|
| 75 | Selected: Boolean);
|
|---|
| 76 | procedure ListViewFilter1Change(Sender: TObject);
|
|---|
| 77 | procedure ListViewSort1ColumnWidthChanged(Sender: TObject);
|
|---|
| 78 | function ListViewSort1CompareItem(Item1, Item2: TObject): Integer;
|
|---|
| 79 | procedure ListViewSort1Filter(ListViewSort: TListViewSort);
|
|---|
| 80 | private
|
|---|
| 81 | FContacts: TContacts;
|
|---|
| 82 | FUpdateCount: Integer;
|
|---|
| 83 | procedure FilterList(List: TObjectList<TObject>);
|
|---|
| 84 | procedure SetContacts(AValue: TContacts);
|
|---|
| 85 | function GetPreviousContact(Contact: TContact): TContact;
|
|---|
| 86 | function GetNextContact(Contact: TContact): TContact;
|
|---|
| 87 | procedure DoUpdateInterface;
|
|---|
| 88 | procedure UpdateColumns;
|
|---|
| 89 | procedure LoadFromRegistry(Context: TRegistryContext);
|
|---|
| 90 | procedure SaveToRegistry(Context: TRegistryContext);
|
|---|
| 91 | public
|
|---|
| 92 | ListViewColumns: TContactFieldIndexes;
|
|---|
| 93 | FilterItems: TContactFilterItems;
|
|---|
| 94 | Context: TRegistryContext;
|
|---|
| 95 | procedure ReloadList;
|
|---|
| 96 | procedure BeginUpdate;
|
|---|
| 97 | procedure EndUpdate;
|
|---|
| 98 | procedure UpdateInterface;
|
|---|
| 99 | property Contacts: TContacts read FContacts write SetContacts;
|
|---|
| 100 | end;
|
|---|
| 101 |
|
|---|
| 102 | var
|
|---|
| 103 | FormContacts: TFormContacts;
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 | implementation
|
|---|
| 107 |
|
|---|
| 108 | {$R *.lfm}
|
|---|
| 109 |
|
|---|
| 110 | uses
|
|---|
| 111 | UFormContact, UCore, UVCardFile, UFormColumns;
|
|---|
| 112 |
|
|---|
| 113 | resourcestring
|
|---|
| 114 | SRemoveContacts = 'Remove contacts';
|
|---|
| 115 | SRemoveContactsQuery = 'Do you want to remove selected contacts?';
|
|---|
| 116 | STotal = 'Total';
|
|---|
| 117 | SFiltered = 'Filtered';
|
|---|
| 118 | SSelected = 'Selected';
|
|---|
| 119 | SEndUpdateTooLow = 'Update counter error';
|
|---|
| 120 |
|
|---|
| 121 | { TFormContacts }
|
|---|
| 122 |
|
|---|
| 123 | procedure TFormContacts.ListView1Data(Sender: TObject; Item: TListItem);
|
|---|
| 124 |
|
|---|
| 125 | procedure AddItem(Text: string; IsCaption: Boolean = False);
|
|---|
| 126 | begin
|
|---|
| 127 | if IsCaption then begin
|
|---|
| 128 | if Text <> '' then Item.Caption := Text
|
|---|
| 129 | else Item.Caption := ' ';
|
|---|
| 130 | end else begin
|
|---|
| 131 | if Text <> '' then Item.SubItems.Add(Text)
|
|---|
| 132 | else Item.SubItems.Add(' ');
|
|---|
| 133 | end;
|
|---|
| 134 | end;
|
|---|
| 135 |
|
|---|
| 136 | var
|
|---|
| 137 | I: Integer;
|
|---|
| 138 | begin
|
|---|
| 139 | if Item.Index < ListViewSort1.List.Count then
|
|---|
| 140 | with TContact(ListViewSort1.List[Item.Index]) do begin
|
|---|
| 141 | for I := 0 to ListViewColumns.Count - 1 do begin
|
|---|
| 142 | AddItem(Fields[ListViewColumns[I]], I = 0);
|
|---|
| 143 | end;
|
|---|
| 144 | Item.Data := ListViewSort1.List[Item.Index];
|
|---|
| 145 | end;
|
|---|
| 146 | end;
|
|---|
| 147 |
|
|---|
| 148 | procedure TFormContacts.ListView1DblClick(Sender: TObject);
|
|---|
| 149 | begin
|
|---|
| 150 | AModify.Execute;
|
|---|
| 151 | end;
|
|---|
| 152 |
|
|---|
| 153 | procedure TFormContacts.ListView1SelectItem(Sender: TObject; Item: TListItem;
|
|---|
| 154 | Selected: Boolean);
|
|---|
| 155 | begin
|
|---|
| 156 | UpdateInterface;
|
|---|
| 157 | end;
|
|---|
| 158 |
|
|---|
| 159 | procedure TFormContacts.ListViewFilter1Change(Sender: TObject);
|
|---|
| 160 | var
|
|---|
| 161 | I: Integer;
|
|---|
| 162 | begin
|
|---|
| 163 | // Load filter StringGrid cells into filter
|
|---|
| 164 | FilterItems.Clear;
|
|---|
| 165 | for I := 0 to ListViewColumns.Count - 1 do
|
|---|
| 166 | if I < ListViewFilter1.StringGrid.ColCount then
|
|---|
| 167 | if ListViewFilter1.StringGrid.Cells[I, 0] <> '' then
|
|---|
| 168 | FilterItems.AddNew(ListViewColumns[I], ListViewFilter1.StringGrid.Cells[I, 0]);
|
|---|
| 169 |
|
|---|
| 170 | ReloadList;
|
|---|
| 171 | UpdateInterface;
|
|---|
| 172 | end;
|
|---|
| 173 |
|
|---|
| 174 | procedure TFormContacts.ListViewSort1ColumnWidthChanged(Sender: TObject);
|
|---|
| 175 | begin
|
|---|
| 176 | ListViewFilter1.UpdateFromListView(ListView1);
|
|---|
| 177 | end;
|
|---|
| 178 |
|
|---|
| 179 | function TFormContacts.ListViewSort1CompareItem(Item1, Item2: TObject): Integer;
|
|---|
| 180 | begin
|
|---|
| 181 | Result := 0;
|
|---|
| 182 | if Assigned(Item1) and Assigned(Item2) and (ListViewSort1.Order <> soNone) then begin
|
|---|
| 183 | with ListViewSort1 do
|
|---|
| 184 | Result := CompareString(TContact(Item1).Fields[ListViewColumns[Column]], TContact(Item2).Fields[ListViewColumns[Column]]);
|
|---|
| 185 | if ListViewSort1.Order = soDown then Result := -Result;
|
|---|
| 186 | end else Result := 0;
|
|---|
| 187 | end;
|
|---|
| 188 |
|
|---|
| 189 | procedure TFormContacts.ListViewSort1Filter(ListViewSort: TListViewSort);
|
|---|
| 190 | begin
|
|---|
| 191 | if Assigned(Contacts) then Contacts.AssignToList(ListViewSort1.List)
|
|---|
| 192 | else begin
|
|---|
| 193 | ListViewSort1.List.Clear;
|
|---|
| 194 | end;
|
|---|
| 195 | FilterList(ListViewSort1.List);
|
|---|
| 196 | end;
|
|---|
| 197 |
|
|---|
| 198 | procedure TFormContacts.FilterList(List: TObjectList<TObject>);
|
|---|
| 199 | var
|
|---|
| 200 | I: Integer;
|
|---|
| 201 | J: Integer;
|
|---|
| 202 | K: Integer;
|
|---|
| 203 | FoundCount: Integer;
|
|---|
| 204 | Item: TContact;
|
|---|
| 205 | begin
|
|---|
| 206 | for I := List.Count - 1 downto 0 do begin
|
|---|
| 207 | if List.Items[I] is TContact then begin
|
|---|
| 208 | Item := TContact(List.Items[I]);
|
|---|
| 209 | with Item do begin
|
|---|
| 210 | FoundCount := 0;
|
|---|
| 211 | for J := 0 to FilterItems.Count - 1 do
|
|---|
| 212 | if FilterItems[J].FieldIndex = cfNone then begin
|
|---|
| 213 | for K := 0 to Item.GetFields.Count - 1 do begin
|
|---|
| 214 | if Pos(UTF8LowerCase(FilterItems[J].Value),
|
|---|
| 215 | UTF8LowerCase(Item.Fields[TContact.GetFields[K].Index])) > 0 then begin
|
|---|
| 216 | Inc(FoundCount);
|
|---|
| 217 | Break;
|
|---|
| 218 | end;
|
|---|
| 219 | end;
|
|---|
| 220 | end else begin
|
|---|
| 221 | if Pos(UTF8LowerCase(FilterItems[J].Value),
|
|---|
| 222 | UTF8LowerCase(Item.Fields[FilterItems[J].FieldIndex])) > 0 then
|
|---|
| 223 | Inc(FoundCount);
|
|---|
| 224 | end;
|
|---|
| 225 | end;
|
|---|
| 226 | if FoundCount <> FilterItems.Count then List.Delete(I);
|
|---|
| 227 | end;
|
|---|
| 228 | end;
|
|---|
| 229 | end;
|
|---|
| 230 |
|
|---|
| 231 | procedure TFormContacts.SetContacts(AValue: TContacts);
|
|---|
| 232 | begin
|
|---|
| 233 | if FContacts = AValue then Exit;
|
|---|
| 234 | FContacts := AValue;
|
|---|
| 235 | ReloadList;
|
|---|
| 236 | BeginUpdate;
|
|---|
| 237 | try
|
|---|
| 238 | ListViewFilter1.Reset;
|
|---|
| 239 | finally
|
|---|
| 240 | EndUpdate;
|
|---|
| 241 | end;
|
|---|
| 242 | end;
|
|---|
| 243 |
|
|---|
| 244 | function TFormContacts.GetPreviousContact(Contact: TContact): TContact;
|
|---|
| 245 | var
|
|---|
| 246 | I: Integer;
|
|---|
| 247 | begin
|
|---|
| 248 | I := ListViewSort1.List.IndexOf(Contact);
|
|---|
| 249 | if (I <> -1) and (I > 0) then
|
|---|
| 250 | Result := TContact(ListViewSort1.List[I - 1])
|
|---|
| 251 | else Result := nil;
|
|---|
| 252 | end;
|
|---|
| 253 |
|
|---|
| 254 | function TFormContacts.GetNextContact(Contact: TContact): TContact;
|
|---|
| 255 | var
|
|---|
| 256 | I: Integer;
|
|---|
| 257 | begin
|
|---|
| 258 | I := ListViewSort1.List.IndexOf(Contact);
|
|---|
| 259 | if (I <> -1) and (I < ListViewSort1.List.Count - 1) then
|
|---|
| 260 | Result := TContact(ListViewSort1.List[I + 1])
|
|---|
| 261 | else Result := nil;
|
|---|
| 262 | end;
|
|---|
| 263 |
|
|---|
| 264 | procedure TFormContacts.DoUpdateInterface;
|
|---|
| 265 | var
|
|---|
| 266 | Text: string;
|
|---|
| 267 | SelectedCount: Integer;
|
|---|
| 268 | Selected: Boolean;
|
|---|
| 269 | begin
|
|---|
| 270 | if not ListView1.HandleAllocated then Exit;
|
|---|
| 271 |
|
|---|
| 272 | Selected := Assigned(ListView1.Selected);
|
|---|
| 273 | AAdd.Enabled := Assigned(Contacts);
|
|---|
| 274 | AModify.Enabled := Assigned(Contacts) and Selected;
|
|---|
| 275 | ARemove.Enabled := Assigned(Contacts) and Selected;
|
|---|
| 276 | AAdd.Enabled := Assigned(Contacts);
|
|---|
| 277 | AModify.Enabled := Assigned(Contacts) and Selected;
|
|---|
| 278 | AClone.Enabled := Assigned(Contacts) and Selected;
|
|---|
| 279 | ARemove.Enabled := Assigned(Contacts) and Selected;
|
|---|
| 280 | ASelectAll.Enabled := ListView1.Items.Count > 0;
|
|---|
| 281 | ALoadFromFile.Enabled := Assigned(Contacts) and Selected;
|
|---|
| 282 | ASaveToFile.Enabled := Assigned(Contacts) and Selected;
|
|---|
| 283 | ACopy.Enabled := Assigned(Contacts) and Selected;
|
|---|
| 284 | ACut.Enabled := Assigned(Contacts) and Selected;
|
|---|
| 285 | APaste.Enabled := Assigned(Contacts) and (Clipboard.AsText <> '');
|
|---|
| 286 |
|
|---|
| 287 | Text := '';
|
|---|
| 288 | if Assigned(Contacts) then begin
|
|---|
| 289 | Text := STotal + ': ' + IntToStr(Contacts.Count);
|
|---|
| 290 | if ListView1.Items.Count < Contacts.Count then
|
|---|
| 291 | Text := Text + ', ' + SFiltered + ': ' + IntToStr(ListView1.Items.Count);
|
|---|
| 292 | SelectedCount := ListView1.SelCount;
|
|---|
| 293 | if SelectedCount > 0 then
|
|---|
| 294 | Text := Text + ', ' + SSelected + ': ' + IntToStr(SelectedCount);
|
|---|
| 295 | end;
|
|---|
| 296 | StatusBar1.Panels[0].Text := Text;
|
|---|
| 297 |
|
|---|
| 298 | UpdateColumns;
|
|---|
| 299 | end;
|
|---|
| 300 |
|
|---|
| 301 | procedure TFormContacts.UpdateColumns;
|
|---|
| 302 | var
|
|---|
| 303 | I: Integer;
|
|---|
| 304 | Field: TContactField;
|
|---|
| 305 | begin
|
|---|
| 306 | ListView1.Columns.BeginUpdate;
|
|---|
| 307 | try
|
|---|
| 308 | while ListView1.Columns.Count < ListViewColumns.Count do
|
|---|
| 309 | ListView1.Columns.Add;
|
|---|
| 310 | while ListView1.Columns.Count > ListViewColumns.Count do
|
|---|
| 311 | ListView1.Columns.Delete(ListView1.Columns.Count - 1);
|
|---|
| 312 | for I := 0 to ListView1.Columns.Count - 1 do begin
|
|---|
| 313 | if Assigned(Contacts) and Assigned(Contacts.ParentVCard) then begin
|
|---|
| 314 | Field := TContact.GetFields.GetByIndex(ListViewColumns[I]);
|
|---|
| 315 | if Assigned(Field) then
|
|---|
| 316 | ListView1.Columns[I].Caption := Field.Title;
|
|---|
| 317 | end;
|
|---|
| 318 | end;
|
|---|
| 319 | finally
|
|---|
| 320 | ListView1.Columns.EndUpdate;
|
|---|
| 321 | end;
|
|---|
| 322 | end;
|
|---|
| 323 |
|
|---|
| 324 | procedure TFormContacts.LoadFromRegistry(Context: TRegistryContext);
|
|---|
| 325 | var
|
|---|
| 326 | I: Integer;
|
|---|
| 327 | Registry: TRegistryEx;
|
|---|
| 328 | ContactFieldIndex: TContactFieldIndex;
|
|---|
| 329 | begin
|
|---|
| 330 | Registry := TRegistryEx.Create;
|
|---|
| 331 | with Registry do
|
|---|
| 332 | try
|
|---|
| 333 | RootKey := Context.RootKey;
|
|---|
| 334 | OpenKey(Context.Key, True);
|
|---|
| 335 | ListViewColumns.Clear;
|
|---|
| 336 | I := 0;
|
|---|
| 337 | while ValueExists('Column' + IntToStr(I)) do begin
|
|---|
| 338 | ContactFieldIndex := TContactFieldIndex(ReadIntegerWithDefault('Column' + IntToStr(I), -1));
|
|---|
| 339 | ListViewColumns.Add(ContactFieldIndex);
|
|---|
| 340 | Inc(I);
|
|---|
| 341 | end;
|
|---|
| 342 |
|
|---|
| 343 | if ListViewColumns.Count = 0 then begin
|
|---|
| 344 | with ListViewColumns do begin
|
|---|
| 345 | Add(cfFullName);
|
|---|
| 346 | Add(cfFirstName);
|
|---|
| 347 | Add(cfMiddleName);
|
|---|
| 348 | Add(cfLastName);
|
|---|
| 349 | Add(cfTel);
|
|---|
| 350 | Add(cfTelCell);
|
|---|
| 351 | Add(cfTelHome);
|
|---|
| 352 | Add(cfTelWork);
|
|---|
| 353 | Add(cfEmailWork);
|
|---|
| 354 | Add(cfOrganization);
|
|---|
| 355 | end;
|
|---|
| 356 | end;
|
|---|
| 357 | finally
|
|---|
| 358 | Free;
|
|---|
| 359 | end;
|
|---|
| 360 | end;
|
|---|
| 361 |
|
|---|
| 362 | procedure TFormContacts.SaveToRegistry(Context: TRegistryContext);
|
|---|
| 363 | var
|
|---|
| 364 | I: Integer;
|
|---|
| 365 | Registry: TRegistryEx;
|
|---|
| 366 | begin
|
|---|
| 367 | Registry := TRegistryEx.Create;
|
|---|
| 368 | with Registry do
|
|---|
| 369 | try
|
|---|
| 370 | RootKey := Context.RootKey;
|
|---|
| 371 | OpenKey(Context.Key, True);
|
|---|
| 372 | for I := 0 to ListViewColumns.Count - 1 do
|
|---|
| 373 | WriteInteger('Column' + IntToStr(I), Integer(ListViewColumns[I]));
|
|---|
| 374 |
|
|---|
| 375 | // Remove old columns
|
|---|
| 376 | I := ListViewColumns.Count;
|
|---|
| 377 | while ValueExists('Column' + IntToStr(I)) do begin
|
|---|
| 378 | DeleteValue('Column' + IntToStr(I));
|
|---|
| 379 | Inc(I);
|
|---|
| 380 | end;
|
|---|
| 381 | finally
|
|---|
| 382 | Free;
|
|---|
| 383 | end;
|
|---|
| 384 | end;
|
|---|
| 385 |
|
|---|
| 386 | procedure TFormContacts.FormShow(Sender: TObject);
|
|---|
| 387 | begin
|
|---|
| 388 | Core.Translator.TranslateComponentRecursive(Self);
|
|---|
| 389 | Core.ThemeManager1.UseTheme(Self);
|
|---|
| 390 | Core.PersistentForm1.Load(Self);
|
|---|
| 391 | LoadFromRegistry(Context);
|
|---|
| 392 | ReloadList;
|
|---|
| 393 | UpdateInterface;
|
|---|
| 394 | ListViewFilter1.UpdateFromListView(ListView1);
|
|---|
| 395 | end;
|
|---|
| 396 |
|
|---|
| 397 | procedure TFormContacts.AAddExecute(Sender: TObject);
|
|---|
| 398 | var
|
|---|
| 399 | FormContact: TFormContact;
|
|---|
| 400 | Contact: TContact;
|
|---|
| 401 | begin
|
|---|
| 402 | FormContact := TFormContact.Create(nil);
|
|---|
| 403 | try
|
|---|
| 404 | Contact := TContact.Create;
|
|---|
| 405 | try
|
|---|
| 406 | Contact.ParentVCard := Contacts.ParentVCard;
|
|---|
| 407 | FormContact.Contact := Contact;
|
|---|
| 408 | FormContact.OnGetPrevious := GetPreviousContact;
|
|---|
| 409 | FormContact.OnGetNext := GetNextContact;
|
|---|
| 410 | Contact.Properties.AddNew('VERSION', Core.DefaultVcardVersion);
|
|---|
| 411 | if FormContact.ShowModal = mrOK then begin
|
|---|
| 412 | Contacts.Add(Contact);
|
|---|
| 413 | Core.DataFile.Modified := True;
|
|---|
| 414 | ReloadList;
|
|---|
| 415 | UpdateInterface;
|
|---|
| 416 | Contact := nil;
|
|---|
| 417 | end;
|
|---|
| 418 | finally
|
|---|
| 419 | if Assigned(Contact) then
|
|---|
| 420 | Contact.Free;
|
|---|
| 421 | end;
|
|---|
| 422 | finally
|
|---|
| 423 | FormContact.Free;
|
|---|
| 424 | end;
|
|---|
| 425 | end;
|
|---|
| 426 |
|
|---|
| 427 | procedure TFormContacts.ACloneExecute(Sender: TObject);
|
|---|
| 428 | var
|
|---|
| 429 | FormContact: TFormContact;
|
|---|
| 430 | Contact: TContact;
|
|---|
| 431 | begin
|
|---|
| 432 | FormContact := TFormContact.Create(nil);
|
|---|
| 433 | try
|
|---|
| 434 | Contact := TContact.Create;
|
|---|
| 435 | try
|
|---|
| 436 | Contact.ParentVCard := Contacts.ParentVCard;
|
|---|
| 437 | Contact.Assign(TContact(ListView1.Selected.Data));
|
|---|
| 438 | FormContact.Contact := Contact;
|
|---|
| 439 | FormContact.OnGetPrevious := GetPreviousContact;
|
|---|
| 440 | FormContact.OnGetNext := GetNextContact;
|
|---|
| 441 | if FormContact.ShowModal = mrOK then begin
|
|---|
| 442 | Contacts.Add(Contact);
|
|---|
| 443 | Contact := nil;
|
|---|
| 444 | Core.DataFile.Modified := True;
|
|---|
| 445 | ReloadList;
|
|---|
| 446 | UpdateInterface;
|
|---|
| 447 | end;
|
|---|
| 448 | finally
|
|---|
| 449 | if Assigned(Contact) then
|
|---|
| 450 | Contact.Free;
|
|---|
| 451 | end;
|
|---|
| 452 | finally
|
|---|
| 453 | FormContact.Free;
|
|---|
| 454 | end;
|
|---|
| 455 | end;
|
|---|
| 456 |
|
|---|
| 457 | procedure TFormContacts.AColumnsExecute(Sender: TObject);
|
|---|
| 458 | var
|
|---|
| 459 | FormColumns: TFormColumns;
|
|---|
| 460 | I: Integer;
|
|---|
| 461 | Field: TContactField;
|
|---|
| 462 | begin
|
|---|
| 463 | FormColumns := TFormColumns.Create(nil);
|
|---|
| 464 | with FormColumns do
|
|---|
| 465 | try
|
|---|
| 466 | for I := 0 to ListViewColumns.Count - 1 do begin
|
|---|
| 467 | Field := TContact.GetFields.GetByIndex(ListViewColumns[I]);
|
|---|
| 468 | if Assigned(Field) then
|
|---|
| 469 | ActiveColumns.AddObject(Field.Title, Field);
|
|---|
| 470 | end;
|
|---|
| 471 | for I := 0 to TContact.GetFields.Count - 1 do begin
|
|---|
| 472 | Field := TContact.GetFields[I];
|
|---|
| 473 | if ListViewColumns.IndexOf(Field.Index) = -1 then
|
|---|
| 474 | AvailableColumns.AddObject(Field.Title, Field);
|
|---|
| 475 | end;
|
|---|
| 476 | if ShowModal = mrOK then begin
|
|---|
| 477 | ListViewColumns.Clear;
|
|---|
| 478 | for I := 0 to ActiveColumns.Count - 1 do begin
|
|---|
| 479 | ListViewColumns.Add(TContactField(ActiveColumns.Objects[I]).Index);
|
|---|
| 480 | end;
|
|---|
| 481 | UpdateColumns;
|
|---|
| 482 | ReloadList;
|
|---|
| 483 | end;
|
|---|
| 484 | finally
|
|---|
| 485 | Free;
|
|---|
| 486 | end;
|
|---|
| 487 | end;
|
|---|
| 488 |
|
|---|
| 489 | procedure TFormContacts.ACopyExecute(Sender: TObject);
|
|---|
| 490 | var
|
|---|
| 491 | Text: string;
|
|---|
| 492 | Strings: TStringList;
|
|---|
| 493 | I: Integer;
|
|---|
| 494 | begin
|
|---|
| 495 | Strings := TStringList.Create;
|
|---|
| 496 | try
|
|---|
| 497 | Text := '';
|
|---|
| 498 | for I := 0 to ListView1.Items.Count - 1 do
|
|---|
| 499 | if ListView1.Items[I].Selected then begin
|
|---|
| 500 | Strings.Clear;
|
|---|
| 501 | TContact(ListView1.Items[I].Data).SaveToStrings(Strings);
|
|---|
| 502 | Text := Text + Strings.Text;
|
|---|
| 503 | end;
|
|---|
| 504 | Clipboard.AsText := Text;
|
|---|
| 505 | finally
|
|---|
| 506 | Strings.Free;
|
|---|
| 507 | end;
|
|---|
| 508 | end;
|
|---|
| 509 |
|
|---|
| 510 | procedure TFormContacts.ACutExecute(Sender: TObject);
|
|---|
| 511 | var
|
|---|
| 512 | Text: string;
|
|---|
| 513 | Strings: TStringList;
|
|---|
| 514 | I: Integer;
|
|---|
| 515 | begin
|
|---|
| 516 | Strings := TStringList.Create;
|
|---|
| 517 | try
|
|---|
| 518 | Text := '';
|
|---|
| 519 | for I := 0 to ListView1.Items.Count - 1 do
|
|---|
| 520 | if ListView1.Items[I].Selected then begin
|
|---|
| 521 | Strings.Clear;
|
|---|
| 522 | TContact(ListView1.Items[I].Data).SaveToStrings(Strings);
|
|---|
| 523 | Text := Text + Strings.Text;
|
|---|
| 524 | end;
|
|---|
| 525 | Clipboard.AsText := Text;
|
|---|
| 526 | for I := 0 to ListView1.Items.Count - 1 do
|
|---|
| 527 | if ListView1.Items[I].Selected then begin
|
|---|
| 528 | Contacts.Delete(Contacts.IndexOf(ListView1.Items[I].Data));
|
|---|
| 529 | end;
|
|---|
| 530 | ReloadList;
|
|---|
| 531 | ListView1.ClearSelection;
|
|---|
| 532 | UpdateInterface;
|
|---|
| 533 | finally
|
|---|
| 534 | Strings.Free;
|
|---|
| 535 | end;
|
|---|
| 536 | end;
|
|---|
| 537 |
|
|---|
| 538 | procedure TFormContacts.ALoadFromFileExecute(Sender: TObject);
|
|---|
| 539 | var
|
|---|
| 540 | TempFile: TVCardFile;
|
|---|
| 541 | begin
|
|---|
| 542 | if Assigned(ListView1.Selected) then begin
|
|---|
| 543 | TempFile := TVCardFile.Create(nil);
|
|---|
| 544 | try
|
|---|
| 545 | OpenDialog1.Filter := TempFile.GetFileFilter;
|
|---|
| 546 | OpenDialog1.DefaultExt := TempFile.GetFileExt;
|
|---|
| 547 | finally
|
|---|
| 548 | TempFile.Free;
|
|---|
| 549 | end;
|
|---|
| 550 | OpenDialog1.InitialDir := ExtractFileDir(Core.LastContactFileName);
|
|---|
| 551 | OpenDialog1.FileName := ExtractFileName(Core.LastContactFileName);
|
|---|
| 552 | if OpenDialog1.Execute then begin
|
|---|
| 553 | TContact(ListView1.Selected.Data).LoadFromFile(OpenDialog1.FileName);
|
|---|
| 554 | Core.LastContactFileName := OpenDialog1.FileName;
|
|---|
| 555 | ReloadList;
|
|---|
| 556 | end;
|
|---|
| 557 | end;
|
|---|
| 558 | end;
|
|---|
| 559 |
|
|---|
| 560 | procedure TFormContacts.AModifyExecute(Sender: TObject);
|
|---|
| 561 | var
|
|---|
| 562 | FormContact: TFormContact;
|
|---|
| 563 | Contact: TContact;
|
|---|
| 564 | begin
|
|---|
| 565 | FormContact := TFormContact.Create(nil);
|
|---|
| 566 | try
|
|---|
| 567 | Contact := TContact.Create;
|
|---|
| 568 | try
|
|---|
| 569 | Contact.ParentVCard := Contacts.ParentVCard;
|
|---|
| 570 | Contact.Assign(TContact(ListView1.Selected.Data));
|
|---|
| 571 | FormContact.Contact := Contact;
|
|---|
| 572 | FormContact.OnGetPrevious := GetPreviousContact;
|
|---|
| 573 | FormContact.OnGetNext := GetNextContact;
|
|---|
| 574 | if FormContact.ShowModal = mrOK then begin
|
|---|
| 575 | if not TContact(ListView1.Selected.Data).CompareTo(Contact) then
|
|---|
| 576 | Core.DataFile.Modified := True;
|
|---|
| 577 | TContact(ListView1.Selected.Data).Assign(Contact);
|
|---|
| 578 | ReloadList;
|
|---|
| 579 | UpdateInterface;
|
|---|
| 580 | end;
|
|---|
| 581 | finally
|
|---|
| 582 | Contact.Free;
|
|---|
| 583 | end;
|
|---|
| 584 | finally
|
|---|
| 585 | FormContact.Free;
|
|---|
| 586 | end;
|
|---|
| 587 | end;
|
|---|
| 588 |
|
|---|
| 589 | procedure TFormContacts.APasteExecute(Sender: TObject);
|
|---|
| 590 | var
|
|---|
| 591 | PasteContacts: TVCardFile;
|
|---|
| 592 | Lines: TStringList;
|
|---|
| 593 | begin
|
|---|
| 594 | PasteContacts := TVCardFile.Create(nil);
|
|---|
| 595 | Lines := TStringList.Create;
|
|---|
| 596 | try
|
|---|
| 597 | Lines.Text := Clipboard.AsText;
|
|---|
| 598 | PasteContacts.VCard.LoadFromStrings(Lines);
|
|---|
| 599 | if PasteContacts.VCard.Contacts.Count > 0 then begin
|
|---|
| 600 | if Assigned(ListView1.Selected) then begin
|
|---|
| 601 | Contacts.InsertContacts(Contacts.IndexOf(ListView1.Selected.Data),
|
|---|
| 602 | PasteContacts.VCard.Contacts);
|
|---|
| 603 | end else Contacts.AddContacts(PasteContacts.VCard.Contacts);
|
|---|
| 604 | Core.DataFile.Modified := True;
|
|---|
| 605 | ReloadList;
|
|---|
| 606 | UpdateInterface;
|
|---|
| 607 | end;
|
|---|
| 608 | finally
|
|---|
| 609 | Lines.Free;
|
|---|
| 610 | PasteContacts.Free;
|
|---|
| 611 | end;
|
|---|
| 612 | end;
|
|---|
| 613 |
|
|---|
| 614 | procedure TFormContacts.ARemoveExecute(Sender: TObject);
|
|---|
| 615 | var
|
|---|
| 616 | I: Integer;
|
|---|
| 617 | begin
|
|---|
| 618 | if Assigned(ListView1.Selected) then
|
|---|
| 619 | if MessageDlg(SRemoveContacts, SRemoveContactsQuery,
|
|---|
| 620 | TMsgDlgType.mtConfirmation, [mbCancel, mbOk], 0) = mrOk then begin
|
|---|
| 621 | for I := ListView1.Items.Count - 1 downto 0 do
|
|---|
| 622 | if ListView1.Items[I].Selected then begin
|
|---|
| 623 | Contacts.Delete(Contacts.IndexOf(ListView1.Items[I].Data));
|
|---|
| 624 | end;
|
|---|
| 625 | Core.DataFile.Modified := True;
|
|---|
| 626 | ReloadList;
|
|---|
| 627 | UpdateInterface;
|
|---|
| 628 | end;
|
|---|
| 629 | end;
|
|---|
| 630 |
|
|---|
| 631 | procedure TFormContacts.ASaveToFileExecute(Sender: TObject);
|
|---|
| 632 | var
|
|---|
| 633 | TempFile: TVCardFile;
|
|---|
| 634 | begin
|
|---|
| 635 | if Assigned(ListView1.Selected) then begin
|
|---|
| 636 | TempFile := TVCardFile.Create(nil);
|
|---|
| 637 | try
|
|---|
| 638 | SaveDialog1.Filter := TempFile.GetFileFilter;
|
|---|
| 639 | SaveDialog1.DefaultExt := TempFile.GetFileExt;
|
|---|
| 640 | finally
|
|---|
| 641 | TempFile.Free;
|
|---|
| 642 | end;
|
|---|
| 643 | SaveDialog1.InitialDir := ExtractFileDir(Core.LastContactFileName);
|
|---|
| 644 | SaveDialog1.FileName := TContact(ListView1.Selected.Data).FullNameToFileName +
|
|---|
| 645 | VCardFileExt;
|
|---|
| 646 | if SaveDialog1.Execute then begin
|
|---|
| 647 | TContact(ListView1.Selected.Data).SaveToFile(SaveDialog1.FileName);
|
|---|
| 648 | Core.LastContactFileName := SaveDialog1.FileName;
|
|---|
| 649 | end;
|
|---|
| 650 | end;
|
|---|
| 651 | end;
|
|---|
| 652 |
|
|---|
| 653 | procedure TFormContacts.ASelectAllExecute(Sender: TObject);
|
|---|
| 654 | var
|
|---|
| 655 | I: Integer;
|
|---|
| 656 | begin
|
|---|
| 657 | BeginUpdate;
|
|---|
| 658 | try
|
|---|
| 659 | ListView1.BeginUpdate;
|
|---|
| 660 | try
|
|---|
| 661 | for I := 0 to ListView1.Items.Count - 1 do
|
|---|
| 662 | ListView1.Items[I].Selected := True;
|
|---|
| 663 | //ListView1.SelectAll;
|
|---|
| 664 | finally
|
|---|
| 665 | ListView1.EndUpdate;
|
|---|
| 666 | end;
|
|---|
| 667 | finally
|
|---|
| 668 | EndUpdate;
|
|---|
| 669 | end;
|
|---|
| 670 | end;
|
|---|
| 671 |
|
|---|
| 672 | procedure TFormContacts.FormClose(Sender: TObject; var CloseAction: TCloseAction
|
|---|
| 673 | );
|
|---|
| 674 | begin
|
|---|
| 675 | SaveToRegistry(Context);
|
|---|
| 676 | Core.PersistentForm1.Save(Self);
|
|---|
| 677 | end;
|
|---|
| 678 |
|
|---|
| 679 | procedure TFormContacts.FormCreate(Sender: TObject);
|
|---|
| 680 | var
|
|---|
| 681 | I: Integer;
|
|---|
| 682 | begin
|
|---|
| 683 | FilterItems := TContactFilterItems.Create;
|
|---|
| 684 | ListViewColumns := TContactFieldIndexes.Create;
|
|---|
| 685 |
|
|---|
| 686 | FContacts := nil;
|
|---|
| 687 | for I := 0 to ToolBar1.ButtonCount - 1 do begin
|
|---|
| 688 | ToolBar1.Buttons[I].ShowHint := True;
|
|---|
| 689 | ToolBar1.Buttons[I].Hint := ToolBar1.Buttons[I].Caption;
|
|---|
| 690 | end;
|
|---|
| 691 | end;
|
|---|
| 692 |
|
|---|
| 693 | procedure TFormContacts.FormDestroy(Sender: TObject);
|
|---|
| 694 | begin
|
|---|
| 695 | FreeAndNil(ListViewColumns);
|
|---|
| 696 | FreeAndNil(FilterItems);
|
|---|
| 697 | end;
|
|---|
| 698 |
|
|---|
| 699 | procedure TFormContacts.FormResize(Sender: TObject);
|
|---|
| 700 | begin
|
|---|
| 701 | ListViewFilter1.UpdateFromListView(ListView1);
|
|---|
| 702 | end;
|
|---|
| 703 |
|
|---|
| 704 | procedure TFormContacts.ReloadList;
|
|---|
| 705 | begin
|
|---|
| 706 | ListViewSort1.Refresh;
|
|---|
| 707 | end;
|
|---|
| 708 |
|
|---|
| 709 | procedure TFormContacts.BeginUpdate;
|
|---|
| 710 | begin
|
|---|
| 711 | Inc(FUpdateCount);
|
|---|
| 712 | end;
|
|---|
| 713 |
|
|---|
| 714 | procedure TFormContacts.EndUpdate;
|
|---|
| 715 | begin
|
|---|
| 716 | if FUpdateCount <= 0 then raise Exception(SEndUpdateTooLow);
|
|---|
| 717 | Dec(FUpdateCount);
|
|---|
| 718 | if FUpdateCount = 0 then DoUpdateInterface;
|
|---|
| 719 | end;
|
|---|
| 720 |
|
|---|
| 721 | procedure TFormContacts.UpdateInterface;
|
|---|
| 722 | begin
|
|---|
| 723 | if FUpdateCount = 0 then DoUpdateInterface;
|
|---|
| 724 | end;
|
|---|
| 725 |
|
|---|
| 726 | end.
|
|---|
| 727 |
|
|---|