2 * Copyright (C) 2005 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
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.
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.
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.
28 #include "collect-table.h"
31 #include "history-list.h"
32 #include "image-load.h"
34 #include "layout-image.h"
35 #include "layout-util.h"
39 #include "pixbuf-util.h"
42 #include "ui-fileops.h"
45 #include "ui-tree-edit.h"
46 #include "uri-utils.h"
53 #define DUPE_DEF_WIDTH 800
54 #define DUPE_DEF_HEIGHT 400
55 #define DUPE_PROGRESS_PULSE_STEP 0.0001
57 /** column assignment order (simply change them here)
60 DUPE_COLUMN_POINTER = 0,
66 DUPE_COLUMN_DIMENSIONS,
70 DUPE_COLUMN_COUNT /**< total columns */
73 enum DUPE_CHECK_RESULT {
79 /** Used for similarity checks. One for each item pushed
80 * onto the thread pool.
86 GList *work; /**< pointer into \a dw->list or \a dw->second_list (#DupeItem) */
87 gint index; /**< The order items pushed onto thread pool. Used to sort returned matches */
90 /** Used for similarity checks thread. One for each pair match found.
92 struct DupeSearchMatch
94 DupeItem *a; /**< \a a / \a b matched pair found */
95 DupeItem *b; /**< \a a / \a b matched pair found */
97 gint index; /**< The order items pushed onto thread pool. Used to sort returned matches */
100 static DupeMatchType param_match_mask;
101 static GList *dupe_window_list = nullptr; /**< list of open DupeWindow *s */
104 * Well, after adding the 'compare two sets' option things got a little sloppy in here
105 * because we have to account for two 'modes' everywhere. (be careful).
108 static void dupe_match_unlink(DupeItem *a, DupeItem *b);
109 static DupeItem *dupe_match_find_parent(DupeWindow *dw, DupeItem *child);
111 static gint dupe_match(DupeItem *a, DupeItem *b, DupeMatchType mask, gdouble *rank, gint fast);
113 static void dupe_thumb_step(DupeWindow *dw);
114 static gint dupe_check_cb(gpointer data);
116 static void dupe_second_add(DupeWindow *dw, DupeItem *di);
117 static void dupe_second_remove(DupeWindow *dw, DupeItem *di);
118 static GtkWidget *dupe_menu_popup_second(DupeWindow *dw, DupeItem *di);
120 static void dupe_dnd_init(DupeWindow *dw);
122 static void dupe_notify_cb(FileData *fd, NotifyType type, gpointer data);
123 static void delete_finished_cb(gboolean success, const gchar *dest_path, gpointer data);
125 static GtkWidget *submenu_add_export(GtkWidget *menu, GtkWidget **menu_item, GCallback func, gpointer data);
126 static void dupe_pop_menu_export_cb(GtkWidget *widget, gpointer data);
128 static void dupe_init_list_cache(DupeWindow *dw);
129 static void dupe_destroy_list_cache(DupeWindow *dw);
130 static gboolean dupe_insert_in_list_cache(DupeWindow *dw, FileData *fd);
132 static void dupe_match_link(DupeItem *a, DupeItem *b, gdouble rank);
133 static gint dupe_match_link_exists(DupeItem *child, DupeItem *parent);
136 * This array must be kept in sync with the contents of:\n
137 * @link dupe_window_keypress_cb() @endlink \n
138 * @link dupe_menu_popup_main() @endlink
140 * See also @link hard_coded_window_keys @endlink
142 hard_coded_window_keys dupe_window_keys[] = {
143 {GDK_CONTROL_MASK, 'C', N_("Copy")},
144 {GDK_CONTROL_MASK, 'M', N_("Move")},
145 {GDK_CONTROL_MASK, 'R', N_("Rename")},
146 {GDK_CONTROL_MASK, 'D', N_("Move to Trash")},
147 {GDK_SHIFT_MASK, GDK_KEY_Delete, N_("Delete")},
148 {static_cast<GdkModifierType>(0), GDK_KEY_Delete, N_("Remove")},
149 {GDK_CONTROL_MASK, GDK_KEY_Delete, N_("Clear")},
150 {GDK_CONTROL_MASK, 'A', N_("Select all")},
151 {static_cast<GdkModifierType>(GDK_CONTROL_MASK + GDK_SHIFT_MASK), 'A', N_("Select none")},
152 {GDK_CONTROL_MASK, 'T', N_("Toggle thumbs")},
153 {GDK_CONTROL_MASK, 'W', N_("Close window")},
154 {static_cast<GdkModifierType>(0), GDK_KEY_Return, N_("View")},
155 {static_cast<GdkModifierType>(0), 'V', N_("View in new window")},
156 {static_cast<GdkModifierType>(0), 'C', N_("Collection from selection")},
157 {GDK_CONTROL_MASK, 'L', N_("Append list")},
158 {static_cast<GdkModifierType>(0), '0', N_("Select none")},
159 {static_cast<GdkModifierType>(0), '1', N_("Select group 1 duplicates")},
160 {static_cast<GdkModifierType>(0), '2', N_("Select group 2 duplicates")},
161 {static_cast<GdkModifierType>(0), 0, nullptr}
165 * @brief The function run in threads for similarity checks
166 * @param d1 #DupeQueueItem
167 * @param d2 #DupeWindow
169 * Used only for similarity checks.\n
170 * Search \a dqi->list for \a dqi->needle and if a match is
171 * found, create a #DupeSearchMatch and add to \a dw->search_matches list\n
172 * If \a dw->abort is set, just increment \a dw->thread_count
174 static void dupe_comparison_func(gpointer d1, gpointer d2)
176 auto dqi = static_cast<DupeQueueItem *>(d1);
177 auto dw = static_cast<DupeWindow *>(d2);
178 DupeSearchMatch *dsm;
180 GList *matches = nullptr;
185 GList *work = dqi->work;
188 di = static_cast<DupeItem *>(work->data);
190 /* forward for second set, back for simple compare */
200 if (dupe_match(di, dqi->needle, dqi->dw->match_mask, &rank, TRUE))
202 dsm = g_new0(DupeSearchMatch, 1);
204 dsm->b = dqi->needle;
206 matches = g_list_prepend(matches, dsm);
207 dsm->index = dqi->index;
216 matches = g_list_reverse(matches);
217 g_mutex_lock(&dw->search_matches_mutex);
218 dw->search_matches = g_list_concat(dw->search_matches, matches);
219 g_mutex_unlock(&dw->search_matches_mutex);
222 g_mutex_lock(&dw->thread_count_mutex);
224 g_mutex_unlock(&dw->thread_count_mutex);
229 * ------------------------------------------------------------------
231 * ------------------------------------------------------------------
235 * @brief Update display of status label
241 static void dupe_window_update_count(DupeWindow *dw, gboolean count_only)
247 text = g_strdup(_("Drop files to compare them."));
251 text = g_strdup_printf(_("%d files"), g_list_length(dw->list));
255 text = g_strdup_printf(_("%d matches found in %d files"), g_list_length(dw->dupes), g_list_length(dw->list));
260 gchar *buf = g_strconcat(text, " ", _("[set 1]"), NULL);
264 gtk_label_set_text(GTK_LABEL(dw->status_label), text);
270 * @brief Returns time in µsec since Epoch
275 static guint64 msec_time()
279 if (gettimeofday(&tv, nullptr) == -1) return 0;
281 return static_cast<guint64>(tv.tv_sec) * 1000000 + static_cast<guint64>(tv.tv_usec);
284 static gint dupe_iterations(gint n)
286 return (n * ((n + 1) / 2));
296 * If \a status is blank, clear status bar text and set progress to zero. \n
297 * If \a force is not set, after 2 secs has elapsed, update time-to-go every 250 ms.
299 static void dupe_window_update_progress(DupeWindow *dw, const gchar *status, gdouble value, gboolean force)
301 const gchar *status_text;
305 guint64 new_time = 0;
307 if (dw->setup_n % 10 == 0)
309 new_time = msec_time() - dw->setup_time;
314 dw->setup_count > 0 &&
322 if (new_time - dw->setup_time_count < 250000) return;
323 dw->setup_time_count = new_time;
330 d = dw->setup_count - dw->setup_n;
334 t = dupe_iterations(dw->setup_count);
335 d = dupe_iterations(dw->setup_count - dw->setup_n);
341 d = dw->setup_count - dw->setup_n;
344 rem = (t - d) ? (static_cast<gdouble>(dw->setup_time_count / 1000000) / (t - d)) * d : 0;
346 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(dw->extra_label), value);
348 buf = g_strdup_printf("%s %d:%02d ", status, rem / 60, rem % 60);
349 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(dw->extra_label), buf);
356 dw->setup_count == 0 ||
357 dw->setup_time_count == 0 ||
358 (new_time > 0 && new_time - dw->setup_time_count >= 250000))
360 if (dw->setup_time_count == 0) dw->setup_time_count = 1;
361 if (new_time > 0) dw->setup_time_count = new_time;
362 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(dw->extra_label), value);
363 status_text = status;
367 status_text = nullptr;
372 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(dw->extra_label), 0.0);
376 if (status_text) gtk_progress_bar_set_text(GTK_PROGRESS_BAR(dw->extra_label), status_text);
379 static void widget_set_cursor(GtkWidget *widget, gint icon)
384 if (!gtk_widget_get_window(widget)) return;
392 display = gdk_display_get_default();
393 cursor = gdk_cursor_new_for_display(display, static_cast<GdkCursorType>(icon));
396 gdk_window_set_cursor(gtk_widget_get_window(widget), cursor);
398 if (cursor) g_object_unref(G_OBJECT(cursor));
402 * ------------------------------------------------------------------
404 * ------------------------------------------------------------------
407 static void dupe_listview_realign_colors(DupeWindow *dw)
411 gboolean color_set = TRUE;
412 DupeItem *parent = nullptr;
415 store = gtk_tree_view_get_model(GTK_TREE_VIEW(dw->listview));
416 valid = gtk_tree_model_get_iter_first(store, &iter);
420 DupeItem *child_parent;
422 gtk_tree_model_get(store, &iter, DUPE_COLUMN_POINTER, &child, -1);
423 child_parent = dupe_match_find_parent(dw, child);
424 if (!parent || parent != child_parent)
428 /* keep the first row as it is */
429 gtk_tree_model_get(store, &iter, DUPE_COLUMN_COLOR, &color_set, -1);
433 color_set = !color_set;
435 parent = dupe_match_find_parent(dw, child);
437 gtk_list_store_set(GTK_LIST_STORE(store), &iter, DUPE_COLUMN_COLOR, color_set, -1);
439 valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
444 * ------------------------------------------------------------------
446 * ------------------------------------------------------------------
449 static DupeItem *dupe_item_new(FileData *fd)
453 di = g_new0(DupeItem, 1);
455 di->fd = file_data_ref(fd);
456 di->group_rank = 0.0;
461 static void dupe_item_free(DupeItem *di)
463 file_data_unref(di->fd);
464 image_sim_free(di->simd);
466 if (di->pixbuf) g_object_unref(di->pixbuf);
471 #pragma GCC diagnostic push
472 #pragma GCC diagnostic ignored "-Wunused-function"
473 static DupeItem *dupe_item_find_fd_by_list_unused(FileData *fd, GList *work)
477 DupeItem *di = static_cast<DupeItem *>(work->data);
479 if (di->fd == fd) return di;
487 static DupeItem *dupe_item_find_fd_unused(DupeWindow *dw, FileData *fd)
491 di = dupe_item_find_fd_by_list_unused(fd, dw->list);
492 if (!di && dw->second_set) di = dupe_item_find_fd_by_list_unused(fd, dw->second_list);
497 static DupeItem *dupe_item_find_path_by_list_unused(const gchar *path, GList *work)
501 DupeItem *di = static_cast<DupeItem *>(work->data);
503 if (strcmp(di->fd->path, path) == 0) return di;
511 static DupeItem *dupe_item_find_path_unused(DupeWindow *dw, const gchar *path)
515 di = dupe_item_find_path_by_list_unused(path, dw->list);
516 if (!di && dw->second_set) di = dupe_item_find_path_by_list_unused(path, dw->second_list);
520 #pragma GCC diagnostic pop
523 * ------------------------------------------------------------------
524 * Image property cache
525 * ------------------------------------------------------------------
528 static void dupe_item_read_cache(DupeItem *di)
535 path = cache_find_location(CACHE_TYPE_SIM, di->fd->path);
538 if (filetime(di->fd->path) != filetime(path))
544 cd = cache_sim_data_load(path);
549 if (!di->simd && cd->sim)
554 if (di->width == 0 && di->height == 0 && cd->dimensions)
556 di->width = cd->width;
557 di->height = cd->height;
558 di->dimensions = (di->width << 16) + di->height;
560 if (!di->md5sum && cd->have_md5sum)
562 di->md5sum = md5_digest_to_text(cd->md5sum);
564 cache_sim_data_free(cd);
568 static void dupe_item_write_cache(DupeItem *di)
575 base = cache_get_location(CACHE_TYPE_SIM, di->fd->path, FALSE, &mode);
576 if (recursive_mkdir_if_not_exists(base, mode))
580 cd = cache_sim_data_new();
581 cd->path = cache_get_location(CACHE_TYPE_SIM, di->fd->path, TRUE, nullptr);
583 if (di->width != 0) cache_sim_data_set_dimensions(cd, di->width, di->height);
587 if (md5_digest_from_text(di->md5sum, digest)) cache_sim_data_set_md5sum(cd, digest);
589 if (di->simd) cache_sim_data_set_similarity(cd, di->simd);
591 if (cache_sim_data_save(cd))
593 filetime_set(cd->path, filetime(di->fd->path));
595 cache_sim_data_free(cd);
601 * ------------------------------------------------------------------
603 * ------------------------------------------------------------------
606 static gint dupe_listview_find_item(GtkListStore *store, DupeItem *item, GtkTreeIter *iter)
611 valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), iter);
615 gtk_tree_model_get(GTK_TREE_MODEL(store), iter, DUPE_COLUMN_POINTER, &item_n, -1);
616 if (item_n == item) return row;
618 valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), iter);
625 static void dupe_listview_add(DupeWindow *dw, DupeItem *parent, DupeItem *child)
629 gchar *text[DUPE_COLUMN_COUNT];
632 gboolean color_set = FALSE;
637 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(dw->listview)));
643 row = dupe_listview_find_item(store, parent, &iter);
644 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, DUPE_COLUMN_COLOR, &color_set, -1);
650 dm = static_cast<DupeMatch *>(child->group->data);
651 rank = static_cast<gint>(floor(dm->rank));
656 log_printf("NULL group in item!\n");
661 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter))
663 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, DUPE_COLUMN_COLOR, &color_set, -1);
664 color_set = !color_set;
675 di = (child) ? child : parent;
677 if (!child && dw->second_set)
679 text[DUPE_COLUMN_RANK] = g_strdup("[1]");
683 text[DUPE_COLUMN_RANK] = g_strdup((di->second) ? "(2)" : "");
687 text[DUPE_COLUMN_RANK] = g_strdup_printf("%d%s", rank, (di->second) ? " (2)" : "");
690 text[DUPE_COLUMN_THUMB] = nullptr;
691 text[DUPE_COLUMN_NAME] = const_cast<gchar *>(di->fd->name);
692 text[DUPE_COLUMN_SIZE] = text_from_size(di->fd->size);
693 text[DUPE_COLUMN_DATE] = const_cast<gchar *>(text_from_time(di->fd->date));
694 if (di->width > 0 && di->height > 0)
696 text[DUPE_COLUMN_DIMENSIONS] = g_strdup_printf("%d x %d", di->width, di->height);
700 text[DUPE_COLUMN_DIMENSIONS] = g_strdup("");
702 text[DUPE_COLUMN_PATH] = di->fd->path;
703 text[DUPE_COLUMN_COLOR] = nullptr;
705 gtk_list_store_insert(store, &iter, row);
706 gtk_list_store_set(store, &iter,
707 DUPE_COLUMN_POINTER, di,
708 DUPE_COLUMN_RANK, text[DUPE_COLUMN_RANK],
709 DUPE_COLUMN_THUMB, NULL,
710 DUPE_COLUMN_NAME, text[DUPE_COLUMN_NAME],
711 DUPE_COLUMN_SIZE, text[DUPE_COLUMN_SIZE],
712 DUPE_COLUMN_DATE, text[DUPE_COLUMN_DATE],
713 DUPE_COLUMN_DIMENSIONS, text[DUPE_COLUMN_DIMENSIONS],
714 DUPE_COLUMN_PATH, text[DUPE_COLUMN_PATH],
715 DUPE_COLUMN_COLOR, color_set,
716 DUPE_COLUMN_SET, dw->set_count,
719 g_free(text[DUPE_COLUMN_RANK]);
720 g_free(text[DUPE_COLUMN_SIZE]);
721 g_free(text[DUPE_COLUMN_DIMENSIONS]);
724 static void dupe_listview_select_dupes(DupeWindow *dw, DupeSelectType parents);
726 static void dupe_listview_populate(DupeWindow *dw)
731 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(dw->listview)));
732 gtk_list_store_clear(store);
734 work = g_list_last(dw->dupes);
737 auto parent = static_cast<DupeItem *>(work->data);
740 dupe_listview_add(dw, parent, nullptr);
742 temp = g_list_last(parent->group);
745 auto dm = static_cast<DupeMatch *>(temp->data);
750 dupe_listview_add(dw, parent, child);
758 gtk_tree_view_columns_autosize(GTK_TREE_VIEW(dw->listview));
760 if (options->duplicates_select_type == DUPE_SELECT_GROUP1)
762 dupe_listview_select_dupes(dw, DUPE_SELECT_GROUP1);
764 else if (options->duplicates_select_type == DUPE_SELECT_GROUP2)
766 dupe_listview_select_dupes(dw, DUPE_SELECT_GROUP2);
771 static void dupe_listview_remove(DupeWindow *dw, DupeItem *di)
779 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(dw->listview)));
780 row = dupe_listview_find_item(store, di, &iter);
783 tree_view_move_cursor_away(GTK_TREE_VIEW(dw->listview), &iter, TRUE);
784 gtk_list_store_remove(store, &iter);
786 if (g_list_find(dw->dupes, di) != nullptr)
788 if (!dw->color_frozen) dupe_listview_realign_colors(dw);
793 static GList *dupe_listview_get_filelist(DupeWindow *, GtkWidget *listview)
798 GList *list = nullptr;
800 store = gtk_tree_view_get_model(GTK_TREE_VIEW(listview));
801 valid = gtk_tree_model_get_iter_first(store, &iter);
805 gtk_tree_model_get(store, &iter, DUPE_COLUMN_POINTER, &di, -1);
806 list = g_list_prepend(list, file_data_ref(di->fd));
808 valid = gtk_tree_model_iter_next(store, &iter);
811 return g_list_reverse(list);
815 static GList *dupe_listview_get_selection(DupeWindow *, GtkWidget *listview)
818 GtkTreeSelection *selection;
820 GList *list = nullptr;
823 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(listview));
824 slist = gtk_tree_selection_get_selected_rows(selection, &store);
828 auto tpath = static_cast<GtkTreePath *>(work->data);
829 DupeItem *di = nullptr;
832 gtk_tree_model_get_iter(store, &iter, tpath);
833 gtk_tree_model_get(store, &iter, DUPE_COLUMN_POINTER, &di, -1);
836 list = g_list_prepend(list, file_data_ref(di->fd));
840 g_list_free_full(slist, reinterpret_cast<GDestroyNotify>(gtk_tree_path_free));
842 return g_list_reverse(list);
845 static gboolean dupe_listview_item_is_selected(DupeWindow *, DupeItem *di, GtkWidget *listview)
848 GtkTreeSelection *selection;
851 gboolean found = FALSE;
853 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(listview));
854 slist = gtk_tree_selection_get_selected_rows(selection, &store);
856 while (!found && work)
858 auto tpath = static_cast<GtkTreePath *>(work->data);
862 gtk_tree_model_get_iter(store, &iter, tpath);
863 gtk_tree_model_get(store, &iter, DUPE_COLUMN_POINTER, &di_n, -1);
864 if (di_n == di) found = TRUE;
867 g_list_free_full(slist, reinterpret_cast<GDestroyNotify>(gtk_tree_path_free));
872 static void dupe_listview_select_dupes(DupeWindow *dw, DupeSelectType parents)
875 GtkTreeSelection *selection;
879 gint set_count_last = -1;
881 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dw->listview));
882 gtk_tree_selection_unselect_all(selection);
884 store = gtk_tree_view_get_model(GTK_TREE_VIEW(dw->listview));
885 valid = gtk_tree_model_get_iter_first(store, &iter);
890 gtk_tree_model_get(store, &iter, DUPE_COLUMN_POINTER, &di, DUPE_COLUMN_SET, &set_count, -1);
891 if (set_count != set_count_last)
893 set_count_last = set_count;
894 if (parents == DUPE_SELECT_GROUP1)
896 gtk_tree_selection_select_iter(selection, &iter);
901 if (parents == DUPE_SELECT_GROUP2)
903 gtk_tree_selection_select_iter(selection, &iter);
906 valid = gtk_tree_model_iter_next(store, &iter);
911 * ------------------------------------------------------------------
912 * Match group manipulation
913 * ------------------------------------------------------------------
917 * @brief Search \a parent->group for \a child (#DupeItem)
923 static DupeMatch *dupe_match_find_match(DupeItem *child, DupeItem *parent)
927 work = parent->group;
930 auto dm = static_cast<DupeMatch *>(work->data);
931 if (dm->di == child) return dm;
938 * @brief Create #DupeMatch structure for \a child, and insert into \a parent->group list.
944 static void dupe_match_link_child(DupeItem *child, DupeItem *parent, gdouble rank)
948 dm = g_new0(DupeMatch, 1);
951 parent->group = g_list_append(parent->group, dm);
955 * @brief Link \a a & \a b as both parent and child
960 * Link \a a as child of \a b, and \a b as child of \a a
962 static void dupe_match_link(DupeItem *a, DupeItem *b, gdouble rank)
964 dupe_match_link_child(a, b, rank);
965 dupe_match_link_child(b, a, rank);
969 * @brief Remove \a child #DupeMatch from \a parent->group list.
974 static void dupe_match_unlink_child(DupeItem *child, DupeItem *parent)
978 dm = dupe_match_find_match(child, parent);
981 parent->group = g_list_remove(parent->group, dm);
987 * @brief Unlink \a a from \a b, and \a b from \a a
991 * Free the relevant #DupeMatch items from the #DupeItem group lists
993 static void dupe_match_unlink(DupeItem *a, DupeItem *b)
995 dupe_match_unlink_child(a, b);
996 dupe_match_unlink_child(b, a);
1002 * @param unlink_children
1004 * If \a unlink_children is set, unlink all entries in \a parent->group list. \n
1005 * Free the \a parent->group list and set group_rank to zero;
1007 static void dupe_match_link_clear(DupeItem *parent, gboolean unlink_children)
1009 if (unlink_children)
1013 work = parent->group;
1016 auto dm = static_cast<DupeMatch *>(work->data);
1019 dupe_match_unlink_child(parent, dm->di);
1023 g_list_free_full(parent->group, g_free);
1024 parent->group = nullptr;
1025 parent->group_rank = 0.0;
1029 * @brief Search \a parent->group list for \a child
1032 * @returns boolean TRUE/FALSE found/not found
1035 static gint dupe_match_link_exists(DupeItem *child, DupeItem *parent)
1037 return (dupe_match_find_match(child, parent) != nullptr);
1041 * @brief Search \a parent->group for \a child, and return \a child->rank
1044 * @returns \a dm->di->rank
1047 static gdouble dupe_match_link_rank(DupeItem *child, DupeItem *parent)
1051 dm = dupe_match_find_match(child, parent);
1052 if (dm) return dm->rank;
1058 * @brief Find highest rank in \a child->group
1062 * Search the #DupeMatch entries in the \a child->group list.
1063 * Return the #DupeItem with the highest rank. If more than one have
1064 * the same rank, the first encountered is used.
1066 static DupeItem *dupe_match_highest_rank(DupeItem *child)
1072 work = child->group;
1075 auto dm = static_cast<DupeMatch *>(work->data);
1076 if (!dr || dm->rank > dr->rank)
1083 return (dr) ? dr->di : nullptr;
1087 * @brief Compute and store \a parent->group_rank
1090 * Group_rank = (sum of all child ranks) / n
1092 static void dupe_match_rank_update(DupeItem *parent)
1098 work = parent->group;
1101 auto dm = static_cast<DupeMatch *>(work->data);
1109 parent->group_rank = rank / c;
1113 parent->group_rank = 0.0;
1117 static DupeItem *dupe_match_find_parent(DupeWindow *dw, DupeItem *child)
1121 if (g_list_find(dw->dupes, child)) return child;
1123 work = child->group;
1126 auto dm = static_cast<DupeMatch *>(work->data);
1127 if (g_list_find(dw->dupes, dm->di)) return dm->di;
1136 * @param work (#DupeItem) dw->list or dw->second_list
1138 * Unlink all #DupeItem-s in \a work.
1139 * Do not unlink children.
1141 static void dupe_match_reset_list(GList *work)
1145 auto di = static_cast<DupeItem *>(work->data);
1148 dupe_match_link_clear(di, FALSE);
1152 static void dupe_match_reparent(DupeWindow *dw, DupeItem *old_parent, DupeItem *new_parent)
1156 if (!old_parent || !new_parent || !dupe_match_link_exists(old_parent, new_parent)) return;
1158 dupe_match_link_clear(new_parent, TRUE);
1159 work = old_parent->group;
1162 auto dm = static_cast<DupeMatch *>(work->data);
1163 dupe_match_unlink_child(old_parent, dm->di);
1164 dupe_match_link_child(new_parent, dm->di, dm->rank);
1168 new_parent->group = old_parent->group;
1169 old_parent->group = nullptr;
1171 work = g_list_find(dw->dupes, old_parent);
1172 if (work) work->data = new_parent;
1175 static void dupe_match_print_group(DupeItem *di)
1179 log_printf("+ %f %s\n", di->group_rank, di->fd->name);
1184 auto dm = static_cast<DupeMatch *>(work->data);
1187 log_printf(" %f %s\n", dm->rank, dm->di->fd->name);
1193 static void dupe_match_print_list(GList *list)
1200 auto di = static_cast<DupeItem *>(work->data);
1201 dupe_match_print_group(di);
1206 /* level 3, unlinking and orphan handling */
1210 * @param parent \a di from \a child->group
1211 * @param[inout] list \a dw->list sorted by rank (#DupeItem)
1213 * @returns modified \a list
1215 * Called for each entry in \a child->group (#DupeMatch) with \a parent set to \a dm->di. \n
1216 * Find the highest rank #DupeItem of the \a parent's children. \n
1217 * If that is == \a child OR
1218 * highest rank #DupeItem of \a child == \a parent then FIXME:
1221 static GList *dupe_match_unlink_by_rank(DupeItem *child, DupeItem *parent, GList *list, DupeWindow *dw)
1223 DupeItem *best = nullptr;
1225 best = dupe_match_highest_rank(parent); // highest rank in parent->group
1226 if (best == child || dupe_match_highest_rank(child) == parent)
1231 DEBUG_2("link found %s to %s [%d]", child->fd->name, parent->fd->name, g_list_length(parent->group));
1233 work = parent->group;
1236 auto dm = static_cast<DupeMatch *>(work->data);
1241 if (orphan != child && g_list_length(orphan->group) < 2)
1243 dupe_match_link_clear(orphan, TRUE);
1244 if (!dw->second_set || orphan->second)
1246 dupe_match(orphan, child, dw->match_mask, &rank, FALSE);
1247 dupe_match_link(orphan, child, rank);
1249 list = g_list_remove(list, orphan);
1253 rank = dupe_match_link_rank(child, parent); // child->rank
1254 dupe_match_link_clear(parent, TRUE);
1255 dupe_match_link(child, parent, rank);
1256 list = g_list_remove(list, parent);
1260 DEBUG_2("unlinking %s and %s", child->fd->name, parent->fd->name);
1262 dupe_match_unlink(child, parent);
1271 * @param[inout] list \a dw->list sorted by rank (#DupeItem)
1274 * @returns modified \a list
1276 * Called for each entry in \a list.
1277 * Call unlink for each child in \a di->group
1279 static GList *dupe_match_group_filter(GList *list, DupeItem *di, DupeWindow *dw)
1283 work = g_list_last(di->group);
1286 auto dm = static_cast<DupeMatch *>(work->data);
1288 list = dupe_match_unlink_by_rank(di, dm->di, list, dw);
1297 * @param[inout] list \a dw->list sorted by rank (#DupeItem)
1299 * @returns Filtered \a list
1302 * Call group filter for each \a di in \a list
1304 static GList *dupe_match_group_trim(GList *list, DupeWindow *dw)
1311 auto di = static_cast<DupeItem *>(work->data);
1312 if (!di->second) list = dupe_match_group_filter(list, di, dw);
1314 if (di->second) list = g_list_remove(list, di);
1320 static gint dupe_match_sort_groups_cb(gconstpointer a, gconstpointer b)
1322 auto da = static_cast<const DupeMatch *>(a);
1323 auto db = static_cast<const DupeMatch *>(b);
1325 if (da->rank > db->rank) return -1;
1326 if (da->rank < db->rank) return 1;
1331 * @brief Sorts the children of each #DupeItem in \a list
1332 * @param list #DupeItem
1334 * Sorts the #DupeItem->group children on rank
1336 static void dupe_match_sort_groups(GList *list)
1343 auto di = static_cast<DupeItem *>(work->data);
1344 di->group = g_list_sort(di->group, dupe_match_sort_groups_cb);
1349 static gint dupe_match_totals_sort_cb(gconstpointer a, gconstpointer b)
1351 auto da = static_cast<const DupeItem *>(a);
1352 auto db = static_cast<const DupeItem *>(b);
1354 if (g_list_length(da->group) > g_list_length(db->group)) return -1;
1355 if (g_list_length(da->group) < g_list_length(db->group)) return 1;
1357 if (da->group_rank < db->group_rank) return -1;
1358 if (da->group_rank > db->group_rank) return 1;
1364 * @brief Callback for group_rank sort
1371 static gint dupe_match_rank_sort_cb(gconstpointer a, gconstpointer b)
1373 auto da = static_cast<const DupeItem *>(a);
1374 auto db = static_cast<const DupeItem *>(b);
1376 if (da->group_rank > db->group_rank) return -1;
1377 if (da->group_rank < db->group_rank) return 1;
1382 * @brief Sorts \a source_list by group-rank
1383 * @param source_list #DupeItem
1386 * Computes group_rank for each #DupeItem. \n
1387 * Items with no group list are ignored.
1388 * Returns allocated GList of #DupeItem-s sorted by group_rank
1390 static GList *dupe_match_rank_sort(GList *source_list)
1392 GList *list = nullptr;
1398 auto di = static_cast<DupeItem *>(work->data);
1402 dupe_match_rank_update(di); // Compute and store group_rank for di
1403 list = g_list_prepend(list, di);
1409 return g_list_sort(list, dupe_match_rank_sort_cb);
1413 * @brief Returns allocated GList of dupes sorted by totals
1414 * @param source_list
1419 static GList *dupe_match_totals_sort(GList *source_list)
1421 source_list = g_list_sort(source_list, dupe_match_totals_sort_cb);
1423 source_list = g_list_first(source_list);
1424 return g_list_reverse(source_list);
1433 static void dupe_match_rank(DupeWindow *dw)
1437 list = dupe_match_rank_sort(dw->list); // sorted by group_rank, no-matches filtered out
1439 if (required_debug_level(2)) dupe_match_print_list(list);
1441 DEBUG_1("Similar items: %d", g_list_length(list));
1442 list = dupe_match_group_trim(list, dw);
1443 DEBUG_1("Unique groups: %d", g_list_length(list));
1445 dupe_match_sort_groups(list);
1447 if (required_debug_level(2)) dupe_match_print_list(list);
1449 list = dupe_match_rank_sort(list);
1450 if (options->sort_totals)
1452 list = dupe_match_totals_sort(list);
1454 if (required_debug_level(2)) dupe_match_print_list(list);
1456 g_list_free(dw->dupes);
1461 * ------------------------------------------------------------------
1463 * ------------------------------------------------------------------
1475 * For similarity checks, compute rank - (similarity factor between a and b). \n
1476 * If rank < user-set sim value, returns FALSE.
1478 static gboolean dupe_match(DupeItem *a, DupeItem *b, DupeMatchType mask, gdouble *rank, gint fast)
1482 if (a->fd->path == b->fd->path) return FALSE;
1484 if (mask & DUPE_MATCH_ALL)
1488 if (mask & DUPE_MATCH_PATH)
1490 if (utf8_compare(a->fd->path, b->fd->path, TRUE) != 0) return FALSE;
1492 if (mask & DUPE_MATCH_NAME)
1494 if (strcmp(a->fd->collate_key_name, b->fd->collate_key_name) != 0) return FALSE;
1496 if (mask & DUPE_MATCH_NAME_CI)
1498 if (strcmp(a->fd->collate_key_name_nocase, b->fd->collate_key_name_nocase) != 0) return FALSE;
1500 if (mask & DUPE_MATCH_NAME_CONTENT)
1502 if (strcmp(a->fd->collate_key_name, b->fd->collate_key_name) == 0)
1504 if (!a->md5sum) a->md5sum = md5_text_from_file_utf8(a->fd->path, "");
1505 if (!b->md5sum) b->md5sum = md5_text_from_file_utf8(b->fd->path, "");
1506 if (a->md5sum[0] == '\0' ||
1507 b->md5sum[0] == '\0' ||
1508 strcmp(a->md5sum, b->md5sum) != 0)
1522 if (mask & DUPE_MATCH_NAME_CI_CONTENT)
1524 if (strcmp(a->fd->collate_key_name_nocase, b->fd->collate_key_name_nocase) == 0)
1526 if (!a->md5sum) a->md5sum = md5_text_from_file_utf8(a->fd->path, "");
1527 if (!b->md5sum) b->md5sum = md5_text_from_file_utf8(b->fd->path, "");
1528 if (a->md5sum[0] == '\0' ||
1529 b->md5sum[0] == '\0' ||
1530 strcmp(a->md5sum, b->md5sum) != 0)
1544 if (mask & DUPE_MATCH_SIZE)
1546 if (a->fd->size != b->fd->size) return FALSE;
1548 if (mask & DUPE_MATCH_DATE)
1550 if (a->fd->date != b->fd->date) return FALSE;
1552 if (mask & DUPE_MATCH_SUM)
1554 if (!a->md5sum) a->md5sum = md5_text_from_file_utf8(a->fd->path, "");
1555 if (!b->md5sum) b->md5sum = md5_text_from_file_utf8(b->fd->path, "");
1556 if (a->md5sum[0] == '\0' ||
1557 b->md5sum[0] == '\0' ||
1558 strcmp(a->md5sum, b->md5sum) != 0) return FALSE;
1560 if (mask & DUPE_MATCH_DIM)
1562 if (a->width == 0) image_load_dimensions(a->fd, &a->width, &a->height);
1563 if (b->width == 0) image_load_dimensions(b->fd, &b->width, &b->height);
1564 if (a->width != b->width || a->height != b->height) return FALSE;
1566 if (mask & DUPE_MATCH_SIM_HIGH ||
1567 mask & DUPE_MATCH_SIM_MED ||
1568 mask & DUPE_MATCH_SIM_LOW ||
1569 mask & DUPE_MATCH_SIM_CUSTOM)
1574 if (mask & DUPE_MATCH_SIM_HIGH) m = 0.95;
1575 else if (mask & DUPE_MATCH_SIM_MED) m = 0.90;
1576 else if (mask & DUPE_MATCH_SIM_CUSTOM) m = static_cast<gdouble>(options->duplicates_similarity_threshold) / 100.0;
1581 f = image_sim_compare_fast(a->simd, b->simd, m);
1585 f = image_sim_compare(a->simd, b->simd);
1590 if (f < m) return FALSE;
1592 DEBUG_3("similar: %32s %32s = %f", a->fd->name, b->fd->name, f);
1599 * @brief Determine if there is a match
1603 * @returns DUPE_MATCH/DUPE_NO_MATCH/DUPE_NAME_MATCH
1604 * DUPE_NAME_MATCH is used for name != contents searches:
1605 * the name and content match i.e.
1606 * no match, but keep searching
1608 * Called when stepping down the array looking for adjacent matches,
1609 * and from the 2nd set search.
1611 * Is not used for similarity checks.
1613 static DUPE_CHECK_RESULT dupe_match_check(DupeItem *di1, DupeItem *di2, gpointer data)
1615 auto dw = static_cast<DupeWindow *>(data);
1616 DupeMatchType mask = dw->match_mask;
1618 if (mask & DUPE_MATCH_ALL)
1622 if (mask & DUPE_MATCH_PATH)
1624 if (utf8_compare(di1->fd->path, di2->fd->path, TRUE) != 0)
1626 return DUPE_NO_MATCH;
1629 if (mask & DUPE_MATCH_NAME)
1631 if (g_strcmp0(di1->fd->collate_key_name, di2->fd->collate_key_name) != 0)
1633 return DUPE_NO_MATCH;
1636 if (mask & DUPE_MATCH_NAME_CI)
1638 if (g_strcmp0(di1->fd->collate_key_name_nocase, di2->fd->collate_key_name_nocase) != 0 )
1640 return DUPE_NO_MATCH;
1643 if (mask & DUPE_MATCH_NAME_CONTENT)
1645 if (g_strcmp0(di1->fd->collate_key_name, di2->fd->collate_key_name) == 0)
1647 if (g_strcmp0(di1->md5sum, di2->md5sum) == 0)
1649 return DUPE_NAME_MATCH;
1654 return DUPE_NO_MATCH;
1657 if (mask & DUPE_MATCH_NAME_CI_CONTENT)
1659 if (strcmp(di1->fd->collate_key_name_nocase, di2->fd->collate_key_name_nocase) == 0)
1661 if (g_strcmp0(di1->md5sum, di2->md5sum) == 0)
1663 return DUPE_NAME_MATCH;
1668 return DUPE_NO_MATCH;
1671 if (mask & DUPE_MATCH_SIZE)
1673 if (di1->fd->size != di2->fd->size)
1675 return DUPE_NO_MATCH;
1678 if (mask & DUPE_MATCH_DATE)
1680 if (di1->fd->date != di2->fd->date)
1682 return DUPE_NO_MATCH;
1685 if (mask & DUPE_MATCH_SUM)
1687 if (g_strcmp0(di1->md5sum, di2->md5sum) != 0)
1689 return DUPE_NO_MATCH;
1692 if (mask & DUPE_MATCH_DIM)
1694 if (di1->dimensions != di2->dimensions)
1696 return DUPE_NO_MATCH;
1704 * @brief The callback for the binary search
1707 * @param param_match_mask
1708 * @returns negative/0/positive
1710 * Is not used for similarity checks.
1712 * Used only when two file sets are used.
1713 * Requires use of a global for param_match_mask because there is no
1714 * g_array_binary_search_with_data() function in glib.
1716 static gint dupe_match_binary_search_cb(gconstpointer a, gconstpointer b)
1718 auto di1 = *(static_cast<const DupeItem *const *>(a));
1719 auto di2 = static_cast<const DupeItem *>(b);
1720 DupeMatchType mask = param_match_mask;
1722 if (mask & DUPE_MATCH_ALL)
1726 if (mask & DUPE_MATCH_PATH)
1728 return utf8_compare(di1->fd->path, di2->fd->path, TRUE);
1730 if (mask & DUPE_MATCH_NAME)
1732 return g_strcmp0(di1->fd->collate_key_name, di2->fd->collate_key_name);
1734 if (mask & DUPE_MATCH_NAME_CI)
1736 return strcmp(di1->fd->collate_key_name_nocase, di2->fd->collate_key_name_nocase);
1738 if (mask & DUPE_MATCH_NAME_CONTENT)
1740 return g_strcmp0(di1->fd->collate_key_name, di2->fd->collate_key_name);
1742 if (mask & DUPE_MATCH_NAME_CI_CONTENT)
1744 return strcmp(di1->fd->collate_key_name_nocase, di2->fd->collate_key_name_nocase);
1746 if (mask & DUPE_MATCH_SIZE)
1748 return (di1->fd->size - di2->fd->size);
1750 if (mask & DUPE_MATCH_DATE)
1752 return (di1->fd->date - di2->fd->date);
1754 if (mask & DUPE_MATCH_SUM)
1756 return g_strcmp0(di1->md5sum, di2->md5sum);
1758 if (mask & DUPE_MATCH_DIM)
1760 return (di1->dimensions - di2->dimensions);
1767 * @brief The callback for the array sort
1771 * @returns negative/0/positive
1773 * Is not used for similarity checks.
1775 static gint dupe_match_sort_cb(gconstpointer a, gconstpointer b, gpointer data)
1777 auto di1 = *(static_cast<const DupeItem *const *>(a));
1778 auto di2 = *(static_cast<const DupeItem *const *>(b));
1779 auto dw = static_cast<DupeWindow *>(data);
1780 DupeMatchType mask = dw->match_mask;
1782 if (mask & DUPE_MATCH_ALL)
1786 if (mask & DUPE_MATCH_PATH)
1788 return utf8_compare(di1->fd->path, di2->fd->path, TRUE);
1790 if (mask & DUPE_MATCH_NAME)
1792 return g_strcmp0(di1->fd->collate_key_name, di2->fd->collate_key_name);
1794 if (mask & DUPE_MATCH_NAME_CI)
1796 return strcmp(di1->fd->collate_key_name_nocase, di2->fd->collate_key_name_nocase);
1798 if (mask & DUPE_MATCH_NAME_CONTENT)
1800 return g_strcmp0(di1->fd->collate_key_name, di2->fd->collate_key_name);
1802 if (mask & DUPE_MATCH_NAME_CI_CONTENT)
1804 return strcmp(di1->fd->collate_key_name_nocase, di2->fd->collate_key_name_nocase);
1806 if (mask & DUPE_MATCH_SIZE)
1808 return (di1->fd->size - di2->fd->size);
1810 if (mask & DUPE_MATCH_DATE)
1812 return (di1->fd->date - di2->fd->date);
1814 if (mask & DUPE_MATCH_SUM)
1816 if (di1->md5sum[0] == '\0' || di2->md5sum[0] == '\0')
1822 return strcmp(di1->md5sum, di2->md5sum);
1825 if (mask & DUPE_MATCH_DIM)
1827 if (!di1 || !di2 || !di1->width || !di1->height || !di2->width || !di2->height)
1831 return (di1->dimensions - di2->dimensions);
1834 return 0; // should not execute
1838 * @brief Check for duplicate matches
1841 * Is not used for similarity checks.
1843 * Loads the file sets into an array and sorts on the searched
1846 * If one file set, steps down the array looking for adjacent equal values.
1848 * If two file sets, steps down the first set and for each value
1849 * does a binary search for matches in the second set.
1851 static void dupe_array_check(DupeWindow *dw )
1858 DUPE_CHECK_RESULT check_result;
1859 param_match_mask = dw->match_mask;
1860 guint out_match_index;
1861 gboolean match_found = FALSE;;
1863 if (!dw->list) return;
1865 array_set1 = g_array_new(TRUE, TRUE, sizeof(gpointer));
1866 array_set2 = g_array_new(TRUE, TRUE, sizeof(gpointer));
1867 dupe_match_reset_list(dw->list);
1872 auto di = static_cast<DupeItem *>(work->data);
1873 g_array_append_val(array_set1, di);
1877 g_array_sort_with_data(array_set1, dupe_match_sort_cb, dw);
1881 /* Two sets - nothing can be done until a second set is loaded */
1882 if (dw->second_list)
1884 work = dw->second_list;
1887 g_array_append_val(array_set2, (work->data));
1890 g_array_sort_with_data(array_set2, dupe_match_sort_cb, dw);
1892 for (i_set1 = 0; i_set1 <= static_cast<gint>(array_set1->len) - 1; i_set1++)
1894 auto di1 = static_cast<DupeItem *>(g_array_index(array_set1, gpointer, i_set1));
1895 DupeItem *di2 = nullptr;
1896 /* If multiple identical entries in set 1, use the last one */
1897 if (i_set1 < static_cast<gint>(array_set1->len) - 2)
1899 di2 = static_cast<DupeItem *>(g_array_index(array_set1, gpointer, i_set1 + 1));
1900 check_result = dupe_match_check(di1, di2, dw);
1901 if (check_result == DUPE_MATCH || check_result == DUPE_NAME_MATCH)
1907 #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION >= 62))
1908 match_found = g_array_binary_search(array_set2, di1, dupe_match_binary_search_cb, &out_match_index);
1912 match_found = FALSE;
1913 for(i=0; i < array_set2->len; i++)
1915 di2 = static_cast<DupeItem *>(g_array_index(array_set2, gpointer, i));
1916 check_result = dupe_match_check(di1, di2, dw);
1917 if (check_result == DUPE_MATCH)
1920 out_match_index = i;
1928 di2 = static_cast<DupeItem *>(g_array_index(array_set2, gpointer, out_match_index));
1930 check_result = dupe_match_check(di1, di2, dw);
1931 if (check_result == DUPE_MATCH || check_result == DUPE_NAME_MATCH)
1933 if (check_result == DUPE_MATCH)
1935 dupe_match_link(di2, di1, 0.0);
1937 i_set2 = out_match_index + 1;
1939 if (i_set2 > static_cast<gint>(array_set2->len) - 1)
1943 /* Look for multiple matches in set 2 for item di1 */
1944 di2 = static_cast<DupeItem *>(g_array_index(array_set2, gpointer, i_set2));
1945 check_result = dupe_match_check(di1, di2, dw);
1946 while (check_result == DUPE_MATCH || check_result == DUPE_NAME_MATCH)
1948 if (check_result == DUPE_MATCH)
1950 dupe_match_link(di2, di1, 0.0);
1953 if (i_set2 > static_cast<gint>(array_set2->len) - 1)
1957 di2 = static_cast<DupeItem *>(g_array_index(array_set2, gpointer, i_set2));
1958 check_result = dupe_match_check(di1, di2, dw);
1967 /* File set 1 only */
1968 g_list_free(dw->dupes);
1969 dw->dupes = nullptr;
1971 if (static_cast<gint>(array_set1->len) > 1)
1973 for (i_set1 = 0; i_set1 <= static_cast<gint>(array_set1->len) - 2; i_set1++)
1975 auto di1 = static_cast<DupeItem *>(g_array_index(array_set1, gpointer, i_set1));
1976 auto di2 = static_cast<DupeItem *>(g_array_index(array_set1, gpointer, i_set1 + 1));
1978 check_result = dupe_match_check(di1, di2, dw);
1979 if (check_result == DUPE_MATCH || check_result == DUPE_NAME_MATCH)
1981 if (check_result == DUPE_MATCH)
1983 dupe_match_link(di2, di1, 0.0);
1987 if ( i_set1 + 1 > static_cast<gint>(array_set1->len) - 1)
1991 /* Look for multiple matches for item di1 */
1992 di2 = static_cast<DupeItem *>(g_array_index(array_set1, gpointer, i_set1 + 1));
1993 check_result = dupe_match_check(di1, di2, dw);
1994 while (check_result == DUPE_MATCH || check_result == DUPE_NAME_MATCH)
1996 if (check_result == DUPE_MATCH)
1998 dupe_match_link(di2, di1, 0.0);
2002 if (i_set1 + 1 > static_cast<gint>(array_set1->len) - 1)
2006 di2 = static_cast<DupeItem *>(g_array_index(array_set1, gpointer, i_set1 + 1));
2007 check_result = dupe_match_check(di1, di2, dw);
2013 g_array_free(array_set1, TRUE);
2014 g_array_free(array_set2, TRUE);
2018 * @brief Look for similarity match
2023 * Only used for similarity checks.\n
2024 * Called from dupe_check_cb.
2025 * Called for each entry in the list.
2026 * Steps through the list looking for matches against needle.
2027 * Pushes a #DupeQueueItem onto thread pool queue.
2029 static void dupe_list_check_match(DupeWindow *dw, DupeItem *needle, GList *start)
2036 work = dw->second_list;
2044 work = g_list_last(dw->list);
2047 dqi = g_new0(DupeQueueItem, 1);
2048 dqi->needle = needle;
2051 dqi->index = dw->queue_count;
2052 g_thread_pool_push(dw->dupe_comparison_thread_pool, dqi, nullptr);
2056 * ------------------------------------------------------------------
2057 * Thumbnail handling
2058 * ------------------------------------------------------------------
2061 static void dupe_listview_set_thumb(DupeWindow *dw, DupeItem *di, GtkTreeIter *iter)
2063 GtkListStore *store;
2066 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(dw->listview)));
2069 if (dupe_listview_find_item(store, di, &iter_n) >= 0)
2075 if (iter) gtk_list_store_set(store, iter, DUPE_COLUMN_THUMB, di->pixbuf, -1);
2078 static void dupe_thumb_do(DupeWindow *dw)
2082 if (!dw->thumb_loader || !dw->thumb_item) return;
2083 di = dw->thumb_item;
2085 if (di->pixbuf) g_object_unref(di->pixbuf);
2086 di->pixbuf = thumb_loader_get_pixbuf(dw->thumb_loader);
2088 dupe_listview_set_thumb(dw, di, nullptr);
2091 static void dupe_thumb_error_cb(ThumbLoader *, gpointer data)
2093 auto dw = static_cast<DupeWindow *>(data);
2096 dupe_thumb_step(dw);
2099 static void dupe_thumb_done_cb(ThumbLoader *, gpointer data)
2101 auto dw = static_cast<DupeWindow *>(data);
2104 dupe_thumb_step(dw);
2107 static void dupe_thumb_step(DupeWindow *dw)
2109 GtkTreeModel *store;
2111 DupeItem *di = nullptr;
2116 store = gtk_tree_view_get_model(GTK_TREE_VIEW(dw->listview));
2117 valid = gtk_tree_model_get_iter_first(store, &iter);
2119 while (!di && valid)
2124 gtk_tree_model_get(store, &iter, DUPE_COLUMN_POINTER, &di, DUPE_COLUMN_THUMB, &pixbuf, -1);
2125 if (pixbuf || di->pixbuf)
2127 if (!pixbuf) gtk_list_store_set(GTK_LIST_STORE(store), &iter, DUPE_COLUMN_THUMB, di->pixbuf, -1);
2131 valid = gtk_tree_model_iter_next(store, &iter);
2135 while (gtk_tree_model_iter_next(store, &iter)) length++;
2140 dw->thumb_item = nullptr;
2141 thumb_loader_free(dw->thumb_loader);
2142 dw->thumb_loader = nullptr;
2144 dupe_window_update_progress(dw, nullptr, 0.0, FALSE);
2148 dupe_window_update_progress(dw, _("Loading thumbs..."),
2149 length == 0 ? 0.0 : static_cast<gdouble>(row) / length, FALSE);
2151 dw->thumb_item = di;
2152 thumb_loader_free(dw->thumb_loader);
2153 dw->thumb_loader = thumb_loader_new(options->thumbnails.max_width, options->thumbnails.max_height);
2155 thumb_loader_set_callbacks(dw->thumb_loader,
2157 dupe_thumb_error_cb,
2162 if (!thumb_loader_start(dw->thumb_loader, di->fd))
2164 /* error, handle it, do next */
2165 DEBUG_1("error loading thumb for %s", di->fd->path);
2167 dupe_thumb_step(dw);
2172 * ------------------------------------------------------------------
2173 * Dupe checking loop
2174 * ------------------------------------------------------------------
2177 static void dupe_check_stop(DupeWindow *dw)
2179 if (dw->idle_id > 0)
2181 g_source_remove(dw->idle_id);
2187 while (dw->thread_count < dw->queue_count) // Wait for the queue to empty
2189 dupe_window_update_progress(dw, nullptr, 0.0, FALSE);
2190 widget_set_cursor(dw->listview, -1);
2193 g_list_free(dw->search_matches);
2194 dw->search_matches = nullptr;
2196 if (dw->idle_id || dw->img_loader || dw->thumb_loader)
2198 if (dw->idle_id > 0)
2200 g_source_remove(dw->idle_id);
2203 dupe_window_update_progress(dw, nullptr, 0.0, FALSE);
2204 widget_set_cursor(dw->listview, -1);
2207 if (dw->add_files_queue_id)
2209 g_source_remove(dw->add_files_queue_id);
2210 dw->add_files_queue_id = 0;
2211 dupe_destroy_list_cache(dw);
2212 gtk_widget_set_sensitive(dw->controls_box, TRUE);
2213 if (g_list_length(dw->add_files_queue) > 0)
2215 filelist_free(dw->add_files_queue);
2217 dw->add_files_queue = nullptr;
2218 dupe_window_update_progress(dw, nullptr, 0.0, FALSE);
2219 widget_set_cursor(dw->listview, -1);
2222 thumb_loader_free(dw->thumb_loader);
2223 dw->thumb_loader = nullptr;
2225 image_loader_free(dw->img_loader);
2226 dw->img_loader = nullptr;
2229 static void dupe_check_stop_cb(GtkWidget *, gpointer data)
2231 auto dw = static_cast<DupeWindow *>(data);
2233 dupe_check_stop(dw);
2236 static void dupe_loader_done_cb(ImageLoader *il, gpointer data)
2238 auto dw = static_cast<DupeWindow *>(data);
2241 pixbuf = image_loader_get_pixbuf(il);
2243 if (dw->setup_point)
2245 auto di = static_cast<DupeItem *>(dw->setup_point->data);
2249 di->simd = image_sim_new_from_pixbuf(pixbuf);
2253 image_sim_fill_data(di->simd, pixbuf);
2256 if (di->width == 0 && di->height == 0)
2258 di->width = gdk_pixbuf_get_width(pixbuf);
2259 di->height = gdk_pixbuf_get_height(pixbuf);
2261 if (options->thumbnails.enable_caching)
2263 dupe_item_write_cache(di);
2266 image_sim_alternate_processing(di->simd);
2269 image_loader_free(dw->img_loader);
2270 dw->img_loader = nullptr;
2272 dw->idle_id = g_idle_add(dupe_check_cb, dw);
2275 static void dupe_setup_reset(DupeWindow *dw)
2277 dw->setup_point = nullptr;
2279 dw->setup_time = msec_time();
2280 dw->setup_time_count = 0;
2283 static GList *dupe_setup_point_step(DupeWindow *dw, GList *p)
2285 if (!p) return nullptr;
2287 if (p->next) return p->next;
2289 if (dw->second_set && g_list_first(p) == dw->list) return dw->second_list;
2295 * @brief Generates the sumcheck or dimensions
2296 * @param list Set1 or set2
2297 * @returns TRUE/FALSE = not completed/completed
2299 * Ensures that the DIs contain the MD5SUM or dimensions for all items in
2300 * the list. One item at a time. Re-enters if not completed.
2302 static gboolean create_checksums_dimensions(DupeWindow *dw, GList *list)
2304 if ((dw->match_mask & DUPE_MATCH_SUM) ||
2305 (dw->match_mask & DUPE_MATCH_NAME_CONTENT) ||
2306 (dw->match_mask & DUPE_MATCH_NAME_CI_CONTENT))
2309 if (!dw->setup_point) dw->setup_point = list; // setup_point clear on 1st entry
2311 while (dw->setup_point)
2313 auto di = static_cast<DupeItem *>(dw->setup_point->data);
2315 dw->setup_point = dupe_setup_point_step(dw, dw->setup_point);
2320 dupe_window_update_progress(dw, _("Reading checksums..."),
2321 dw->setup_count == 0 ? 0.0 : static_cast<gdouble>(dw->setup_n - 1) / dw->setup_count, FALSE);
2323 if (options->thumbnails.enable_caching)
2325 dupe_item_read_cache(di);
2332 di->md5sum = md5_text_from_file_utf8(di->fd->path, "");
2333 if (options->thumbnails.enable_caching)
2335 dupe_item_write_cache(di);
2340 dupe_setup_reset(dw);
2343 if ((dw->match_mask & DUPE_MATCH_DIM) )
2345 /* Dimensions only */
2346 if (!dw->setup_point) dw->setup_point = list;
2348 while (dw->setup_point)
2350 auto di = static_cast<DupeItem *>(dw->setup_point->data);
2352 dw->setup_point = dupe_setup_point_step(dw, dw->setup_point);
2354 if (di->width == 0 && di->height == 0)
2356 dupe_window_update_progress(dw, _("Reading dimensions..."),
2357 dw->setup_count == 0 ? 0.0 : static_cast<gdouble>(dw->setup_n - 1) / dw->setup_count, FALSE);
2359 if (options->thumbnails.enable_caching)
2361 dupe_item_read_cache(di);
2362 if (di->width != 0 || di->height != 0)
2368 image_load_dimensions(di->fd, &di->width, &di->height);
2369 di->dimensions = (di->width << 16) + di->height;
2370 if (options->thumbnails.enable_caching)
2372 dupe_item_write_cache(di);
2377 dupe_setup_reset(dw);
2384 * @brief Compare func. for sorting search matches
2385 * @param a #DupeSearchMatch
2386 * @param b #DupeSearchMatch
2389 * Used only for similarity checks\n
2390 * Sorts search matches on order they were inserted into the pool queue
2392 static gint sort_func(gconstpointer a, gconstpointer b)
2394 return static_cast<const DupeSearchMatch *>(a)->index - static_cast<const DupeSearchMatch *>(b)->index;
2398 * @brief Check set 1 (and set 2) for matches
2399 * @param data DupeWindow
2400 * @returns TRUE/FALSE = not completed/completed
2402 * Initiated from start, loader done and item remove
2404 * On first entry generates di->MD5SUM, di->dimensions and sim data,
2405 * and updates the cache.
2407 static gboolean dupe_check_cb(gpointer data)
2409 auto dw = static_cast<DupeWindow *>(data);
2410 DupeSearchMatch *search_match_list_item;
2414 return G_SOURCE_REMOVE;
2417 if (!dw->setup_done) /* Clear on 1st entry */
2421 if (create_checksums_dimensions(dw, dw->list))
2423 return G_SOURCE_CONTINUE;
2426 if (dw->second_list)
2428 if (create_checksums_dimensions(dw, dw->second_list))
2430 return G_SOURCE_CONTINUE;
2433 if ((dw->match_mask & DUPE_MATCH_SIM_HIGH ||
2434 dw->match_mask & DUPE_MATCH_SIM_MED ||
2435 dw->match_mask & DUPE_MATCH_SIM_LOW ||
2436 dw->match_mask & DUPE_MATCH_SIM_CUSTOM) &&
2437 !(dw->setup_mask & DUPE_MATCH_SIM_MED) )
2439 /* Similarity only */
2440 if (!dw->setup_point) dw->setup_point = dw->list;
2442 while (dw->setup_point)
2444 auto di = static_cast<DupeItem *>(dw->setup_point->data);
2448 dupe_window_update_progress(dw, _("Reading similarity data..."),
2449 dw->setup_count == 0 ? 0.0 : static_cast<gdouble>(dw->setup_n) / dw->setup_count, FALSE);
2451 if (options->thumbnails.enable_caching)
2453 dupe_item_read_cache(di);
2454 if (cache_sim_data_filled(di->simd))
2456 image_sim_alternate_processing(di->simd);
2457 return G_SOURCE_CONTINUE;
2461 dw->img_loader = image_loader_new(di->fd);
2462 image_loader_set_buffer_size(dw->img_loader, 8);
2463 g_signal_connect(G_OBJECT(dw->img_loader), "error", (GCallback)dupe_loader_done_cb, dw);
2464 g_signal_connect(G_OBJECT(dw->img_loader), "done", (GCallback)dupe_loader_done_cb, dw);
2466 if (!image_loader_start(dw->img_loader))
2468 image_sim_free(di->simd);
2469 di->simd = image_sim_new();
2470 image_loader_free(dw->img_loader);
2471 dw->img_loader = nullptr;
2472 return G_SOURCE_CONTINUE;
2475 return G_SOURCE_REMOVE;
2478 dw->setup_point = dupe_setup_point_step(dw, dw->setup_point);
2481 dw->setup_mask = static_cast<DupeMatchType>(dw->setup_mask | DUPE_MATCH_SIM_MED);
2482 dupe_setup_reset(dw);
2485 /* End of setup not done */
2486 dupe_window_update_progress(dw, _("Comparing..."), 0.0, FALSE);
2487 dw->setup_done = TRUE;
2488 dupe_setup_reset(dw);
2489 dw->setup_count = g_list_length(dw->list);
2492 /* Setup done - dw->working set to NULL below
2493 * Set before 1st entry: dw->working = g_list_last(dw->list)
2494 * Set before 1st entry: dw->setup_count = g_list_length(dw->list)
2498 /* Similarity check threads may still be running */
2499 if (dw->setup_count > 0 && (dw->match_mask == DUPE_MATCH_SIM_HIGH ||
2500 dw->match_mask == DUPE_MATCH_SIM_MED ||
2501 dw->match_mask == DUPE_MATCH_SIM_LOW ||
2502 dw->match_mask == DUPE_MATCH_SIM_CUSTOM))
2504 if( dw->thread_count < dw->queue_count)
2506 dupe_window_update_progress(dw, _("Comparing..."), 0.0, FALSE);
2508 return G_SOURCE_CONTINUE;
2511 if (dw->search_matches_sorted == nullptr)
2513 dw->search_matches_sorted = g_list_sort(dw->search_matches, sort_func);
2514 dupe_setup_reset(dw);
2517 while (dw->search_matches_sorted)
2520 dupe_window_update_progress(dw, _("Sorting..."), 0.0, FALSE);
2521 search_match_list_item = static_cast<DupeSearchMatch *>(dw->search_matches_sorted->data);
2523 if (!dupe_match_link_exists(search_match_list_item->a, search_match_list_item->b))
2525 dupe_match_link(search_match_list_item->a, search_match_list_item->b, search_match_list_item->rank);
2528 dw->search_matches_sorted = dw->search_matches_sorted->next;
2530 if (dw->search_matches_sorted != nullptr)
2532 return G_SOURCE_CONTINUE;
2535 g_list_free(dw->search_matches);
2536 dw->search_matches = nullptr;
2537 g_list_free(dw->search_matches_sorted);
2538 dw->search_matches_sorted = nullptr;
2539 dw->setup_count = 0;
2543 if (dw->setup_count > 0)
2545 dw->setup_count = 0;
2546 dupe_window_update_progress(dw, _("Sorting..."), 1.0, TRUE);
2547 return G_SOURCE_CONTINUE;
2552 dupe_window_update_progress(dw, nullptr, 0.0, FALSE);
2554 dupe_match_rank(dw);
2555 dupe_window_update_count(dw, FALSE);
2557 dupe_listview_populate(dw);
2560 if (dw->show_thumbs) dupe_thumb_step(dw);
2562 widget_set_cursor(dw->listview, -1);
2564 return G_SOURCE_REMOVE;
2568 /* Setup done - working */
2569 if (dw->match_mask == DUPE_MATCH_SIM_HIGH ||
2570 dw->match_mask == DUPE_MATCH_SIM_MED ||
2571 dw->match_mask == DUPE_MATCH_SIM_LOW ||
2572 dw->match_mask == DUPE_MATCH_SIM_CUSTOM)
2574 /* This is the similarity comparison */
2575 dupe_list_check_match(dw, static_cast<DupeItem *>(dw->working->data), dw->working);
2576 dupe_window_update_progress(dw, _("Queuing..."), dw->setup_count == 0 ? 0.0 : static_cast<gdouble>(dw->setup_n) / dw->setup_count, FALSE);
2580 dw->working = dw->working->prev; /* Is NULL when complete */
2584 /* This is the comparison for all other parameters.
2585 * dupe_array_check() processes the entire list in one go
2587 dw->working = nullptr;
2588 dupe_window_update_progress(dw, _("Comparing..."), 0.0, FALSE);
2589 dupe_array_check(dw);
2592 return G_SOURCE_CONTINUE;
2595 static void dupe_check_start(DupeWindow *dw)
2597 dw->setup_done = FALSE;
2599 dw->setup_count = g_list_length(dw->list);
2600 if (dw->second_set) dw->setup_count += g_list_length(dw->second_list);
2602 dw->setup_mask = DUPE_MATCH_NONE;
2603 dupe_setup_reset(dw);
2605 dw->working = g_list_last(dw->list);
2607 dupe_window_update_count(dw, TRUE);
2608 widget_set_cursor(dw->listview, GDK_WATCH);
2609 dw->queue_count = 0;
2610 dw->thread_count = 0;
2611 dw->search_matches_sorted = nullptr;
2614 if (dw->idle_id) return;
2616 dw->idle_id = g_idle_add(dupe_check_cb, dw);
2619 static gboolean dupe_check_start_cb(gpointer data)
2621 auto dw = static_cast<DupeWindow *>(data);
2623 dupe_check_start(dw);
2629 * ------------------------------------------------------------------
2630 * Item addition, removal
2631 * ------------------------------------------------------------------
2634 static void dupe_item_remove(DupeWindow *dw, DupeItem *di)
2638 /* handle things that may be in progress... */
2639 if (dw->working && dw->working->data == di)
2641 dw->working = dw->working->prev;
2643 if (dw->thumb_loader && dw->thumb_item == di)
2645 dupe_thumb_step(dw);
2647 if (dw->setup_point && dw->setup_point->data == di)
2649 dw->setup_point = dupe_setup_point_step(dw, dw->setup_point);
2652 image_loader_free(dw->img_loader);
2653 dw->img_loader = nullptr;
2654 dw->idle_id = g_idle_add(dupe_check_cb, dw);
2658 if (di->group && dw->dupes)
2660 /* is a dupe, must remove from group/reset children if a parent */
2663 parent = dupe_match_find_parent(dw, di);
2666 if (g_list_length(parent->group) < 2)
2670 child = dupe_match_highest_rank(parent);
2671 dupe_match_link_clear(child, TRUE);
2672 dupe_listview_remove(dw, child);
2674 dupe_match_link_clear(parent, TRUE);
2675 dupe_listview_remove(dw, parent);
2676 dw->dupes = g_list_remove(dw->dupes, parent);
2680 DupeItem *new_parent;
2683 dm = static_cast<DupeMatch *>(parent->group->data);
2684 new_parent = dm->di;
2685 dupe_match_reparent(dw, parent, new_parent);
2686 dupe_listview_remove(dw, parent);
2691 if (g_list_length(parent->group) < 2)
2693 dupe_match_link_clear(parent, TRUE);
2694 dupe_listview_remove(dw, parent);
2695 dw->dupes = g_list_remove(dw->dupes, parent);
2697 dupe_match_link_clear(di, TRUE);
2698 dupe_listview_remove(dw, di);
2703 /* not a dupe, or not sorted yet, simply reset */
2704 dupe_match_link_clear(di, TRUE);
2707 if (dw->second_list && g_list_find(dw->second_list, di))
2709 dupe_second_remove(dw, di);
2713 dw->list = g_list_remove(dw->list, di);
2717 dupe_window_update_count(dw, FALSE);
2720 #pragma GCC diagnostic push
2721 #pragma GCC diagnostic ignored "-Wunused-function"
2722 static gboolean dupe_item_remove_by_path_unused(DupeWindow *dw, const gchar *path)
2726 di = dupe_item_find_path_unused(dw, path);
2727 if (!di) return FALSE;
2729 dupe_item_remove(dw, di);
2733 #pragma GCC diagnostic pop
2735 static gboolean dupe_files_add_queue_cb(gpointer data)
2737 DupeItem *di = nullptr;
2738 auto dw = static_cast<DupeWindow *>(data);
2740 GList *queue = dw->add_files_queue;
2742 gtk_progress_bar_pulse(GTK_PROGRESS_BAR(dw->extra_label));
2744 if (queue == nullptr)
2746 dw->add_files_queue_id = 0;
2747 dupe_destroy_list_cache(dw);
2748 g_idle_add(dupe_check_start_cb, dw);
2749 gtk_widget_set_sensitive(dw->controls_box, TRUE);
2753 fd = static_cast<FileData *>(queue->data);
2756 if (isfile(fd->path))
2758 di = dupe_item_new(fd);
2760 else if (isdir(fd->path))
2763 dw->add_files_queue = g_list_remove(dw->add_files_queue, g_list_first(dw->add_files_queue)->data);
2765 if (filelist_read(fd, &f, &d))
2767 f = filelist_filter(f, FALSE);
2768 d = filelist_filter(d, TRUE);
2770 dw->add_files_queue = g_list_concat(f, dw->add_files_queue);
2771 dw->add_files_queue = g_list_concat(d, dw->add_files_queue);
2776 /* Not a file and not a dir */
2777 dw->add_files_queue = g_list_remove(dw->add_files_queue, g_list_first(dw->add_files_queue)->data);
2783 /* A dir was found. Process the contents on next entry */
2787 dw->add_files_queue = g_list_remove(dw->add_files_queue, g_list_first(dw->add_files_queue)->data);
2789 dupe_item_read_cache(di);
2791 /* Ensure images in the lists have unique FileDatas */
2792 if (!dupe_insert_in_list_cache(dw, di->fd))
2798 if (dw->second_drop)
2800 dupe_second_add(dw, di);
2804 dw->list = g_list_prepend(dw->list, di);
2807 if (dw->add_files_queue != nullptr)
2813 dw->add_files_queue_id = 0;
2814 dupe_destroy_list_cache(dw);
2815 g_idle_add(dupe_check_start_cb, dw);
2816 gtk_widget_set_sensitive(dw->controls_box, TRUE);
2821 static void dupe_files_add(DupeWindow *dw, CollectionData *, CollectInfo *info,
2822 FileData *fd, gboolean recurse)
2824 DupeItem *di = nullptr;
2828 di = dupe_item_new(info->fd);
2832 if (isfile(fd->path) && !g_file_test(fd->path, G_FILE_TEST_IS_SYMLINK))
2834 di = dupe_item_new(fd);
2836 else if (isdir(fd->path) && recurse)
2839 if (filelist_read(fd, &f, &d))
2843 f = filelist_filter(f, FALSE);
2844 d = filelist_filter(d, TRUE);
2849 dupe_files_add(dw, nullptr, nullptr, static_cast<FileData *>(work->data), TRUE);
2856 dupe_files_add(dw, nullptr, nullptr, static_cast<FileData *>(work->data), TRUE);
2866 dupe_item_read_cache(di);
2868 /* Ensure images in the lists have unique FileDatas */
2871 work = g_list_first(dw->list);
2874 di_list = static_cast<DupeItem *>(work->data);
2875 if (di_list->fd == di->fd)
2885 if (dw->second_list)
2887 work = g_list_first(dw->second_list);
2890 di_list = static_cast<DupeItem *>(work->data);
2891 if (di_list->fd == di->fd)
2902 if (dw->second_drop)
2904 dupe_second_add(dw, di);
2908 dw->list = g_list_prepend(dw->list, di);
2912 static void dupe_init_list_cache(DupeWindow *dw)
2914 dw->list_cache = g_hash_table_new(g_direct_hash, g_direct_equal);
2915 dw->second_list_cache = g_hash_table_new(g_direct_hash, g_direct_equal);
2917 for (GList *i = dw->list; i != nullptr; i = i->next)
2919 auto di = static_cast<DupeItem *>(i->data);
2921 g_hash_table_add(dw->list_cache, di->fd);
2924 for (GList *i = dw->second_list; i != nullptr; i = i->next)
2926 auto di = static_cast<DupeItem *>(i->data);
2928 g_hash_table_add(dw->second_list_cache, di->fd);
2932 static void dupe_destroy_list_cache(DupeWindow *dw)
2934 g_hash_table_destroy(dw->list_cache);
2935 g_hash_table_destroy(dw->second_list_cache);
2939 * @brief Return true if the fd was not in the cache
2946 static gboolean dupe_insert_in_list_cache(DupeWindow *dw, FileData *fd)
2949 dw->second_drop ? dw->second_list_cache : dw->list_cache;
2950 /* We do this as a lookup + add as we don't want to overwrite
2951 items as that would leak the old value. */
2952 if (g_hash_table_lookup(table, fd) != nullptr)
2954 return g_hash_table_add(table, fd);
2957 void dupe_window_add_collection(DupeWindow *dw, CollectionData *collection)
2961 info = collection_get_first(collection);
2964 dupe_files_add(dw, collection, info, nullptr, FALSE);
2965 info = collection_next_by_info(collection, info);
2968 dupe_check_start(dw);
2971 void dupe_window_add_files(DupeWindow *dw, GList *list, gboolean recurse)
2978 auto fd = static_cast<FileData *>(work->data);
2980 if (isdir(fd->path) && !recurse)
2984 if (filelist_read(fd, &f, &d))
2991 /* Add only the files, ignore the dirs when no recurse */
2992 dw->add_files_queue = g_list_prepend(dw->add_files_queue, work_file->data);
2993 file_data_ref((FileData *)work_file->data);
2994 work_file = work_file->next;
3002 dw->add_files_queue = g_list_prepend(dw->add_files_queue, fd);
3006 if (dw->add_files_queue_id == 0)
3008 gtk_progress_bar_pulse(GTK_PROGRESS_BAR(dw->extra_label));
3009 gtk_progress_bar_set_pulse_step(GTK_PROGRESS_BAR(dw->extra_label), DUPE_PROGRESS_PULSE_STEP);
3010 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(dw->extra_label), _("Loading file list"));
3012 dupe_init_list_cache(dw);
3013 dw->add_files_queue_id = g_idle_add(dupe_files_add_queue_cb, dw);
3014 gtk_widget_set_sensitive(dw->controls_box, FALSE);
3018 static void dupe_item_update(DupeWindow *dw, DupeItem *di)
3020 if ( (dw->match_mask & DUPE_MATCH_NAME) || (dw->match_mask & DUPE_MATCH_PATH || (dw->match_mask & DUPE_MATCH_NAME_CI)) )
3022 /* only effects matches on name or path */
3024 FileData *fd = file_data_ref(di->fd);
3027 second = di->second;
3028 dupe_item_remove(dw, di);
3030 dw->second_drop = second;
3031 dupe_files_add(dw, NULL, NULL, fd, FALSE);
3032 dw->second_drop = FALSE;
3034 file_data_unref(fd);
3036 dupe_check_start(dw);
3040 GtkListStore *store;
3043 /* update the listview(s) */
3045 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(dw->listview)));
3046 row = dupe_listview_find_item(store, di, &iter);
3049 gtk_list_store_set(store, &iter,
3050 DUPE_COLUMN_NAME, di->fd->name,
3051 DUPE_COLUMN_PATH, di->fd->path, -1);
3054 if (dw->second_listview)
3056 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(dw->second_listview)));
3057 row = dupe_listview_find_item(store, di, &iter);
3060 gtk_list_store_set(store, &iter, 1, di->fd->path, -1);
3067 static void dupe_item_update_fd_in_list(DupeWindow *dw, FileData *fd, GList *work)
3071 auto di = static_cast<DupeItem *>(work->data);
3074 dupe_item_update(dw, di);
3080 static void dupe_item_update_fd(DupeWindow *dw, FileData *fd)
3082 dupe_item_update_fd_in_list(dw, fd, dw->list);
3083 if (dw->second_set) dupe_item_update_fd_in_list(dw, fd, dw->second_list);
3088 * ------------------------------------------------------------------
3090 * ------------------------------------------------------------------
3093 static GtkWidget *dupe_display_label(GtkWidget *vbox, const gchar *description, const gchar *text)
3098 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
3100 label = gtk_label_new(description);
3101 gq_gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
3102 gtk_widget_show(label);
3104 label = gtk_label_new(text);
3105 gq_gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
3106 gtk_widget_show(label);
3108 gq_gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
3109 gtk_widget_show(hbox);
3114 static void dupe_display_stats(DupeWindow *dw, DupeItem *di)
3121 gd = file_util_gen_dlg("Image thumbprint debug info", "thumbprint",
3124 generic_dialog_add_button(gd, GQ_ICON_CLOSE, _("Close"), nullptr, TRUE);
3126 dupe_display_label(gd->vbox, "name:", di->fd->name);
3127 buf = text_from_size(di->fd->size);
3128 dupe_display_label(gd->vbox, "size:", buf);
3130 dupe_display_label(gd->vbox, "date:", text_from_time(di->fd->date));
3131 buf = g_strdup_printf("%d x %d", di->width, di->height);
3132 dupe_display_label(gd->vbox, "dimensions:", buf);
3134 dupe_display_label(gd->vbox, "md5sum:", (di->md5sum) ? di->md5sum : "not generated");
3136 dupe_display_label(gd->vbox, "thumbprint:", (di->simd) ? "" : "not generated");
3147 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, 32, 32);
3148 rs = gdk_pixbuf_get_rowstride(pixbuf);
3149 d_pix = gdk_pixbuf_get_pixels(pixbuf);
3151 for (y = 0; y < 32; y++)
3153 dp = d_pix + (y * rs);
3155 for (x = 0; x < 32; x++)
3157 *(dp++) = di->simd->avg_r[sp + x];
3158 *(dp++) = di->simd->avg_g[sp + x];
3159 *(dp++) = di->simd->avg_b[sp + x];
3163 image = gtk_image_new_from_pixbuf(pixbuf);
3164 gq_gtk_box_pack_start(GTK_BOX(gd->vbox), image, FALSE, FALSE, 0);
3165 gtk_widget_show(image);
3167 g_object_unref(pixbuf);
3170 gtk_widget_show(gd->dialog);
3173 static void dupe_window_recompare(DupeWindow *dw)
3175 GtkListStore *store;
3177 dupe_check_stop(dw);
3179 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(dw->listview)));
3180 gtk_list_store_clear(store);
3182 g_list_free(dw->dupes);
3183 dw->dupes = nullptr;
3185 dupe_match_reset_list(dw->list);
3186 dupe_match_reset_list(dw->second_list);
3189 dupe_check_start(dw);
3192 static void dupe_menu_view(DupeWindow *dw, DupeItem *di, GtkWidget *listview, gint new_window)
3196 if (di->collection && collection_info_valid(di->collection, di->info))
3200 view_window_new_from_collection(di->collection, di->info);
3204 layout_image_set_collection(nullptr, di->collection, di->info);
3213 list = dupe_listview_get_selection(dw, listview);
3214 view_window_new_from_list(list);
3215 filelist_free(list);
3219 layout_set_fd(nullptr, di->fd);
3224 static void dupe_window_remove_selection(DupeWindow *dw, GtkWidget *listview)
3226 GtkTreeSelection *selection;
3227 GtkTreeModel *store;
3230 GList *list = nullptr;
3233 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(listview));
3234 slist = gtk_tree_selection_get_selected_rows(selection, &store);
3238 auto tpath = static_cast<GtkTreePath *>(work->data);
3239 DupeItem *di = nullptr;
3241 gtk_tree_model_get_iter(store, &iter, tpath);
3242 gtk_tree_model_get(store, &iter, DUPE_COLUMN_POINTER, &di, -1);
3243 if (di) list = g_list_prepend(list, di);
3246 g_list_free_full(slist, reinterpret_cast<GDestroyNotify>(gtk_tree_path_free));
3248 dw->color_frozen = TRUE;
3254 di = static_cast<DupeItem *>(work->data);
3256 dupe_item_remove(dw, di);
3258 dw->color_frozen = FALSE;
3262 dupe_listview_realign_colors(dw);
3265 static void dupe_window_edit_selected(DupeWindow *dw, const gchar *key)
3267 file_util_start_editor_from_filelist(key, dupe_listview_get_selection(dw, dw->listview), nullptr, dw->window);
3270 static void dupe_window_collection_from_selection(DupeWindow *dw)
3275 list = dupe_listview_get_selection(dw, dw->listview);
3276 w = collection_window_new(nullptr);
3277 collection_table_add_filelist(w->table, list);
3278 filelist_free(list);
3281 static void dupe_window_append_file_list(DupeWindow *dw, gint on_second)
3285 dw->second_drop = (dw->second_set && on_second);
3287 list = layout_list(nullptr);
3288 dupe_window_add_files(dw, list, FALSE);
3289 filelist_free(list);
3293 *-------------------------------------------------------------------
3294 * main pop-up menu callbacks
3295 *-------------------------------------------------------------------
3298 static void dupe_menu_view_cb(GtkWidget *, gpointer data)
3300 auto dw = static_cast<DupeWindow *>(data);
3302 if (dw->click_item) dupe_menu_view(dw, dw->click_item, dw->listview, FALSE);
3305 static void dupe_menu_viewnew_cb(GtkWidget *, gpointer data)
3307 auto dw = static_cast<DupeWindow *>(data);
3309 if (dw->click_item) dupe_menu_view(dw, dw->click_item, dw->listview, TRUE);
3312 static void dupe_menu_select_all_cb(GtkWidget *, gpointer data)
3314 auto dw = static_cast<DupeWindow *>(data);
3315 GtkTreeSelection *selection;
3317 options->duplicates_select_type = DUPE_SELECT_NONE;
3318 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dw->listview));
3319 gtk_tree_selection_select_all(selection);
3322 static void dupe_menu_select_none_cb(GtkWidget *, gpointer data)
3324 auto dw = static_cast<DupeWindow *>(data);
3325 GtkTreeSelection *selection;
3327 options->duplicates_select_type = DUPE_SELECT_NONE;
3328 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dw->listview));
3329 gtk_tree_selection_unselect_all(selection);
3332 static void dupe_menu_select_dupes_set1_cb(GtkWidget *, gpointer data)
3334 auto dw = static_cast<DupeWindow *>(data);
3336 options->duplicates_select_type = DUPE_SELECT_GROUP1;
3337 dupe_listview_select_dupes(dw, DUPE_SELECT_GROUP1);
3340 static void dupe_menu_select_dupes_set2_cb(GtkWidget *, gpointer data)
3342 auto dw = static_cast<DupeWindow *>(data);
3344 options->duplicates_select_type = DUPE_SELECT_GROUP2;
3345 dupe_listview_select_dupes(dw, DUPE_SELECT_GROUP2);
3348 static void dupe_menu_edit_cb(GtkWidget *widget, gpointer data)
3351 auto key = static_cast<const gchar *>(data);
3353 dw = static_cast<DupeWindow *>(submenu_item_get_data(widget));
3356 dupe_window_edit_selected(dw, key);
3359 static void dupe_menu_print_cb(GtkWidget *, gpointer data)
3361 auto dw = static_cast<DupeWindow *>(data);
3364 fd = (dw->click_item) ? dw->click_item->fd : nullptr;
3366 print_window_new(fd,
3367 dupe_listview_get_selection(dw, dw->listview),
3368 dupe_listview_get_filelist(dw, dw->listview), dw->window);
3371 static void dupe_menu_copy_cb(GtkWidget *, gpointer data)
3373 auto dw = static_cast<DupeWindow *>(data);
3375 file_util_copy(nullptr, dupe_listview_get_selection(dw, dw->listview), nullptr, dw->window);
3378 static void dupe_menu_move_cb(GtkWidget *, gpointer data)
3380 auto dw = static_cast<DupeWindow *>(data);
3382 file_util_move(nullptr, dupe_listview_get_selection(dw, dw->listview), nullptr, dw->window);
3385 static void dupe_menu_rename_cb(GtkWidget *, gpointer data)
3387 auto dw = static_cast<DupeWindow *>(data);
3389 file_util_rename(nullptr, dupe_listview_get_selection(dw, dw->listview), dw->window);
3392 static void dupe_menu_delete_cb(GtkWidget *, gpointer data)
3394 auto dw = static_cast<DupeWindow *>(data);
3396 options->file_ops.safe_delete_enable = FALSE;
3397 file_util_delete_notify_done(nullptr, dupe_listview_get_selection(dw, dw->listview), dw->window, delete_finished_cb, dw);
3400 static void dupe_menu_move_to_trash_cb(GtkWidget *, gpointer data)
3402 auto dw = static_cast<DupeWindow *>(data);
3404 options->file_ops.safe_delete_enable = TRUE;
3405 file_util_delete_notify_done(nullptr, dupe_listview_get_selection(dw, dw->listview), dw->window, delete_finished_cb, dw);
3408 static void dupe_menu_copy_path_cb(GtkWidget *, gpointer data)
3410 auto dw = static_cast<DupeWindow *>(data);
3412 file_util_copy_path_list_to_clipboard(dupe_listview_get_selection(dw, dw->listview), TRUE);
3415 static void dupe_menu_copy_path_unquoted_cb(GtkWidget *, gpointer data)
3417 auto dw = static_cast<DupeWindow *>(data);
3419 file_util_copy_path_list_to_clipboard(dupe_listview_get_selection(dw, dw->listview), FALSE);
3422 static void dupe_menu_remove_cb(GtkWidget *, gpointer data)
3424 auto dw = static_cast<DupeWindow *>(data);
3426 dupe_window_remove_selection(dw, dw->listview);
3429 static void dupe_menu_clear_cb(GtkWidget *, gpointer data)
3431 auto dw = static_cast<DupeWindow *>(data);
3433 dupe_window_clear(dw);
3436 static void dupe_menu_close_cb(GtkWidget *, gpointer data)
3438 auto dw = static_cast<DupeWindow *>(data);
3440 dupe_window_close(dw);
3443 static void dupe_menu_popup_destroy_cb(GtkWidget *, gpointer data)
3445 auto editmenu_fd_list = static_cast<GList *>(data);
3447 filelist_free(editmenu_fd_list);
3450 static GList *dupe_window_get_fd_list(DupeWindow *dw)
3454 if (gtk_widget_has_focus(dw->second_listview))
3456 list = dupe_listview_get_selection(dw, dw->second_listview);
3460 list = dupe_listview_get_selection(dw, dw->listview);
3467 * @brief Add file selection list to a collection
3469 * @param[in] data Index to the collection list menu item selected, or -1 for new collection
3473 static void dupe_pop_menu_collections_cb(GtkWidget *widget, gpointer data)
3476 GList *selection_list;
3478 dw = static_cast<DupeWindow *>(submenu_item_get_data(widget));
3479 selection_list = dupe_listview_get_selection(dw, dw->listview);
3480 pop_menu_collections(selection_list, data);
3482 filelist_free(selection_list);
3485 static GtkWidget *dupe_menu_popup_main(DupeWindow *dw, DupeItem *di)
3490 GList *editmenu_fd_list;
3491 GtkAccelGroup *accel_group;
3493 on_row = (di != nullptr);
3495 menu = popup_menu_short_lived();
3497 accel_group = gtk_accel_group_new();
3498 gtk_menu_set_accel_group(GTK_MENU(menu), accel_group);
3500 g_object_set_data(G_OBJECT(menu), "window_keys", dupe_window_keys);
3501 g_object_set_data(G_OBJECT(menu), "accel_group", accel_group);
3503 menu_item_add_sensitive(menu, _("_View"), on_row,
3504 G_CALLBACK(dupe_menu_view_cb), dw);
3505 menu_item_add_icon_sensitive(menu, _("View in _new window"), GQ_ICON_NEW, on_row,
3506 G_CALLBACK(dupe_menu_viewnew_cb), dw);
3507 menu_item_add_divider(menu);
3508 menu_item_add_sensitive(menu, _("Select all"), (dw->dupes != nullptr),
3509 G_CALLBACK(dupe_menu_select_all_cb), dw);
3510 menu_item_add_sensitive(menu, _("Select none"), (dw->dupes != nullptr),
3511 G_CALLBACK(dupe_menu_select_none_cb), dw);
3512 menu_item_add_sensitive(menu, _("Select group _1 duplicates"), (dw->dupes != nullptr),
3513 G_CALLBACK(dupe_menu_select_dupes_set1_cb), dw);
3514 menu_item_add_sensitive(menu, _("Select group _2 duplicates"), (dw->dupes != nullptr),
3515 G_CALLBACK(dupe_menu_select_dupes_set2_cb), dw);
3516 menu_item_add_divider(menu);
3518 submenu_add_export(menu, &item, G_CALLBACK(dupe_pop_menu_export_cb), dw);
3519 gtk_widget_set_sensitive(item, on_row);
3520 menu_item_add_divider(menu);
3522 editmenu_fd_list = dupe_window_get_fd_list(dw);
3523 g_signal_connect(G_OBJECT(menu), "destroy",
3524 G_CALLBACK(dupe_menu_popup_destroy_cb), editmenu_fd_list);
3525 submenu_add_edit(menu, &item, G_CALLBACK(dupe_menu_edit_cb), dw, editmenu_fd_list);
3526 if (!on_row) gtk_widget_set_sensitive(item, FALSE);
3528 submenu_add_collections(menu, &item,
3529 G_CALLBACK(dupe_pop_menu_collections_cb), dw);
3530 gtk_widget_set_sensitive(item, on_row);
3532 menu_item_add_icon_sensitive(menu, _("Print..."), GQ_ICON_PRINT, on_row,
3533 G_CALLBACK(dupe_menu_print_cb), dw);
3534 menu_item_add_divider(menu);
3535 menu_item_add_icon_sensitive(menu, _("_Copy..."), GQ_ICON_COPY, on_row,
3536 G_CALLBACK(dupe_menu_copy_cb), dw);
3537 menu_item_add_sensitive(menu, _("_Move..."), on_row,
3538 G_CALLBACK(dupe_menu_move_cb), dw);
3539 menu_item_add_sensitive(menu, _("_Rename..."), on_row,
3540 G_CALLBACK(dupe_menu_rename_cb), dw);
3541 menu_item_add_sensitive(menu, _("_Copy path"), on_row,
3542 G_CALLBACK(dupe_menu_copy_path_cb), dw);
3543 menu_item_add_sensitive(menu, _("_Copy path unquoted"), on_row,
3544 G_CALLBACK(dupe_menu_copy_path_unquoted_cb), dw);
3546 menu_item_add_divider(menu);
3547 menu_item_add_icon_sensitive(menu,
3548 options->file_ops.confirm_move_to_trash ? _("Move to Trash...") :
3549 _("Move to Trash"), GQ_ICON_DELETE, on_row,
3550 G_CALLBACK(dupe_menu_move_to_trash_cb), dw);
3551 menu_item_add_icon_sensitive(menu,
3552 options->file_ops.confirm_delete ? _("_Delete...") :
3553 _("_Delete"), GQ_ICON_DELETE_SHRED, on_row,
3554 G_CALLBACK(dupe_menu_delete_cb), dw);
3556 menu_item_add_divider(menu);
3557 menu_item_add_icon_sensitive(menu, _("Rem_ove"), GQ_ICON_REMOVE, on_row,
3558 G_CALLBACK(dupe_menu_remove_cb), dw);
3559 menu_item_add_icon_sensitive(menu, _("C_lear"), GQ_ICON_CLEAR, (dw->list != nullptr),
3560 G_CALLBACK(dupe_menu_clear_cb), dw);
3561 menu_item_add_divider(menu);
3562 menu_item_add_icon(menu, _("Close _window"), GQ_ICON_CLOSE,
3563 G_CALLBACK(dupe_menu_close_cb), dw);
3568 static gboolean dupe_listview_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
3570 auto dw = static_cast<DupeWindow *>(data);
3571 GtkTreeModel *store;
3574 DupeItem *di = nullptr;
3576 store = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
3578 if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), bevent->x, bevent->y,
3579 &tpath, nullptr, nullptr, nullptr))
3581 gtk_tree_model_get_iter(store, &iter, tpath);
3582 gtk_tree_model_get(store, &iter, DUPE_COLUMN_POINTER, &di, -1);
3583 gtk_tree_path_free(tpath);
3586 dw->click_item = di;
3588 if (bevent->button == MOUSE_BUTTON_RIGHT)
3590 /* right click menu */
3593 if (bevent->state & GDK_CONTROL_MASK && bevent->state & GDK_SHIFT_MASK)
3595 dupe_display_stats(dw, di);
3598 if (widget == dw->listview)
3600 menu = dupe_menu_popup_main(dw, di);
3604 menu = dupe_menu_popup_second(dw, di);
3606 gtk_menu_popup_at_pointer(GTK_MENU(menu), nullptr);
3609 if (!di) return FALSE;
3611 if (bevent->button == MOUSE_BUTTON_LEFT &&
3612 bevent->type == GDK_2BUTTON_PRESS)
3614 dupe_menu_view(dw, di, widget, FALSE);
3617 if (bevent->button == MOUSE_BUTTON_MIDDLE) return TRUE;
3619 if (bevent->button == MOUSE_BUTTON_RIGHT)
3621 if (!dupe_listview_item_is_selected(dw, di, widget))
3623 GtkTreeSelection *selection;
3625 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
3626 gtk_tree_selection_unselect_all(selection);
3627 gtk_tree_selection_select_iter(selection, &iter);
3629 tpath = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &iter);
3630 gtk_tree_view_set_cursor(GTK_TREE_VIEW(widget), tpath, nullptr, FALSE);
3631 gtk_tree_path_free(tpath);
3637 if (bevent->button == MOUSE_BUTTON_LEFT &&
3638 bevent->type == GDK_BUTTON_PRESS &&
3639 !(bevent->state & GDK_SHIFT_MASK ) &&
3640 !(bevent->state & GDK_CONTROL_MASK ) &&
3641 dupe_listview_item_is_selected(dw, di, widget))
3643 /* this selection handled on release_cb */
3644 gtk_widget_grab_focus(widget);
3651 static gboolean dupe_listview_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
3653 auto dw = static_cast<DupeWindow *>(data);
3654 GtkTreeModel *store;
3657 DupeItem *di = nullptr;
3659 if (bevent->button != MOUSE_BUTTON_LEFT && bevent->button != MOUSE_BUTTON_MIDDLE) return TRUE;
3661 store = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
3663 if ((bevent->x != 0 || bevent->y != 0) &&
3664 gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), bevent->x, bevent->y,
3665 &tpath, nullptr, nullptr, nullptr))
3667 gtk_tree_model_get_iter(store, &iter, tpath);
3668 gtk_tree_model_get(store, &iter, DUPE_COLUMN_POINTER, &di, -1);
3669 gtk_tree_path_free(tpath);
3672 if (bevent->button == MOUSE_BUTTON_MIDDLE)
3674 if (di && dw->click_item == di)
3676 GtkTreeSelection *selection;
3678 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
3679 if (dupe_listview_item_is_selected(dw, di, widget))
3681 gtk_tree_selection_unselect_iter(selection, &iter);
3685 gtk_tree_selection_select_iter(selection, &iter);
3691 if (di && dw->click_item == di &&
3692 !(bevent->state & GDK_SHIFT_MASK ) &&
3693 !(bevent->state & GDK_CONTROL_MASK ) &&
3694 dupe_listview_item_is_selected(dw, di, widget))
3696 GtkTreeSelection *selection;
3698 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
3699 gtk_tree_selection_unselect_all(selection);
3700 gtk_tree_selection_select_iter(selection, &iter);
3702 tpath = gtk_tree_model_get_path(store, &iter);
3703 gtk_tree_view_set_cursor(GTK_TREE_VIEW(widget), tpath, nullptr, FALSE);
3704 gtk_tree_path_free(tpath);
3713 *-------------------------------------------------------------------
3715 *-------------------------------------------------------------------
3718 static void dupe_second_update_status(DupeWindow *dw)
3722 buf = g_strdup_printf(_("%d files (set 2)"), g_list_length(dw->second_list));
3723 gtk_label_set_text(GTK_LABEL(dw->second_status_label), buf);
3727 static void dupe_second_add(DupeWindow *dw, DupeItem *di)
3729 GtkListStore *store;
3735 dw->second_list = g_list_prepend(dw->second_list, di);
3737 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(dw->second_listview)));
3738 gtk_list_store_append(store, &iter);
3739 gtk_list_store_set(store, &iter, DUPE_COLUMN_POINTER, di, 1, di->fd->path, -1);
3741 dupe_second_update_status(dw);
3744 static void dupe_second_remove(DupeWindow *dw, DupeItem *di)
3746 GtkListStore *store;
3749 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(dw->second_listview)));
3750 if (dupe_listview_find_item(store, di, &iter) >= 0)
3752 tree_view_move_cursor_away(GTK_TREE_VIEW(dw->second_listview), &iter, TRUE);
3753 gtk_list_store_remove(store, &iter);
3756 dw->second_list = g_list_remove(dw->second_list, di);
3758 dupe_second_update_status(dw);
3761 static void dupe_second_clear(DupeWindow *dw)
3763 GtkListStore *store;
3765 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(dw->second_listview)));
3766 gtk_list_store_clear(store);
3767 gtk_tree_view_columns_autosize(GTK_TREE_VIEW(dw->second_listview));
3769 g_list_free(dw->dupes);
3770 dw->dupes = nullptr;
3772 g_list_free_full(dw->second_list, reinterpret_cast<GDestroyNotify>(dupe_item_free));
3773 dw->second_list = nullptr;
3775 dupe_match_reset_list(dw->list);
3777 dupe_second_update_status(dw);
3780 static void dupe_second_menu_view_cb(GtkWidget *, gpointer data)
3782 auto dw = static_cast<DupeWindow *>(data);
3784 if (dw->click_item) dupe_menu_view(dw, dw->click_item, dw->second_listview, FALSE);
3787 static void dupe_second_menu_viewnew_cb(GtkWidget *, gpointer data)
3789 auto dw = static_cast<DupeWindow *>(data);
3791 if (dw->click_item) dupe_menu_view(dw, dw->click_item, dw->second_listview, TRUE);
3794 static void dupe_second_menu_select_all_cb(GtkWidget *, gpointer data)
3796 GtkTreeSelection *selection;
3797 auto dw = static_cast<DupeWindow *>(data);
3799 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dw->second_listview));
3800 gtk_tree_selection_select_all(selection);
3803 static void dupe_second_menu_select_none_cb(GtkWidget *, gpointer data)
3805 GtkTreeSelection *selection;
3806 auto dw = static_cast<DupeWindow *>(data);
3808 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dw->second_listview));
3809 gtk_tree_selection_unselect_all(selection);
3812 static void dupe_second_menu_remove_cb(GtkWidget *, gpointer data)
3814 auto dw = static_cast<DupeWindow *>(data);
3816 dupe_window_remove_selection(dw, dw->second_listview);
3819 static void dupe_second_menu_clear_cb(GtkWidget *, gpointer data)
3821 auto dw = static_cast<DupeWindow *>(data);
3823 dupe_second_clear(dw);
3824 dupe_window_recompare(dw);
3827 static GtkWidget *dupe_menu_popup_second(DupeWindow *dw, DupeItem *di)
3830 gboolean notempty = (dw->second_list != nullptr);
3831 gboolean on_row = (di != nullptr);
3832 GtkAccelGroup *accel_group;
3834 menu = popup_menu_short_lived();
3835 accel_group = gtk_accel_group_new();
3836 gtk_menu_set_accel_group(GTK_MENU(menu), accel_group);
3838 g_object_set_data(G_OBJECT(menu), "window_keys", dupe_window_keys);
3839 g_object_set_data(G_OBJECT(menu), "accel_group", accel_group);
3841 menu_item_add_sensitive(menu, _("_View"), on_row,
3842 G_CALLBACK(dupe_second_menu_view_cb), dw);
3843 menu_item_add_icon_sensitive(menu, _("View in _new window"), GQ_ICON_NEW, on_row,
3844 G_CALLBACK(dupe_second_menu_viewnew_cb), dw);
3845 menu_item_add_divider(menu);
3846 menu_item_add_sensitive(menu, _("Select all"), notempty,
3847 G_CALLBACK(dupe_second_menu_select_all_cb), dw);
3848 menu_item_add_sensitive(menu, _("Select none"), notempty,
3849 G_CALLBACK(dupe_second_menu_select_none_cb), dw);
3850 menu_item_add_divider(menu);
3851 menu_item_add_icon_sensitive(menu, _("Rem_ove"), GQ_ICON_REMOVE, on_row,
3852 G_CALLBACK(dupe_second_menu_remove_cb), dw);
3853 menu_item_add_icon_sensitive(menu, _("C_lear"), GQ_ICON_CLEAR, notempty,
3854 G_CALLBACK(dupe_second_menu_clear_cb), dw);
3855 menu_item_add_divider(menu);
3856 menu_item_add_icon(menu, _("Close _window"), GQ_ICON_CLOSE,
3857 G_CALLBACK(dupe_menu_close_cb), dw);
3862 static void dupe_second_set_toggle_cb(GtkWidget *widget, gpointer data)
3864 auto dw = static_cast<DupeWindow *>(data);
3866 dw->second_set = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
3870 dupe_second_update_status(dw);
3871 gtk_grid_set_column_spacing(GTK_GRID(dw->table), PREF_PAD_GAP);
3872 gtk_widget_show(dw->second_vbox);
3876 gtk_grid_set_column_spacing(GTK_GRID(dw->table), 0);
3877 gtk_widget_hide(dw->second_vbox);
3878 dupe_second_clear(dw);
3881 dupe_window_recompare(dw);
3884 static void dupe_sort_totals_toggle_cb(GtkWidget *widget, gpointer data)
3886 auto dw = static_cast<DupeWindow *>(data);
3888 options->sort_totals = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
3889 dupe_window_recompare(dw);
3894 *-------------------------------------------------------------------
3896 *-------------------------------------------------------------------
3900 DUPE_MENU_COLUMN_NAME = 0,
3901 DUPE_MENU_COLUMN_MASK
3904 static void dupe_listview_show_rank(GtkWidget *listview, gboolean rank);
3906 static void dupe_menu_type_cb(GtkWidget *combo, gpointer data)
3908 auto dw = static_cast<DupeWindow *>(data);
3909 GtkTreeModel *store;
3912 store = gtk_combo_box_get_model(GTK_COMBO_BOX(combo));
3913 if (!gtk_combo_box_get_active_iter(GTK_COMBO_BOX(combo), &iter)) return;
3914 gtk_tree_model_get(store, &iter, DUPE_MENU_COLUMN_MASK, &dw->match_mask, -1);
3916 options->duplicates_match = dw->match_mask;
3918 if (dw->match_mask & (DUPE_MATCH_SIM_HIGH | DUPE_MATCH_SIM_MED | DUPE_MATCH_SIM_LOW | DUPE_MATCH_SIM_CUSTOM))
3920 dupe_listview_show_rank(dw->listview, TRUE);
3924 dupe_listview_show_rank(dw->listview, FALSE);
3926 dupe_window_recompare(dw);
3929 static void dupe_menu_add_item(GtkListStore *store, const gchar *text, DupeMatchType type, DupeWindow *dw)
3933 gtk_list_store_append(store, &iter);
3934 gtk_list_store_set(store, &iter, DUPE_MENU_COLUMN_NAME, text,
3935 DUPE_MENU_COLUMN_MASK, type, -1);
3937 if (dw->match_mask == type) gtk_combo_box_set_active_iter(GTK_COMBO_BOX(dw->combo), &iter);
3940 static void dupe_menu_setup(DupeWindow *dw)
3942 GtkListStore *store;
3943 GtkCellRenderer *renderer;
3945 store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT);
3946 dw->combo = gtk_combo_box_new_with_model(GTK_TREE_MODEL(store));
3947 g_object_unref(store);
3949 renderer = gtk_cell_renderer_text_new();
3950 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(dw->combo), renderer, TRUE);
3951 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(dw->combo), renderer,
3952 "text", DUPE_MENU_COLUMN_NAME, NULL);
3954 dupe_menu_add_item(store, _("Name"), DUPE_MATCH_NAME, dw);
3955 dupe_menu_add_item(store, _("Name case-insensitive"), DUPE_MATCH_NAME_CI, dw);
3956 dupe_menu_add_item(store, _("Size"), DUPE_MATCH_SIZE, dw);
3957 dupe_menu_add_item(store, _("Date"), DUPE_MATCH_DATE, dw);
3958 dupe_menu_add_item(store, _("Dimensions"), DUPE_MATCH_DIM, dw);
3959 dupe_menu_add_item(store, _("Checksum"), DUPE_MATCH_SUM, dw);
3960 dupe_menu_add_item(store, _("Path"), DUPE_MATCH_PATH, dw);
3961 dupe_menu_add_item(store, _("Similarity (high - 95)"), DUPE_MATCH_SIM_HIGH, dw);
3962 dupe_menu_add_item(store, _("Similarity (med. - 90)"), DUPE_MATCH_SIM_MED, dw);
3963 dupe_menu_add_item(store, _("Similarity (low - 85)"), DUPE_MATCH_SIM_LOW, dw);
3964 dupe_menu_add_item(store, _("Similarity (custom)"), DUPE_MATCH_SIM_CUSTOM, dw);
3965 dupe_menu_add_item(store, _("Name ≠content"), DUPE_MATCH_NAME_CONTENT, dw);
3966 dupe_menu_add_item(store, _("Name case-insensitive ≠content"), DUPE_MATCH_NAME_CI_CONTENT, dw);
3967 dupe_menu_add_item(store, _("Show all"), DUPE_MATCH_ALL, dw);
3969 g_signal_connect(G_OBJECT(dw->combo), "changed",
3970 G_CALLBACK(dupe_menu_type_cb), dw);
3974 *-------------------------------------------------------------------
3976 *-------------------------------------------------------------------
3979 /* this overrides the low default of a GtkCellRenderer from 100 to CELL_HEIGHT_OVERRIDE, something sane for our purposes */
3981 #define CELL_HEIGHT_OVERRIDE 512
3983 void cell_renderer_height_override(GtkCellRenderer *renderer)
3987 spec = g_object_class_find_property(G_OBJECT_GET_CLASS(G_OBJECT(renderer)), "height");
3988 if (spec && G_IS_PARAM_SPEC_INT(spec))
3990 GParamSpecInt *spec_int;
3992 spec_int = G_PARAM_SPEC_INT(spec);
3993 if (spec_int->maximum < CELL_HEIGHT_OVERRIDE) spec_int->maximum = CELL_HEIGHT_OVERRIDE;
3997 static GdkRGBA *dupe_listview_color_shifted(GtkWidget *widget)
3999 static GdkRGBA color;
4000 static GdkRGBA color_style;
4001 static GtkWidget *done = nullptr;
4007 style = gtk_widget_get_style(widget);
4008 convert_gdkcolor_to_gdkrgba(&style->base[GTK_STATE_NORMAL], &color_style);
4010 memcpy(&color, &color_style, sizeof(color));
4011 shift_color(&color, -1, 0);
4018 static void dupe_listview_color_cb(GtkTreeViewColumn *, GtkCellRenderer *cell,
4019 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
4021 auto dw = static_cast<DupeWindow *>(data);
4024 gtk_tree_model_get(tree_model, iter, DUPE_COLUMN_COLOR, &set, -1);
4025 g_object_set(G_OBJECT(cell),
4026 "cell-background-gdk", dupe_listview_color_shifted(dw->listview),
4027 "cell-background-set", set, NULL);
4030 static void dupe_listview_add_column(DupeWindow *dw, GtkWidget *listview, gint n, const gchar *title, gboolean image, gboolean right_justify)
4032 GtkTreeViewColumn *column;
4033 GtkCellRenderer *renderer;
4035 column = gtk_tree_view_column_new();
4036 gtk_tree_view_column_set_title(column, title);
4037 gtk_tree_view_column_set_min_width(column, 4);
4038 gtk_tree_view_column_set_sort_column_id(column, n);
4040 if (n != DUPE_COLUMN_RANK &&
4041 n != DUPE_COLUMN_THUMB)
4043 gtk_tree_view_column_set_resizable(column, TRUE);
4048 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_GROW_ONLY);
4049 renderer = gtk_cell_renderer_text_new();
4052 g_object_set(G_OBJECT(renderer), "xalign", 1.0, NULL);
4054 gtk_tree_view_column_pack_start(column, renderer, TRUE);
4055 gtk_tree_view_column_add_attribute(column, renderer, "text", n);
4059 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
4060 renderer = gtk_cell_renderer_pixbuf_new();
4061 cell_renderer_height_override(renderer);
4062 gtk_tree_view_column_pack_start(column, renderer, TRUE);
4063 gtk_tree_view_column_add_attribute(column, renderer, "pixbuf", n);
4066 if (listview == dw->listview)
4068 /* sets background before rendering */
4069 gtk_tree_view_column_set_cell_data_func(column, renderer, dupe_listview_color_cb, dw, nullptr);
4072 gtk_tree_view_append_column(GTK_TREE_VIEW(listview), column);
4075 static void dupe_listview_set_height(GtkWidget *listview, gboolean thumb)
4077 GtkTreeViewColumn *column;
4078 GtkCellRenderer *cell;
4081 column = gtk_tree_view_get_column(GTK_TREE_VIEW(listview), DUPE_COLUMN_THUMB - 1);
4082 if (!column) return;
4084 gtk_tree_view_column_set_fixed_width(column, (thumb) ? options->thumbnails.max_width : 4);
4085 gtk_tree_view_column_set_visible(column, thumb);
4087 list = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(column));
4089 cell = static_cast<GtkCellRenderer *>(list->data);
4092 g_object_set(G_OBJECT(cell), "height", (thumb) ? options->thumbnails.max_height : -1, NULL);
4093 gtk_tree_view_columns_autosize(GTK_TREE_VIEW(listview));
4096 static void dupe_listview_show_rank(GtkWidget *listview, gboolean rank)
4098 GtkTreeViewColumn *column;
4100 column = gtk_tree_view_get_column(GTK_TREE_VIEW(listview), DUPE_COLUMN_RANK - 1);
4101 if (!column) return;
4103 gtk_tree_view_column_set_visible(column, rank);
4107 *-------------------------------------------------------------------
4109 *-------------------------------------------------------------------
4112 static void dupe_window_show_thumb_cb(GtkWidget *widget, gpointer data)
4114 auto dw = static_cast<DupeWindow *>(data);
4116 dw->show_thumbs = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
4117 options->duplicates_thumbnails = dw->show_thumbs;
4119 if (dw->show_thumbs)
4121 if (!dw->working) dupe_thumb_step(dw);
4125 GtkTreeModel *store;
4129 thumb_loader_free(dw->thumb_loader);
4130 dw->thumb_loader = nullptr;
4132 store = gtk_tree_view_get_model(GTK_TREE_VIEW(dw->listview));
4133 valid = gtk_tree_model_get_iter_first(store, &iter);
4137 gtk_list_store_set(GTK_LIST_STORE(store), &iter, DUPE_COLUMN_THUMB, NULL, -1);
4138 valid = gtk_tree_model_iter_next(store, &iter);
4140 dupe_window_update_progress(dw, nullptr, 0.0, FALSE);
4143 dupe_listview_set_height(dw->listview, dw->show_thumbs);
4146 static void dupe_window_rotation_invariant_cb(GtkWidget *widget, gpointer data)
4148 auto dw = static_cast<DupeWindow *>(data);
4150 options->rot_invariant_sim = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
4151 dupe_window_recompare(dw);
4154 static void dupe_window_custom_threshold_cb(GtkWidget *widget, gpointer data)
4156 auto dw = static_cast<DupeWindow *>(data);
4157 DupeMatchType match_type;
4158 GtkTreeModel *store;
4162 options->duplicates_similarity_threshold = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget));
4163 dw->match_mask = DUPE_MATCH_SIM_CUSTOM;
4165 store = gtk_combo_box_get_model(GTK_COMBO_BOX(dw->combo));
4166 valid = gtk_tree_model_get_iter_first(store, &iter);
4169 gtk_tree_model_get(store, &iter, DUPE_MENU_COLUMN_MASK, &match_type, -1);
4170 if (match_type == DUPE_MATCH_SIM_CUSTOM)
4174 valid = gtk_tree_model_iter_next(store, &iter);
4177 gtk_combo_box_set_active_iter(GTK_COMBO_BOX(dw->combo), &iter);
4178 dupe_window_recompare(dw);
4181 static gboolean dupe_window_keypress_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
4183 auto dw = static_cast<DupeWindow *>(data);
4184 gboolean stop_signal = FALSE;
4186 GtkWidget *listview;
4187 GtkTreeModel *store;
4188 GtkTreeSelection *selection;
4190 DupeItem *di = nullptr;
4192 on_second = gtk_widget_has_focus(dw->second_listview);
4196 listview = dw->second_listview;
4200 listview = dw->listview;
4203 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(listview));
4204 slist = gtk_tree_selection_get_selected_rows(selection, &store);
4211 last = g_list_last(slist);
4212 tpath = static_cast<GtkTreePath *>(last->data);
4214 /* last is newest selected file */
4215 gtk_tree_model_get_iter(store, &iter, tpath);
4216 gtk_tree_model_get(store, &iter, DUPE_COLUMN_POINTER, &di, -1);
4218 g_list_free_full(slist, reinterpret_cast<GDestroyNotify>(gtk_tree_path_free));
4220 if (event->state & GDK_CONTROL_MASK)
4225 switch (event->keyval)
4239 file_util_copy(nullptr, dupe_listview_get_selection(dw, listview),
4240 nullptr, dw->window);
4243 file_util_move(nullptr, dupe_listview_get_selection(dw, listview),
4244 nullptr, dw->window);
4247 file_util_rename(nullptr, dupe_listview_get_selection(dw, listview), dw->window);
4250 options->file_ops.safe_delete_enable = TRUE;
4251 file_util_delete(nullptr, dupe_listview_get_selection(dw, listview), dw->window);
4254 stop_signal = FALSE;
4262 switch (event->keyval)
4265 if (event->state & GDK_SHIFT_MASK)
4267 gtk_tree_selection_unselect_all(selection);
4271 gtk_tree_selection_select_all(selection);
4274 case GDK_KEY_Delete: case GDK_KEY_KP_Delete:
4277 dupe_second_clear(dw);
4278 dupe_window_recompare(dw);
4282 dupe_window_clear(dw);
4286 dupe_window_append_file_list(dw, FALSE);
4289 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dw->button_thumbs),
4290 !gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dw->button_thumbs)));
4293 dupe_window_close(dw);
4296 stop_signal = FALSE;
4301 else if (event->state & GDK_SHIFT_MASK)
4304 switch (event->keyval)
4306 case GDK_KEY_Delete:
4307 case GDK_KEY_KP_Delete:
4308 options->file_ops.safe_delete_enable = FALSE;
4309 file_util_delete_notify_done(nullptr, dupe_listview_get_selection(dw, dw->listview), dw->window, delete_finished_cb, dw);
4312 stop_signal = FALSE;
4319 switch (event->keyval)
4321 case GDK_KEY_Return: case GDK_KEY_KP_Enter:
4322 dupe_menu_view(dw, di, listview, FALSE);
4325 dupe_menu_view(dw, di, listview, TRUE);
4327 case GDK_KEY_Delete: case GDK_KEY_KP_Delete:
4328 dupe_window_remove_selection(dw, listview);
4333 dupe_window_collection_from_selection(dw);
4337 options->duplicates_select_type = DUPE_SELECT_NONE;
4338 dupe_listview_select_dupes(dw, DUPE_SELECT_NONE);
4341 options->duplicates_select_type = DUPE_SELECT_GROUP1;
4342 dupe_listview_select_dupes(dw, DUPE_SELECT_GROUP1);
4345 options->duplicates_select_type = DUPE_SELECT_GROUP2;
4346 dupe_listview_select_dupes(dw, DUPE_SELECT_GROUP2);
4354 menu = dupe_menu_popup_main(dw, di);
4355 gtk_menu_popup_at_widget(GTK_MENU(menu), widget, GDK_GRAVITY_CENTER, GDK_GRAVITY_CENTER, nullptr);
4361 menu = dupe_menu_popup_second(dw, di);
4362 gtk_menu_popup_at_widget(GTK_MENU(menu), widget, GDK_GRAVITY_CENTER, GDK_GRAVITY_CENTER, nullptr);
4366 stop_signal = FALSE;
4370 if (!stop_signal && is_help_key(event))
4372 help_window_show("GuideImageSearchFindingDuplicates.html");
4380 void dupe_window_clear(DupeWindow *dw)
4382 GtkListStore *store;
4384 dupe_check_stop(dw);
4386 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(dw->listview)));
4387 gtk_list_store_clear(store);
4388 gtk_tree_view_columns_autosize(GTK_TREE_VIEW(dw->listview));
4390 g_list_free(dw->dupes);
4391 dw->dupes = nullptr;
4393 g_list_free_full(dw->list, reinterpret_cast<GDestroyNotify>(dupe_item_free));
4397 dupe_match_reset_list(dw->second_list);
4399 dupe_window_update_count(dw, FALSE);
4400 dupe_window_update_progress(dw, nullptr, 0.0, FALSE);
4403 static void dupe_window_get_geometry(DupeWindow *dw)
4406 LayoutWindow *lw = nullptr;
4410 if (!dw || !lw) return;
4412 window = gtk_widget_get_window(dw->window);
4413 gdk_window_get_position(window, &lw->options.dupe_window.x, &lw->options.dupe_window.y);
4414 lw->options.dupe_window.w = gdk_window_get_width(window);
4415 lw->options.dupe_window.h = gdk_window_get_height(window);
4418 void dupe_window_close(DupeWindow *dw)
4420 dupe_check_stop(dw);
4422 dupe_window_get_geometry(dw);
4424 dupe_window_list = g_list_remove(dupe_window_list, dw);
4425 gq_gtk_widget_destroy(dw->window);
4427 g_list_free(dw->dupes);
4428 g_list_free_full(dw->list, reinterpret_cast<GDestroyNotify>(dupe_item_free));
4430 g_list_free_full(dw->second_list, reinterpret_cast<GDestroyNotify>(dupe_item_free));
4432 file_data_unregister_notify_func(dupe_notify_cb, dw);
4434 g_thread_pool_free(dw->dupe_comparison_thread_pool, TRUE, TRUE);
4439 static gint dupe_window_close_cb(GtkWidget *, gpointer data)
4441 auto dw = static_cast<DupeWindow *>(data);
4443 dupe_window_close(dw);
4448 static gint dupe_window_delete(GtkWidget *, GdkEvent *, gpointer data)
4450 auto dw = static_cast<DupeWindow *>(data);
4451 dupe_window_close(dw);
4456 static void dupe_help_cb(GtkAction *, gpointer)
4458 help_window_show("GuideImageSearchFindingDuplicates.html");
4461 static gint default_sort_cb(GtkTreeModel *, GtkTreeIter *, GtkTreeIter *, gpointer)
4466 static gint column_sort_cb(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b, gpointer data)
4468 auto sortable = static_cast<GtkTreeSortable *>(data);
4470 gchar *rank_str_a, *rank_str_b;
4475 gint sort_column_id;
4476 GtkSortType sort_order;
4480 gtk_tree_sortable_get_sort_column_id(sortable, &sort_column_id, &sort_order);
4482 gtk_tree_model_get(model, a, DUPE_COLUMN_RANK, &rank_str_a, DUPE_COLUMN_SET, &group_a, DUPE_COLUMN_POINTER, &di_a, -1);
4484 gtk_tree_model_get(model, b, DUPE_COLUMN_RANK, &rank_str_b, DUPE_COLUMN_SET, &group_b, DUPE_COLUMN_POINTER, &di_b, -1);
4486 if (group_a == group_b)
4488 switch (sort_column_id)
4490 case DUPE_COLUMN_NAME:
4491 ret = utf8_compare(di_a->fd->name, di_b->fd->name, TRUE);
4493 case DUPE_COLUMN_SIZE:
4494 if (di_a->fd->size == di_b->fd->size)
4500 ret = (di_a->fd->size > di_b->fd->size) ? 1 : -1;
4503 case DUPE_COLUMN_DATE:
4504 if (di_a->fd->date == di_b->fd->date)
4510 ret = (di_a->fd->date > di_b->fd->date) ? 1 : -1;
4513 case DUPE_COLUMN_DIMENSIONS:
4514 if ((di_a->width == di_b->width) && (di_a->height == di_b->height))
4520 ret = ((di_a->width * di_a->height) > (di_b->width * di_b->height)) ? 1 : -1;
4523 case DUPE_COLUMN_RANK:
4524 rank_int_a = atoi(rank_str_a);
4525 rank_int_b = atoi(rank_str_b);
4526 if (rank_int_a == 0) rank_int_a = 101;
4527 if (rank_int_b == 0) rank_int_b = 101;
4529 if (rank_int_a == rank_int_b)
4535 ret = (rank_int_a > rank_int_b) ? 1 : -1;
4538 case DUPE_COLUMN_PATH:
4539 ret = utf8_compare(di_a->fd->path, di_b->fd->path, TRUE);
4543 else if (group_a < group_b)
4545 ret = (sort_order == GTK_SORT_ASCENDING) ? 1 : -1;
4549 ret = (sort_order == GTK_SORT_ASCENDING) ? -1 : 1;
4555 static void column_clicked_cb(GtkWidget *, gpointer data)
4557 auto dw = static_cast<DupeWindow *>(data);
4559 options->duplicates_match = DUPE_SELECT_NONE;
4560 dupe_listview_select_dupes(dw, DUPE_SELECT_NONE);
4563 /* collection and files can be NULL */
4564 DupeWindow *dupe_window_new()
4569 GtkWidget *scrolled;
4571 GtkWidget *status_box;
4572 GtkWidget *controls_box;
4573 GtkWidget *button_box;
4576 GtkListStore *store;
4577 GtkTreeSelection *selection;
4578 GdkGeometry geometry;
4579 LayoutWindow *lw = nullptr;
4583 dw = g_new0(DupeWindow, 1);
4584 dw->add_files_queue = nullptr;
4585 dw->add_files_queue_id = 0;
4587 dw->match_mask = DUPE_MATCH_NAME;
4588 if (options->duplicates_match == DUPE_MATCH_NAME) dw->match_mask = DUPE_MATCH_NAME;
4589 if (options->duplicates_match == DUPE_MATCH_SIZE) dw->match_mask = DUPE_MATCH_SIZE;
4590 if (options->duplicates_match == DUPE_MATCH_DATE) dw->match_mask = DUPE_MATCH_DATE;
4591 if (options->duplicates_match == DUPE_MATCH_DIM) dw->match_mask = DUPE_MATCH_DIM;
4592 if (options->duplicates_match == DUPE_MATCH_SUM) dw->match_mask = DUPE_MATCH_SUM;
4593 if (options->duplicates_match == DUPE_MATCH_PATH) dw->match_mask = DUPE_MATCH_PATH;
4594 if (options->duplicates_match == DUPE_MATCH_SIM_HIGH) dw->match_mask = DUPE_MATCH_SIM_HIGH;
4595 if (options->duplicates_match == DUPE_MATCH_SIM_MED) dw->match_mask = DUPE_MATCH_SIM_MED;
4596 if (options->duplicates_match == DUPE_MATCH_SIM_LOW) dw->match_mask = DUPE_MATCH_SIM_LOW;
4597 if (options->duplicates_match == DUPE_MATCH_SIM_CUSTOM) dw->match_mask = DUPE_MATCH_SIM_CUSTOM;
4598 if (options->duplicates_match == DUPE_MATCH_NAME_CI) dw->match_mask = DUPE_MATCH_NAME_CI;
4599 if (options->duplicates_match == DUPE_MATCH_NAME_CONTENT) dw->match_mask = DUPE_MATCH_NAME_CONTENT;
4600 if (options->duplicates_match == DUPE_MATCH_NAME_CI_CONTENT) dw->match_mask = DUPE_MATCH_NAME_CI_CONTENT;
4601 if (options->duplicates_match == DUPE_MATCH_ALL) dw->match_mask = DUPE_MATCH_ALL;
4603 dw->window = window_new("dupe", nullptr, nullptr, _("Find duplicates"));
4604 DEBUG_NAME(dw->window);
4606 geometry.min_width = DEFAULT_MINIMAL_WINDOW_SIZE;
4607 geometry.min_height = DEFAULT_MINIMAL_WINDOW_SIZE;
4608 geometry.base_width = DUPE_DEF_WIDTH;
4609 geometry.base_height = DUPE_DEF_HEIGHT;
4610 gtk_window_set_geometry_hints(GTK_WINDOW(dw->window), nullptr, &geometry,
4611 static_cast<GdkWindowHints>(GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE));
4613 if (lw && options->save_window_positions)
4615 gtk_window_set_default_size(GTK_WINDOW(dw->window), lw->options.dupe_window.w, lw->options.dupe_window.h);
4616 gtk_window_move(GTK_WINDOW(dw->window), lw->options.dupe_window.x, lw->options.dupe_window.y);
4620 gtk_window_set_default_size(GTK_WINDOW(dw->window), DUPE_DEF_WIDTH, DUPE_DEF_HEIGHT);
4623 gtk_window_set_resizable(GTK_WINDOW(dw->window), TRUE);
4624 gtk_container_set_border_width(GTK_CONTAINER(dw->window), 0);
4626 g_signal_connect(G_OBJECT(dw->window), "delete_event",
4627 G_CALLBACK(dupe_window_delete), dw);
4628 g_signal_connect(G_OBJECT(dw->window), "key_press_event",
4629 G_CALLBACK(dupe_window_keypress_cb), dw);
4631 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
4632 gq_gtk_container_add(GTK_WIDGET(dw->window), vbox);
4633 gtk_widget_show(vbox);
4635 dw->table = gtk_grid_new();
4636 gq_gtk_box_pack_start(GTK_BOX(vbox), dw->table, TRUE, TRUE, 0);
4637 gtk_grid_set_row_homogeneous(GTK_GRID(dw->table), TRUE);
4638 gtk_grid_set_column_homogeneous(GTK_GRID(dw->table), TRUE);
4639 gtk_widget_show(dw->table);
4641 scrolled = gq_gtk_scrolled_window_new(nullptr, nullptr);
4642 gq_gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
4643 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
4644 gtk_grid_attach(GTK_GRID(dw->table), scrolled, 0, 0, 2, 1);
4645 gtk_widget_show(scrolled);
4647 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);
4648 dw->listview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
4649 g_object_unref(store);
4651 dw->sortable = GTK_TREE_SORTABLE(store);
4653 gtk_tree_sortable_set_sort_func(dw->sortable, DUPE_COLUMN_RANK, column_sort_cb, dw->sortable, nullptr);
4654 gtk_tree_sortable_set_sort_func(dw->sortable, DUPE_COLUMN_SET, default_sort_cb, dw->sortable, nullptr);
4655 gtk_tree_sortable_set_sort_func(dw->sortable, DUPE_COLUMN_THUMB, default_sort_cb, dw->sortable, nullptr);
4656 gtk_tree_sortable_set_sort_func(dw->sortable, DUPE_COLUMN_NAME, column_sort_cb, dw->sortable, nullptr);
4657 gtk_tree_sortable_set_sort_func(dw->sortable, DUPE_COLUMN_SIZE, column_sort_cb, dw->sortable, nullptr);
4658 gtk_tree_sortable_set_sort_func(dw->sortable, DUPE_COLUMN_DATE, column_sort_cb, dw->sortable, nullptr);
4659 gtk_tree_sortable_set_sort_func(dw->sortable, DUPE_COLUMN_DIMENSIONS, column_sort_cb, dw->sortable, nullptr);
4660 gtk_tree_sortable_set_sort_func(dw->sortable, DUPE_COLUMN_PATH, column_sort_cb, dw->sortable, nullptr);
4662 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dw->listview));
4663 gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_MULTIPLE);
4664 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(dw->listview), TRUE);
4665 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(dw->listview), FALSE);
4667 dupe_listview_add_column(dw, dw->listview, DUPE_COLUMN_RANK, _("Rank"), FALSE, TRUE);
4668 dupe_listview_add_column(dw, dw->listview, DUPE_COLUMN_THUMB, _("Thumb"), TRUE, FALSE);
4669 dupe_listview_add_column(dw, dw->listview, DUPE_COLUMN_NAME, _("Name"), FALSE, FALSE);
4670 dupe_listview_add_column(dw, dw->listview, DUPE_COLUMN_SIZE, _("Size"), FALSE, TRUE);
4671 dupe_listview_add_column(dw, dw->listview, DUPE_COLUMN_DATE, _("Date"), FALSE, TRUE);
4672 dupe_listview_add_column(dw, dw->listview, DUPE_COLUMN_DIMENSIONS, _("Dimensions"), FALSE, FALSE);
4673 dupe_listview_add_column(dw, dw->listview, DUPE_COLUMN_PATH, _("Path"), FALSE, FALSE);
4674 dupe_listview_add_column(dw, dw->listview, DUPE_COLUMN_SET, _("Set"), FALSE, FALSE);
4676 g_signal_connect(gtk_tree_view_get_column(GTK_TREE_VIEW(dw->listview), DUPE_COLUMN_RANK - 1), "clicked", (GCallback)column_clicked_cb, dw);
4677 g_signal_connect(gtk_tree_view_get_column(GTK_TREE_VIEW(dw->listview), DUPE_COLUMN_NAME - 1), "clicked", (GCallback)column_clicked_cb, dw);
4678 g_signal_connect(gtk_tree_view_get_column(GTK_TREE_VIEW(dw->listview), DUPE_COLUMN_SIZE - 1), "clicked", (GCallback)column_clicked_cb, dw);
4679 g_signal_connect(gtk_tree_view_get_column(GTK_TREE_VIEW(dw->listview), DUPE_COLUMN_DATE - 1), "clicked", (GCallback)column_clicked_cb, dw);
4680 g_signal_connect(gtk_tree_view_get_column(GTK_TREE_VIEW(dw->listview), DUPE_COLUMN_DIMENSIONS - 1), "clicked", (GCallback)column_clicked_cb, dw);
4681 g_signal_connect(gtk_tree_view_get_column(GTK_TREE_VIEW(dw->listview), DUPE_COLUMN_PATH - 1), "clicked", (GCallback)column_clicked_cb, dw);
4683 gq_gtk_container_add(GTK_WIDGET(scrolled), dw->listview);
4684 gtk_widget_show(dw->listview);
4686 dw->second_vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
4687 gtk_grid_attach(GTK_GRID(dw->table), dw->second_vbox, 2, 0, 3, 1);
4690 gtk_grid_set_column_spacing(GTK_GRID(dw->table), PREF_PAD_GAP);
4691 gtk_widget_show(dw->second_vbox);
4695 gtk_grid_set_column_spacing(GTK_GRID(dw->table), 0);
4698 scrolled = gq_gtk_scrolled_window_new(nullptr, nullptr);
4699 gq_gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
4700 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
4701 gq_gtk_box_pack_start(GTK_BOX(dw->second_vbox), scrolled, TRUE, TRUE, 0);
4702 gtk_widget_show(scrolled);
4704 store = gtk_list_store_new(2, G_TYPE_POINTER, G_TYPE_STRING);
4705 dw->second_listview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
4707 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dw->second_listview));
4708 gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_MULTIPLE);
4710 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(dw->second_listview), TRUE);
4711 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(dw->second_listview), FALSE);
4713 dupe_listview_add_column(dw, dw->second_listview, 1, _("Compare to:"), FALSE, FALSE);
4715 gq_gtk_container_add(GTK_WIDGET(scrolled), dw->second_listview);
4716 gtk_widget_show(dw->second_listview);
4718 dw->second_status_label = gtk_label_new("");
4719 gq_gtk_box_pack_start(GTK_BOX(dw->second_vbox), dw->second_status_label, FALSE, FALSE, 0);
4720 gtk_widget_show(dw->second_status_label);
4722 pref_line(dw->second_vbox, GTK_ORIENTATION_HORIZONTAL);
4724 status_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
4725 gq_gtk_box_pack_start(GTK_BOX(vbox), status_box, FALSE, FALSE, 0);
4726 gtk_widget_show(status_box);
4728 frame = gtk_frame_new(nullptr);
4730 gq_gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
4731 gq_gtk_box_pack_start(GTK_BOX(status_box), frame, TRUE, TRUE, 0);
4732 gtk_widget_show(frame);
4734 dw->status_label = gtk_label_new("");
4735 gq_gtk_container_add(GTK_WIDGET(frame), dw->status_label);
4736 gtk_widget_show(dw->status_label);
4738 dw->extra_label = gtk_progress_bar_new();
4739 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(dw->extra_label), 0.0);
4740 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(dw->extra_label), "");
4741 gtk_progress_bar_set_show_text(GTK_PROGRESS_BAR(dw->extra_label), TRUE);
4742 gq_gtk_box_pack_start(GTK_BOX(status_box), dw->extra_label, FALSE, FALSE, PREF_PAD_SPACE);
4743 gtk_widget_show(dw->extra_label);
4745 controls_box = pref_box_new(vbox, FALSE, GTK_ORIENTATION_HORIZONTAL, 0);
4746 dw->controls_box = controls_box;
4748 dw->button_thumbs = gtk_check_button_new_with_label(_("Thumbnails"));
4749 gtk_widget_set_tooltip_text(GTK_WIDGET(dw->button_thumbs), "Ctrl-T");
4750 dw->show_thumbs = options->duplicates_thumbnails;
4751 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dw->button_thumbs), dw->show_thumbs);
4752 g_signal_connect(G_OBJECT(dw->button_thumbs), "toggled",
4753 G_CALLBACK(dupe_window_show_thumb_cb), dw);
4754 gq_gtk_box_pack_start(GTK_BOX(controls_box), dw->button_thumbs, FALSE, FALSE, PREF_PAD_SPACE);
4755 gtk_widget_show(dw->button_thumbs);
4757 label = gtk_label_new(_("Compare by:"));
4758 gq_gtk_box_pack_start(GTK_BOX(controls_box), label, FALSE, FALSE, PREF_PAD_SPACE);
4759 gtk_widget_show(label);
4761 dupe_menu_setup(dw);
4762 gq_gtk_box_pack_start(GTK_BOX(controls_box), dw->combo, FALSE, FALSE, 0);
4763 gtk_widget_show(dw->combo);
4765 label = gtk_label_new(_("Custom Threshold"));
4766 gq_gtk_box_pack_start(GTK_BOX(controls_box), label, FALSE, FALSE, PREF_PAD_SPACE);
4767 gtk_widget_show(label);
4768 dw->custom_threshold = gtk_spin_button_new_with_range(1, 100, 1);
4769 gtk_widget_set_tooltip_text(GTK_WIDGET(dw->custom_threshold), "Custom similarity threshold\n(Use tab key to set value)");
4770 gtk_spin_button_set_value(GTK_SPIN_BUTTON(dw->custom_threshold), options->duplicates_similarity_threshold);
4771 g_signal_connect(G_OBJECT(dw->custom_threshold), "value_changed", G_CALLBACK(dupe_window_custom_threshold_cb), dw);
4772 gq_gtk_box_pack_start(GTK_BOX(controls_box), dw->custom_threshold, FALSE, FALSE, PREF_PAD_SPACE);
4773 gtk_widget_show(dw->custom_threshold);
4775 button = gtk_check_button_new_with_label(_("Sort"));
4776 gtk_widget_set_tooltip_text(GTK_WIDGET(button), "Sort by group totals");
4777 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), options->sort_totals);
4778 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(dupe_sort_totals_toggle_cb), dw);
4779 gq_gtk_box_pack_start(GTK_BOX(controls_box), button, FALSE, FALSE, PREF_PAD_SPACE);
4780 gtk_widget_show(button);
4782 dw->button_rotation_invariant = gtk_check_button_new_with_label(_("Ignore Orientation"));
4783 gtk_widget_set_tooltip_text(GTK_WIDGET(dw->button_rotation_invariant), "Ignore image orientation");
4784 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dw->button_rotation_invariant), options->rot_invariant_sim);
4785 g_signal_connect(G_OBJECT(dw->button_rotation_invariant), "toggled",
4786 G_CALLBACK(dupe_window_rotation_invariant_cb), dw);
4787 gq_gtk_box_pack_start(GTK_BOX(controls_box), dw->button_rotation_invariant, FALSE, FALSE, PREF_PAD_SPACE);
4788 gtk_widget_show(dw->button_rotation_invariant);
4790 button = gtk_check_button_new_with_label(_("Compare two file sets"));
4791 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), dw->second_set);
4792 g_signal_connect(G_OBJECT(button), "toggled",
4793 G_CALLBACK(dupe_second_set_toggle_cb), dw);
4794 gq_gtk_box_pack_start(GTK_BOX(controls_box), button, FALSE, FALSE, PREF_PAD_SPACE);
4795 gtk_widget_show(button);
4797 button_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
4798 gq_gtk_box_pack_start(GTK_BOX(vbox), button_box, FALSE, FALSE, 0);
4799 gtk_widget_show(button_box);
4801 hbox = gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL);
4802 gtk_button_box_set_layout(GTK_BUTTON_BOX(hbox), GTK_BUTTONBOX_END);
4803 gtk_box_set_spacing(GTK_BOX(hbox), PREF_PAD_SPACE);
4804 gq_gtk_box_pack_end(GTK_BOX(button_box), hbox, FALSE, FALSE, 0);
4805 gtk_widget_show(hbox);
4807 button = pref_button_new(nullptr, GQ_ICON_HELP, _("Help"), G_CALLBACK(dupe_help_cb), nullptr);
4808 gtk_widget_set_tooltip_text(GTK_WIDGET(button), "F1");
4809 gq_gtk_container_add(GTK_WIDGET(hbox), button);
4810 gtk_widget_set_can_default(button, TRUE);
4811 gtk_widget_show(button);
4813 button = pref_button_new(nullptr, GQ_ICON_STOP, _("Stop"), G_CALLBACK(dupe_check_stop_cb), dw);
4814 gq_gtk_container_add(GTK_WIDGET(hbox), button);
4815 gtk_widget_set_can_default(button, TRUE);
4816 gtk_widget_show(button);
4818 button = pref_button_new(nullptr, GQ_ICON_CLOSE, _("Close"), G_CALLBACK(dupe_window_close_cb), dw);
4819 gtk_widget_set_tooltip_text(GTK_WIDGET(button), "Ctrl-W");
4820 gq_gtk_container_add(GTK_WIDGET(hbox), button);
4821 gtk_widget_set_can_default(button, TRUE);
4822 gtk_widget_grab_default(button);
4823 gtk_widget_show(button);
4826 /* order is important here, dnd_init should be seeing mouse
4827 * presses before we possibly handle (and stop) the signal
4829 g_signal_connect(G_OBJECT(dw->listview), "button_press_event",
4830 G_CALLBACK(dupe_listview_press_cb), dw);
4831 g_signal_connect(G_OBJECT(dw->listview), "button_release_event",
4832 G_CALLBACK(dupe_listview_release_cb), dw);
4833 g_signal_connect(G_OBJECT(dw->second_listview), "button_press_event",
4834 G_CALLBACK(dupe_listview_press_cb), dw);
4835 g_signal_connect(G_OBJECT(dw->second_listview), "button_release_event",
4836 G_CALLBACK(dupe_listview_release_cb), dw);
4838 gtk_widget_show(dw->window);
4840 dupe_listview_set_height(dw->listview, dw->show_thumbs);
4841 g_signal_emit_by_name(G_OBJECT(dw->combo), "changed");
4843 dupe_window_update_count(dw, TRUE);
4844 dupe_window_update_progress(dw, nullptr, 0.0, FALSE);
4846 dupe_window_list = g_list_append(dupe_window_list, dw);
4848 file_data_register_notify_func(dupe_notify_cb, dw, NOTIFY_PRIORITY_MEDIUM);
4850 g_mutex_init(&dw->thread_count_mutex);
4851 g_mutex_init(&dw->search_matches_mutex);
4852 dw->dupe_comparison_thread_pool = g_thread_pool_new(dupe_comparison_func, dw, options->threads.duplicates, FALSE, nullptr);
4858 *-------------------------------------------------------------------
4860 *-------------------------------------------------------------------
4863 struct CDupeConfirmD {
4868 static void confirm_dir_list_cancel(GtkWidget *, gpointer)
4873 static void confirm_dir_list_add(GtkWidget *, gpointer data)
4875 auto d = static_cast<CDupeConfirmD *>(data);
4878 dupe_window_add_files(d->dw, d->list, FALSE);
4883 auto fd = static_cast<FileData *>(work->data);
4885 if (isdir(fd->path))
4889 filelist_read(fd, &list, nullptr);
4890 list = filelist_filter(list, FALSE);
4893 dupe_window_add_files(d->dw, list, FALSE);
4894 filelist_free(list);
4900 static void confirm_dir_list_recurse(GtkWidget *, gpointer data)
4902 auto d = static_cast<CDupeConfirmD *>(data);
4903 dupe_window_add_files(d->dw, d->list, TRUE);
4906 static void confirm_dir_list_skip(GtkWidget *, gpointer data)
4908 auto d = static_cast<CDupeConfirmD *>(data);
4909 dupe_window_add_files(d->dw, d->list, FALSE);
4912 static void confirm_dir_list_destroy(GtkWidget *, gpointer data)
4914 auto d = static_cast<CDupeConfirmD *>(data);
4915 filelist_free(d->list);
4919 static GtkWidget *dupe_confirm_dir_list(DupeWindow *dw, GList *list)
4924 d = g_new0(CDupeConfirmD, 1);
4928 menu = popup_menu_short_lived();
4929 g_signal_connect(G_OBJECT(menu), "destroy",
4930 G_CALLBACK(confirm_dir_list_destroy), d);
4932 menu_item_add_stock(menu, _("Dropped list includes folders."), GQ_ICON_DND, nullptr, nullptr);
4933 menu_item_add_divider(menu);
4934 menu_item_add_icon(menu, _("_Add contents"), GQ_ICON_OK, G_CALLBACK(confirm_dir_list_add), d);
4935 menu_item_add_icon(menu, _("Add contents _recursive"), GQ_ICON_ADD, G_CALLBACK(confirm_dir_list_recurse), d);
4936 menu_item_add_icon(menu, _("_Skip folders"), GQ_ICON_REMOVE, G_CALLBACK(confirm_dir_list_skip), d);
4937 menu_item_add_divider(menu);
4938 menu_item_add_icon(menu, _("Cancel"), GQ_ICON_CANCEL, G_CALLBACK(confirm_dir_list_cancel), d);
4944 *-------------------------------------------------------------------
4946 *-------------------------------------------------------------------
4949 static GtkTargetEntry dupe_drag_types[] = {
4950 { const_cast<gchar *>("text/uri-list"), 0, TARGET_URI_LIST },
4951 { const_cast<gchar *>("text/plain"), 0, TARGET_TEXT_PLAIN }
4953 static gint n_dupe_drag_types = 2;
4955 static GtkTargetEntry dupe_drop_types[] = {
4956 { const_cast<gchar *>(TARGET_APP_COLLECTION_MEMBER_STRING), 0, TARGET_APP_COLLECTION_MEMBER },
4957 { const_cast<gchar *>("text/uri-list"), 0, TARGET_URI_LIST }
4959 static gint n_dupe_drop_types = 2;
4961 static void dupe_dnd_data_set(GtkWidget *widget, GdkDragContext *,
4962 GtkSelectionData *selection_data, guint info,
4963 guint, gpointer data)
4965 auto dw = static_cast<DupeWindow *>(data);
4970 case TARGET_URI_LIST:
4971 case TARGET_TEXT_PLAIN:
4972 list = dupe_listview_get_selection(dw, widget);
4974 uri_selection_data_set_uris_from_filelist(selection_data, list);
4975 filelist_free(list);
4982 static void dupe_dnd_data_get(GtkWidget *widget, GdkDragContext *context,
4984 GtkSelectionData *selection_data, guint info,
4985 guint, gpointer data)
4987 auto dw = static_cast<DupeWindow *>(data);
4989 GList *list = nullptr;
4992 if (dw->add_files_queue_id > 0)
4994 warning_dialog(_("Find duplicates"), _("Please wait for the current file selection to be loaded."), GQ_ICON_DIALOG_INFO, dw->window);
4999 source = gtk_drag_get_source_widget(context);
5000 if (source == dw->listview || source == dw->second_listview) return;
5002 dw->second_drop = (dw->second_set && widget == dw->second_listview);
5006 case TARGET_APP_COLLECTION_MEMBER:
5007 collection_from_dnd_data(reinterpret_cast<const gchar *>(gtk_selection_data_get_data(selection_data)), &list, nullptr);
5009 case TARGET_URI_LIST:
5010 list = uri_filelist_from_gtk_selection_data(selection_data);
5014 auto fd = static_cast<FileData *>(work->data);
5015 if (isdir(fd->path))
5018 menu = dupe_confirm_dir_list(dw, list);
5019 gtk_menu_popup_at_pointer(GTK_MENU(menu), nullptr);
5032 dupe_window_add_files(dw, list, FALSE);
5033 filelist_free(list);
5037 static void dupe_dest_set(GtkWidget *widget, gboolean enable)
5041 gtk_drag_dest_set(widget,
5042 static_cast<GtkDestDefaults>(GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_HIGHLIGHT | GTK_DEST_DEFAULT_DROP),
5043 dupe_drop_types, n_dupe_drop_types,
5044 static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_ASK));
5049 gtk_drag_dest_unset(widget);
5053 static void dupe_dnd_begin(GtkWidget *widget, GdkDragContext *context, gpointer data)
5055 auto dw = static_cast<DupeWindow *>(data);
5056 dupe_dest_set(dw->listview, FALSE);
5057 dupe_dest_set(dw->second_listview, FALSE);
5059 if (dw->click_item && !dupe_listview_item_is_selected(dw, dw->click_item, widget))
5061 GtkListStore *store;
5064 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(widget)));
5065 if (dupe_listview_find_item(store, dw->click_item, &iter) >= 0)
5067 GtkTreeSelection *selection;
5070 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
5071 gtk_tree_selection_unselect_all(selection);
5072 gtk_tree_selection_select_iter(selection, &iter);
5074 tpath = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &iter);
5075 gtk_tree_view_set_cursor(GTK_TREE_VIEW(widget), tpath, nullptr, FALSE);
5076 gtk_tree_path_free(tpath);
5080 if (dw->show_thumbs &&
5081 widget == dw->listview &&
5082 dw->click_item && dw->click_item->pixbuf)
5084 GtkTreeSelection *selection;
5087 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
5088 items = gtk_tree_selection_count_selected_rows(selection);
5089 dnd_set_drag_icon(widget, context, dw->click_item->pixbuf, items);
5093 static void dupe_dnd_end(GtkWidget *, GdkDragContext *, gpointer data)
5095 auto dw = static_cast<DupeWindow *>(data);
5096 dupe_dest_set(dw->listview, TRUE);
5097 dupe_dest_set(dw->second_listview, TRUE);
5100 static void dupe_dnd_init(DupeWindow *dw)
5102 gtk_drag_source_set(dw->listview, static_cast<GdkModifierType>(GDK_BUTTON1_MASK | GDK_BUTTON2_MASK),
5103 dupe_drag_types, n_dupe_drag_types,
5104 static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK));
5105 g_signal_connect(G_OBJECT(dw->listview), "drag_data_get",
5106 G_CALLBACK(dupe_dnd_data_set), dw);
5107 g_signal_connect(G_OBJECT(dw->listview), "drag_begin",
5108 G_CALLBACK(dupe_dnd_begin), dw);
5109 g_signal_connect(G_OBJECT(dw->listview), "drag_end",
5110 G_CALLBACK(dupe_dnd_end), dw);
5112 dupe_dest_set(dw->listview, TRUE);
5113 g_signal_connect(G_OBJECT(dw->listview), "drag_data_received",
5114 G_CALLBACK(dupe_dnd_data_get), dw);
5116 gtk_drag_source_set(dw->second_listview, static_cast<GdkModifierType>(GDK_BUTTON1_MASK | GDK_BUTTON2_MASK),
5117 dupe_drag_types, n_dupe_drag_types,
5118 static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK));
5119 g_signal_connect(G_OBJECT(dw->second_listview), "drag_data_get",
5120 G_CALLBACK(dupe_dnd_data_set), dw);
5121 g_signal_connect(G_OBJECT(dw->second_listview), "drag_begin",
5122 G_CALLBACK(dupe_dnd_begin), dw);
5123 g_signal_connect(G_OBJECT(dw->second_listview), "drag_end",
5124 G_CALLBACK(dupe_dnd_end), dw);
5126 dupe_dest_set(dw->second_listview, TRUE);
5127 g_signal_connect(G_OBJECT(dw->second_listview), "drag_data_received",
5128 G_CALLBACK(dupe_dnd_data_get), dw);
5132 *-------------------------------------------------------------------
5133 * maintenance (move, delete, etc.)
5134 *-------------------------------------------------------------------
5137 static void dupe_notify_cb(FileData *fd, NotifyType type, gpointer data)
5139 auto dw = static_cast<DupeWindow *>(data);
5141 if (!(type & NOTIFY_CHANGE) || !fd->change) return;
5143 DEBUG_1("Notify dupe: %s %04x", fd->path, type);
5145 switch (fd->change->type)
5147 case FILEDATA_CHANGE_MOVE:
5148 case FILEDATA_CHANGE_RENAME:
5149 dupe_item_update_fd(dw, fd);
5151 case FILEDATA_CHANGE_COPY:
5153 case FILEDATA_CHANGE_DELETE:
5154 /* Update the UI only once, after the operation finishes */
5156 case FILEDATA_CHANGE_UNSPECIFIED:
5157 case FILEDATA_CHANGE_WRITE_METADATA:
5164 * @brief Refresh window after a file delete operation
5165 * @param success (ud->phase != UTILITY_PHASE_CANCEL) #file_util_dialog_run
5166 * @param dest_path Not used
5167 * @param data #DupeWindow
5169 * If the window is refreshed after each file of a large set is deleted,
5170 * the UI slows to an unacceptable level. The #FileUtilDoneFunc is used
5171 * to call this function once, when the entire delete operation is completed.
5173 static void delete_finished_cb(gboolean success, const gchar *, gpointer data)
5175 auto dw = static_cast<DupeWindow *>(data);
5182 dupe_window_remove_selection(dw, dw->listview);
5186 *-------------------------------------------------------------------
5187 * Export duplicates data
5188 *-------------------------------------------------------------------
5191 enum SeparatorType {
5196 struct ExportDupesData
5199 SeparatorType separator;
5200 DupeWindow *dupewindow;
5203 static void export_duplicates_close(ExportDupesData *edd)
5205 if (edd->dialog) file_dialog_close(edd->dialog);
5206 edd->dialog = nullptr;
5209 static void export_duplicates_data_cancel_cb(FileDialog *, gpointer data)
5211 auto edd = static_cast<ExportDupesData *>(data);
5213 export_duplicates_close(edd);
5216 static void export_duplicates_data_save_cb(FileDialog *fdlg, gpointer data)
5218 auto edd = static_cast<ExportDupesData *>(data);
5219 GError *error = nullptr;
5220 GtkTreeModel *store;
5223 GFileOutputStream *gfstream;
5225 GString *output_string;
5228 GtkTreeSelection *selection;
5233 gboolean color_old = FALSE;
5234 gboolean color_new = FALSE;
5238 history_list_add_to_key("export_duplicates", fdlg->dest_path, -1);
5240 out_file = g_file_new_for_path(fdlg->dest_path);
5242 gfstream = g_file_replace(out_file, nullptr, TRUE, G_FILE_CREATE_NONE, nullptr, &error);
5245 log_printf(_("Error creating Export duplicates data file: Error: %s\n"), error->message);
5246 g_error_free(error);
5250 const gchar *sep = (edd->separator == EXPORT_CSV) ? "," : "\t";
5251 output_string = g_string_new(g_strjoin(sep, _("Match"), _("Group"), _("Similarity"), _("Set"), _("Thumbnail"), _("Name"), _("Size"), _("Date"), _("Width"), _("Height"), _("Path\n"), NULL));
5253 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(edd->dupewindow->listview));
5254 slist = gtk_tree_selection_get_selected_rows(selection, &store);
5257 tpath = static_cast<GtkTreePath *>(work->data);
5258 gtk_tree_model_get_iter(store, &iter, tpath);
5259 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, DUPE_COLUMN_COLOR, &color_new, -1);
5260 color_old = !color_new;
5265 tpath = static_cast<GtkTreePath *>(work->data);
5266 gtk_tree_model_get_iter(store, &iter, tpath);
5268 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, DUPE_COLUMN_POINTER, &di, -1);
5270 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, DUPE_COLUMN_COLOR, &color_new, -1);
5271 if (color_new != color_old)
5275 color_old = color_new;
5276 g_string_append_printf(output_string, "%d", match_count);
5277 output_string = g_string_append(output_string, sep);
5279 if ((dupe_match_find_parent(edd->dupewindow, di) == di))
5281 output_string = g_string_append(output_string, "1");
5285 output_string = g_string_append(output_string, "2");
5287 output_string = g_string_append(output_string, sep);
5289 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, DUPE_COLUMN_RANK, &rank, -1);
5290 rank_split = g_strsplit_set(rank, " [(", -1);
5291 if (rank_split[0] == nullptr)
5293 output_string = g_string_append(output_string, "");
5297 output_string = g_string_append(output_string, rank_split[0]);
5299 output_string = g_string_append(output_string, sep);
5301 g_strfreev(rank_split);
5303 g_string_append_printf(output_string, "%d", di->second + 1);
5304 output_string = g_string_append(output_string, sep);
5306 thumb_cache = cache_find_location(CACHE_TYPE_THUMB, di->fd->path);
5309 output_string = g_string_append(output_string, thumb_cache);
5310 g_free(thumb_cache);
5314 output_string = g_string_append(output_string, "");
5316 output_string = g_string_append(output_string, sep);
5318 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, DUPE_COLUMN_NAME, &name, -1);
5319 output_string = g_string_append(output_string, name);
5320 output_string = g_string_append(output_string, sep);
5323 g_string_append_printf(output_string, "%" PRIu64, di->fd->size);
5324 output_string = g_string_append(output_string, sep);
5325 output_string = g_string_append(output_string, text_from_time(di->fd->date));
5326 output_string = g_string_append(output_string, sep);
5327 g_string_append_printf(output_string, "%d", di->width);
5328 output_string = g_string_append(output_string, sep);
5329 g_string_append_printf(output_string, "%d", di->height);
5330 output_string = g_string_append(output_string, sep);
5331 output_string = g_string_append(output_string, di->fd->path);
5332 output_string = g_string_append_c(output_string, '\n');
5337 g_output_stream_write(G_OUTPUT_STREAM(gfstream), output_string->str, output_string->len, nullptr, &error);
5339 g_string_free(output_string, TRUE);
5340 g_object_unref(gfstream);
5341 g_object_unref(out_file);
5343 export_duplicates_close(edd);
5346 static void pop_menu_export(GList *, gpointer dupe_window, gpointer data)
5348 const gint index = GPOINTER_TO_INT(data);
5349 auto dw = static_cast<DupeWindow *>(dupe_window);
5350 const gchar *title = "Export duplicates data";
5351 const gchar *default_path = "/tmp/";
5352 gchar *file_extension;
5353 ExportDupesData *edd;
5354 const gchar *previous_path;
5356 edd = g_new0(ExportDupesData, 1);
5357 edd->dialog = file_util_file_dlg(title, "export_duplicates", nullptr, export_duplicates_data_cancel_cb, edd);
5362 edd->separator = EXPORT_CSV;
5363 file_extension = g_strdup(".csv");
5366 edd->separator = EXPORT_TSV;
5367 file_extension = g_strdup(".tsv");
5373 generic_dialog_add_message(GENERIC_DIALOG(edd->dialog), nullptr, title, nullptr, FALSE);
5374 file_dialog_add_button(edd->dialog, GQ_ICON_SAVE, _("Save"), export_duplicates_data_save_cb, TRUE);
5376 previous_path = history_list_find_last_path_by_key("export_duplicates");
5378 file_dialog_add_path_widgets(edd->dialog, default_path, previous_path, "export_duplicates", file_extension, _("Export Files"));
5380 edd->dupewindow = dw;
5382 gtk_widget_show(GENERIC_DIALOG(edd->dialog)->dialog);
5384 g_free(file_extension);
5387 static void dupe_pop_menu_export_cb(GtkWidget *widget, gpointer data)
5390 GList *selection_list;
5392 dw = static_cast<DupeWindow *>(submenu_item_get_data(widget));
5393 selection_list = dupe_listview_get_selection(dw, dw->listview);
5394 pop_menu_export(selection_list, dw, data);
5396 filelist_free(selection_list);
5399 static GtkWidget *submenu_add_export(GtkWidget *menu, GtkWidget **menu_item, GCallback func, gpointer data)
5404 item = menu_item_add(menu, _("_Export"), nullptr, nullptr);
5406 submenu = gtk_menu_new();
5407 g_object_set_data(G_OBJECT(submenu), "submenu_data", data);
5409 menu_item_add_icon_sensitive(submenu, _("Export to csv"),
5410 GQ_ICON_EXPORT, TRUE, G_CALLBACK(func), GINT_TO_POINTER(0));
5411 menu_item_add_icon_sensitive(submenu, _("Export to tab-delimited"),
5412 GQ_ICON_EXPORT, TRUE, G_CALLBACK(func), GINT_TO_POINTER(1));
5414 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
5415 if (menu_item) *menu_item = item;
5420 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */