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