-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathduplicateremover.pas
287 lines (251 loc) · 10.9 KB
/
duplicateremover.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
unit duplicateremover;
{$I videlibrilanguageconfig.inc}
interface
uses
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls, TreeListView, bookListView, applicationformconfig;
type
TduplicateForm = class(TVideLibriForm)
Button1: TButton;
Button2: TButton;
cbAccounts: TComboBox;
cbTimeConstraint: TComboBox;
Label1: TLabel;
Label2: TLabel;
lbCount: TLabel;
Panel2: TPanel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure CheckBox1Change(Sender: TObject);
procedure cbAccountsSelect(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
listview: TBookListView;
totalcount,checkedcount: integer;
procedure itemPositioning({%H-}sender: TObject; {%H-}visualColumnIndex: integer; recordItem: TTreeListRecordItem; var aposition: TRect);
procedure listviewCustomRecordItemDraw({%H-}sender: TObject; eventTyp_cdet: TCustomDrawEventTyp; recordItem: TTreeListRecordItem;
var {%H-}defaultDraw: Boolean);
procedure listviewMouseDown(Sender: TObject; {%H-}Button: TMouseButton; {%H-}Shift: TShiftState; X, Y: Integer);
procedure searchDuplicates;
end;
var
duplicateForm: TduplicateForm;
implementation
uses applicationconfig,applicationdesktopconfig ,booklistreader,bbutils,math,libraryAccess,libraryParser, themes;
resourcestring
rsConfirmDelete = 'Sollen die ausgewählten Duplikate durch jeweils einen einzelnen Eintrag ersetzt werden?';
procedure TduplicateForm.Button2Click(Sender: TObject);
begin
Close;
end;
procedure TduplicateForm.Button1Click(Sender: TObject);
var
i, j: Integer;
begin
if not confirm(rsConfirmDelete) then exit;
system.EnterCriticalsection(updateThreadConfig.libraryAccessSection);
for i := 0 to listview.Items.Count - 1 do begin
if listview.getAdditionalBookData(listview.Items[i]).checked = csUnchecked then continue;
(listview.books[i].owningAccount as TCustomAccountAccess).books.old.add(listview.books[i]);
for j := 0 to listview.Items[i].SubItems.Count - 1 do
if listview.getAdditionalBookData(listview.Items[i]).checked <> csUnchecked then
(tbook(listview.Items[i].SubItems[j].data.obj).owningAccount as TCustomAccountAccess).books.old.remove(tbook(listview.Items[i].SubItems[j].data.obj));
end;
for i := 0 to accounts.Count - 1 do
accounts[i].saveBooks();
//it does not seem like a bad idea to block everything if an error occured
system.LeaveCriticalsection(updateThreadConfig.libraryAccessSection);
searchDuplicates;
end;
procedure TduplicateForm.CheckBox1Change(Sender: TObject);
begin
searchDuplicates;
end;
procedure TduplicateForm.cbAccountsSelect(Sender: TObject);
begin
searchDuplicates;
end;
procedure TduplicateForm.FormCreate(Sender: TObject);
var
i: Integer;
begin
listview := TBookListView.create(self,true);
listview.Align := alClient;
listview.Options := listview.Options + [tlvoSorted];
listview.Parent := self;
listview.OnCustomRecordItemPositioning:=@itemPositioning;
listview.OnCustomRecordItemDraw:=@listviewCustomRecordItemDraw;
listview.OnMouseDown:=@listviewMouseDown;
listview.addDefaultColumns;
listview.Columns[high(listview.properties)].Width:=15;
listview.addColumn('_firstexistsdate');
listview.addColumn('_lastexistsdate');
for i := 0 to accounts.count - 1 do cbAccounts.Items.add(accounts[i].prettyName);
searchDuplicates;
end;
var sortUsers: boolean;
function bookcompare(Item1, Item2: Pointer): Integer;
begin
result := striCompareClever(tbook(item1).title, tbook(item2).title);
if result <> 0 then exit;
result := striCompareClever(tbook(item1).author, tbook(item2).author);
if result <> 0 then exit;
result := striCompareClever(tbook(item1).year, tbook(item2).year);
if result <> 0 then exit;
result := striCompareClever(tbook(item1).id, tbook(item2).id);
if result <> 0 then exit;
if sortUsers then begin
result := Sign(PtrInt(tbook(item1).owningAccount) - PtrInt(tbook(item2).owningAccount));
if result <> 0 then exit;
end;
result := Sign(tbook(item1).issueDate - tbook(item2).issueDate);
if result <> 0 then exit;
result := Sign(tbook(item1).dueDate - tbook(item2).dueDate);
if result <> 0 then exit;
end;
function minPos(const d, e: Integer): integer;
begin
if d <= 0 then result := e
else if e <= 0 then result := d
else result := min(d,e);
end;
procedure TduplicateForm.searchDuplicates;
procedure fillItem(item: TTreeListItem; book: TBook);
begin
listview.fillBookItem(item, book);
listview.getAdditionalBookData(item).checked := csChecked;
item.RecordItemsText[listview.getPropertyColumnIndex('dueDate')] := DateToPrettyStr(book.dueDate);
end;
var alllists: TBookList;
i, accountMode, timeMode: Integer;
ingroup: Boolean;
subitem, item: TTreeListItem;
itemBook: TBook;
begin
accountMode := cbAccounts.ItemIndex;
timeMode := cbTimeConstraint.ItemIndex;
listview.clear;
alllists := TBookList.create();
system.EnterCriticalSection(updateThreadConfig.libraryAccessSection);
try
if (accountMode < 2) then begin
for i := 0 to accounts.Count - 1 do
alllists.addList(accounts[i].books.old);
end else alllists.addList(accounts[cbAccounts.ItemIndex-2].books.old);
sortUsers := accountMode = 1;
alllists.Sort(@bookcompare);
totalcount := 0;
item := nil; itembook := nil;
ingroup := false;
listview.BeginUpdate;
for i := 1 to alllists.Count - 1 do begin
if alllists[i].equalToKey(alllists[i-1])
and ((accountMode <> 1) or (alllists[i].owningAccount = alllists[i-1].owningAccount))
and ((timeMode = 0)
or ((timeMode = 1) and ( max(alllists[i-1].dueDate, alllists[i-1].lastExistsDate) >= minPos(alllists[i].issueDate, alllists[i].firstExistsDate ) - 1))
or ((timeMode = 2) and (alllists[i-1].issueDate = alllists[i].issueDate)))
then begin
if not ingroup then begin
if item <> nil then fillItem(item, itembook);
item := listview.Items.Add;
itemBook := alllists[i-1].clone;
itemBook.owningAccount := alllists[i-1].owningAccount;
subitem := item.SubItems.add;
fillItem(subitem, alllists[i-1]);
ingroup := true;
totalcount += 1;
end;
subitem := item.SubItems.add;
fillItem(subitem, alllists[i]);
if (alllists[i].firstExistsDate <> 0) and ( (alllists[i].firstExistsDate < itemBook.firstExistsDate) or (itemBook.firstExistsDate = 0)) then
itemBook.firstExistsDate := alllists[i].firstExistsDate;
if (alllists[i].issueDate <> 0) and ( (alllists[i].issueDate < itemBook.issueDate) or (itemBook.issueDate = 0)) then
itemBook.issueDate := alllists[i].issueDate;
if (alllists[i].lastExistsDate <> 0) and ( (alllists[i].lastExistsDate > itemBook.lastExistsDate) or (itemBook.lastExistsDate = 0)) then
itemBook.lastExistsDate := alllists[i].lastExistsDate;
if (alllists[i].dueDate <> 0) and ( (alllists[i].dueDate > itemBook.dueDate) or (itemBook.dueDate = 0)) then
itemBook.dueDate := alllists[i].dueDate;
totalcount += 1;
end else ingroup := false;
end;
if item <> nil then fillItem(item, itembook);
listview.EndUpdate;
checkedcount := listview.Items.Count;
lbCount.Caption := IntToStr(checkedcount) + ' / ' + IntToStr(listview.Items.Count) + ' / ' + IntToStr(totalcount);
finally
alllists.free;
system.LeaveCriticalsection(updateThreadConfig.libraryAccessSection);
end;
end;
procedure TduplicateForm.listviewCustomRecordItemDraw(sender: TObject; eventTyp_cdet: TCustomDrawEventTyp; recordItem: TTreeListRecordItem;
var defaultDraw: Boolean);
var
cb: TThemedElementDetails;
contentrect: TRect;
w: LongInt;
begin
if eventTyp_cdet = cdetPrePaint then exit;
if recordItem.Index <> 0 then exit;
case listview.getAdditionalBookData(recordItem.Parent).checked of
csChecked: cb := ThemeServices.GetElementDetails(tbCheckBoxCheckedNormal);
csDisabled: cb := ThemeServices.GetElementDetails(tbCheckBoxUncheckedDisabled);
csMixed: cb := ThemeServices.GetElementDetails(tbCheckBoxMixedNormal);
//CHECKBOX_HIDDEN: exit;
else cb := ThemeServices.GetElementDetails(tbCheckBoxUncheckedNormal);
end;
contentrect := listview.DrawingRecordItemRect;
w := contentrect.Bottom - contentrect.Top; //contentrect.Right - contentrect.left;
contentrect.Right := contentrect.left + w;
ThemeServices.DrawElement(listview.Canvas.Handle, cb, contentrect, @listview.DrawingRecordItemRect);
end;
procedure TduplicateForm.listviewMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
recorditem: TTreeListRecordItem;
item, counteditem: TTreeListItem;
rec: TRect;
hasChecked, hasUnchecked, old: Boolean;
i: Integer;
begin
recorditem := listview.GetRecordItemAtPos(x,y);
if (recorditem = nil) or (recorditem.Index > 0) then exit;
item := recorditem.Parent;
rec := item.getBounds(0);
if (x >= 15) and (x <= 15 + 5 + rec.Bottom - rec.Top) then begin
old := listview.getAdditionalBookData(item).checked <> csUNCHECKED;
case listview.getAdditionalBookData(item).checked of
csUNCHECKED: listview.getAdditionalBookData(item).checked := csCHECKED;
else listview.getAdditionalBookData(item).checked := csUNCHECKED;
end;
listview.invalidateItem(item);
if item.Parent <> nil then begin
hasChecked := false;
hasUnchecked := false;
counteditem := item.Parent;
old := listview.getAdditionalBookData(item.Parent).checked <> csUnchecked;
for i := 0 to item.Parent.SubItems.Count - 1 do
if listview.getAdditionalBookData(item.Parent.SubItems[i]).checked = csCHECKED then hasChecked := true
else hasUnchecked := true;
if hasChecked and hasUnchecked then listview.getAdditionalBookData(item.parent).checked := csMIXED
else if hasChecked then listview.getAdditionalBookData(item.parent).checked := csCHECKED
else if hasUnchecked then listview.getAdditionalBookData(item.parent).checked := csUNCHECKED;
listview.invalidateItem(item.parent);
end else counteditem := item;
for i := 0 to item.SubItems.Count - 1 do
listview.getAdditionalBookData(item.SubItems[i]).checked := listview.getAdditionalBookData(item).checked;
if (old <> (listview.getAdditionalBookData(counteditem).checked <> csUNCHECKED)) then
if listview.getAdditionalBookData(counteditem).checked = csUNCHECKED then dec(checkedcount)
else inc(checkedcount);
listview.sheduleInternRepaint();
lbCount.Caption := IntToStr(checkedcount) + ' / ' + IntToStr(listview.Items.Count) + ' / ' + IntToStr(totalcount);
end;
end;
procedure TduplicateForm.itemPositioning(sender: TObject; visualColumnIndex: integer; recordItem: TTreeListRecordItem; var aposition: TRect
);
begin
if recordItem.Index = 0 then aposition.Left += aposition.Bottom - aposition.top + 5;
end;
initialization
{$I duplicateremover.lrs}
end.