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