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"
41 COLLECT_DEF_WIDTH = 440,
42 COLLECT_DEF_HEIGHT = 450
46 * list of paths to collections */
49 * @brief List of currently open Collections.
51 * Type ::_CollectionData
53 static GList *collection_list = nullptr;
56 * @brief List of currently open Collection windows.
58 * Type ::_CollectWindow
60 static GList *collection_window_list = nullptr;
62 static void collection_window_get_geometry(CollectWindow *cw);
63 static void collection_window_refresh(CollectWindow *cw);
64 static void collection_window_update_title(CollectWindow *cw);
65 static void collection_window_add(CollectWindow *cw, CollectInfo *ci);
66 static void collection_window_insert(CollectWindow *cw, CollectInfo *ci);
67 static void collection_window_remove(CollectWindow *cw, CollectInfo *ci);
68 static void collection_window_update(CollectWindow *cw, CollectInfo *ci);
70 static void collection_window_close(CollectWindow *cw);
72 static void collection_notify_cb(FileData *fd, NotifyType type, gpointer data);
75 *-------------------------------------------------------------------
77 *-------------------------------------------------------------------
80 CollectInfo *collection_info_new(FileData *fd, struct stat *, GdkPixbuf *pixbuf)
84 if (!fd) return nullptr;
86 ci = g_new0(CollectInfo, 1);
87 ci->fd = file_data_ref(fd);
90 if (ci->pixbuf) g_object_ref(ci->pixbuf);
95 void collection_info_free_thumb(CollectInfo *ci)
97 if (ci->pixbuf) g_object_unref(ci->pixbuf);
101 void collection_info_free(CollectInfo *ci)
105 file_data_unref(ci->fd);
106 collection_info_free_thumb(ci);
110 void collection_info_set_thumb(CollectInfo *ci, GdkPixbuf *pixbuf)
112 if (pixbuf) g_object_ref(pixbuf);
113 collection_info_free_thumb(ci);
117 #pragma GCC diagnostic push
118 #pragma GCC diagnostic ignored "-Wunused-function"
119 gboolean collection_info_load_thumb_unused(CollectInfo *ci)
121 if (!ci) return FALSE;
123 collection_info_free_thumb(ci);
125 log_printf("collection_info_load_thumb not implemented!\n(because an instant thumb loader not implemented)");
128 #pragma GCC diagnostic pop
130 /* an ugly static var, well what ya gonna do ? */
131 static SortType collection_list_sort_method = SORT_NAME;
133 static gint collection_list_sort_cb(gconstpointer a, gconstpointer b)
135 auto cia = static_cast<const CollectInfo *>(a);
136 auto cib = static_cast<const CollectInfo *>(b);
138 switch (collection_list_sort_method)
146 if (cia->fd->size < cib->fd->size) return -1;
147 if (cia->fd->size > cib->fd->size) return 1;
151 if (cia->fd->date < cib->fd->date) return -1;
152 if (cia->fd->date > cib->fd->date) return 1;
156 if (cia->fd->cdate < cib->fd->cdate) return -1;
157 if (cia->fd->cdate > cib->fd->cdate) return 1;
161 if (cia->fd->exifdate < cib->fd->exifdate) return -1;
162 if (cia->fd->exifdate > cib->fd->exifdate) return 1;
164 case SORT_EXIFTIMEDIGITIZED:
165 if (cia->fd->exifdate_digitized < cib->fd->exifdate_digitized) return -1;
166 if (cia->fd->exifdate_digitized > cib->fd->exifdate_digitized) return 1;
169 if (cia->fd->rating < cib->fd->rating) return -1;
170 if (cia->fd->rating > cib->fd->rating) return 1;
173 return utf8_compare(cia->fd->path, cib->fd->path, options->file_sort.case_sensitive);
176 if (cia->fd->format_class < cib->fd->format_class) return -1;
177 if (cia->fd->format_class > cib->fd->format_class) return 1;
183 if (options->file_sort.case_sensitive)
184 return strcmp(cia->fd->collate_key_name, cib->fd->collate_key_name);
186 return strcmp(cia->fd->collate_key_name_nocase, cib->fd->collate_key_name_nocase);
189 GList *collection_list_sort(GList *list, SortType method)
191 if (method == SORT_NONE) return list;
193 collection_list_sort_method = method;
195 return g_list_sort(list, collection_list_sort_cb);
198 GList *collection_list_randomize(GList *list)
200 guint random, length, i;
202 GList *nlist, *olist;
204 length = g_list_length(list);
205 if (!length) return nullptr;
207 srand(static_cast<unsigned int>(time(nullptr))); // Initialize random generator (hasn't to be that much strong)
209 for (i = 0; i < length; i++)
211 random = static_cast<guint>(1.0 * length * rand()/(RAND_MAX + 1.0));
212 olist = g_list_nth(list, i);
213 nlist = g_list_nth(list, random);
215 olist->data = nlist->data;
222 GList *collection_list_add(GList *list, CollectInfo *ci, SortType method)
224 if (method != SORT_NONE)
226 collection_list_sort_method = method;
227 list = g_list_insert_sorted(list, ci, collection_list_sort_cb);
231 list = g_list_append(list, ci);
237 GList *collection_list_insert(GList *list, CollectInfo *ci, CollectInfo *insert_ci, SortType method)
239 if (method != SORT_NONE)
241 collection_list_sort_method = method;
242 list = g_list_insert_sorted(list, ci, collection_list_sort_cb);
248 point = g_list_find(list, insert_ci);
249 list = uig_list_insert_link(list, point, ci);
255 GList *collection_list_remove(GList *list, CollectInfo *ci)
257 list = g_list_remove(list, ci);
258 collection_info_free(ci);
262 CollectInfo *collection_list_find_fd(GList *list, FileData *fd)
268 auto ci = static_cast<CollectInfo *>(work->data);
269 if (ci->fd == fd) return ci;
276 GList *collection_list_to_filelist(GList *list)
278 GList *filelist = nullptr;
283 auto info = static_cast<CollectInfo *>(work->data);
284 filelist = g_list_prepend(filelist, file_data_ref(info->fd));
288 filelist = g_list_reverse(filelist);
292 CollectWindow *collection_window_find(CollectionData *cd)
296 work = collection_window_list;
299 auto cw = static_cast<CollectWindow *>(work->data);
300 if (cw->cd == cd) return cw;
307 CollectWindow *collection_window_find_by_path(const gchar *path)
311 if (!path) return nullptr;
313 work = collection_window_list;
316 auto cw = static_cast<CollectWindow *>(work->data);
317 if (cw->cd->path && strcmp(cw->cd->path, path) == 0) return cw;
325 * @brief Checks string for existence of Collection.
326 * @param[in] param Filename, with or without extension of any collection
327 * @returns full pathname if found or NULL
329 * Return value must be freed with g_free()
331 gchar *collection_path(const gchar *param)
333 gchar *path = nullptr;
334 gchar *full_name = nullptr;
336 if (file_extension_match(param, GQ_COLLECTION_EXT))
338 path = g_build_filename(get_collections_dir(), param, NULL);
340 else if (file_extension_match(param, nullptr))
342 full_name = g_strconcat(param, GQ_COLLECTION_EXT, NULL);
343 path = g_build_filename(get_collections_dir(), full_name, NULL);
357 * @brief Checks input string for existence of Collection.
358 * @param[in] param Filename with or without extension of any collection
359 * @returns TRUE if found
363 gboolean is_collection(const gchar *param)
365 gchar *name = nullptr;
367 name = collection_path(param);
377 * @brief Creates a text list of the image paths of the contents of a Collection
378 * @param[in] name The name of the collection, with or without extension
379 * @param[inout] contents A GString to which the image paths are appended
383 void collection_contents(const gchar *name, GString **contents)
391 if (is_collection(name))
393 path = collection_path(name);
394 cd = collection_new("");
395 collection_load(cd, path, COLLECTION_LOAD_APPEND);
399 ci = static_cast<CollectInfo *>(work->data);
401 *contents = g_string_append(*contents, fd->path);
402 *contents = g_string_append(*contents, "\n");
412 * @brief Returns a list of filedatas of the contents of a Collection
413 * @param[in] name The name of the collection, with or without extension
417 GList *collection_contents_fd(const gchar *name)
423 GList *list = nullptr;
425 if (is_collection(name))
427 path = collection_path(name);
428 cd = collection_new("");
429 collection_load(cd, path, COLLECTION_LOAD_APPEND);
433 ci = static_cast<CollectInfo *>(work->data);
434 list = g_list_append(list, ci->fd);
446 *-------------------------------------------------------------------
447 * please use these to actually add/remove stuff
448 *-------------------------------------------------------------------
451 CollectionData *collection_new(const gchar *path)
454 static gint untitled_counter = 0;
456 cd = g_new0(CollectionData, 1);
458 cd->ref = 1; /* starts with a ref of 1 */
459 cd->sort_method = SORT_NONE;
460 cd->window.width = COLLECT_DEF_WIDTH;
461 cd->window.height = COLLECT_DEF_HEIGHT;
462 cd->existence = g_hash_table_new(nullptr, nullptr);
466 cd->path = g_strdup(path);
467 cd->name = g_strdup(filename_from_path(cd->path));
472 if (untitled_counter == 0)
474 cd->name = g_strdup(_("Untitled"));
478 cd->name = g_strdup_printf(_("Untitled (%d)"), untitled_counter + 1);
484 file_data_register_notify_func(collection_notify_cb, cd, NOTIFY_PRIORITY_MEDIUM);
487 collection_list = g_list_append(collection_list, cd);
492 void collection_free(CollectionData *cd)
496 DEBUG_1("collection \"%s\" freed", cd->name);
498 collection_load_stop(cd);
499 g_list_free_full(cd->list, reinterpret_cast<GDestroyNotify>(collection_info_free));
501 file_data_unregister_notify_func(collection_notify_cb, cd);
503 collection_list = g_list_remove(collection_list, cd);
505 g_hash_table_destroy(cd->existence);
507 g_free(cd->collection_path);
514 void collection_ref(CollectionData *cd)
518 DEBUG_1("collection \"%s\" ref count = %d", cd->name, cd->ref);
521 void collection_unref(CollectionData *cd)
525 DEBUG_1("collection \"%s\" ref count = %d", cd->name, cd->ref);
533 void collection_path_changed(CollectionData *cd)
535 collection_window_update_title(collection_window_find(cd));
538 gint collection_to_number(CollectionData *cd)
540 return g_list_index(collection_list, cd);
543 CollectionData *collection_from_number(gint n)
545 return static_cast<CollectionData *>(g_list_nth_data(collection_list, n));
548 CollectionData *collection_from_dnd_data(const gchar *data, GList **list, GList **info_list)
551 gint collection_number;
554 if (list) *list = nullptr;
555 if (info_list) *info_list = nullptr;
557 if (strncmp(data, "COLLECTION:", 11) != 0) return nullptr;
561 collection_number = atoi(ptr);
562 cd = collection_from_number(collection_number);
563 if (!cd) return nullptr;
565 if (!list && !info_list) return cd;
567 while (*ptr != '\0' && *ptr != '\n' ) ptr++;
568 if (*ptr == '\0') return cd;
576 item_number = static_cast<guint>(atoi(ptr));
577 while (*ptr != '\n' && *ptr != '\0') ptr++;
581 while (*ptr == '\n') ptr++;
583 info = static_cast<CollectInfo *>(g_list_nth_data(cd->list, item_number));
586 if (list) *list = g_list_append(*list, file_data_ref(info->fd));
587 if (info_list) *info_list = g_list_append(*info_list, info);
593 gchar *collection_info_list_to_dnd_data(CollectionData *cd, GList *list, gint *length)
596 GList *temp = nullptr;
600 gint collection_number;
603 if (!list) return nullptr;
605 collection_number = collection_to_number(cd);
606 if (collection_number < 0) return nullptr;
608 text = g_strdup_printf("COLLECTION:%d\n", collection_number);
609 *length += strlen(text);
610 temp = g_list_prepend(temp, text);
615 gint item_number = g_list_index(cd->list, work->data);
619 if (item_number < 0) continue;
621 text = g_strdup_printf("%d\n", item_number);
622 temp = g_list_prepend(temp, text);
623 *length += strlen(text);
626 *length += 1; /* ending nul char */
628 uri_text = static_cast<gchar *>(g_malloc(*length));
631 work = g_list_last(temp);
635 auto text = static_cast<gchar *>(work->data);
640 memcpy(ptr, text, len);
646 g_list_free_full(temp, g_free);
651 gint collection_info_valid(CollectionData *cd, CollectInfo *info)
653 if (collection_to_number(cd) < 0) return FALSE;
655 return (g_list_index(cd->list, info) != 0);
658 CollectInfo *collection_next_by_info(CollectionData *cd, CollectInfo *info)
662 work = g_list_find(cd->list, info);
664 if (!work) return nullptr;
666 if (work) return static_cast<CollectInfo *>(work->data);
670 CollectInfo *collection_prev_by_info(CollectionData *cd, CollectInfo *info)
674 work = g_list_find(cd->list, info);
676 if (!work) return nullptr;
678 if (work) return static_cast<CollectInfo *>(work->data);
682 CollectInfo *collection_get_first(CollectionData *cd)
684 if (cd->list) return static_cast<CollectInfo *>(cd->list->data);
689 CollectInfo *collection_get_last(CollectionData *cd)
693 list = g_list_last(cd->list);
695 if (list) return static_cast<CollectInfo *>(list->data);
700 void collection_set_sort_method(CollectionData *cd, SortType method)
704 if (cd->sort_method == method) return;
706 cd->sort_method = method;
707 cd->list = collection_list_sort(cd->list, cd->sort_method);
708 if (cd->list) cd->changed = TRUE;
710 collection_window_refresh(collection_window_find(cd));
713 void collection_randomize(CollectionData *cd)
717 cd->list = collection_list_randomize(cd->list);
718 cd->sort_method = SORT_NONE;
719 if (cd->list) cd->changed = TRUE;
721 collection_window_refresh(collection_window_find(cd));
724 void collection_set_update_info_func(CollectionData *cd,
725 void (*func)(CollectionData *, CollectInfo *, gpointer), gpointer data)
727 cd->info_updated_func = func;
728 cd->info_updated_data = data;
731 static CollectInfo *collection_info_new_if_not_exists(CollectionData *cd, struct stat *st, FileData *fd)
735 if (!options->collections_duplicates)
737 if (g_hash_table_lookup(cd->existence, fd->path)) return nullptr;
740 ci = collection_info_new(fd, st, nullptr);
741 if (ci) g_hash_table_insert(cd->existence, fd->path, g_strdup(""));
745 gboolean collection_add_check(CollectionData *cd, FileData *fd, gboolean sorted, gboolean must_exist)
750 if (!fd) return FALSE;
752 g_assert(fd->magick == FD_MAGICK);
756 valid = (stat_utf8(fd->path, &st) && !S_ISDIR(st.st_mode));
769 ci = collection_info_new_if_not_exists(cd, &st, fd);
770 if (!ci) return FALSE;
771 DEBUG_3("add to collection: %s", fd->path);
773 cd->list = collection_list_add(cd->list, ci, sorted ? cd->sort_method : SORT_NONE);
776 if (!sorted || cd->sort_method == SORT_NONE)
778 collection_window_add(collection_window_find(cd), ci);
782 collection_window_insert(collection_window_find(cd), ci);
789 gboolean collection_add(CollectionData *cd, FileData *fd, gboolean sorted)
791 return collection_add_check(cd, fd, sorted, TRUE);
794 gboolean collection_insert(CollectionData *cd, FileData *fd, CollectInfo *insert_ci, gboolean sorted)
798 if (!insert_ci) return collection_add(cd, fd, sorted);
800 if (stat_utf8(fd->path, &st) >= 0 && !S_ISDIR(st.st_mode))
804 ci = collection_info_new_if_not_exists(cd, &st, fd);
805 if (!ci) return FALSE;
807 DEBUG_3("insert in collection: %s", fd->path);
809 cd->list = collection_list_insert(cd->list, ci, insert_ci, sorted ? cd->sort_method : SORT_NONE);
812 collection_window_insert(collection_window_find(cd), ci);
820 gboolean collection_remove(CollectionData *cd, FileData *fd)
824 ci = collection_list_find_fd(cd->list, fd);
826 if (!ci) return FALSE;
828 g_hash_table_remove(cd->existence, fd->path);
830 cd->list = g_list_remove(cd->list, ci);
833 collection_window_remove(collection_window_find(cd), ci);
834 collection_info_free(ci);
839 static void collection_remove_by_info(CollectionData *cd, CollectInfo *info)
841 if (!info || !g_list_find(cd->list, info)) return;
843 cd->list = g_list_remove(cd->list, info);
844 cd->changed = (cd->list != nullptr);
846 collection_window_remove(collection_window_find(cd), info);
847 collection_info_free(info);
850 void collection_remove_by_info_list(CollectionData *cd, GList *list)
858 /* more efficient (in collect-table) to remove a single item this way */
859 collection_remove_by_info(cd, static_cast<CollectInfo *>(list->data));
866 cd->list = collection_list_remove(cd->list, static_cast<CollectInfo *>(work->data));
869 cd->changed = (cd->list != nullptr);
871 collection_window_refresh(collection_window_find(cd));
874 gboolean collection_rename(CollectionData *cd, FileData *fd)
877 ci = collection_list_find_fd(cd->list, fd);
879 if (!ci) return FALSE;
883 collection_window_update(collection_window_find(cd), ci);
888 void collection_update_geometry(CollectionData *cd)
890 collection_window_get_geometry(collection_window_find(cd));
894 *-------------------------------------------------------------------
895 * simple maintenance for renaming, deleting
896 *-------------------------------------------------------------------
899 static void collection_notify_cb(FileData *fd, NotifyType type, gpointer data)
901 auto cd = static_cast<CollectionData *>(data);
903 if (!(type & NOTIFY_CHANGE) || !fd->change) return;
905 DEBUG_1("Notify collection: %s %04x", fd->path, type);
907 switch (fd->change->type)
909 case FILEDATA_CHANGE_MOVE:
910 case FILEDATA_CHANGE_RENAME:
911 collection_rename(cd, fd);
913 case FILEDATA_CHANGE_COPY:
915 case FILEDATA_CHANGE_DELETE:
916 while (collection_remove(cd, fd));
918 case FILEDATA_CHANGE_UNSPECIFIED:
919 case FILEDATA_CHANGE_WRITE_METADATA:
927 *-------------------------------------------------------------------
929 *-------------------------------------------------------------------
932 static gboolean collection_window_keypress(GtkWidget *, GdkEventKey *event, gpointer data)
934 auto cw = static_cast<CollectWindow *>(data);
935 gboolean stop_signal = FALSE;
938 if (event->state & GDK_CONTROL_MASK)
941 switch (event->keyval)
955 if (event->state & GDK_SHIFT_MASK)
957 collection_table_unselect_all(cw->table);
961 collection_table_select_all(cw->table);
965 list = layout_list(nullptr);
968 collection_table_add_filelist(cw->table, list);
973 file_util_copy(nullptr, collection_table_selection_get_list(cw->table), nullptr, cw->window);
976 file_util_move(nullptr, collection_table_selection_get_list(cw->table), nullptr, cw->window);
979 file_util_rename(nullptr, collection_table_selection_get_list(cw->table), cw->window);
982 options->file_ops.safe_delete_enable = TRUE;
983 file_util_delete(nullptr, collection_table_selection_get_list(cw->table), cw->window);
986 collection_dialog_save_as(cw->cd);
989 collection_window_close(cw);
999 switch (event->keyval)
1001 case GDK_KEY_Return: case GDK_KEY_KP_Enter:
1002 layout_image_set_collection(nullptr, cw->cd,
1003 collection_table_get_focus_info(cw->table));
1006 view_window_new_from_collection(cw->cd,
1007 collection_table_get_focus_info(cw->table));
1012 collection_dialog_save_as(cw->cd);
1014 else if (!collection_save(cw->cd, cw->cd->path))
1016 log_printf("failed saving to collection path: %s\n", cw->cd->path);
1020 collection_dialog_append(cw->cd);
1023 collection_set_sort_method(cw->cd, SORT_NAME);
1026 collection_set_sort_method(cw->cd, SORT_TIME);
1029 collection_set_sort_method(cw->cd, SORT_SIZE);
1032 if (event->state & GDK_SHIFT_MASK)
1036 info = collection_table_get_focus_info(cw->table);
1038 print_window_new(info->fd, collection_table_selection_get_list(cw->table),
1039 collection_list_to_filelist(cw->cd->list), cw->window);
1043 collection_set_sort_method(cw->cd, SORT_PATH);
1047 if (event->state & GDK_MOD1_MASK)
1049 options->collections.rectangular_selection = !(options->collections.rectangular_selection);
1052 case GDK_KEY_Delete: case GDK_KEY_KP_Delete:
1053 list = g_list_copy(cw->table->selection);
1056 collection_remove_by_info_list(cw->cd, list);
1057 collection_table_refresh(cw->table);
1062 collection_remove_by_info(cw->cd, collection_table_get_focus_info(cw->table));
1066 stop_signal = FALSE;
1070 if (!stop_signal && is_help_key(event))
1072 help_window_show("GuideCollections.html");
1080 *-------------------------------------------------------------------
1082 *-------------------------------------------------------------------
1084 static void collection_window_get_geometry(CollectWindow *cw)
1092 window = gtk_widget_get_window(cw->window);
1093 gdk_window_get_position(window, &cd->window.x, &cd->window.y);
1094 cd->window.width = gdk_window_get_width(window);
1095 cd->window.height = gdk_window_get_height(window);
1096 cd->window_read = TRUE;
1099 static void collection_window_refresh(CollectWindow *cw)
1103 collection_table_refresh(cw->table);
1106 static void collection_window_update_title(CollectWindow *cw)
1108 gboolean free_name = FALSE;
1114 if (file_extension_match(cw->cd->name, GQ_COLLECTION_EXT))
1116 name = remove_extension_from_path(cw->cd->name);
1121 name = cw->cd->name;
1124 buf = g_strdup_printf(_("%s - Collection - %s"), name, GQ_APPNAME);
1125 if (free_name) g_free(name);
1126 gtk_window_set_title(GTK_WINDOW(cw->window), buf);
1130 static void collection_window_update_info(CollectionData *, CollectInfo *ci, gpointer data)
1132 auto cw = static_cast<CollectWindow *>(data);
1134 collection_table_file_update(cw->table, ci);
1137 static void collection_window_add(CollectWindow *cw, CollectInfo *ci)
1141 if (!ci->pixbuf) collection_load_thumb_idle(cw->cd);
1142 collection_table_file_add(cw->table, ci);
1145 static void collection_window_insert(CollectWindow *cw, CollectInfo *ci)
1149 if (!ci->pixbuf) collection_load_thumb_idle(cw->cd);
1150 collection_table_file_insert(cw->table, ci);
1154 static void collection_window_remove(CollectWindow *cw, CollectInfo *ci)
1158 collection_table_file_remove(cw->table, ci);
1161 static void collection_window_update(CollectWindow *cw, CollectInfo *ci)
1165 collection_table_file_update(cw->table, ci);
1166 collection_table_file_update(cw->table, nullptr);
1169 static void collection_window_close_final(CollectWindow *cw)
1171 if (cw->close_dialog) return;
1173 collection_window_list = g_list_remove(collection_window_list, cw);
1174 collection_window_get_geometry(cw);
1176 gq_gtk_widget_destroy(cw->window);
1178 collection_set_update_info_func(cw->cd, nullptr, nullptr);
1179 collection_unref(cw->cd);
1184 static void collection_close_save_cb(GenericDialog *gd, gpointer data)
1186 auto cw = static_cast<CollectWindow *>(data);
1188 cw->close_dialog = nullptr;
1189 generic_dialog_close(gd);
1193 collection_dialog_save_close(cw->cd);
1197 if (!collection_save(cw->cd, cw->cd->path))
1200 buf = g_strdup_printf(_("Failed to save the collection:\n%s"), cw->cd->path);
1201 warning_dialog(_("Save Failed"), buf, GQ_ICON_DIALOG_ERROR, cw->window);
1206 collection_window_close_final(cw);
1209 static void collection_close_close_cb(GenericDialog *gd, gpointer data)
1211 auto cw = static_cast<CollectWindow *>(data);
1213 cw->close_dialog = nullptr;
1214 generic_dialog_close(gd);
1216 collection_window_close_final(cw);
1219 static void collection_close_cancel_cb(GenericDialog *gd, gpointer data)
1221 auto cw = static_cast<CollectWindow *>(data);
1223 cw->close_dialog = nullptr;
1224 generic_dialog_close(gd);
1227 static void collection_close_dlg_show(CollectWindow *cw)
1231 if (cw->close_dialog)
1233 gtk_window_present(GTK_WINDOW(cw->close_dialog));
1237 gd = generic_dialog_new(_("Close collection"),
1238 "close_collection", cw->window, FALSE,
1239 collection_close_cancel_cb, cw);
1240 generic_dialog_add_message(gd, GQ_ICON_DIALOG_QUESTION,
1241 _("Close collection"),
1242 _("Collection has been modified.\nSave first?"), TRUE);
1244 generic_dialog_add_button(gd, GQ_ICON_SAVE, _("Save"), collection_close_save_cb, TRUE);
1245 generic_dialog_add_button(gd, GQ_ICON_DELETE, _("_Discard"), collection_close_close_cb, FALSE);
1247 cw->close_dialog = gd->dialog;
1249 gtk_widget_show(gd->dialog);
1252 static void collection_window_close(CollectWindow *cw)
1254 if (!cw->cd->changed && !cw->close_dialog)
1256 collection_window_close_final(cw);
1260 collection_close_dlg_show(cw);
1263 void collection_window_close_by_collection(CollectionData *cd)
1267 cw = collection_window_find(cd);
1268 if (cw) collection_window_close_final(cw);
1272 * @brief Check if any Collection windows have unsaved data
1273 * @returns TRUE if unsaved data exists
1275 * Also saves window geometry for Collection windows that have
1278 gboolean collection_window_modified_exists()
1285 work = collection_window_list;
1288 auto cw = static_cast<CollectWindow *>(work->data);
1289 if (cw->cd->changed)
1295 if (!collection_save(cw->table->cd, cw->table->cd->path))
1297 log_printf("failed saving to collection path: %s\n", cw->table->cd->path);
1306 static gboolean collection_window_delete(GtkWidget *, GdkEvent *, gpointer data)
1308 auto cw = static_cast<CollectWindow *>(data);
1309 collection_window_close(cw);
1314 CollectWindow *collection_window_new(const gchar *path)
1319 GtkWidget *status_label;
1320 GtkWidget *extra_label;
1321 GdkGeometry geometry;
1323 /* If the collection is already opened in another window, return that one */
1324 cw = collection_window_find_by_path(path);
1330 cw = g_new0(CollectWindow, 1);
1332 collection_window_list = g_list_append(collection_window_list, cw);
1334 cw->cd = collection_new(path);
1336 cw->window = window_new("collection", PIXBUF_INLINE_ICON_BOOK, nullptr, nullptr);
1337 DEBUG_NAME(cw->window);
1339 geometry.min_width = DEFAULT_MINIMAL_WINDOW_SIZE;
1340 geometry.min_height = DEFAULT_MINIMAL_WINDOW_SIZE;
1341 geometry.base_width = COLLECT_DEF_WIDTH;
1342 geometry.base_height = COLLECT_DEF_HEIGHT;
1343 gtk_window_set_geometry_hints(GTK_WINDOW(cw->window), nullptr, &geometry,
1344 static_cast<GdkWindowHints>(GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE));
1346 if (options->collections_on_top)
1348 gq_gtk_window_set_keep_above(GTK_WINDOW(cw->window), TRUE);
1351 if (options->save_window_positions && path && collection_load_only_geometry(cw->cd, path))
1353 gtk_window_set_default_size(GTK_WINDOW(cw->window), cw->cd->window.width, cw->cd->window.height);
1354 gq_gtk_window_move(GTK_WINDOW(cw->window), cw->cd->window.x, cw->cd->window.y);
1358 gtk_window_set_default_size(GTK_WINDOW(cw->window), COLLECT_DEF_WIDTH, COLLECT_DEF_HEIGHT);
1361 gtk_window_set_resizable(GTK_WINDOW(cw->window), TRUE);
1362 collection_window_update_title(cw);
1363 gtk_container_set_border_width(GTK_CONTAINER(cw->window), 0);
1365 g_signal_connect(G_OBJECT(cw->window), "delete_event",
1366 G_CALLBACK(collection_window_delete), cw);
1368 g_signal_connect(G_OBJECT(cw->window), "key_press_event",
1369 G_CALLBACK(collection_window_keypress), cw);
1371 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
1372 gq_gtk_container_add(GTK_WIDGET(cw->window), vbox);
1373 gtk_widget_show(vbox);
1375 cw->table = collection_table_new(cw->cd);
1376 gq_gtk_box_pack_start(GTK_BOX(vbox), cw->table->scrolled, TRUE, TRUE, 0);
1377 gtk_widget_show(cw->table->scrolled);
1379 cw->status_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
1380 gq_gtk_box_pack_start(GTK_BOX(vbox), cw->status_box, FALSE, FALSE, 0);
1381 gtk_widget_show(cw->status_box);
1383 frame = gtk_frame_new(nullptr);
1385 gq_gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
1386 gq_gtk_box_pack_start(GTK_BOX(cw->status_box), frame, TRUE, TRUE, 0);
1387 gtk_widget_show(frame);
1389 status_label = gtk_label_new("");
1390 gq_gtk_container_add(GTK_WIDGET(frame), status_label);
1391 gtk_widget_show(status_label);
1393 extra_label = gtk_progress_bar_new();
1394 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(extra_label), 0.0);
1395 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(extra_label), "");
1396 gtk_progress_bar_set_show_text(GTK_PROGRESS_BAR(extra_label), TRUE);
1398 gq_gtk_box_pack_start(GTK_BOX(cw->status_box), extra_label, TRUE, TRUE, 0);
1399 gtk_widget_show(extra_label);
1401 collection_table_set_labels(cw->table, status_label, extra_label);
1403 gtk_widget_show(cw->window);
1404 gtk_widget_grab_focus(cw->table->listview);
1406 collection_set_update_info_func(cw->cd, collection_window_update_info, cw);
1408 if (path && *path == G_DIR_SEPARATOR) collection_load_begin(cw->cd, nullptr, COLLECTION_LOAD_NONE);
1412 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */