source: trunk/Forms/FormFindDuplicity.pas

Last change on this file was 162, checked in by chronos, 17 months ago
  • Modified: Updated Common package.
File size: 6.3 KB
Line 
1unit FormFindDuplicity;
2
3interface
4
5uses
6 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
7 ExtCtrls, StdCtrls, ActnList, Menus, VCard, Generics.Collections, RegistryEx,
8 Generics.Defaults, FormEx;
9
10type
11
12 { TFoundItem }
13
14 TFoundItem = class
15 Field: string;
16 Contacts: TContacts;
17 constructor Create;
18 destructor Destroy; override;
19 end;
20
21 { TFoundItems }
22
23 TFoundItems = class(TObjectList<TFoundItem>)
24 function SearchByField(Field: string): TFoundItem;
25 end;
26
27 { TFormFindDuplicity }
28
29 TFormFindDuplicity = class(TFormEx)
30 AShowContacts: TAction;
31 ActionList1: TActionList;
32 ButtonMerge: TButton;
33 ComboBoxField: TComboBox;
34 Label1: TLabel;
35 ListView1: TListView;
36 MenuItem1: TMenuItem;
37 Panel1: TPanel;
38 PopupMenu1: TPopupMenu;
39 procedure AShowContactsExecute(Sender: TObject);
40 procedure ButtonMergeClick(Sender: TObject);
41 procedure ComboBoxFieldChange(Sender: TObject);
42 procedure FormCreate(Sender: TObject);
43 procedure FormDestroy(Sender: TObject);
44 procedure FormShow(Sender: TObject);
45 procedure ListView1Data(Sender: TObject; Item: TListItem);
46 private
47 FContacts: TContacts;
48 procedure SetContacts(AValue: TContacts);
49 public
50 FoundItems: TFoundItems;
51 ContactFieldIndex: TContactFieldIndex;
52 procedure Find;
53 procedure ReloadList;
54 property Contacts: TContacts read FContacts write SetContacts;
55 end;
56
57
58implementation
59
60{$R *.lfm}
61
62uses
63 Core, FormContacts, VCardFile;
64
65{ TFoundItems }
66
67function TFoundItems.SearchByField(Field: string): TFoundItem;
68var
69 Item: TFoundItem;
70begin
71 Result := nil;
72 for Item in Self do
73 if Item.Field = Field then begin
74 Result := Item;
75 Break;
76 end;
77end;
78
79{ TFoundItem }
80
81constructor TFoundItem.Create;
82begin
83 Contacts := TContacts.Create(False);
84end;
85
86destructor TFoundItem.Destroy;
87begin
88 FreeAndNil(Contacts);
89 inherited;
90end;
91
92{ TFormFindDuplicity }
93
94procedure TFormFindDuplicity.ListView1Data(Sender: TObject; Item: TListItem);
95begin
96 if Item.Index < FoundItems.Count then
97 with TFoundItem(FoundItems[Item.Index]) do begin
98 Item.Caption := Field;
99 Item.Data := FoundItems[Item.Index];
100 Item.SubItems.Add(Contacts.ToString);
101 Item.SubItems.Add(IntToStr(Contacts.Count));
102 end;
103end;
104
105procedure TFormFindDuplicity.SetContacts(AValue: TContacts);
106var
107 ContactField: TContactField;
108 Items: TStringList;
109 I: Integer;
110begin
111 if FContacts = AValue then Exit;
112 FContacts := AValue;
113 if Assigned(FContacts) then begin
114 Items := TStringList.Create;
115 try
116 TContact.GetFields.LoadToStrings(Items);
117
118 // Remove fields which are not used in contacts
119 for I := Items.Count - 1 downto 0 do
120 if Contacts.CountByField(TContactField(Items.Objects[I]).Index) = 0 then
121 Items.Delete(I);
122
123 ComboBoxField.Items.Assign(Items);
124 finally
125 Items.Free;
126 end;
127 ContactField := TContact.GetFields.GetByIndex(ContactFieldIndex);
128 ComboBoxField.ItemIndex := ComboBoxField.Items.IndexOfObject(ContactField);
129 if (ComboBoxField.Items.Count > 0) and (ComboBoxField.ItemIndex = -1) then
130 ComboBoxField.ItemIndex := 0;
131 end else ComboBoxField.Clear;
132end;
133
134function FoundItemsSort(constref Item1, Item2: TFoundItem): Integer;
135begin
136 if Item1.Contacts.Count < Item2.Contacts.Count then Result := 1
137 else if Item1.Contacts.Count > Item2.Contacts.Count then Result := -1
138 else Result := 0;
139end;
140
141procedure TFormFindDuplicity.Find;
142var
143 I: Integer;
144 Item: TFoundItem;
145 FieldName: string;
146begin
147 FoundItems.Clear;
148 for I := 0 to Contacts.Count - 1 do begin
149 FieldName := Contacts[I].Fields[ContactFieldIndex];
150 if FieldName <> '' then begin
151 Item := FoundItems.SearchByField(FieldName);
152 if not Assigned(Item) then begin
153 Item := TFoundItem.Create;
154 Item.Field := FieldName;
155 FoundItems.Add(Item);
156 end;
157 Item.Contacts.Add(Contacts[I]);
158 end;
159 end;
160 FoundItems.Sort(TComparer<TFoundItem>.Construct(FoundItemsSort));
161 ReloadList;
162end;
163
164procedure TFormFindDuplicity.FormCreate(Sender: TObject);
165begin
166 FoundItems := TFoundItems.Create;
167 ContactFieldIndex := cfFullName;
168end;
169
170procedure TFormFindDuplicity.ComboBoxFieldChange(Sender: TObject);
171begin
172 if ComboBoxField.ItemIndex <> -1 then
173 ContactFieldIndex := TContactField(ComboBoxField.Items.Objects[ComboBoxField.ItemIndex]).Index
174 else ContactFieldIndex := cfTelCell;
175 Find;
176end;
177
178procedure TFormFindDuplicity.AShowContactsExecute(Sender: TObject);
179var
180 Form: TFormContacts;
181 I: Integer;
182begin
183 if Assigned(ListView1.Selected) then begin
184 Form := TFormContacts.Create(nil);
185 Form.Contacts := TContacts.Create(False);
186 Form.Contacts.ParentVCard := Contacts.ParentVCard;
187 Form.Context := TRegistryContext.Create(Core.Core.ApplicationInfo1.RegistryRoot,
188 Core.Core.ApplicationInfo1.RegistryKey + '\ContactsColumns');
189 with TFoundItem(ListView1.Selected.Data) do
190 for I := 0 to Contacts.Count - 1 do
191 Form.Contacts.Add(Contacts[I]);
192 Form.ShowModal;
193 with TFoundItem(ListView1.Selected.Data) do begin
194 // Remove all deleted
195 for I := 0 to Contacts.Count - 1 do
196 if Form.Contacts.IndexOf(Contacts[I]) = -1 then begin
197 Form.Contacts.Remove(Contacts[I]);
198 Self.Contacts.Remove(Contacts[I]);
199 Self.Contacts.ParentVCard.Modified := True;
200 end;
201
202 // Add newly added
203 for I := 0 to Form.Contacts.Count - 1 do
204 if Contacts.IndexOf(Form.Contacts[I]) = -1 then begin
205 Form.Contacts.Add(Form.Contacts[I]);
206 Self.Contacts.Add(Form.Contacts[I]);
207 Self.Contacts.ParentVCard.Modified := True;
208 end;
209 end;
210 Form.Contacts.Free;
211 Form.Free;
212 Find;
213 end;
214end;
215
216procedure TFormFindDuplicity.ButtonMergeClick(Sender: TObject);
217var
218 TempContacts: TVCardFile;
219 I: Integer;
220begin
221 TempContacts := TVCardFile.Create(nil);
222 try
223 for I := 0 to Contacts.Count - 1 do
224 TempContacts.VCard.Contacts.Merge(Contacts[I], TContactField(ComboBoxField.Items.Objects[ComboBoxField.ItemIndex]).Index);
225 Contacts.Assign(TempContacts.VCard.Contacts);
226 Find;
227 finally
228 TempContacts.Free;
229 end;
230end;
231
232procedure TFormFindDuplicity.FormDestroy(Sender: TObject);
233begin
234 FreeAndNil(FoundItems);
235end;
236
237procedure TFormFindDuplicity.FormShow(Sender: TObject);
238begin
239 Find;
240end;
241
242procedure TFormFindDuplicity.ReloadList;
243begin
244 ListView1.Items.Count := FoundItems.Count;
245 ListView1.Refresh;
246end;
247
248end.
249
Note: See TracBrowser for help on using the repository browser.