1 | unit FormFindDuplicity;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
|
---|
7 | ExtCtrls, StdCtrls, ActnList, Menus, VCard, Generics.Collections, RegistryEx,
|
---|
8 | Generics.Defaults, FormEx;
|
---|
9 |
|
---|
10 | type
|
---|
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 |
|
---|
58 | implementation
|
---|
59 |
|
---|
60 | {$R *.lfm}
|
---|
61 |
|
---|
62 | uses
|
---|
63 | Core, FormContacts, VCardFile;
|
---|
64 |
|
---|
65 | { TFoundItems }
|
---|
66 |
|
---|
67 | function TFoundItems.SearchByField(Field: string): TFoundItem;
|
---|
68 | var
|
---|
69 | Item: TFoundItem;
|
---|
70 | begin
|
---|
71 | Result := nil;
|
---|
72 | for Item in Self do
|
---|
73 | if Item.Field = Field then begin
|
---|
74 | Result := Item;
|
---|
75 | Break;
|
---|
76 | end;
|
---|
77 | end;
|
---|
78 |
|
---|
79 | { TFoundItem }
|
---|
80 |
|
---|
81 | constructor TFoundItem.Create;
|
---|
82 | begin
|
---|
83 | Contacts := TContacts.Create(False);
|
---|
84 | end;
|
---|
85 |
|
---|
86 | destructor TFoundItem.Destroy;
|
---|
87 | begin
|
---|
88 | FreeAndNil(Contacts);
|
---|
89 | inherited;
|
---|
90 | end;
|
---|
91 |
|
---|
92 | { TFormFindDuplicity }
|
---|
93 |
|
---|
94 | procedure TFormFindDuplicity.ListView1Data(Sender: TObject; Item: TListItem);
|
---|
95 | begin
|
---|
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;
|
---|
103 | end;
|
---|
104 |
|
---|
105 | procedure TFormFindDuplicity.SetContacts(AValue: TContacts);
|
---|
106 | var
|
---|
107 | ContactField: TContactField;
|
---|
108 | Items: TStringList;
|
---|
109 | I: Integer;
|
---|
110 | begin
|
---|
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;
|
---|
132 | end;
|
---|
133 |
|
---|
134 | function FoundItemsSort(constref Item1, Item2: TFoundItem): Integer;
|
---|
135 | begin
|
---|
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;
|
---|
139 | end;
|
---|
140 |
|
---|
141 | procedure TFormFindDuplicity.Find;
|
---|
142 | var
|
---|
143 | I: Integer;
|
---|
144 | Item: TFoundItem;
|
---|
145 | FieldName: string;
|
---|
146 | begin
|
---|
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;
|
---|
162 | end;
|
---|
163 |
|
---|
164 | procedure TFormFindDuplicity.FormCreate(Sender: TObject);
|
---|
165 | begin
|
---|
166 | FoundItems := TFoundItems.Create;
|
---|
167 | ContactFieldIndex := cfFullName;
|
---|
168 | end;
|
---|
169 |
|
---|
170 | procedure TFormFindDuplicity.ComboBoxFieldChange(Sender: TObject);
|
---|
171 | begin
|
---|
172 | if ComboBoxField.ItemIndex <> -1 then
|
---|
173 | ContactFieldIndex := TContactField(ComboBoxField.Items.Objects[ComboBoxField.ItemIndex]).Index
|
---|
174 | else ContactFieldIndex := cfTelCell;
|
---|
175 | Find;
|
---|
176 | end;
|
---|
177 |
|
---|
178 | procedure TFormFindDuplicity.AShowContactsExecute(Sender: TObject);
|
---|
179 | var
|
---|
180 | Form: TFormContacts;
|
---|
181 | I: Integer;
|
---|
182 | begin
|
---|
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;
|
---|
214 | end;
|
---|
215 |
|
---|
216 | procedure TFormFindDuplicity.ButtonMergeClick(Sender: TObject);
|
---|
217 | var
|
---|
218 | TempContacts: TVCardFile;
|
---|
219 | I: Integer;
|
---|
220 | begin
|
---|
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;
|
---|
230 | end;
|
---|
231 |
|
---|
232 | procedure TFormFindDuplicity.FormDestroy(Sender: TObject);
|
---|
233 | begin
|
---|
234 | FreeAndNil(FoundItems);
|
---|
235 | end;
|
---|
236 |
|
---|
237 | procedure TFormFindDuplicity.FormShow(Sender: TObject);
|
---|
238 | begin
|
---|
239 | Find;
|
---|
240 | end;
|
---|
241 |
|
---|
242 | procedure TFormFindDuplicity.ReloadList;
|
---|
243 | begin
|
---|
244 | ListView1.Items.Count := FoundItems.Count;
|
---|
245 | ListView1.Refresh;
|
---|
246 | end;
|
---|
247 |
|
---|
248 | end.
|
---|
249 |
|
---|