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