Remove UNUSED macro
[geeqie.git] / src / dupe.cc
1 /*
2  * Copyright (C) 2005 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: John Ellis
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include <cinttypes>
23
24 #include "main.h"
25 #include "dupe.h"
26
27 #include "cache.h"
28 #include "collect-table.h"
29 #include "dnd.h"
30 #include "filedata.h"
31 #include "history-list.h"
32 #include "image-load.h"
33 #include "img-view.h"
34 #include "layout-image.h"
35 #include "layout-util.h"
36 #include "md5-util.h"
37 #include "menu.h"
38 #include "misc.h"
39 #include "pixbuf-util.h"
40 #include "print.h"
41 #include "thumb.h"
42 #include "ui-fileops.h"
43 #include "ui-menu.h"
44 #include "ui-misc.h"
45 #include "ui-tree-edit.h"
46 #include "uri-utils.h"
47 #include "utilops.h"
48 #include "window.h"
49
50 #include <cmath>
51
52
53 #define DUPE_DEF_WIDTH 800
54 #define DUPE_DEF_HEIGHT 400
55 #define DUPE_PROGRESS_PULSE_STEP 0.0001
56
57 /** column assignment order (simply change them here)
58  */
59 enum {
60         DUPE_COLUMN_POINTER = 0,
61         DUPE_COLUMN_RANK,
62         DUPE_COLUMN_THUMB,
63         DUPE_COLUMN_NAME,
64         DUPE_COLUMN_SIZE,
65         DUPE_COLUMN_DATE,
66         DUPE_COLUMN_DIMENSIONS,
67         DUPE_COLUMN_PATH,
68         DUPE_COLUMN_COLOR,
69         DUPE_COLUMN_SET,
70         DUPE_COLUMN_COUNT       /**< total columns */
71 };
72
73 enum DUPE_CHECK_RESULT {
74         DUPE_MATCH = 0,
75         DUPE_NO_MATCH,
76         DUPE_NAME_MATCH
77 };
78
79 /** Used for similarity checks. One for each item pushed
80  * onto the thread pool.
81  */
82 struct DupeQueueItem
83 {
84         DupeItem *needle;
85         DupeWindow *dw;
86         GList *work; /**< pointer into \a dw->list or \a dw->second_list (#DupeItem) */
87         gint index; /**< The order items pushed onto thread pool. Used to sort returned matches */
88 };
89
90 /** Used for similarity checks thread. One for each pair match found.
91  */
92 struct DupeSearchMatch
93 {
94         DupeItem *a; /**< \a a / \a b matched pair found */
95         DupeItem *b; /**< \a a / \a b matched pair found */
96         gdouble rank;
97         gint index; /**< The order items pushed onto thread pool. Used to sort returned matches */
98 };
99
100 static DupeMatchType param_match_mask;
101 static GList *dupe_window_list = nullptr;       /**< list of open DupeWindow *s */
102
103 /*
104  * Well, after adding the 'compare two sets' option things got a little sloppy in here
105  * because we have to account for two 'modes' everywhere. (be careful).
106  */
107
108 static void dupe_match_unlink(DupeItem *a, DupeItem *b);
109 static DupeItem *dupe_match_find_parent(DupeWindow *dw, DupeItem *child);
110
111 static gint dupe_match(DupeItem *a, DupeItem *b, DupeMatchType mask, gdouble *rank, gint fast);
112
113 static void dupe_thumb_step(DupeWindow *dw);
114 static gint dupe_check_cb(gpointer data);
115
116 static void dupe_second_add(DupeWindow *dw, DupeItem *di);
117 static void dupe_second_remove(DupeWindow *dw, DupeItem *di);
118 static GtkWidget *dupe_menu_popup_second(DupeWindow *dw, DupeItem *di);
119
120 static void dupe_dnd_init(DupeWindow *dw);
121
122 static void dupe_notify_cb(FileData *fd, NotifyType type, gpointer data);
123 static void delete_finished_cb(gboolean success, const gchar *dest_path, gpointer data);
124
125 static GtkWidget *submenu_add_export(GtkWidget *menu, GtkWidget **menu_item, GCallback func, gpointer data);
126 static void dupe_pop_menu_export_cb(GtkWidget *widget, gpointer data);
127
128 static void dupe_init_list_cache(DupeWindow *dw);
129 static void dupe_destroy_list_cache(DupeWindow *dw);
130 static gboolean dupe_insert_in_list_cache(DupeWindow *dw, FileData *fd);
131
132 static void dupe_match_link(DupeItem *a, DupeItem *b, gdouble rank);
133 static gint dupe_match_link_exists(DupeItem *child, DupeItem *parent);
134
135 /**
136  * This array must be kept in sync with the contents of:\n
137  *  @link dupe_window_keypress_cb() @endlink \n
138  *  @link dupe_menu_popup_main() @endlink
139  *
140  * See also @link hard_coded_window_keys @endlink
141  **/
142 hard_coded_window_keys dupe_window_keys[] = {
143         {GDK_CONTROL_MASK, 'C', N_("Copy")},
144         {GDK_CONTROL_MASK, 'M', N_("Move")},
145         {GDK_CONTROL_MASK, 'R', N_("Rename")},
146         {GDK_CONTROL_MASK, 'D', N_("Move to Trash")},
147         {GDK_SHIFT_MASK, GDK_KEY_Delete, N_("Delete")},
148         {static_cast<GdkModifierType>(0), GDK_KEY_Delete, N_("Remove")},
149         {GDK_CONTROL_MASK, GDK_KEY_Delete, N_("Clear")},
150         {GDK_CONTROL_MASK, 'A', N_("Select all")},
151         {static_cast<GdkModifierType>(GDK_CONTROL_MASK + GDK_SHIFT_MASK), 'A', N_("Select none")},
152         {GDK_CONTROL_MASK, 'T', N_("Toggle thumbs")},
153         {GDK_CONTROL_MASK, 'W', N_("Close window")},
154         {static_cast<GdkModifierType>(0), GDK_KEY_Return, N_("View")},
155         {static_cast<GdkModifierType>(0), 'V', N_("View in new window")},
156         {static_cast<GdkModifierType>(0), 'C', N_("Collection from selection")},
157         {GDK_CONTROL_MASK, 'L', N_("Append list")},
158         {static_cast<GdkModifierType>(0), '0', N_("Select none")},
159         {static_cast<GdkModifierType>(0), '1', N_("Select group 1 duplicates")},
160         {static_cast<GdkModifierType>(0), '2', N_("Select group 2 duplicates")},
161         {static_cast<GdkModifierType>(0), 0, nullptr}
162 };
163
164 /**
165  * @brief The function run in threads for similarity checks
166  * @param d1 #DupeQueueItem
167  * @param d2 #DupeWindow
168  *
169  * Used only for similarity checks.\n
170  * Search \a dqi->list for \a dqi->needle and if a match is
171  * found, create a #DupeSearchMatch and add to \a dw->search_matches list\n
172  * If \a dw->abort is set, just increment \a dw->thread_count
173  */
174 static void dupe_comparison_func(gpointer d1, gpointer d2)
175 {
176         auto dqi = static_cast<DupeQueueItem *>(d1);
177         auto dw = static_cast<DupeWindow *>(d2);
178         DupeSearchMatch *dsm;
179         DupeItem *di;
180         GList *matches = nullptr;
181         gdouble rank = 0;
182
183         if (!dw->abort)
184                 {
185                 GList *work = dqi->work;
186                 while (work)
187                         {
188                         di = static_cast<DupeItem *>(work->data);
189
190                         /* forward for second set, back for simple compare */
191                         if (dw->second_set)
192                                 {
193                                 work = work->next;
194                                 }
195                         else
196                                 {
197                                 work = work->prev;
198                                 }
199
200                         if (dupe_match(di, dqi->needle, dqi->dw->match_mask, &rank, TRUE))
201                                 {
202                                 dsm = g_new0(DupeSearchMatch, 1);
203                                 dsm->a = di;
204                                 dsm->b = dqi->needle;
205                                 dsm->rank = rank;
206                                 matches = g_list_prepend(matches, dsm);
207                                 dsm->index = dqi->index;
208                                 }
209
210                         if (dw->abort)
211                                 {
212                                 break;
213                                 }
214                         }
215
216                 matches = g_list_reverse(matches);
217                 g_mutex_lock(&dw->search_matches_mutex);
218                 dw->search_matches = g_list_concat(dw->search_matches, matches);
219                 g_mutex_unlock(&dw->search_matches_mutex);
220                 }
221
222         g_mutex_lock(&dw->thread_count_mutex);
223         dw->thread_count++;
224         g_mutex_unlock(&dw->thread_count_mutex);
225         g_free(dqi);
226 }
227
228 /*
229  * ------------------------------------------------------------------
230  * Window updates
231  * ------------------------------------------------------------------
232  */
233
234 /**
235  * @brief Update display of status label
236  * @param dw
237  * @param count_only
238  *
239  *
240  */
241 static void dupe_window_update_count(DupeWindow *dw, gboolean count_only)
242 {
243         gchar *text;
244
245         if (!dw->list)
246                 {
247                 text = g_strdup(_("Drop files to compare them."));
248                 }
249         else if (count_only)
250                 {
251                 text = g_strdup_printf(_("%d files"), g_list_length(dw->list));
252                 }
253         else
254                 {
255                 text = g_strdup_printf(_("%d matches found in %d files"), g_list_length(dw->dupes), g_list_length(dw->list));
256                 }
257
258         if (dw->second_set)
259                 {
260                 gchar *buf = g_strconcat(text, " ", _("[set 1]"), NULL);
261                 g_free(text);
262                 text = buf;
263                 }
264         gtk_label_set_text(GTK_LABEL(dw->status_label), text);
265
266         g_free(text);
267 }
268
269 /**
270  * @brief Returns time in Âµsec since Epoch
271  * @returns
272  *
273  *
274  */
275 static guint64 msec_time()
276 {
277         struct timeval tv;
278
279         if (gettimeofday(&tv, nullptr) == -1) return 0;
280
281         return static_cast<guint64>(tv.tv_sec) * 1000000 + static_cast<guint64>(tv.tv_usec);
282 }
283
284 static gint dupe_iterations(gint n)
285 {
286         return (n * ((n + 1) / 2));
287 }
288
289 /**
290  * @brief
291  * @param dw
292  * @param status
293  * @param value
294  * @param force
295  *
296  * If \a status is blank, clear status bar text and set progress to zero. \n
297  * If \a force is not set, after 2 secs has elapsed, update time-to-go every 250 ms.
298  */
299 static void dupe_window_update_progress(DupeWindow *dw, const gchar *status, gdouble value, gboolean force)
300 {
301         const gchar *status_text;
302
303         if (status)
304                 {
305                 guint64 new_time = 0;
306
307                 if (dw->setup_n % 10 == 0)
308                         {
309                         new_time = msec_time() - dw->setup_time;
310                         }
311
312                 if (!force &&
313                     value != 0.0 &&
314                     dw->setup_count > 0 &&
315                     new_time > 2000000)
316                         {
317                         gchar *buf;
318                         gint t;
319                         gint d;
320                         guint32 rem;
321
322                         if (new_time - dw->setup_time_count < 250000) return;
323                         dw->setup_time_count = new_time;
324
325                         if (dw->setup_done)
326                                 {
327                                 if (dw->second_set)
328                                         {
329                                         t = dw->setup_count;
330                                         d = dw->setup_count - dw->setup_n;
331                                         }
332                                 else
333                                         {
334                                         t = dupe_iterations(dw->setup_count);
335                                         d = dupe_iterations(dw->setup_count - dw->setup_n);
336                                         }
337                                 }
338                         else
339                                 {
340                                 t = dw->setup_count;
341                                 d = dw->setup_count - dw->setup_n;
342                                 }
343
344                         rem = (t - d) ? (static_cast<gdouble>(dw->setup_time_count / 1000000) / (t - d)) * d : 0;
345
346                         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(dw->extra_label), value);
347
348                         buf = g_strdup_printf("%s %d:%02d ", status, rem / 60, rem % 60);
349                         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(dw->extra_label), buf);
350                         g_free(buf);
351
352                         return;
353                         }
354                 else if (force ||
355                          value == 0.0 ||
356                          dw->setup_count == 0 ||
357                          dw->setup_time_count == 0 ||
358                          (new_time > 0 && new_time - dw->setup_time_count >= 250000))
359                         {
360                         if (dw->setup_time_count == 0) dw->setup_time_count = 1;
361                         if (new_time > 0) dw->setup_time_count = new_time;
362                         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(dw->extra_label), value);
363                         status_text = status;
364                         }
365                 else
366                         {
367                         status_text = nullptr;
368                         }
369                 }
370         else
371                 {
372                 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(dw->extra_label), 0.0);
373                 status_text = " ";
374                 }
375
376         if (status_text) gtk_progress_bar_set_text(GTK_PROGRESS_BAR(dw->extra_label), status_text);
377 }
378
379 static void widget_set_cursor(GtkWidget *widget, gint icon)
380 {
381         GdkCursor *cursor;
382
383         if (!gtk_widget_get_window(widget)) return;
384
385         if (icon == -1)
386                 {
387                 cursor = nullptr;
388                 }
389         else
390                 {
391                 cursor = gdk_cursor_new(static_cast<GdkCursorType>(icon));
392                 }
393
394         gdk_window_set_cursor(gtk_widget_get_window(widget), cursor);
395
396         if (cursor) g_object_unref(G_OBJECT(cursor));
397 }
398
399 /*
400  * ------------------------------------------------------------------
401  * row color utils
402  * ------------------------------------------------------------------
403  */
404
405 static void dupe_listview_realign_colors(DupeWindow *dw)
406 {
407         GtkTreeModel *store;
408         GtkTreeIter iter;
409         gboolean color_set = TRUE;
410         DupeItem *parent = nullptr;
411         gboolean valid;
412
413         store = gtk_tree_view_get_model(GTK_TREE_VIEW(dw->listview));
414         valid = gtk_tree_model_get_iter_first(store, &iter);
415         while (valid)
416                 {
417                 DupeItem *child;
418                 DupeItem *child_parent;
419
420                 gtk_tree_model_get(store, &iter, DUPE_COLUMN_POINTER, &child, -1);
421                 child_parent = dupe_match_find_parent(dw, child);
422                 if (!parent || parent != child_parent)
423                         {
424                         if (!parent)
425                                 {
426                                 /* keep the first row as it is */
427                                 gtk_tree_model_get(store, &iter, DUPE_COLUMN_COLOR, &color_set, -1);
428                                 }
429                         else
430                                 {
431                                 color_set = !color_set;
432                                 }
433                         parent = dupe_match_find_parent(dw, child);
434                         }
435                 gtk_list_store_set(GTK_LIST_STORE(store), &iter, DUPE_COLUMN_COLOR, color_set, -1);
436
437                 valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
438                 }
439 }
440
441 /*
442  * ------------------------------------------------------------------
443  * Dupe item utils
444  * ------------------------------------------------------------------
445  */
446
447 static DupeItem *dupe_item_new(FileData *fd)
448 {
449         DupeItem *di;
450
451         di = g_new0(DupeItem, 1);
452
453         di->fd = file_data_ref(fd);
454         di->group_rank = 0.0;
455
456         return di;
457 }
458
459 static void dupe_item_free(DupeItem *di)
460 {
461         file_data_unref(di->fd);
462         image_sim_free(di->simd);
463         g_free(di->md5sum);
464         if (di->pixbuf) g_object_unref(di->pixbuf);
465
466         g_free(di);
467 }
468
469 #pragma GCC diagnostic push
470 #pragma GCC diagnostic ignored "-Wunused-function"
471 static DupeItem *dupe_item_find_fd_by_list_unused(FileData *fd, GList *work)
472 {
473         while (work)
474                 {
475                 DupeItem *di = static_cast<DupeItem *>(work->data);
476
477                 if (di->fd == fd) return di;
478
479                 work = work->next;
480                 }
481
482         return NULL;
483 }
484
485 static DupeItem *dupe_item_find_fd_unused(DupeWindow *dw, FileData *fd)
486 {
487         DupeItem *di;
488
489         di = dupe_item_find_fd_by_list_unused(fd, dw->list);
490         if (!di && dw->second_set) di = dupe_item_find_fd_by_list_unused(fd, dw->second_list);
491
492         return di;
493 }
494
495 static DupeItem *dupe_item_find_path_by_list_unused(const gchar *path, GList *work)
496 {
497         while (work)
498                 {
499                 DupeItem *di = static_cast<DupeItem *>(work->data);
500
501                 if (strcmp(di->fd->path, path) == 0) return di;
502
503                 work = work->next;
504                 }
505
506         return NULL;
507 }
508
509 static DupeItem *dupe_item_find_path_unused(DupeWindow *dw, const gchar *path)
510 {
511         DupeItem *di;
512
513         di = dupe_item_find_path_by_list_unused(path, dw->list);
514         if (!di && dw->second_set) di = dupe_item_find_path_by_list_unused(path, dw->second_list);
515
516         return di;
517 }
518 #pragma GCC diagnostic pop
519
520 /*
521  * ------------------------------------------------------------------
522  * Image property cache
523  * ------------------------------------------------------------------
524  */
525
526 static void dupe_item_read_cache(DupeItem *di)
527 {
528         gchar *path;
529         CacheData *cd;
530
531         if (!di) return;
532
533         path = cache_find_location(CACHE_TYPE_SIM, di->fd->path);
534         if (!path) return;
535
536         if (filetime(di->fd->path) != filetime(path))
537                 {
538                 g_free(path);
539                 return;
540                 }
541
542         cd = cache_sim_data_load(path);
543         g_free(path);
544
545         if (cd)
546                 {
547                 if (!di->simd && cd->sim)
548                         {
549                         di->simd = cd->sim;
550                         cd->sim = nullptr;
551                         }
552                 if (di->width == 0 && di->height == 0 && cd->dimensions)
553                         {
554                         di->width = cd->width;
555                         di->height = cd->height;
556                         di->dimensions = (di->width << 16) + di->height;
557                         }
558                 if (!di->md5sum && cd->have_md5sum)
559                         {
560                         di->md5sum = md5_digest_to_text(cd->md5sum);
561                         }
562                 cache_sim_data_free(cd);
563                 }
564 }
565
566 static void dupe_item_write_cache(DupeItem *di)
567 {
568         gchar *base;
569         mode_t mode = 0755;
570
571         if (!di) return;
572
573         base = cache_get_location(CACHE_TYPE_SIM, di->fd->path, FALSE, &mode);
574         if (recursive_mkdir_if_not_exists(base, mode))
575                 {
576                 CacheData *cd;
577
578                 cd = cache_sim_data_new();
579                 cd->path = cache_get_location(CACHE_TYPE_SIM, di->fd->path, TRUE, nullptr);
580
581                 if (di->width != 0) cache_sim_data_set_dimensions(cd, di->width, di->height);
582                 if (di->md5sum)
583                         {
584                         guchar digest[16];
585                         if (md5_digest_from_text(di->md5sum, digest)) cache_sim_data_set_md5sum(cd, digest);
586                         }
587                 if (di->simd) cache_sim_data_set_similarity(cd, di->simd);
588
589                 if (cache_sim_data_save(cd))
590                         {
591                         filetime_set(cd->path, filetime(di->fd->path));
592                         }
593                 cache_sim_data_free(cd);
594                 }
595         g_free(base);
596 }
597
598 /*
599  * ------------------------------------------------------------------
600  * Window list utils
601  * ------------------------------------------------------------------
602  */
603
604 static gint dupe_listview_find_item(GtkListStore *store, DupeItem *item, GtkTreeIter *iter)
605 {
606         gboolean valid;
607         gint row = 0;
608
609         valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), iter);
610         while (valid)
611                 {
612                 DupeItem *item_n;
613                 gtk_tree_model_get(GTK_TREE_MODEL(store), iter, DUPE_COLUMN_POINTER, &item_n, -1);
614                 if (item_n == item) return row;
615
616                 valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), iter);
617                 row++;
618                 }
619
620         return -1;
621 }
622
623 static void dupe_listview_add(DupeWindow *dw, DupeItem *parent, DupeItem *child)
624 {
625         DupeItem *di;
626         gint row;
627         gchar *text[DUPE_COLUMN_COUNT];
628         GtkListStore *store;
629         GtkTreeIter iter;
630         gboolean color_set = FALSE;
631         gint rank;
632
633         if (!parent) return;
634
635         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(dw->listview)));
636
637         if (child)
638                 {
639                 DupeMatch *dm;
640
641                 row = dupe_listview_find_item(store, parent, &iter);
642                 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, DUPE_COLUMN_COLOR, &color_set, -1);
643
644                 row++;
645
646                 if (child->group)
647                         {
648                         dm = static_cast<DupeMatch *>(child->group->data);
649                         rank = static_cast<gint>(floor(dm->rank));
650                         }
651                 else
652                         {
653                         rank = 1;
654                         log_printf("NULL group in item!\n");
655                         }
656                 }
657         else
658                 {
659                 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter))
660                         {
661                         gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, DUPE_COLUMN_COLOR, &color_set, -1);
662                         color_set = !color_set;
663                         dw->set_count++;
664                         }
665                 else
666                         {
667                         color_set = FALSE;
668                         }
669                 row = 0;
670                 rank = 0;
671                 }
672
673         di = (child) ? child : parent;
674
675         if (!child && dw->second_set)
676                 {
677                 text[DUPE_COLUMN_RANK] = g_strdup("[1]");
678                 }
679         else if (rank == 0)
680                 {
681                 text[DUPE_COLUMN_RANK] = g_strdup((di->second) ? "(2)" : "");
682                 }
683         else
684                 {
685                 text[DUPE_COLUMN_RANK] = g_strdup_printf("%d%s", rank, (di->second) ? " (2)" : "");
686                 }
687
688         text[DUPE_COLUMN_THUMB] = nullptr;
689         text[DUPE_COLUMN_NAME] = const_cast<gchar *>(di->fd->name);
690         text[DUPE_COLUMN_SIZE] = text_from_size(di->fd->size);
691         text[DUPE_COLUMN_DATE] = const_cast<gchar *>(text_from_time(di->fd->date));
692         if (di->width > 0 && di->height > 0)
693                 {
694                 text[DUPE_COLUMN_DIMENSIONS] = g_strdup_printf("%d x %d", di->width, di->height);
695                 }
696         else
697                 {
698                 text[DUPE_COLUMN_DIMENSIONS] = g_strdup("");
699                 }
700         text[DUPE_COLUMN_PATH] = di->fd->path;
701         text[DUPE_COLUMN_COLOR] = nullptr;
702
703         gtk_list_store_insert(store, &iter, row);
704         gtk_list_store_set(store, &iter,
705                                 DUPE_COLUMN_POINTER, di,
706                                 DUPE_COLUMN_RANK, text[DUPE_COLUMN_RANK],
707                                 DUPE_COLUMN_THUMB, NULL,
708                                 DUPE_COLUMN_NAME, text[DUPE_COLUMN_NAME],
709                                 DUPE_COLUMN_SIZE, text[DUPE_COLUMN_SIZE],
710                                 DUPE_COLUMN_DATE, text[DUPE_COLUMN_DATE],
711                                 DUPE_COLUMN_DIMENSIONS, text[DUPE_COLUMN_DIMENSIONS],
712                                 DUPE_COLUMN_PATH, text[DUPE_COLUMN_PATH],
713                                 DUPE_COLUMN_COLOR, color_set,
714                                 DUPE_COLUMN_SET, dw->set_count,
715                                 -1);
716
717         g_free(text[DUPE_COLUMN_RANK]);
718         g_free(text[DUPE_COLUMN_SIZE]);
719         g_free(text[DUPE_COLUMN_DIMENSIONS]);
720 }
721
722 static void dupe_listview_select_dupes(DupeWindow *dw, DupeSelectType parents);
723
724 static void dupe_listview_populate(DupeWindow *dw)
725 {
726         GtkListStore *store;
727         GList *work;
728
729         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(dw->listview)));
730         gtk_list_store_clear(store);
731
732         work = g_list_last(dw->dupes);
733         while (work)
734                 {
735                 auto parent = static_cast<DupeItem *>(work->data);
736                 GList *temp;
737
738                 dupe_listview_add(dw, parent, nullptr);
739
740                 temp = g_list_last(parent->group);
741                 while (temp)
742                         {
743                         auto dm = static_cast<DupeMatch *>(temp->data);
744                         DupeItem *child;
745
746                         child = dm->di;
747
748                         dupe_listview_add(dw, parent, child);
749
750                         temp = temp->prev;
751                         }
752
753                 work = work->prev;
754                 }
755
756         gtk_tree_view_columns_autosize(GTK_TREE_VIEW(dw->listview));
757
758         if (options->duplicates_select_type == DUPE_SELECT_GROUP1)
759                 {
760                 dupe_listview_select_dupes(dw, DUPE_SELECT_GROUP1);
761                 }
762         else if (options->duplicates_select_type == DUPE_SELECT_GROUP2)
763                 {
764                 dupe_listview_select_dupes(dw, DUPE_SELECT_GROUP2);
765                 }
766
767 }
768
769 static void dupe_listview_remove(DupeWindow *dw, DupeItem *di)
770 {
771         GtkListStore *store;
772         GtkTreeIter iter;
773         gint row;
774
775         if (!di) return;
776
777         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(dw->listview)));
778         row = dupe_listview_find_item(store, di, &iter);
779         if (row < 0) return;
780
781         tree_view_move_cursor_away(GTK_TREE_VIEW(dw->listview), &iter, TRUE);
782         gtk_list_store_remove(store, &iter);
783
784         if (g_list_find(dw->dupes, di) != nullptr)
785                 {
786                 if (!dw->color_frozen) dupe_listview_realign_colors(dw);
787                 }
788 }
789
790
791 static GList *dupe_listview_get_filelist(DupeWindow *, GtkWidget *listview)
792 {
793         GtkTreeModel *store;
794         GtkTreeIter iter;
795         gboolean valid;
796         GList *list = nullptr;
797
798         store = gtk_tree_view_get_model(GTK_TREE_VIEW(listview));
799         valid = gtk_tree_model_get_iter_first(store, &iter);
800         while (valid)
801                 {
802                 DupeItem *di;
803                 gtk_tree_model_get(store, &iter, DUPE_COLUMN_POINTER, &di, -1);
804                 list = g_list_prepend(list, file_data_ref(di->fd));
805
806                 valid = gtk_tree_model_iter_next(store, &iter);
807                 }
808
809         return g_list_reverse(list);
810 }
811
812
813 static GList *dupe_listview_get_selection(DupeWindow *, GtkWidget *listview)
814 {
815         GtkTreeModel *store;
816         GtkTreeSelection *selection;
817         GList *slist;
818         GList *list = nullptr;
819         GList *work;
820
821         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(listview));
822         slist = gtk_tree_selection_get_selected_rows(selection, &store);
823         work = slist;
824         while (work)
825                 {
826                 auto tpath = static_cast<GtkTreePath *>(work->data);
827                 DupeItem *di = nullptr;
828                 GtkTreeIter iter;
829
830                 gtk_tree_model_get_iter(store, &iter, tpath);
831                 gtk_tree_model_get(store, &iter, DUPE_COLUMN_POINTER, &di, -1);
832                 if (di)
833                         {
834                         list = g_list_prepend(list, file_data_ref(di->fd));
835                         }
836                 work = work->next;
837                 }
838         g_list_free_full(slist, reinterpret_cast<GDestroyNotify>(gtk_tree_path_free));
839
840         return g_list_reverse(list);
841 }
842
843 static gboolean dupe_listview_item_is_selected(DupeWindow *, DupeItem *di, GtkWidget *listview)
844 {
845         GtkTreeModel *store;
846         GtkTreeSelection *selection;
847         GList *slist;
848         GList *work;
849         gboolean found = FALSE;
850
851         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(listview));
852         slist = gtk_tree_selection_get_selected_rows(selection, &store);
853         work = slist;
854         while (!found && work)
855                 {
856                 auto tpath = static_cast<GtkTreePath *>(work->data);
857                 DupeItem *di_n;
858                 GtkTreeIter iter;
859
860                 gtk_tree_model_get_iter(store, &iter, tpath);
861                 gtk_tree_model_get(store, &iter, DUPE_COLUMN_POINTER, &di_n, -1);
862                 if (di_n == di) found = TRUE;
863                 work = work->next;
864                 }
865         g_list_free_full(slist, reinterpret_cast<GDestroyNotify>(gtk_tree_path_free));
866
867         return found;
868 }
869
870 static void dupe_listview_select_dupes(DupeWindow *dw, DupeSelectType parents)
871 {
872         GtkTreeModel *store;
873         GtkTreeSelection *selection;
874         GtkTreeIter iter;
875         gboolean valid;
876         gint set_count = 0;
877         gint set_count_last = -1;
878
879         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dw->listview));
880         gtk_tree_selection_unselect_all(selection);
881
882         store = gtk_tree_view_get_model(GTK_TREE_VIEW(dw->listview));
883         valid = gtk_tree_model_get_iter_first(store, &iter);
884         while (valid)
885                 {
886                 DupeItem *di;
887
888                 gtk_tree_model_get(store, &iter, DUPE_COLUMN_POINTER, &di, DUPE_COLUMN_SET, &set_count, -1);
889                 if (set_count != set_count_last)
890                         {
891                         set_count_last = set_count;
892                         if (parents == DUPE_SELECT_GROUP1)
893                                 {
894                                 gtk_tree_selection_select_iter(selection, &iter);
895                                 }
896                         }
897                 else
898                         {
899                         if (parents == DUPE_SELECT_GROUP2)
900                                 {
901                                 gtk_tree_selection_select_iter(selection, &iter);
902                                 }
903                         }
904                 valid = gtk_tree_model_iter_next(store, &iter);
905                 }
906 }
907
908 /*
909  * ------------------------------------------------------------------
910  * Match group manipulation
911  * ------------------------------------------------------------------
912  */
913
914 /**
915  * @brief Search \a parent->group for \a child (#DupeItem)
916  * @param child
917  * @param parent
918  * @returns
919  *
920  */
921 static DupeMatch *dupe_match_find_match(DupeItem *child, DupeItem *parent)
922 {
923         GList *work;
924
925         work = parent->group;
926         while (work)
927                 {
928                 auto dm = static_cast<DupeMatch *>(work->data);
929                 if (dm->di == child) return dm;
930                 work = work->next;
931                 }
932         return nullptr;
933 }
934
935 /**
936  * @brief Create #DupeMatch structure for \a child, and insert into \a parent->group list.
937  * @param child
938  * @param parent
939  * @param rank
940  *
941  */
942 static void dupe_match_link_child(DupeItem *child, DupeItem *parent, gdouble rank)
943 {
944         DupeMatch *dm;
945
946         dm = g_new0(DupeMatch, 1);
947         dm->di = child;
948         dm->rank = rank;
949         parent->group = g_list_append(parent->group, dm);
950 }
951
952 /**
953  * @brief Link \a a & \a b as both parent and child
954  * @param a
955  * @param b
956  * @param rank
957  *
958  * Link \a a as child of \a b, and \a b as child of \a a
959  */
960 static void dupe_match_link(DupeItem *a, DupeItem *b, gdouble rank)
961 {
962         dupe_match_link_child(a, b, rank);
963         dupe_match_link_child(b, a, rank);
964 }
965
966 /**
967  * @brief Remove \a child #DupeMatch from \a parent->group list.
968  * @param child
969  * @param parent
970  *
971  */
972 static void dupe_match_unlink_child(DupeItem *child, DupeItem *parent)
973 {
974         DupeMatch *dm;
975
976         dm = dupe_match_find_match(child, parent);
977         if (dm)
978                 {
979                 parent->group = g_list_remove(parent->group, dm);
980                 g_free(dm);
981                 }
982 }
983
984 /**
985  * @brief  Unlink \a a from \a b, and \a b from \a a
986  * @param a
987  * @param b
988  *
989  * Free the relevant #DupeMatch items from the #DupeItem group lists
990  */
991 static void dupe_match_unlink(DupeItem *a, DupeItem *b)
992 {
993         dupe_match_unlink_child(a, b);
994         dupe_match_unlink_child(b, a);
995 }
996
997 /**
998  * @brief
999  * @param parent
1000  * @param unlink_children
1001  *
1002  * If \a unlink_children is set, unlink all entries in \a parent->group list. \n
1003  * Free the \a parent->group list and set group_rank to zero;
1004  */
1005 static void dupe_match_link_clear(DupeItem *parent, gboolean unlink_children)
1006 {
1007         if (unlink_children)
1008                 {
1009                 GList *work;
1010
1011                 work = parent->group;
1012                 while (work)
1013                         {
1014                         auto dm = static_cast<DupeMatch *>(work->data);
1015                         work = work->next;
1016
1017                         dupe_match_unlink_child(parent, dm->di);
1018                         }
1019                 }
1020
1021         g_list_free_full(parent->group, g_free);
1022         parent->group = nullptr;
1023         parent->group_rank = 0.0;
1024 }
1025
1026 /**
1027  * @brief Search \a parent->group list for \a child
1028  * @param child
1029  * @param parent
1030  * @returns boolean TRUE/FALSE found/not found
1031  *
1032  */
1033 static gint dupe_match_link_exists(DupeItem *child, DupeItem *parent)
1034 {
1035         return (dupe_match_find_match(child, parent) != nullptr);
1036 }
1037
1038 /**
1039  * @brief  Search \a parent->group for \a child, and return \a child->rank
1040  * @param child
1041  * @param parent
1042  * @returns \a dm->di->rank
1043  *
1044  */
1045 static gdouble dupe_match_link_rank(DupeItem *child, DupeItem *parent)
1046 {
1047         DupeMatch *dm;
1048
1049         dm = dupe_match_find_match(child, parent);
1050         if (dm) return dm->rank;
1051
1052         return 0.0;
1053 }
1054
1055 /**
1056  * @brief Find highest rank in \a child->group
1057  * @param child
1058  * @returns
1059  *
1060  * Search the #DupeMatch entries in the \a child->group list.
1061  * Return the #DupeItem with the highest rank. If more than one have
1062  * the same rank, the first encountered is used.
1063  */
1064 static DupeItem *dupe_match_highest_rank(DupeItem *child)
1065 {
1066         DupeMatch *dr;
1067         GList *work;
1068
1069         dr = nullptr;
1070         work = child->group;
1071         while (work)
1072                 {
1073                 auto dm = static_cast<DupeMatch *>(work->data);
1074                 if (!dr || dm->rank > dr->rank)
1075                         {
1076                         dr = dm;
1077                         }
1078                 work = work->next;
1079                 }
1080
1081         return (dr) ? dr->di : nullptr;
1082 }
1083
1084 /**
1085  * @brief Compute and store \a parent->group_rank
1086  * @param parent
1087  *
1088  * Group_rank = (sum of all child ranks) / n
1089  */
1090 static void dupe_match_rank_update(DupeItem *parent)
1091 {
1092         GList *work;
1093         gdouble rank = 0.0;
1094         gint c = 0;
1095
1096         work = parent->group;
1097         while (work)
1098                 {
1099                 auto dm = static_cast<DupeMatch *>(work->data);
1100                 work = work->next;
1101                 rank += dm->rank;
1102                 c++;
1103                 }
1104
1105         if (c > 0)
1106                 {
1107                 parent->group_rank = rank / c;
1108                 }
1109         else
1110                 {
1111                 parent->group_rank = 0.0;
1112                 }
1113 }
1114
1115 static DupeItem *dupe_match_find_parent(DupeWindow *dw, DupeItem *child)
1116 {
1117         GList *work;
1118
1119         if (g_list_find(dw->dupes, child)) return child;
1120
1121         work = child->group;
1122         while (work)
1123                 {
1124                 auto dm = static_cast<DupeMatch *>(work->data);
1125                 if (g_list_find(dw->dupes, dm->di)) return dm->di;
1126                 work = work->next;
1127                 }
1128
1129         return nullptr;
1130 }
1131
1132 /**
1133  * @brief
1134  * @param work (#DupeItem) dw->list or dw->second_list
1135  *
1136  * Unlink all #DupeItem-s in \a work.
1137  * Do not unlink children.
1138  */
1139 static void dupe_match_reset_list(GList *work)
1140 {
1141         while (work)
1142                 {
1143                 auto di = static_cast<DupeItem *>(work->data);
1144                 work = work->next;
1145
1146                 dupe_match_link_clear(di, FALSE);
1147                 }
1148 }
1149
1150 static void dupe_match_reparent(DupeWindow *dw, DupeItem *old_parent, DupeItem *new_parent)
1151 {
1152         GList *work;
1153
1154         if (!old_parent || !new_parent || !dupe_match_link_exists(old_parent, new_parent)) return;
1155
1156         dupe_match_link_clear(new_parent, TRUE);
1157         work = old_parent->group;
1158         while (work)
1159                 {
1160                 auto dm = static_cast<DupeMatch *>(work->data);
1161                 dupe_match_unlink_child(old_parent, dm->di);
1162                 dupe_match_link_child(new_parent, dm->di, dm->rank);
1163                 work = work->next;
1164                 }
1165
1166         new_parent->group = old_parent->group;
1167         old_parent->group = nullptr;
1168
1169         work = g_list_find(dw->dupes, old_parent);
1170         if (work) work->data = new_parent;
1171 }
1172
1173 static void dupe_match_print_group(DupeItem *di)
1174 {
1175         GList *work;
1176
1177         log_printf("+ %f %s\n", di->group_rank, di->fd->name);
1178
1179         work = di->group;
1180         while (work)
1181                 {
1182                 auto dm = static_cast<DupeMatch *>(work->data);
1183                 work = work->next;
1184
1185                 log_printf("  %f %s\n", dm->rank, dm->di->fd->name);
1186                 }
1187
1188         log_printf("\n");
1189 }
1190
1191 static void dupe_match_print_list(GList *list)
1192 {
1193         GList *work;
1194
1195         work = list;
1196         while (work)
1197                 {
1198                 auto di = static_cast<DupeItem *>(work->data);
1199                 dupe_match_print_group(di);
1200                 work = work->next;
1201                 }
1202 }
1203
1204 /* level 3, unlinking and orphan handling */
1205 /**
1206  * @brief
1207  * @param child
1208  * @param parent \a di from \a child->group
1209  * @param[inout] list \a dw->list sorted by rank (#DupeItem)
1210  * @param dw
1211  * @returns modified \a list
1212  *
1213  * Called for each entry in \a child->group (#DupeMatch) with \a parent set to \a dm->di. \n
1214  * Find the highest rank #DupeItem of the \a parent's children. \n
1215  * If that is == \a child OR
1216  * highest rank #DupeItem of \a child == \a parent then FIXME:
1217  *
1218  */
1219 static GList *dupe_match_unlink_by_rank(DupeItem *child, DupeItem *parent, GList *list, DupeWindow *dw)
1220 {
1221         DupeItem *best = nullptr;
1222
1223         best = dupe_match_highest_rank(parent); // highest rank in parent->group
1224         if (best == child || dupe_match_highest_rank(child) == parent)
1225                 {
1226                 GList *work;
1227                 gdouble rank;
1228
1229                 DEBUG_2("link found %s to %s [%d]", child->fd->name, parent->fd->name, g_list_length(parent->group));
1230
1231                 work = parent->group;
1232                 while (work)
1233                         {
1234                         auto dm = static_cast<DupeMatch *>(work->data);
1235                         DupeItem *orphan;
1236
1237                         work = work->next;
1238                         orphan = dm->di;
1239                         if (orphan != child && g_list_length(orphan->group) < 2)
1240                                 {
1241                                 dupe_match_link_clear(orphan, TRUE);
1242                                 if (!dw->second_set || orphan->second)
1243                                         {
1244                                         dupe_match(orphan, child, dw->match_mask, &rank, FALSE);
1245                                         dupe_match_link(orphan, child, rank);
1246                                         }
1247                                 list = g_list_remove(list, orphan);
1248                                 }
1249                         }
1250
1251                 rank = dupe_match_link_rank(child, parent); // child->rank
1252                 dupe_match_link_clear(parent, TRUE);
1253                 dupe_match_link(child, parent, rank);
1254                 list = g_list_remove(list, parent);
1255                 }
1256         else
1257                 {
1258                 DEBUG_2("unlinking %s and %s", child->fd->name, parent->fd->name);
1259
1260                 dupe_match_unlink(child, parent);
1261                 }
1262
1263         return list;
1264 }
1265
1266 /* level 2 */
1267 /**
1268  * @brief
1269  * @param[inout] list \a dw->list sorted by rank (#DupeItem)
1270  * @param di
1271  * @param dw
1272  * @returns modified \a list
1273  *
1274  * Called for each entry in \a list.
1275  * Call unlink for each child in \a di->group
1276  */
1277 static GList *dupe_match_group_filter(GList *list, DupeItem *di, DupeWindow *dw)
1278 {
1279         GList *work;
1280
1281         work = g_list_last(di->group);
1282         while (work)
1283                 {
1284                 auto dm = static_cast<DupeMatch *>(work->data);
1285                 work = work->prev;
1286                 list = dupe_match_unlink_by_rank(di, dm->di, list, dw);
1287                 }
1288
1289         return list;
1290 }
1291
1292 /* level 1 (top) */
1293 /**
1294  * @brief
1295  * @param[inout] list \a dw->list sorted by rank (#DupeItem)
1296  * @param dw
1297  * @returns Filtered \a list
1298  *
1299  * Called once.
1300  * Call group filter for each \a di in \a list
1301  */
1302 static GList *dupe_match_group_trim(GList *list, DupeWindow *dw)
1303 {
1304         GList *work;
1305
1306         work = list;
1307         while (work)
1308                 {
1309                 auto di = static_cast<DupeItem *>(work->data);
1310                 if (!di->second) list = dupe_match_group_filter(list, di, dw);
1311                 work = work->next;
1312                 if (di->second) list = g_list_remove(list, di);
1313                 }
1314
1315         return list;
1316 }
1317
1318 static gint dupe_match_sort_groups_cb(gconstpointer a, gconstpointer b)
1319 {
1320         auto da = static_cast<const DupeMatch *>(a);
1321         auto db = static_cast<const DupeMatch *>(b);
1322
1323         if (da->rank > db->rank) return -1;
1324         if (da->rank < db->rank) return 1;
1325         return 0;
1326 }
1327
1328 /**
1329  * @brief Sorts the children of each #DupeItem in \a list
1330  * @param list #DupeItem
1331  *
1332  * Sorts the #DupeItem->group children on rank
1333  */
1334 static void dupe_match_sort_groups(GList *list)
1335 {
1336         GList *work;
1337
1338         work = list;
1339         while (work)
1340                 {
1341                 auto di = static_cast<DupeItem *>(work->data);
1342                 di->group = g_list_sort(di->group, dupe_match_sort_groups_cb);
1343                 work = work->next;
1344                 }
1345 }
1346
1347 static gint dupe_match_totals_sort_cb(gconstpointer a, gconstpointer b)
1348 {
1349         auto da = static_cast<const DupeItem *>(a);
1350         auto db = static_cast<const DupeItem *>(b);
1351
1352         if (g_list_length(da->group) > g_list_length(db->group)) return -1;
1353         if (g_list_length(da->group) < g_list_length(db->group)) return 1;
1354
1355         if (da->group_rank < db->group_rank) return -1;
1356         if (da->group_rank > db->group_rank) return 1;
1357
1358         return 0;
1359 }
1360
1361 /**
1362  * @brief Callback for group_rank sort
1363  * @param a
1364  * @param b
1365  * @returns
1366  *
1367  *
1368  */
1369 static gint dupe_match_rank_sort_cb(gconstpointer a, gconstpointer b)
1370 {
1371         auto da = static_cast<const DupeItem *>(a);
1372         auto db = static_cast<const DupeItem *>(b);
1373
1374         if (da->group_rank > db->group_rank) return -1;
1375         if (da->group_rank < db->group_rank) return 1;
1376         return 0;
1377 }
1378
1379 /**
1380  * @brief Sorts \a source_list by group-rank
1381  * @param source_list #DupeItem
1382  * @returns
1383  *
1384  * Computes group_rank for each #DupeItem. \n
1385  * Items with no group list are ignored.
1386  * Returns allocated GList of #DupeItem-s sorted by group_rank
1387  */
1388 static GList *dupe_match_rank_sort(GList *source_list)
1389 {
1390         GList *list = nullptr;
1391         GList *work;
1392
1393         work = source_list;
1394         while (work)
1395                 {
1396                 auto di = static_cast<DupeItem *>(work->data);
1397
1398                 if (di->group)
1399                         {
1400                         dupe_match_rank_update(di); // Compute and store group_rank for di
1401                         list = g_list_prepend(list, di);
1402                         }
1403
1404                 work = work->next;
1405                 }
1406
1407         return g_list_sort(list, dupe_match_rank_sort_cb);
1408 }
1409
1410 /**
1411  * @brief Returns allocated GList of dupes sorted by totals
1412  * @param source_list
1413  * @returns
1414  *
1415  *
1416  */
1417 static GList *dupe_match_totals_sort(GList *source_list)
1418 {
1419         source_list = g_list_sort(source_list, dupe_match_totals_sort_cb);
1420
1421         source_list = g_list_first(source_list);
1422         return g_list_reverse(source_list);
1423 }
1424
1425 /**
1426  * @brief
1427  * @param dw
1428  *
1429  * Called once.
1430  */
1431 static void dupe_match_rank(DupeWindow *dw)
1432 {
1433         GList *list;
1434
1435         list = dupe_match_rank_sort(dw->list); // sorted by group_rank, no-matches filtered out
1436
1437         if (required_debug_level(2)) dupe_match_print_list(list);
1438
1439         DEBUG_1("Similar items: %d", g_list_length(list));
1440         list = dupe_match_group_trim(list, dw);
1441         DEBUG_1("Unique groups: %d", g_list_length(list));
1442
1443         dupe_match_sort_groups(list);
1444
1445         if (required_debug_level(2)) dupe_match_print_list(list);
1446
1447         list = dupe_match_rank_sort(list);
1448         if (options->sort_totals)
1449                 {
1450                 list = dupe_match_totals_sort(list);
1451                 }
1452         if (required_debug_level(2)) dupe_match_print_list(list);
1453
1454         g_list_free(dw->dupes);
1455         dw->dupes = list;
1456 }
1457
1458 /*
1459  * ------------------------------------------------------------------
1460  * Match group tests
1461  * ------------------------------------------------------------------
1462  */
1463
1464 /**
1465  * @brief
1466  * @param[in] a
1467  * @param[in] b
1468  * @param[in] mask
1469  * @param[out] rank
1470  * @param[in] fast
1471  * @returns
1472  *
1473  * For similarity checks, compute rank - (similarity factor between a and b). \n
1474  * If rank < user-set sim value, returns FALSE.
1475  */
1476 static gboolean dupe_match(DupeItem *a, DupeItem *b, DupeMatchType mask, gdouble *rank, gint fast)
1477 {
1478         *rank = 0.0;
1479
1480         if (a->fd->path == b->fd->path) return FALSE;
1481
1482         if (mask & DUPE_MATCH_ALL)
1483                 {
1484                 return TRUE;
1485                 }
1486         if (mask & DUPE_MATCH_PATH)
1487                 {
1488                 if (utf8_compare(a->fd->path, b->fd->path, TRUE) != 0) return FALSE;
1489                 }
1490         if (mask & DUPE_MATCH_NAME)
1491                 {
1492                 if (strcmp(a->fd->collate_key_name, b->fd->collate_key_name) != 0) return FALSE;
1493                 }
1494         if (mask & DUPE_MATCH_NAME_CI)
1495                 {
1496                 if (strcmp(a->fd->collate_key_name_nocase, b->fd->collate_key_name_nocase) != 0) return FALSE;
1497                 }
1498         if (mask & DUPE_MATCH_NAME_CONTENT)
1499                 {
1500                 if (strcmp(a->fd->collate_key_name, b->fd->collate_key_name) == 0)
1501                         {
1502                         if (!a->md5sum) a->md5sum = md5_text_from_file_utf8(a->fd->path, "");
1503                         if (!b->md5sum) b->md5sum = md5_text_from_file_utf8(b->fd->path, "");
1504                         if (a->md5sum[0] == '\0' ||
1505                             b->md5sum[0] == '\0' ||
1506                             strcmp(a->md5sum, b->md5sum) != 0)
1507                                 {
1508                                 return TRUE;
1509                                 }
1510                         else
1511                                 {
1512                                 return FALSE;
1513                                 }
1514                         }
1515                 else
1516                         {
1517                         return FALSE;
1518                         }
1519                 }
1520         if (mask & DUPE_MATCH_NAME_CI_CONTENT)
1521                 {
1522                 if (strcmp(a->fd->collate_key_name_nocase, b->fd->collate_key_name_nocase) == 0)
1523                         {
1524                         if (!a->md5sum) a->md5sum = md5_text_from_file_utf8(a->fd->path, "");
1525                         if (!b->md5sum) b->md5sum = md5_text_from_file_utf8(b->fd->path, "");
1526                         if (a->md5sum[0] == '\0' ||
1527                             b->md5sum[0] == '\0' ||
1528                             strcmp(a->md5sum, b->md5sum) != 0)
1529                                 {
1530                                 return TRUE;
1531                                 }
1532                         else
1533                                 {
1534                                 return FALSE;
1535                                 }
1536                         }
1537                 else
1538                         {
1539                         return FALSE;
1540                         }
1541                 }
1542         if (mask & DUPE_MATCH_SIZE)
1543                 {
1544                 if (a->fd->size != b->fd->size) return FALSE;
1545                 }
1546         if (mask & DUPE_MATCH_DATE)
1547                 {
1548                 if (a->fd->date != b->fd->date) return FALSE;
1549                 }
1550         if (mask & DUPE_MATCH_SUM)
1551                 {
1552                 if (!a->md5sum) a->md5sum = md5_text_from_file_utf8(a->fd->path, "");
1553                 if (!b->md5sum) b->md5sum = md5_text_from_file_utf8(b->fd->path, "");
1554                 if (a->md5sum[0] == '\0' ||
1555                     b->md5sum[0] == '\0' ||
1556                     strcmp(a->md5sum, b->md5sum) != 0) return FALSE;
1557                 }
1558         if (mask & DUPE_MATCH_DIM)
1559                 {
1560                 if (a->width == 0) image_load_dimensions(a->fd, &a->width, &a->height);
1561                 if (b->width == 0) image_load_dimensions(b->fd, &b->width, &b->height);
1562                 if (a->width != b->width || a->height != b->height) return FALSE;
1563                 }
1564         if (mask & DUPE_MATCH_SIM_HIGH ||
1565             mask & DUPE_MATCH_SIM_MED ||
1566             mask & DUPE_MATCH_SIM_LOW ||
1567             mask & DUPE_MATCH_SIM_CUSTOM)
1568                 {
1569                 gdouble f;
1570                 gdouble m;
1571
1572                 if (mask & DUPE_MATCH_SIM_HIGH) m = 0.95;
1573                 else if (mask & DUPE_MATCH_SIM_MED) m = 0.90;
1574                 else if (mask & DUPE_MATCH_SIM_CUSTOM) m = static_cast<gdouble>(options->duplicates_similarity_threshold) / 100.0;
1575                 else m = 0.85;
1576
1577                 if (fast)
1578                         {
1579                         f = image_sim_compare_fast(a->simd, b->simd, m);
1580                         }
1581                 else
1582                         {
1583                         f = image_sim_compare(a->simd, b->simd);
1584                         }
1585
1586                 *rank = f * 100.0;
1587
1588                 if (f < m) return FALSE;
1589
1590                 DEBUG_3("similar: %32s %32s = %f", a->fd->name, b->fd->name, f);
1591                 }
1592
1593         return TRUE;
1594 }
1595
1596 /**
1597  * @brief  Determine if there is a match
1598  * @param di1
1599  * @param di2
1600  * @param data
1601  * @returns DUPE_MATCH/DUPE_NO_MATCH/DUPE_NAME_MATCH
1602  *                      DUPE_NAME_MATCH is used for name != contents searches:
1603  *                                                      the name and content match i.e.
1604  *                                                      no match, but keep searching
1605  *
1606  * Called when stepping down the array looking for adjacent matches,
1607  * and from the 2nd set search.
1608  *
1609  * Is not used for similarity checks.
1610  */
1611 static DUPE_CHECK_RESULT dupe_match_check(DupeItem *di1, DupeItem *di2, gpointer data)
1612 {
1613         auto dw = static_cast<DupeWindow *>(data);
1614         DupeMatchType mask = dw->match_mask;
1615
1616         if (mask & DUPE_MATCH_ALL)
1617                 {
1618                 return DUPE_MATCH;
1619                 }
1620         if (mask & DUPE_MATCH_PATH)
1621                 {
1622                 if (utf8_compare(di1->fd->path, di2->fd->path, TRUE) != 0)
1623                         {
1624                         return DUPE_NO_MATCH;
1625                         }
1626                 }
1627         if (mask & DUPE_MATCH_NAME)
1628                 {
1629                 if (g_strcmp0(di1->fd->collate_key_name, di2->fd->collate_key_name) != 0)
1630                         {
1631                         return DUPE_NO_MATCH;
1632                         }
1633                 }
1634         if (mask & DUPE_MATCH_NAME_CI)
1635                 {
1636                 if (g_strcmp0(di1->fd->collate_key_name_nocase, di2->fd->collate_key_name_nocase) != 0 )
1637                         {
1638                         return DUPE_NO_MATCH;
1639                         }
1640                 }
1641         if (mask & DUPE_MATCH_NAME_CONTENT)
1642                 {
1643                 if (g_strcmp0(di1->fd->collate_key_name, di2->fd->collate_key_name) == 0)
1644                         {
1645                         if (g_strcmp0(di1->md5sum, di2->md5sum) == 0)
1646                                 {
1647                                 return DUPE_NAME_MATCH;
1648                                 }
1649                         }
1650                 else
1651                         {
1652                         return DUPE_NO_MATCH;
1653                         }
1654                 }
1655         if (mask & DUPE_MATCH_NAME_CI_CONTENT)
1656                 {
1657                 if (strcmp(di1->fd->collate_key_name_nocase, di2->fd->collate_key_name_nocase) == 0)
1658                         {
1659                         if (g_strcmp0(di1->md5sum, di2->md5sum) == 0)
1660                                 {
1661                                 return DUPE_NAME_MATCH;
1662                                 }
1663                         }
1664                 else
1665                         {
1666                         return DUPE_NO_MATCH;
1667                         }
1668                 }
1669         if (mask & DUPE_MATCH_SIZE)
1670                 {
1671                 if (di1->fd->size != di2->fd->size)
1672                         {
1673                         return DUPE_NO_MATCH;
1674                         }
1675                 }
1676         if (mask & DUPE_MATCH_DATE)
1677                 {
1678                 if (di1->fd->date != di2->fd->date)
1679                         {
1680                         return DUPE_NO_MATCH;
1681                         }
1682                 }
1683         if (mask & DUPE_MATCH_SUM)
1684                 {
1685                 if (g_strcmp0(di1->md5sum, di2->md5sum) != 0)
1686                         {
1687                         return DUPE_NO_MATCH;
1688                         }
1689                 }
1690         if (mask & DUPE_MATCH_DIM)
1691                 {
1692                 if (di1->dimensions != di2->dimensions)
1693                         {
1694                         return DUPE_NO_MATCH;
1695                         }
1696                 }
1697
1698         return DUPE_MATCH;
1699 }
1700
1701 /**
1702  * @brief The callback for the binary search
1703  * @param a
1704  * @param b
1705  * @param param_match_mask
1706  * @returns negative/0/positive
1707  *
1708  * Is not used for similarity checks.
1709  *
1710  * Used only when two file sets are used.
1711  * Requires use of a global for param_match_mask because there is no
1712  * g_array_binary_search_with_data() function in glib.
1713  */
1714 static gint dupe_match_binary_search_cb(gconstpointer a, gconstpointer b)
1715 {
1716         auto di1 = *(static_cast<const DupeItem *const *>(a));
1717         auto di2 = static_cast<const DupeItem *>(b);
1718         DupeMatchType mask = param_match_mask;
1719
1720         if (mask & DUPE_MATCH_ALL)
1721                 {
1722                 return 0;
1723                 }
1724         if (mask & DUPE_MATCH_PATH)
1725                 {
1726                 return utf8_compare(di1->fd->path, di2->fd->path, TRUE);
1727                 }
1728         if (mask & DUPE_MATCH_NAME)
1729                 {
1730                 return g_strcmp0(di1->fd->collate_key_name, di2->fd->collate_key_name);
1731                 }
1732         if (mask & DUPE_MATCH_NAME_CI)
1733                 {
1734                 return strcmp(di1->fd->collate_key_name_nocase, di2->fd->collate_key_name_nocase);
1735                 }
1736         if (mask & DUPE_MATCH_NAME_CONTENT)
1737                 {
1738                 return g_strcmp0(di1->fd->collate_key_name, di2->fd->collate_key_name);
1739                 }
1740         if (mask & DUPE_MATCH_NAME_CI_CONTENT)
1741                 {
1742                 return strcmp(di1->fd->collate_key_name_nocase, di2->fd->collate_key_name_nocase);
1743                 }
1744         if (mask & DUPE_MATCH_SIZE)
1745                 {
1746                 return (di1->fd->size - di2->fd->size);
1747                 }
1748         if (mask & DUPE_MATCH_DATE)
1749                 {
1750                 return (di1->fd->date - di2->fd->date);
1751                 }
1752         if (mask & DUPE_MATCH_SUM)
1753                 {
1754                 return g_strcmp0(di1->md5sum, di2->md5sum);
1755                 }
1756         if (mask & DUPE_MATCH_DIM)
1757                 {
1758                 return (di1->dimensions - di2->dimensions);
1759                 }
1760
1761         return 0;
1762 }
1763
1764 /**
1765  * @brief The callback for the array sort
1766  * @param a
1767  * @param b
1768  * @param data
1769  * @returns negative/0/positive
1770  *
1771  * Is not used for similarity checks.
1772 */
1773 static gint dupe_match_sort_cb(gconstpointer a, gconstpointer b, gpointer data)
1774 {
1775         auto di1 = *(static_cast<const DupeItem *const *>(a));
1776         auto di2 = *(static_cast<const DupeItem *const *>(b));
1777         auto dw = static_cast<DupeWindow *>(data);
1778         DupeMatchType mask = dw->match_mask;
1779
1780         if (mask & DUPE_MATCH_ALL)
1781                 {
1782                 return 0;
1783                 }
1784         if (mask & DUPE_MATCH_PATH)
1785                 {
1786                 return utf8_compare(di1->fd->path, di2->fd->path, TRUE);
1787                 }
1788         if (mask & DUPE_MATCH_NAME)
1789                 {
1790                 return g_strcmp0(di1->fd->collate_key_name, di2->fd->collate_key_name);
1791                 }
1792         if (mask & DUPE_MATCH_NAME_CI)
1793                 {
1794                 return strcmp(di1->fd->collate_key_name_nocase, di2->fd->collate_key_name_nocase);
1795                 }
1796         if (mask & DUPE_MATCH_NAME_CONTENT)
1797                 {
1798                 return g_strcmp0(di1->fd->collate_key_name, di2->fd->collate_key_name);
1799                 }
1800         if (mask & DUPE_MATCH_NAME_CI_CONTENT)
1801                 {
1802                 return strcmp(di1->fd->collate_key_name_nocase, di2->fd->collate_key_name_nocase);
1803                 }
1804         if (mask & DUPE_MATCH_SIZE)
1805                 {
1806                 return (di1->fd->size - di2->fd->size);
1807                 }
1808         if (mask & DUPE_MATCH_DATE)
1809                 {
1810                 return (di1->fd->date - di2->fd->date);
1811                 }
1812         if (mask & DUPE_MATCH_SUM)
1813                 {
1814                 if (di1->md5sum[0] == '\0' || di2->md5sum[0] == '\0')
1815                     {
1816                         return -1;
1817                         }
1818                 else
1819                         {
1820                         return strcmp(di1->md5sum, di2->md5sum);
1821                         }
1822                 }
1823         if (mask & DUPE_MATCH_DIM)
1824                 {
1825                 if (!di1 || !di2 || !di1->width || !di1->height || !di2->width || !di2->height)
1826                         {
1827                         return -1;
1828                         }
1829                 return (di1->dimensions - di2->dimensions);
1830                 }
1831
1832         return 0; // should not execute
1833 }
1834
1835 /**
1836  * @brief Check for duplicate matches
1837  * @param dw
1838  *
1839  * Is not used for similarity checks.
1840  *
1841  * Loads the file sets into an array and sorts on the searched
1842  * for parameter.
1843  *
1844  * If one file set, steps down the array looking for adjacent equal values.
1845  *
1846  * If two file sets, steps down the first set and for each value
1847  * does a binary search for matches in the second set.
1848  */
1849 static void dupe_array_check(DupeWindow *dw )
1850 {
1851         GArray *array_set1;
1852         GArray *array_set2;
1853         GList *work;
1854         gint i_set1;
1855         gint i_set2;
1856         DUPE_CHECK_RESULT check_result;
1857         param_match_mask = dw->match_mask;
1858         guint out_match_index;
1859         gboolean match_found = FALSE;;
1860
1861         if (!dw->list) return;
1862
1863         array_set1 = g_array_new(TRUE, TRUE, sizeof(gpointer));
1864         array_set2 = g_array_new(TRUE, TRUE, sizeof(gpointer));
1865         dupe_match_reset_list(dw->list);
1866
1867         work = dw->list;
1868         while (work)
1869                 {
1870                 auto di = static_cast<DupeItem *>(work->data);
1871                 g_array_append_val(array_set1, di);
1872                 work = work->next;
1873                 }
1874
1875         g_array_sort_with_data(array_set1, dupe_match_sort_cb, dw);
1876
1877         if (dw->second_set)
1878                 {
1879                 /* Two sets - nothing can be done until a second set is loaded */
1880                 if (dw->second_list)
1881                         {
1882                         work = dw->second_list;
1883                         while (work)
1884                                 {
1885                                 g_array_append_val(array_set2, (work->data));
1886                                 work = work->next;
1887                                 }
1888                         g_array_sort_with_data(array_set2, dupe_match_sort_cb, dw);
1889
1890                         for (i_set1 = 0; i_set1 <= static_cast<gint>(array_set1->len) - 1; i_set1++)
1891                                 {
1892                                 auto di1 = static_cast<DupeItem *>(g_array_index(array_set1, gpointer, i_set1));
1893                                 DupeItem *di2 = nullptr;
1894                                 /* If multiple identical entries in set 1, use the last one */
1895                                 if (i_set1 < static_cast<gint>(array_set1->len) - 2)
1896                                         {
1897                                         di2 = static_cast<DupeItem *>(g_array_index(array_set1, gpointer, i_set1 + 1));
1898                                         check_result = dupe_match_check(di1, di2, dw);
1899                                         if (check_result == DUPE_MATCH || check_result == DUPE_NAME_MATCH)
1900                                                 {
1901                                                 continue;
1902                                                 }
1903                                         }
1904
1905 #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION >= 62))
1906                                 match_found = g_array_binary_search(array_set2, di1, dupe_match_binary_search_cb, &out_match_index);
1907 #else
1908                                 gint i;
1909
1910                                 match_found = FALSE;
1911                                 for(i=0; i < array_set2->len; i++)
1912                                         {
1913                                         di2 = static_cast<DupeItem *>(g_array_index(array_set2,  gpointer, i));
1914                                         check_result = dupe_match_check(di1, di2, dw);
1915                                         if (check_result == DUPE_MATCH)
1916                                                 {
1917                                                 match_found = TRUE;
1918                                                 out_match_index = i;
1919                                                 break;
1920                                                 }
1921                                         }
1922 #endif
1923
1924                                 if (match_found)
1925                                         {
1926                                         di2 = static_cast<DupeItem *>(g_array_index(array_set2, gpointer, out_match_index));
1927
1928                                         check_result = dupe_match_check(di1, di2, dw);
1929                                         if (check_result == DUPE_MATCH || check_result == DUPE_NAME_MATCH)
1930                                                 {
1931                                                 if (check_result == DUPE_MATCH)
1932                                                         {
1933                                                         dupe_match_link(di2, di1, 0.0);
1934                                                         }
1935                                                 i_set2 = out_match_index + 1;
1936
1937                                                 if (i_set2 > static_cast<gint>(array_set2->len) - 1)
1938                                                         {
1939                                                         break;
1940                                                         }
1941                                                 /* Look for multiple matches in set 2 for item di1 */
1942                                                 di2 = static_cast<DupeItem *>(g_array_index(array_set2, gpointer, i_set2));
1943                                                 check_result = dupe_match_check(di1, di2, dw);
1944                                                 while (check_result == DUPE_MATCH || check_result == DUPE_NAME_MATCH)
1945                                                         {
1946                                                         if (check_result == DUPE_MATCH)
1947                                                                 {
1948                                                                 dupe_match_link(di2, di1, 0.0);
1949                                                                 }
1950                                                         i_set2++;
1951                                                         if (i_set2 > static_cast<gint>(array_set2->len) - 1)
1952                                                                 {
1953                                                                 break;
1954                                                                 }
1955                                                         di2 = static_cast<DupeItem *>(g_array_index(array_set2, gpointer, i_set2));
1956                                                         check_result = dupe_match_check(di1, di2, dw);
1957                                                         }
1958                                                 }
1959                                         }
1960                                 }
1961                         }
1962                 }
1963         else
1964                 {
1965                 /* File set 1 only */
1966                 g_list_free(dw->dupes);
1967                 dw->dupes = nullptr;
1968
1969                 if (static_cast<gint>(array_set1->len) > 1)
1970                         {
1971                         for (i_set1 = 0; i_set1 <= static_cast<gint>(array_set1->len) - 2; i_set1++)
1972                                 {
1973                                 auto di1 = static_cast<DupeItem *>(g_array_index(array_set1, gpointer, i_set1));
1974                                 auto di2 = static_cast<DupeItem *>(g_array_index(array_set1, gpointer, i_set1 + 1));
1975
1976                                 check_result = dupe_match_check(di1, di2, dw);
1977                                 if (check_result == DUPE_MATCH || check_result == DUPE_NAME_MATCH)
1978                                         {
1979                                         if (check_result == DUPE_MATCH)
1980                                                 {
1981                                                 dupe_match_link(di2, di1, 0.0);
1982                                                 }
1983                                         i_set1++;
1984
1985                                         if ( i_set1 + 1 > static_cast<gint>(array_set1->len) - 1)
1986                                                 {
1987                                                 break;
1988                                                 }
1989                                         /* Look for multiple matches for item di1 */
1990                                         di2 = static_cast<DupeItem *>(g_array_index(array_set1, gpointer, i_set1 + 1));
1991                                         check_result = dupe_match_check(di1, di2, dw);
1992                                         while (check_result == DUPE_MATCH || check_result == DUPE_NAME_MATCH)
1993                                                 {
1994                                                 if (check_result == DUPE_MATCH)
1995                                                         {
1996                                                         dupe_match_link(di2, di1, 0.0);
1997                                                         }
1998                                                 i_set1++;
1999
2000                                                 if (i_set1 + 1 > static_cast<gint>(array_set1->len) - 1)
2001                                                         {
2002                                                         break;
2003                                                         }
2004                                                 di2 = static_cast<DupeItem *>(g_array_index(array_set1, gpointer, i_set1 + 1));
2005                                                 check_result = dupe_match_check(di1, di2, dw);
2006                                                 }
2007                                         }
2008                                 }
2009                         }
2010                 }
2011         g_array_free(array_set1, TRUE);
2012         g_array_free(array_set2, TRUE);
2013 }
2014
2015 /**
2016  * @brief Look for similarity match
2017  * @param dw
2018  * @param needle
2019  * @param start
2020  *
2021  * Only used for similarity checks.\n
2022  * Called from dupe_check_cb.
2023  * Called for each entry in the list.
2024  * Steps through the list looking for matches against needle.
2025  * Pushes a #DupeQueueItem onto thread pool queue.
2026  */
2027 static void dupe_list_check_match(DupeWindow *dw, DupeItem *needle, GList *start)
2028 {
2029         GList *work;
2030         DupeQueueItem *dqi;
2031
2032         if (dw->second_set)
2033                 {
2034                 work = dw->second_list;
2035                 }
2036         else if (start)
2037                 {
2038                 work = start;
2039                 }
2040         else
2041                 {
2042                 work = g_list_last(dw->list);
2043                 }
2044
2045         dqi = g_new0(DupeQueueItem, 1);
2046         dqi->needle = needle;
2047         dqi->dw = dw;
2048         dqi->work = work;
2049         dqi->index = dw->queue_count;
2050         g_thread_pool_push(dw->dupe_comparison_thread_pool, dqi, nullptr);
2051 }
2052
2053 /*
2054  * ------------------------------------------------------------------
2055  * Thumbnail handling
2056  * ------------------------------------------------------------------
2057  */
2058
2059 static void dupe_listview_set_thumb(DupeWindow *dw, DupeItem *di, GtkTreeIter *iter)
2060 {
2061         GtkListStore *store;
2062         GtkTreeIter iter_n;
2063
2064         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(dw->listview)));
2065         if (!iter)
2066                 {
2067                 if (dupe_listview_find_item(store, di, &iter_n) >= 0)
2068                         {
2069                         iter = &iter_n;
2070                         }
2071                 }
2072
2073         if (iter) gtk_list_store_set(store, iter, DUPE_COLUMN_THUMB, di->pixbuf, -1);
2074 }
2075
2076 static void dupe_thumb_do(DupeWindow *dw)
2077 {
2078         DupeItem *di;
2079
2080         if (!dw->thumb_loader || !dw->thumb_item) return;
2081         di = dw->thumb_item;
2082
2083         if (di->pixbuf) g_object_unref(di->pixbuf);
2084         di->pixbuf = thumb_loader_get_pixbuf(dw->thumb_loader);
2085
2086         dupe_listview_set_thumb(dw, di, nullptr);
2087 }
2088
2089 static void dupe_thumb_error_cb(ThumbLoader *, gpointer data)
2090 {
2091         auto dw = static_cast<DupeWindow *>(data);
2092
2093         dupe_thumb_do(dw);
2094         dupe_thumb_step(dw);
2095 }
2096
2097 static void dupe_thumb_done_cb(ThumbLoader *, gpointer data)
2098 {
2099         auto dw = static_cast<DupeWindow *>(data);
2100
2101         dupe_thumb_do(dw);
2102         dupe_thumb_step(dw);
2103 }
2104
2105 static void dupe_thumb_step(DupeWindow *dw)
2106 {
2107         GtkTreeModel *store;
2108         GtkTreeIter iter;
2109         DupeItem *di = nullptr;
2110         gboolean valid;
2111         gint row = 0;
2112         gint length = 0;
2113
2114         store = gtk_tree_view_get_model(GTK_TREE_VIEW(dw->listview));
2115         valid = gtk_tree_model_get_iter_first(store, &iter);
2116
2117         while (!di && valid)
2118                 {
2119                 GdkPixbuf *pixbuf;
2120
2121                 length++;
2122                 gtk_tree_model_get(store, &iter, DUPE_COLUMN_POINTER, &di, DUPE_COLUMN_THUMB, &pixbuf, -1);
2123                 if (pixbuf || di->pixbuf)
2124                         {
2125                         if (!pixbuf) gtk_list_store_set(GTK_LIST_STORE(store), &iter, DUPE_COLUMN_THUMB, di->pixbuf, -1);
2126                         row++;
2127                         di = nullptr;
2128                         }
2129                 valid = gtk_tree_model_iter_next(store, &iter);
2130                 }
2131         if (valid)
2132                 {
2133                 while (gtk_tree_model_iter_next(store, &iter)) length++;
2134                 }
2135
2136         if (!di)
2137                 {
2138                 dw->thumb_item = nullptr;
2139                 thumb_loader_free(dw->thumb_loader);
2140                 dw->thumb_loader = nullptr;
2141
2142                 dupe_window_update_progress(dw, nullptr, 0.0, FALSE);
2143                 return;
2144                 }
2145
2146         dupe_window_update_progress(dw, _("Loading thumbs..."),
2147                                     length == 0 ? 0.0 : static_cast<gdouble>(row) / length, FALSE);
2148
2149         dw->thumb_item = di;
2150         thumb_loader_free(dw->thumb_loader);
2151         dw->thumb_loader = thumb_loader_new(options->thumbnails.max_width, options->thumbnails.max_height);
2152
2153         thumb_loader_set_callbacks(dw->thumb_loader,
2154                                    dupe_thumb_done_cb,
2155                                    dupe_thumb_error_cb,
2156                                    nullptr,
2157                                    dw);
2158
2159         /* start it */
2160         if (!thumb_loader_start(dw->thumb_loader, di->fd))
2161                 {
2162                 /* error, handle it, do next */
2163                 DEBUG_1("error loading thumb for %s", di->fd->path);
2164                 dupe_thumb_do(dw);
2165                 dupe_thumb_step(dw);
2166                 }
2167 }
2168
2169 /*
2170  * ------------------------------------------------------------------
2171  * Dupe checking loop
2172  * ------------------------------------------------------------------
2173  */
2174
2175 static void dupe_check_stop(DupeWindow *dw)
2176 {
2177         if (dw->idle_id > 0)
2178                 {
2179                 g_source_remove(dw->idle_id);
2180                 dw->idle_id = 0;
2181                 }
2182
2183         dw->abort = TRUE;
2184
2185         while (dw->thread_count < dw->queue_count) // Wait for the queue to empty
2186                 {
2187                 dupe_window_update_progress(dw, nullptr, 0.0, FALSE);
2188                 widget_set_cursor(dw->listview, -1);
2189                 }
2190
2191         g_list_free(dw->search_matches);
2192         dw->search_matches = nullptr;
2193
2194         if (dw->idle_id || dw->img_loader || dw->thumb_loader)
2195                 {
2196                 if (dw->idle_id > 0)
2197                         {
2198                         g_source_remove(dw->idle_id);
2199                         dw->idle_id = 0;
2200                         }
2201                 dupe_window_update_progress(dw, nullptr, 0.0, FALSE);
2202                 widget_set_cursor(dw->listview, -1);
2203                 }
2204
2205         if (dw->add_files_queue_id)
2206                 {
2207                 g_source_remove(dw->add_files_queue_id);
2208                 dw->add_files_queue_id = 0;
2209                 dupe_destroy_list_cache(dw);
2210                 gtk_widget_set_sensitive(dw->controls_box, TRUE);
2211                 if (g_list_length(dw->add_files_queue) > 0)
2212                         {
2213                         filelist_free(dw->add_files_queue);
2214                         }
2215                 dw->add_files_queue = nullptr;
2216                 dupe_window_update_progress(dw, nullptr, 0.0, FALSE);
2217                 widget_set_cursor(dw->listview, -1);
2218                 }
2219
2220         thumb_loader_free(dw->thumb_loader);
2221         dw->thumb_loader = nullptr;
2222
2223         image_loader_free(dw->img_loader);
2224         dw->img_loader = nullptr;
2225 }
2226
2227 static void dupe_check_stop_cb(GtkWidget *, gpointer data)
2228 {
2229         auto dw = static_cast<DupeWindow *>(data);
2230
2231         dupe_check_stop(dw);
2232 }
2233
2234 static void dupe_loader_done_cb(ImageLoader *il, gpointer data)
2235 {
2236         auto dw = static_cast<DupeWindow *>(data);
2237         GdkPixbuf *pixbuf;
2238
2239         pixbuf = image_loader_get_pixbuf(il);
2240
2241         if (dw->setup_point)
2242                 {
2243                 auto di = static_cast<DupeItem *>(dw->setup_point->data);
2244
2245                 if (!di->simd)
2246                         {
2247                         di->simd = image_sim_new_from_pixbuf(pixbuf);
2248                         }
2249                 else
2250                         {
2251                         image_sim_fill_data(di->simd, pixbuf);
2252                         }
2253
2254                 if (di->width == 0 && di->height == 0)
2255                         {
2256                         di->width = gdk_pixbuf_get_width(pixbuf);
2257                         di->height = gdk_pixbuf_get_height(pixbuf);
2258                         }
2259                 if (options->thumbnails.enable_caching)
2260                         {
2261                         dupe_item_write_cache(di);
2262                         }
2263
2264                 image_sim_alternate_processing(di->simd);
2265                 }
2266
2267         image_loader_free(dw->img_loader);
2268         dw->img_loader = nullptr;
2269
2270         dw->idle_id = g_idle_add(dupe_check_cb, dw);
2271 }
2272
2273 static void dupe_setup_reset(DupeWindow *dw)
2274 {
2275         dw->setup_point = nullptr;
2276         dw->setup_n = 0;
2277         dw->setup_time = msec_time();
2278         dw->setup_time_count = 0;
2279 }
2280
2281 static GList *dupe_setup_point_step(DupeWindow *dw, GList *p)
2282 {
2283         if (!p) return nullptr;
2284
2285         if (p->next) return p->next;
2286
2287         if (dw->second_set && g_list_first(p) == dw->list) return dw->second_list;
2288
2289         return nullptr;
2290 }
2291
2292 /**
2293  * @brief Generates the sumcheck or dimensions
2294  * @param list Set1 or set2
2295  * @returns TRUE/FALSE = not completed/completed
2296  *
2297  * Ensures that the DIs contain the MD5SUM or dimensions for all items in
2298  * the list. One item at a time. Re-enters if not completed.
2299  */
2300 static gboolean create_checksums_dimensions(DupeWindow *dw, GList *list)
2301 {
2302                 if ((dw->match_mask & DUPE_MATCH_SUM) ||
2303                         (dw->match_mask & DUPE_MATCH_NAME_CONTENT) ||
2304                         (dw->match_mask & DUPE_MATCH_NAME_CI_CONTENT))
2305                         {
2306                         /* MD5SUM only */
2307                         if (!dw->setup_point) dw->setup_point = list; // setup_point clear on 1st entry
2308
2309                         while (dw->setup_point)
2310                                 {
2311                                 auto di = static_cast<DupeItem *>(dw->setup_point->data);
2312
2313                                 dw->setup_point = dupe_setup_point_step(dw, dw->setup_point);
2314                                 dw->setup_n++;
2315
2316                                 if (!di->md5sum)
2317                                         {
2318                                         dupe_window_update_progress(dw, _("Reading checksums..."),
2319                                                 dw->setup_count == 0 ? 0.0 : static_cast<gdouble>(dw->setup_n - 1) / dw->setup_count, FALSE);
2320
2321                                         if (options->thumbnails.enable_caching)
2322                                                 {
2323                                                 dupe_item_read_cache(di);
2324                                                 if (di->md5sum)
2325                                                         {
2326                                                         return TRUE;
2327                                                         }
2328                                                 }
2329
2330                                         di->md5sum = md5_text_from_file_utf8(di->fd->path, "");
2331                                         if (options->thumbnails.enable_caching)
2332                                                 {
2333                                                 dupe_item_write_cache(di);
2334                                                 }
2335                                         return TRUE;
2336                                         }
2337                                 }
2338                         dupe_setup_reset(dw);
2339                         }
2340
2341                 if ((dw->match_mask & DUPE_MATCH_DIM)  )
2342                         {
2343                         /* Dimensions only */
2344                         if (!dw->setup_point) dw->setup_point = list;
2345
2346                         while (dw->setup_point)
2347                                 {
2348                                 auto di = static_cast<DupeItem *>(dw->setup_point->data);
2349
2350                                 dw->setup_point = dupe_setup_point_step(dw, dw->setup_point);
2351                                 dw->setup_n++;
2352                                 if (di->width == 0 && di->height == 0)
2353                                         {
2354                                         dupe_window_update_progress(dw, _("Reading dimensions..."),
2355                                                 dw->setup_count == 0 ? 0.0 : static_cast<gdouble>(dw->setup_n - 1) / dw->setup_count, FALSE);
2356
2357                                         if (options->thumbnails.enable_caching)
2358                                                 {
2359                                                 dupe_item_read_cache(di);
2360                                                 if (di->width != 0 || di->height != 0)
2361                                                         {
2362                                                         return TRUE;
2363                                                         }
2364                                                 }
2365
2366                                         image_load_dimensions(di->fd, &di->width, &di->height);
2367                                         di->dimensions = (di->width << 16) + di->height;
2368                                         if (options->thumbnails.enable_caching)
2369                                                 {
2370                                                 dupe_item_write_cache(di);
2371                                                 }
2372                                         return TRUE;
2373                                         }
2374                                 }
2375                         dupe_setup_reset(dw);
2376                         }
2377
2378         return FALSE;
2379 }
2380
2381 /**
2382  * @brief Compare func. for sorting search matches
2383  * @param a #DupeSearchMatch
2384  * @param b #DupeSearchMatch
2385  * @returns
2386  *
2387  * Used only for similarity checks\n
2388  * Sorts search matches on order they were inserted into the pool queue
2389  */
2390 static gint sort_func(gconstpointer a, gconstpointer b)
2391 {
2392         return static_cast<const DupeSearchMatch *>(a)->index - static_cast<const DupeSearchMatch *>(b)->index;
2393 }
2394
2395 /**
2396  * @brief Check set 1 (and set 2) for matches
2397  * @param data DupeWindow
2398  * @returns TRUE/FALSE = not completed/completed
2399  *
2400  * Initiated from start, loader done and item remove
2401  *
2402  * On first entry generates di->MD5SUM, di->dimensions and sim data,
2403  * and updates the cache.
2404  */
2405 static gboolean dupe_check_cb(gpointer data)
2406 {
2407         auto dw = static_cast<DupeWindow *>(data);
2408         DupeSearchMatch *search_match_list_item;
2409
2410         if (!dw->idle_id)
2411                 {
2412                 return G_SOURCE_REMOVE;
2413                 }
2414
2415         if (!dw->setup_done) /* Clear on 1st entry */
2416                 {
2417                 if (dw->list)
2418                         {
2419                         if (create_checksums_dimensions(dw, dw->list))
2420                                 {
2421                                 return G_SOURCE_CONTINUE;
2422                                 }
2423                         }
2424                 if (dw->second_list)
2425                         {
2426                         if (create_checksums_dimensions(dw, dw->second_list))
2427                                 {
2428                                 return G_SOURCE_CONTINUE;
2429                                 }
2430                         }
2431                 if ((dw->match_mask & DUPE_MATCH_SIM_HIGH ||
2432                      dw->match_mask & DUPE_MATCH_SIM_MED ||
2433                      dw->match_mask & DUPE_MATCH_SIM_LOW ||
2434                      dw->match_mask & DUPE_MATCH_SIM_CUSTOM) &&
2435                     !(dw->setup_mask & DUPE_MATCH_SIM_MED) )
2436                         {
2437                         /* Similarity only */
2438                         if (!dw->setup_point) dw->setup_point = dw->list;
2439
2440                         while (dw->setup_point)
2441                                 {
2442                                 auto di = static_cast<DupeItem *>(dw->setup_point->data);
2443
2444                                 if (!di->simd)
2445                                         {
2446                                         dupe_window_update_progress(dw, _("Reading similarity data..."),
2447                                                 dw->setup_count == 0 ? 0.0 : static_cast<gdouble>(dw->setup_n) / dw->setup_count, FALSE);
2448
2449                                         if (options->thumbnails.enable_caching)
2450                                                 {
2451                                                 dupe_item_read_cache(di);
2452                                                 if (cache_sim_data_filled(di->simd))
2453                                                         {
2454                                                         image_sim_alternate_processing(di->simd);
2455                                                         return G_SOURCE_CONTINUE;
2456                                                         }
2457                                                 }
2458
2459                                         dw->img_loader = image_loader_new(di->fd);
2460                                         image_loader_set_buffer_size(dw->img_loader, 8);
2461                                         g_signal_connect(G_OBJECT(dw->img_loader), "error", (GCallback)dupe_loader_done_cb, dw);
2462                                         g_signal_connect(G_OBJECT(dw->img_loader), "done", (GCallback)dupe_loader_done_cb, dw);
2463
2464                                         if (!image_loader_start(dw->img_loader))
2465                                                 {
2466                                                 image_sim_free(di->simd);
2467                                                 di->simd = image_sim_new();
2468                                                 image_loader_free(dw->img_loader);
2469                                                 dw->img_loader = nullptr;
2470                                                 return G_SOURCE_CONTINUE;
2471                                                 }
2472                                         dw->idle_id = 0;
2473                                         return G_SOURCE_REMOVE;
2474                                         }
2475
2476                                 dw->setup_point = dupe_setup_point_step(dw, dw->setup_point);
2477                                 dw->setup_n++;
2478                                 }
2479                         dw->setup_mask = static_cast<DupeMatchType>(dw->setup_mask | DUPE_MATCH_SIM_MED);
2480                         dupe_setup_reset(dw);
2481                         }
2482
2483                 /* End of setup not done */
2484                 dupe_window_update_progress(dw, _("Comparing..."), 0.0, FALSE);
2485                 dw->setup_done = TRUE;
2486                 dupe_setup_reset(dw);
2487                 dw->setup_count = g_list_length(dw->list);
2488                 }
2489
2490         /* Setup done - dw->working set to NULL below
2491          * Set before 1st entry: dw->working = g_list_last(dw->list)
2492          * Set before 1st entry: dw->setup_count = g_list_length(dw->list)
2493          */
2494         if (!dw->working)
2495                 {
2496                 /* Similarity check threads may still be running */
2497                 if (dw->setup_count > 0 && (dw->match_mask == DUPE_MATCH_SIM_HIGH ||
2498                         dw->match_mask == DUPE_MATCH_SIM_MED ||
2499                         dw->match_mask == DUPE_MATCH_SIM_LOW ||
2500                         dw->match_mask == DUPE_MATCH_SIM_CUSTOM))
2501                         {
2502                         if( dw->thread_count < dw->queue_count)
2503                                 {
2504                                 dupe_window_update_progress(dw, _("Comparing..."), 0.0, FALSE);
2505
2506                                 return G_SOURCE_CONTINUE;
2507                                 }
2508
2509                         if (dw->search_matches_sorted == nullptr)
2510                                 {
2511                                 dw->search_matches_sorted = g_list_sort(dw->search_matches, sort_func);
2512                                 dupe_setup_reset(dw);
2513                                 }
2514
2515                         while (dw->search_matches_sorted)
2516                                 {
2517                                 dw->setup_n++;
2518                                 dupe_window_update_progress(dw, _("Sorting..."), 0.0, FALSE);
2519                                 search_match_list_item = static_cast<DupeSearchMatch *>(dw->search_matches_sorted->data);
2520
2521                                 if (!dupe_match_link_exists(search_match_list_item->a, search_match_list_item->b))
2522                                         {
2523                                         dupe_match_link(search_match_list_item->a, search_match_list_item->b, search_match_list_item->rank);
2524                                         }
2525
2526                                 dw->search_matches_sorted = dw->search_matches_sorted->next;
2527
2528                                 if (dw->search_matches_sorted != nullptr)
2529                                         {
2530                                         return G_SOURCE_CONTINUE;
2531                                         }
2532                                 }
2533                         g_list_free(dw->search_matches);
2534                         dw->search_matches = nullptr;
2535                         g_list_free(dw->search_matches_sorted);
2536                         dw->search_matches_sorted = nullptr;
2537                         dw->setup_count = 0;
2538                         }
2539                 else
2540                         {
2541                         if (dw->setup_count > 0)
2542                                 {
2543                                 dw->setup_count = 0;
2544                                 dupe_window_update_progress(dw, _("Sorting..."), 1.0, TRUE);
2545                                 return G_SOURCE_CONTINUE;
2546                                 }
2547                         }
2548
2549                 dw->idle_id = 0;
2550                 dupe_window_update_progress(dw, nullptr, 0.0, FALSE);
2551
2552                 dupe_match_rank(dw);
2553                 dupe_window_update_count(dw, FALSE);
2554
2555                 dupe_listview_populate(dw);
2556
2557                 /* check thumbs */
2558                 if (dw->show_thumbs) dupe_thumb_step(dw);
2559
2560                 widget_set_cursor(dw->listview, -1);
2561
2562                 return G_SOURCE_REMOVE;
2563                 /* The end */
2564                 }
2565
2566         /* Setup done - working */
2567         if (dw->match_mask == DUPE_MATCH_SIM_HIGH ||
2568                 dw->match_mask == DUPE_MATCH_SIM_MED ||
2569                 dw->match_mask == DUPE_MATCH_SIM_LOW ||
2570                 dw->match_mask == DUPE_MATCH_SIM_CUSTOM)
2571                 {
2572                 /* This is the similarity comparison */
2573                 dupe_list_check_match(dw, static_cast<DupeItem *>(dw->working->data), dw->working);
2574                 dupe_window_update_progress(dw, _("Queuing..."), dw->setup_count == 0 ? 0.0 : static_cast<gdouble>(dw->setup_n) / dw->setup_count, FALSE);
2575                 dw->setup_n++;
2576                 dw->queue_count++;
2577
2578                 dw->working = dw->working->prev; /* Is NULL when complete */
2579                 }
2580         else
2581                 {
2582                 /* This is the comparison for all other parameters.
2583                  * dupe_array_check() processes the entire list in one go
2584                 */
2585                 dw->working = nullptr;
2586                 dupe_window_update_progress(dw, _("Comparing..."), 0.0, FALSE);
2587                 dupe_array_check(dw);
2588                 }
2589
2590         return G_SOURCE_CONTINUE;
2591 }
2592
2593 static void dupe_check_start(DupeWindow *dw)
2594 {
2595         dw->setup_done = FALSE;
2596
2597         dw->setup_count = g_list_length(dw->list);
2598         if (dw->second_set) dw->setup_count += g_list_length(dw->second_list);
2599
2600         dw->setup_mask = DUPE_MATCH_NONE;
2601         dupe_setup_reset(dw);
2602
2603         dw->working = g_list_last(dw->list);
2604
2605         dupe_window_update_count(dw, TRUE);
2606         widget_set_cursor(dw->listview, GDK_WATCH);
2607         dw->queue_count = 0;
2608         dw->thread_count = 0;
2609         dw->search_matches_sorted = nullptr;
2610         dw->abort = FALSE;
2611
2612         if (dw->idle_id) return;
2613
2614         dw->idle_id = g_idle_add(dupe_check_cb, dw);
2615 }
2616
2617 static gboolean dupe_check_start_cb(gpointer data)
2618 {
2619         auto dw = static_cast<DupeWindow *>(data);
2620
2621         dupe_check_start(dw);
2622
2623         return FALSE;
2624 }
2625
2626 /*
2627  * ------------------------------------------------------------------
2628  * Item addition, removal
2629  * ------------------------------------------------------------------
2630  */
2631
2632 static void dupe_item_remove(DupeWindow *dw, DupeItem *di)
2633 {
2634         if (!di) return;
2635
2636         /* handle things that may be in progress... */
2637         if (dw->working && dw->working->data == di)
2638                 {
2639                 dw->working = dw->working->prev;
2640                 }
2641         if (dw->thumb_loader && dw->thumb_item == di)
2642                 {
2643                 dupe_thumb_step(dw);
2644                 }
2645         if (dw->setup_point && dw->setup_point->data == di)
2646                 {
2647                 dw->setup_point = dupe_setup_point_step(dw, dw->setup_point);
2648                 if (dw->img_loader)
2649                         {
2650                         image_loader_free(dw->img_loader);
2651                         dw->img_loader = nullptr;
2652                         dw->idle_id = g_idle_add(dupe_check_cb, dw);
2653                         }
2654                 }
2655
2656         if (di->group && dw->dupes)
2657                 {
2658                 /* is a dupe, must remove from group/reset children if a parent */
2659                 DupeItem *parent;
2660
2661                 parent = dupe_match_find_parent(dw, di);
2662                 if (di == parent)
2663                         {
2664                         if (g_list_length(parent->group) < 2)
2665                                 {
2666                                 DupeItem *child;
2667
2668                                 child = dupe_match_highest_rank(parent);
2669                                 dupe_match_link_clear(child, TRUE);
2670                                 dupe_listview_remove(dw, child);
2671
2672                                 dupe_match_link_clear(parent, TRUE);
2673                                 dupe_listview_remove(dw, parent);
2674                                 dw->dupes = g_list_remove(dw->dupes, parent);
2675                                 }
2676                         else
2677                                 {
2678                                 DupeItem *new_parent;
2679                                 DupeMatch *dm;
2680
2681                                 dm = static_cast<DupeMatch *>(parent->group->data);
2682                                 new_parent = dm->di;
2683                                 dupe_match_reparent(dw, parent, new_parent);
2684                                 dupe_listview_remove(dw, parent);
2685                                 }
2686                         }
2687                 else
2688                         {
2689                         if (g_list_length(parent->group) < 2)
2690                                 {
2691                                 dupe_match_link_clear(parent, TRUE);
2692                                 dupe_listview_remove(dw, parent);
2693                                 dw->dupes = g_list_remove(dw->dupes, parent);
2694                                 }
2695                         dupe_match_link_clear(di, TRUE);
2696                         dupe_listview_remove(dw, di);
2697                         }
2698                 }
2699         else
2700                 {
2701                 /* not a dupe, or not sorted yet, simply reset */
2702                 dupe_match_link_clear(di, TRUE);
2703                 }
2704
2705         if (dw->second_list && g_list_find(dw->second_list, di))
2706                 {
2707                 dupe_second_remove(dw, di);
2708                 }
2709         else
2710                 {
2711                 dw->list = g_list_remove(dw->list, di);
2712                 }
2713         dupe_item_free(di);
2714
2715         dupe_window_update_count(dw, FALSE);
2716 }
2717
2718 #pragma GCC diagnostic push
2719 #pragma GCC diagnostic ignored "-Wunused-function"
2720 static gboolean dupe_item_remove_by_path_unused(DupeWindow *dw, const gchar *path)
2721 {
2722         DupeItem *di;
2723
2724         di = dupe_item_find_path_unused(dw, path);
2725         if (!di) return FALSE;
2726
2727         dupe_item_remove(dw, di);
2728
2729         return TRUE;
2730 }
2731 #pragma GCC diagnostic pop
2732
2733 static gboolean dupe_files_add_queue_cb(gpointer data)
2734 {
2735         DupeItem *di = nullptr;
2736         auto dw = static_cast<DupeWindow *>(data);
2737         FileData *fd;
2738         GList *queue = dw->add_files_queue;
2739
2740         gtk_progress_bar_pulse(GTK_PROGRESS_BAR(dw->extra_label));
2741
2742         if (queue == nullptr)
2743                 {
2744                 dw->add_files_queue_id = 0;
2745                 dupe_destroy_list_cache(dw);
2746                 g_idle_add(dupe_check_start_cb, dw);
2747                 gtk_widget_set_sensitive(dw->controls_box, TRUE);
2748                 return FALSE;
2749                 }
2750
2751         fd = static_cast<FileData *>(queue->data);
2752         if (fd)
2753                 {
2754                 if (isfile(fd->path))
2755                         {
2756                         di = dupe_item_new(fd);
2757                         }
2758                 else if (isdir(fd->path))
2759                         {
2760                         GList *f, *d;
2761                         dw->add_files_queue = g_list_remove(dw->add_files_queue, g_list_first(dw->add_files_queue)->data);
2762
2763                         if (filelist_read(fd, &f, &d))
2764                                 {
2765                                 f = filelist_filter(f, FALSE);
2766                                 d = filelist_filter(d, TRUE);
2767
2768                                 dw->add_files_queue = g_list_concat(f, dw->add_files_queue);
2769                                 dw->add_files_queue = g_list_concat(d, dw->add_files_queue);
2770                                 }
2771                         }
2772                 else
2773                         {
2774                         /* Not a file and not a dir */
2775                         dw->add_files_queue = g_list_remove(dw->add_files_queue, g_list_first(dw->add_files_queue)->data);
2776                         }
2777                 }
2778
2779         if (!di)
2780                 {
2781                 /* A dir was found. Process the contents on next entry */
2782                 return TRUE;
2783                 }
2784
2785         dw->add_files_queue = g_list_remove(dw->add_files_queue, g_list_first(dw->add_files_queue)->data);
2786
2787         dupe_item_read_cache(di);
2788
2789         /* Ensure images in the lists have unique FileDatas */
2790         if (!dupe_insert_in_list_cache(dw, di->fd))
2791                 {
2792                 dupe_item_free(di);
2793                 return TRUE;
2794                 }
2795
2796         if (dw->second_drop)
2797                 {
2798                 dupe_second_add(dw, di);
2799                 }
2800         else
2801                 {
2802                 dw->list = g_list_prepend(dw->list, di);
2803                 }
2804
2805         if (dw->add_files_queue != nullptr)
2806                 {
2807                 return TRUE;
2808                 }
2809         else
2810                 {
2811                 dw->add_files_queue_id = 0;
2812                 dupe_destroy_list_cache(dw);
2813                 g_idle_add(dupe_check_start_cb, dw);
2814                 gtk_widget_set_sensitive(dw->controls_box, TRUE);
2815                 return FALSE;
2816                 }
2817 }
2818
2819 static void dupe_files_add(DupeWindow *dw, CollectionData *, CollectInfo *info,
2820                            FileData *fd, gboolean recurse)
2821 {
2822         DupeItem *di = nullptr;
2823
2824         if (info)
2825                 {
2826                 di = dupe_item_new(info->fd);
2827                 }
2828         else if (fd)
2829                 {
2830                 if (isfile(fd->path) && !g_file_test(fd->path, G_FILE_TEST_IS_SYMLINK))
2831                         {
2832                         di = dupe_item_new(fd);
2833                         }
2834                 else if (isdir(fd->path) && recurse)
2835                         {
2836                         GList *f, *d;
2837                         if (filelist_read(fd, &f, &d))
2838                                 {
2839                                 GList *work;
2840
2841                                 f = filelist_filter(f, FALSE);
2842                                 d = filelist_filter(d, TRUE);
2843
2844                                 work = f;
2845                                 while (work)
2846                                         {
2847                                         dupe_files_add(dw, nullptr, nullptr, static_cast<FileData *>(work->data), TRUE);
2848                                         work = work->next;
2849                                         }
2850                                 filelist_free(f);
2851                                 work = d;
2852                                 while (work)
2853                                         {
2854                                         dupe_files_add(dw, nullptr, nullptr, static_cast<FileData *>(work->data), TRUE);
2855                                         work = work->next;
2856                                         }
2857                                 filelist_free(d);
2858                                 }
2859                         }
2860                 }
2861
2862         if (!di) return;
2863
2864         dupe_item_read_cache(di);
2865
2866         /* Ensure images in the lists have unique FileDatas */
2867         GList *work;
2868         DupeItem *di_list;
2869         work = g_list_first(dw->list);
2870         while (work)
2871                 {
2872                 di_list = static_cast<DupeItem *>(work->data);
2873                 if (di_list->fd == di->fd)
2874                         {
2875                         return;
2876                         }
2877                 else
2878                         {
2879                         work = work->next;
2880                         }
2881                 }
2882
2883         if (dw->second_list)
2884                 {
2885                 work = g_list_first(dw->second_list);
2886                 while (work)
2887                         {
2888                         di_list = static_cast<DupeItem *>(work->data);
2889                         if (di_list->fd == di->fd)
2890                                 {
2891                                 return;
2892                                 }
2893                         else
2894                                 {
2895                                 work = work->next;
2896                                 }
2897                         }
2898                 }
2899
2900         if (dw->second_drop)
2901                 {
2902                 dupe_second_add(dw, di);
2903                 }
2904         else
2905                 {
2906                 dw->list = g_list_prepend(dw->list, di);
2907                 }
2908 }
2909
2910 static void dupe_init_list_cache(DupeWindow *dw)
2911 {
2912         dw->list_cache = g_hash_table_new(g_direct_hash, g_direct_equal);
2913         dw->second_list_cache = g_hash_table_new(g_direct_hash, g_direct_equal);
2914
2915         for (GList *i = dw->list; i != nullptr; i = i->next)
2916                 {
2917                         auto di = static_cast<DupeItem *>(i->data);
2918
2919                         g_hash_table_add(dw->list_cache, di->fd);
2920                 }
2921
2922         for (GList *i = dw->second_list; i != nullptr; i = i->next)
2923                 {
2924                         auto di = static_cast<DupeItem *>(i->data);
2925
2926                         g_hash_table_add(dw->second_list_cache, di->fd);
2927                 }
2928 }
2929
2930 static void dupe_destroy_list_cache(DupeWindow *dw)
2931 {
2932         g_hash_table_destroy(dw->list_cache);
2933         g_hash_table_destroy(dw->second_list_cache);
2934 }
2935
2936 /**
2937  * @brief Return true if the fd was not in the cache
2938  * @param dw
2939  * @param fd
2940  * @returns
2941  *
2942  *
2943  */
2944 static gboolean dupe_insert_in_list_cache(DupeWindow *dw, FileData *fd)
2945 {
2946         GHashTable *table =
2947                 dw->second_drop ? dw->second_list_cache : dw->list_cache;
2948         /* We do this as a lookup + add as we don't want to overwrite
2949            items as that would leak the old value. */
2950         if (g_hash_table_lookup(table, fd) != nullptr)
2951                 return FALSE;
2952         return g_hash_table_add(table, fd);
2953 }
2954
2955 void dupe_window_add_collection(DupeWindow *dw, CollectionData *collection)
2956 {
2957         CollectInfo *info;
2958
2959         info = collection_get_first(collection);
2960         while (info)
2961                 {
2962                 dupe_files_add(dw, collection, info, nullptr, FALSE);
2963                 info = collection_next_by_info(collection, info);
2964                 }
2965
2966         dupe_check_start(dw);
2967 }
2968
2969 void dupe_window_add_files(DupeWindow *dw, GList *list, gboolean recurse)
2970 {
2971         GList *work;
2972
2973         work = list;
2974         while (work)
2975                 {
2976                 auto fd = static_cast<FileData *>(work->data);
2977                 work = work->next;
2978                 if (isdir(fd->path) && !recurse)
2979                         {
2980                         GList *f, *d;
2981
2982                         if (filelist_read(fd, &f, &d))
2983                                 {
2984                                 GList *work_file;
2985                                 work_file = f;
2986
2987                                 while (work_file)
2988                                         {
2989                                         /* Add only the files, ignore the dirs when no recurse */
2990                                         dw->add_files_queue = g_list_prepend(dw->add_files_queue, work_file->data);
2991                                         file_data_ref((FileData *)work_file->data);
2992                                         work_file = work_file->next;
2993                                         }
2994                                 g_list_free(f);
2995                                 g_list_free(d);
2996                                 }
2997                         }
2998                 else
2999                         {
3000                         dw->add_files_queue = g_list_prepend(dw->add_files_queue, fd);
3001                         file_data_ref(fd);
3002                         }
3003                 }
3004         if (dw->add_files_queue_id == 0)
3005                 {
3006                 gtk_progress_bar_pulse(GTK_PROGRESS_BAR(dw->extra_label));
3007                 gtk_progress_bar_set_pulse_step(GTK_PROGRESS_BAR(dw->extra_label), DUPE_PROGRESS_PULSE_STEP);
3008                 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(dw->extra_label), _("Loading file list"));
3009
3010                 dupe_init_list_cache(dw);
3011                 dw->add_files_queue_id = g_idle_add(dupe_files_add_queue_cb, dw);
3012                 gtk_widget_set_sensitive(dw->controls_box, FALSE);
3013                 }
3014 }
3015
3016 static void dupe_item_update(DupeWindow *dw, DupeItem *di)
3017 {
3018         if ( (dw->match_mask & DUPE_MATCH_NAME) || (dw->match_mask & DUPE_MATCH_PATH || (dw->match_mask & DUPE_MATCH_NAME_CI)) )
3019                 {
3020                 /* only effects matches on name or path */
3021 /*
3022                 FileData *fd = file_data_ref(di->fd);
3023                 gint second;
3024
3025                 second = di->second;
3026                 dupe_item_remove(dw, di);
3027
3028                 dw->second_drop = second;
3029                 dupe_files_add(dw, NULL, NULL, fd, FALSE);
3030                 dw->second_drop = FALSE;
3031
3032                 file_data_unref(fd);
3033 */
3034                 dupe_check_start(dw);
3035                 }
3036         else
3037                 {
3038                 GtkListStore *store;
3039                 GtkTreeIter iter;
3040                 gint row;
3041                 /* update the listview(s) */
3042
3043                 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(dw->listview)));
3044                 row = dupe_listview_find_item(store, di, &iter);
3045                 if (row >= 0)
3046                         {
3047                         gtk_list_store_set(store, &iter,
3048                                            DUPE_COLUMN_NAME, di->fd->name,
3049                                            DUPE_COLUMN_PATH, di->fd->path, -1);
3050                         }
3051
3052                 if (dw->second_listview)
3053                         {
3054                         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(dw->second_listview)));
3055                         row = dupe_listview_find_item(store, di, &iter);
3056                         if (row >= 0)
3057                                 {
3058                                 gtk_list_store_set(store, &iter, 1, di->fd->path, -1);
3059                                 }
3060                         }
3061                 }
3062
3063 }
3064
3065 static void dupe_item_update_fd_in_list(DupeWindow *dw, FileData *fd, GList *work)
3066 {
3067         while (work)
3068                 {
3069                 auto di = static_cast<DupeItem *>(work->data);
3070
3071                 if (di->fd == fd)
3072                         dupe_item_update(dw, di);
3073
3074                 work = work->next;
3075                 }
3076 }
3077
3078 static void dupe_item_update_fd(DupeWindow *dw, FileData *fd)
3079 {
3080         dupe_item_update_fd_in_list(dw, fd, dw->list);
3081         if (dw->second_set) dupe_item_update_fd_in_list(dw, fd, dw->second_list);
3082 }
3083
3084
3085 /*
3086  * ------------------------------------------------------------------
3087  * Misc.
3088  * ------------------------------------------------------------------
3089  */
3090
3091 static GtkWidget *dupe_display_label(GtkWidget *vbox, const gchar *description, const gchar *text)
3092 {
3093         GtkWidget *hbox;
3094         GtkWidget *label;
3095
3096         hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
3097
3098         label = gtk_label_new(description);
3099         gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
3100         gtk_widget_show(label);
3101
3102         label = gtk_label_new(text);
3103         gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
3104         gtk_widget_show(label);
3105
3106         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
3107         gtk_widget_show(hbox);
3108
3109         return label;
3110 }
3111
3112 static void dupe_display_stats(DupeWindow *dw, DupeItem *di)
3113 {
3114         GenericDialog *gd;
3115         gchar *buf;
3116
3117         if (!di) return;
3118
3119         gd = file_util_gen_dlg("Image thumbprint debug info", "thumbprint",
3120                                dw->window, TRUE,
3121                                nullptr, nullptr);
3122         generic_dialog_add_button(gd, GTK_STOCK_CLOSE, nullptr, nullptr, TRUE);
3123
3124         dupe_display_label(gd->vbox, "name:", di->fd->name);
3125         buf = text_from_size(di->fd->size);
3126         dupe_display_label(gd->vbox, "size:", buf);
3127         g_free(buf);
3128         dupe_display_label(gd->vbox, "date:", text_from_time(di->fd->date));
3129         buf = g_strdup_printf("%d x %d", di->width, di->height);
3130         dupe_display_label(gd->vbox, "dimensions:", buf);
3131         g_free(buf);
3132         dupe_display_label(gd->vbox, "md5sum:", (di->md5sum) ? di->md5sum : "not generated");
3133
3134         dupe_display_label(gd->vbox, "thumbprint:", (di->simd) ? "" : "not generated");
3135         if (di->simd)
3136                 {
3137                 GtkWidget *image;
3138                 GdkPixbuf *pixbuf;
3139                 gint x, y;
3140                 guchar *d_pix;
3141                 guchar *dp;
3142                 gint rs;
3143                 gint sp;
3144
3145                 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, 32, 32);
3146                 rs = gdk_pixbuf_get_rowstride(pixbuf);
3147                 d_pix = gdk_pixbuf_get_pixels(pixbuf);
3148
3149                 for (y = 0; y < 32; y++)
3150                         {
3151                         dp = d_pix + (y * rs);
3152                         sp = y * 32;
3153                         for (x = 0; x < 32; x++)
3154                                 {
3155                                 *(dp++) = di->simd->avg_r[sp + x];
3156                                 *(dp++) = di->simd->avg_g[sp + x];
3157                                 *(dp++) = di->simd->avg_b[sp + x];
3158                                 }
3159                         }
3160
3161                 image = gtk_image_new_from_pixbuf(pixbuf);
3162                 gtk_box_pack_start(GTK_BOX(gd->vbox), image, FALSE, FALSE, 0);
3163                 gtk_widget_show(image);
3164
3165                 g_object_unref(pixbuf);
3166                 }
3167
3168         gtk_widget_show(gd->dialog);
3169 }
3170
3171 static void dupe_window_recompare(DupeWindow *dw)
3172 {
3173         GtkListStore *store;
3174
3175         dupe_check_stop(dw);
3176
3177         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(dw->listview)));
3178         gtk_list_store_clear(store);
3179
3180         g_list_free(dw->dupes);
3181         dw->dupes = nullptr;
3182
3183         dupe_match_reset_list(dw->list);
3184         dupe_match_reset_list(dw->second_list);
3185         dw->set_count = 0;
3186
3187         dupe_check_start(dw);
3188 }
3189
3190 static void dupe_menu_view(DupeWindow *dw, DupeItem *di, GtkWidget *listview, gint new_window)
3191 {
3192         if (!di) return;
3193
3194         if (di->collection && collection_info_valid(di->collection, di->info))
3195                 {
3196                 if (new_window)
3197                         {
3198                         view_window_new_from_collection(di->collection, di->info);
3199                         }
3200                 else
3201                         {
3202                         layout_image_set_collection(nullptr, di->collection, di->info);
3203                         }
3204                 }
3205         else
3206                 {
3207                 if (new_window)
3208                         {
3209                         GList *list;
3210
3211                         list = dupe_listview_get_selection(dw, listview);
3212                         view_window_new_from_list(list);
3213                         filelist_free(list);
3214                         }
3215                 else
3216                         {
3217                         layout_set_fd(nullptr, di->fd);
3218                         }
3219                 }
3220 }
3221
3222 static void dupe_window_remove_selection(DupeWindow *dw, GtkWidget *listview)
3223 {
3224         GtkTreeSelection *selection;
3225         GtkTreeModel *store;
3226         GtkTreeIter iter;
3227         GList *slist;
3228         GList *list = nullptr;
3229         GList *work;
3230
3231         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(listview));
3232         slist = gtk_tree_selection_get_selected_rows(selection, &store);
3233         work = slist;
3234         while (work)
3235                 {
3236                 auto tpath = static_cast<GtkTreePath *>(work->data);
3237                 DupeItem *di = nullptr;
3238
3239                 gtk_tree_model_get_iter(store, &iter, tpath);
3240                 gtk_tree_model_get(store, &iter, DUPE_COLUMN_POINTER, &di, -1);
3241                 if (di) list = g_list_prepend(list, di);
3242                 work = work->next;
3243                 }
3244         g_list_free_full(slist, reinterpret_cast<GDestroyNotify>(gtk_tree_path_free));
3245
3246         dw->color_frozen = TRUE;
3247         work = list;
3248         while (work)
3249                 {
3250                 DupeItem *di;
3251
3252                 di = static_cast<DupeItem *>(work->data);
3253                 work = work->next;
3254                 dupe_item_remove(dw, di);
3255                 }
3256         dw->color_frozen = FALSE;
3257
3258         g_list_free(list);
3259
3260         dupe_listview_realign_colors(dw);
3261 }
3262
3263 static void dupe_window_edit_selected(DupeWindow *dw, const gchar *key)
3264 {
3265         file_util_start_editor_from_filelist(key, dupe_listview_get_selection(dw, dw->listview), nullptr, dw->window);
3266 }
3267
3268 static void dupe_window_collection_from_selection(DupeWindow *dw)
3269 {
3270         CollectWindow *w;
3271         GList *list;
3272
3273         list = dupe_listview_get_selection(dw, dw->listview);
3274         w = collection_window_new(nullptr);
3275         collection_table_add_filelist(w->table, list);
3276         filelist_free(list);
3277 }
3278
3279 static void dupe_window_append_file_list(DupeWindow *dw, gint on_second)
3280 {
3281         GList *list;
3282
3283         dw->second_drop = (dw->second_set && on_second);
3284
3285         list = layout_list(nullptr);
3286         dupe_window_add_files(dw, list, FALSE);
3287         filelist_free(list);
3288 }
3289
3290 /*
3291  *-------------------------------------------------------------------
3292  * main pop-up menu callbacks
3293  *-------------------------------------------------------------------
3294  */
3295
3296 static void dupe_menu_view_cb(GtkWidget *, gpointer data)
3297 {
3298         auto dw = static_cast<DupeWindow *>(data);
3299
3300         if (dw->click_item) dupe_menu_view(dw, dw->click_item, dw->listview, FALSE);
3301 }
3302
3303 static void dupe_menu_viewnew_cb(GtkWidget *, gpointer data)
3304 {
3305         auto dw = static_cast<DupeWindow *>(data);
3306
3307         if (dw->click_item) dupe_menu_view(dw, dw->click_item, dw->listview, TRUE);
3308 }
3309
3310 static void dupe_menu_select_all_cb(GtkWidget *, gpointer data)
3311 {
3312         auto dw = static_cast<DupeWindow *>(data);
3313         GtkTreeSelection *selection;
3314
3315         options->duplicates_select_type = DUPE_SELECT_NONE;
3316         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dw->listview));
3317         gtk_tree_selection_select_all(selection);
3318 }
3319
3320 static void dupe_menu_select_none_cb(GtkWidget *, gpointer data)
3321 {
3322         auto dw = static_cast<DupeWindow *>(data);
3323         GtkTreeSelection *selection;
3324
3325         options->duplicates_select_type = DUPE_SELECT_NONE;
3326         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dw->listview));
3327         gtk_tree_selection_unselect_all(selection);
3328 }
3329
3330 static void dupe_menu_select_dupes_set1_cb(GtkWidget *, gpointer data)
3331 {
3332         auto dw = static_cast<DupeWindow *>(data);
3333
3334         options->duplicates_select_type = DUPE_SELECT_GROUP1;
3335         dupe_listview_select_dupes(dw, DUPE_SELECT_GROUP1);
3336 }
3337
3338 static void dupe_menu_select_dupes_set2_cb(GtkWidget *, gpointer data)
3339 {
3340         auto dw = static_cast<DupeWindow *>(data);
3341
3342         options->duplicates_select_type = DUPE_SELECT_GROUP2;
3343         dupe_listview_select_dupes(dw, DUPE_SELECT_GROUP2);
3344 }
3345
3346 static void dupe_menu_edit_cb(GtkWidget *widget, gpointer data)
3347 {
3348         DupeWindow *dw;
3349         auto key = static_cast<const gchar *>(data);
3350
3351         dw = static_cast<DupeWindow *>(submenu_item_get_data(widget));
3352         if (!dw) return;
3353
3354         dupe_window_edit_selected(dw, key);
3355 }
3356
3357 static void dupe_menu_print_cb(GtkWidget *, gpointer data)
3358 {
3359         auto dw = static_cast<DupeWindow *>(data);
3360         FileData *fd;
3361
3362         fd = (dw->click_item) ? dw->click_item->fd : nullptr;
3363
3364         print_window_new(fd,
3365                          dupe_listview_get_selection(dw, dw->listview),
3366                          dupe_listview_get_filelist(dw, dw->listview), dw->window);
3367 }
3368
3369 static void dupe_menu_copy_cb(GtkWidget *, gpointer data)
3370 {
3371         auto dw = static_cast<DupeWindow *>(data);
3372
3373         file_util_copy(nullptr, dupe_listview_get_selection(dw, dw->listview), nullptr, dw->window);
3374 }
3375
3376 static void dupe_menu_move_cb(GtkWidget *, gpointer data)
3377 {
3378         auto dw = static_cast<DupeWindow *>(data);
3379
3380         file_util_move(nullptr, dupe_listview_get_selection(dw, dw->listview), nullptr, dw->window);
3381 }
3382
3383 static void dupe_menu_rename_cb(GtkWidget *, gpointer data)
3384 {
3385         auto dw = static_cast<DupeWindow *>(data);
3386
3387         file_util_rename(nullptr, dupe_listview_get_selection(dw, dw->listview), dw->window);
3388 }
3389
3390 static void dupe_menu_delete_cb(GtkWidget *, gpointer data)
3391 {
3392         auto dw = static_cast<DupeWindow *>(data);
3393
3394         options->file_ops.safe_delete_enable = FALSE;
3395         file_util_delete_notify_done(nullptr, dupe_listview_get_selection(dw, dw->listview), dw->window, delete_finished_cb, dw);
3396 }
3397
3398 static void dupe_menu_move_to_trash_cb(GtkWidget *, gpointer data)
3399 {
3400         auto dw = static_cast<DupeWindow *>(data);
3401
3402         options->file_ops.safe_delete_enable = TRUE;
3403         file_util_delete_notify_done(nullptr, dupe_listview_get_selection(dw, dw->listview), dw->window, delete_finished_cb, dw);
3404 }
3405
3406 static void dupe_menu_copy_path_cb(GtkWidget *, gpointer data)
3407 {
3408         auto dw = static_cast<DupeWindow *>(data);
3409
3410         file_util_copy_path_list_to_clipboard(dupe_listview_get_selection(dw, dw->listview), TRUE);
3411 }
3412
3413 static void dupe_menu_copy_path_unquoted_cb(GtkWidget *, gpointer data)
3414 {
3415         auto dw = static_cast<DupeWindow *>(data);
3416
3417         file_util_copy_path_list_to_clipboard(dupe_listview_get_selection(dw, dw->listview), FALSE);
3418 }
3419
3420 static void dupe_menu_remove_cb(GtkWidget *, gpointer data)
3421 {
3422         auto dw = static_cast<DupeWindow *>(data);
3423
3424         dupe_window_remove_selection(dw, dw->listview);
3425 }
3426
3427 static void dupe_menu_clear_cb(GtkWidget *, gpointer data)
3428 {
3429         auto dw = static_cast<DupeWindow *>(data);
3430
3431         dupe_window_clear(dw);
3432 }
3433
3434 static void dupe_menu_close_cb(GtkWidget *, gpointer data)
3435 {
3436         auto dw = static_cast<DupeWindow *>(data);
3437
3438         dupe_window_close(dw);
3439 }
3440
3441 static void dupe_menu_popup_destroy_cb(GtkWidget *, gpointer data)
3442 {
3443         auto editmenu_fd_list = static_cast<GList *>(data);
3444
3445         filelist_free(editmenu_fd_list);
3446 }
3447
3448 static GList *dupe_window_get_fd_list(DupeWindow *dw)
3449 {
3450         GList *list;
3451
3452         if (gtk_widget_has_focus(dw->second_listview))
3453                 {
3454                 list = dupe_listview_get_selection(dw, dw->second_listview);
3455                 }
3456         else
3457                 {
3458                 list = dupe_listview_get_selection(dw, dw->listview);
3459                 }
3460
3461         return list;
3462 }
3463
3464 /**
3465  * @brief Add file selection list to a collection
3466  * @param[in] widget
3467  * @param[in] data Index to the collection list menu item selected, or -1 for new collection
3468  *
3469  *
3470  */
3471 static void dupe_pop_menu_collections_cb(GtkWidget *widget, gpointer data)
3472 {
3473         DupeWindow *dw;
3474         GList *selection_list;
3475
3476         dw = static_cast<DupeWindow *>(submenu_item_get_data(widget));
3477         selection_list = dupe_listview_get_selection(dw, dw->listview);
3478         pop_menu_collections(selection_list, data);
3479
3480         filelist_free(selection_list);
3481 }
3482
3483 static GtkWidget *dupe_menu_popup_main(DupeWindow *dw, DupeItem *di)
3484 {
3485         GtkWidget *menu;
3486         GtkWidget *item;
3487         gint on_row;
3488         GList *editmenu_fd_list;
3489         GtkAccelGroup *accel_group;
3490
3491         on_row = (di != nullptr);
3492
3493         menu = popup_menu_short_lived();
3494
3495         accel_group = gtk_accel_group_new();
3496         gtk_menu_set_accel_group(GTK_MENU(menu), accel_group);
3497
3498         g_object_set_data(G_OBJECT(menu), "window_keys", dupe_window_keys);
3499         g_object_set_data(G_OBJECT(menu), "accel_group", accel_group);
3500
3501         menu_item_add_sensitive(menu, _("_View"), on_row,
3502                                 G_CALLBACK(dupe_menu_view_cb), dw);
3503         menu_item_add_stock_sensitive(menu, _("View in _new window"), GTK_STOCK_NEW, on_row,
3504                                 G_CALLBACK(dupe_menu_viewnew_cb), dw);
3505         menu_item_add_divider(menu);
3506         menu_item_add_sensitive(menu, _("Select all"), (dw->dupes != nullptr),
3507                                 G_CALLBACK(dupe_menu_select_all_cb), dw);
3508         menu_item_add_sensitive(menu, _("Select none"), (dw->dupes != nullptr),
3509                                 G_CALLBACK(dupe_menu_select_none_cb), dw);
3510         menu_item_add_sensitive(menu, _("Select group _1 duplicates"), (dw->dupes != nullptr),
3511                                 G_CALLBACK(dupe_menu_select_dupes_set1_cb), dw);
3512         menu_item_add_sensitive(menu, _("Select group _2 duplicates"), (dw->dupes != nullptr),
3513                                 G_CALLBACK(dupe_menu_select_dupes_set2_cb), dw);
3514         menu_item_add_divider(menu);
3515
3516         submenu_add_export(menu, &item, G_CALLBACK(dupe_pop_menu_export_cb), dw);
3517         gtk_widget_set_sensitive(item, on_row);
3518         menu_item_add_divider(menu);
3519
3520         editmenu_fd_list = dupe_window_get_fd_list(dw);
3521         g_signal_connect(G_OBJECT(menu), "destroy",
3522                          G_CALLBACK(dupe_menu_popup_destroy_cb), editmenu_fd_list);
3523         submenu_add_edit(menu, &item, G_CALLBACK(dupe_menu_edit_cb), dw, editmenu_fd_list);
3524         if (!on_row) gtk_widget_set_sensitive(item, FALSE);
3525
3526         submenu_add_collections(menu, &item,
3527                                                                 G_CALLBACK(dupe_pop_menu_collections_cb), dw);
3528         gtk_widget_set_sensitive(item, on_row);
3529
3530         menu_item_add_stock_sensitive(menu, _("Print..."), GTK_STOCK_PRINT, on_row,
3531                                 G_CALLBACK(dupe_menu_print_cb), dw);
3532         menu_item_add_divider(menu);
3533         menu_item_add_stock_sensitive(menu, _("_Copy..."), GTK_STOCK_COPY, on_row,
3534                                 G_CALLBACK(dupe_menu_copy_cb), dw);
3535         menu_item_add_sensitive(menu, _("_Move..."), on_row,
3536                                 G_CALLBACK(dupe_menu_move_cb), dw);
3537         menu_item_add_sensitive(menu, _("_Rename..."), on_row,
3538                                 G_CALLBACK(dupe_menu_rename_cb), dw);
3539         menu_item_add_sensitive(menu, _("_Copy path"), on_row,
3540                                 G_CALLBACK(dupe_menu_copy_path_cb), dw);
3541         menu_item_add_sensitive(menu, _("_Copy path unquoted"), on_row,
3542                                 G_CALLBACK(dupe_menu_copy_path_unquoted_cb), dw);
3543
3544         menu_item_add_divider(menu);
3545         menu_item_add_stock_sensitive(menu,
3546                                 options->file_ops.confirm_move_to_trash ? _("Move to Trash...") :
3547                                         _("Move to Trash"), PIXBUF_INLINE_ICON_TRASH, on_row,
3548                                 G_CALLBACK(dupe_menu_move_to_trash_cb), dw);
3549         menu_item_add_stock_sensitive(menu,
3550                                 options->file_ops.confirm_delete ? _("_Delete...") :
3551                                         _("_Delete"), GTK_STOCK_DELETE, on_row,
3552                                 G_CALLBACK(dupe_menu_delete_cb), dw);
3553
3554         menu_item_add_divider(menu);
3555         menu_item_add_stock_sensitive(menu, _("Rem_ove"), GTK_STOCK_REMOVE, on_row,
3556                                 G_CALLBACK(dupe_menu_remove_cb), dw);
3557         menu_item_add_stock_sensitive(menu, _("C_lear"), GTK_STOCK_CLEAR, (dw->list != nullptr),
3558                                 G_CALLBACK(dupe_menu_clear_cb), dw);
3559         menu_item_add_divider(menu);
3560         menu_item_add_stock(menu, _("Close _window"), GTK_STOCK_CLOSE,
3561                             G_CALLBACK(dupe_menu_close_cb), dw);
3562
3563         return menu;
3564 }
3565
3566 static gboolean dupe_listview_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
3567 {
3568         auto dw = static_cast<DupeWindow *>(data);
3569         GtkTreeModel *store;
3570         GtkTreePath *tpath;
3571         GtkTreeIter iter;
3572         DupeItem *di = nullptr;
3573
3574         store = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
3575
3576         if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), bevent->x, bevent->y,
3577                                           &tpath, nullptr, nullptr, nullptr))
3578                 {
3579                 gtk_tree_model_get_iter(store, &iter, tpath);
3580                 gtk_tree_model_get(store, &iter, DUPE_COLUMN_POINTER, &di, -1);
3581                 gtk_tree_path_free(tpath);
3582                 }
3583
3584         dw->click_item = di;
3585
3586         if (bevent->button == MOUSE_BUTTON_RIGHT)
3587                 {
3588                 /* right click menu */
3589                 GtkWidget *menu;
3590
3591                 if (bevent->state & GDK_CONTROL_MASK && bevent->state & GDK_SHIFT_MASK)
3592                         {
3593                         dupe_display_stats(dw, di);
3594                         return TRUE;
3595                         }
3596                 if (widget == dw->listview)
3597                         {
3598                         menu = dupe_menu_popup_main(dw, di);
3599                         }
3600                 else
3601                         {
3602                         menu = dupe_menu_popup_second(dw, di);
3603                         }
3604                 gtk_menu_popup_at_pointer(GTK_MENU(menu), nullptr);
3605                 }
3606
3607         if (!di) return FALSE;
3608
3609         if (bevent->button == MOUSE_BUTTON_LEFT &&
3610             bevent->type == GDK_2BUTTON_PRESS)
3611                 {
3612                 dupe_menu_view(dw, di, widget, FALSE);
3613                 }
3614
3615         if (bevent->button == MOUSE_BUTTON_MIDDLE) return TRUE;
3616
3617         if (bevent->button == MOUSE_BUTTON_RIGHT)
3618                 {
3619                 if (!dupe_listview_item_is_selected(dw, di, widget))
3620                         {
3621                         GtkTreeSelection *selection;
3622
3623                         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
3624                         gtk_tree_selection_unselect_all(selection);
3625                         gtk_tree_selection_select_iter(selection, &iter);
3626
3627                         tpath = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &iter);
3628                         gtk_tree_view_set_cursor(GTK_TREE_VIEW(widget), tpath, nullptr, FALSE);
3629                         gtk_tree_path_free(tpath);
3630                         }
3631
3632                 return TRUE;
3633                 }
3634
3635         if (bevent->button == MOUSE_BUTTON_LEFT &&
3636             bevent->type == GDK_BUTTON_PRESS &&
3637             !(bevent->state & GDK_SHIFT_MASK ) &&
3638             !(bevent->state & GDK_CONTROL_MASK ) &&
3639             dupe_listview_item_is_selected(dw, di, widget))
3640                 {
3641                 /* this selection handled on release_cb */
3642                 gtk_widget_grab_focus(widget);
3643                 return TRUE;
3644                 }
3645
3646         return FALSE;
3647 }
3648
3649 static gboolean dupe_listview_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
3650 {
3651         auto dw = static_cast<DupeWindow *>(data);
3652         GtkTreeModel *store;
3653         GtkTreePath *tpath;
3654         GtkTreeIter iter;
3655         DupeItem *di = nullptr;
3656
3657         if (bevent->button != MOUSE_BUTTON_LEFT && bevent->button != MOUSE_BUTTON_MIDDLE) return TRUE;
3658
3659         store = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
3660
3661         if ((bevent->x != 0 || bevent->y != 0) &&
3662             gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), bevent->x, bevent->y,
3663                                           &tpath, nullptr, nullptr, nullptr))
3664                 {
3665                 gtk_tree_model_get_iter(store, &iter, tpath);
3666                 gtk_tree_model_get(store, &iter, DUPE_COLUMN_POINTER, &di, -1);
3667                 gtk_tree_path_free(tpath);
3668                 }
3669
3670         if (bevent->button == MOUSE_BUTTON_MIDDLE)
3671                 {
3672                 if (di && dw->click_item == di)
3673                         {
3674                         GtkTreeSelection *selection;
3675
3676                         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
3677                         if (dupe_listview_item_is_selected(dw, di, widget))
3678                                 {
3679                                 gtk_tree_selection_unselect_iter(selection, &iter);
3680                                 }
3681                         else
3682                                 {
3683                                 gtk_tree_selection_select_iter(selection, &iter);
3684                                 }
3685                         }
3686                 return TRUE;
3687                 }
3688
3689         if (di && dw->click_item == di &&
3690             !(bevent->state & GDK_SHIFT_MASK ) &&
3691             !(bevent->state & GDK_CONTROL_MASK ) &&
3692             dupe_listview_item_is_selected(dw, di, widget))
3693                 {
3694                 GtkTreeSelection *selection;
3695
3696                 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
3697                 gtk_tree_selection_unselect_all(selection);
3698                 gtk_tree_selection_select_iter(selection, &iter);
3699
3700                 tpath = gtk_tree_model_get_path(store, &iter);
3701                 gtk_tree_view_set_cursor(GTK_TREE_VIEW(widget), tpath, nullptr, FALSE);
3702                 gtk_tree_path_free(tpath);
3703
3704                 return TRUE;
3705                 }
3706
3707         return FALSE;
3708 }
3709
3710 /*
3711  *-------------------------------------------------------------------
3712  * second set stuff
3713  *-------------------------------------------------------------------
3714  */
3715
3716 static void dupe_second_update_status(DupeWindow *dw)
3717 {
3718         gchar *buf;
3719
3720         buf = g_strdup_printf(_("%d files (set 2)"), g_list_length(dw->second_list));
3721         gtk_label_set_text(GTK_LABEL(dw->second_status_label), buf);
3722         g_free(buf);
3723 }
3724
3725 static void dupe_second_add(DupeWindow *dw, DupeItem *di)
3726 {
3727         GtkListStore *store;
3728         GtkTreeIter iter;
3729
3730         if (!di) return;
3731
3732         di->second = TRUE;
3733         dw->second_list = g_list_prepend(dw->second_list, di);
3734
3735         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(dw->second_listview)));
3736         gtk_list_store_append(store, &iter);
3737         gtk_list_store_set(store, &iter, DUPE_COLUMN_POINTER, di, 1, di->fd->path, -1);
3738
3739         dupe_second_update_status(dw);
3740 }
3741
3742 static void dupe_second_remove(DupeWindow *dw, DupeItem *di)
3743 {
3744         GtkListStore *store;
3745         GtkTreeIter iter;
3746
3747         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(dw->second_listview)));
3748         if (dupe_listview_find_item(store, di, &iter) >= 0)
3749                 {
3750                 tree_view_move_cursor_away(GTK_TREE_VIEW(dw->second_listview), &iter, TRUE);
3751                 gtk_list_store_remove(store, &iter);
3752                 }
3753
3754         dw->second_list = g_list_remove(dw->second_list, di);
3755
3756         dupe_second_update_status(dw);
3757 }
3758
3759 static void dupe_second_clear(DupeWindow *dw)
3760 {
3761         GtkListStore *store;
3762
3763         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(dw->second_listview)));
3764         gtk_list_store_clear(store);
3765         gtk_tree_view_columns_autosize(GTK_TREE_VIEW(dw->second_listview));
3766
3767         g_list_free(dw->dupes);
3768         dw->dupes = nullptr;
3769
3770         g_list_free_full(dw->second_list, reinterpret_cast<GDestroyNotify>(dupe_item_free));
3771         dw->second_list = nullptr;
3772
3773         dupe_match_reset_list(dw->list);
3774
3775         dupe_second_update_status(dw);
3776 }
3777
3778 static void dupe_second_menu_view_cb(GtkWidget *, gpointer data)
3779 {
3780         auto dw = static_cast<DupeWindow *>(data);
3781
3782         if (dw->click_item) dupe_menu_view(dw, dw->click_item, dw->second_listview, FALSE);
3783 }
3784
3785 static void dupe_second_menu_viewnew_cb(GtkWidget *, gpointer data)
3786 {
3787         auto dw = static_cast<DupeWindow *>(data);
3788
3789         if (dw->click_item) dupe_menu_view(dw, dw->click_item, dw->second_listview, TRUE);
3790 }
3791
3792 static void dupe_second_menu_select_all_cb(GtkWidget *, gpointer data)
3793 {
3794         GtkTreeSelection *selection;
3795         auto dw = static_cast<DupeWindow *>(data);
3796
3797         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dw->second_listview));
3798         gtk_tree_selection_select_all(selection);
3799 }
3800
3801 static void dupe_second_menu_select_none_cb(GtkWidget *, gpointer data)
3802 {
3803         GtkTreeSelection *selection;
3804         auto dw = static_cast<DupeWindow *>(data);
3805
3806         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dw->second_listview));
3807         gtk_tree_selection_unselect_all(selection);
3808 }
3809
3810 static void dupe_second_menu_remove_cb(GtkWidget *, gpointer data)
3811 {
3812         auto dw = static_cast<DupeWindow *>(data);
3813
3814         dupe_window_remove_selection(dw, dw->second_listview);
3815 }
3816
3817 static void dupe_second_menu_clear_cb(GtkWidget *, gpointer data)
3818 {
3819         auto dw = static_cast<DupeWindow *>(data);
3820
3821         dupe_second_clear(dw);
3822         dupe_window_recompare(dw);
3823 }
3824
3825 static GtkWidget *dupe_menu_popup_second(DupeWindow *dw, DupeItem *di)
3826 {
3827         GtkWidget *menu;
3828         gboolean notempty = (dw->second_list != nullptr);
3829         gboolean on_row = (di != nullptr);
3830         GtkAccelGroup *accel_group;
3831
3832         menu = popup_menu_short_lived();
3833         accel_group = gtk_accel_group_new();
3834         gtk_menu_set_accel_group(GTK_MENU(menu), accel_group);
3835
3836         g_object_set_data(G_OBJECT(menu), "window_keys", dupe_window_keys);
3837         g_object_set_data(G_OBJECT(menu), "accel_group", accel_group);
3838
3839         menu_item_add_sensitive(menu, _("_View"), on_row,
3840                                 G_CALLBACK(dupe_second_menu_view_cb), dw);
3841         menu_item_add_stock_sensitive(menu, _("View in _new window"), GTK_STOCK_NEW, on_row,
3842                                 G_CALLBACK(dupe_second_menu_viewnew_cb), dw);
3843         menu_item_add_divider(menu);
3844         menu_item_add_sensitive(menu, _("Select all"), notempty,
3845                                 G_CALLBACK(dupe_second_menu_select_all_cb), dw);
3846         menu_item_add_sensitive(menu, _("Select none"), notempty,
3847                                 G_CALLBACK(dupe_second_menu_select_none_cb), dw);
3848         menu_item_add_divider(menu);
3849         menu_item_add_stock_sensitive(menu, _("Rem_ove"), GTK_STOCK_REMOVE, on_row,
3850                                       G_CALLBACK(dupe_second_menu_remove_cb), dw);
3851         menu_item_add_stock_sensitive(menu, _("C_lear"), GTK_STOCK_CLEAR, notempty,
3852                                    G_CALLBACK(dupe_second_menu_clear_cb), dw);
3853         menu_item_add_divider(menu);
3854         menu_item_add_stock(menu, _("Close _window"), GTK_STOCK_CLOSE,
3855                             G_CALLBACK(dupe_menu_close_cb), dw);
3856
3857         return menu;
3858 }
3859
3860 static void dupe_second_set_toggle_cb(GtkWidget *widget, gpointer data)
3861 {
3862         auto dw = static_cast<DupeWindow *>(data);
3863
3864         dw->second_set = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
3865
3866         if (dw->second_set)
3867                 {
3868                 dupe_second_update_status(dw);
3869                 gtk_table_set_col_spacings(GTK_TABLE(dw->table), PREF_PAD_GAP);
3870                 gtk_widget_show(dw->second_vbox);
3871                 }
3872         else
3873                 {
3874                 gtk_table_set_col_spacings(GTK_TABLE(dw->table), 0);
3875                 gtk_widget_hide(dw->second_vbox);
3876                 dupe_second_clear(dw);
3877                 }
3878
3879         dupe_window_recompare(dw);
3880 }
3881
3882 static void dupe_sort_totals_toggle_cb(GtkWidget *widget, gpointer data)
3883 {
3884         auto dw = static_cast<DupeWindow *>(data);
3885
3886         options->sort_totals = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
3887         dupe_window_recompare(dw);
3888
3889 }
3890
3891 /*
3892  *-------------------------------------------------------------------
3893  * match type menu
3894  *-------------------------------------------------------------------
3895  */
3896
3897 enum {
3898         DUPE_MENU_COLUMN_NAME = 0,
3899         DUPE_MENU_COLUMN_MASK
3900 };
3901
3902 static void dupe_listview_show_rank(GtkWidget *listview, gboolean rank);
3903
3904 static void dupe_menu_type_cb(GtkWidget *combo, gpointer data)
3905 {
3906         auto dw = static_cast<DupeWindow *>(data);
3907         GtkTreeModel *store;
3908         GtkTreeIter iter;
3909
3910         store = gtk_combo_box_get_model(GTK_COMBO_BOX(combo));
3911         if (!gtk_combo_box_get_active_iter(GTK_COMBO_BOX(combo), &iter)) return;
3912         gtk_tree_model_get(store, &iter, DUPE_MENU_COLUMN_MASK, &dw->match_mask, -1);
3913
3914         options->duplicates_match = dw->match_mask;
3915
3916         if (dw->match_mask & (DUPE_MATCH_SIM_HIGH | DUPE_MATCH_SIM_MED | DUPE_MATCH_SIM_LOW | DUPE_MATCH_SIM_CUSTOM))
3917                 {
3918                 dupe_listview_show_rank(dw->listview, TRUE);
3919                 }
3920         else
3921                 {
3922                 dupe_listview_show_rank(dw->listview, FALSE);
3923                 }
3924         dupe_window_recompare(dw);
3925 }
3926
3927 static void dupe_menu_add_item(GtkListStore *store, const gchar *text, DupeMatchType type, DupeWindow *dw)
3928 {
3929         GtkTreeIter iter;
3930
3931         gtk_list_store_append(store, &iter);
3932         gtk_list_store_set(store, &iter, DUPE_MENU_COLUMN_NAME, text,
3933                                          DUPE_MENU_COLUMN_MASK, type, -1);
3934
3935         if (dw->match_mask == type) gtk_combo_box_set_active_iter(GTK_COMBO_BOX(dw->combo), &iter);
3936 }
3937
3938 static void dupe_menu_setup(DupeWindow *dw)
3939 {
3940         GtkListStore *store;
3941         GtkCellRenderer *renderer;
3942
3943         store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT);
3944         dw->combo = gtk_combo_box_new_with_model(GTK_TREE_MODEL(store));
3945         g_object_unref(store);
3946
3947         renderer = gtk_cell_renderer_text_new();
3948         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(dw->combo), renderer, TRUE);
3949         gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(dw->combo), renderer,
3950                                        "text", DUPE_MENU_COLUMN_NAME, NULL);
3951
3952         dupe_menu_add_item(store, _("Name"), DUPE_MATCH_NAME, dw);
3953         dupe_menu_add_item(store, _("Name case-insensitive"), DUPE_MATCH_NAME_CI, dw);
3954         dupe_menu_add_item(store, _("Size"), DUPE_MATCH_SIZE, dw);
3955         dupe_menu_add_item(store, _("Date"), DUPE_MATCH_DATE, dw);
3956         dupe_menu_add_item(store, _("Dimensions"), DUPE_MATCH_DIM, dw);
3957         dupe_menu_add_item(store, _("Checksum"), DUPE_MATCH_SUM, dw);
3958         dupe_menu_add_item(store, _("Path"), DUPE_MATCH_PATH, dw);
3959         dupe_menu_add_item(store, _("Similarity (high - 95)"), DUPE_MATCH_SIM_HIGH, dw);
3960         dupe_menu_add_item(store, _("Similarity (med. - 90)"), DUPE_MATCH_SIM_MED, dw);
3961         dupe_menu_add_item(store, _("Similarity (low - 85)"), DUPE_MATCH_SIM_LOW, dw);
3962         dupe_menu_add_item(store, _("Similarity (custom)"), DUPE_MATCH_SIM_CUSTOM, dw);
3963         dupe_menu_add_item(store, _("Name â‰  content"), DUPE_MATCH_NAME_CONTENT, dw);
3964         dupe_menu_add_item(store, _("Name case-insensitive â‰  content"), DUPE_MATCH_NAME_CI_CONTENT, dw);
3965         dupe_menu_add_item(store, _("Show all"), DUPE_MATCH_ALL, dw);
3966
3967         g_signal_connect(G_OBJECT(dw->combo), "changed",
3968                          G_CALLBACK(dupe_menu_type_cb), dw);
3969 }
3970
3971 /*
3972  *-------------------------------------------------------------------
3973  * list view columns
3974  *-------------------------------------------------------------------
3975  */
3976
3977 /* this overrides the low default of a GtkCellRenderer from 100 to CELL_HEIGHT_OVERRIDE, something sane for our purposes */
3978
3979 #define CELL_HEIGHT_OVERRIDE 512
3980
3981 void cell_renderer_height_override(GtkCellRenderer *renderer)
3982 {
3983         GParamSpec *spec;
3984
3985         spec = g_object_class_find_property(G_OBJECT_GET_CLASS(G_OBJECT(renderer)), "height");
3986         if (spec && G_IS_PARAM_SPEC_INT(spec))
3987                 {
3988                 GParamSpecInt *spec_int;
3989
3990                 spec_int = G_PARAM_SPEC_INT(spec);
3991                 if (spec_int->maximum < CELL_HEIGHT_OVERRIDE) spec_int->maximum = CELL_HEIGHT_OVERRIDE;
3992                 }
3993 }
3994
3995 static GdkColor *dupe_listview_color_shifted(GtkWidget *widget)
3996 {
3997         static GdkColor color;
3998         static GtkWidget *done = nullptr;
3999
4000         if (done != widget)
4001                 {
4002                 GtkStyle *style;
4003
4004                 style = gtk_widget_get_style(widget);
4005                 memcpy(&color, &style->base[GTK_STATE_NORMAL], sizeof(color));
4006                 shift_color(&color, -1, 0);
4007                 done = widget;
4008                 }
4009
4010         return &color;
4011 }
4012
4013 static void dupe_listview_color_cb(GtkTreeViewColumn *, GtkCellRenderer *cell,
4014                                    GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
4015 {
4016         auto dw = static_cast<DupeWindow *>(data);
4017         gboolean set;
4018
4019         gtk_tree_model_get(tree_model, iter, DUPE_COLUMN_COLOR, &set, -1);
4020         g_object_set(G_OBJECT(cell),
4021                      "cell-background-gdk", dupe_listview_color_shifted(dw->listview),
4022                      "cell-background-set", set, NULL);
4023 }
4024
4025 static void dupe_listview_add_column(DupeWindow *dw, GtkWidget *listview, gint n, const gchar *title, gboolean image, gboolean right_justify)
4026 {
4027         GtkTreeViewColumn *column;
4028         GtkCellRenderer *renderer;
4029
4030         column = gtk_tree_view_column_new();
4031         gtk_tree_view_column_set_title(column, title);
4032         gtk_tree_view_column_set_min_width(column, 4);
4033         gtk_tree_view_column_set_sort_column_id(column, n);
4034
4035         if (n != DUPE_COLUMN_RANK &&
4036             n != DUPE_COLUMN_THUMB)
4037                 {
4038                 gtk_tree_view_column_set_resizable(column, TRUE);
4039                 }
4040
4041         if (!image)
4042                 {
4043                 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_GROW_ONLY);
4044                 renderer = gtk_cell_renderer_text_new();
4045                 if (right_justify)
4046                         {
4047                         g_object_set(G_OBJECT(renderer), "xalign", 1.0, NULL);
4048                         }
4049                 gtk_tree_view_column_pack_start(column, renderer, TRUE);
4050                 gtk_tree_view_column_add_attribute(column, renderer, "text", n);
4051                 }
4052         else
4053                 {
4054                 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
4055                 renderer = gtk_cell_renderer_pixbuf_new();
4056                 cell_renderer_height_override(renderer);
4057                 gtk_tree_view_column_pack_start(column, renderer, TRUE);
4058                 gtk_tree_view_column_add_attribute(column, renderer, "pixbuf", n);
4059                 }
4060
4061         if (listview == dw->listview)
4062                 {
4063                 /* sets background before rendering */
4064                 gtk_tree_view_column_set_cell_data_func(column, renderer, dupe_listview_color_cb, dw, nullptr);
4065                 }
4066
4067         gtk_tree_view_append_column(GTK_TREE_VIEW(listview), column);
4068 }
4069
4070 static void dupe_listview_set_height(GtkWidget *listview, gboolean thumb)
4071 {
4072         GtkTreeViewColumn *column;
4073         GtkCellRenderer *cell;
4074         GList *list;
4075
4076         column = gtk_tree_view_get_column(GTK_TREE_VIEW(listview), DUPE_COLUMN_THUMB - 1);
4077         if (!column) return;
4078
4079         gtk_tree_view_column_set_fixed_width(column, (thumb) ? options->thumbnails.max_width : 4);
4080         gtk_tree_view_column_set_visible(column, thumb);
4081
4082         list = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(column));
4083         if (!list) return;
4084         cell = static_cast<GtkCellRenderer *>(list->data);
4085         g_list_free(list);
4086
4087         g_object_set(G_OBJECT(cell), "height", (thumb) ? options->thumbnails.max_height : -1, NULL);
4088         gtk_tree_view_columns_autosize(GTK_TREE_VIEW(listview));
4089 }
4090
4091 static void dupe_listview_show_rank(GtkWidget *listview, gboolean rank)
4092 {
4093         GtkTreeViewColumn *column;
4094
4095         column = gtk_tree_view_get_column(GTK_TREE_VIEW(listview), DUPE_COLUMN_RANK - 1);
4096         if (!column) return;
4097
4098         gtk_tree_view_column_set_visible(column, rank);
4099 }
4100
4101 /*
4102  *-------------------------------------------------------------------
4103  * misc cb
4104  *-------------------------------------------------------------------
4105  */
4106
4107 static void dupe_window_show_thumb_cb(GtkWidget *widget, gpointer data)
4108 {
4109         auto dw = static_cast<DupeWindow *>(data);
4110
4111         dw->show_thumbs = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
4112         options->duplicates_thumbnails = dw->show_thumbs;
4113
4114         if (dw->show_thumbs)
4115                 {
4116                 if (!dw->working) dupe_thumb_step(dw);
4117                 }
4118         else
4119                 {
4120                 GtkTreeModel *store;
4121                 GtkTreeIter iter;
4122                 gboolean valid;
4123
4124                 thumb_loader_free(dw->thumb_loader);
4125                 dw->thumb_loader = nullptr;
4126
4127                 store = gtk_tree_view_get_model(GTK_TREE_VIEW(dw->listview));
4128                 valid = gtk_tree_model_get_iter_first(store, &iter);
4129
4130                 while (valid)
4131                         {
4132                         gtk_list_store_set(GTK_LIST_STORE(store), &iter, DUPE_COLUMN_THUMB, NULL, -1);
4133                         valid = gtk_tree_model_iter_next(store, &iter);
4134                         }
4135                 dupe_window_update_progress(dw, nullptr, 0.0, FALSE);
4136                 }
4137
4138         dupe_listview_set_height(dw->listview, dw->show_thumbs);
4139 }
4140
4141 static void dupe_window_rotation_invariant_cb(GtkWidget *widget, gpointer data)
4142 {
4143         auto dw = static_cast<DupeWindow *>(data);
4144
4145         options->rot_invariant_sim = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
4146         dupe_window_recompare(dw);
4147 }
4148
4149 static void dupe_window_custom_threshold_cb(GtkWidget *widget, gpointer data)
4150 {
4151         auto dw = static_cast<DupeWindow *>(data);
4152         DupeMatchType match_type;
4153         GtkTreeModel *store;
4154         gboolean valid;
4155         GtkTreeIter iter;
4156
4157         options->duplicates_similarity_threshold = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget));
4158         dw->match_mask = DUPE_MATCH_SIM_CUSTOM;
4159
4160         store = gtk_combo_box_get_model(GTK_COMBO_BOX(dw->combo));
4161         valid = gtk_tree_model_get_iter_first(store, &iter);
4162         while (valid)
4163                 {
4164                 gtk_tree_model_get(store, &iter, DUPE_MENU_COLUMN_MASK, &match_type, -1);
4165                 if (match_type == DUPE_MATCH_SIM_CUSTOM)
4166                         {
4167                         break;
4168                         }
4169                 valid = gtk_tree_model_iter_next(store, &iter);
4170                 }
4171
4172         gtk_combo_box_set_active_iter(GTK_COMBO_BOX(dw->combo), &iter);
4173         dupe_window_recompare(dw);
4174 }
4175
4176 static gboolean dupe_window_keypress_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
4177 {
4178         auto dw = static_cast<DupeWindow *>(data);
4179         gboolean stop_signal = FALSE;
4180         gboolean on_second;
4181         GtkWidget *listview;
4182         GtkTreeModel *store;
4183         GtkTreeSelection *selection;
4184         GList *slist;
4185         DupeItem *di = nullptr;
4186
4187         on_second = gtk_widget_has_focus(dw->second_listview);
4188
4189         if (on_second)
4190                 {
4191                 listview = dw->second_listview;
4192                 }
4193         else
4194                 {
4195                 listview = dw->listview;
4196                 }
4197
4198         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(listview));
4199         slist = gtk_tree_selection_get_selected_rows(selection, &store);
4200         if (slist)
4201                 {
4202                 GtkTreePath *tpath;
4203                 GtkTreeIter iter;
4204                 GList *last;
4205
4206                 last = g_list_last(slist);
4207                 tpath = static_cast<GtkTreePath *>(last->data);
4208
4209                 /* last is newest selected file */
4210                 gtk_tree_model_get_iter(store, &iter, tpath);
4211                 gtk_tree_model_get(store, &iter, DUPE_COLUMN_POINTER, &di, -1);
4212                 }
4213         g_list_free_full(slist, reinterpret_cast<GDestroyNotify>(gtk_tree_path_free));
4214
4215         if (event->state & GDK_CONTROL_MASK)
4216                 {
4217                 if (!on_second)
4218                         {
4219                         stop_signal = TRUE;
4220                         switch (event->keyval)
4221                                 {
4222                                 case '1':
4223                                 case '2':
4224                                 case '3':
4225                                 case '4':
4226                                 case '5':
4227                                 case '6':
4228                                 case '7':
4229                                 case '8':
4230                                 case '9':
4231                                 case '0':
4232                                         break;
4233                                 case 'C': case 'c':
4234                                         file_util_copy(nullptr, dupe_listview_get_selection(dw, listview),
4235                                                        nullptr, dw->window);
4236                                         break;
4237                                 case 'M': case 'm':
4238                                         file_util_move(nullptr, dupe_listview_get_selection(dw, listview),
4239                                                        nullptr, dw->window);
4240                                         break;
4241                                 case 'R': case 'r':
4242                                         file_util_rename(nullptr, dupe_listview_get_selection(dw, listview), dw->window);
4243                                         break;
4244                                 case 'D': case 'd':
4245                                         options->file_ops.safe_delete_enable = TRUE;
4246                                         file_util_delete(nullptr, dupe_listview_get_selection(dw, listview), dw->window);
4247                                         break;
4248                                 default:
4249                                         stop_signal = FALSE;
4250                                         break;
4251                                 }
4252                         }
4253
4254                 if (!stop_signal)
4255                         {
4256                         stop_signal = TRUE;
4257                         switch (event->keyval)
4258                                 {
4259                                 case 'A': case 'a':
4260                                         if (event->state & GDK_SHIFT_MASK)
4261                                                 {
4262                                                 gtk_tree_selection_unselect_all(selection);
4263                                                 }
4264                                         else
4265                                                 {
4266                                                 gtk_tree_selection_select_all(selection);
4267                                                 }
4268                                         break;
4269                                 case GDK_KEY_Delete: case GDK_KEY_KP_Delete:
4270                                         if (on_second)
4271                                                 {
4272                                                 dupe_second_clear(dw);
4273                                                 dupe_window_recompare(dw);
4274                                                 }
4275                                         else
4276                                                 {
4277                                                 dupe_window_clear(dw);
4278                                                 }
4279                                         break;
4280                                 case 'L': case 'l':
4281                                         dupe_window_append_file_list(dw, FALSE);
4282                                         break;
4283                                 case 'T': case 't':
4284                                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dw->button_thumbs),
4285                                                 !gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dw->button_thumbs)));
4286                                         break;
4287                                 case 'W': case 'w':
4288                                         dupe_window_close(dw);
4289                                         break;
4290                                 default:
4291                                         stop_signal = FALSE;
4292                                         break;
4293                                 }
4294                         }
4295                 }
4296         else
4297                 {
4298                 stop_signal = TRUE;
4299                 switch (event->keyval)
4300                         {
4301                         case GDK_KEY_Return: case GDK_KEY_KP_Enter:
4302                                 dupe_menu_view(dw, di, listview, FALSE);
4303                                 break;
4304                         case 'V': case 'v':
4305                                 dupe_menu_view(dw, di, listview, TRUE);
4306                                 break;
4307                         case GDK_KEY_Delete: case GDK_KEY_KP_Delete:
4308                                 dupe_window_remove_selection(dw, listview);
4309                                 break;
4310                         case 'C': case 'c':
4311                                 if (!on_second)
4312                                         {
4313                                         dupe_window_collection_from_selection(dw);
4314                                         }
4315                                 break;
4316                         case '0':
4317                                 options->duplicates_select_type = DUPE_SELECT_NONE;
4318                                 dupe_listview_select_dupes(dw, DUPE_SELECT_NONE);
4319                                 break;
4320                         case '1':
4321                                 options->duplicates_select_type = DUPE_SELECT_GROUP1;
4322                                 dupe_listview_select_dupes(dw, DUPE_SELECT_GROUP1);
4323                                 break;
4324                         case '2':
4325                                 options->duplicates_select_type = DUPE_SELECT_GROUP2;
4326                                 dupe_listview_select_dupes(dw, DUPE_SELECT_GROUP2);
4327                                 break;
4328                         case GDK_KEY_Menu:
4329                         case GDK_KEY_F10:
4330                                 if (!on_second)
4331                                         {
4332                                         GtkWidget *menu;
4333
4334                                         menu = dupe_menu_popup_main(dw, di);
4335                                         gtk_menu_popup_at_widget(GTK_MENU(menu), widget, GDK_GRAVITY_CENTER, GDK_GRAVITY_CENTER, nullptr);
4336                                         }
4337                                 else
4338                                         {
4339                                         GtkWidget *menu;
4340
4341                                         menu = dupe_menu_popup_second(dw, di);
4342                                         gtk_menu_popup_at_widget(GTK_MENU(menu), widget, GDK_GRAVITY_CENTER, GDK_GRAVITY_CENTER, nullptr);
4343                                         }
4344                                 break;
4345                         default:
4346                                 stop_signal = FALSE;
4347                                 break;
4348                         }
4349                 }
4350         if (!stop_signal && is_help_key(event))
4351                 {
4352                 help_window_show("GuideImageSearchFindingDuplicates.html");
4353                 stop_signal = TRUE;
4354                 }
4355
4356         return stop_signal;
4357 }
4358
4359
4360 void dupe_window_clear(DupeWindow *dw)
4361 {
4362         GtkListStore *store;
4363
4364         dupe_check_stop(dw);
4365
4366         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(dw->listview)));
4367         gtk_list_store_clear(store);
4368         gtk_tree_view_columns_autosize(GTK_TREE_VIEW(dw->listview));
4369
4370         g_list_free(dw->dupes);
4371         dw->dupes = nullptr;
4372
4373         g_list_free_full(dw->list, reinterpret_cast<GDestroyNotify>(dupe_item_free));
4374         dw->list = nullptr;
4375         dw->set_count = 0;
4376
4377         dupe_match_reset_list(dw->second_list);
4378
4379         dupe_window_update_count(dw, FALSE);
4380         dupe_window_update_progress(dw, nullptr, 0.0, FALSE);
4381 }
4382
4383 static void dupe_window_get_geometry(DupeWindow *dw)
4384 {
4385         GdkWindow *window;
4386         LayoutWindow *lw = nullptr;
4387
4388         layout_valid(&lw);
4389
4390         if (!dw || !lw) return;
4391
4392         window = gtk_widget_get_window(dw->window);
4393         gdk_window_get_position(window, &lw->options.dupe_window.x, &lw->options.dupe_window.y);
4394         lw->options.dupe_window.w = gdk_window_get_width(window);
4395         lw->options.dupe_window.h = gdk_window_get_height(window);
4396 }
4397
4398 void dupe_window_close(DupeWindow *dw)
4399 {
4400         dupe_check_stop(dw);
4401
4402         dupe_window_get_geometry(dw);
4403
4404         dupe_window_list = g_list_remove(dupe_window_list, dw);
4405         gtk_widget_destroy(dw->window);
4406
4407         g_list_free(dw->dupes);
4408         g_list_free_full(dw->list, reinterpret_cast<GDestroyNotify>(dupe_item_free));
4409
4410         g_list_free_full(dw->second_list, reinterpret_cast<GDestroyNotify>(dupe_item_free));
4411
4412         file_data_unregister_notify_func(dupe_notify_cb, dw);
4413
4414         g_thread_pool_free(dw->dupe_comparison_thread_pool, TRUE, TRUE);
4415
4416         g_free(dw);
4417 }
4418
4419 static gint dupe_window_close_cb(GtkWidget *, gpointer data)
4420 {
4421         auto dw = static_cast<DupeWindow *>(data);
4422
4423         dupe_window_close(dw);
4424
4425         return TRUE;
4426 }
4427
4428 static gint dupe_window_delete(GtkWidget *, GdkEvent *, gpointer data)
4429 {
4430         auto dw = static_cast<DupeWindow *>(data);
4431         dupe_window_close(dw);
4432
4433         return TRUE;
4434 }
4435
4436 static void dupe_help_cb(GtkAction *, gpointer)
4437 {
4438         help_window_show("GuideImageSearchFindingDuplicates.html");
4439 }
4440
4441 static gint default_sort_cb(GtkTreeModel *, GtkTreeIter *, GtkTreeIter *, gpointer)
4442 {
4443         return 0;
4444 }
4445
4446 static gint column_sort_cb(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b, gpointer data)
4447 {
4448         auto sortable = static_cast<GtkTreeSortable *>(data);
4449         gint ret = 0;
4450         gchar *rank_str_a, *rank_str_b;
4451         gint rank_int_a;
4452         gint rank_int_b;
4453         gint group_a;
4454         gint group_b;
4455         gint sort_column_id;
4456         GtkSortType sort_order;
4457         DupeItem *di_a;
4458         DupeItem *di_b;
4459
4460         gtk_tree_sortable_get_sort_column_id(sortable, &sort_column_id, &sort_order);
4461
4462         gtk_tree_model_get(model, a, DUPE_COLUMN_RANK, &rank_str_a, DUPE_COLUMN_SET, &group_a, DUPE_COLUMN_POINTER, &di_a, -1);
4463
4464         gtk_tree_model_get(model, b, DUPE_COLUMN_RANK, &rank_str_b, DUPE_COLUMN_SET, &group_b, DUPE_COLUMN_POINTER, &di_b, -1);
4465
4466         if (group_a == group_b)
4467                 {
4468                 switch (sort_column_id)
4469                         {
4470                         case DUPE_COLUMN_NAME:
4471                                 ret = utf8_compare(di_a->fd->name, di_b->fd->name, TRUE);
4472                                 break;
4473                         case DUPE_COLUMN_SIZE:
4474                                 if (di_a->fd->size == di_b->fd->size)
4475                                         {
4476                                         ret = 0;
4477                                         }
4478                                 else
4479                                         {
4480                                         ret = (di_a->fd->size > di_b->fd->size) ? 1 : -1;
4481                                         }
4482                                 break;
4483                         case DUPE_COLUMN_DATE:
4484                                 if (di_a->fd->date == di_b->fd->date)
4485                                         {
4486                                         ret = 0;
4487                                         }
4488                                 else
4489                                         {
4490                                         ret = (di_a->fd->date > di_b->fd->date) ? 1 : -1;
4491                                         }
4492                                 break;
4493                         case DUPE_COLUMN_DIMENSIONS:
4494                                 if ((di_a->width == di_b->width) && (di_a->height == di_b->height))
4495                                         {
4496                                         ret = 0;
4497                                         }
4498                                 else
4499                                         {
4500                                         ret = ((di_a->width * di_a->height) > (di_b->width * di_b->height)) ? 1 : -1;
4501                                         }
4502                                 break;
4503                         case DUPE_COLUMN_RANK:
4504                                 rank_int_a = atoi(rank_str_a);
4505                                 rank_int_b = atoi(rank_str_b);
4506                                 if (rank_int_a == 0) rank_int_a = 101;
4507                                 if (rank_int_b == 0) rank_int_b = 101;
4508
4509                                 if (rank_int_a == rank_int_b)
4510                                         {
4511                                         ret = 0;
4512                                         }
4513                                 else
4514                                         {
4515                                         ret = (rank_int_a > rank_int_b) ? 1 : -1;
4516                                         }
4517                                 break;
4518                         case DUPE_COLUMN_PATH:
4519                                 ret = utf8_compare(di_a->fd->path, di_b->fd->path, TRUE);
4520                                 break;
4521                         }
4522                 }
4523         else if (group_a < group_b)
4524                 {
4525                 ret = (sort_order == GTK_SORT_ASCENDING) ? 1 : -1;
4526                 }
4527         else
4528                 {
4529                 ret = (sort_order == GTK_SORT_ASCENDING) ? -1 : 1;
4530                 }
4531
4532         return ret;
4533 }
4534
4535 static void column_clicked_cb(GtkWidget *,  gpointer data)
4536 {
4537         auto dw = static_cast<DupeWindow *>(data);
4538
4539         options->duplicates_match = DUPE_SELECT_NONE;
4540         dupe_listview_select_dupes(dw, DUPE_SELECT_NONE);
4541 }
4542
4543 /* collection and files can be NULL */
4544 DupeWindow *dupe_window_new()
4545 {
4546         DupeWindow *dw;
4547         GtkWidget *vbox;
4548         GtkWidget *hbox;
4549         GtkWidget *scrolled;
4550         GtkWidget *frame;
4551         GtkWidget *status_box;
4552         GtkWidget *controls_box;
4553         GtkWidget *button_box;
4554         GtkWidget *label;
4555         GtkWidget *button;
4556         GtkListStore *store;
4557         GtkTreeSelection *selection;
4558         GdkGeometry geometry;
4559         LayoutWindow *lw = nullptr;
4560
4561         layout_valid(&lw);
4562
4563         dw = g_new0(DupeWindow, 1);
4564         dw->add_files_queue = nullptr;
4565         dw->add_files_queue_id = 0;
4566
4567         dw->match_mask = DUPE_MATCH_NAME;
4568         if (options->duplicates_match == DUPE_MATCH_NAME) dw->match_mask = DUPE_MATCH_NAME;
4569         if (options->duplicates_match == DUPE_MATCH_SIZE) dw->match_mask = DUPE_MATCH_SIZE;
4570         if (options->duplicates_match == DUPE_MATCH_DATE) dw->match_mask = DUPE_MATCH_DATE;
4571         if (options->duplicates_match == DUPE_MATCH_DIM) dw->match_mask = DUPE_MATCH_DIM;
4572         if (options->duplicates_match == DUPE_MATCH_SUM) dw->match_mask = DUPE_MATCH_SUM;
4573         if (options->duplicates_match == DUPE_MATCH_PATH) dw->match_mask = DUPE_MATCH_PATH;
4574         if (options->duplicates_match == DUPE_MATCH_SIM_HIGH) dw->match_mask = DUPE_MATCH_SIM_HIGH;
4575         if (options->duplicates_match == DUPE_MATCH_SIM_MED) dw->match_mask = DUPE_MATCH_SIM_MED;
4576         if (options->duplicates_match == DUPE_MATCH_SIM_LOW) dw->match_mask = DUPE_MATCH_SIM_LOW;
4577         if (options->duplicates_match == DUPE_MATCH_SIM_CUSTOM) dw->match_mask = DUPE_MATCH_SIM_CUSTOM;
4578         if (options->duplicates_match == DUPE_MATCH_NAME_CI) dw->match_mask = DUPE_MATCH_NAME_CI;
4579         if (options->duplicates_match == DUPE_MATCH_NAME_CONTENT) dw->match_mask = DUPE_MATCH_NAME_CONTENT;
4580         if (options->duplicates_match == DUPE_MATCH_NAME_CI_CONTENT) dw->match_mask = DUPE_MATCH_NAME_CI_CONTENT;
4581         if (options->duplicates_match == DUPE_MATCH_ALL) dw->match_mask = DUPE_MATCH_ALL;
4582
4583         dw->window = window_new(GTK_WINDOW_TOPLEVEL, "dupe", nullptr, nullptr, _("Find duplicates"));
4584         DEBUG_NAME(dw->window);
4585
4586         geometry.min_width = DEFAULT_MINIMAL_WINDOW_SIZE;
4587         geometry.min_height = DEFAULT_MINIMAL_WINDOW_SIZE;
4588         geometry.base_width = DUPE_DEF_WIDTH;
4589         geometry.base_height = DUPE_DEF_HEIGHT;
4590         gtk_window_set_geometry_hints(GTK_WINDOW(dw->window), nullptr, &geometry,
4591                                       static_cast<GdkWindowHints>(GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE));
4592
4593         if (lw && options->save_window_positions)
4594                 {
4595                 gtk_window_set_default_size(GTK_WINDOW(dw->window), lw->options.dupe_window.w, lw->options.dupe_window.h);
4596                 gtk_window_move(GTK_WINDOW(dw->window), lw->options.dupe_window.x, lw->options.dupe_window.y);
4597                 }
4598         else
4599                 {
4600                 gtk_window_set_default_size(GTK_WINDOW(dw->window), DUPE_DEF_WIDTH, DUPE_DEF_HEIGHT);
4601                 }
4602
4603         gtk_window_set_resizable(GTK_WINDOW(dw->window), TRUE);
4604         gtk_container_set_border_width(GTK_CONTAINER(dw->window), 0);
4605
4606         g_signal_connect(G_OBJECT(dw->window), "delete_event",
4607                          G_CALLBACK(dupe_window_delete), dw);
4608         g_signal_connect(G_OBJECT(dw->window), "key_press_event",
4609                          G_CALLBACK(dupe_window_keypress_cb), dw);
4610
4611         vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
4612         gtk_container_add(GTK_CONTAINER(dw->window), vbox);
4613         gtk_widget_show(vbox);
4614
4615         dw->table = gtk_table_new(1, 3, FALSE);
4616         gtk_box_pack_start(GTK_BOX(vbox), dw->table, TRUE, TRUE, 0);
4617         gtk_widget_show(dw->table);
4618
4619         scrolled = gtk_scrolled_window_new(nullptr, nullptr);
4620         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
4621         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
4622         gtk_table_attach_defaults(GTK_TABLE(dw->table), scrolled, 0, 2, 0, 1);
4623         gtk_widget_show(scrolled);
4624
4625         store = gtk_list_store_new(DUPE_COLUMN_COUNT, G_TYPE_POINTER, G_TYPE_STRING, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_INT, G_TYPE_INT);
4626         dw->listview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
4627         g_object_unref(store);
4628
4629         dw->sortable = GTK_TREE_SORTABLE(store);
4630
4631         gtk_tree_sortable_set_sort_func(dw->sortable, DUPE_COLUMN_RANK, column_sort_cb, dw->sortable, nullptr);
4632         gtk_tree_sortable_set_sort_func(dw->sortable, DUPE_COLUMN_SET, default_sort_cb, dw->sortable, nullptr);
4633         gtk_tree_sortable_set_sort_func(dw->sortable, DUPE_COLUMN_THUMB, default_sort_cb, dw->sortable, nullptr);
4634         gtk_tree_sortable_set_sort_func(dw->sortable, DUPE_COLUMN_NAME, column_sort_cb, dw->sortable, nullptr);
4635         gtk_tree_sortable_set_sort_func(dw->sortable, DUPE_COLUMN_SIZE, column_sort_cb, dw->sortable, nullptr);
4636         gtk_tree_sortable_set_sort_func(dw->sortable, DUPE_COLUMN_DATE, column_sort_cb, dw->sortable, nullptr);
4637         gtk_tree_sortable_set_sort_func(dw->sortable, DUPE_COLUMN_DIMENSIONS, column_sort_cb, dw->sortable, nullptr);
4638         gtk_tree_sortable_set_sort_func(dw->sortable, DUPE_COLUMN_PATH, column_sort_cb, dw->sortable, nullptr);
4639
4640         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dw->listview));
4641         gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_MULTIPLE);
4642         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(dw->listview), TRUE);
4643         gtk_tree_view_set_enable_search(GTK_TREE_VIEW(dw->listview), FALSE);
4644
4645         dupe_listview_add_column(dw, dw->listview, DUPE_COLUMN_RANK, _("Rank"), FALSE, TRUE);
4646         dupe_listview_add_column(dw, dw->listview, DUPE_COLUMN_THUMB, _("Thumb"), TRUE, FALSE);
4647         dupe_listview_add_column(dw, dw->listview, DUPE_COLUMN_NAME, _("Name"), FALSE, FALSE);
4648         dupe_listview_add_column(dw, dw->listview, DUPE_COLUMN_SIZE, _("Size"), FALSE, TRUE);
4649         dupe_listview_add_column(dw, dw->listview, DUPE_COLUMN_DATE, _("Date"), FALSE, TRUE);
4650         dupe_listview_add_column(dw, dw->listview, DUPE_COLUMN_DIMENSIONS, _("Dimensions"), FALSE, FALSE);
4651         dupe_listview_add_column(dw, dw->listview, DUPE_COLUMN_PATH, _("Path"), FALSE, FALSE);
4652         dupe_listview_add_column(dw, dw->listview, DUPE_COLUMN_SET, _("Set"), FALSE, FALSE);
4653
4654         g_signal_connect(gtk_tree_view_get_column(GTK_TREE_VIEW(dw->listview), DUPE_COLUMN_RANK - 1), "clicked", (GCallback)column_clicked_cb, dw);
4655         g_signal_connect(gtk_tree_view_get_column(GTK_TREE_VIEW(dw->listview), DUPE_COLUMN_NAME - 1), "clicked", (GCallback)column_clicked_cb, dw);
4656         g_signal_connect(gtk_tree_view_get_column(GTK_TREE_VIEW(dw->listview), DUPE_COLUMN_SIZE - 1), "clicked", (GCallback)column_clicked_cb, dw);
4657         g_signal_connect(gtk_tree_view_get_column(GTK_TREE_VIEW(dw->listview), DUPE_COLUMN_DATE - 1), "clicked", (GCallback)column_clicked_cb, dw);
4658         g_signal_connect(gtk_tree_view_get_column(GTK_TREE_VIEW(dw->listview), DUPE_COLUMN_DIMENSIONS - 1), "clicked", (GCallback)column_clicked_cb, dw);
4659         g_signal_connect(gtk_tree_view_get_column(GTK_TREE_VIEW(dw->listview), DUPE_COLUMN_PATH - 1), "clicked", (GCallback)column_clicked_cb, dw);
4660
4661         gtk_container_add(GTK_CONTAINER(scrolled), dw->listview);
4662         gtk_widget_show(dw->listview);
4663
4664         dw->second_vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
4665         gtk_table_attach_defaults(GTK_TABLE(dw->table), dw->second_vbox, 2, 3, 0, 1);
4666         if (dw->second_set)
4667                 {
4668                 gtk_table_set_col_spacings(GTK_TABLE(dw->table), PREF_PAD_GAP);
4669                 gtk_widget_show(dw->second_vbox);
4670                 }
4671         else
4672                 {
4673                 gtk_table_set_col_spacings(GTK_TABLE(dw->table), 0);
4674                 }
4675
4676         scrolled = gtk_scrolled_window_new(nullptr, nullptr);
4677         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
4678         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
4679         gtk_box_pack_start(GTK_BOX(dw->second_vbox), scrolled, TRUE, TRUE, 0);
4680         gtk_widget_show(scrolled);
4681
4682         store = gtk_list_store_new(2, G_TYPE_POINTER, G_TYPE_STRING);
4683         dw->second_listview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
4684
4685         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dw->second_listview));
4686         gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_MULTIPLE);
4687
4688         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(dw->second_listview), TRUE);
4689         gtk_tree_view_set_enable_search(GTK_TREE_VIEW(dw->second_listview), FALSE);
4690
4691         dupe_listview_add_column(dw, dw->second_listview, 1, _("Compare to:"), FALSE, FALSE);
4692
4693         gtk_container_add(GTK_CONTAINER(scrolled), dw->second_listview);
4694         gtk_widget_show(dw->second_listview);
4695
4696         dw->second_status_label = gtk_label_new("");
4697         gtk_box_pack_start(GTK_BOX(dw->second_vbox), dw->second_status_label, FALSE, FALSE, 0);
4698         gtk_widget_show(dw->second_status_label);
4699
4700         pref_line(dw->second_vbox, GTK_ORIENTATION_HORIZONTAL);
4701
4702         status_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
4703         gtk_box_pack_start(GTK_BOX(vbox), status_box, FALSE, FALSE, 0);
4704         gtk_widget_show(status_box);
4705
4706         frame = gtk_frame_new(nullptr);
4707         DEBUG_NAME(frame);
4708         gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
4709         gtk_box_pack_start(GTK_BOX(status_box), frame, TRUE, TRUE, 0);
4710         gtk_widget_show(frame);
4711
4712         dw->status_label = gtk_label_new("");
4713         gtk_container_add(GTK_CONTAINER(frame), dw->status_label);
4714         gtk_widget_show(dw->status_label);
4715
4716         dw->extra_label = gtk_progress_bar_new();
4717         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(dw->extra_label), 0.0);
4718         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(dw->extra_label), "");
4719         gtk_progress_bar_set_show_text(GTK_PROGRESS_BAR(dw->extra_label), TRUE);
4720         gtk_box_pack_start(GTK_BOX(status_box), dw->extra_label, FALSE, FALSE, PREF_PAD_SPACE);
4721         gtk_widget_show(dw->extra_label);
4722
4723         controls_box = pref_box_new(vbox, FALSE, GTK_ORIENTATION_HORIZONTAL, 0);
4724         dw->controls_box = controls_box;
4725
4726         dw->button_thumbs = gtk_check_button_new_with_label(_("Thumbnails"));
4727         gtk_widget_set_tooltip_text(GTK_WIDGET(dw->button_thumbs), "Ctrl-T");
4728         dw->show_thumbs = options->duplicates_thumbnails;
4729         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dw->button_thumbs), dw->show_thumbs);
4730         g_signal_connect(G_OBJECT(dw->button_thumbs), "toggled",
4731                          G_CALLBACK(dupe_window_show_thumb_cb), dw);
4732         gtk_box_pack_start(GTK_BOX(controls_box), dw->button_thumbs, FALSE, FALSE, PREF_PAD_SPACE);
4733         gtk_widget_show(dw->button_thumbs);
4734
4735         label = gtk_label_new(_("Compare by:"));
4736         gtk_box_pack_start(GTK_BOX(controls_box), label, FALSE, FALSE, PREF_PAD_SPACE);
4737         gtk_widget_show(label);
4738
4739         dupe_menu_setup(dw);
4740         gtk_box_pack_start(GTK_BOX(controls_box), dw->combo, FALSE, FALSE, 0);
4741         gtk_widget_show(dw->combo);
4742
4743         label = gtk_label_new(_("Custom Threshold"));
4744         gtk_box_pack_start(GTK_BOX(controls_box), label, FALSE, FALSE, PREF_PAD_SPACE);
4745         gtk_widget_show(label);
4746         dw->custom_threshold = gtk_spin_button_new_with_range(1, 100, 1);
4747         gtk_widget_set_tooltip_text(GTK_WIDGET(dw->custom_threshold), "Custom similarity threshold\n(Use tab key to set value)");
4748         gtk_spin_button_set_value(GTK_SPIN_BUTTON(dw->custom_threshold), options->duplicates_similarity_threshold);
4749         g_signal_connect(G_OBJECT(dw->custom_threshold), "value_changed", G_CALLBACK(dupe_window_custom_threshold_cb), dw);
4750         gtk_box_pack_start(GTK_BOX(controls_box), dw->custom_threshold, FALSE, FALSE, PREF_PAD_SPACE);
4751         gtk_widget_show(dw->custom_threshold);
4752
4753         button = gtk_check_button_new_with_label(_("Sort"));
4754         gtk_widget_set_tooltip_text(GTK_WIDGET(button), "Sort by group totals");
4755         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), options->sort_totals);
4756         g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(dupe_sort_totals_toggle_cb), dw);
4757         gtk_box_pack_start(GTK_BOX(controls_box), button, FALSE, FALSE, PREF_PAD_SPACE);
4758         gtk_widget_show(button);
4759
4760         dw->button_rotation_invariant = gtk_check_button_new_with_label(_("Ignore Orientation"));
4761         gtk_widget_set_tooltip_text(GTK_WIDGET(dw->button_rotation_invariant), "Ignore image orientation");
4762         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dw->button_rotation_invariant), options->rot_invariant_sim);
4763         g_signal_connect(G_OBJECT(dw->button_rotation_invariant), "toggled",
4764                          G_CALLBACK(dupe_window_rotation_invariant_cb), dw);
4765         gtk_box_pack_start(GTK_BOX(controls_box), dw->button_rotation_invariant, FALSE, FALSE, PREF_PAD_SPACE);
4766         gtk_widget_show(dw->button_rotation_invariant);
4767
4768         button = gtk_check_button_new_with_label(_("Compare two file sets"));
4769         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), dw->second_set);
4770         g_signal_connect(G_OBJECT(button), "toggled",
4771                          G_CALLBACK(dupe_second_set_toggle_cb), dw);
4772         gtk_box_pack_start(GTK_BOX(controls_box), button, FALSE, FALSE, PREF_PAD_SPACE);
4773         gtk_widget_show(button);
4774
4775         button_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
4776         gtk_box_pack_start(GTK_BOX(vbox), button_box, FALSE, FALSE, 0);
4777         gtk_widget_show(button_box);
4778
4779         hbox = gtk_hbutton_box_new();
4780         gtk_button_box_set_layout(GTK_BUTTON_BOX(hbox), GTK_BUTTONBOX_END);
4781         gtk_box_set_spacing(GTK_BOX(hbox), PREF_PAD_SPACE);
4782         gtk_box_pack_end(GTK_BOX(button_box), hbox, FALSE, FALSE, 0);
4783         gtk_widget_show(hbox);
4784
4785         button = pref_button_new(nullptr, GTK_STOCK_HELP, nullptr, FALSE, G_CALLBACK(dupe_help_cb), nullptr);
4786         gtk_widget_set_tooltip_text(GTK_WIDGET(button), "F1");
4787         gtk_container_add(GTK_CONTAINER(hbox), button);
4788         gtk_widget_set_can_default(button, TRUE);
4789         gtk_widget_show(button);
4790
4791         button = pref_button_new(nullptr, GTK_STOCK_STOP, nullptr, FALSE, G_CALLBACK(dupe_check_stop_cb), dw);
4792         gtk_container_add(GTK_CONTAINER(hbox), button);
4793         gtk_widget_set_can_default(button, TRUE);
4794         gtk_widget_show(button);
4795
4796         button = pref_button_new(nullptr, GTK_STOCK_CLOSE, nullptr, FALSE, G_CALLBACK(dupe_window_close_cb), dw);
4797         gtk_widget_set_tooltip_text(GTK_WIDGET(button), "Ctrl-W");
4798         gtk_container_add(GTK_CONTAINER(hbox), button);
4799         gtk_widget_set_can_default(button, TRUE);
4800         gtk_widget_grab_default(button);
4801         gtk_widget_show(button);
4802         dupe_dnd_init(dw);
4803
4804         /* order is important here, dnd_init should be seeing mouse
4805          * presses before we possibly handle (and stop) the signal
4806          */
4807         g_signal_connect(G_OBJECT(dw->listview), "button_press_event",
4808                          G_CALLBACK(dupe_listview_press_cb), dw);
4809         g_signal_connect(G_OBJECT(dw->listview), "button_release_event",
4810                          G_CALLBACK(dupe_listview_release_cb), dw);
4811         g_signal_connect(G_OBJECT(dw->second_listview), "button_press_event",
4812                          G_CALLBACK(dupe_listview_press_cb), dw);
4813         g_signal_connect(G_OBJECT(dw->second_listview), "button_release_event",
4814                          G_CALLBACK(dupe_listview_release_cb), dw);
4815
4816         gtk_widget_show(dw->window);
4817
4818         dupe_listview_set_height(dw->listview, dw->show_thumbs);
4819         g_signal_emit_by_name(G_OBJECT(dw->combo), "changed");
4820
4821         dupe_window_update_count(dw, TRUE);
4822         dupe_window_update_progress(dw, nullptr, 0.0, FALSE);
4823
4824         dupe_window_list = g_list_append(dupe_window_list, dw);
4825
4826         file_data_register_notify_func(dupe_notify_cb, dw, NOTIFY_PRIORITY_MEDIUM);
4827
4828         g_mutex_init(&dw->thread_count_mutex);
4829         g_mutex_init(&dw->search_matches_mutex);
4830         dw->dupe_comparison_thread_pool = g_thread_pool_new(dupe_comparison_func, dw, options->threads.duplicates, FALSE, nullptr);
4831
4832         return dw;
4833 }
4834
4835 /*
4836  *-------------------------------------------------------------------
4837  * dnd confirm dir
4838  *-------------------------------------------------------------------
4839  */
4840
4841 struct CDupeConfirmD {
4842         DupeWindow *dw;
4843         GList *list;
4844 };
4845
4846 static void confirm_dir_list_cancel(GtkWidget *, gpointer)
4847 {
4848         /* do nothing */
4849 }
4850
4851 static void confirm_dir_list_add(GtkWidget *, gpointer data)
4852 {
4853         auto d = static_cast<CDupeConfirmD *>(data);
4854         GList *work;
4855
4856         dupe_window_add_files(d->dw, d->list, FALSE);
4857
4858         work = d->list;
4859         while (work)
4860                 {
4861                 auto fd = static_cast<FileData *>(work->data);
4862                 work = work->next;
4863                 if (isdir(fd->path))
4864                         {
4865                         GList *list;
4866
4867                         filelist_read(fd, &list, nullptr);
4868                         list = filelist_filter(list, FALSE);
4869                         if (list)
4870                                 {
4871                                 dupe_window_add_files(d->dw, list, FALSE);
4872                                 filelist_free(list);
4873                                 }
4874                         }
4875                 }
4876 }
4877
4878 static void confirm_dir_list_recurse(GtkWidget *, gpointer data)
4879 {
4880         auto d = static_cast<CDupeConfirmD *>(data);
4881         dupe_window_add_files(d->dw, d->list, TRUE);
4882 }
4883
4884 static void confirm_dir_list_skip(GtkWidget *, gpointer data)
4885 {
4886         auto d = static_cast<CDupeConfirmD *>(data);
4887         dupe_window_add_files(d->dw, d->list, FALSE);
4888 }
4889
4890 static void confirm_dir_list_destroy(GtkWidget *, gpointer data)
4891 {
4892         auto d = static_cast<CDupeConfirmD *>(data);
4893         filelist_free(d->list);
4894         g_free(d);
4895 }
4896
4897 static GtkWidget *dupe_confirm_dir_list(DupeWindow *dw, GList *list)
4898 {
4899         GtkWidget *menu;
4900         CDupeConfirmD *d;
4901
4902         d = g_new0(CDupeConfirmD, 1);
4903         d->dw = dw;
4904         d->list = list;
4905
4906         menu = popup_menu_short_lived();
4907         g_signal_connect(G_OBJECT(menu), "destroy",
4908                          G_CALLBACK(confirm_dir_list_destroy), d);
4909
4910         menu_item_add_stock(menu, _("Dropped list includes folders."), GTK_STOCK_DND_MULTIPLE, nullptr, nullptr);
4911         menu_item_add_divider(menu);
4912         menu_item_add_stock(menu, _("_Add contents"), GTK_STOCK_OK, G_CALLBACK(confirm_dir_list_add), d);
4913         menu_item_add_stock(menu, _("Add contents _recursive"), GTK_STOCK_ADD, G_CALLBACK(confirm_dir_list_recurse), d);
4914         menu_item_add_stock(menu, _("_Skip folders"), GTK_STOCK_REMOVE, G_CALLBACK(confirm_dir_list_skip), d);
4915         menu_item_add_divider(menu);
4916         menu_item_add_stock(menu, _("Cancel"), GTK_STOCK_CANCEL, G_CALLBACK(confirm_dir_list_cancel), d);
4917
4918         return menu;
4919 }
4920
4921 /*
4922  *-------------------------------------------------------------------
4923  * dnd
4924  *-------------------------------------------------------------------
4925  */
4926
4927 static GtkTargetEntry dupe_drag_types[] = {
4928         { const_cast<gchar *>("text/uri-list"), 0, TARGET_URI_LIST },
4929         { const_cast<gchar *>("text/plain"), 0, TARGET_TEXT_PLAIN }
4930 };
4931 static gint n_dupe_drag_types = 2;
4932
4933 static GtkTargetEntry dupe_drop_types[] = {
4934         { const_cast<gchar *>(TARGET_APP_COLLECTION_MEMBER_STRING), 0, TARGET_APP_COLLECTION_MEMBER },
4935         { const_cast<gchar *>("text/uri-list"), 0, TARGET_URI_LIST }
4936 };
4937 static gint n_dupe_drop_types = 2;
4938
4939 static void dupe_dnd_data_set(GtkWidget *widget, GdkDragContext *,
4940                               GtkSelectionData *selection_data, guint info,
4941                               guint, gpointer data)
4942 {
4943         auto dw = static_cast<DupeWindow *>(data);
4944         GList *list;
4945
4946         switch (info)
4947                 {
4948                 case TARGET_URI_LIST:
4949                 case TARGET_TEXT_PLAIN:
4950                         list = dupe_listview_get_selection(dw, widget);
4951                         if (!list) return;
4952                         uri_selection_data_set_uris_from_filelist(selection_data, list);
4953                         filelist_free(list);
4954                         break;
4955                 default:
4956                         break;
4957                 }
4958 }
4959
4960 static void dupe_dnd_data_get(GtkWidget *widget, GdkDragContext *context,
4961                               gint, gint,
4962                               GtkSelectionData *selection_data, guint info,
4963                               guint, gpointer data)
4964 {
4965         auto dw = static_cast<DupeWindow *>(data);
4966         GtkWidget *source;
4967         GList *list = nullptr;
4968         GList *work;
4969
4970         if (dw->add_files_queue_id > 0)
4971                 {
4972                 warning_dialog(_("Find duplicates"), _("Please wait for the current file selection to be loaded."), GTK_STOCK_DIALOG_INFO, dw->window);
4973
4974                 return;
4975                 }
4976
4977         source = gtk_drag_get_source_widget(context);
4978         if (source == dw->listview || source == dw->second_listview) return;
4979
4980         dw->second_drop = (dw->second_set && widget == dw->second_listview);
4981
4982         switch (info)
4983                 {
4984                 case TARGET_APP_COLLECTION_MEMBER:
4985                         collection_from_dnd_data(reinterpret_cast<const gchar *>(gtk_selection_data_get_data(selection_data)), &list, nullptr);
4986                         break;
4987                 case TARGET_URI_LIST:
4988                         list = uri_filelist_from_gtk_selection_data(selection_data);
4989                         work = list;
4990                         while (work)
4991                                 {
4992                                 auto fd = static_cast<FileData *>(work->data);
4993                                 if (isdir(fd->path))
4994                                         {
4995                                         GtkWidget *menu;
4996                                         menu = dupe_confirm_dir_list(dw, list);
4997                                         gtk_menu_popup_at_pointer(GTK_MENU(menu), nullptr);
4998                                         return;
4999                                         }
5000                                 work = work->next;
5001                                 }
5002                         break;
5003                 default:
5004                         list = nullptr;
5005                         break;
5006                 }
5007
5008         if (list)
5009                 {
5010                 dupe_window_add_files(dw, list, FALSE);
5011                 filelist_free(list);
5012                 }
5013 }
5014
5015 static void dupe_dest_set(GtkWidget *widget, gboolean enable)
5016 {
5017         if (enable)
5018                 {
5019                 gtk_drag_dest_set(widget,
5020                         static_cast<GtkDestDefaults>(GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_HIGHLIGHT | GTK_DEST_DEFAULT_DROP),
5021                         dupe_drop_types, n_dupe_drop_types,
5022                         static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_ASK));
5023
5024                 }
5025         else
5026                 {
5027                 gtk_drag_dest_unset(widget);
5028                 }
5029 }
5030
5031 static void dupe_dnd_begin(GtkWidget *widget, GdkDragContext *context, gpointer data)
5032 {
5033         auto dw = static_cast<DupeWindow *>(data);
5034         dupe_dest_set(dw->listview, FALSE);
5035         dupe_dest_set(dw->second_listview, FALSE);
5036
5037         if (dw->click_item && !dupe_listview_item_is_selected(dw, dw->click_item, widget))
5038                 {
5039                 GtkListStore *store;
5040                 GtkTreeIter iter;
5041
5042                 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(widget)));
5043                 if (dupe_listview_find_item(store, dw->click_item, &iter) >= 0)
5044                         {
5045                         GtkTreeSelection *selection;
5046                         GtkTreePath *tpath;
5047
5048                         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
5049                         gtk_tree_selection_unselect_all(selection);
5050                         gtk_tree_selection_select_iter(selection, &iter);
5051
5052                         tpath = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &iter);
5053                         gtk_tree_view_set_cursor(GTK_TREE_VIEW(widget), tpath, nullptr, FALSE);
5054                         gtk_tree_path_free(tpath);
5055                         }
5056                 }
5057
5058         if (dw->show_thumbs &&
5059             widget == dw->listview &&
5060             dw->click_item && dw->click_item->pixbuf)
5061                 {
5062                 GtkTreeSelection *selection;
5063                 gint items;
5064
5065                 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
5066                 items = gtk_tree_selection_count_selected_rows(selection);
5067                 dnd_set_drag_icon(widget, context, dw->click_item->pixbuf, items);
5068                 }
5069 }
5070
5071 static void dupe_dnd_end(GtkWidget *, GdkDragContext *, gpointer data)
5072 {
5073         auto dw = static_cast<DupeWindow *>(data);
5074         dupe_dest_set(dw->listview, TRUE);
5075         dupe_dest_set(dw->second_listview, TRUE);
5076 }
5077
5078 static void dupe_dnd_init(DupeWindow *dw)
5079 {
5080         gtk_drag_source_set(dw->listview, static_cast<GdkModifierType>(GDK_BUTTON1_MASK | GDK_BUTTON2_MASK),
5081                             dupe_drag_types, n_dupe_drag_types,
5082                             static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK));
5083         g_signal_connect(G_OBJECT(dw->listview), "drag_data_get",
5084                          G_CALLBACK(dupe_dnd_data_set), dw);
5085         g_signal_connect(G_OBJECT(dw->listview), "drag_begin",
5086                          G_CALLBACK(dupe_dnd_begin), dw);
5087         g_signal_connect(G_OBJECT(dw->listview), "drag_end",
5088                          G_CALLBACK(dupe_dnd_end), dw);
5089
5090         dupe_dest_set(dw->listview, TRUE);
5091         g_signal_connect(G_OBJECT(dw->listview), "drag_data_received",
5092                          G_CALLBACK(dupe_dnd_data_get), dw);
5093
5094         gtk_drag_source_set(dw->second_listview, static_cast<GdkModifierType>(GDK_BUTTON1_MASK | GDK_BUTTON2_MASK),
5095                             dupe_drag_types, n_dupe_drag_types,
5096                             static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK));
5097         g_signal_connect(G_OBJECT(dw->second_listview), "drag_data_get",
5098                          G_CALLBACK(dupe_dnd_data_set), dw);
5099         g_signal_connect(G_OBJECT(dw->second_listview), "drag_begin",
5100                          G_CALLBACK(dupe_dnd_begin), dw);
5101         g_signal_connect(G_OBJECT(dw->second_listview), "drag_end",
5102                          G_CALLBACK(dupe_dnd_end), dw);
5103
5104         dupe_dest_set(dw->second_listview, TRUE);
5105         g_signal_connect(G_OBJECT(dw->second_listview), "drag_data_received",
5106                          G_CALLBACK(dupe_dnd_data_get), dw);
5107 }
5108
5109 /*
5110  *-------------------------------------------------------------------
5111  * maintenance (move, delete, etc.)
5112  *-------------------------------------------------------------------
5113  */
5114
5115 static void dupe_notify_cb(FileData *fd, NotifyType type, gpointer data)
5116 {
5117         auto dw = static_cast<DupeWindow *>(data);
5118
5119         if (!(type & NOTIFY_CHANGE) || !fd->change) return;
5120
5121         DEBUG_1("Notify dupe: %s %04x", fd->path, type);
5122
5123         switch (fd->change->type)
5124                 {
5125                 case FILEDATA_CHANGE_MOVE:
5126                 case FILEDATA_CHANGE_RENAME:
5127                         dupe_item_update_fd(dw, fd);
5128                         break;
5129                 case FILEDATA_CHANGE_COPY:
5130                         break;
5131                 case FILEDATA_CHANGE_DELETE:
5132                         /* Update the UI only once, after the operation finishes */
5133                         break;
5134                 case FILEDATA_CHANGE_UNSPECIFIED:
5135                 case FILEDATA_CHANGE_WRITE_METADATA:
5136                         break;
5137                 }
5138
5139 }
5140
5141 /**
5142  * @brief Refresh window after a file delete operation
5143  * @param success (ud->phase != UTILITY_PHASE_CANCEL) #file_util_dialog_run
5144  * @param dest_path Not used
5145  * @param data #DupeWindow
5146  *
5147  * If the window is refreshed after each file of a large set is deleted,
5148  * the UI slows to an unacceptable level. The #FileUtilDoneFunc is used
5149  * to call this function once, when the entire delete operation is completed.
5150  */
5151 static void delete_finished_cb(gboolean success, const gchar *, gpointer data)
5152 {
5153         auto dw = static_cast<DupeWindow *>(data);
5154
5155         if (!success)
5156                 {
5157                 return;
5158                 }
5159
5160         dupe_window_remove_selection(dw, dw->listview);
5161 }
5162
5163 /*
5164  *-------------------------------------------------------------------
5165  * Export duplicates data
5166  *-------------------------------------------------------------------
5167  */
5168
5169 enum SeparatorType {
5170         EXPORT_CSV = 0,
5171         EXPORT_TSV
5172 };
5173
5174 struct ExportDupesData
5175 {
5176         FileDialog *dialog;
5177         SeparatorType separator;
5178         DupeWindow *dupewindow;
5179 };
5180
5181 static void export_duplicates_close(ExportDupesData *edd)
5182 {
5183         if (edd->dialog) file_dialog_close(edd->dialog);
5184         edd->dialog = nullptr;
5185 }
5186
5187 static void export_duplicates_data_cancel_cb(FileDialog *, gpointer data)
5188 {
5189         auto edd = static_cast<ExportDupesData *>(data);
5190
5191         export_duplicates_close(edd);
5192 }
5193
5194 static void export_duplicates_data_save_cb(FileDialog *fdlg, gpointer data)
5195 {
5196         auto edd = static_cast<ExportDupesData *>(data);
5197         GError *error = nullptr;
5198         GtkTreeModel *store;
5199         GtkTreeIter iter;
5200         DupeItem *di;
5201         GFileOutputStream *gfstream;
5202         GFile *out_file;
5203         GString *output_string;
5204         gchar *sep;
5205         gchar* rank;
5206         GList *work;
5207         GtkTreeSelection *selection;
5208         GList *slist;
5209         gchar *thumb_cache;
5210         gchar **rank_split;
5211         GtkTreePath *tpath;
5212         gboolean color_old = FALSE;
5213         gboolean color_new = FALSE;
5214         gint match_count;
5215         gchar *name;
5216
5217         history_list_add_to_key("export_duplicates", fdlg->dest_path, -1);
5218
5219         out_file = g_file_new_for_path(fdlg->dest_path);
5220
5221         gfstream = g_file_replace(out_file, nullptr, TRUE, G_FILE_CREATE_NONE, nullptr, &error);
5222         if (error)
5223                 {
5224                 log_printf(_("Error creating Export duplicates data file: Error: %s\n"), error->message);
5225                 g_error_free(error);
5226                 return;
5227                 }
5228
5229         sep = g_strdup((edd->separator == EXPORT_CSV) ?  "," : "\t");
5230         output_string = g_string_new(g_strjoin(sep, _("Match"), _("Group"), _("Similarity"), _("Set"), _("Thumbnail"), _("Name"), _("Size"), _("Date"), _("Width"), _("Height"), _("Path\n"), NULL));
5231
5232         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(edd->dupewindow->listview));
5233         slist = gtk_tree_selection_get_selected_rows(selection, &store);
5234         work = slist;
5235
5236         tpath = static_cast<GtkTreePath *>(work->data);
5237         gtk_tree_model_get_iter(store, &iter, tpath);
5238         gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, DUPE_COLUMN_COLOR, &color_new, -1);
5239         color_old = !color_new;
5240         match_count = 0;
5241
5242         while (work)
5243                 {
5244                 tpath = static_cast<GtkTreePath *>(work->data);
5245                 gtk_tree_model_get_iter(store, &iter, tpath);
5246
5247                 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, DUPE_COLUMN_POINTER, &di, -1);
5248
5249                 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, DUPE_COLUMN_COLOR, &color_new, -1);
5250                 if (color_new != color_old)
5251                         {
5252                         match_count++;
5253                         }
5254                 color_old = color_new;
5255                 output_string = g_string_append(output_string, g_strdup_printf("%d", match_count));
5256                 output_string = g_string_append(output_string, sep);
5257
5258                 if ((dupe_match_find_parent(edd->dupewindow, di) == di))
5259                         {
5260                         output_string = g_string_append(output_string, "1");
5261                         }
5262                 else
5263                         {
5264                         output_string = g_string_append(output_string, "2");
5265                         }
5266                 output_string = g_string_append(output_string, sep);
5267
5268                 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, DUPE_COLUMN_RANK, &rank, -1);
5269                 rank_split = g_strsplit_set(rank, " [(", -1);
5270                 if (rank_split[0] == nullptr)
5271                         {
5272                         output_string = g_string_append(output_string, "");
5273                         }
5274                 else
5275                         {
5276                         output_string = g_string_append(output_string, g_strdup_printf("%s", rank_split[0]));
5277                         }
5278                 output_string = g_string_append(output_string, sep);
5279                 g_free(rank);
5280                 g_strfreev(rank_split);
5281
5282                 output_string = g_string_append(output_string, g_strdup_printf("%d", (di->second + 1)));
5283                 output_string = g_string_append(output_string, sep);
5284
5285                 thumb_cache = cache_find_location(CACHE_TYPE_THUMB, di->fd->path);
5286                 if (thumb_cache)
5287                         {
5288                         output_string = g_string_append(output_string, thumb_cache);
5289                         g_free(thumb_cache);
5290                         }
5291                 else
5292                         {
5293                         output_string = g_string_append(output_string, "");
5294                         }
5295                 output_string = g_string_append(output_string, sep);
5296
5297                 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, DUPE_COLUMN_NAME, &name, -1);
5298                 output_string = g_string_append(output_string, name);
5299                 output_string = g_string_append(output_string, sep);
5300                 g_free(name);
5301
5302                 output_string = g_string_append(output_string, g_strdup_printf("%" PRIu64, di->fd->size));
5303                 output_string = g_string_append(output_string, sep);
5304                 output_string = g_string_append(output_string, text_from_time(di->fd->date));
5305                 output_string = g_string_append(output_string, sep);
5306                 output_string = g_string_append(output_string, g_strdup_printf("%d", (di->width ? di->width : 0)));
5307                 output_string = g_string_append(output_string, sep);
5308                 output_string = g_string_append(output_string, g_strdup_printf("%d", (di->height ? di->height : 0)));
5309                 output_string = g_string_append(output_string, sep);
5310                 output_string = g_string_append(output_string, di->fd->path);
5311                 output_string = g_string_append_c(output_string, '\n');
5312
5313                 work = work->next;
5314                 }
5315
5316         g_output_stream_write(G_OUTPUT_STREAM(gfstream), output_string->str, strlen(output_string->str), nullptr, &error);
5317
5318         g_free(sep);
5319         g_string_free(output_string, TRUE);
5320         g_object_unref(gfstream);
5321         g_object_unref(out_file);
5322
5323         export_duplicates_close(edd);
5324 }
5325
5326 static void pop_menu_export(GList *, gpointer dupe_window, gpointer data)
5327 {
5328         const gint index = GPOINTER_TO_INT(data);
5329         auto dw = static_cast<DupeWindow *>(dupe_window);
5330         const gchar *title = "Export duplicates data";
5331         const gchar *default_path = "/tmp/";
5332         gchar *file_extension;
5333         const gchar *stock_id;
5334         ExportDupesData *edd;
5335         const gchar *previous_path;
5336
5337         edd = g_new0(ExportDupesData, 1);
5338         edd->dialog = file_util_file_dlg(title, "export_duplicates", nullptr, export_duplicates_data_cancel_cb, edd);
5339
5340         switch (index)
5341                 {
5342                 case EXPORT_CSV:
5343                         edd->separator = EXPORT_CSV;
5344                         file_extension = g_strdup(".csv");
5345                         break;
5346                 case EXPORT_TSV:
5347                         edd->separator = EXPORT_TSV;
5348                         file_extension = g_strdup(".tsv");
5349                         break;
5350                 default:
5351                         return;
5352                 }
5353
5354         stock_id = GTK_STOCK_SAVE;
5355
5356         generic_dialog_add_message(GENERIC_DIALOG(edd->dialog), nullptr, title, nullptr, FALSE);
5357         file_dialog_add_button(edd->dialog, stock_id, nullptr, export_duplicates_data_save_cb, TRUE);
5358
5359         previous_path = history_list_find_last_path_by_key("export_duplicates");
5360
5361         file_dialog_add_path_widgets(edd->dialog, default_path, previous_path, "export_duplicates", file_extension, _("Export Files"));
5362
5363         edd->dupewindow = dw;
5364
5365         gtk_widget_show(GENERIC_DIALOG(edd->dialog)->dialog);
5366
5367         g_free(file_extension);
5368 }
5369
5370 static void dupe_pop_menu_export_cb(GtkWidget *widget, gpointer data)
5371 {
5372         DupeWindow *dw;
5373         GList *selection_list;
5374
5375         dw = static_cast<DupeWindow *>(submenu_item_get_data(widget));
5376         selection_list = dupe_listview_get_selection(dw, dw->listview);
5377         pop_menu_export(selection_list, dw, data);
5378
5379         filelist_free(selection_list);
5380 }
5381
5382 static GtkWidget *submenu_add_export(GtkWidget *menu, GtkWidget **menu_item, GCallback func, gpointer data)
5383 {
5384         GtkWidget *item;
5385         GtkWidget *submenu;
5386
5387         item = menu_item_add(menu, _("_Export"), nullptr, nullptr);
5388
5389         submenu = gtk_menu_new();
5390         g_object_set_data(G_OBJECT(submenu), "submenu_data", data);
5391
5392         menu_item_add_stock_sensitive(submenu, _("Export to csv"),
5393                                         GTK_STOCK_INDEX, TRUE, G_CALLBACK(func), GINT_TO_POINTER(0));
5394         menu_item_add_stock_sensitive(submenu, _("Export to tab-delimited"),
5395                                         GTK_STOCK_INDEX, TRUE, G_CALLBACK(func), GINT_TO_POINTER(1));
5396
5397         gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
5398         if (menu_item) *menu_item = item;
5399
5400         return submenu;
5401 }
5402
5403 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */