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 */
79 typedef struct _DupeQueueItem DupeQueueItem;
80 /** Used for similarity checks. One for each item pushed
81 * onto the thread pool.
87 GList *work; /**< pointer into \a dw->list or \a dw->second_list (#DupeItem) */
88 gint index; /**< The order items pushed onto thread pool. Used to sort returned matches */
91 typedef struct _DupeSearchMatch DupeSearchMatch;
92 /** Used for similarity checks thread. One for each pair match found.
94 struct _DupeSearchMatch
96 DupeItem *a; /**< \a a / \a b matched pair found */
97 DupeItem *b; /**< \a a / \a b matched pair found */
99 gint index; /**< The order items pushed onto thread pool. Used to sort returned matches */
102 static DupeMatchType param_match_mask;
103 static GList *dupe_window_list = NULL; /**< list of open DupeWindow *s */
106 * Well, after adding the 'compare two sets' option things got a little sloppy in here
107 * because we have to account for two 'modes' everywhere. (be careful).
110 static void dupe_match_unlink(DupeItem *a, DupeItem *b);
111 static DupeItem *dupe_match_find_parent(DupeWindow *dw, DupeItem *child);
113 static gint dupe_match(DupeItem *a, DupeItem *b, DupeMatchType mask, gdouble *rank, gint fast);
115 static void dupe_thumb_step(DupeWindow *dw);
116 static gint dupe_check_cb(gpointer data);
118 static void dupe_second_add(DupeWindow *dw, DupeItem *di);
119 static void dupe_second_remove(DupeWindow *dw, DupeItem *di);
120 static GtkWidget *dupe_menu_popup_second(DupeWindow *dw, DupeItem *di);
122 static void dupe_dnd_init(DupeWindow *dw);
124 static void dupe_notify_cb(FileData *fd, NotifyType type, gpointer data);
125 static void delete_finished_cb(gboolean success, const gchar *dest_path, gpointer data);
127 static GtkWidget *submenu_add_export(GtkWidget *menu, GtkWidget **menu_item, GCallback func, gpointer data);
128 static void dupe_pop_menu_export_cb(GtkWidget *widget, gpointer data);
130 static void dupe_init_list_cache(DupeWindow *dw);
131 static void dupe_destroy_list_cache(DupeWindow *dw);
132 static gboolean dupe_insert_in_list_cache(DupeWindow *dw, FileData *fd);
134 static void dupe_match_link(DupeItem *a, DupeItem *b, gdouble rank);
135 static gint dupe_match_link_exists(DupeItem *child, DupeItem *parent);
138 * This array must be kept in sync with the contents of:\n
139 * @link dupe_window_keypress_cb() @endlink \n
140 * @link dupe_menu_popup_main() @endlink
142 * See also @link hard_coded_window_keys @endlink
144 hard_coded_window_keys dupe_window_keys[] = {
145 {GDK_CONTROL_MASK, 'C', N_("Copy")},
146 {GDK_CONTROL_MASK, 'M', N_("Move")},
147 {GDK_CONTROL_MASK, 'R', N_("Rename")},
148 {GDK_CONTROL_MASK, 'D', N_("Move to Trash")},
149 {GDK_SHIFT_MASK, GDK_KEY_Delete, N_("Delete")},
150 {static_cast<GdkModifierType>(0), GDK_KEY_Delete, N_("Remove")},
151 {GDK_CONTROL_MASK, GDK_KEY_Delete, N_("Clear")},
152 {GDK_CONTROL_MASK, 'A', N_("Select all")},
153 {static_cast<GdkModifierType>(GDK_CONTROL_MASK + GDK_SHIFT_MASK), 'A', N_("Select none")},
154 {GDK_CONTROL_MASK, 'T', N_("Toggle thumbs")},
155 {GDK_CONTROL_MASK, 'W', N_("Close window")},
156 {static_cast<GdkModifierType>(0), GDK_KEY_Return, N_("View")},
157 {static_cast<GdkModifierType>(0), 'V', N_("View in new window")},
158 {static_cast<GdkModifierType>(0), 'C', N_("Collection from selection")},
159 {GDK_CONTROL_MASK, 'L', N_("Append list")},
160 {static_cast<GdkModifierType>(0), '0', N_("Select none")},
161 {static_cast<GdkModifierType>(0), '1', N_("Select group 1 duplicates")},
162 {static_cast<GdkModifierType>(0), '2', N_("Select group 2 duplicates")},
163 {static_cast<GdkModifierType>(0), 0, NULL}
167 * @brief The function run in threads for similarity checks
168 * @param d1 #DupeQueueItem
169 * @param d2 #DupeWindow
171 * Used only for similarity checks.\n
172 * Search \a dqi->list for \a dqi->needle and if a match is
173 * found, create a #DupeSearchMatch and add to \a dw->search_matches list\n
174 * If \a dw->abort is set, just increment \a dw->thread_count
176 static void dupe_comparison_func(gpointer d1, gpointer d2)
178 DupeQueueItem *dqi = static_cast<DupeQueueItem *>(d1);
179 DupeWindow *dw = static_cast<DupeWindow *>(d2);
180 DupeSearchMatch *dsm;
182 GList *matches = NULL;
187 GList *work = dqi->work;
190 di = static_cast<DupeItem *>(work->data);
192 /* forward for second set, back for simple compare */
202 if (dupe_match(di, dqi->needle, dqi->dw->match_mask, &rank, TRUE))
204 dsm = g_new0(DupeSearchMatch, 1);
206 dsm->b = dqi->needle;
208 matches = g_list_prepend(matches, dsm);
209 dsm->index = dqi->index;
218 matches = g_list_reverse(matches);
219 g_mutex_lock(&dw->search_matches_mutex);
220 dw->search_matches = g_list_concat(dw->search_matches, matches);
221 g_mutex_unlock(&dw->search_matches_mutex);
224 g_mutex_lock(&dw->thread_count_mutex);
226 g_mutex_unlock(&dw->thread_count_mutex);
231 * ------------------------------------------------------------------
233 * ------------------------------------------------------------------
237 * @brief Update display of status label
243 static void dupe_window_update_count(DupeWindow *dw, gboolean count_only)
249 text = g_strdup(_("Drop files to compare them."));
253 text = g_strdup_printf(_("%d files"), g_list_length(dw->list));
257 text = g_strdup_printf(_("%d matches found in %d files"), g_list_length(dw->dupes), g_list_length(dw->list));
262 gchar *buf = g_strconcat(text, " ", _("[set 1]"), NULL);
266 gtk_label_set_text(GTK_LABEL(dw->status_label), text);
272 * @brief Returns time in µsec since Epoch
277 static guint64 msec_time(void)
281 if (gettimeofday(&tv, NULL) == -1) return 0;
283 return (guint64)tv.tv_sec * 1000000 + (guint64)tv.tv_usec;
286 static gint dupe_iterations(gint n)
288 return (n * ((n + 1) / 2));
298 * If \a status is blank, clear status bar text and set progress to zero. \n
299 * If \a force is not set, after 2 secs has elapsed, update time-to-go every 250 ms.
301 static void dupe_window_update_progress(DupeWindow *dw, const gchar *status, gdouble value, gboolean force)
303 const gchar *status_text;
307 guint64 new_time = 0;
309 if (dw->setup_n % 10 == 0)
311 new_time = msec_time() - dw->setup_time;
316 dw->setup_count > 0 &&
324 if (new_time - dw->setup_time_count < 250000) return;
325 dw->setup_time_count = new_time;
332 d = dw->setup_count - dw->setup_n;
336 t = dupe_iterations(dw->setup_count);
337 d = dupe_iterations(dw->setup_count - dw->setup_n);
343 d = dw->setup_count - dw->setup_n;
346 rem = (t - d) ? ((gdouble)(dw->setup_time_count / 1000000) / (t - d)) * d : 0;
348 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(dw->extra_label), value);
350 buf = g_strdup_printf("%s %d:%02d ", status, rem / 60, rem % 60);
351 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(dw->extra_label), buf);
358 dw->setup_count == 0 ||
359 dw->setup_time_count == 0 ||
360 (new_time > 0 && new_time - dw->setup_time_count >= 250000))
362 if (dw->setup_time_count == 0) dw->setup_time_count = 1;
363 if (new_time > 0) dw->setup_time_count = new_time;
364 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(dw->extra_label), value);
365 status_text = status;
374 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(dw->extra_label), 0.0);
378 if (status_text) gtk_progress_bar_set_text(GTK_PROGRESS_BAR(dw->extra_label), status_text);
381 static void widget_set_cursor(GtkWidget *widget, gint icon)
385 if (!gtk_widget_get_window(widget)) return;
393 cursor = gdk_cursor_new(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 = NULL;
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 static void dupe_list_free(GList *list)
476 DupeItem *di = static_cast<DupeItem *>(work->data);
484 static DupeItem *dupe_item_find_fd_by_list(FileData *fd, GList *work)
488 DupeItem *di = static_cast<DupeItem *>(work->data);
490 if (di->fd == fd) return di;
500 static DupeItem *dupe_item_find_fd(DupeWindow *dw, FileData *fd)
504 di = dupe_item_find_fd_by_list(fd, dw->list);
505 if (!di && dw->second_set) di = dupe_item_find_fd_by_list(fd, dw->second_list);
512 static DupeItem *dupe_item_find_path_by_list(const gchar *path, GList *work)
516 DupeItem *di = static_cast<DupeItem *>(work->data);
518 if (strcmp(di->fd->path, path) == 0) return di;
528 static DupeItem *dupe_item_find_path(DupeWindow *dw, const gchar *path)
532 di = dupe_item_find_path_by_list(path, dw->list);
533 if (!di && dw->second_set) di = dupe_item_find_path_by_list(path, dw->second_list);
540 * ------------------------------------------------------------------
541 * Image property cache
542 * ------------------------------------------------------------------
545 static void dupe_item_read_cache(DupeItem *di)
552 path = cache_find_location(CACHE_TYPE_SIM, di->fd->path);
555 if (filetime(di->fd->path) != filetime(path))
561 cd = cache_sim_data_load(path);
566 if (!di->simd && cd->sim)
571 if (di->width == 0 && di->height == 0 && cd->dimensions)
573 di->width = cd->width;
574 di->height = cd->height;
575 di->dimensions = (di->width << 16) + di->height;
577 if (!di->md5sum && cd->have_md5sum)
579 di->md5sum = md5_digest_to_text(cd->md5sum);
581 cache_sim_data_free(cd);
585 static void dupe_item_write_cache(DupeItem *di)
592 base = cache_get_location(CACHE_TYPE_SIM, di->fd->path, FALSE, &mode);
593 if (recursive_mkdir_if_not_exists(base, mode))
597 cd = cache_sim_data_new();
598 cd->path = cache_get_location(CACHE_TYPE_SIM, di->fd->path, TRUE, NULL);
600 if (di->width != 0) cache_sim_data_set_dimensions(cd, di->width, di->height);
604 if (md5_digest_from_text(di->md5sum, digest)) cache_sim_data_set_md5sum(cd, digest);
606 if (di->simd) cache_sim_data_set_similarity(cd, di->simd);
608 if (cache_sim_data_save(cd))
610 filetime_set(cd->path, filetime(di->fd->path));
612 cache_sim_data_free(cd);
618 * ------------------------------------------------------------------
620 * ------------------------------------------------------------------
623 static gint dupe_listview_find_item(GtkListStore *store, DupeItem *item, GtkTreeIter *iter)
628 valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), iter);
632 gtk_tree_model_get(GTK_TREE_MODEL(store), iter, DUPE_COLUMN_POINTER, &item_n, -1);
633 if (item_n == item) return row;
635 valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), iter);
642 static void dupe_listview_add(DupeWindow *dw, DupeItem *parent, DupeItem *child)
646 gchar *text[DUPE_COLUMN_COUNT];
649 gboolean color_set = FALSE;
654 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(dw->listview)));
660 row = dupe_listview_find_item(store, parent, &iter);
661 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, DUPE_COLUMN_COLOR, &color_set, -1);
667 dm = static_cast<DupeMatch *>(child->group->data);
668 rank = (gint)floor(dm->rank);
673 log_printf("NULL group in item!\n");
678 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter))
680 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, DUPE_COLUMN_COLOR, &color_set, -1);
681 color_set = !color_set;
692 di = (child) ? child : parent;
694 if (!child && dw->second_set)
696 text[DUPE_COLUMN_RANK] = g_strdup("[1]");
700 text[DUPE_COLUMN_RANK] = g_strdup((di->second) ? "(2)" : "");
704 text[DUPE_COLUMN_RANK] = g_strdup_printf("%d%s", rank, (di->second) ? " (2)" : "");
707 text[DUPE_COLUMN_THUMB] = _("");
708 text[DUPE_COLUMN_NAME] = (gchar *)di->fd->name;
709 text[DUPE_COLUMN_SIZE] = text_from_size(di->fd->size);
710 text[DUPE_COLUMN_DATE] = (gchar *)text_from_time(di->fd->date);
711 if (di->width > 0 && di->height > 0)
713 text[DUPE_COLUMN_DIMENSIONS] = g_strdup_printf("%d x %d", di->width, di->height);
717 text[DUPE_COLUMN_DIMENSIONS] = g_strdup("");
719 text[DUPE_COLUMN_PATH] = di->fd->path;
720 text[DUPE_COLUMN_COLOR] = NULL;
722 gtk_list_store_insert(store, &iter, row);
723 gtk_list_store_set(store, &iter,
724 DUPE_COLUMN_POINTER, di,
725 DUPE_COLUMN_RANK, text[DUPE_COLUMN_RANK],
726 DUPE_COLUMN_THUMB, NULL,
727 DUPE_COLUMN_NAME, text[DUPE_COLUMN_NAME],
728 DUPE_COLUMN_SIZE, text[DUPE_COLUMN_SIZE],
729 DUPE_COLUMN_DATE, text[DUPE_COLUMN_DATE],
730 DUPE_COLUMN_DIMENSIONS, text[DUPE_COLUMN_DIMENSIONS],
731 DUPE_COLUMN_PATH, text[DUPE_COLUMN_PATH],
732 DUPE_COLUMN_COLOR, color_set,
733 DUPE_COLUMN_SET, dw->set_count,
736 g_free(text[DUPE_COLUMN_RANK]);
737 g_free(text[DUPE_COLUMN_SIZE]);
738 g_free(text[DUPE_COLUMN_DIMENSIONS]);
741 static void dupe_listview_select_dupes(DupeWindow *dw, DupeSelectType parents);
743 static void dupe_listview_populate(DupeWindow *dw)
748 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(dw->listview)));
749 gtk_list_store_clear(store);
751 work = g_list_last(dw->dupes);
754 DupeItem *parent = static_cast<DupeItem *>(work->data);
757 dupe_listview_add(dw, parent, NULL);
759 temp = g_list_last(parent->group);
762 DupeMatch *dm = static_cast<DupeMatch *>(temp->data);
767 dupe_listview_add(dw, parent, child);
775 gtk_tree_view_columns_autosize(GTK_TREE_VIEW(dw->listview));
777 if (options->duplicates_select_type == DUPE_SELECT_GROUP1)
779 dupe_listview_select_dupes(dw, DUPE_SELECT_GROUP1);
781 else if (options->duplicates_select_type == DUPE_SELECT_GROUP2)
783 dupe_listview_select_dupes(dw, DUPE_SELECT_GROUP2);
788 static void dupe_listview_remove(DupeWindow *dw, DupeItem *di)
796 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(dw->listview)));
797 row = dupe_listview_find_item(store, di, &iter);
800 tree_view_move_cursor_away(GTK_TREE_VIEW(dw->listview), &iter, TRUE);
801 gtk_list_store_remove(store, &iter);
803 if (g_list_find(dw->dupes, di) != NULL)
805 if (!dw->color_frozen) dupe_listview_realign_colors(dw);
810 static GList *dupe_listview_get_filelist(DupeWindow *UNUSED(dw), GtkWidget *listview)
817 store = gtk_tree_view_get_model(GTK_TREE_VIEW(listview));
818 valid = gtk_tree_model_get_iter_first(store, &iter);
822 gtk_tree_model_get(store, &iter, DUPE_COLUMN_POINTER, &di, -1);
823 list = g_list_prepend(list, file_data_ref(di->fd));
825 valid = gtk_tree_model_iter_next(store, &iter);
828 return g_list_reverse(list);
832 static GList *dupe_listview_get_selection(DupeWindow *UNUSED(dw), GtkWidget *listview)
835 GtkTreeSelection *selection;
840 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(listview));
841 slist = gtk_tree_selection_get_selected_rows(selection, &store);
845 GtkTreePath *tpath = static_cast<GtkTreePath *>(work->data);
849 gtk_tree_model_get_iter(store, &iter, tpath);
850 gtk_tree_model_get(store, &iter, DUPE_COLUMN_POINTER, &di, -1);
853 list = g_list_prepend(list, file_data_ref(di->fd));
857 g_list_foreach(slist, (GFunc)tree_path_free_wrapper, NULL);
860 return g_list_reverse(list);
863 static gboolean dupe_listview_item_is_selected(DupeWindow *UNUSED(dw), DupeItem *di, GtkWidget *listview)
866 GtkTreeSelection *selection;
869 gboolean found = FALSE;
871 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(listview));
872 slist = gtk_tree_selection_get_selected_rows(selection, &store);
874 while (!found && work)
876 GtkTreePath *tpath = static_cast<GtkTreePath *>(work->data);
880 gtk_tree_model_get_iter(store, &iter, tpath);
881 gtk_tree_model_get(store, &iter, DUPE_COLUMN_POINTER, &di_n, -1);
882 if (di_n == di) found = TRUE;
885 g_list_foreach(slist, (GFunc)tree_path_free_wrapper, NULL);
891 static void dupe_listview_select_dupes(DupeWindow *dw, DupeSelectType parents)
894 GtkTreeSelection *selection;
898 gint set_count_last = -1;
900 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dw->listview));
901 gtk_tree_selection_unselect_all(selection);
903 store = gtk_tree_view_get_model(GTK_TREE_VIEW(dw->listview));
904 valid = gtk_tree_model_get_iter_first(store, &iter);
909 gtk_tree_model_get(store, &iter, DUPE_COLUMN_POINTER, &di, DUPE_COLUMN_SET, &set_count, -1);
910 if (set_count != set_count_last)
912 set_count_last = set_count;
913 if (parents == DUPE_SELECT_GROUP1)
915 gtk_tree_selection_select_iter(selection, &iter);
920 if (parents == DUPE_SELECT_GROUP2)
922 gtk_tree_selection_select_iter(selection, &iter);
925 valid = gtk_tree_model_iter_next(store, &iter);
930 * ------------------------------------------------------------------
931 * Match group manipulation
932 * ------------------------------------------------------------------
936 * @brief Search \a parent->group for \a child (#DupeItem)
942 static DupeMatch *dupe_match_find_match(DupeItem *child, DupeItem *parent)
946 work = parent->group;
949 DupeMatch *dm = static_cast<DupeMatch *>(work->data);
950 if (dm->di == child) return dm;
957 * @brief Create #DupeMatch structure for \a child, and insert into \a parent->group list.
963 static void dupe_match_link_child(DupeItem *child, DupeItem *parent, gdouble rank)
967 dm = g_new0(DupeMatch, 1);
970 parent->group = g_list_append(parent->group, dm);
974 * @brief Link \a a & \a b as both parent and child
979 * Link \a a as child of \a b, and \a b as child of \a a
981 static void dupe_match_link(DupeItem *a, DupeItem *b, gdouble rank)
983 dupe_match_link_child(a, b, rank);
984 dupe_match_link_child(b, a, rank);
988 * @brief Remove \a child #DupeMatch from \a parent->group list.
993 static void dupe_match_unlink_child(DupeItem *child, DupeItem *parent)
997 dm = dupe_match_find_match(child, parent);
1000 parent->group = g_list_remove(parent->group, dm);
1006 * @brief Unlink \a a from \a b, and \a b from \a a
1010 * Free the relevant #DupeMatch items from the #DupeItem group lists
1012 static void dupe_match_unlink(DupeItem *a, DupeItem *b)
1014 dupe_match_unlink_child(a, b);
1015 dupe_match_unlink_child(b, a);
1021 * @param unlink_children
1023 * If \a unlink_children is set, unlink all entries in \a parent->group list. \n
1024 * Free the \a parent->group list and set group_rank to zero;
1026 static void dupe_match_link_clear(DupeItem *parent, gboolean unlink_children)
1030 work = parent->group;
1033 DupeMatch *dm = static_cast<DupeMatch *>(work->data);
1036 if (unlink_children) dupe_match_unlink_child(parent, dm->di);
1041 g_list_free(parent->group);
1042 parent->group = NULL;
1043 parent->group_rank = 0.0;
1047 * @brief Search \a parent->group list for \a child
1050 * @returns boolean TRUE/FALSE found/not found
1053 static gint dupe_match_link_exists(DupeItem *child, DupeItem *parent)
1055 return (dupe_match_find_match(child, parent) != NULL);
1059 * @brief Search \a parent->group for \a child, and return \a child->rank
1062 * @returns \a dm->di->rank
1065 static gdouble dupe_match_link_rank(DupeItem *child, DupeItem *parent)
1069 dm = dupe_match_find_match(child, parent);
1070 if (dm) return dm->rank;
1076 * @brief Find highest rank in \a child->group
1080 * Search the #DupeMatch entries in the \a child->group list.
1081 * Return the #DupeItem with the highest rank. If more than one have
1082 * the same rank, the first encountered is used.
1084 static DupeItem *dupe_match_highest_rank(DupeItem *child)
1090 work = child->group;
1093 DupeMatch *dm = static_cast<DupeMatch *>(work->data);
1094 if (!dr || dm->rank > dr->rank)
1101 return (dr) ? dr->di : NULL;
1105 * @brief Compute and store \a parent->group_rank
1108 * Group_rank = (sum of all child ranks) / n
1110 static void dupe_match_rank_update(DupeItem *parent)
1116 work = parent->group;
1119 DupeMatch *dm = static_cast<DupeMatch *>(work->data);
1127 parent->group_rank = rank / c;
1131 parent->group_rank = 0.0;
1135 static DupeItem *dupe_match_find_parent(DupeWindow *dw, DupeItem *child)
1139 if (g_list_find(dw->dupes, child)) return child;
1141 work = child->group;
1144 DupeMatch *dm = static_cast<DupeMatch *>(work->data);
1145 if (g_list_find(dw->dupes, dm->di)) return dm->di;
1154 * @param work (#DupeItem) dw->list or dw->second_list
1156 * Unlink all #DupeItem-s in \a work.
1157 * Do not unlink children.
1159 static void dupe_match_reset_list(GList *work)
1163 DupeItem *di = static_cast<DupeItem *>(work->data);
1166 dupe_match_link_clear(di, FALSE);
1170 static void dupe_match_reparent(DupeWindow *dw, DupeItem *old_parent, DupeItem *new_parent)
1174 if (!old_parent || !new_parent || !dupe_match_link_exists(old_parent, new_parent)) return;
1176 dupe_match_link_clear(new_parent, TRUE);
1177 work = old_parent->group;
1180 DupeMatch *dm = static_cast<DupeMatch *>(work->data);
1181 dupe_match_unlink_child(old_parent, dm->di);
1182 dupe_match_link_child(new_parent, dm->di, dm->rank);
1186 new_parent->group = old_parent->group;
1187 old_parent->group = NULL;
1189 work = g_list_find(dw->dupes, old_parent);
1190 if (work) work->data = new_parent;
1193 static void dupe_match_print_group(DupeItem *di)
1197 log_printf("+ %f %s\n", di->group_rank, di->fd->name);
1202 DupeMatch *dm = static_cast<DupeMatch *>(work->data);
1205 log_printf(" %f %s\n", dm->rank, dm->di->fd->name);
1211 static void dupe_match_print_list(GList *list)
1218 DupeItem *di = static_cast<DupeItem *>(work->data);
1219 dupe_match_print_group(di);
1224 /* level 3, unlinking and orphan handling */
1228 * @param parent \a di from \a child->group
1229 * @param[inout] list \a dw->list sorted by rank (#DupeItem)
1231 * @returns modified \a list
1233 * Called for each entry in \a child->group (#DupeMatch) with \a parent set to \a dm->di. \n
1234 * Find the highest rank #DupeItem of the \a parent's children. \n
1235 * If that is == \a child OR
1236 * highest rank #DupeItem of \a child == \a parent then FIXME:
1239 static GList *dupe_match_unlink_by_rank(DupeItem *child, DupeItem *parent, GList *list, DupeWindow *dw)
1241 DupeItem *best = NULL;
1243 best = dupe_match_highest_rank(parent); // highest rank in parent->group
1244 if (best == child || dupe_match_highest_rank(child) == parent)
1249 DEBUG_2("link found %s to %s [%d]", child->fd->name, parent->fd->name, g_list_length(parent->group));
1251 work = parent->group;
1254 DupeMatch *dm = static_cast<DupeMatch *>(work->data);
1259 if (orphan != child && g_list_length(orphan->group) < 2)
1261 dupe_match_link_clear(orphan, TRUE);
1262 if (!dw->second_set || orphan->second)
1264 dupe_match(orphan, child, dw->match_mask, &rank, FALSE);
1265 dupe_match_link(orphan, child, rank);
1267 list = g_list_remove(list, orphan);
1271 rank = dupe_match_link_rank(child, parent); // child->rank
1272 dupe_match_link_clear(parent, TRUE);
1273 dupe_match_link(child, parent, rank);
1274 list = g_list_remove(list, parent);
1278 DEBUG_2("unlinking %s and %s", child->fd->name, parent->fd->name);
1280 dupe_match_unlink(child, parent);
1289 * @param[inout] list \a dw->list sorted by rank (#DupeItem)
1292 * @returns modified \a list
1294 * Called for each entry in \a list.
1295 * Call unlink for each child in \a di->group
1297 static GList *dupe_match_group_filter(GList *list, DupeItem *di, DupeWindow *dw)
1301 work = g_list_last(di->group);
1304 DupeMatch *dm = static_cast<DupeMatch *>(work->data);
1306 list = dupe_match_unlink_by_rank(di, dm->di, list, dw);
1315 * @param[inout] list \a dw->list sorted by rank (#DupeItem)
1317 * @returns Filtered \a list
1320 * Call group filter for each \a di in \a list
1322 static GList *dupe_match_group_trim(GList *list, DupeWindow *dw)
1329 DupeItem *di = static_cast<DupeItem *>(work->data);
1330 if (!di->second) list = dupe_match_group_filter(list, di, dw);
1332 if (di->second) list = g_list_remove(list, di);
1338 static gint dupe_match_sort_groups_cb(gconstpointer a, gconstpointer b)
1340 DupeMatch *da = (DupeMatch *)a;
1341 DupeMatch *db = (DupeMatch *)b;
1343 if (da->rank > db->rank) return -1;
1344 if (da->rank < db->rank) return 1;
1349 * @brief Sorts the children of each #DupeItem in \a list
1350 * @param list #DupeItem
1352 * Sorts the #DupeItem->group children on rank
1354 static void dupe_match_sort_groups(GList *list)
1361 DupeItem *di = static_cast<DupeItem *>(work->data);
1362 di->group = g_list_sort(di->group, dupe_match_sort_groups_cb);
1367 static gint dupe_match_totals_sort_cb(gconstpointer a, gconstpointer b)
1369 DupeItem *da = (DupeItem *)a;
1370 DupeItem *db = (DupeItem *)b;
1372 if (g_list_length(da->group) > g_list_length(db->group)) return -1;
1373 if (g_list_length(da->group) < g_list_length(db->group)) return 1;
1375 if (da->group_rank < db->group_rank) return -1;
1376 if (da->group_rank > db->group_rank) return 1;
1382 * @brief Callback for group_rank sort
1389 static gint dupe_match_rank_sort_cb(gconstpointer a, gconstpointer b)
1391 DupeItem *da = (DupeItem *)a;
1392 DupeItem *db = (DupeItem *)b;
1394 if (da->group_rank > db->group_rank) return -1;
1395 if (da->group_rank < db->group_rank) return 1;
1400 * @brief Sorts \a source_list by group-rank
1401 * @param source_list #DupeItem
1404 * Computes group_rank for each #DupeItem. \n
1405 * Items with no group list are ignored.
1406 * Returns allocated GList of #DupeItem-s sorted by group_rank
1408 static GList *dupe_match_rank_sort(GList *source_list)
1416 DupeItem *di = static_cast<DupeItem *>(work->data);
1420 dupe_match_rank_update(di); // Compute and store group_rank for di
1421 list = g_list_prepend(list, di);
1427 return g_list_sort(list, dupe_match_rank_sort_cb);
1431 * @brief Returns allocated GList of dupes sorted by totals
1432 * @param source_list
1437 static GList *dupe_match_totals_sort(GList *source_list)
1439 source_list = g_list_sort(source_list, dupe_match_totals_sort_cb);
1441 source_list = g_list_first(source_list);
1442 return g_list_reverse(source_list);
1451 static void dupe_match_rank(DupeWindow *dw)
1455 list = dupe_match_rank_sort(dw->list); // sorted by group_rank, no-matches filtered out
1457 if (required_debug_level(2)) dupe_match_print_list(list);
1459 DEBUG_1("Similar items: %d", g_list_length(list));
1460 list = dupe_match_group_trim(list, dw);
1461 DEBUG_1("Unique groups: %d", g_list_length(list));
1463 dupe_match_sort_groups(list);
1465 if (required_debug_level(2)) dupe_match_print_list(list);
1467 list = dupe_match_rank_sort(list);
1468 if (options->sort_totals)
1470 list = dupe_match_totals_sort(list);
1472 if (required_debug_level(2)) dupe_match_print_list(list);
1474 g_list_free(dw->dupes);
1479 * ------------------------------------------------------------------
1481 * ------------------------------------------------------------------
1493 * For similarity checks, compute rank - (similarity factor between a and b). \n
1494 * If rank < user-set sim value, returns FALSE.
1496 static gboolean dupe_match(DupeItem *a, DupeItem *b, DupeMatchType mask, gdouble *rank, gint fast)
1500 if (a->fd->path == b->fd->path) return FALSE;
1502 if (mask & DUPE_MATCH_ALL)
1506 if (mask & DUPE_MATCH_PATH)
1508 if (utf8_compare(a->fd->path, b->fd->path, TRUE) != 0) return FALSE;
1510 if (mask & DUPE_MATCH_NAME)
1512 if (strcmp(a->fd->collate_key_name, b->fd->collate_key_name) != 0) return FALSE;
1514 if (mask & DUPE_MATCH_NAME_CI)
1516 if (strcmp(a->fd->collate_key_name_nocase, b->fd->collate_key_name_nocase) != 0) return FALSE;
1518 if (mask & DUPE_MATCH_NAME_CONTENT)
1520 if (strcmp(a->fd->collate_key_name, b->fd->collate_key_name) == 0)
1522 if (!a->md5sum) a->md5sum = md5_text_from_file_utf8(a->fd->path, "");
1523 if (!b->md5sum) b->md5sum = md5_text_from_file_utf8(b->fd->path, "");
1524 if (a->md5sum[0] == '\0' ||
1525 b->md5sum[0] == '\0' ||
1526 strcmp(a->md5sum, b->md5sum) != 0)
1540 if (mask & DUPE_MATCH_NAME_CI_CONTENT)
1542 if (strcmp(a->fd->collate_key_name_nocase, b->fd->collate_key_name_nocase) == 0)
1544 if (!a->md5sum) a->md5sum = md5_text_from_file_utf8(a->fd->path, "");
1545 if (!b->md5sum) b->md5sum = md5_text_from_file_utf8(b->fd->path, "");
1546 if (a->md5sum[0] == '\0' ||
1547 b->md5sum[0] == '\0' ||
1548 strcmp(a->md5sum, b->md5sum) != 0)
1562 if (mask & DUPE_MATCH_SIZE)
1564 if (a->fd->size != b->fd->size) return FALSE;
1566 if (mask & DUPE_MATCH_DATE)
1568 if (a->fd->date != b->fd->date) return FALSE;
1570 if (mask & DUPE_MATCH_SUM)
1572 if (!a->md5sum) a->md5sum = md5_text_from_file_utf8(a->fd->path, "");
1573 if (!b->md5sum) b->md5sum = md5_text_from_file_utf8(b->fd->path, "");
1574 if (a->md5sum[0] == '\0' ||
1575 b->md5sum[0] == '\0' ||
1576 strcmp(a->md5sum, b->md5sum) != 0) return FALSE;
1578 if (mask & DUPE_MATCH_DIM)
1580 if (a->width == 0) image_load_dimensions(a->fd, &a->width, &a->height);
1581 if (b->width == 0) image_load_dimensions(b->fd, &b->width, &b->height);
1582 if (a->width != b->width || a->height != b->height) return FALSE;
1584 if (mask & DUPE_MATCH_SIM_HIGH ||
1585 mask & DUPE_MATCH_SIM_MED ||
1586 mask & DUPE_MATCH_SIM_LOW ||
1587 mask & DUPE_MATCH_SIM_CUSTOM)
1592 if (mask & DUPE_MATCH_SIM_HIGH) m = 0.95;
1593 else if (mask & DUPE_MATCH_SIM_MED) m = 0.90;
1594 else if (mask & DUPE_MATCH_SIM_CUSTOM) m = (gdouble)options->duplicates_similarity_threshold / 100.0;
1599 f = image_sim_compare_fast(a->simd, b->simd, m);
1603 f = image_sim_compare(a->simd, b->simd);
1608 if (f < m) return FALSE;
1610 DEBUG_3("similar: %32s %32s = %f", a->fd->name, b->fd->name, f);
1617 * @brief Determine if there is a match
1621 * @returns DUPE_MATCH/DUPE_NO_MATCH/DUPE_NAME_MATCH
1622 * DUPE_NAME_MATCH is used for name != contents searches:
1623 * the name and content match i.e.
1624 * no match, but keep searching
1626 * Called when stepping down the array looking for adjacent matches,
1627 * and from the 2nd set search.
1629 * Is not used for similarity checks.
1631 static DUPE_CHECK_RESULT dupe_match_check(DupeItem *di1, DupeItem *di2, gpointer data)
1633 DupeWindow *dw = static_cast<DupeWindow *>(data);
1634 DupeMatchType mask = dw->match_mask;
1636 if (mask & DUPE_MATCH_ALL)
1640 if (mask & DUPE_MATCH_PATH)
1642 if (utf8_compare(di1->fd->path, di2->fd->path, TRUE) != 0)
1644 return DUPE_NO_MATCH;
1647 if (mask & DUPE_MATCH_NAME)
1649 if (g_strcmp0(di1->fd->collate_key_name, di2->fd->collate_key_name) != 0)
1651 return DUPE_NO_MATCH;
1654 if (mask & DUPE_MATCH_NAME_CI)
1656 if (g_strcmp0(di1->fd->collate_key_name_nocase, di2->fd->collate_key_name_nocase) != 0 )
1658 return DUPE_NO_MATCH;
1661 if (mask & DUPE_MATCH_NAME_CONTENT)
1663 if (g_strcmp0(di1->fd->collate_key_name, di2->fd->collate_key_name) == 0)
1665 if (g_strcmp0(di1->md5sum, di2->md5sum) == 0)
1667 return DUPE_NAME_MATCH;
1672 return DUPE_NO_MATCH;
1675 if (mask & DUPE_MATCH_NAME_CI_CONTENT)
1677 if (strcmp(di1->fd->collate_key_name_nocase, di2->fd->collate_key_name_nocase) == 0)
1679 if (g_strcmp0(di1->md5sum, di2->md5sum) == 0)
1681 return DUPE_NAME_MATCH;
1686 return DUPE_NO_MATCH;
1689 if (mask & DUPE_MATCH_SIZE)
1691 if (di1->fd->size != di2->fd->size)
1693 return DUPE_NO_MATCH;
1696 if (mask & DUPE_MATCH_DATE)
1698 if (di1->fd->date != di2->fd->date)
1700 return DUPE_NO_MATCH;
1703 if (mask & DUPE_MATCH_SUM)
1705 if (g_strcmp0(di1->md5sum, di2->md5sum) != 0)
1707 return DUPE_NO_MATCH;
1710 if (mask & DUPE_MATCH_DIM)
1712 if (di1->dimensions != di2->dimensions)
1714 return DUPE_NO_MATCH;
1722 * @brief The callback for the binary search
1725 * @param param_match_mask
1726 * @returns negative/0/positive
1728 * Is not used for similarity checks.
1730 * Used only when two file sets are used.
1731 * Requires use of a global for param_match_mask because there is no
1732 * g_array_binary_search_with_data() function in glib.
1734 static gint dupe_match_binary_search_cb(gconstpointer a, gconstpointer b)
1736 const DupeItem *di1 = *((DupeItem **) a);
1737 const DupeItem *di2 = static_cast<const DupeItem *>(b);
1738 DupeMatchType mask = param_match_mask;
1740 if (mask & DUPE_MATCH_ALL)
1744 if (mask & DUPE_MATCH_PATH)
1746 return utf8_compare(di1->fd->path, di2->fd->path, TRUE);
1748 if (mask & DUPE_MATCH_NAME)
1750 return g_strcmp0(di1->fd->collate_key_name, di2->fd->collate_key_name);
1752 if (mask & DUPE_MATCH_NAME_CI)
1754 return strcmp(di1->fd->collate_key_name_nocase, di2->fd->collate_key_name_nocase);
1756 if (mask & DUPE_MATCH_NAME_CONTENT)
1758 return g_strcmp0(di1->fd->collate_key_name, di2->fd->collate_key_name);
1760 if (mask & DUPE_MATCH_NAME_CI_CONTENT)
1762 return strcmp(di1->fd->collate_key_name_nocase, di2->fd->collate_key_name_nocase);
1764 if (mask & DUPE_MATCH_SIZE)
1766 return (di1->fd->size - di2->fd->size);
1768 if (mask & DUPE_MATCH_DATE)
1770 return (di1->fd->date - di2->fd->date);
1772 if (mask & DUPE_MATCH_SUM)
1774 return g_strcmp0(di1->md5sum, di2->md5sum);
1776 if (mask & DUPE_MATCH_DIM)
1778 return (di1->dimensions - di2->dimensions);
1785 * @brief The callback for the array sort
1789 * @returns negative/0/positive
1791 * Is not used for similarity checks.
1793 static gint dupe_match_sort_cb(gconstpointer a, gconstpointer b, gpointer data)
1795 const DupeItem *di1 = *((DupeItem **) a);
1796 const DupeItem *di2 = *((DupeItem **) b);
1797 DupeWindow *dw = static_cast<DupeWindow *>(data);
1798 DupeMatchType mask = dw->match_mask;
1800 if (mask & DUPE_MATCH_ALL)
1804 if (mask & DUPE_MATCH_PATH)
1806 return utf8_compare(di1->fd->path, di2->fd->path, TRUE);
1808 if (mask & DUPE_MATCH_NAME)
1810 return g_strcmp0(di1->fd->collate_key_name, di2->fd->collate_key_name);
1812 if (mask & DUPE_MATCH_NAME_CI)
1814 return strcmp(di1->fd->collate_key_name_nocase, di2->fd->collate_key_name_nocase);
1816 if (mask & DUPE_MATCH_NAME_CONTENT)
1818 return g_strcmp0(di1->fd->collate_key_name, di2->fd->collate_key_name);
1820 if (mask & DUPE_MATCH_NAME_CI_CONTENT)
1822 return strcmp(di1->fd->collate_key_name_nocase, di2->fd->collate_key_name_nocase);
1824 if (mask & DUPE_MATCH_SIZE)
1826 return (di1->fd->size - di2->fd->size);
1828 if (mask & DUPE_MATCH_DATE)
1830 return (di1->fd->date - di2->fd->date);
1832 if (mask & DUPE_MATCH_SUM)
1834 if (di1->md5sum[0] == '\0' || di2->md5sum[0] == '\0')
1840 return strcmp(di1->md5sum, di2->md5sum);
1843 if (mask & DUPE_MATCH_DIM)
1845 if (!di1 || !di2 || !di1->width || !di1->height || !di2->width || !di2->height)
1849 return (di1->dimensions - di2->dimensions);
1852 return 0; // should not execute
1856 * @brief Check for duplicate matches
1859 * Is not used for similarity checks.
1861 * Loads the file sets into an array and sorts on the searched
1864 * If one file set, steps down the array looking for adjacent equal values.
1866 * If two file sets, steps down the first set and for each value
1867 * does a binary search for matches in the second set.
1869 static void dupe_array_check(DupeWindow *dw )
1876 DUPE_CHECK_RESULT check_result;
1877 param_match_mask = dw->match_mask;
1878 guint out_match_index;
1879 gboolean match_found = FALSE;;
1881 if (!dw->list) return;
1883 array_set1 = g_array_new(TRUE, TRUE, sizeof(gpointer));
1884 array_set2 = g_array_new(TRUE, TRUE, sizeof(gpointer));
1885 dupe_match_reset_list(dw->list);
1890 DupeItem *di = static_cast<DupeItem *>(work->data);
1891 g_array_append_val(array_set1, di);
1895 g_array_sort_with_data(array_set1, dupe_match_sort_cb, dw);
1899 /* Two sets - nothing can be done until a second set is loaded */
1900 if (dw->second_list)
1902 work = dw->second_list;
1905 g_array_append_val(array_set2, (work->data));
1908 g_array_sort_with_data(array_set2, dupe_match_sort_cb, dw);
1910 for (i_set1 = 0; i_set1 <= (gint)(array_set1->len) - 1; i_set1++)
1912 DupeItem *di1 = static_cast<DupeItem *>(g_array_index(array_set1, gpointer, i_set1));
1913 DupeItem *di2 = NULL;
1914 /* If multiple identical entries in set 1, use the last one */
1915 if (i_set1 < (gint)(array_set1->len) - 2)
1917 di2 = static_cast<DupeItem *>(g_array_index(array_set1, gpointer, i_set1 + 1));
1918 check_result = dupe_match_check(di1, di2, dw);
1919 if (check_result == DUPE_MATCH || check_result == DUPE_NAME_MATCH)
1925 #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION >= 62))
1926 match_found = g_array_binary_search(array_set2, di1, dupe_match_binary_search_cb, &out_match_index);
1930 match_found = FALSE;
1931 for(i=0; i < array_set2->len; i++)
1933 di2 = static_cast<DupeItem *>(g_array_index(array_set2, gpointer, i));
1934 check_result = dupe_match_check(di1, di2, dw);
1935 if (check_result == DUPE_MATCH)
1938 out_match_index = i;
1946 di2 = static_cast<DupeItem *>(g_array_index(array_set2, gpointer, out_match_index));
1948 check_result = dupe_match_check(di1, di2, dw);
1949 if (check_result == DUPE_MATCH || check_result == DUPE_NAME_MATCH)
1951 if (check_result == DUPE_MATCH)
1953 dupe_match_link(di2, di1, 0.0);
1955 i_set2 = out_match_index + 1;
1957 if (i_set2 > (gint)(array_set2->len) - 1)
1961 /* Look for multiple matches in set 2 for item di1 */
1962 di2 = static_cast<DupeItem *>(g_array_index(array_set2, gpointer, i_set2));
1963 check_result = dupe_match_check(di1, di2, dw);
1964 while (check_result == DUPE_MATCH || check_result == DUPE_NAME_MATCH)
1966 if (check_result == DUPE_MATCH)
1968 dupe_match_link(di2, di1, 0.0);
1971 if (i_set2 > (gint)(array_set2->len) - 1)
1975 di2 = static_cast<DupeItem *>(g_array_index(array_set2, gpointer, i_set2));
1976 check_result = dupe_match_check(di1, di2, dw);
1985 /* File set 1 only */
1986 g_list_free(dw->dupes);
1989 if ((gint)(array_set1->len) > 1)
1991 for (i_set1 = 0; i_set1 <= (gint)(array_set1->len) - 2; i_set1++)
1993 DupeItem *di1 = static_cast<DupeItem *>(g_array_index(array_set1, gpointer, i_set1));
1994 DupeItem *di2 = static_cast<DupeItem *>(g_array_index(array_set1, gpointer, i_set1 + 1));
1996 check_result = dupe_match_check(di1, di2, dw);
1997 if (check_result == DUPE_MATCH || check_result == DUPE_NAME_MATCH)
1999 if (check_result == DUPE_MATCH)
2001 dupe_match_link(di2, di1, 0.0);
2005 if ( i_set1 + 1 > (gint)(array_set1->len) - 1)
2009 /* Look for multiple matches for item di1 */
2010 di2 = static_cast<DupeItem *>(g_array_index(array_set1, gpointer, i_set1 + 1));
2011 check_result = dupe_match_check(di1, di2, dw);
2012 while (check_result == DUPE_MATCH || check_result == DUPE_NAME_MATCH)
2014 if (check_result == DUPE_MATCH)
2016 dupe_match_link(di2, di1, 0.0);
2020 if (i_set1 + 1 > (gint)(array_set1->len) - 1)
2024 di2 = static_cast<DupeItem *>(g_array_index(array_set1, gpointer, i_set1 + 1));
2025 check_result = dupe_match_check(di1, di2, dw);
2031 g_array_free(array_set1, TRUE);
2032 g_array_free(array_set2, TRUE);
2036 * @brief Look for similarity match
2041 * Only used for similarity checks.\n
2042 * Called from dupe_check_cb.
2043 * Called for each entry in the list.
2044 * Steps through the list looking for matches against needle.
2045 * Pushes a #DupeQueueItem onto thread pool queue.
2047 static void dupe_list_check_match(DupeWindow *dw, DupeItem *needle, GList *start)
2054 work = dw->second_list;
2062 work = g_list_last(dw->list);
2065 dqi = g_new0(DupeQueueItem, 1);
2066 dqi->needle = needle;
2069 dqi->index = dw->queue_count;
2070 g_thread_pool_push(dw->dupe_comparison_thread_pool, dqi, NULL);
2074 * ------------------------------------------------------------------
2075 * Thumbnail handling
2076 * ------------------------------------------------------------------
2079 static void dupe_listview_set_thumb(DupeWindow *dw, DupeItem *di, GtkTreeIter *iter)
2081 GtkListStore *store;
2084 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(dw->listview)));
2087 if (dupe_listview_find_item(store, di, &iter_n) >= 0)
2093 if (iter) gtk_list_store_set(store, iter, DUPE_COLUMN_THUMB, di->pixbuf, -1);
2096 static void dupe_thumb_do(DupeWindow *dw)
2100 if (!dw->thumb_loader || !dw->thumb_item) return;
2101 di = dw->thumb_item;
2103 if (di->pixbuf) g_object_unref(di->pixbuf);
2104 di->pixbuf = thumb_loader_get_pixbuf(dw->thumb_loader);
2106 dupe_listview_set_thumb(dw, di, NULL);
2109 static void dupe_thumb_error_cb(ThumbLoader *UNUSED(tl), gpointer data)
2111 DupeWindow *dw = static_cast<DupeWindow *>(data);
2114 dupe_thumb_step(dw);
2117 static void dupe_thumb_done_cb(ThumbLoader *UNUSED(tl), gpointer data)
2119 DupeWindow *dw = static_cast<DupeWindow *>(data);
2122 dupe_thumb_step(dw);
2125 static void dupe_thumb_step(DupeWindow *dw)
2127 GtkTreeModel *store;
2129 DupeItem *di = NULL;
2134 store = gtk_tree_view_get_model(GTK_TREE_VIEW(dw->listview));
2135 valid = gtk_tree_model_get_iter_first(store, &iter);
2137 while (!di && valid)
2142 gtk_tree_model_get(store, &iter, DUPE_COLUMN_POINTER, &di, DUPE_COLUMN_THUMB, &pixbuf, -1);
2143 if (pixbuf || di->pixbuf)
2145 if (!pixbuf) gtk_list_store_set(GTK_LIST_STORE(store), &iter, DUPE_COLUMN_THUMB, di->pixbuf, -1);
2149 valid = gtk_tree_model_iter_next(store, &iter);
2153 while (gtk_tree_model_iter_next(store, &iter)) length++;
2158 dw->thumb_item = NULL;
2159 thumb_loader_free(dw->thumb_loader);
2160 dw->thumb_loader = NULL;
2162 dupe_window_update_progress(dw, NULL, 0.0, FALSE);
2166 dupe_window_update_progress(dw, _("Loading thumbs..."),
2167 length == 0 ? 0.0 : (gdouble)(row) / length, FALSE);
2169 dw->thumb_item = di;
2170 thumb_loader_free(dw->thumb_loader);
2171 dw->thumb_loader = thumb_loader_new(options->thumbnails.max_width, options->thumbnails.max_height);
2173 thumb_loader_set_callbacks(dw->thumb_loader,
2175 dupe_thumb_error_cb,
2180 if (!thumb_loader_start(dw->thumb_loader, di->fd))
2182 /* error, handle it, do next */
2183 DEBUG_1("error loading thumb for %s", di->fd->path);
2185 dupe_thumb_step(dw);
2190 * ------------------------------------------------------------------
2191 * Dupe checking loop
2192 * ------------------------------------------------------------------
2195 static void dupe_check_stop(DupeWindow *dw)
2197 if (dw->idle_id > 0)
2199 g_source_remove(dw->idle_id);
2205 while (dw->thread_count < dw->queue_count) // Wait for the queue to empty
2207 dupe_window_update_progress(dw, NULL, 0.0, FALSE);
2208 widget_set_cursor(dw->listview, -1);
2211 g_list_free(dw->search_matches);
2212 dw->search_matches = NULL;
2214 if (dw->idle_id || dw->img_loader || dw->thumb_loader)
2216 if (dw->idle_id > 0)
2218 g_source_remove(dw->idle_id);
2221 dupe_window_update_progress(dw, NULL, 0.0, FALSE);
2222 widget_set_cursor(dw->listview, -1);
2225 if (dw->add_files_queue_id)
2227 g_source_remove(dw->add_files_queue_id);
2228 dw->add_files_queue_id = 0;
2229 dupe_destroy_list_cache(dw);
2230 gtk_widget_set_sensitive(dw->controls_box, TRUE);
2231 if (g_list_length(dw->add_files_queue) > 0)
2233 filelist_free(dw->add_files_queue);
2235 dw->add_files_queue = NULL;
2236 dupe_window_update_progress(dw, NULL, 0.0, FALSE);
2237 widget_set_cursor(dw->listview, -1);
2240 thumb_loader_free(dw->thumb_loader);
2241 dw->thumb_loader = NULL;
2243 image_loader_free(dw->img_loader);
2244 dw->img_loader = NULL;
2247 static void dupe_check_stop_cb(GtkWidget *UNUSED(widget), gpointer data)
2249 DupeWindow *dw = static_cast<DupeWindow *>(data);
2251 dupe_check_stop(dw);
2254 static void dupe_loader_done_cb(ImageLoader *il, gpointer data)
2256 DupeWindow *dw = static_cast<DupeWindow *>(data);
2259 pixbuf = image_loader_get_pixbuf(il);
2261 if (dw->setup_point)
2263 DupeItem *di = static_cast<DupeItem *>(dw->setup_point->data);
2267 di->simd = image_sim_new_from_pixbuf(pixbuf);
2271 image_sim_fill_data(di->simd, pixbuf);
2274 if (di->width == 0 && di->height == 0)
2276 di->width = gdk_pixbuf_get_width(pixbuf);
2277 di->height = gdk_pixbuf_get_height(pixbuf);
2279 if (options->thumbnails.enable_caching)
2281 dupe_item_write_cache(di);
2284 image_sim_alternate_processing(di->simd);
2287 image_loader_free(dw->img_loader);
2288 dw->img_loader = NULL;
2290 dw->idle_id = g_idle_add(dupe_check_cb, dw);
2293 static void dupe_setup_reset(DupeWindow *dw)
2295 dw->setup_point = NULL;
2297 dw->setup_time = msec_time();
2298 dw->setup_time_count = 0;
2301 static GList *dupe_setup_point_step(DupeWindow *dw, GList *p)
2303 if (!p) return NULL;
2305 if (p->next) return p->next;
2307 if (dw->second_set && g_list_first(p) == dw->list) return dw->second_list;
2313 * @brief Generates the sumcheck or dimensions
2314 * @param list Set1 or set2
2315 * @returns TRUE/FALSE = not completed/completed
2317 * Ensures that the DIs contain the MD5SUM or dimensions for all items in
2318 * the list. One item at a time. Re-enters if not completed.
2320 static gboolean create_checksums_dimensions(DupeWindow *dw, GList *list)
2322 if ((dw->match_mask & DUPE_MATCH_SUM) ||
2323 (dw->match_mask & DUPE_MATCH_NAME_CONTENT) ||
2324 (dw->match_mask & DUPE_MATCH_NAME_CI_CONTENT))
2327 if (!dw->setup_point) dw->setup_point = list; // setup_point clear on 1st entry
2329 while (dw->setup_point)
2331 DupeItem *di = static_cast<DupeItem *>(dw->setup_point->data);
2333 dw->setup_point = dupe_setup_point_step(dw, dw->setup_point);
2338 dupe_window_update_progress(dw, _("Reading checksums..."),
2339 dw->setup_count == 0 ? 0.0 : (gdouble)(dw->setup_n - 1) / dw->setup_count, FALSE);
2341 if (options->thumbnails.enable_caching)
2343 dupe_item_read_cache(di);
2350 di->md5sum = md5_text_from_file_utf8(di->fd->path, "");
2351 if (options->thumbnails.enable_caching)
2353 dupe_item_write_cache(di);
2358 dupe_setup_reset(dw);
2361 if ((dw->match_mask & DUPE_MATCH_DIM) )
2363 /* Dimensions only */
2364 if (!dw->setup_point) dw->setup_point = list;
2366 while (dw->setup_point)
2368 DupeItem *di = static_cast<DupeItem *>(dw->setup_point->data);
2370 dw->setup_point = dupe_setup_point_step(dw, dw->setup_point);
2372 if (di->width == 0 && di->height == 0)
2374 dupe_window_update_progress(dw, _("Reading dimensions..."),
2375 dw->setup_count == 0 ? 0.0 : (gdouble)(dw->setup_n - 1) / dw->setup_count, FALSE);
2377 if (options->thumbnails.enable_caching)
2379 dupe_item_read_cache(di);
2380 if (di->width != 0 || di->height != 0)
2386 image_load_dimensions(di->fd, &di->width, &di->height);
2387 di->dimensions = (di->width << 16) + di->height;
2388 if (options->thumbnails.enable_caching)
2390 dupe_item_write_cache(di);
2395 dupe_setup_reset(dw);
2402 * @brief Compare func. for sorting search matches
2403 * @param a #DupeSearchMatch
2404 * @param b #DupeSearchMatch
2407 * Used only for similarity checks\n
2408 * Sorts search matches on order they were inserted into the pool queue
2410 static gint sort_func(gconstpointer a, gconstpointer b)
2412 return (((DupeSearchMatch *)a)->index - ((DupeSearchMatch *)b)->index);
2416 * @brief Check set 1 (and set 2) for matches
2417 * @param data DupeWindow
2418 * @returns TRUE/FALSE = not completed/completed
2420 * Initiated from start, loader done and item remove
2422 * On first entry generates di->MD5SUM, di->dimensions and sim data,
2423 * and updates the cache.
2425 static gboolean dupe_check_cb(gpointer data)
2427 DupeWindow *dw = static_cast<DupeWindow *>(data);
2428 DupeSearchMatch *search_match_list_item;
2435 if (!dw->setup_done) /* Clear on 1st entry */
2439 if (create_checksums_dimensions(dw, dw->list))
2444 if (dw->second_list)
2446 if (create_checksums_dimensions(dw, dw->second_list))
2451 if ((dw->match_mask & DUPE_MATCH_SIM_HIGH ||
2452 dw->match_mask & DUPE_MATCH_SIM_MED ||
2453 dw->match_mask & DUPE_MATCH_SIM_LOW ||
2454 dw->match_mask & DUPE_MATCH_SIM_CUSTOM) &&
2455 !(dw->setup_mask & DUPE_MATCH_SIM_MED) )
2457 /* Similarity only */
2458 if (!dw->setup_point) dw->setup_point = dw->list;
2460 while (dw->setup_point)
2462 DupeItem *di = static_cast<DupeItem *>(dw->setup_point->data);
2466 dupe_window_update_progress(dw, _("Reading similarity data..."),
2467 dw->setup_count == 0 ? 0.0 : (gdouble)dw->setup_n / dw->setup_count, FALSE);
2469 if (options->thumbnails.enable_caching)
2471 dupe_item_read_cache(di);
2472 if (cache_sim_data_filled(di->simd))
2474 image_sim_alternate_processing(di->simd);
2479 dw->img_loader = image_loader_new(di->fd);
2480 image_loader_set_buffer_size(dw->img_loader, 8);
2481 g_signal_connect(G_OBJECT(dw->img_loader), "error", (GCallback)dupe_loader_done_cb, dw);
2482 g_signal_connect(G_OBJECT(dw->img_loader), "done", (GCallback)dupe_loader_done_cb, dw);
2484 if (!image_loader_start(dw->img_loader))
2486 image_sim_free(di->simd);
2487 di->simd = image_sim_new();
2488 image_loader_free(dw->img_loader);
2489 dw->img_loader = NULL;
2496 dw->setup_point = dupe_setup_point_step(dw, dw->setup_point);
2499 dw->setup_mask = static_cast<DupeMatchType>(dw->setup_mask | DUPE_MATCH_SIM_MED);
2500 dupe_setup_reset(dw);
2503 /* End of setup not done */
2504 dupe_window_update_progress(dw, _("Comparing..."), 0.0, FALSE);
2505 dw->setup_done = TRUE;
2506 dupe_setup_reset(dw);
2507 dw->setup_count = g_list_length(dw->list);
2510 /* Setup done - dw->working set to NULL below
2511 * Set before 1st entry: dw->working = g_list_last(dw->list)
2512 * Set before 1st entry: dw->setup_count = g_list_length(dw->list)
2516 /* Similarity check threads may still be running */
2517 if (dw->setup_count > 0 && (dw->match_mask == DUPE_MATCH_SIM_HIGH ||
2518 dw->match_mask == DUPE_MATCH_SIM_MED ||
2519 dw->match_mask == DUPE_MATCH_SIM_LOW ||
2520 dw->match_mask == DUPE_MATCH_SIM_CUSTOM))
2522 if( dw->thread_count < dw->queue_count)
2524 dupe_window_update_progress(dw, _("Comparing..."), 0.0, FALSE);
2529 if (dw->search_matches_sorted == NULL)
2531 dw->search_matches_sorted = g_list_sort(dw->search_matches, sort_func);
2532 dupe_setup_reset(dw);
2535 while (dw->search_matches_sorted)
2538 dupe_window_update_progress(dw, _("Sorting..."), 0.0, FALSE);
2539 search_match_list_item = static_cast<DupeSearchMatch *>(dw->search_matches_sorted->data);
2541 if (!dupe_match_link_exists(search_match_list_item->a, search_match_list_item->b))
2543 dupe_match_link(search_match_list_item->a, search_match_list_item->b, search_match_list_item->rank);
2546 dw->search_matches_sorted = dw->search_matches_sorted->next;
2548 if (dw->search_matches_sorted != NULL)
2553 g_list_free(dw->search_matches);
2554 dw->search_matches = NULL;
2555 g_list_free(dw->search_matches_sorted);
2556 dw->search_matches_sorted = NULL;
2557 dw->setup_count = 0;
2561 if (dw->setup_count > 0)
2563 dw->setup_count = 0;
2564 dupe_window_update_progress(dw, _("Sorting..."), 1.0, TRUE);
2570 dupe_window_update_progress(dw, NULL, 0.0, FALSE);
2572 dupe_match_rank(dw);
2573 dupe_window_update_count(dw, FALSE);
2575 dupe_listview_populate(dw);
2578 if (dw->show_thumbs) dupe_thumb_step(dw);
2580 widget_set_cursor(dw->listview, -1);
2586 /* Setup done - working */
2587 if (dw->match_mask == DUPE_MATCH_SIM_HIGH ||
2588 dw->match_mask == DUPE_MATCH_SIM_MED ||
2589 dw->match_mask == DUPE_MATCH_SIM_LOW ||
2590 dw->match_mask == DUPE_MATCH_SIM_CUSTOM)
2592 /* This is the similarity comparison */
2593 dupe_list_check_match(dw, (DupeItem *)dw->working->data, dw->working);
2594 dupe_window_update_progress(dw, _("Queuing..."), dw->setup_count == 0 ? 0.0 : (gdouble) dw->setup_n / dw->setup_count, FALSE);
2598 dw->working = dw->working->prev; /* Is NULL when complete */
2602 /* This is the comparison for all other parameters.
2603 * dupe_array_check() processes the entire list in one go
2606 dupe_window_update_progress(dw, _("Comparing..."), 0.0, FALSE);
2607 dupe_array_check(dw);
2613 static void dupe_check_start(DupeWindow *dw)
2615 dw->setup_done = FALSE;
2617 dw->setup_count = g_list_length(dw->list);
2618 if (dw->second_set) dw->setup_count += g_list_length(dw->second_list);
2620 dw->setup_mask = DUPE_MATCH_NONE;
2621 dupe_setup_reset(dw);
2623 dw->working = g_list_last(dw->list);
2625 dupe_window_update_count(dw, TRUE);
2626 widget_set_cursor(dw->listview, GDK_WATCH);
2627 dw->queue_count = 0;
2628 dw->thread_count = 0;
2629 dw->search_matches_sorted = NULL;
2632 if (dw->idle_id) return;
2634 dw->idle_id = g_idle_add(dupe_check_cb, dw);
2637 static gboolean dupe_check_start_cb(gpointer data)
2639 DupeWindow *dw = static_cast<DupeWindow *>(data);
2641 dupe_check_start(dw);
2647 * ------------------------------------------------------------------
2648 * Item addition, removal
2649 * ------------------------------------------------------------------
2652 static void dupe_item_remove(DupeWindow *dw, DupeItem *di)
2656 /* handle things that may be in progress... */
2657 if (dw->working && dw->working->data == di)
2659 dw->working = dw->working->prev;
2661 if (dw->thumb_loader && dw->thumb_item == di)
2663 dupe_thumb_step(dw);
2665 if (dw->setup_point && dw->setup_point->data == di)
2667 dw->setup_point = dupe_setup_point_step(dw, dw->setup_point);
2670 image_loader_free(dw->img_loader);
2671 dw->img_loader = NULL;
2672 dw->idle_id = g_idle_add(dupe_check_cb, dw);
2676 if (di->group && dw->dupes)
2678 /* is a dupe, must remove from group/reset children if a parent */
2681 parent = dupe_match_find_parent(dw, di);
2684 if (g_list_length(parent->group) < 2)
2688 child = dupe_match_highest_rank(parent);
2689 dupe_match_link_clear(child, TRUE);
2690 dupe_listview_remove(dw, child);
2692 dupe_match_link_clear(parent, TRUE);
2693 dupe_listview_remove(dw, parent);
2694 dw->dupes = g_list_remove(dw->dupes, parent);
2698 DupeItem *new_parent;
2701 dm = static_cast<DupeMatch *>(parent->group->data);
2702 new_parent = dm->di;
2703 dupe_match_reparent(dw, parent, new_parent);
2704 dupe_listview_remove(dw, parent);
2709 if (g_list_length(parent->group) < 2)
2711 dupe_match_link_clear(parent, TRUE);
2712 dupe_listview_remove(dw, parent);
2713 dw->dupes = g_list_remove(dw->dupes, parent);
2715 dupe_match_link_clear(di, TRUE);
2716 dupe_listview_remove(dw, di);
2721 /* not a dupe, or not sorted yet, simply reset */
2722 dupe_match_link_clear(di, TRUE);
2725 if (dw->second_list && g_list_find(dw->second_list, di))
2727 dupe_second_remove(dw, di);
2731 dw->list = g_list_remove(dw->list, di);
2735 dupe_window_update_count(dw, FALSE);
2739 static gboolean dupe_item_remove_by_path(DupeWindow *dw, const gchar *path)
2743 di = dupe_item_find_path(dw, path);
2744 if (!di) return FALSE;
2746 dupe_item_remove(dw, di);
2752 static gboolean dupe_files_add_queue_cb(gpointer data)
2754 DupeItem *di = NULL;
2755 DupeWindow *dw = static_cast<DupeWindow *>(data);
2757 GList *queue = dw->add_files_queue;
2759 gtk_progress_bar_pulse(GTK_PROGRESS_BAR(dw->extra_label));
2763 dw->add_files_queue_id = 0;
2764 dupe_destroy_list_cache(dw);
2765 g_idle_add(dupe_check_start_cb, dw);
2766 gtk_widget_set_sensitive(dw->controls_box, TRUE);
2770 fd = static_cast<FileData *>(queue->data);
2773 if (isfile(fd->path))
2775 di = dupe_item_new(fd);
2777 else if (isdir(fd->path))
2780 dw->add_files_queue = g_list_remove(dw->add_files_queue, g_list_first(dw->add_files_queue)->data);
2782 if (filelist_read(fd, &f, &d))
2784 f = filelist_filter(f, FALSE);
2785 d = filelist_filter(d, TRUE);
2787 dw->add_files_queue = g_list_concat(f, dw->add_files_queue);
2788 dw->add_files_queue = g_list_concat(d, dw->add_files_queue);
2793 /* Not a file and not a dir */
2794 dw->add_files_queue = g_list_remove(dw->add_files_queue, g_list_first(dw->add_files_queue)->data);
2800 /* A dir was found. Process the contents on next entry */
2804 dw->add_files_queue = g_list_remove(dw->add_files_queue, g_list_first(dw->add_files_queue)->data);
2806 dupe_item_read_cache(di);
2808 /* Ensure images in the lists have unique FileDatas */
2809 if (!dupe_insert_in_list_cache(dw, di->fd))
2815 if (dw->second_drop)
2817 dupe_second_add(dw, di);
2821 dw->list = g_list_prepend(dw->list, di);
2824 if (dw->add_files_queue != NULL)
2830 dw->add_files_queue_id = 0;
2831 dupe_destroy_list_cache(dw);
2832 g_idle_add(dupe_check_start_cb, dw);
2833 gtk_widget_set_sensitive(dw->controls_box, TRUE);
2838 static void dupe_files_add(DupeWindow *dw, CollectionData *UNUSED(collection), CollectInfo *info,
2839 FileData *fd, gboolean recurse)
2841 DupeItem *di = NULL;
2845 di = dupe_item_new(info->fd);
2849 if (isfile(fd->path) && !g_file_test(fd->path, G_FILE_TEST_IS_SYMLINK))
2851 di = dupe_item_new(fd);
2853 else if (isdir(fd->path) && recurse)
2856 if (filelist_read(fd, &f, &d))
2860 f = filelist_filter(f, FALSE);
2861 d = filelist_filter(d, TRUE);
2866 dupe_files_add(dw, NULL, NULL, (FileData *)work->data, TRUE);
2873 dupe_files_add(dw, NULL, NULL, (FileData *)work->data, TRUE);
2883 dupe_item_read_cache(di);
2885 /* Ensure images in the lists have unique FileDatas */
2888 work = g_list_first(dw->list);
2891 di_list = static_cast<DupeItem *>(work->data);
2892 if (di_list->fd == di->fd)
2902 if (dw->second_list)
2904 work = g_list_first(dw->second_list);
2907 di_list = static_cast<DupeItem *>(work->data);
2908 if (di_list->fd == di->fd)
2919 if (dw->second_drop)
2921 dupe_second_add(dw, di);
2925 dw->list = g_list_prepend(dw->list, di);
2929 static void dupe_init_list_cache(DupeWindow *dw)
2931 dw->list_cache = g_hash_table_new(g_direct_hash, g_direct_equal);
2932 dw->second_list_cache = g_hash_table_new(g_direct_hash, g_direct_equal);
2934 for (GList *i = dw->list; i != NULL; i = i->next)
2936 DupeItem *di = static_cast<DupeItem *>(i->data);
2938 g_hash_table_add(dw->list_cache, di->fd);
2941 for (GList *i = dw->second_list; i != NULL; i = i->next)
2943 DupeItem *di = static_cast<DupeItem *>(i->data);
2945 g_hash_table_add(dw->second_list_cache, di->fd);
2949 static void dupe_destroy_list_cache(DupeWindow *dw)
2951 g_hash_table_destroy(dw->list_cache);
2952 g_hash_table_destroy(dw->second_list_cache);
2956 * @brief Return true if the fd was not in the cache
2963 static gboolean dupe_insert_in_list_cache(DupeWindow *dw, FileData *fd)
2966 dw->second_drop ? dw->second_list_cache : dw->list_cache;
2967 /* We do this as a lookup + add as we don't want to overwrite
2968 items as that would leak the old value. */
2969 if (g_hash_table_lookup(table, fd) != NULL)
2971 return g_hash_table_add(table, fd);
2974 void dupe_window_add_collection(DupeWindow *dw, CollectionData *collection)
2978 info = collection_get_first(collection);
2981 dupe_files_add(dw, collection, info, NULL, FALSE);
2982 info = collection_next_by_info(collection, info);
2985 dupe_check_start(dw);
2988 void dupe_window_add_files(DupeWindow *dw, GList *list, gboolean recurse)
2995 FileData *fd = static_cast<FileData *>(work->data);
2997 if (isdir(fd->path) && !recurse)
3001 if (filelist_read(fd, &f, &d))
3008 /* Add only the files, ignore the dirs when no recurse */
3009 dw->add_files_queue = g_list_prepend(dw->add_files_queue, work_file->data);
3010 file_data_ref((FileData *)work_file->data);
3011 work_file = work_file->next;
3019 dw->add_files_queue = g_list_prepend(dw->add_files_queue, fd);
3023 if (dw->add_files_queue_id == 0)
3025 gtk_progress_bar_pulse(GTK_PROGRESS_BAR(dw->extra_label));
3026 gtk_progress_bar_set_pulse_step(GTK_PROGRESS_BAR(dw->extra_label), DUPE_PROGRESS_PULSE_STEP);
3027 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(dw->extra_label), _("Loading file list"));
3029 dupe_init_list_cache(dw);
3030 dw->add_files_queue_id = g_idle_add(dupe_files_add_queue_cb, dw);
3031 gtk_widget_set_sensitive(dw->controls_box, FALSE);
3035 static void dupe_item_update(DupeWindow *dw, DupeItem *di)
3037 if ( (dw->match_mask & DUPE_MATCH_NAME) || (dw->match_mask & DUPE_MATCH_PATH || (dw->match_mask & DUPE_MATCH_NAME_CI)) )
3039 /* only effects matches on name or path */
3041 FileData *fd = file_data_ref(di->fd);
3044 second = di->second;
3045 dupe_item_remove(dw, di);
3047 dw->second_drop = second;
3048 dupe_files_add(dw, NULL, NULL, fd, FALSE);
3049 dw->second_drop = FALSE;
3051 file_data_unref(fd);
3053 dupe_check_start(dw);
3057 GtkListStore *store;
3060 /* update the listview(s) */
3062 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(dw->listview)));
3063 row = dupe_listview_find_item(store, di, &iter);
3066 gtk_list_store_set(store, &iter,
3067 DUPE_COLUMN_NAME, di->fd->name,
3068 DUPE_COLUMN_PATH, di->fd->path, -1);
3071 if (dw->second_listview)
3073 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(dw->second_listview)));
3074 row = dupe_listview_find_item(store, di, &iter);
3077 gtk_list_store_set(store, &iter, 1, di->fd->path, -1);
3084 static void dupe_item_update_fd_in_list(DupeWindow *dw, FileData *fd, GList *work)
3088 DupeItem *di = static_cast<DupeItem *>(work->data);
3091 dupe_item_update(dw, di);
3097 static void dupe_item_update_fd(DupeWindow *dw, FileData *fd)
3099 dupe_item_update_fd_in_list(dw, fd, dw->list);
3100 if (dw->second_set) dupe_item_update_fd_in_list(dw, fd, dw->second_list);
3105 * ------------------------------------------------------------------
3107 * ------------------------------------------------------------------
3110 static GtkWidget *dupe_display_label(GtkWidget *vbox, const gchar *description, const gchar *text)
3115 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
3117 label = gtk_label_new(description);
3118 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
3119 gtk_widget_show(label);
3121 label = gtk_label_new(text);
3122 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
3123 gtk_widget_show(label);
3125 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
3126 gtk_widget_show(hbox);
3131 static void dupe_display_stats(DupeWindow *dw, DupeItem *di)
3138 gd = file_util_gen_dlg("Image thumbprint debug info", "thumbprint",
3141 generic_dialog_add_button(gd, GTK_STOCK_CLOSE, NULL, NULL, TRUE);
3143 dupe_display_label(gd->vbox, "name:", di->fd->name);
3144 buf = text_from_size(di->fd->size);
3145 dupe_display_label(gd->vbox, "size:", buf);
3147 dupe_display_label(gd->vbox, "date:", text_from_time(di->fd->date));
3148 buf = g_strdup_printf("%d x %d", di->width, di->height);
3149 dupe_display_label(gd->vbox, "dimensions:", buf);
3151 dupe_display_label(gd->vbox, "md5sum:", (di->md5sum) ? di->md5sum : "not generated");
3153 dupe_display_label(gd->vbox, "thumbprint:", (di->simd) ? "" : "not generated");
3164 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, 32, 32);
3165 rs = gdk_pixbuf_get_rowstride(pixbuf);
3166 d_pix = gdk_pixbuf_get_pixels(pixbuf);
3168 for (y = 0; y < 32; y++)
3170 dp = d_pix + (y * rs);
3172 for (x = 0; x < 32; x++)
3174 *(dp++) = di->simd->avg_r[sp + x];
3175 *(dp++) = di->simd->avg_g[sp + x];
3176 *(dp++) = di->simd->avg_b[sp + x];
3180 image = gtk_image_new_from_pixbuf(pixbuf);
3181 gtk_box_pack_start(GTK_BOX(gd->vbox), image, FALSE, FALSE, 0);
3182 gtk_widget_show(image);
3184 g_object_unref(pixbuf);
3187 gtk_widget_show(gd->dialog);
3190 static void dupe_window_recompare(DupeWindow *dw)
3192 GtkListStore *store;
3194 dupe_check_stop(dw);
3196 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(dw->listview)));
3197 gtk_list_store_clear(store);
3199 g_list_free(dw->dupes);
3202 dupe_match_reset_list(dw->list);
3203 dupe_match_reset_list(dw->second_list);
3206 dupe_check_start(dw);
3209 static void dupe_menu_view(DupeWindow *dw, DupeItem *di, GtkWidget *listview, gint new_window)
3213 if (di->collection && collection_info_valid(di->collection, di->info))
3217 view_window_new_from_collection(di->collection, di->info);
3221 layout_image_set_collection(NULL, di->collection, di->info);
3230 list = dupe_listview_get_selection(dw, listview);
3231 view_window_new_from_list(list);
3232 filelist_free(list);
3236 layout_set_fd(NULL, di->fd);
3241 static void dupe_window_remove_selection(DupeWindow *dw, GtkWidget *listview)
3243 GtkTreeSelection *selection;
3244 GtkTreeModel *store;
3250 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(listview));
3251 slist = gtk_tree_selection_get_selected_rows(selection, &store);
3255 GtkTreePath *tpath = static_cast<GtkTreePath *>(work->data);
3256 DupeItem *di = NULL;
3258 gtk_tree_model_get_iter(store, &iter, tpath);
3259 gtk_tree_model_get(store, &iter, DUPE_COLUMN_POINTER, &di, -1);
3260 if (di) list = g_list_prepend(list, di);
3263 g_list_foreach(slist, (GFunc)tree_path_free_wrapper, NULL);
3266 dw->color_frozen = TRUE;
3272 di = static_cast<DupeItem *>(work->data);
3274 dupe_item_remove(dw, di);
3276 dw->color_frozen = FALSE;
3280 dupe_listview_realign_colors(dw);
3283 static void dupe_window_edit_selected(DupeWindow *dw, const gchar *key)
3285 file_util_start_editor_from_filelist(key, dupe_listview_get_selection(dw, dw->listview), NULL, dw->window);
3288 static void dupe_window_collection_from_selection(DupeWindow *dw)
3293 list = dupe_listview_get_selection(dw, dw->listview);
3294 w = collection_window_new(NULL);
3295 collection_table_add_filelist(w->table, list);
3296 filelist_free(list);
3299 static void dupe_window_append_file_list(DupeWindow *dw, gint on_second)
3303 dw->second_drop = (dw->second_set && on_second);
3305 list = layout_list(NULL);
3306 dupe_window_add_files(dw, list, FALSE);
3307 filelist_free(list);
3311 *-------------------------------------------------------------------
3312 * main pop-up menu callbacks
3313 *-------------------------------------------------------------------
3316 static void dupe_menu_view_cb(GtkWidget *UNUSED(widget), gpointer data)
3318 DupeWindow *dw = static_cast<DupeWindow *>(data);
3320 if (dw->click_item) dupe_menu_view(dw, dw->click_item, dw->listview, FALSE);
3323 static void dupe_menu_viewnew_cb(GtkWidget *UNUSED(widget), gpointer data)
3325 DupeWindow *dw = static_cast<DupeWindow *>(data);
3327 if (dw->click_item) dupe_menu_view(dw, dw->click_item, dw->listview, TRUE);
3330 static void dupe_menu_select_all_cb(GtkWidget *UNUSED(widget), gpointer data)
3332 DupeWindow *dw = static_cast<DupeWindow *>(data);
3333 GtkTreeSelection *selection;
3335 options->duplicates_select_type = DUPE_SELECT_NONE;
3336 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dw->listview));
3337 gtk_tree_selection_select_all(selection);
3340 static void dupe_menu_select_none_cb(GtkWidget *UNUSED(widget), gpointer data)
3342 DupeWindow *dw = static_cast<DupeWindow *>(data);
3343 GtkTreeSelection *selection;
3345 options->duplicates_select_type = DUPE_SELECT_NONE;
3346 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dw->listview));
3347 gtk_tree_selection_unselect_all(selection);
3350 static void dupe_menu_select_dupes_set1_cb(GtkWidget *UNUSED(widget), gpointer data)
3352 DupeWindow *dw = static_cast<DupeWindow *>(data);
3354 options->duplicates_select_type = DUPE_SELECT_GROUP1;
3355 dupe_listview_select_dupes(dw, DUPE_SELECT_GROUP1);
3358 static void dupe_menu_select_dupes_set2_cb(GtkWidget *UNUSED(widget), gpointer data)
3360 DupeWindow *dw = static_cast<DupeWindow *>(data);
3362 options->duplicates_select_type = DUPE_SELECT_GROUP2;
3363 dupe_listview_select_dupes(dw, DUPE_SELECT_GROUP2);
3366 static void dupe_menu_edit_cb(GtkWidget *widget, gpointer data)
3369 const gchar *key = static_cast<const gchar *>(data);
3371 dw = static_cast<DupeWindow *>(submenu_item_get_data(widget));
3374 dupe_window_edit_selected(dw, key);
3377 static void dupe_menu_print_cb(GtkWidget *UNUSED(widget), gpointer data)
3379 DupeWindow *dw = static_cast<DupeWindow *>(data);
3382 fd = (dw->click_item) ? dw->click_item->fd : NULL;
3384 print_window_new(fd,
3385 dupe_listview_get_selection(dw, dw->listview),
3386 dupe_listview_get_filelist(dw, dw->listview), dw->window);
3389 static void dupe_menu_copy_cb(GtkWidget *UNUSED(widget), gpointer data)
3391 DupeWindow *dw = static_cast<DupeWindow *>(data);
3393 file_util_copy(NULL, dupe_listview_get_selection(dw, dw->listview), NULL, dw->window);
3396 static void dupe_menu_move_cb(GtkWidget *UNUSED(widget), gpointer data)
3398 DupeWindow *dw = static_cast<DupeWindow *>(data);
3400 file_util_move(NULL, dupe_listview_get_selection(dw, dw->listview), NULL, dw->window);
3403 static void dupe_menu_rename_cb(GtkWidget *UNUSED(widget), gpointer data)
3405 DupeWindow *dw = static_cast<DupeWindow *>(data);
3407 file_util_rename(NULL, dupe_listview_get_selection(dw, dw->listview), dw->window);
3410 static void dupe_menu_delete_cb(GtkWidget *UNUSED(widget), gpointer data)
3412 DupeWindow *dw = static_cast<DupeWindow *>(data);
3414 options->file_ops.safe_delete_enable = FALSE;
3415 file_util_delete_notify_done(NULL, dupe_listview_get_selection(dw, dw->listview), dw->window, delete_finished_cb, dw);
3418 static void dupe_menu_move_to_trash_cb(GtkWidget *UNUSED(widget), gpointer data)
3420 DupeWindow *dw = static_cast<DupeWindow *>(data);
3422 options->file_ops.safe_delete_enable = TRUE;
3423 file_util_delete_notify_done(NULL, dupe_listview_get_selection(dw, dw->listview), dw->window, delete_finished_cb, dw);
3426 static void dupe_menu_copy_path_cb(GtkWidget *UNUSED(widget), gpointer data)
3428 DupeWindow *dw = static_cast<DupeWindow *>(data);
3430 file_util_copy_path_list_to_clipboard(dupe_listview_get_selection(dw, dw->listview), TRUE);
3433 static void dupe_menu_copy_path_unquoted_cb(GtkWidget *UNUSED(widget), gpointer data)
3435 DupeWindow *dw = static_cast<DupeWindow *>(data);
3437 file_util_copy_path_list_to_clipboard(dupe_listview_get_selection(dw, dw->listview), FALSE);
3440 static void dupe_menu_remove_cb(GtkWidget *UNUSED(widget), gpointer data)
3442 DupeWindow *dw = static_cast<DupeWindow *>(data);
3444 dupe_window_remove_selection(dw, dw->listview);
3447 static void dupe_menu_clear_cb(GtkWidget *UNUSED(widget), gpointer data)
3449 DupeWindow *dw = static_cast<DupeWindow *>(data);
3451 dupe_window_clear(dw);
3454 static void dupe_menu_close_cb(GtkWidget *UNUSED(widget), gpointer data)
3456 DupeWindow *dw = static_cast<DupeWindow *>(data);
3458 dupe_window_close(dw);
3461 static void dupe_menu_popup_destroy_cb(GtkWidget *UNUSED(widget), gpointer data)
3463 GList *editmenu_fd_list = static_cast<GList *>(data);
3465 filelist_free(editmenu_fd_list);
3468 static GList *dupe_window_get_fd_list(DupeWindow *dw)
3472 if (gtk_widget_has_focus(dw->second_listview))
3474 list = dupe_listview_get_selection(dw, dw->second_listview);
3478 list = dupe_listview_get_selection(dw, dw->listview);
3485 * @brief Add file selection list to a collection
3487 * @param[in] data Index to the collection list menu item selected, or -1 for new collection
3491 static void dupe_pop_menu_collections_cb(GtkWidget *widget, gpointer data)
3494 GList *selection_list;
3496 dw = static_cast<DupeWindow *>(submenu_item_get_data(widget));
3497 selection_list = dupe_listview_get_selection(dw, dw->listview);
3498 pop_menu_collections(selection_list, data);
3500 filelist_free(selection_list);
3503 static GtkWidget *dupe_menu_popup_main(DupeWindow *dw, DupeItem *di)
3508 GList *editmenu_fd_list;
3509 GtkAccelGroup *accel_group;
3511 on_row = (di != NULL);
3513 menu = popup_menu_short_lived();
3515 accel_group = gtk_accel_group_new();
3516 gtk_menu_set_accel_group(GTK_MENU(menu), accel_group);
3518 g_object_set_data(G_OBJECT(menu), "window_keys", dupe_window_keys);
3519 g_object_set_data(G_OBJECT(menu), "accel_group", accel_group);
3521 menu_item_add_sensitive(menu, _("_View"), on_row,
3522 G_CALLBACK(dupe_menu_view_cb), dw);
3523 menu_item_add_stock_sensitive(menu, _("View in _new window"), GTK_STOCK_NEW, on_row,
3524 G_CALLBACK(dupe_menu_viewnew_cb), dw);
3525 menu_item_add_divider(menu);
3526 menu_item_add_sensitive(menu, _("Select all"), (dw->dupes != NULL),
3527 G_CALLBACK(dupe_menu_select_all_cb), dw);
3528 menu_item_add_sensitive(menu, _("Select none"), (dw->dupes != NULL),
3529 G_CALLBACK(dupe_menu_select_none_cb), dw);
3530 menu_item_add_sensitive(menu, _("Select group _1 duplicates"), (dw->dupes != NULL),
3531 G_CALLBACK(dupe_menu_select_dupes_set1_cb), dw);
3532 menu_item_add_sensitive(menu, _("Select group _2 duplicates"), (dw->dupes != NULL),
3533 G_CALLBACK(dupe_menu_select_dupes_set2_cb), dw);
3534 menu_item_add_divider(menu);
3536 submenu_add_export(menu, &item, G_CALLBACK(dupe_pop_menu_export_cb), dw);
3537 gtk_widget_set_sensitive(item, on_row);
3538 menu_item_add_divider(menu);
3540 editmenu_fd_list = dupe_window_get_fd_list(dw);
3541 g_signal_connect(G_OBJECT(menu), "destroy",
3542 G_CALLBACK(dupe_menu_popup_destroy_cb), editmenu_fd_list);
3543 submenu_add_edit(menu, &item, G_CALLBACK(dupe_menu_edit_cb), dw, editmenu_fd_list);
3544 if (!on_row) gtk_widget_set_sensitive(item, FALSE);
3546 submenu_add_collections(menu, &item,
3547 G_CALLBACK(dupe_pop_menu_collections_cb), dw);
3548 gtk_widget_set_sensitive(item, on_row);
3550 menu_item_add_stock_sensitive(menu, _("Print..."), GTK_STOCK_PRINT, on_row,
3551 G_CALLBACK(dupe_menu_print_cb), dw);
3552 menu_item_add_divider(menu);
3553 menu_item_add_stock_sensitive(menu, _("_Copy..."), GTK_STOCK_COPY, on_row,
3554 G_CALLBACK(dupe_menu_copy_cb), dw);
3555 menu_item_add_sensitive(menu, _("_Move..."), on_row,
3556 G_CALLBACK(dupe_menu_move_cb), dw);
3557 menu_item_add_sensitive(menu, _("_Rename..."), on_row,
3558 G_CALLBACK(dupe_menu_rename_cb), dw);
3559 menu_item_add_sensitive(menu, _("_Copy path"), on_row,
3560 G_CALLBACK(dupe_menu_copy_path_cb), dw);
3561 menu_item_add_sensitive(menu, _("_Copy path unquoted"), on_row,
3562 G_CALLBACK(dupe_menu_copy_path_unquoted_cb), dw);
3564 menu_item_add_divider(menu);
3565 menu_item_add_stock_sensitive(menu,
3566 options->file_ops.confirm_move_to_trash ? _("Move to Trash...") :
3567 _("Move to Trash"), PIXBUF_INLINE_ICON_TRASH, on_row,
3568 G_CALLBACK(dupe_menu_move_to_trash_cb), dw);
3569 menu_item_add_stock_sensitive(menu,
3570 options->file_ops.confirm_delete ? _("_Delete...") :
3571 _("_Delete"), GTK_STOCK_DELETE, on_row,
3572 G_CALLBACK(dupe_menu_delete_cb), dw);
3574 menu_item_add_divider(menu);
3575 menu_item_add_stock_sensitive(menu, _("Rem_ove"), GTK_STOCK_REMOVE, on_row,
3576 G_CALLBACK(dupe_menu_remove_cb), dw);
3577 menu_item_add_stock_sensitive(menu, _("C_lear"), GTK_STOCK_CLEAR, (dw->list != NULL),
3578 G_CALLBACK(dupe_menu_clear_cb), dw);
3579 menu_item_add_divider(menu);
3580 menu_item_add_stock(menu, _("Close _window"), GTK_STOCK_CLOSE,
3581 G_CALLBACK(dupe_menu_close_cb), dw);
3586 static gboolean dupe_listview_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
3588 DupeWindow *dw = static_cast<DupeWindow *>(data);
3589 GtkTreeModel *store;
3592 DupeItem *di = NULL;
3594 store = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
3596 if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), bevent->x, bevent->y,
3597 &tpath, NULL, NULL, NULL))
3599 gtk_tree_model_get_iter(store, &iter, tpath);
3600 gtk_tree_model_get(store, &iter, DUPE_COLUMN_POINTER, &di, -1);
3601 gtk_tree_path_free(tpath);
3604 dw->click_item = di;
3606 if (bevent->button == MOUSE_BUTTON_RIGHT)
3608 /* right click menu */
3611 if (bevent->state & GDK_CONTROL_MASK && bevent->state & GDK_SHIFT_MASK)
3613 dupe_display_stats(dw, di);
3616 if (widget == dw->listview)
3618 menu = dupe_menu_popup_main(dw, di);
3622 menu = dupe_menu_popup_second(dw, di);
3624 gtk_menu_popup_at_pointer(GTK_MENU(menu), NULL);
3627 if (!di) return FALSE;
3629 if (bevent->button == MOUSE_BUTTON_LEFT &&
3630 bevent->type == GDK_2BUTTON_PRESS)
3632 dupe_menu_view(dw, di, widget, FALSE);
3635 if (bevent->button == MOUSE_BUTTON_MIDDLE) return TRUE;
3637 if (bevent->button == MOUSE_BUTTON_RIGHT)
3639 if (!dupe_listview_item_is_selected(dw, di, widget))
3641 GtkTreeSelection *selection;
3643 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
3644 gtk_tree_selection_unselect_all(selection);
3645 gtk_tree_selection_select_iter(selection, &iter);
3647 tpath = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &iter);
3648 gtk_tree_view_set_cursor(GTK_TREE_VIEW(widget), tpath, NULL, FALSE);
3649 gtk_tree_path_free(tpath);
3655 if (bevent->button == MOUSE_BUTTON_LEFT &&
3656 bevent->type == GDK_BUTTON_PRESS &&
3657 !(bevent->state & GDK_SHIFT_MASK ) &&
3658 !(bevent->state & GDK_CONTROL_MASK ) &&
3659 dupe_listview_item_is_selected(dw, di, widget))
3661 /* this selection handled on release_cb */
3662 gtk_widget_grab_focus(widget);
3669 static gboolean dupe_listview_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
3671 DupeWindow *dw = static_cast<DupeWindow *>(data);
3672 GtkTreeModel *store;
3675 DupeItem *di = NULL;
3677 if (bevent->button != MOUSE_BUTTON_LEFT && bevent->button != MOUSE_BUTTON_MIDDLE) return TRUE;
3679 store = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
3681 if ((bevent->x != 0 || bevent->y != 0) &&
3682 gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), bevent->x, bevent->y,
3683 &tpath, NULL, NULL, NULL))
3685 gtk_tree_model_get_iter(store, &iter, tpath);
3686 gtk_tree_model_get(store, &iter, DUPE_COLUMN_POINTER, &di, -1);
3687 gtk_tree_path_free(tpath);
3690 if (bevent->button == MOUSE_BUTTON_MIDDLE)
3692 if (di && dw->click_item == di)
3694 GtkTreeSelection *selection;
3696 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
3697 if (dupe_listview_item_is_selected(dw, di, widget))
3699 gtk_tree_selection_unselect_iter(selection, &iter);
3703 gtk_tree_selection_select_iter(selection, &iter);
3709 if (di && dw->click_item == di &&
3710 !(bevent->state & GDK_SHIFT_MASK ) &&
3711 !(bevent->state & GDK_CONTROL_MASK ) &&
3712 dupe_listview_item_is_selected(dw, di, widget))
3714 GtkTreeSelection *selection;
3716 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
3717 gtk_tree_selection_unselect_all(selection);
3718 gtk_tree_selection_select_iter(selection, &iter);
3720 tpath = gtk_tree_model_get_path(store, &iter);
3721 gtk_tree_view_set_cursor(GTK_TREE_VIEW(widget), tpath, NULL, FALSE);
3722 gtk_tree_path_free(tpath);
3731 *-------------------------------------------------------------------
3733 *-------------------------------------------------------------------
3736 static void dupe_second_update_status(DupeWindow *dw)
3740 buf = g_strdup_printf(_("%d files (set 2)"), g_list_length(dw->second_list));
3741 gtk_label_set_text(GTK_LABEL(dw->second_status_label), buf);
3745 static void dupe_second_add(DupeWindow *dw, DupeItem *di)
3747 GtkListStore *store;
3753 dw->second_list = g_list_prepend(dw->second_list, di);
3755 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(dw->second_listview)));
3756 gtk_list_store_append(store, &iter);
3757 gtk_list_store_set(store, &iter, DUPE_COLUMN_POINTER, di, 1, di->fd->path, -1);
3759 dupe_second_update_status(dw);
3762 static void dupe_second_remove(DupeWindow *dw, DupeItem *di)
3764 GtkListStore *store;
3767 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(dw->second_listview)));
3768 if (dupe_listview_find_item(store, di, &iter) >= 0)
3770 tree_view_move_cursor_away(GTK_TREE_VIEW(dw->second_listview), &iter, TRUE);
3771 gtk_list_store_remove(store, &iter);
3774 dw->second_list = g_list_remove(dw->second_list, di);
3776 dupe_second_update_status(dw);
3779 static void dupe_second_clear(DupeWindow *dw)
3781 GtkListStore *store;
3783 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(dw->second_listview)));
3784 gtk_list_store_clear(store);
3785 gtk_tree_view_columns_autosize(GTK_TREE_VIEW(dw->second_listview));
3787 g_list_free(dw->dupes);
3790 dupe_list_free(dw->second_list);
3791 dw->second_list = NULL;
3793 dupe_match_reset_list(dw->list);
3795 dupe_second_update_status(dw);
3798 static void dupe_second_menu_view_cb(GtkWidget *UNUSED(widget), gpointer data)
3800 DupeWindow *dw = static_cast<DupeWindow *>(data);
3802 if (dw->click_item) dupe_menu_view(dw, dw->click_item, dw->second_listview, FALSE);
3805 static void dupe_second_menu_viewnew_cb(GtkWidget *UNUSED(widget), gpointer data)
3807 DupeWindow *dw = static_cast<DupeWindow *>(data);
3809 if (dw->click_item) dupe_menu_view(dw, dw->click_item, dw->second_listview, TRUE);
3812 static void dupe_second_menu_select_all_cb(GtkWidget *UNUSED(widget), gpointer data)
3814 GtkTreeSelection *selection;
3815 DupeWindow *dw = static_cast<DupeWindow *>(data);
3817 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dw->second_listview));
3818 gtk_tree_selection_select_all(selection);
3821 static void dupe_second_menu_select_none_cb(GtkWidget *UNUSED(widget), gpointer data)
3823 GtkTreeSelection *selection;
3824 DupeWindow *dw = static_cast<DupeWindow *>(data);
3826 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dw->second_listview));
3827 gtk_tree_selection_unselect_all(selection);
3830 static void dupe_second_menu_remove_cb(GtkWidget *UNUSED(widget), gpointer data)
3832 DupeWindow *dw = static_cast<DupeWindow *>(data);
3834 dupe_window_remove_selection(dw, dw->second_listview);
3837 static void dupe_second_menu_clear_cb(GtkWidget *UNUSED(widget), gpointer data)
3839 DupeWindow *dw = static_cast<DupeWindow *>(data);
3841 dupe_second_clear(dw);
3842 dupe_window_recompare(dw);
3845 static GtkWidget *dupe_menu_popup_second(DupeWindow *dw, DupeItem *di)
3848 gboolean notempty = (dw->second_list != NULL);
3849 gboolean on_row = (di != NULL);
3850 GtkAccelGroup *accel_group;
3852 menu = popup_menu_short_lived();
3853 accel_group = gtk_accel_group_new();
3854 gtk_menu_set_accel_group(GTK_MENU(menu), accel_group);
3856 g_object_set_data(G_OBJECT(menu), "window_keys", dupe_window_keys);
3857 g_object_set_data(G_OBJECT(menu), "accel_group", accel_group);
3859 menu_item_add_sensitive(menu, _("_View"), on_row,
3860 G_CALLBACK(dupe_second_menu_view_cb), dw);
3861 menu_item_add_stock_sensitive(menu, _("View in _new window"), GTK_STOCK_NEW, on_row,
3862 G_CALLBACK(dupe_second_menu_viewnew_cb), dw);
3863 menu_item_add_divider(menu);
3864 menu_item_add_sensitive(menu, _("Select all"), notempty,
3865 G_CALLBACK(dupe_second_menu_select_all_cb), dw);
3866 menu_item_add_sensitive(menu, _("Select none"), notempty,
3867 G_CALLBACK(dupe_second_menu_select_none_cb), dw);
3868 menu_item_add_divider(menu);
3869 menu_item_add_stock_sensitive(menu, _("Rem_ove"), GTK_STOCK_REMOVE, on_row,
3870 G_CALLBACK(dupe_second_menu_remove_cb), dw);
3871 menu_item_add_stock_sensitive(menu, _("C_lear"), GTK_STOCK_CLEAR, notempty,
3872 G_CALLBACK(dupe_second_menu_clear_cb), dw);
3873 menu_item_add_divider(menu);
3874 menu_item_add_stock(menu, _("Close _window"), GTK_STOCK_CLOSE,
3875 G_CALLBACK(dupe_menu_close_cb), dw);
3880 static void dupe_second_set_toggle_cb(GtkWidget *widget, gpointer data)
3882 DupeWindow *dw = static_cast<DupeWindow *>(data);
3884 dw->second_set = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
3888 dupe_second_update_status(dw);
3889 gtk_table_set_col_spacings(GTK_TABLE(dw->table), PREF_PAD_GAP);
3890 gtk_widget_show(dw->second_vbox);
3894 gtk_table_set_col_spacings(GTK_TABLE(dw->table), 0);
3895 gtk_widget_hide(dw->second_vbox);
3896 dupe_second_clear(dw);
3899 dupe_window_recompare(dw);
3902 static void dupe_sort_totals_toggle_cb(GtkWidget *widget, gpointer data)
3904 DupeWindow *dw = static_cast<DupeWindow *>(data);
3906 options->sort_totals = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
3907 dupe_window_recompare(dw);
3912 *-------------------------------------------------------------------
3914 *-------------------------------------------------------------------
3918 DUPE_MENU_COLUMN_NAME = 0,
3919 DUPE_MENU_COLUMN_MASK
3922 static void dupe_listview_show_rank(GtkWidget *listview, gboolean rank);
3924 static void dupe_menu_type_cb(GtkWidget *combo, gpointer data)
3926 DupeWindow *dw = static_cast<DupeWindow *>(data);
3927 GtkTreeModel *store;
3930 store = gtk_combo_box_get_model(GTK_COMBO_BOX(combo));
3931 if (!gtk_combo_box_get_active_iter(GTK_COMBO_BOX(combo), &iter)) return;
3932 gtk_tree_model_get(store, &iter, DUPE_MENU_COLUMN_MASK, &dw->match_mask, -1);
3934 options->duplicates_match = dw->match_mask;
3936 if (dw->match_mask & (DUPE_MATCH_SIM_HIGH | DUPE_MATCH_SIM_MED | DUPE_MATCH_SIM_LOW | DUPE_MATCH_SIM_CUSTOM))
3938 dupe_listview_show_rank(dw->listview, TRUE);
3942 dupe_listview_show_rank(dw->listview, FALSE);
3944 dupe_window_recompare(dw);
3947 static void dupe_menu_add_item(GtkListStore *store, const gchar *text, DupeMatchType type, DupeWindow *dw)
3951 gtk_list_store_append(store, &iter);
3952 gtk_list_store_set(store, &iter, DUPE_MENU_COLUMN_NAME, text,
3953 DUPE_MENU_COLUMN_MASK, type, -1);
3955 if (dw->match_mask == type) gtk_combo_box_set_active_iter(GTK_COMBO_BOX(dw->combo), &iter);
3958 static void dupe_menu_setup(DupeWindow *dw)
3960 GtkListStore *store;
3961 GtkCellRenderer *renderer;
3963 store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT);
3964 dw->combo = gtk_combo_box_new_with_model(GTK_TREE_MODEL(store));
3965 g_object_unref(store);
3967 renderer = gtk_cell_renderer_text_new();
3968 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(dw->combo), renderer, TRUE);
3969 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(dw->combo), renderer,
3970 "text", DUPE_MENU_COLUMN_NAME, NULL);
3972 dupe_menu_add_item(store, _("Name"), DUPE_MATCH_NAME, dw);
3973 dupe_menu_add_item(store, _("Name case-insensitive"), DUPE_MATCH_NAME_CI, dw);
3974 dupe_menu_add_item(store, _("Size"), DUPE_MATCH_SIZE, dw);
3975 dupe_menu_add_item(store, _("Date"), DUPE_MATCH_DATE, dw);
3976 dupe_menu_add_item(store, _("Dimensions"), DUPE_MATCH_DIM, dw);
3977 dupe_menu_add_item(store, _("Checksum"), DUPE_MATCH_SUM, dw);
3978 dupe_menu_add_item(store, _("Path"), DUPE_MATCH_PATH, dw);
3979 dupe_menu_add_item(store, _("Similarity (high - 95)"), DUPE_MATCH_SIM_HIGH, dw);
3980 dupe_menu_add_item(store, _("Similarity (med. - 90)"), DUPE_MATCH_SIM_MED, dw);
3981 dupe_menu_add_item(store, _("Similarity (low - 85)"), DUPE_MATCH_SIM_LOW, dw);
3982 dupe_menu_add_item(store, _("Similarity (custom)"), DUPE_MATCH_SIM_CUSTOM, dw);
3983 dupe_menu_add_item(store, _("Name ≠content"), DUPE_MATCH_NAME_CONTENT, dw);
3984 dupe_menu_add_item(store, _("Name case-insensitive ≠content"), DUPE_MATCH_NAME_CI_CONTENT, dw);
3985 dupe_menu_add_item(store, _("Show all"), DUPE_MATCH_ALL, dw);
3987 g_signal_connect(G_OBJECT(dw->combo), "changed",
3988 G_CALLBACK(dupe_menu_type_cb), dw);
3992 *-------------------------------------------------------------------
3994 *-------------------------------------------------------------------
3997 /* this overrides the low default of a GtkCellRenderer from 100 to CELL_HEIGHT_OVERRIDE, something sane for our purposes */
3999 #define CELL_HEIGHT_OVERRIDE 512
4001 void cell_renderer_height_override(GtkCellRenderer *renderer)
4005 spec = g_object_class_find_property(G_OBJECT_GET_CLASS(G_OBJECT(renderer)), "height");
4006 if (spec && G_IS_PARAM_SPEC_INT(spec))
4008 GParamSpecInt *spec_int;
4010 spec_int = G_PARAM_SPEC_INT(spec);
4011 if (spec_int->maximum < CELL_HEIGHT_OVERRIDE) spec_int->maximum = CELL_HEIGHT_OVERRIDE;
4015 static GdkColor *dupe_listview_color_shifted(GtkWidget *widget)
4017 static GdkColor color;
4018 static GtkWidget *done = NULL;
4024 style = gtk_widget_get_style(widget);
4025 memcpy(&color, &style->base[GTK_STATE_NORMAL], sizeof(color));
4026 shift_color(&color, -1, 0);
4033 static void dupe_listview_color_cb(GtkTreeViewColumn *UNUSED(tree_column), GtkCellRenderer *cell,
4034 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
4036 DupeWindow *dw = static_cast<DupeWindow *>(data);
4039 gtk_tree_model_get(tree_model, iter, DUPE_COLUMN_COLOR, &set, -1);
4040 g_object_set(G_OBJECT(cell),
4041 "cell-background-gdk", dupe_listview_color_shifted(dw->listview),
4042 "cell-background-set", set, NULL);
4045 static void dupe_listview_add_column(DupeWindow *dw, GtkWidget *listview, gint n, const gchar *title, gboolean image, gboolean right_justify)
4047 GtkTreeViewColumn *column;
4048 GtkCellRenderer *renderer;
4050 column = gtk_tree_view_column_new();
4051 gtk_tree_view_column_set_title(column, title);
4052 gtk_tree_view_column_set_min_width(column, 4);
4053 gtk_tree_view_column_set_sort_column_id(column, n);
4055 if (n != DUPE_COLUMN_RANK &&
4056 n != DUPE_COLUMN_THUMB)
4058 gtk_tree_view_column_set_resizable(column, TRUE);
4063 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_GROW_ONLY);
4064 renderer = gtk_cell_renderer_text_new();
4067 g_object_set(G_OBJECT(renderer), "xalign", 1.0, NULL);
4069 gtk_tree_view_column_pack_start(column, renderer, TRUE);
4070 gtk_tree_view_column_add_attribute(column, renderer, "text", n);
4074 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
4075 renderer = gtk_cell_renderer_pixbuf_new();
4076 cell_renderer_height_override(renderer);
4077 gtk_tree_view_column_pack_start(column, renderer, TRUE);
4078 gtk_tree_view_column_add_attribute(column, renderer, "pixbuf", n);
4081 if (listview == dw->listview)
4083 /* sets background before rendering */
4084 gtk_tree_view_column_set_cell_data_func(column, renderer, dupe_listview_color_cb, dw, NULL);
4087 gtk_tree_view_append_column(GTK_TREE_VIEW(listview), column);
4090 static void dupe_listview_set_height(GtkWidget *listview, gboolean thumb)
4092 GtkTreeViewColumn *column;
4093 GtkCellRenderer *cell;
4096 column = gtk_tree_view_get_column(GTK_TREE_VIEW(listview), DUPE_COLUMN_THUMB - 1);
4097 if (!column) return;
4099 gtk_tree_view_column_set_fixed_width(column, (thumb) ? options->thumbnails.max_width : 4);
4100 gtk_tree_view_column_set_visible(column, thumb);
4102 list = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(column));
4104 cell = static_cast<GtkCellRenderer *>(list->data);
4107 g_object_set(G_OBJECT(cell), "height", (thumb) ? options->thumbnails.max_height : -1, NULL);
4108 gtk_tree_view_columns_autosize(GTK_TREE_VIEW(listview));
4111 static void dupe_listview_show_rank(GtkWidget *listview, gboolean rank)
4113 GtkTreeViewColumn *column;
4115 column = gtk_tree_view_get_column(GTK_TREE_VIEW(listview), DUPE_COLUMN_RANK - 1);
4116 if (!column) return;
4118 gtk_tree_view_column_set_visible(column, rank);
4122 *-------------------------------------------------------------------
4124 *-------------------------------------------------------------------
4127 static void dupe_window_show_thumb_cb(GtkWidget *widget, gpointer data)
4129 DupeWindow *dw = static_cast<DupeWindow *>(data);
4131 dw->show_thumbs = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
4132 options->duplicates_thumbnails = dw->show_thumbs;
4134 if (dw->show_thumbs)
4136 if (!dw->working) dupe_thumb_step(dw);
4140 GtkTreeModel *store;
4144 thumb_loader_free(dw->thumb_loader);
4145 dw->thumb_loader = NULL;
4147 store = gtk_tree_view_get_model(GTK_TREE_VIEW(dw->listview));
4148 valid = gtk_tree_model_get_iter_first(store, &iter);
4152 gtk_list_store_set(GTK_LIST_STORE(store), &iter, DUPE_COLUMN_THUMB, NULL, -1);
4153 valid = gtk_tree_model_iter_next(store, &iter);
4155 dupe_window_update_progress(dw, NULL, 0.0, FALSE);
4158 dupe_listview_set_height(dw->listview, dw->show_thumbs);
4161 static void dupe_window_rotation_invariant_cb(GtkWidget *widget, gpointer data)
4163 DupeWindow *dw = static_cast<DupeWindow *>(data);
4165 options->rot_invariant_sim = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
4166 dupe_window_recompare(dw);
4169 static void dupe_window_custom_threshold_cb(GtkWidget *widget, gpointer data)
4171 DupeWindow *dw = static_cast<DupeWindow *>(data);
4172 DupeMatchType match_type;
4173 GtkTreeModel *store;
4177 options->duplicates_similarity_threshold = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget));
4178 dw->match_mask = DUPE_MATCH_SIM_CUSTOM;
4180 store = gtk_combo_box_get_model(GTK_COMBO_BOX(dw->combo));
4181 valid = gtk_tree_model_get_iter_first(store, &iter);
4184 gtk_tree_model_get(store, &iter, DUPE_MENU_COLUMN_MASK, &match_type, -1);
4185 if (match_type == DUPE_MATCH_SIM_CUSTOM)
4189 valid = gtk_tree_model_iter_next(store, &iter);
4192 gtk_combo_box_set_active_iter(GTK_COMBO_BOX(dw->combo), &iter);
4193 dupe_window_recompare(dw);
4196 static gboolean dupe_window_keypress_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
4198 DupeWindow *dw = static_cast<DupeWindow *>(data);
4199 gboolean stop_signal = FALSE;
4201 GtkWidget *listview;
4202 GtkTreeModel *store;
4203 GtkTreeSelection *selection;
4205 DupeItem *di = NULL;
4207 on_second = gtk_widget_has_focus(dw->second_listview);
4211 listview = dw->second_listview;
4215 listview = dw->listview;
4218 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(listview));
4219 slist = gtk_tree_selection_get_selected_rows(selection, &store);
4226 last = g_list_last(slist);
4227 tpath = static_cast<GtkTreePath *>(last->data);
4229 /* last is newest selected file */
4230 gtk_tree_model_get_iter(store, &iter, tpath);
4231 gtk_tree_model_get(store, &iter, DUPE_COLUMN_POINTER, &di, -1);
4233 g_list_foreach(slist, (GFunc)tree_path_free_wrapper, NULL);
4236 if (event->state & GDK_CONTROL_MASK)
4241 switch (event->keyval)
4255 file_util_copy(NULL, dupe_listview_get_selection(dw, listview),
4259 file_util_move(NULL, dupe_listview_get_selection(dw, listview),
4263 file_util_rename(NULL, dupe_listview_get_selection(dw, listview), dw->window);
4266 options->file_ops.safe_delete_enable = TRUE;
4267 file_util_delete(NULL, dupe_listview_get_selection(dw, listview), dw->window);
4270 stop_signal = FALSE;
4278 switch (event->keyval)
4281 if (event->state & GDK_SHIFT_MASK)
4283 gtk_tree_selection_unselect_all(selection);
4287 gtk_tree_selection_select_all(selection);
4290 case GDK_KEY_Delete: case GDK_KEY_KP_Delete:
4293 dupe_second_clear(dw);
4294 dupe_window_recompare(dw);
4298 dupe_window_clear(dw);
4302 dupe_window_append_file_list(dw, FALSE);
4305 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dw->button_thumbs),
4306 !gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dw->button_thumbs)));
4309 dupe_window_close(dw);
4312 stop_signal = FALSE;
4320 switch (event->keyval)
4322 case GDK_KEY_Return: case GDK_KEY_KP_Enter:
4323 dupe_menu_view(dw, di, listview, FALSE);
4326 dupe_menu_view(dw, di, listview, TRUE);
4328 case GDK_KEY_Delete: case GDK_KEY_KP_Delete:
4329 dupe_window_remove_selection(dw, listview);
4334 dupe_window_collection_from_selection(dw);
4338 options->duplicates_select_type = DUPE_SELECT_NONE;
4339 dupe_listview_select_dupes(dw, DUPE_SELECT_NONE);
4342 options->duplicates_select_type = DUPE_SELECT_GROUP1;
4343 dupe_listview_select_dupes(dw, DUPE_SELECT_GROUP1);
4346 options->duplicates_select_type = DUPE_SELECT_GROUP2;
4347 dupe_listview_select_dupes(dw, DUPE_SELECT_GROUP2);
4355 menu = dupe_menu_popup_main(dw, di);
4356 gtk_menu_popup_at_widget(GTK_MENU(menu), widget, GDK_GRAVITY_CENTER, GDK_GRAVITY_CENTER, NULL);
4362 menu = dupe_menu_popup_second(dw, di);
4363 gtk_menu_popup_at_widget(GTK_MENU(menu), widget, GDK_GRAVITY_CENTER, GDK_GRAVITY_CENTER, NULL);
4367 stop_signal = FALSE;
4371 if (!stop_signal && is_help_key(event))
4373 help_window_show("GuideImageSearchFindingDuplicates.html");
4381 void dupe_window_clear(DupeWindow *dw)
4383 GtkListStore *store;
4385 dupe_check_stop(dw);
4387 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(dw->listview)));
4388 gtk_list_store_clear(store);
4389 gtk_tree_view_columns_autosize(GTK_TREE_VIEW(dw->listview));
4391 g_list_free(dw->dupes);
4394 dupe_list_free(dw->list);
4398 dupe_match_reset_list(dw->second_list);
4400 dupe_window_update_count(dw, FALSE);
4401 dupe_window_update_progress(dw, NULL, 0.0, FALSE);
4404 static void dupe_window_get_geometry(DupeWindow *dw)
4407 LayoutWindow *lw = NULL;
4411 if (!dw || !lw) return;
4413 window = gtk_widget_get_window(dw->window);
4414 gdk_window_get_position(window, &lw->options.dupe_window.x, &lw->options.dupe_window.y);
4415 lw->options.dupe_window.w = gdk_window_get_width(window);
4416 lw->options.dupe_window.h = gdk_window_get_height(window);
4419 void dupe_window_close(DupeWindow *dw)
4421 dupe_check_stop(dw);
4423 dupe_window_get_geometry(dw);
4425 dupe_window_list = g_list_remove(dupe_window_list, dw);
4426 gtk_widget_destroy(dw->window);
4428 g_list_free(dw->dupes);
4429 dupe_list_free(dw->list);
4431 dupe_list_free(dw->second_list);
4433 file_data_unregister_notify_func(dupe_notify_cb, dw);
4435 g_thread_pool_free(dw->dupe_comparison_thread_pool, TRUE, TRUE);
4440 static gint dupe_window_close_cb(GtkWidget *UNUSED(widget), gpointer data)
4442 DupeWindow *dw = static_cast<DupeWindow *>(data);
4444 dupe_window_close(dw);
4449 static gint dupe_window_delete(GtkWidget *UNUSED(widget), GdkEvent *UNUSED(event), gpointer data)
4451 DupeWindow *dw = static_cast<DupeWindow *>(data);
4452 dupe_window_close(dw);
4457 static void dupe_help_cb(GtkAction *UNUSED(action), gpointer UNUSED(data))
4459 help_window_show("GuideImageSearchFindingDuplicates.html");
4462 static gint default_sort_cb(GtkTreeModel *UNUSED(model), GtkTreeIter *UNUSED(a), GtkTreeIter *UNUSED(b), gpointer UNUSED(data))
4467 static gint column_sort_cb(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b, gpointer data)
4469 GtkTreeSortable *sortable = static_cast<GtkTreeSortable *>(data);
4471 gchar *rank_str_a, *rank_str_b;
4476 gint sort_column_id;
4477 GtkSortType sort_order;
4481 gtk_tree_sortable_get_sort_column_id(sortable, &sort_column_id, &sort_order);
4483 gtk_tree_model_get(model, a, DUPE_COLUMN_RANK, &rank_str_a, DUPE_COLUMN_SET, &group_a, DUPE_COLUMN_POINTER, &di_a, -1);
4485 gtk_tree_model_get(model, b, DUPE_COLUMN_RANK, &rank_str_b, DUPE_COLUMN_SET, &group_b, DUPE_COLUMN_POINTER, &di_b, -1);
4487 if (group_a == group_b)
4489 switch (sort_column_id)
4491 case DUPE_COLUMN_NAME:
4492 ret = utf8_compare(di_a->fd->name, di_b->fd->name, TRUE);
4494 case DUPE_COLUMN_SIZE:
4495 if (di_a->fd->size == di_b->fd->size)
4501 ret = (di_a->fd->size > di_b->fd->size) ? 1 : -1;
4504 case DUPE_COLUMN_DATE:
4505 if (di_a->fd->date == di_b->fd->date)
4511 ret = (di_a->fd->date > di_b->fd->date) ? 1 : -1;
4514 case DUPE_COLUMN_DIMENSIONS:
4515 if ((di_a->width == di_b->width) && (di_a->height == di_b->height))
4521 ret = ((di_a->width * di_a->height) > (di_b->width * di_b->height)) ? 1 : -1;
4524 case DUPE_COLUMN_RANK:
4525 rank_int_a = atoi(rank_str_a);
4526 rank_int_b = atoi(rank_str_b);
4527 if (rank_int_a == 0) rank_int_a = 101;
4528 if (rank_int_b == 0) rank_int_b = 101;
4530 if (rank_int_a == rank_int_b)
4536 ret = (rank_int_a > rank_int_b) ? 1 : -1;
4539 case DUPE_COLUMN_PATH:
4540 ret = utf8_compare(di_a->fd->path, di_b->fd->path, TRUE);
4544 else if (group_a < group_b)
4546 ret = (sort_order == GTK_SORT_ASCENDING) ? 1 : -1;
4550 ret = (sort_order == GTK_SORT_ASCENDING) ? -1 : 1;
4556 static void column_clicked_cb(GtkWidget *UNUSED(widget), gpointer data)
4558 DupeWindow *dw = static_cast<DupeWindow *>(data);
4560 options->duplicates_match = DUPE_SELECT_NONE;
4561 dupe_listview_select_dupes(dw, DUPE_SELECT_NONE);
4564 /* collection and files can be NULL */
4565 DupeWindow *dupe_window_new()
4570 GtkWidget *scrolled;
4572 GtkWidget *status_box;
4573 GtkWidget *controls_box;
4574 GtkWidget *button_box;
4577 GtkListStore *store;
4578 GtkTreeSelection *selection;
4579 GdkGeometry geometry;
4580 LayoutWindow *lw = NULL;
4584 dw = g_new0(DupeWindow, 1);
4585 dw->add_files_queue = NULL;
4586 dw->add_files_queue_id = 0;
4588 dw->match_mask = DUPE_MATCH_NAME;
4589 if (options->duplicates_match == DUPE_MATCH_NAME) dw->match_mask = DUPE_MATCH_NAME;
4590 if (options->duplicates_match == DUPE_MATCH_SIZE) dw->match_mask = DUPE_MATCH_SIZE;
4591 if (options->duplicates_match == DUPE_MATCH_DATE) dw->match_mask = DUPE_MATCH_DATE;
4592 if (options->duplicates_match == DUPE_MATCH_DIM) dw->match_mask = DUPE_MATCH_DIM;
4593 if (options->duplicates_match == DUPE_MATCH_SUM) dw->match_mask = DUPE_MATCH_SUM;
4594 if (options->duplicates_match == DUPE_MATCH_PATH) dw->match_mask = DUPE_MATCH_PATH;
4595 if (options->duplicates_match == DUPE_MATCH_SIM_HIGH) dw->match_mask = DUPE_MATCH_SIM_HIGH;
4596 if (options->duplicates_match == DUPE_MATCH_SIM_MED) dw->match_mask = DUPE_MATCH_SIM_MED;
4597 if (options->duplicates_match == DUPE_MATCH_SIM_LOW) dw->match_mask = DUPE_MATCH_SIM_LOW;
4598 if (options->duplicates_match == DUPE_MATCH_SIM_CUSTOM) dw->match_mask = DUPE_MATCH_SIM_CUSTOM;
4599 if (options->duplicates_match == DUPE_MATCH_NAME_CI) dw->match_mask = DUPE_MATCH_NAME_CI;
4600 if (options->duplicates_match == DUPE_MATCH_NAME_CONTENT) dw->match_mask = DUPE_MATCH_NAME_CONTENT;
4601 if (options->duplicates_match == DUPE_MATCH_NAME_CI_CONTENT) dw->match_mask = DUPE_MATCH_NAME_CI_CONTENT;
4602 if (options->duplicates_match == DUPE_MATCH_ALL) dw->match_mask = DUPE_MATCH_ALL;
4604 dw->window = window_new(GTK_WINDOW_TOPLEVEL, "dupe", NULL, NULL, _("Find duplicates"));
4605 DEBUG_NAME(dw->window);
4607 geometry.min_width = DEFAULT_MINIMAL_WINDOW_SIZE;
4608 geometry.min_height = DEFAULT_MINIMAL_WINDOW_SIZE;
4609 geometry.base_width = DUPE_DEF_WIDTH;
4610 geometry.base_height = DUPE_DEF_HEIGHT;
4611 gtk_window_set_geometry_hints(GTK_WINDOW(dw->window), NULL, &geometry,
4612 static_cast<GdkWindowHints>(GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE));
4614 if (lw && options->save_window_positions)
4616 gtk_window_set_default_size(GTK_WINDOW(dw->window), lw->options.dupe_window.w, lw->options.dupe_window.h);
4617 gtk_window_move(GTK_WINDOW(dw->window), lw->options.dupe_window.x, lw->options.dupe_window.y);
4621 gtk_window_set_default_size(GTK_WINDOW(dw->window), DUPE_DEF_WIDTH, DUPE_DEF_HEIGHT);
4624 gtk_window_set_resizable(GTK_WINDOW(dw->window), TRUE);
4625 gtk_container_set_border_width(GTK_CONTAINER(dw->window), 0);
4627 g_signal_connect(G_OBJECT(dw->window), "delete_event",
4628 G_CALLBACK(dupe_window_delete), dw);
4629 g_signal_connect(G_OBJECT(dw->window), "key_press_event",
4630 G_CALLBACK(dupe_window_keypress_cb), dw);
4632 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
4633 gtk_container_add(GTK_CONTAINER(dw->window), vbox);
4634 gtk_widget_show(vbox);
4636 dw->table = gtk_table_new(1, 3, FALSE);
4637 gtk_box_pack_start(GTK_BOX(vbox), dw->table, TRUE, TRUE, 0);
4638 gtk_widget_show(dw->table);
4640 scrolled = gtk_scrolled_window_new(NULL, NULL);
4641 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
4642 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
4643 gtk_table_attach_defaults(GTK_TABLE(dw->table), scrolled, 0, 2, 0, 1);
4644 gtk_widget_show(scrolled);
4646 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);
4647 dw->listview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
4648 g_object_unref(store);
4650 dw->sortable = GTK_TREE_SORTABLE(store);
4652 gtk_tree_sortable_set_sort_func(dw->sortable, DUPE_COLUMN_RANK, column_sort_cb, dw->sortable, NULL);
4653 gtk_tree_sortable_set_sort_func(dw->sortable, DUPE_COLUMN_SET, default_sort_cb, dw->sortable, NULL);
4654 gtk_tree_sortable_set_sort_func(dw->sortable, DUPE_COLUMN_THUMB, default_sort_cb, dw->sortable, NULL);
4655 gtk_tree_sortable_set_sort_func(dw->sortable, DUPE_COLUMN_NAME, column_sort_cb, dw->sortable, NULL);
4656 gtk_tree_sortable_set_sort_func(dw->sortable, DUPE_COLUMN_SIZE, column_sort_cb, dw->sortable, NULL);
4657 gtk_tree_sortable_set_sort_func(dw->sortable, DUPE_COLUMN_DATE, column_sort_cb, dw->sortable, NULL);
4658 gtk_tree_sortable_set_sort_func(dw->sortable, DUPE_COLUMN_DIMENSIONS, column_sort_cb, dw->sortable, NULL);
4659 gtk_tree_sortable_set_sort_func(dw->sortable, DUPE_COLUMN_PATH, column_sort_cb, dw->sortable, NULL);
4661 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dw->listview));
4662 gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_MULTIPLE);
4663 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(dw->listview), TRUE);
4664 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(dw->listview), FALSE);
4666 dupe_listview_add_column(dw, dw->listview, DUPE_COLUMN_RANK, _("Rank"), FALSE, TRUE);
4667 dupe_listview_add_column(dw, dw->listview, DUPE_COLUMN_THUMB, _("Thumb"), TRUE, FALSE);
4668 dupe_listview_add_column(dw, dw->listview, DUPE_COLUMN_NAME, _("Name"), FALSE, FALSE);
4669 dupe_listview_add_column(dw, dw->listview, DUPE_COLUMN_SIZE, _("Size"), FALSE, TRUE);
4670 dupe_listview_add_column(dw, dw->listview, DUPE_COLUMN_DATE, _("Date"), FALSE, TRUE);
4671 dupe_listview_add_column(dw, dw->listview, DUPE_COLUMN_DIMENSIONS, _("Dimensions"), FALSE, FALSE);
4672 dupe_listview_add_column(dw, dw->listview, DUPE_COLUMN_PATH, _("Path"), FALSE, FALSE);
4673 dupe_listview_add_column(dw, dw->listview, DUPE_COLUMN_SET, _("Set"), FALSE, FALSE);
4675 g_signal_connect(gtk_tree_view_get_column(GTK_TREE_VIEW(dw->listview), DUPE_COLUMN_RANK - 1), "clicked", (GCallback)column_clicked_cb, dw);
4676 g_signal_connect(gtk_tree_view_get_column(GTK_TREE_VIEW(dw->listview), DUPE_COLUMN_NAME - 1), "clicked", (GCallback)column_clicked_cb, dw);
4677 g_signal_connect(gtk_tree_view_get_column(GTK_TREE_VIEW(dw->listview), DUPE_COLUMN_SIZE - 1), "clicked", (GCallback)column_clicked_cb, dw);
4678 g_signal_connect(gtk_tree_view_get_column(GTK_TREE_VIEW(dw->listview), DUPE_COLUMN_DATE - 1), "clicked", (GCallback)column_clicked_cb, dw);
4679 g_signal_connect(gtk_tree_view_get_column(GTK_TREE_VIEW(dw->listview), DUPE_COLUMN_DIMENSIONS - 1), "clicked", (GCallback)column_clicked_cb, dw);
4680 g_signal_connect(gtk_tree_view_get_column(GTK_TREE_VIEW(dw->listview), DUPE_COLUMN_PATH - 1), "clicked", (GCallback)column_clicked_cb, dw);
4682 gtk_container_add(GTK_CONTAINER(scrolled), dw->listview);
4683 gtk_widget_show(dw->listview);
4685 dw->second_vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
4686 gtk_table_attach_defaults(GTK_TABLE(dw->table), dw->second_vbox, 2, 3, 0, 1);
4689 gtk_table_set_col_spacings(GTK_TABLE(dw->table), PREF_PAD_GAP);
4690 gtk_widget_show(dw->second_vbox);
4694 gtk_table_set_col_spacings(GTK_TABLE(dw->table), 0);
4697 scrolled = gtk_scrolled_window_new(NULL, NULL);
4698 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
4699 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
4700 gtk_box_pack_start(GTK_BOX(dw->second_vbox), scrolled, TRUE, TRUE, 0);
4701 gtk_widget_show(scrolled);
4703 store = gtk_list_store_new(2, G_TYPE_POINTER, G_TYPE_STRING);
4704 dw->second_listview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
4706 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dw->second_listview));
4707 gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_MULTIPLE);
4709 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(dw->second_listview), TRUE);
4710 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(dw->second_listview), FALSE);
4712 dupe_listview_add_column(dw, dw->second_listview, 1, _("Compare to:"), FALSE, FALSE);
4714 gtk_container_add(GTK_CONTAINER(scrolled), dw->second_listview);
4715 gtk_widget_show(dw->second_listview);
4717 dw->second_status_label = gtk_label_new("");
4718 gtk_box_pack_start(GTK_BOX(dw->second_vbox), dw->second_status_label, FALSE, FALSE, 0);
4719 gtk_widget_show(dw->second_status_label);
4721 pref_line(dw->second_vbox, GTK_ORIENTATION_HORIZONTAL);
4723 status_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
4724 gtk_box_pack_start(GTK_BOX(vbox), status_box, FALSE, FALSE, 0);
4725 gtk_widget_show(status_box);
4727 frame = gtk_frame_new(NULL);
4729 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
4730 gtk_box_pack_start(GTK_BOX(status_box), frame, TRUE, TRUE, 0);
4731 gtk_widget_show(frame);
4733 dw->status_label = gtk_label_new("");
4734 gtk_container_add(GTK_CONTAINER(frame), dw->status_label);
4735 gtk_widget_show(dw->status_label);
4737 dw->extra_label = gtk_progress_bar_new();
4738 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(dw->extra_label), 0.0);
4739 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(dw->extra_label), "");
4740 gtk_progress_bar_set_show_text(GTK_PROGRESS_BAR(dw->extra_label), TRUE);
4741 gtk_box_pack_start(GTK_BOX(status_box), dw->extra_label, FALSE, FALSE, PREF_PAD_SPACE);
4742 gtk_widget_show(dw->extra_label);
4744 controls_box = pref_box_new(vbox, FALSE, GTK_ORIENTATION_HORIZONTAL, 0);
4745 dw->controls_box = controls_box;
4747 dw->button_thumbs = gtk_check_button_new_with_label(_("Thumbnails"));
4748 gtk_widget_set_tooltip_text(GTK_WIDGET(dw->button_thumbs), "Ctrl-T");
4749 dw->show_thumbs = options->duplicates_thumbnails;
4750 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dw->button_thumbs), dw->show_thumbs);
4751 g_signal_connect(G_OBJECT(dw->button_thumbs), "toggled",
4752 G_CALLBACK(dupe_window_show_thumb_cb), dw);
4753 gtk_box_pack_start(GTK_BOX(controls_box), dw->button_thumbs, FALSE, FALSE, PREF_PAD_SPACE);
4754 gtk_widget_show(dw->button_thumbs);
4756 label = gtk_label_new(_("Compare by:"));
4757 gtk_box_pack_start(GTK_BOX(controls_box), label, FALSE, FALSE, PREF_PAD_SPACE);
4758 gtk_widget_show(label);
4760 dupe_menu_setup(dw);
4761 gtk_box_pack_start(GTK_BOX(controls_box), dw->combo, FALSE, FALSE, 0);
4762 gtk_widget_show(dw->combo);
4764 label = gtk_label_new(_("Custom Threshold"));
4765 gtk_box_pack_start(GTK_BOX(controls_box), label, FALSE, FALSE, PREF_PAD_SPACE);
4766 gtk_widget_show(label);
4767 dw->custom_threshold = gtk_spin_button_new_with_range(1, 100, 1);
4768 gtk_widget_set_tooltip_text(GTK_WIDGET(dw->custom_threshold), "Custom similarity threshold\n(Use tab key to set value)");
4769 gtk_spin_button_set_value(GTK_SPIN_BUTTON(dw->custom_threshold), options->duplicates_similarity_threshold);
4770 g_signal_connect(G_OBJECT(dw->custom_threshold), "value_changed", G_CALLBACK(dupe_window_custom_threshold_cb), dw);
4771 gtk_box_pack_start(GTK_BOX(controls_box), dw->custom_threshold, FALSE, FALSE, PREF_PAD_SPACE);
4772 gtk_widget_show(dw->custom_threshold);
4774 button = gtk_check_button_new_with_label(_("Sort"));
4775 gtk_widget_set_tooltip_text(GTK_WIDGET(button), "Sort by group totals");
4776 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), options->sort_totals);
4777 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(dupe_sort_totals_toggle_cb), dw);
4778 gtk_box_pack_start(GTK_BOX(controls_box), button, FALSE, FALSE, PREF_PAD_SPACE);
4779 gtk_widget_show(button);
4781 dw->button_rotation_invariant = gtk_check_button_new_with_label(_("Ignore Orientation"));
4782 gtk_widget_set_tooltip_text(GTK_WIDGET(dw->button_rotation_invariant), "Ignore image orientation");
4783 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dw->button_rotation_invariant), options->rot_invariant_sim);
4784 g_signal_connect(G_OBJECT(dw->button_rotation_invariant), "toggled",
4785 G_CALLBACK(dupe_window_rotation_invariant_cb), dw);
4786 gtk_box_pack_start(GTK_BOX(controls_box), dw->button_rotation_invariant, FALSE, FALSE, PREF_PAD_SPACE);
4787 gtk_widget_show(dw->button_rotation_invariant);
4789 button = gtk_check_button_new_with_label(_("Compare two file sets"));
4790 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), dw->second_set);
4791 g_signal_connect(G_OBJECT(button), "toggled",
4792 G_CALLBACK(dupe_second_set_toggle_cb), dw);
4793 gtk_box_pack_start(GTK_BOX(controls_box), button, FALSE, FALSE, PREF_PAD_SPACE);
4794 gtk_widget_show(button);
4796 button_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
4797 gtk_box_pack_start(GTK_BOX(vbox), button_box, FALSE, FALSE, 0);
4798 gtk_widget_show(button_box);
4800 hbox = gtk_hbutton_box_new();
4801 gtk_button_box_set_layout(GTK_BUTTON_BOX(hbox), GTK_BUTTONBOX_END);
4802 gtk_box_set_spacing(GTK_BOX(hbox), PREF_PAD_SPACE);
4803 gtk_box_pack_end(GTK_BOX(button_box), hbox, FALSE, FALSE, 0);
4804 gtk_widget_show(hbox);
4806 button = pref_button_new(NULL, GTK_STOCK_HELP, NULL, FALSE, G_CALLBACK(dupe_help_cb), NULL);
4807 gtk_widget_set_tooltip_text(GTK_WIDGET(button), "F1");
4808 gtk_container_add(GTK_CONTAINER(hbox), button);
4809 gtk_widget_set_can_default(button, TRUE);
4810 gtk_widget_show(button);
4812 button = pref_button_new(NULL, GTK_STOCK_STOP, NULL, FALSE, G_CALLBACK(dupe_check_stop_cb), dw);
4813 gtk_container_add(GTK_CONTAINER(hbox), button);
4814 gtk_widget_set_can_default(button, TRUE);
4815 gtk_widget_show(button);
4817 button = pref_button_new(NULL, GTK_STOCK_CLOSE, NULL, FALSE, G_CALLBACK(dupe_window_close_cb), dw);
4818 gtk_widget_set_tooltip_text(GTK_WIDGET(button), "Ctrl-W");
4819 gtk_container_add(GTK_CONTAINER(hbox), button);
4820 gtk_widget_set_can_default(button, TRUE);
4821 gtk_widget_grab_default(button);
4822 gtk_widget_show(button);
4825 /* order is important here, dnd_init should be seeing mouse
4826 * presses before we possibly handle (and stop) the signal
4828 g_signal_connect(G_OBJECT(dw->listview), "button_press_event",
4829 G_CALLBACK(dupe_listview_press_cb), dw);
4830 g_signal_connect(G_OBJECT(dw->listview), "button_release_event",
4831 G_CALLBACK(dupe_listview_release_cb), dw);
4832 g_signal_connect(G_OBJECT(dw->second_listview), "button_press_event",
4833 G_CALLBACK(dupe_listview_press_cb), dw);
4834 g_signal_connect(G_OBJECT(dw->second_listview), "button_release_event",
4835 G_CALLBACK(dupe_listview_release_cb), dw);
4837 gtk_widget_show(dw->window);
4839 dupe_listview_set_height(dw->listview, dw->show_thumbs);
4840 g_signal_emit_by_name(G_OBJECT(dw->combo), "changed");
4842 dupe_window_update_count(dw, TRUE);
4843 dupe_window_update_progress(dw, NULL, 0.0, FALSE);
4845 dupe_window_list = g_list_append(dupe_window_list, dw);
4847 file_data_register_notify_func(dupe_notify_cb, dw, NOTIFY_PRIORITY_MEDIUM);
4849 g_mutex_init(&dw->thread_count_mutex);
4850 g_mutex_init(&dw->search_matches_mutex);
4851 dw->dupe_comparison_thread_pool = g_thread_pool_new(dupe_comparison_func, dw, options->threads.duplicates, FALSE, NULL);
4857 *-------------------------------------------------------------------
4859 *-------------------------------------------------------------------
4867 static void confirm_dir_list_cancel(GtkWidget *UNUSED(widget), gpointer UNUSED(data))
4872 static void confirm_dir_list_add(GtkWidget *UNUSED(widget), gpointer data)
4874 CDupeConfirmD *d = static_cast<CDupeConfirmD *>(data);
4877 dupe_window_add_files(d->dw, d->list, FALSE);
4882 FileData *fd = static_cast<FileData *>(work->data);
4884 if (isdir(fd->path))
4888 filelist_read(fd, &list, NULL);
4889 list = filelist_filter(list, FALSE);
4892 dupe_window_add_files(d->dw, list, FALSE);
4893 filelist_free(list);
4899 static void confirm_dir_list_recurse(GtkWidget *UNUSED(widget), gpointer data)
4901 CDupeConfirmD *d = static_cast<CDupeConfirmD *>(data);
4902 dupe_window_add_files(d->dw, d->list, TRUE);
4905 static void confirm_dir_list_skip(GtkWidget *UNUSED(widget), gpointer data)
4907 CDupeConfirmD *d = static_cast<CDupeConfirmD *>(data);
4908 dupe_window_add_files(d->dw, d->list, FALSE);
4911 static void confirm_dir_list_destroy(GtkWidget *UNUSED(widget), gpointer data)
4913 CDupeConfirmD *d = static_cast<CDupeConfirmD *>(data);
4914 filelist_free(d->list);
4918 static GtkWidget *dupe_confirm_dir_list(DupeWindow *dw, GList *list)
4923 d = g_new0(CDupeConfirmD, 1);
4927 menu = popup_menu_short_lived();
4928 g_signal_connect(G_OBJECT(menu), "destroy",
4929 G_CALLBACK(confirm_dir_list_destroy), d);
4931 menu_item_add_stock(menu, _("Dropped list includes folders."), GTK_STOCK_DND_MULTIPLE, NULL, NULL);
4932 menu_item_add_divider(menu);
4933 menu_item_add_stock(menu, _("_Add contents"), GTK_STOCK_OK, G_CALLBACK(confirm_dir_list_add), d);
4934 menu_item_add_stock(menu, _("Add contents _recursive"), GTK_STOCK_ADD, G_CALLBACK(confirm_dir_list_recurse), d);
4935 menu_item_add_stock(menu, _("_Skip folders"), GTK_STOCK_REMOVE, G_CALLBACK(confirm_dir_list_skip), d);
4936 menu_item_add_divider(menu);
4937 menu_item_add_stock(menu, _("Cancel"), GTK_STOCK_CANCEL, G_CALLBACK(confirm_dir_list_cancel), d);
4943 *-------------------------------------------------------------------
4945 *-------------------------------------------------------------------
4948 static GtkTargetEntry dupe_drag_types[] = {
4949 { const_cast<gchar *>("text/uri-list"), 0, TARGET_URI_LIST },
4950 { const_cast<gchar *>("text/plain"), 0, TARGET_TEXT_PLAIN }
4952 static gint n_dupe_drag_types = 2;
4954 static GtkTargetEntry dupe_drop_types[] = {
4955 { const_cast<gchar *>(TARGET_APP_COLLECTION_MEMBER_STRING), 0, TARGET_APP_COLLECTION_MEMBER },
4956 { const_cast<gchar *>("text/uri-list"), 0, TARGET_URI_LIST }
4958 static gint n_dupe_drop_types = 2;
4960 static void dupe_dnd_data_set(GtkWidget *widget, GdkDragContext *UNUSED(context),
4961 GtkSelectionData *selection_data, guint info,
4962 guint UNUSED(time), gpointer data)
4964 DupeWindow *dw = static_cast<DupeWindow *>(data);
4969 case TARGET_URI_LIST:
4970 case TARGET_TEXT_PLAIN:
4971 list = dupe_listview_get_selection(dw, widget);
4973 uri_selection_data_set_uris_from_filelist(selection_data, list);
4974 filelist_free(list);
4981 static void dupe_dnd_data_get(GtkWidget *widget, GdkDragContext *context,
4982 gint UNUSED(x), gint UNUSED(y),
4983 GtkSelectionData *selection_data, guint info,
4984 guint UNUSED(time), gpointer data)
4986 DupeWindow *dw = static_cast<DupeWindow *>(data);
4991 if (dw->add_files_queue_id > 0)
4993 warning_dialog(_("Find duplicates"), _("Please wait for the current file selection to be loaded."), GTK_STOCK_DIALOG_INFO, dw->window);
4998 source = gtk_drag_get_source_widget(context);
4999 if (source == dw->listview || source == dw->second_listview) return;
5001 dw->second_drop = (dw->second_set && widget == dw->second_listview);
5005 case TARGET_APP_COLLECTION_MEMBER:
5006 collection_from_dnd_data((gchar *)gtk_selection_data_get_data(selection_data), &list, NULL);
5008 case TARGET_URI_LIST:
5009 list = uri_filelist_from_gtk_selection_data(selection_data);
5013 FileData *fd = static_cast<FileData *>(work->data);
5014 if (isdir(fd->path))
5017 menu = dupe_confirm_dir_list(dw, list);
5018 gtk_menu_popup_at_pointer(GTK_MENU(menu), NULL);
5031 dupe_window_add_files(dw, list, FALSE);
5032 filelist_free(list);
5036 static void dupe_dest_set(GtkWidget *widget, gboolean enable)
5040 gtk_drag_dest_set(widget,
5041 static_cast<GtkDestDefaults>(GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_HIGHLIGHT | GTK_DEST_DEFAULT_DROP),
5042 dupe_drop_types, n_dupe_drop_types,
5043 static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_ASK));
5048 gtk_drag_dest_unset(widget);
5052 static void dupe_dnd_begin(GtkWidget *widget, GdkDragContext *context, gpointer data)
5054 DupeWindow *dw = static_cast<DupeWindow *>(data);
5055 dupe_dest_set(dw->listview, FALSE);
5056 dupe_dest_set(dw->second_listview, FALSE);
5058 if (dw->click_item && !dupe_listview_item_is_selected(dw, dw->click_item, widget))
5060 GtkListStore *store;
5063 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(widget)));
5064 if (dupe_listview_find_item(store, dw->click_item, &iter) >= 0)
5066 GtkTreeSelection *selection;
5069 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
5070 gtk_tree_selection_unselect_all(selection);
5071 gtk_tree_selection_select_iter(selection, &iter);
5073 tpath = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &iter);
5074 gtk_tree_view_set_cursor(GTK_TREE_VIEW(widget), tpath, NULL, FALSE);
5075 gtk_tree_path_free(tpath);
5079 if (dw->show_thumbs &&
5080 widget == dw->listview &&
5081 dw->click_item && dw->click_item->pixbuf)
5083 GtkTreeSelection *selection;
5086 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
5087 items = gtk_tree_selection_count_selected_rows(selection);
5088 dnd_set_drag_icon(widget, context, dw->click_item->pixbuf, items);
5092 static void dupe_dnd_end(GtkWidget *UNUSED(widget), GdkDragContext *UNUSED(context), gpointer data)
5094 DupeWindow *dw = static_cast<DupeWindow *>(data);
5095 dupe_dest_set(dw->listview, TRUE);
5096 dupe_dest_set(dw->second_listview, TRUE);
5099 static void dupe_dnd_init(DupeWindow *dw)
5101 gtk_drag_source_set(dw->listview, static_cast<GdkModifierType>(GDK_BUTTON1_MASK | GDK_BUTTON2_MASK),
5102 dupe_drag_types, n_dupe_drag_types,
5103 static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK));
5104 g_signal_connect(G_OBJECT(dw->listview), "drag_data_get",
5105 G_CALLBACK(dupe_dnd_data_set), dw);
5106 g_signal_connect(G_OBJECT(dw->listview), "drag_begin",
5107 G_CALLBACK(dupe_dnd_begin), dw);
5108 g_signal_connect(G_OBJECT(dw->listview), "drag_end",
5109 G_CALLBACK(dupe_dnd_end), dw);
5111 dupe_dest_set(dw->listview, TRUE);
5112 g_signal_connect(G_OBJECT(dw->listview), "drag_data_received",
5113 G_CALLBACK(dupe_dnd_data_get), dw);
5115 gtk_drag_source_set(dw->second_listview, static_cast<GdkModifierType>(GDK_BUTTON1_MASK | GDK_BUTTON2_MASK),
5116 dupe_drag_types, n_dupe_drag_types,
5117 static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK));
5118 g_signal_connect(G_OBJECT(dw->second_listview), "drag_data_get",
5119 G_CALLBACK(dupe_dnd_data_set), dw);
5120 g_signal_connect(G_OBJECT(dw->second_listview), "drag_begin",
5121 G_CALLBACK(dupe_dnd_begin), dw);
5122 g_signal_connect(G_OBJECT(dw->second_listview), "drag_end",
5123 G_CALLBACK(dupe_dnd_end), dw);
5125 dupe_dest_set(dw->second_listview, TRUE);
5126 g_signal_connect(G_OBJECT(dw->second_listview), "drag_data_received",
5127 G_CALLBACK(dupe_dnd_data_get), dw);
5131 *-------------------------------------------------------------------
5132 * maintenance (move, delete, etc.)
5133 *-------------------------------------------------------------------
5136 static void dupe_notify_cb(FileData *fd, NotifyType type, gpointer data)
5138 DupeWindow *dw = static_cast<DupeWindow *>(data);
5140 if (!(type & NOTIFY_CHANGE) || !fd->change) return;
5142 DEBUG_1("Notify dupe: %s %04x", fd->path, type);
5144 switch (fd->change->type)
5146 case FILEDATA_CHANGE_MOVE:
5147 case FILEDATA_CHANGE_RENAME:
5148 dupe_item_update_fd(dw, fd);
5150 case FILEDATA_CHANGE_COPY:
5152 case FILEDATA_CHANGE_DELETE:
5153 /* Update the UI only once, after the operation finishes */
5155 case FILEDATA_CHANGE_UNSPECIFIED:
5156 case FILEDATA_CHANGE_WRITE_METADATA:
5163 * @brief Refresh window after a file delete operation
5164 * @param success (ud->phase != UTILITY_PHASE_CANCEL) #file_util_dialog_run
5165 * @param dest_path Not used
5166 * @param data #DupeWindow
5168 * If the window is refreshed after each file of a large set is deleted,
5169 * the UI slows to an unacceptable level. The #FileUtilDoneFunc is used
5170 * to call this function once, when the entire delete operation is completed.
5172 static void delete_finished_cb(gboolean success, const gchar *UNUSED(dest_path), gpointer data)
5174 DupeWindow *dw = static_cast<DupeWindow *>(data);
5181 dupe_window_remove_selection(dw, dw->listview);
5185 *-------------------------------------------------------------------
5186 * Export duplicates data
5187 *-------------------------------------------------------------------
5195 typedef struct _ExportDupesData ExportDupesData;
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);
5209 static void export_duplicates_data_cancel_cb(FileDialog *UNUSED(fdlg), gpointer data)
5211 ExportDupesData *edd = static_cast<ExportDupesData *>(data);
5213 export_duplicates_close(edd);
5216 static void export_duplicates_data_save_cb(FileDialog *fdlg, gpointer data)
5218 ExportDupesData *edd = static_cast<ExportDupesData *>(data);
5219 GError *error = NULL;
5220 GtkTreeModel *store;
5223 GFileOutputStream *gfstream;
5225 GString *output_string;
5229 GtkTreeSelection *selection;
5234 gboolean color_old = FALSE;
5235 gboolean color_new = FALSE;
5239 history_list_add_to_key("export_duplicates", fdlg->dest_path, -1);
5241 out_file = g_file_new_for_path(fdlg->dest_path);
5243 gfstream = g_file_replace(out_file, NULL, TRUE, G_FILE_CREATE_NONE, NULL, &error);
5246 log_printf(_("Error creating Export duplicates data file: Error: %s\n"), error->message);
5247 g_error_free(error);
5251 sep = g_strdup((edd->separator == EXPORT_CSV) ? "," : "\t");
5252 output_string = g_string_new(g_strjoin(sep, _("Match"), _("Group"), _("Similarity"), _("Set"), _("Thumbnail"), _("Name"), _("Size"), _("Date"), _("Width"), _("Height"), _("Path\n"), NULL));
5254 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(edd->dupewindow->listview));
5255 slist = gtk_tree_selection_get_selected_rows(selection, &store);
5258 tpath = static_cast<GtkTreePath *>(work->data);
5259 gtk_tree_model_get_iter(store, &iter, tpath);
5260 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, DUPE_COLUMN_COLOR, &color_new, -1);
5261 color_old = !color_new;
5266 tpath = static_cast<GtkTreePath *>(work->data);
5267 gtk_tree_model_get_iter(store, &iter, tpath);
5269 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, DUPE_COLUMN_POINTER, &di, -1);
5271 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, DUPE_COLUMN_COLOR, &color_new, -1);
5272 if (color_new != color_old)
5276 color_old = color_new;
5277 output_string = g_string_append(output_string, g_strdup_printf("%d", match_count));
5278 output_string = g_string_append(output_string, sep);
5280 if ((dupe_match_find_parent(edd->dupewindow, di) == di))
5282 output_string = g_string_append(output_string, "1");
5286 output_string = g_string_append(output_string, "2");
5288 output_string = g_string_append(output_string, sep);
5290 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, DUPE_COLUMN_RANK, &rank, -1);
5291 rank_split = g_strsplit_set(rank, " [(", -1);
5292 if (rank_split[0] == NULL)
5294 output_string = g_string_append(output_string, "");
5298 output_string = g_string_append(output_string, g_strdup_printf("%s", rank_split[0]));
5300 output_string = g_string_append(output_string, sep);
5302 g_strfreev(rank_split);
5304 output_string = g_string_append(output_string, g_strdup_printf("%d", (di->second + 1)));
5305 output_string = g_string_append(output_string, sep);
5307 thumb_cache = cache_find_location(CACHE_TYPE_THUMB, di->fd->path);
5310 output_string = g_string_append(output_string, thumb_cache);
5311 g_free(thumb_cache);
5315 output_string = g_string_append(output_string, "");
5317 output_string = g_string_append(output_string, sep);
5319 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, DUPE_COLUMN_NAME, &name, -1);
5320 output_string = g_string_append(output_string, name);
5321 output_string = g_string_append(output_string, sep);
5324 output_string = g_string_append(output_string, g_strdup_printf("%" PRIu64, di->fd->size));
5325 output_string = g_string_append(output_string, sep);
5326 output_string = g_string_append(output_string, text_from_time(di->fd->date));
5327 output_string = g_string_append(output_string, sep);
5328 output_string = g_string_append(output_string, g_strdup_printf("%d", (di->width ? di->width : 0)));
5329 output_string = g_string_append(output_string, sep);
5330 output_string = g_string_append(output_string, g_strdup_printf("%d", (di->height ? di->height : 0)));
5331 output_string = g_string_append(output_string, sep);
5332 output_string = g_string_append(output_string, di->fd->path);
5333 output_string = g_string_append_c(output_string, '\n');
5338 g_output_stream_write(G_OUTPUT_STREAM(gfstream), output_string->str, strlen(output_string->str), NULL, &error);
5341 g_string_free(output_string, TRUE);
5342 g_object_unref(gfstream);
5343 g_object_unref(out_file);
5345 export_duplicates_close(edd);
5348 static void pop_menu_export(GList *UNUSED(selection_list), gpointer dupe_window, gpointer data)
5350 const gint index = GPOINTER_TO_INT(data);
5351 DupeWindow *dw = static_cast<DupeWindow *>(dupe_window);
5352 const gchar *title = "Export duplicates data";
5353 const gchar *default_path = "/tmp/";
5354 gchar *file_extension;
5355 const gchar *stock_id;
5356 ExportDupesData *edd;
5357 const gchar *previous_path;
5359 edd = g_new0(ExportDupesData, 1);
5360 edd->dialog = file_util_file_dlg(title, "export_duplicates", NULL, export_duplicates_data_cancel_cb, edd);
5365 edd->separator = EXPORT_CSV;
5366 file_extension = g_strdup(".csv");
5369 edd->separator = EXPORT_TSV;
5370 file_extension = g_strdup(".tsv");
5376 stock_id = GTK_STOCK_SAVE;
5378 generic_dialog_add_message(GENERIC_DIALOG(edd->dialog), NULL, title, NULL, FALSE);
5379 file_dialog_add_button(edd->dialog, stock_id, NULL, export_duplicates_data_save_cb, TRUE);
5381 previous_path = history_list_find_last_path_by_key("export_duplicates");
5383 file_dialog_add_path_widgets(edd->dialog, default_path, previous_path, "export_duplicates", file_extension, _("Export Files"));
5385 edd->dupewindow = dw;
5387 gtk_widget_show(GENERIC_DIALOG(edd->dialog)->dialog);
5389 g_free(file_extension);
5392 static void dupe_pop_menu_export_cb(GtkWidget *widget, gpointer data)
5395 GList *selection_list;
5397 dw = static_cast<DupeWindow *>(submenu_item_get_data(widget));
5398 selection_list = dupe_listview_get_selection(dw, dw->listview);
5399 pop_menu_export(selection_list, dw, data);
5401 filelist_free(selection_list);
5404 static GtkWidget *submenu_add_export(GtkWidget *menu, GtkWidget **menu_item, GCallback func, gpointer data)
5409 item = menu_item_add(menu, _("_Export"), NULL, NULL);
5411 submenu = gtk_menu_new();
5412 g_object_set_data(G_OBJECT(submenu), "submenu_data", data);
5414 menu_item_add_stock_sensitive(submenu, _("Export to csv"),
5415 GTK_STOCK_INDEX, TRUE, G_CALLBACK(func), GINT_TO_POINTER(0));
5416 menu_item_add_stock_sensitive(submenu, _("Export to tab-delimited"),
5417 GTK_STOCK_INDEX, TRUE, G_CALLBACK(func), GINT_TO_POINTER(1));
5419 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
5420 if (menu_item) *menu_item = item;
5425 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */