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