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