2 * Copyright (C) 2006 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.
25 #include "collect-dlg.h"
26 #include "collect-io.h"
27 #include "collect-table.h"
30 #include "layout-image.h"
31 #include "layout-util.h"
33 #include "pixbuf-util.h"
35 #include "ui-fileops.h"
36 #include "ui-tree-edit.h"
40 #define COLLECT_DEF_WIDTH 440
41 #define COLLECT_DEF_HEIGHT 450
44 * list of paths to collections */
47 * @brief List of currently open Collections.
49 * Type ::_CollectionData
51 static GList *collection_list = nullptr;
54 * @brief List of currently open Collection windows.
56 * Type ::_CollectWindow
58 static GList *collection_window_list = nullptr;
60 static void collection_window_get_geometry(CollectWindow *cw);
61 static void collection_window_refresh(CollectWindow *cw);
62 static void collection_window_update_title(CollectWindow *cw);
63 static void collection_window_add(CollectWindow *cw, CollectInfo *ci);
64 static void collection_window_insert(CollectWindow *cw, CollectInfo *ci);
65 static void collection_window_remove(CollectWindow *cw, CollectInfo *ci);
66 static void collection_window_update(CollectWindow *cw, CollectInfo *ci);
68 static void collection_window_close(CollectWindow *cw);
70 static void collection_notify_cb(FileData *fd, NotifyType type, gpointer data);
73 *-------------------------------------------------------------------
75 *-------------------------------------------------------------------
78 CollectInfo *collection_info_new(FileData *fd, struct stat *, GdkPixbuf *pixbuf)
82 if (!fd) return nullptr;
84 ci = g_new0(CollectInfo, 1);
85 ci->fd = file_data_ref(fd);
88 if (ci->pixbuf) g_object_ref(ci->pixbuf);
93 void collection_info_free_thumb(CollectInfo *ci)
95 if (ci->pixbuf) g_object_unref(ci->pixbuf);
99 void collection_info_free(CollectInfo *ci)
103 file_data_unref(ci->fd);
104 collection_info_free_thumb(ci);
108 void collection_info_set_thumb(CollectInfo *ci, GdkPixbuf *pixbuf)
110 if (pixbuf) g_object_ref(pixbuf);
111 collection_info_free_thumb(ci);
115 #pragma GCC diagnostic push
116 #pragma GCC diagnostic ignored "-Wunused-function"
117 gboolean collection_info_load_thumb_unused(CollectInfo *ci)
119 if (!ci) return FALSE;
121 collection_info_free_thumb(ci);
123 log_printf("collection_info_load_thumb not implemented!\n(because an instant thumb loader not implemented)");
126 #pragma GCC diagnostic pop
128 /* an ugly static var, well what ya gonna do ? */
129 static SortType collection_list_sort_method = SORT_NAME;
131 static gint collection_list_sort_cb(gconstpointer a, gconstpointer b)
133 auto cia = static_cast<const CollectInfo *>(a);
134 auto cib = static_cast<const CollectInfo *>(b);
136 switch (collection_list_sort_method)
144 if (cia->fd->size < cib->fd->size) return -1;
145 if (cia->fd->size > cib->fd->size) return 1;
149 if (cia->fd->date < cib->fd->date) return -1;
150 if (cia->fd->date > cib->fd->date) return 1;
154 if (cia->fd->cdate < cib->fd->cdate) return -1;
155 if (cia->fd->cdate > cib->fd->cdate) return 1;
159 if (cia->fd->exifdate < cib->fd->exifdate) return -1;
160 if (cia->fd->exifdate > cib->fd->exifdate) return 1;
162 case SORT_EXIFTIMEDIGITIZED:
163 if (cia->fd->exifdate_digitized < cib->fd->exifdate_digitized) return -1;
164 if (cia->fd->exifdate_digitized > cib->fd->exifdate_digitized) return 1;
167 if (cia->fd->rating < cib->fd->rating) return -1;
168 if (cia->fd->rating > cib->fd->rating) return 1;
171 return utf8_compare(cia->fd->path, cib->fd->path, options->file_sort.case_sensitive);
174 if (cia->fd->format_class < cib->fd->format_class) return -1;
175 if (cia->fd->format_class > cib->fd->format_class) return 1;
181 if (options->file_sort.case_sensitive)
182 return strcmp(cia->fd->collate_key_name, cib->fd->collate_key_name);
184 return strcmp(cia->fd->collate_key_name_nocase, cib->fd->collate_key_name_nocase);
187 GList *collection_list_sort(GList *list, SortType method)
189 if (method == SORT_NONE) return list;
191 collection_list_sort_method = method;
193 return g_list_sort(list, collection_list_sort_cb);
196 GList *collection_list_randomize(GList *list)
198 guint random, length, i;
200 GList *nlist, *olist;
202 length = g_list_length(list);
203 if (!length) return nullptr;
205 srand(static_cast<unsigned int>(time(nullptr))); // Initialize random generator (hasn't to be that much strong)
207 for (i = 0; i < length; i++)
209 random = static_cast<guint>(1.0 * length * rand()/(RAND_MAX + 1.0));
210 olist = g_list_nth(list, i);
211 nlist = g_list_nth(list, random);
213 olist->data = nlist->data;
220 GList *collection_list_add(GList *list, CollectInfo *ci, SortType method)
222 if (method != SORT_NONE)
224 collection_list_sort_method = method;
225 list = g_list_insert_sorted(list, ci, collection_list_sort_cb);
229 list = g_list_append(list, ci);
235 GList *collection_list_insert(GList *list, CollectInfo *ci, CollectInfo *insert_ci, SortType method)
237 if (method != SORT_NONE)
239 collection_list_sort_method = method;
240 list = g_list_insert_sorted(list, ci, collection_list_sort_cb);
246 point = g_list_find(list, insert_ci);
247 list = uig_list_insert_link(list, point, ci);
253 GList *collection_list_remove(GList *list, CollectInfo *ci)
255 list = g_list_remove(list, ci);
256 collection_info_free(ci);
260 CollectInfo *collection_list_find_fd(GList *list, FileData *fd)
266 auto ci = static_cast<CollectInfo *>(work->data);
267 if (ci->fd == fd) return ci;
274 GList *collection_list_to_filelist(GList *list)
276 GList *filelist = nullptr;
281 auto info = static_cast<CollectInfo *>(work->data);
282 filelist = g_list_prepend(filelist, file_data_ref(info->fd));
286 filelist = g_list_reverse(filelist);
290 CollectWindow *collection_window_find(CollectionData *cd)
294 work = collection_window_list;
297 auto cw = static_cast<CollectWindow *>(work->data);
298 if (cw->cd == cd) return cw;
305 CollectWindow *collection_window_find_by_path(const gchar *path)
309 if (!path) return nullptr;
311 work = collection_window_list;
314 auto cw = static_cast<CollectWindow *>(work->data);
315 if (cw->cd->path && strcmp(cw->cd->path, path) == 0) return cw;
323 * @brief Checks string for existence of Collection.
324 * @param[in] param Filename, with or without extension of any collection
325 * @returns full pathname if found or NULL
327 * Return value must be freed with g_free()
329 gchar *collection_path(const gchar *param)
331 gchar *path = nullptr;
332 gchar *full_name = nullptr;
334 if (file_extension_match(param, GQ_COLLECTION_EXT))
336 path = g_build_filename(get_collections_dir(), param, NULL);
338 else if (file_extension_match(param, nullptr))
340 full_name = g_strconcat(param, GQ_COLLECTION_EXT, NULL);
341 path = g_build_filename(get_collections_dir(), full_name, NULL);
355 * @brief Checks input string for existence of Collection.
356 * @param[in] param Filename with or without extension of any collection
357 * @returns TRUE if found
361 gboolean is_collection(const gchar *param)
363 gchar *name = nullptr;
365 name = collection_path(param);
375 * @brief Creates a text list of the image paths of the contents of a Collection
376 * @param[in] name The name of the collection, with or without extension
377 * @param[inout] contents A GString to which the image paths are appended
381 void collection_contents(const gchar *name, GString **contents)
389 if (is_collection(name))
391 path = collection_path(name);
392 cd = collection_new("");
393 collection_load(cd, path, COLLECTION_LOAD_APPEND);
397 ci = static_cast<CollectInfo *>(work->data);
399 *contents = g_string_append(*contents, fd->path);
400 *contents = g_string_append(*contents, "\n");
410 * @brief Returns a list of filedatas of the contents of a Collection
411 * @param[in] name The name of the collection, with or without extension
415 GList *collection_contents_fd(const gchar *name)
421 GList *list = nullptr;
423 if (is_collection(name))
425 path = collection_path(name);
426 cd = collection_new("");
427 collection_load(cd, path, COLLECTION_LOAD_APPEND);
431 ci = static_cast<CollectInfo *>(work->data);
432 list = g_list_append(list, ci->fd);
444 *-------------------------------------------------------------------
445 * please use these to actually add/remove stuff
446 *-------------------------------------------------------------------
449 CollectionData *collection_new(const gchar *path)
452 static gint untitled_counter = 0;
454 cd = g_new0(CollectionData, 1);
456 cd->ref = 1; /* starts with a ref of 1 */
457 cd->sort_method = SORT_NONE;
458 cd->window.width = COLLECT_DEF_WIDTH;
459 cd->window.height = COLLECT_DEF_HEIGHT;
460 cd->existence = g_hash_table_new(nullptr, nullptr);
464 cd->path = g_strdup(path);
465 cd->name = g_strdup(filename_from_path(cd->path));
470 if (untitled_counter == 0)
472 cd->name = g_strdup(_("Untitled"));
476 cd->name = g_strdup_printf(_("Untitled (%d)"), untitled_counter + 1);
482 file_data_register_notify_func(collection_notify_cb, cd, NOTIFY_PRIORITY_MEDIUM);
485 collection_list = g_list_append(collection_list, cd);
490 void collection_free(CollectionData *cd)
494 DEBUG_1("collection \"%s\" freed", cd->name);
496 collection_load_stop(cd);
497 g_list_free_full(cd->list, reinterpret_cast<GDestroyNotify>(collection_info_free));
499 file_data_unregister_notify_func(collection_notify_cb, cd);
501 collection_list = g_list_remove(collection_list, cd);
503 g_hash_table_destroy(cd->existence);
505 g_free(cd->collection_path);
512 void collection_ref(CollectionData *cd)
516 DEBUG_1("collection \"%s\" ref count = %d", cd->name, cd->ref);
519 void collection_unref(CollectionData *cd)
523 DEBUG_1("collection \"%s\" ref count = %d", cd->name, cd->ref);
531 void collection_path_changed(CollectionData *cd)
533 collection_window_update_title(collection_window_find(cd));
536 gint collection_to_number(CollectionData *cd)
538 return g_list_index(collection_list, cd);
541 CollectionData *collection_from_number(gint n)
543 return static_cast<CollectionData *>(g_list_nth_data(collection_list, n));
546 CollectionData *collection_from_dnd_data(const gchar *data, GList **list, GList **info_list)
549 gint collection_number;
552 if (list) *list = nullptr;
553 if (info_list) *info_list = nullptr;
555 if (strncmp(data, "COLLECTION:", 11) != 0) return nullptr;
559 collection_number = atoi(ptr);
560 cd = collection_from_number(collection_number);
561 if (!cd) return nullptr;
563 if (!list && !info_list) return cd;
565 while (*ptr != '\0' && *ptr != '\n' ) ptr++;
566 if (*ptr == '\0') return cd;
574 item_number = static_cast<guint>(atoi(ptr));
575 while (*ptr != '\n' && *ptr != '\0') ptr++;
579 while (*ptr == '\n') ptr++;
581 info = static_cast<CollectInfo *>(g_list_nth_data(cd->list, item_number));
584 if (list) *list = g_list_append(*list, file_data_ref(info->fd));
585 if (info_list) *info_list = g_list_append(*info_list, info);
591 gchar *collection_info_list_to_dnd_data(CollectionData *cd, GList *list, gint *length)
594 GList *temp = nullptr;
598 gint collection_number;
601 if (!list) return nullptr;
603 collection_number = collection_to_number(cd);
604 if (collection_number < 0) return nullptr;
606 text = g_strdup_printf("COLLECTION:%d\n", collection_number);
607 *length += strlen(text);
608 temp = g_list_prepend(temp, text);
613 gint item_number = g_list_index(cd->list, work->data);
617 if (item_number < 0) continue;
619 text = g_strdup_printf("%d\n", item_number);
620 temp = g_list_prepend(temp, text);
621 *length += strlen(text);
624 *length += 1; /* ending nul char */
626 uri_text = static_cast<gchar *>(g_malloc(*length));
629 work = g_list_last(temp);
633 auto text = static_cast<gchar *>(work->data);
638 memcpy(ptr, text, len);
644 g_list_free_full(temp, g_free);
649 gint collection_info_valid(CollectionData *cd, CollectInfo *info)
651 if (collection_to_number(cd) < 0) return FALSE;
653 return (g_list_index(cd->list, info) != 0);
656 CollectInfo *collection_next_by_info(CollectionData *cd, CollectInfo *info)
660 work = g_list_find(cd->list, info);
662 if (!work) return nullptr;
664 if (work) return static_cast<CollectInfo *>(work->data);
668 CollectInfo *collection_prev_by_info(CollectionData *cd, CollectInfo *info)
672 work = g_list_find(cd->list, info);
674 if (!work) return nullptr;
676 if (work) return static_cast<CollectInfo *>(work->data);
680 CollectInfo *collection_get_first(CollectionData *cd)
682 if (cd->list) return static_cast<CollectInfo *>(cd->list->data);
687 CollectInfo *collection_get_last(CollectionData *cd)
691 list = g_list_last(cd->list);
693 if (list) return static_cast<CollectInfo *>(list->data);
698 void collection_set_sort_method(CollectionData *cd, SortType method)
702 if (cd->sort_method == method) return;
704 cd->sort_method = method;
705 cd->list = collection_list_sort(cd->list, cd->sort_method);
706 if (cd->list) cd->changed = TRUE;
708 collection_window_refresh(collection_window_find(cd));
711 void collection_randomize(CollectionData *cd)
715 cd->list = collection_list_randomize(cd->list);
716 cd->sort_method = SORT_NONE;
717 if (cd->list) cd->changed = TRUE;
719 collection_window_refresh(collection_window_find(cd));
722 void collection_set_update_info_func(CollectionData *cd,
723 void (*func)(CollectionData *, CollectInfo *, gpointer), gpointer data)
725 cd->info_updated_func = func;
726 cd->info_updated_data = data;
729 static CollectInfo *collection_info_new_if_not_exists(CollectionData *cd, struct stat *st, FileData *fd)
733 if (!options->collections_duplicates)
735 if (g_hash_table_lookup(cd->existence, fd->path)) return nullptr;
738 ci = collection_info_new(fd, st, nullptr);
739 if (ci) g_hash_table_insert(cd->existence, fd->path, g_strdup(""));
743 gboolean collection_add_check(CollectionData *cd, FileData *fd, gboolean sorted, gboolean must_exist)
748 if (!fd) return FALSE;
750 g_assert(fd->magick == FD_MAGICK);
754 valid = (stat_utf8(fd->path, &st) && !S_ISDIR(st.st_mode));
767 ci = collection_info_new_if_not_exists(cd, &st, fd);
768 if (!ci) return FALSE;
769 DEBUG_3("add to collection: %s", fd->path);
771 cd->list = collection_list_add(cd->list, ci, sorted ? cd->sort_method : SORT_NONE);
774 if (!sorted || cd->sort_method == SORT_NONE)
776 collection_window_add(collection_window_find(cd), ci);
780 collection_window_insert(collection_window_find(cd), ci);
787 gboolean collection_add(CollectionData *cd, FileData *fd, gboolean sorted)
789 return collection_add_check(cd, fd, sorted, TRUE);
792 gboolean collection_insert(CollectionData *cd, FileData *fd, CollectInfo *insert_ci, gboolean sorted)
796 if (!insert_ci) return collection_add(cd, fd, sorted);
798 if (stat_utf8(fd->path, &st) >= 0 && !S_ISDIR(st.st_mode))
802 ci = collection_info_new_if_not_exists(cd, &st, fd);
803 if (!ci) return FALSE;
805 DEBUG_3("insert in collection: %s", fd->path);
807 cd->list = collection_list_insert(cd->list, ci, insert_ci, sorted ? cd->sort_method : SORT_NONE);
810 collection_window_insert(collection_window_find(cd), ci);
818 gboolean collection_remove(CollectionData *cd, FileData *fd)
822 ci = collection_list_find_fd(cd->list, fd);
824 if (!ci) return FALSE;
826 g_hash_table_remove(cd->existence, fd->path);
828 cd->list = g_list_remove(cd->list, ci);
831 collection_window_remove(collection_window_find(cd), ci);
832 collection_info_free(ci);
837 static void collection_remove_by_info(CollectionData *cd, CollectInfo *info)
839 if (!info || !g_list_find(cd->list, info)) return;
841 cd->list = g_list_remove(cd->list, info);
842 cd->changed = (cd->list != nullptr);
844 collection_window_remove(collection_window_find(cd), info);
845 collection_info_free(info);
848 void collection_remove_by_info_list(CollectionData *cd, GList *list)
856 /* more efficient (in collect-table) to remove a single item this way */
857 collection_remove_by_info(cd, static_cast<CollectInfo *>(list->data));
864 cd->list = collection_list_remove(cd->list, static_cast<CollectInfo *>(work->data));
867 cd->changed = (cd->list != nullptr);
869 collection_window_refresh(collection_window_find(cd));
872 gboolean collection_rename(CollectionData *cd, FileData *fd)
875 ci = collection_list_find_fd(cd->list, fd);
877 if (!ci) return FALSE;
881 collection_window_update(collection_window_find(cd), ci);
886 void collection_update_geometry(CollectionData *cd)
888 collection_window_get_geometry(collection_window_find(cd));
892 *-------------------------------------------------------------------
893 * simple maintenance for renaming, deleting
894 *-------------------------------------------------------------------
897 static void collection_notify_cb(FileData *fd, NotifyType type, gpointer data)
899 auto cd = static_cast<CollectionData *>(data);
901 if (!(type & NOTIFY_CHANGE) || !fd->change) return;
903 DEBUG_1("Notify collection: %s %04x", fd->path, type);
905 switch (fd->change->type)
907 case FILEDATA_CHANGE_MOVE:
908 case FILEDATA_CHANGE_RENAME:
909 collection_rename(cd, fd);
911 case FILEDATA_CHANGE_COPY:
913 case FILEDATA_CHANGE_DELETE:
914 while (collection_remove(cd, fd));
916 case FILEDATA_CHANGE_UNSPECIFIED:
917 case FILEDATA_CHANGE_WRITE_METADATA:
925 *-------------------------------------------------------------------
927 *-------------------------------------------------------------------
930 static gboolean collection_window_keypress(GtkWidget *, GdkEventKey *event, gpointer data)
932 auto cw = static_cast<CollectWindow *>(data);
933 gboolean stop_signal = FALSE;
936 if (event->state & GDK_CONTROL_MASK)
939 switch (event->keyval)
953 if (event->state & GDK_SHIFT_MASK)
955 collection_table_unselect_all(cw->table);
959 collection_table_select_all(cw->table);
963 list = layout_list(nullptr);
966 collection_table_add_filelist(cw->table, list);
971 file_util_copy(nullptr, collection_table_selection_get_list(cw->table), nullptr, cw->window);
974 file_util_move(nullptr, collection_table_selection_get_list(cw->table), nullptr, cw->window);
977 file_util_rename(nullptr, collection_table_selection_get_list(cw->table), cw->window);
980 options->file_ops.safe_delete_enable = TRUE;
981 file_util_delete(nullptr, collection_table_selection_get_list(cw->table), cw->window);
984 collection_dialog_save_as(cw->cd);
987 collection_window_close(cw);
997 switch (event->keyval)
999 case GDK_KEY_Return: case GDK_KEY_KP_Enter:
1000 layout_image_set_collection(nullptr, cw->cd,
1001 collection_table_get_focus_info(cw->table));
1004 view_window_new_from_collection(cw->cd,
1005 collection_table_get_focus_info(cw->table));
1010 collection_dialog_save_as(cw->cd);
1012 else if (!collection_save(cw->cd, cw->cd->path))
1014 log_printf("failed saving to collection path: %s\n", cw->cd->path);
1018 collection_dialog_append(cw->cd);
1021 collection_set_sort_method(cw->cd, SORT_NAME);
1024 collection_set_sort_method(cw->cd, SORT_TIME);
1027 collection_set_sort_method(cw->cd, SORT_SIZE);
1030 if (event->state & GDK_SHIFT_MASK)
1034 info = collection_table_get_focus_info(cw->table);
1036 print_window_new(info->fd, collection_table_selection_get_list(cw->table),
1037 collection_list_to_filelist(cw->cd->list), cw->window);
1041 collection_set_sort_method(cw->cd, SORT_PATH);
1045 if (event->state & GDK_MOD1_MASK)
1047 options->collections.rectangular_selection = !(options->collections.rectangular_selection);
1050 case GDK_KEY_Delete: case GDK_KEY_KP_Delete:
1051 list = g_list_copy(cw->table->selection);
1054 collection_remove_by_info_list(cw->cd, list);
1055 collection_table_refresh(cw->table);
1060 collection_remove_by_info(cw->cd, collection_table_get_focus_info(cw->table));
1064 stop_signal = FALSE;
1068 if (!stop_signal && is_help_key(event))
1070 help_window_show("GuideCollections.html");
1078 *-------------------------------------------------------------------
1080 *-------------------------------------------------------------------
1082 static void collection_window_get_geometry(CollectWindow *cw)
1090 window = gtk_widget_get_window(cw->window);
1091 gdk_window_get_position(window, &cd->window.x, &cd->window.y);
1092 cd->window.width = gdk_window_get_width(window);
1093 cd->window.height = gdk_window_get_height(window);
1094 cd->window_read = TRUE;
1097 static void collection_window_refresh(CollectWindow *cw)
1101 collection_table_refresh(cw->table);
1104 static void collection_window_update_title(CollectWindow *cw)
1106 gboolean free_name = FALSE;
1112 if (file_extension_match(cw->cd->name, GQ_COLLECTION_EXT))
1114 name = remove_extension_from_path(cw->cd->name);
1119 name = cw->cd->name;
1122 buf = g_strdup_printf(_("%s - Collection - %s"), name, GQ_APPNAME);
1123 if (free_name) g_free(name);
1124 gtk_window_set_title(GTK_WINDOW(cw->window), buf);
1128 static void collection_window_update_info(CollectionData *, CollectInfo *ci, gpointer data)
1130 auto cw = static_cast<CollectWindow *>(data);
1132 collection_table_file_update(cw->table, ci);
1135 static void collection_window_add(CollectWindow *cw, CollectInfo *ci)
1139 if (!ci->pixbuf) collection_load_thumb_idle(cw->cd);
1140 collection_table_file_add(cw->table, ci);
1143 static void collection_window_insert(CollectWindow *cw, CollectInfo *ci)
1147 if (!ci->pixbuf) collection_load_thumb_idle(cw->cd);
1148 collection_table_file_insert(cw->table, ci);
1152 static void collection_window_remove(CollectWindow *cw, CollectInfo *ci)
1156 collection_table_file_remove(cw->table, ci);
1159 static void collection_window_update(CollectWindow *cw, CollectInfo *ci)
1163 collection_table_file_update(cw->table, ci);
1164 collection_table_file_update(cw->table, nullptr);
1167 static void collection_window_close_final(CollectWindow *cw)
1169 if (cw->close_dialog) return;
1171 collection_window_list = g_list_remove(collection_window_list, cw);
1172 collection_window_get_geometry(cw);
1174 gq_gtk_widget_destroy(cw->window);
1176 collection_set_update_info_func(cw->cd, nullptr, nullptr);
1177 collection_unref(cw->cd);
1182 static void collection_close_save_cb(GenericDialog *gd, gpointer data)
1184 auto cw = static_cast<CollectWindow *>(data);
1186 cw->close_dialog = nullptr;
1187 generic_dialog_close(gd);
1191 collection_dialog_save_close(cw->cd);
1194 else if (!collection_save(cw->cd, cw->cd->path))
1197 buf = g_strdup_printf(_("Failed to save the collection:\n%s"), cw->cd->path);
1198 warning_dialog(_("Save Failed"), buf, GQ_ICON_DIALOG_ERROR, cw->window);
1203 collection_window_close_final(cw);
1206 static void collection_close_close_cb(GenericDialog *gd, gpointer data)
1208 auto cw = static_cast<CollectWindow *>(data);
1210 cw->close_dialog = nullptr;
1211 generic_dialog_close(gd);
1213 collection_window_close_final(cw);
1216 static void collection_close_cancel_cb(GenericDialog *gd, gpointer data)
1218 auto cw = static_cast<CollectWindow *>(data);
1220 cw->close_dialog = nullptr;
1221 generic_dialog_close(gd);
1224 static void collection_close_dlg_show(CollectWindow *cw)
1228 if (cw->close_dialog)
1230 gtk_window_present(GTK_WINDOW(cw->close_dialog));
1234 gd = generic_dialog_new(_("Close collection"),
1235 "close_collection", cw->window, FALSE,
1236 collection_close_cancel_cb, cw);
1237 generic_dialog_add_message(gd, GQ_ICON_DIALOG_QUESTION,
1238 _("Close collection"),
1239 _("Collection has been modified.\nSave first?"), TRUE);
1241 generic_dialog_add_button(gd, GQ_ICON_SAVE, _("Save"), collection_close_save_cb, TRUE);
1242 generic_dialog_add_button(gd, GQ_ICON_DELETE, _("_Discard"), collection_close_close_cb, FALSE);
1244 cw->close_dialog = gd->dialog;
1246 gtk_widget_show(gd->dialog);
1249 static void collection_window_close(CollectWindow *cw)
1251 if (!cw->cd->changed && !cw->close_dialog)
1253 collection_window_close_final(cw);
1257 collection_close_dlg_show(cw);
1260 void collection_window_close_by_collection(CollectionData *cd)
1264 cw = collection_window_find(cd);
1265 if (cw) collection_window_close_final(cw);
1269 * @brief Check if any Collection windows have unsaved data
1270 * @returns TRUE if unsaved data exists
1272 * Also saves window geometry for Collection windows that have
1275 gboolean collection_window_modified_exists()
1282 work = collection_window_list;
1285 auto cw = static_cast<CollectWindow *>(work->data);
1286 if (cw->cd->changed)
1292 if (!collection_save(cw->table->cd, cw->table->cd->path))
1294 log_printf("failed saving to collection path: %s\n", cw->table->cd->path);
1303 static gboolean collection_window_delete(GtkWidget *, GdkEvent *, gpointer data)
1305 auto cw = static_cast<CollectWindow *>(data);
1306 collection_window_close(cw);
1311 CollectWindow *collection_window_new(const gchar *path)
1316 GtkWidget *status_label;
1317 GtkWidget *extra_label;
1318 GdkGeometry geometry;
1320 /* If the collection is already opened in another window, return that one */
1321 cw = collection_window_find_by_path(path);
1327 cw = g_new0(CollectWindow, 1);
1329 collection_window_list = g_list_append(collection_window_list, cw);
1331 cw->cd = collection_new(path);
1333 cw->window = window_new("collection", PIXBUF_INLINE_ICON_BOOK, nullptr, nullptr);
1334 DEBUG_NAME(cw->window);
1336 geometry.min_width = DEFAULT_MINIMAL_WINDOW_SIZE;
1337 geometry.min_height = DEFAULT_MINIMAL_WINDOW_SIZE;
1338 geometry.base_width = COLLECT_DEF_WIDTH;
1339 geometry.base_height = COLLECT_DEF_HEIGHT;
1340 gtk_window_set_geometry_hints(GTK_WINDOW(cw->window), nullptr, &geometry,
1341 static_cast<GdkWindowHints>(GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE));
1343 if (options->collections_on_top)
1345 gq_gtk_window_set_keep_above(GTK_WINDOW(cw->window), TRUE);
1348 if (options->save_window_positions && path && collection_load_only_geometry(cw->cd, path))
1350 gtk_window_set_default_size(GTK_WINDOW(cw->window), cw->cd->window.width, cw->cd->window.height);
1351 gq_gtk_window_move(GTK_WINDOW(cw->window), cw->cd->window.x, cw->cd->window.y);
1355 gtk_window_set_default_size(GTK_WINDOW(cw->window), COLLECT_DEF_WIDTH, COLLECT_DEF_HEIGHT);
1358 gtk_window_set_resizable(GTK_WINDOW(cw->window), TRUE);
1359 collection_window_update_title(cw);
1360 gtk_container_set_border_width(GTK_CONTAINER(cw->window), 0);
1362 g_signal_connect(G_OBJECT(cw->window), "delete_event",
1363 G_CALLBACK(collection_window_delete), cw);
1365 g_signal_connect(G_OBJECT(cw->window), "key_press_event",
1366 G_CALLBACK(collection_window_keypress), cw);
1368 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
1369 gq_gtk_container_add(GTK_WIDGET(cw->window), vbox);
1370 gtk_widget_show(vbox);
1372 cw->table = collection_table_new(cw->cd);
1373 gq_gtk_box_pack_start(GTK_BOX(vbox), cw->table->scrolled, TRUE, TRUE, 0);
1374 gtk_widget_show(cw->table->scrolled);
1376 cw->status_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
1377 gq_gtk_box_pack_start(GTK_BOX(vbox), cw->status_box, FALSE, FALSE, 0);
1378 gtk_widget_show(cw->status_box);
1380 frame = gtk_frame_new(nullptr);
1382 gq_gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
1383 gq_gtk_box_pack_start(GTK_BOX(cw->status_box), frame, TRUE, TRUE, 0);
1384 gtk_widget_show(frame);
1386 status_label = gtk_label_new("");
1387 gq_gtk_container_add(GTK_WIDGET(frame), status_label);
1388 gtk_widget_show(status_label);
1390 extra_label = gtk_progress_bar_new();
1391 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(extra_label), 0.0);
1392 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(extra_label), "");
1393 gtk_progress_bar_set_show_text(GTK_PROGRESS_BAR(extra_label), TRUE);
1395 gq_gtk_box_pack_start(GTK_BOX(cw->status_box), extra_label, TRUE, TRUE, 0);
1396 gtk_widget_show(extra_label);
1398 collection_table_set_labels(cw->table, status_label, extra_label);
1400 gtk_widget_show(cw->window);
1401 gtk_widget_grab_focus(cw->table->listview);
1403 collection_set_update_info_func(cw->cd, collection_window_update_info, cw);
1405 if (path && *path == G_DIR_SEPARATOR) collection_load_begin(cw->cd, nullptr, COLLECTION_LOAD_NONE);
1409 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */