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.
26 #include "collect-dlg.h"
27 #include "collect-io.h"
28 #include "collect-table.h"
34 #include "layout-image.h"
35 #include "layout-util.h"
36 #include "main-defines.h"
38 #include "pixbuf-util.h"
40 #include "ui-fileops.h"
41 #include "ui-tree-edit.h"
46 COLLECT_DEF_WIDTH = 440,
47 COLLECT_DEF_HEIGHT = 450
51 * list of paths to collections */
54 * @brief List of currently open Collections.
56 * Type ::_CollectionData
58 static GList *collection_list = nullptr;
61 * @brief List of currently open Collection windows.
63 * Type ::_CollectWindow
65 static GList *collection_window_list = nullptr;
67 static void collection_window_get_geometry(CollectWindow *cw);
68 static void collection_window_refresh(CollectWindow *cw);
69 static void collection_window_update_title(CollectWindow *cw);
70 static void collection_window_add(CollectWindow *cw, CollectInfo *ci);
71 static void collection_window_insert(CollectWindow *cw, CollectInfo *ci);
72 static void collection_window_remove(CollectWindow *cw, CollectInfo *ci);
73 static void collection_window_update(CollectWindow *cw, CollectInfo *ci);
75 static void collection_window_close(CollectWindow *cw);
77 static void collection_notify_cb(FileData *fd, NotifyType type, gpointer data);
80 *-------------------------------------------------------------------
82 *-------------------------------------------------------------------
85 CollectInfo *collection_info_new(FileData *fd, struct stat *, GdkPixbuf *pixbuf)
89 if (!fd) return nullptr;
91 ci = g_new0(CollectInfo, 1);
92 ci->fd = file_data_ref(fd);
95 if (ci->pixbuf) g_object_ref(ci->pixbuf);
100 void collection_info_free_thumb(CollectInfo *ci)
102 if (ci->pixbuf) g_object_unref(ci->pixbuf);
103 ci->pixbuf = nullptr;
106 void collection_info_free(CollectInfo *ci)
110 file_data_unref(ci->fd);
111 collection_info_free_thumb(ci);
115 void collection_info_set_thumb(CollectInfo *ci, GdkPixbuf *pixbuf)
117 if (pixbuf) g_object_ref(pixbuf);
118 collection_info_free_thumb(ci);
122 #pragma GCC diagnostic push
123 #pragma GCC diagnostic ignored "-Wunused-function"
124 gboolean collection_info_load_thumb_unused(CollectInfo *ci)
126 if (!ci) return FALSE;
128 collection_info_free_thumb(ci);
130 log_printf("collection_info_load_thumb not implemented!\n(because an instant thumb loader not implemented)");
133 #pragma GCC diagnostic pop
135 /* an ugly static var, well what ya gonna do ? */
136 static SortType collection_list_sort_method = SORT_NAME;
138 static gint collection_list_sort_cb(gconstpointer a, gconstpointer b)
140 auto cia = static_cast<const CollectInfo *>(a);
141 auto cib = static_cast<const CollectInfo *>(b);
143 switch (collection_list_sort_method)
151 if (cia->fd->size < cib->fd->size) return -1;
152 if (cia->fd->size > cib->fd->size) return 1;
156 if (cia->fd->date < cib->fd->date) return -1;
157 if (cia->fd->date > cib->fd->date) return 1;
161 if (cia->fd->cdate < cib->fd->cdate) return -1;
162 if (cia->fd->cdate > cib->fd->cdate) return 1;
166 if (cia->fd->exifdate < cib->fd->exifdate) return -1;
167 if (cia->fd->exifdate > cib->fd->exifdate) return 1;
169 case SORT_EXIFTIMEDIGITIZED:
170 if (cia->fd->exifdate_digitized < cib->fd->exifdate_digitized) return -1;
171 if (cia->fd->exifdate_digitized > cib->fd->exifdate_digitized) return 1;
174 if (cia->fd->rating < cib->fd->rating) return -1;
175 if (cia->fd->rating > cib->fd->rating) return 1;
178 return utf8_compare(cia->fd->path, cib->fd->path, options->file_sort.case_sensitive);
181 if (cia->fd->format_class < cib->fd->format_class) return -1;
182 if (cia->fd->format_class > cib->fd->format_class) return 1;
188 if (options->file_sort.case_sensitive)
189 return strcmp(cia->fd->collate_key_name, cib->fd->collate_key_name);
191 return strcmp(cia->fd->collate_key_name_nocase, cib->fd->collate_key_name_nocase);
194 GList *collection_list_sort(GList *list, SortType method)
196 if (method == SORT_NONE) return list;
198 collection_list_sort_method = method;
200 return g_list_sort(list, collection_list_sort_cb);
203 GList *collection_list_randomize(GList *list)
212 length = g_list_length(list);
213 if (!length) return nullptr;
215 srand(static_cast<unsigned int>(time(nullptr))); // Initialize random generator (hasn't to be that much strong)
217 for (i = 0; i < length; i++)
219 random = static_cast<guint>(1.0 * length * rand()/(RAND_MAX + 1.0));
220 olist = g_list_nth(list, i);
221 nlist = g_list_nth(list, random);
223 olist->data = nlist->data;
230 GList *collection_list_add(GList *list, CollectInfo *ci, SortType method)
232 if (method != SORT_NONE)
234 collection_list_sort_method = method;
235 list = g_list_insert_sorted(list, ci, collection_list_sort_cb);
239 list = g_list_append(list, ci);
245 GList *collection_list_insert(GList *list, CollectInfo *ci, CollectInfo *insert_ci, SortType method)
247 if (method != SORT_NONE)
249 collection_list_sort_method = method;
250 list = g_list_insert_sorted(list, ci, collection_list_sort_cb);
256 point = g_list_find(list, insert_ci);
257 list = uig_list_insert_link(list, point, ci);
263 GList *collection_list_remove(GList *list, CollectInfo *ci)
265 list = g_list_remove(list, ci);
266 collection_info_free(ci);
270 CollectInfo *collection_list_find_fd(GList *list, FileData *fd)
276 auto ci = static_cast<CollectInfo *>(work->data);
277 if (ci->fd == fd) return ci;
284 GList *collection_list_to_filelist(GList *list)
286 GList *filelist = nullptr;
291 auto info = static_cast<CollectInfo *>(work->data);
292 filelist = g_list_prepend(filelist, file_data_ref(info->fd));
296 filelist = g_list_reverse(filelist);
300 CollectWindow *collection_window_find(CollectionData *cd)
304 work = collection_window_list;
307 auto cw = static_cast<CollectWindow *>(work->data);
308 if (cw->cd == cd) return cw;
315 CollectWindow *collection_window_find_by_path(const gchar *path)
319 if (!path) return nullptr;
321 work = collection_window_list;
324 auto cw = static_cast<CollectWindow *>(work->data);
325 if (cw->cd->path && strcmp(cw->cd->path, path) == 0) return cw;
333 * @brief Checks string for existence of Collection.
334 * @param[in] param Filename, with or without extension of any collection
335 * @returns full pathname if found or NULL
337 * Return value must be freed with g_free()
339 gchar *collection_path(const gchar *param)
341 gchar *path = nullptr;
342 gchar *full_name = nullptr;
344 if (file_extension_match(param, GQ_COLLECTION_EXT))
346 path = g_build_filename(get_collections_dir(), param, NULL);
348 else if (file_extension_match(param, nullptr))
350 full_name = g_strconcat(param, GQ_COLLECTION_EXT, NULL);
351 path = g_build_filename(get_collections_dir(), full_name, NULL);
365 * @brief Checks input string for existence of Collection.
366 * @param[in] param Filename with or without extension of any collection
367 * @returns TRUE if found
371 gboolean is_collection(const gchar *param)
373 gchar *name = nullptr;
375 name = collection_path(param);
385 * @brief Creates a text list of the image paths of the contents of a Collection
386 * @param[in] name The name of the collection, with or without extension
387 * @param[inout] contents A GString to which the image paths are appended
391 void collection_contents(const gchar *name, GString **contents)
399 if (is_collection(name))
401 path = collection_path(name);
402 cd = collection_new("");
403 collection_load(cd, path, COLLECTION_LOAD_APPEND);
407 ci = static_cast<CollectInfo *>(work->data);
409 *contents = g_string_append(*contents, fd->path);
410 *contents = g_string_append(*contents, "\n");
420 * @brief Returns a list of filedatas of the contents of a Collection
421 * @param[in] name The name of the collection, with or without extension
425 GList *collection_contents_fd(const gchar *name)
431 GList *list = nullptr;
433 if (is_collection(name))
435 path = collection_path(name);
436 cd = collection_new("");
437 collection_load(cd, path, COLLECTION_LOAD_APPEND);
441 ci = static_cast<CollectInfo *>(work->data);
442 list = g_list_append(list, ci->fd);
454 *-------------------------------------------------------------------
455 * please use these to actually add/remove stuff
456 *-------------------------------------------------------------------
459 CollectionData *collection_new(const gchar *path)
462 static gint untitled_counter = 0;
464 cd = g_new0(CollectionData, 1);
466 cd->ref = 1; /* starts with a ref of 1 */
467 cd->sort_method = SORT_NONE;
468 cd->window.width = COLLECT_DEF_WIDTH;
469 cd->window.height = COLLECT_DEF_HEIGHT;
470 cd->existence = g_hash_table_new(nullptr, nullptr);
474 cd->path = g_strdup(path);
475 cd->name = g_strdup(filename_from_path(cd->path));
480 if (untitled_counter == 0)
482 cd->name = g_strdup(_("Untitled"));
486 cd->name = g_strdup_printf(_("Untitled (%d)"), untitled_counter + 1);
492 file_data_register_notify_func(collection_notify_cb, cd, NOTIFY_PRIORITY_MEDIUM);
495 collection_list = g_list_append(collection_list, cd);
500 void collection_free(CollectionData *cd)
504 DEBUG_1("collection \"%s\" freed", cd->name);
506 collection_load_stop(cd);
507 g_list_free_full(cd->list, reinterpret_cast<GDestroyNotify>(collection_info_free));
509 file_data_unregister_notify_func(collection_notify_cb, cd);
511 collection_list = g_list_remove(collection_list, cd);
513 g_hash_table_destroy(cd->existence);
515 g_free(cd->collection_path);
522 void collection_ref(CollectionData *cd)
526 DEBUG_1("collection \"%s\" ref count = %d", cd->name, cd->ref);
529 void collection_unref(CollectionData *cd)
533 DEBUG_1("collection \"%s\" ref count = %d", cd->name, cd->ref);
541 void collection_path_changed(CollectionData *cd)
543 collection_window_update_title(collection_window_find(cd));
546 gint collection_to_number(CollectionData *cd)
548 return g_list_index(collection_list, cd);
551 CollectionData *collection_from_number(gint n)
553 return static_cast<CollectionData *>(g_list_nth_data(collection_list, n));
556 CollectionData *collection_from_dnd_data(const gchar *data, GList **list, GList **info_list)
559 gint collection_number;
562 if (list) *list = nullptr;
563 if (info_list) *info_list = nullptr;
565 if (strncmp(data, "COLLECTION:", 11) != 0) return nullptr;
569 collection_number = atoi(ptr);
570 cd = collection_from_number(collection_number);
571 if (!cd) return nullptr;
573 if (!list && !info_list) return cd;
575 while (*ptr != '\0' && *ptr != '\n' ) ptr++;
576 if (*ptr == '\0') return cd;
584 item_number = static_cast<guint>(atoi(ptr));
585 while (*ptr != '\n' && *ptr != '\0') ptr++;
589 while (*ptr == '\n') ptr++;
591 info = static_cast<CollectInfo *>(g_list_nth_data(cd->list, item_number));
594 if (list) *list = g_list_append(*list, file_data_ref(info->fd));
595 if (info_list) *info_list = g_list_append(*info_list, info);
601 gchar *collection_info_list_to_dnd_data(CollectionData *cd, GList *list, gint *length)
604 GList *temp = nullptr;
608 gint collection_number;
611 if (!list) return nullptr;
613 collection_number = collection_to_number(cd);
614 if (collection_number < 0) return nullptr;
616 text = g_strdup_printf("COLLECTION:%d\n", collection_number);
617 *length += strlen(text);
618 temp = g_list_prepend(temp, text);
623 gint item_number = g_list_index(cd->list, work->data);
627 if (item_number < 0) continue;
629 text = g_strdup_printf("%d\n", item_number);
630 temp = g_list_prepend(temp, text);
631 *length += strlen(text);
634 *length += 1; /* ending nul char */
636 uri_text = static_cast<gchar *>(g_malloc(*length));
639 work = g_list_last(temp);
643 auto text = static_cast<gchar *>(work->data);
648 memcpy(ptr, text, len);
654 g_list_free_full(temp, g_free);
659 gint collection_info_valid(CollectionData *cd, CollectInfo *info)
661 if (collection_to_number(cd) < 0) return FALSE;
663 return (g_list_index(cd->list, info) != 0);
666 CollectInfo *collection_next_by_info(CollectionData *cd, CollectInfo *info)
670 work = g_list_find(cd->list, info);
672 if (!work) return nullptr;
674 if (work) return static_cast<CollectInfo *>(work->data);
678 CollectInfo *collection_prev_by_info(CollectionData *cd, CollectInfo *info)
682 work = g_list_find(cd->list, info);
684 if (!work) return nullptr;
686 if (work) return static_cast<CollectInfo *>(work->data);
690 CollectInfo *collection_get_first(CollectionData *cd)
692 if (cd->list) return static_cast<CollectInfo *>(cd->list->data);
697 CollectInfo *collection_get_last(CollectionData *cd)
701 list = g_list_last(cd->list);
703 if (list) return static_cast<CollectInfo *>(list->data);
708 void collection_set_sort_method(CollectionData *cd, SortType method)
712 if (cd->sort_method == method) return;
714 cd->sort_method = method;
715 cd->list = collection_list_sort(cd->list, cd->sort_method);
716 if (cd->list) cd->changed = TRUE;
718 collection_window_refresh(collection_window_find(cd));
721 void collection_randomize(CollectionData *cd)
725 cd->list = collection_list_randomize(cd->list);
726 cd->sort_method = SORT_NONE;
727 if (cd->list) cd->changed = TRUE;
729 collection_window_refresh(collection_window_find(cd));
732 void collection_set_update_info_func(CollectionData *cd,
733 void (*func)(CollectionData *, CollectInfo *, gpointer), gpointer data)
735 cd->info_updated_func = func;
736 cd->info_updated_data = data;
739 static CollectInfo *collection_info_new_if_not_exists(CollectionData *cd, struct stat *st, FileData *fd)
743 if (!options->collections_duplicates)
745 if (g_hash_table_lookup(cd->existence, fd->path)) return nullptr;
748 ci = collection_info_new(fd, st, nullptr);
749 if (ci) g_hash_table_insert(cd->existence, fd->path, g_strdup(""));
753 gboolean collection_add_check(CollectionData *cd, FileData *fd, gboolean sorted, gboolean must_exist)
758 if (!fd) return FALSE;
760 g_assert(fd->magick == FD_MAGICK);
764 valid = (stat_utf8(fd->path, &st) && !S_ISDIR(st.st_mode));
777 ci = collection_info_new_if_not_exists(cd, &st, fd);
778 if (!ci) return FALSE;
779 DEBUG_3("add to collection: %s", fd->path);
781 cd->list = collection_list_add(cd->list, ci, sorted ? cd->sort_method : SORT_NONE);
784 if (!sorted || cd->sort_method == SORT_NONE)
786 collection_window_add(collection_window_find(cd), ci);
790 collection_window_insert(collection_window_find(cd), ci);
797 gboolean collection_add(CollectionData *cd, FileData *fd, gboolean sorted)
799 return collection_add_check(cd, fd, sorted, TRUE);
802 gboolean collection_insert(CollectionData *cd, FileData *fd, CollectInfo *insert_ci, gboolean sorted)
806 if (!insert_ci) return collection_add(cd, fd, sorted);
808 if (stat_utf8(fd->path, &st) >= 0 && !S_ISDIR(st.st_mode))
812 ci = collection_info_new_if_not_exists(cd, &st, fd);
813 if (!ci) return FALSE;
815 DEBUG_3("insert in collection: %s", fd->path);
817 cd->list = collection_list_insert(cd->list, ci, insert_ci, sorted ? cd->sort_method : SORT_NONE);
820 collection_window_insert(collection_window_find(cd), ci);
828 gboolean collection_remove(CollectionData *cd, FileData *fd)
832 ci = collection_list_find_fd(cd->list, fd);
834 if (!ci) return FALSE;
836 g_hash_table_remove(cd->existence, fd->path);
838 cd->list = g_list_remove(cd->list, ci);
841 collection_window_remove(collection_window_find(cd), ci);
842 collection_info_free(ci);
847 static void collection_remove_by_info(CollectionData *cd, CollectInfo *info)
849 if (!info || !g_list_find(cd->list, info)) return;
851 cd->list = g_list_remove(cd->list, info);
852 cd->changed = (cd->list != nullptr);
854 collection_window_remove(collection_window_find(cd), info);
855 collection_info_free(info);
858 void collection_remove_by_info_list(CollectionData *cd, GList *list)
866 /* more efficient (in collect-table) to remove a single item this way */
867 collection_remove_by_info(cd, static_cast<CollectInfo *>(list->data));
874 cd->list = collection_list_remove(cd->list, static_cast<CollectInfo *>(work->data));
877 cd->changed = (cd->list != nullptr);
879 collection_window_refresh(collection_window_find(cd));
882 gboolean collection_rename(CollectionData *cd, FileData *fd)
885 ci = collection_list_find_fd(cd->list, fd);
887 if (!ci) return FALSE;
891 collection_window_update(collection_window_find(cd), ci);
896 void collection_update_geometry(CollectionData *cd)
898 collection_window_get_geometry(collection_window_find(cd));
902 *-------------------------------------------------------------------
903 * simple maintenance for renaming, deleting
904 *-------------------------------------------------------------------
907 static void collection_notify_cb(FileData *fd, NotifyType type, gpointer data)
909 auto cd = static_cast<CollectionData *>(data);
911 if (!(type & NOTIFY_CHANGE) || !fd->change) return;
913 DEBUG_1("Notify collection: %s %04x", fd->path, type);
915 switch (fd->change->type)
917 case FILEDATA_CHANGE_MOVE:
918 case FILEDATA_CHANGE_RENAME:
919 collection_rename(cd, fd);
921 case FILEDATA_CHANGE_COPY:
923 case FILEDATA_CHANGE_DELETE:
924 while (collection_remove(cd, fd));
926 case FILEDATA_CHANGE_UNSPECIFIED:
927 case FILEDATA_CHANGE_WRITE_METADATA:
935 *-------------------------------------------------------------------
937 *-------------------------------------------------------------------
940 static gboolean collection_window_keypress(GtkWidget *, GdkEventKey *event, gpointer data)
942 auto cw = static_cast<CollectWindow *>(data);
943 gboolean stop_signal = FALSE;
946 if (event->state & GDK_CONTROL_MASK)
949 switch (event->keyval)
963 if (event->state & GDK_SHIFT_MASK)
965 collection_table_unselect_all(cw->table);
969 collection_table_select_all(cw->table);
973 list = layout_list(nullptr);
976 collection_table_add_filelist(cw->table, list);
981 file_util_copy(nullptr, collection_table_selection_get_list(cw->table), nullptr, cw->window);
984 file_util_move(nullptr, collection_table_selection_get_list(cw->table), nullptr, cw->window);
987 file_util_rename(nullptr, collection_table_selection_get_list(cw->table), cw->window);
990 options->file_ops.safe_delete_enable = TRUE;
991 file_util_delete(nullptr, collection_table_selection_get_list(cw->table), cw->window);
994 collection_dialog_save_as(cw->cd);
997 collection_window_close(cw);
1000 stop_signal = FALSE;
1007 switch (event->keyval)
1009 case GDK_KEY_Return: case GDK_KEY_KP_Enter:
1010 layout_image_set_collection(nullptr, cw->cd,
1011 collection_table_get_focus_info(cw->table));
1014 view_window_new_from_collection(cw->cd,
1015 collection_table_get_focus_info(cw->table));
1020 collection_dialog_save_as(cw->cd);
1022 else if (!collection_save(cw->cd, cw->cd->path))
1024 log_printf("failed saving to collection path: %s\n", cw->cd->path);
1028 collection_dialog_append(cw->cd);
1031 collection_set_sort_method(cw->cd, SORT_NAME);
1034 collection_set_sort_method(cw->cd, SORT_TIME);
1037 collection_set_sort_method(cw->cd, SORT_SIZE);
1040 if (event->state & GDK_SHIFT_MASK)
1044 info = collection_table_get_focus_info(cw->table);
1046 print_window_new(info->fd, collection_table_selection_get_list(cw->table),
1047 collection_list_to_filelist(cw->cd->list), cw->window);
1051 collection_set_sort_method(cw->cd, SORT_PATH);
1055 if (event->state & GDK_MOD1_MASK)
1057 options->collections.rectangular_selection = !(options->collections.rectangular_selection);
1060 case GDK_KEY_Delete: case GDK_KEY_KP_Delete:
1061 list = g_list_copy(cw->table->selection);
1064 collection_remove_by_info_list(cw->cd, list);
1065 collection_table_refresh(cw->table);
1070 collection_remove_by_info(cw->cd, collection_table_get_focus_info(cw->table));
1074 stop_signal = FALSE;
1078 if (!stop_signal && is_help_key(event))
1080 help_window_show("GuideCollections.html");
1088 *-------------------------------------------------------------------
1090 *-------------------------------------------------------------------
1092 static void collection_window_get_geometry(CollectWindow *cw)
1100 window = gtk_widget_get_window(cw->window);
1101 gdk_window_get_position(window, &cd->window.x, &cd->window.y);
1102 cd->window.width = gdk_window_get_width(window);
1103 cd->window.height = gdk_window_get_height(window);
1104 cd->window_read = TRUE;
1107 static void collection_window_refresh(CollectWindow *cw)
1111 collection_table_refresh(cw->table);
1114 static void collection_window_update_title(CollectWindow *cw)
1116 gboolean free_name = FALSE;
1122 if (file_extension_match(cw->cd->name, GQ_COLLECTION_EXT))
1124 name = remove_extension_from_path(cw->cd->name);
1129 name = cw->cd->name;
1132 buf = g_strdup_printf(_("%s - Collection - %s"), name, GQ_APPNAME);
1133 if (free_name) g_free(name);
1134 gtk_window_set_title(GTK_WINDOW(cw->window), buf);
1138 static void collection_window_update_info(CollectionData *, CollectInfo *ci, gpointer data)
1140 auto cw = static_cast<CollectWindow *>(data);
1142 collection_table_file_update(cw->table, ci);
1145 static void collection_window_add(CollectWindow *cw, CollectInfo *ci)
1149 if (!ci->pixbuf) collection_load_thumb_idle(cw->cd);
1150 collection_table_file_add(cw->table, ci);
1153 static void collection_window_insert(CollectWindow *cw, CollectInfo *ci)
1157 if (!ci->pixbuf) collection_load_thumb_idle(cw->cd);
1158 collection_table_file_insert(cw->table, ci);
1162 static void collection_window_remove(CollectWindow *cw, CollectInfo *ci)
1166 collection_table_file_remove(cw->table, ci);
1169 static void collection_window_update(CollectWindow *cw, CollectInfo *ci)
1173 collection_table_file_update(cw->table, ci);
1174 collection_table_file_update(cw->table, nullptr);
1177 static void collection_window_close_final(CollectWindow *cw)
1179 if (cw->close_dialog) return;
1181 collection_window_list = g_list_remove(collection_window_list, cw);
1182 collection_window_get_geometry(cw);
1184 gq_gtk_widget_destroy(cw->window);
1186 collection_set_update_info_func(cw->cd, nullptr, nullptr);
1187 collection_unref(cw->cd);
1192 static void collection_close_save_cb(GenericDialog *gd, gpointer data)
1194 auto cw = static_cast<CollectWindow *>(data);
1196 cw->close_dialog = nullptr;
1197 generic_dialog_close(gd);
1201 collection_dialog_save_close(cw->cd);
1205 if (!collection_save(cw->cd, cw->cd->path))
1208 buf = g_strdup_printf(_("Failed to save the collection:\n%s"), cw->cd->path);
1209 warning_dialog(_("Save Failed"), buf, GQ_ICON_DIALOG_ERROR, cw->window);
1214 collection_window_close_final(cw);
1217 static void collection_close_close_cb(GenericDialog *gd, gpointer data)
1219 auto cw = static_cast<CollectWindow *>(data);
1221 cw->close_dialog = nullptr;
1222 generic_dialog_close(gd);
1224 collection_window_close_final(cw);
1227 static void collection_close_cancel_cb(GenericDialog *gd, gpointer data)
1229 auto cw = static_cast<CollectWindow *>(data);
1231 cw->close_dialog = nullptr;
1232 generic_dialog_close(gd);
1235 static void collection_close_dlg_show(CollectWindow *cw)
1239 if (cw->close_dialog)
1241 gtk_window_present(GTK_WINDOW(cw->close_dialog));
1245 gd = generic_dialog_new(_("Close collection"),
1246 "close_collection", cw->window, FALSE,
1247 collection_close_cancel_cb, cw);
1248 generic_dialog_add_message(gd, GQ_ICON_DIALOG_QUESTION,
1249 _("Close collection"),
1250 _("Collection has been modified.\nSave first?"), TRUE);
1252 generic_dialog_add_button(gd, GQ_ICON_SAVE, _("Save"), collection_close_save_cb, TRUE);
1253 generic_dialog_add_button(gd, GQ_ICON_DELETE, _("_Discard"), collection_close_close_cb, FALSE);
1255 cw->close_dialog = gd->dialog;
1257 gtk_widget_show(gd->dialog);
1260 static void collection_window_close(CollectWindow *cw)
1262 if (!cw->cd->changed && !cw->close_dialog)
1264 collection_window_close_final(cw);
1268 collection_close_dlg_show(cw);
1271 void collection_window_close_by_collection(CollectionData *cd)
1275 cw = collection_window_find(cd);
1276 if (cw) collection_window_close_final(cw);
1280 * @brief Check if any Collection windows have unsaved data
1281 * @returns TRUE if unsaved data exists
1283 * Also saves window geometry for Collection windows that have
1286 gboolean collection_window_modified_exists()
1293 work = collection_window_list;
1296 auto cw = static_cast<CollectWindow *>(work->data);
1297 if (cw->cd->changed)
1303 if (!collection_save(cw->table->cd, cw->table->cd->path))
1305 log_printf("failed saving to collection path: %s\n", cw->table->cd->path);
1314 static gboolean collection_window_delete(GtkWidget *, GdkEvent *, gpointer data)
1316 auto cw = static_cast<CollectWindow *>(data);
1317 collection_window_close(cw);
1322 CollectWindow *collection_window_new(const gchar *path)
1327 GtkWidget *status_label;
1328 GtkWidget *extra_label;
1329 GdkGeometry geometry;
1331 /* If the collection is already opened in another window, return that one */
1332 cw = collection_window_find_by_path(path);
1338 cw = g_new0(CollectWindow, 1);
1340 collection_window_list = g_list_append(collection_window_list, cw);
1342 cw->cd = collection_new(path);
1344 cw->window = window_new("collection", PIXBUF_INLINE_ICON_BOOK, nullptr, nullptr);
1345 DEBUG_NAME(cw->window);
1347 geometry.min_width = DEFAULT_MINIMAL_WINDOW_SIZE;
1348 geometry.min_height = DEFAULT_MINIMAL_WINDOW_SIZE;
1349 geometry.base_width = COLLECT_DEF_WIDTH;
1350 geometry.base_height = COLLECT_DEF_HEIGHT;
1351 gtk_window_set_geometry_hints(GTK_WINDOW(cw->window), nullptr, &geometry,
1352 static_cast<GdkWindowHints>(GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE));
1354 if (options->collections_on_top)
1356 gq_gtk_window_set_keep_above(GTK_WINDOW(cw->window), TRUE);
1359 if (options->save_window_positions && path && collection_load_only_geometry(cw->cd, path))
1361 gtk_window_set_default_size(GTK_WINDOW(cw->window), cw->cd->window.width, cw->cd->window.height);
1362 gq_gtk_window_move(GTK_WINDOW(cw->window), cw->cd->window.x, cw->cd->window.y);
1366 gtk_window_set_default_size(GTK_WINDOW(cw->window), COLLECT_DEF_WIDTH, COLLECT_DEF_HEIGHT);
1369 gtk_window_set_resizable(GTK_WINDOW(cw->window), TRUE);
1370 collection_window_update_title(cw);
1371 gtk_container_set_border_width(GTK_CONTAINER(cw->window), 0);
1373 g_signal_connect(G_OBJECT(cw->window), "delete_event",
1374 G_CALLBACK(collection_window_delete), cw);
1376 g_signal_connect(G_OBJECT(cw->window), "key_press_event",
1377 G_CALLBACK(collection_window_keypress), cw);
1379 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
1380 gq_gtk_container_add(GTK_WIDGET(cw->window), vbox);
1381 gtk_widget_show(vbox);
1383 cw->table = collection_table_new(cw->cd);
1384 gq_gtk_box_pack_start(GTK_BOX(vbox), cw->table->scrolled, TRUE, TRUE, 0);
1385 gtk_widget_show(cw->table->scrolled);
1387 cw->status_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
1388 gq_gtk_box_pack_start(GTK_BOX(vbox), cw->status_box, FALSE, FALSE, 0);
1389 gtk_widget_show(cw->status_box);
1391 frame = gtk_frame_new(nullptr);
1393 gq_gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
1394 gq_gtk_box_pack_start(GTK_BOX(cw->status_box), frame, TRUE, TRUE, 0);
1395 gtk_widget_show(frame);
1397 status_label = gtk_label_new("");
1398 gq_gtk_container_add(GTK_WIDGET(frame), status_label);
1399 gtk_widget_show(status_label);
1401 extra_label = gtk_progress_bar_new();
1402 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(extra_label), 0.0);
1403 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(extra_label), "");
1404 gtk_progress_bar_set_show_text(GTK_PROGRESS_BAR(extra_label), TRUE);
1406 gq_gtk_box_pack_start(GTK_BOX(cw->status_box), extra_label, TRUE, TRUE, 0);
1407 gtk_widget_show(extra_label);
1409 collection_table_set_labels(cw->table, status_label, extra_label);
1411 gtk_widget_show(cw->window);
1412 gtk_widget_grab_focus(cw->table->listview);
1414 collection_set_update_info_func(cw->cd, collection_window_update_info, cw);
1416 if (path && *path == G_DIR_SEPARATOR) collection_load_begin(cw->cd, nullptr, COLLECTION_LOAD_NONE);
1420 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */