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.
31 #include <glib-object.h>
33 #include "collect-dlg.h"
34 #include "collect-io.h"
35 #include "collect-table.h"
41 #include "layout-image.h"
42 #include "layout-util.h"
44 #include "main-defines.h"
47 #include "pixbuf-util.h"
49 #include "ui-fileops.h"
50 #include "ui-tree-edit.h"
51 #include "ui-utildlg.h"
56 COLLECT_DEF_WIDTH = 440,
57 COLLECT_DEF_HEIGHT = 450
61 * list of paths to collections */
64 * @brief List of currently open Collections.
66 * Type ::_CollectionData
68 static GList *collection_list = nullptr;
71 * @brief List of currently open Collection windows.
73 * Type ::_CollectWindow
75 static GList *collection_window_list = nullptr;
77 static void collection_window_get_geometry(CollectWindow *cw);
78 static void collection_window_refresh(CollectWindow *cw);
79 static void collection_window_update_title(CollectWindow *cw);
80 static void collection_window_add(CollectWindow *cw, CollectInfo *ci);
81 static void collection_window_insert(CollectWindow *cw, CollectInfo *ci);
82 static void collection_window_remove(CollectWindow *cw, CollectInfo *ci);
83 static void collection_window_update(CollectWindow *cw, CollectInfo *ci);
85 static void collection_window_close(CollectWindow *cw);
87 static void collection_notify_cb(FileData *fd, NotifyType type, gpointer data);
90 *-------------------------------------------------------------------
92 *-------------------------------------------------------------------
95 CollectInfo *collection_info_new(FileData *fd, struct stat *, GdkPixbuf *pixbuf)
99 if (!fd) return nullptr;
101 ci = g_new0(CollectInfo, 1);
102 ci->fd = file_data_ref(fd);
105 if (ci->pixbuf) g_object_ref(ci->pixbuf);
110 void collection_info_free_thumb(CollectInfo *ci)
112 if (ci->pixbuf) g_object_unref(ci->pixbuf);
113 ci->pixbuf = nullptr;
116 void collection_info_free(CollectInfo *ci)
120 file_data_unref(ci->fd);
121 collection_info_free_thumb(ci);
125 void collection_info_set_thumb(CollectInfo *ci, GdkPixbuf *pixbuf)
127 if (pixbuf) g_object_ref(pixbuf);
128 collection_info_free_thumb(ci);
132 #pragma GCC diagnostic push
133 #pragma GCC diagnostic ignored "-Wunused-function"
134 gboolean collection_info_load_thumb_unused(CollectInfo *ci)
136 if (!ci) return FALSE;
138 collection_info_free_thumb(ci);
140 log_printf("collection_info_load_thumb not implemented!\n(because an instant thumb loader not implemented)");
143 #pragma GCC diagnostic pop
145 /* an ugly static var, well what ya gonna do ? */
146 static SortType collection_list_sort_method = SORT_NAME;
148 static gint collection_list_sort_cb(gconstpointer a, gconstpointer b)
150 auto cia = static_cast<const CollectInfo *>(a);
151 auto cib = static_cast<const CollectInfo *>(b);
153 switch (collection_list_sort_method)
161 if (cia->fd->size < cib->fd->size) return -1;
162 if (cia->fd->size > cib->fd->size) return 1;
166 if (cia->fd->date < cib->fd->date) return -1;
167 if (cia->fd->date > cib->fd->date) return 1;
171 if (cia->fd->cdate < cib->fd->cdate) return -1;
172 if (cia->fd->cdate > cib->fd->cdate) return 1;
176 if (cia->fd->exifdate < cib->fd->exifdate) return -1;
177 if (cia->fd->exifdate > cib->fd->exifdate) return 1;
179 case SORT_EXIFTIMEDIGITIZED:
180 if (cia->fd->exifdate_digitized < cib->fd->exifdate_digitized) return -1;
181 if (cia->fd->exifdate_digitized > cib->fd->exifdate_digitized) return 1;
184 if (cia->fd->rating < cib->fd->rating) return -1;
185 if (cia->fd->rating > cib->fd->rating) return 1;
188 return utf8_compare(cia->fd->path, cib->fd->path, options->file_sort.case_sensitive);
191 if (cia->fd->format_class < cib->fd->format_class) return -1;
192 if (cia->fd->format_class > cib->fd->format_class) return 1;
198 if (options->file_sort.case_sensitive)
199 return strcmp(cia->fd->collate_key_name, cib->fd->collate_key_name);
201 return strcmp(cia->fd->collate_key_name_nocase, cib->fd->collate_key_name_nocase);
204 GList *collection_list_sort(GList *list, SortType method)
206 if (method == SORT_NONE) return list;
208 collection_list_sort_method = method;
210 return g_list_sort(list, collection_list_sort_cb);
213 GList *collection_list_randomize(GList *list)
221 length = g_list_length(list);
222 if (!length) return nullptr;
224 srand(static_cast<unsigned int>(time(nullptr))); // Initialize random generator (hasn't to be that much strong)
226 for (i = 0; i < length; i++)
228 random = static_cast<guint>(1.0 * length * rand()/(RAND_MAX + 1.0));
229 olist = g_list_nth(list, i);
230 nlist = g_list_nth(list, random);
231 std::swap(olist->data, nlist->data);
237 GList *collection_list_add(GList *list, CollectInfo *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);
246 list = g_list_append(list, ci);
252 GList *collection_list_insert(GList *list, CollectInfo *ci, CollectInfo *insert_ci, SortType method)
254 if (method != SORT_NONE)
256 collection_list_sort_method = method;
257 list = g_list_insert_sorted(list, ci, collection_list_sort_cb);
263 point = g_list_find(list, insert_ci);
264 list = uig_list_insert_link(list, point, ci);
270 GList *collection_list_remove(GList *list, CollectInfo *ci)
272 list = g_list_remove(list, ci);
273 collection_info_free(ci);
277 CollectInfo *collection_list_find_fd(GList *list, FileData *fd)
283 auto ci = static_cast<CollectInfo *>(work->data);
284 if (ci->fd == fd) return ci;
291 GList *collection_list_to_filelist(GList *list)
293 GList *filelist = nullptr;
298 auto info = static_cast<CollectInfo *>(work->data);
299 filelist = g_list_prepend(filelist, file_data_ref(info->fd));
303 filelist = g_list_reverse(filelist);
307 CollectWindow *collection_window_find(CollectionData *cd)
311 work = collection_window_list;
314 auto cw = static_cast<CollectWindow *>(work->data);
315 if (cw->cd == cd) return cw;
322 CollectWindow *collection_window_find_by_path(const gchar *path)
326 if (!path) return nullptr;
328 work = collection_window_list;
331 auto cw = static_cast<CollectWindow *>(work->data);
332 if (cw->cd->path && strcmp(cw->cd->path, path) == 0) return cw;
340 * @brief Checks string for existence of Collection.
341 * @param[in] param Filename, with or without extension of any collection
342 * @returns full pathname if found or NULL
344 * Return value must be freed with g_free()
346 gchar *collection_path(const gchar *param)
348 gchar *path = nullptr;
349 gchar *full_name = nullptr;
351 if (file_extension_match(param, GQ_COLLECTION_EXT))
353 path = g_build_filename(get_collections_dir(), param, NULL);
355 else if (file_extension_match(param, nullptr))
357 full_name = g_strconcat(param, GQ_COLLECTION_EXT, NULL);
358 path = g_build_filename(get_collections_dir(), full_name, NULL);
372 * @brief Checks input string for existence of Collection.
373 * @param[in] param Filename with or without extension of any collection
374 * @returns TRUE if found
378 gboolean is_collection(const gchar *param)
380 gchar *name = nullptr;
382 name = collection_path(param);
392 * @brief Creates a text list of the image paths of the contents of a Collection
393 * @param[in] name The name of the collection, with or without extension
394 * @param[inout] contents A GString to which the image paths are appended
398 void collection_contents(const gchar *name, GString **contents)
406 if (is_collection(name))
408 path = collection_path(name);
409 cd = collection_new("");
410 collection_load(cd, path, COLLECTION_LOAD_APPEND);
414 ci = static_cast<CollectInfo *>(work->data);
416 *contents = g_string_append(*contents, fd->path);
417 *contents = g_string_append(*contents, "\n");
427 * @brief Returns a list of filedatas of the contents of a Collection
428 * @param[in] name The name of the collection, with or without extension
432 GList *collection_contents_fd(const gchar *name)
438 GList *list = nullptr;
440 if (is_collection(name))
442 path = collection_path(name);
443 cd = collection_new("");
444 collection_load(cd, path, COLLECTION_LOAD_APPEND);
448 ci = static_cast<CollectInfo *>(work->data);
449 list = g_list_append(list, ci->fd);
461 *-------------------------------------------------------------------
462 * please use these to actually add/remove stuff
463 *-------------------------------------------------------------------
466 CollectionData *collection_new(const gchar *path)
469 static gint untitled_counter = 0;
471 cd = g_new0(CollectionData, 1);
473 cd->ref = 1; /* starts with a ref of 1 */
474 cd->sort_method = SORT_NONE;
475 cd->window.width = COLLECT_DEF_WIDTH;
476 cd->window.height = COLLECT_DEF_HEIGHT;
477 cd->existence = g_hash_table_new(nullptr, nullptr);
481 cd->path = g_strdup(path);
482 cd->name = g_strdup(filename_from_path(cd->path));
487 if (untitled_counter == 0)
489 cd->name = g_strdup(_("Untitled"));
493 cd->name = g_strdup_printf(_("Untitled (%d)"), untitled_counter + 1);
499 file_data_register_notify_func(collection_notify_cb, cd, NOTIFY_PRIORITY_MEDIUM);
502 collection_list = g_list_append(collection_list, cd);
507 void collection_free(CollectionData *cd)
511 DEBUG_1("collection \"%s\" freed", cd->name);
513 collection_load_stop(cd);
514 g_list_free_full(cd->list, reinterpret_cast<GDestroyNotify>(collection_info_free));
516 file_data_unregister_notify_func(collection_notify_cb, cd);
518 collection_list = g_list_remove(collection_list, cd);
520 g_hash_table_destroy(cd->existence);
522 g_free(cd->collection_path);
529 void collection_ref(CollectionData *cd)
533 DEBUG_1("collection \"%s\" ref count = %d", cd->name, cd->ref);
536 void collection_unref(CollectionData *cd)
540 DEBUG_1("collection \"%s\" ref count = %d", cd->name, cd->ref);
548 void collection_path_changed(CollectionData *cd)
550 collection_window_update_title(collection_window_find(cd));
553 gint collection_to_number(CollectionData *cd)
555 return g_list_index(collection_list, cd);
558 CollectionData *collection_from_number(gint n)
560 return static_cast<CollectionData *>(g_list_nth_data(collection_list, n));
563 CollectionData *collection_from_dnd_data(const gchar *data, GList **list, GList **info_list)
566 gint collection_number;
569 if (list) *list = nullptr;
570 if (info_list) *info_list = nullptr;
572 if (strncmp(data, "COLLECTION:", 11) != 0) return nullptr;
576 collection_number = atoi(ptr);
577 cd = collection_from_number(collection_number);
578 if (!cd) return nullptr;
580 if (!list && !info_list) return cd;
582 while (*ptr != '\0' && *ptr != '\n' ) ptr++;
583 if (*ptr == '\0') return cd;
591 item_number = static_cast<guint>(atoi(ptr));
592 while (*ptr != '\n' && *ptr != '\0') ptr++;
596 while (*ptr == '\n') ptr++;
598 info = static_cast<CollectInfo *>(g_list_nth_data(cd->list, item_number));
601 if (list) *list = g_list_append(*list, file_data_ref(info->fd));
602 if (info_list) *info_list = g_list_append(*info_list, info);
608 gchar *collection_info_list_to_dnd_data(CollectionData *cd, GList *list, gint *length)
611 GList *temp = nullptr;
615 gint collection_number;
618 if (!list) return nullptr;
620 collection_number = collection_to_number(cd);
621 if (collection_number < 0) return nullptr;
623 text = g_strdup_printf("COLLECTION:%d\n", collection_number);
624 *length += strlen(text);
625 temp = g_list_prepend(temp, text);
630 gint item_number = g_list_index(cd->list, work->data);
634 if (item_number < 0) continue;
636 text = g_strdup_printf("%d\n", item_number);
637 temp = g_list_prepend(temp, text);
638 *length += strlen(text);
641 *length += 1; /* ending nul char */
643 uri_text = static_cast<gchar *>(g_malloc(*length));
646 work = g_list_last(temp);
650 auto text = static_cast<gchar *>(work->data);
655 memcpy(ptr, text, len);
661 g_list_free_full(temp, g_free);
666 gint collection_info_valid(CollectionData *cd, CollectInfo *info)
668 if (collection_to_number(cd) < 0) return FALSE;
670 return (g_list_index(cd->list, info) != 0);
673 CollectInfo *collection_next_by_info(CollectionData *cd, CollectInfo *info)
677 work = g_list_find(cd->list, info);
679 if (!work) return nullptr;
681 if (work) return static_cast<CollectInfo *>(work->data);
685 CollectInfo *collection_prev_by_info(CollectionData *cd, CollectInfo *info)
689 work = g_list_find(cd->list, info);
691 if (!work) return nullptr;
693 if (work) return static_cast<CollectInfo *>(work->data);
697 CollectInfo *collection_get_first(CollectionData *cd)
699 if (cd->list) return static_cast<CollectInfo *>(cd->list->data);
704 CollectInfo *collection_get_last(CollectionData *cd)
708 list = g_list_last(cd->list);
710 if (list) return static_cast<CollectInfo *>(list->data);
715 void collection_set_sort_method(CollectionData *cd, SortType method)
719 if (cd->sort_method == method) return;
721 cd->sort_method = method;
722 cd->list = collection_list_sort(cd->list, cd->sort_method);
723 if (cd->list) cd->changed = TRUE;
725 collection_window_refresh(collection_window_find(cd));
728 void collection_randomize(CollectionData *cd)
732 cd->list = collection_list_randomize(cd->list);
733 cd->sort_method = SORT_NONE;
734 if (cd->list) cd->changed = TRUE;
736 collection_window_refresh(collection_window_find(cd));
739 void collection_set_update_info_func(CollectionData *cd,
740 void (*func)(CollectionData *, CollectInfo *, gpointer), gpointer data)
742 cd->info_updated_func = func;
743 cd->info_updated_data = data;
746 static CollectInfo *collection_info_new_if_not_exists(CollectionData *cd, struct stat *st, FileData *fd)
750 if (!options->collections_duplicates)
752 if (g_hash_table_lookup(cd->existence, fd->path)) return nullptr;
755 ci = collection_info_new(fd, st, nullptr);
756 if (ci) g_hash_table_insert(cd->existence, fd->path, g_strdup(""));
760 gboolean collection_add_check(CollectionData *cd, FileData *fd, gboolean sorted, gboolean must_exist)
765 if (!fd) return FALSE;
767 g_assert(fd->magick == FD_MAGICK);
771 valid = (stat_utf8(fd->path, &st) && !S_ISDIR(st.st_mode));
784 ci = collection_info_new_if_not_exists(cd, &st, fd);
785 if (!ci) return FALSE;
786 DEBUG_3("add to collection: %s", fd->path);
788 cd->list = collection_list_add(cd->list, ci, sorted ? cd->sort_method : SORT_NONE);
791 if (!sorted || cd->sort_method == SORT_NONE)
793 collection_window_add(collection_window_find(cd), ci);
797 collection_window_insert(collection_window_find(cd), ci);
804 gboolean collection_add(CollectionData *cd, FileData *fd, gboolean sorted)
806 return collection_add_check(cd, fd, sorted, TRUE);
809 gboolean collection_insert(CollectionData *cd, FileData *fd, CollectInfo *insert_ci, gboolean sorted)
813 if (!insert_ci) return collection_add(cd, fd, sorted);
815 if (stat_utf8(fd->path, &st) >= 0 && !S_ISDIR(st.st_mode))
819 ci = collection_info_new_if_not_exists(cd, &st, fd);
820 if (!ci) return FALSE;
822 DEBUG_3("insert in collection: %s", fd->path);
824 cd->list = collection_list_insert(cd->list, ci, insert_ci, sorted ? cd->sort_method : SORT_NONE);
827 collection_window_insert(collection_window_find(cd), ci);
835 gboolean collection_remove(CollectionData *cd, FileData *fd)
839 ci = collection_list_find_fd(cd->list, fd);
841 if (!ci) return FALSE;
843 g_hash_table_remove(cd->existence, fd->path);
845 cd->list = g_list_remove(cd->list, ci);
848 collection_window_remove(collection_window_find(cd), ci);
849 collection_info_free(ci);
854 static void collection_remove_by_info(CollectionData *cd, CollectInfo *info)
856 if (!info || !g_list_find(cd->list, info)) return;
858 cd->list = g_list_remove(cd->list, info);
859 cd->changed = (cd->list != nullptr);
861 collection_window_remove(collection_window_find(cd), info);
862 collection_info_free(info);
865 void collection_remove_by_info_list(CollectionData *cd, GList *list)
873 /* more efficient (in collect-table) to remove a single item this way */
874 collection_remove_by_info(cd, static_cast<CollectInfo *>(list->data));
881 cd->list = collection_list_remove(cd->list, static_cast<CollectInfo *>(work->data));
884 cd->changed = (cd->list != nullptr);
886 collection_window_refresh(collection_window_find(cd));
889 gboolean collection_rename(CollectionData *cd, FileData *fd)
892 ci = collection_list_find_fd(cd->list, fd);
894 if (!ci) return FALSE;
898 collection_window_update(collection_window_find(cd), ci);
903 void collection_update_geometry(CollectionData *cd)
905 collection_window_get_geometry(collection_window_find(cd));
909 *-------------------------------------------------------------------
910 * simple maintenance for renaming, deleting
911 *-------------------------------------------------------------------
914 static void collection_notify_cb(FileData *fd, NotifyType type, gpointer data)
916 auto cd = static_cast<CollectionData *>(data);
918 if (!(type & NOTIFY_CHANGE) || !fd->change) return;
920 DEBUG_1("Notify collection: %s %04x", fd->path, type);
922 switch (fd->change->type)
924 case FILEDATA_CHANGE_MOVE:
925 case FILEDATA_CHANGE_RENAME:
926 collection_rename(cd, fd);
928 case FILEDATA_CHANGE_COPY:
930 case FILEDATA_CHANGE_DELETE:
931 while (collection_remove(cd, fd));
933 case FILEDATA_CHANGE_UNSPECIFIED:
934 case FILEDATA_CHANGE_WRITE_METADATA:
942 *-------------------------------------------------------------------
944 *-------------------------------------------------------------------
947 static gboolean collection_window_keypress(GtkWidget *, GdkEventKey *event, gpointer data)
949 auto cw = static_cast<CollectWindow *>(data);
950 gboolean stop_signal = FALSE;
953 if (event->state & GDK_CONTROL_MASK)
956 switch (event->keyval)
970 if (event->state & GDK_SHIFT_MASK)
972 collection_table_unselect_all(cw->table);
976 collection_table_select_all(cw->table);
980 list = layout_list(nullptr);
983 collection_table_add_filelist(cw->table, list);
988 file_util_copy(nullptr, collection_table_selection_get_list(cw->table), nullptr, cw->window);
991 file_util_move(nullptr, collection_table_selection_get_list(cw->table), nullptr, cw->window);
994 file_util_rename(nullptr, collection_table_selection_get_list(cw->table), cw->window);
997 options->file_ops.safe_delete_enable = TRUE;
998 file_util_delete(nullptr, collection_table_selection_get_list(cw->table), cw->window);
1001 collection_dialog_save_as(cw->cd);
1004 collection_window_close(cw);
1007 stop_signal = FALSE;
1014 switch (event->keyval)
1016 case GDK_KEY_Return: case GDK_KEY_KP_Enter:
1017 layout_image_set_collection(nullptr, cw->cd,
1018 collection_table_get_focus_info(cw->table));
1021 view_window_new_from_collection(cw->cd,
1022 collection_table_get_focus_info(cw->table));
1027 collection_dialog_save_as(cw->cd);
1029 else if (!collection_save(cw->cd, cw->cd->path))
1031 log_printf("failed saving to collection path: %s\n", cw->cd->path);
1035 collection_dialog_append(cw->cd);
1038 collection_set_sort_method(cw->cd, SORT_NAME);
1041 collection_set_sort_method(cw->cd, SORT_TIME);
1044 collection_set_sort_method(cw->cd, SORT_SIZE);
1047 if (event->state & GDK_SHIFT_MASK)
1051 info = collection_table_get_focus_info(cw->table);
1053 print_window_new(info->fd, collection_table_selection_get_list(cw->table),
1054 collection_list_to_filelist(cw->cd->list), cw->window);
1058 collection_set_sort_method(cw->cd, SORT_PATH);
1062 if (event->state & GDK_MOD1_MASK)
1064 options->collections.rectangular_selection = !(options->collections.rectangular_selection);
1067 case GDK_KEY_Delete: case GDK_KEY_KP_Delete:
1068 list = g_list_copy(cw->table->selection);
1071 collection_remove_by_info_list(cw->cd, list);
1072 collection_table_refresh(cw->table);
1077 collection_remove_by_info(cw->cd, collection_table_get_focus_info(cw->table));
1081 stop_signal = FALSE;
1085 if (!stop_signal && is_help_key(event))
1087 help_window_show("GuideCollections.html");
1095 *-------------------------------------------------------------------
1097 *-------------------------------------------------------------------
1099 static void collection_window_get_geometry(CollectWindow *cw)
1107 window = gtk_widget_get_window(cw->window);
1108 gdk_window_get_position(window, &cd->window.x, &cd->window.y);
1109 cd->window.width = gdk_window_get_width(window);
1110 cd->window.height = gdk_window_get_height(window);
1111 cd->window_read = TRUE;
1114 static void collection_window_refresh(CollectWindow *cw)
1118 collection_table_refresh(cw->table);
1121 static void collection_window_update_title(CollectWindow *cw)
1123 gboolean free_name = FALSE;
1129 if (file_extension_match(cw->cd->name, GQ_COLLECTION_EXT))
1131 name = remove_extension_from_path(cw->cd->name);
1136 name = cw->cd->name;
1139 buf = g_strdup_printf(_("%s - Collection - %s"), name, GQ_APPNAME);
1140 if (free_name) g_free(name);
1141 gtk_window_set_title(GTK_WINDOW(cw->window), buf);
1145 static void collection_window_update_info(CollectionData *, CollectInfo *ci, gpointer data)
1147 auto cw = static_cast<CollectWindow *>(data);
1149 collection_table_file_update(cw->table, ci);
1152 static void collection_window_add(CollectWindow *cw, CollectInfo *ci)
1156 if (!ci->pixbuf) collection_load_thumb_idle(cw->cd);
1157 collection_table_file_add(cw->table, ci);
1160 static void collection_window_insert(CollectWindow *cw, CollectInfo *ci)
1164 if (!ci->pixbuf) collection_load_thumb_idle(cw->cd);
1165 collection_table_file_insert(cw->table, ci);
1169 static void collection_window_remove(CollectWindow *cw, CollectInfo *ci)
1173 collection_table_file_remove(cw->table, ci);
1176 static void collection_window_update(CollectWindow *cw, CollectInfo *ci)
1180 collection_table_file_update(cw->table, ci);
1181 collection_table_file_update(cw->table, nullptr);
1184 static void collection_window_close_final(CollectWindow *cw)
1186 if (cw->close_dialog) return;
1188 collection_window_list = g_list_remove(collection_window_list, cw);
1189 collection_window_get_geometry(cw);
1191 gq_gtk_widget_destroy(cw->window);
1193 collection_set_update_info_func(cw->cd, nullptr, nullptr);
1194 collection_unref(cw->cd);
1199 static void collection_close_save_cb(GenericDialog *gd, gpointer data)
1201 auto cw = static_cast<CollectWindow *>(data);
1203 cw->close_dialog = nullptr;
1204 generic_dialog_close(gd);
1208 collection_dialog_save_close(cw->cd);
1212 if (!collection_save(cw->cd, cw->cd->path))
1215 buf = g_strdup_printf(_("Failed to save the collection:\n%s"), cw->cd->path);
1216 warning_dialog(_("Save Failed"), buf, GQ_ICON_DIALOG_ERROR, cw->window);
1221 collection_window_close_final(cw);
1224 static void collection_close_close_cb(GenericDialog *gd, gpointer data)
1226 auto cw = static_cast<CollectWindow *>(data);
1228 cw->close_dialog = nullptr;
1229 generic_dialog_close(gd);
1231 collection_window_close_final(cw);
1234 static void collection_close_cancel_cb(GenericDialog *gd, gpointer data)
1236 auto cw = static_cast<CollectWindow *>(data);
1238 cw->close_dialog = nullptr;
1239 generic_dialog_close(gd);
1242 static void collection_close_dlg_show(CollectWindow *cw)
1246 if (cw->close_dialog)
1248 gtk_window_present(GTK_WINDOW(cw->close_dialog));
1252 gd = generic_dialog_new(_("Close collection"),
1253 "close_collection", cw->window, FALSE,
1254 collection_close_cancel_cb, cw);
1255 generic_dialog_add_message(gd, GQ_ICON_DIALOG_QUESTION,
1256 _("Close collection"),
1257 _("Collection has been modified.\nSave first?"), TRUE);
1259 generic_dialog_add_button(gd, GQ_ICON_SAVE, _("Save"), collection_close_save_cb, TRUE);
1260 generic_dialog_add_button(gd, GQ_ICON_DELETE, _("_Discard"), collection_close_close_cb, FALSE);
1262 cw->close_dialog = gd->dialog;
1264 gtk_widget_show(gd->dialog);
1267 static void collection_window_close(CollectWindow *cw)
1269 if (!cw->cd->changed && !cw->close_dialog)
1271 collection_window_close_final(cw);
1275 collection_close_dlg_show(cw);
1278 void collection_window_close_by_collection(CollectionData *cd)
1282 cw = collection_window_find(cd);
1283 if (cw) collection_window_close_final(cw);
1287 * @brief Check if any Collection windows have unsaved data
1288 * @returns TRUE if unsaved data exists
1290 * Also saves window geometry for Collection windows that have
1293 gboolean collection_window_modified_exists()
1300 work = collection_window_list;
1303 auto cw = static_cast<CollectWindow *>(work->data);
1304 if (cw->cd->changed)
1310 if (!collection_save(cw->table->cd, cw->table->cd->path))
1312 log_printf("failed saving to collection path: %s\n", cw->table->cd->path);
1321 static gboolean collection_window_delete(GtkWidget *, GdkEvent *, gpointer data)
1323 auto cw = static_cast<CollectWindow *>(data);
1324 collection_window_close(cw);
1329 CollectWindow *collection_window_new(const gchar *path)
1334 GtkWidget *status_label;
1335 GtkWidget *extra_label;
1336 GdkGeometry geometry;
1338 /* If the collection is already opened in another window, return that one */
1339 cw = collection_window_find_by_path(path);
1345 cw = g_new0(CollectWindow, 1);
1347 collection_window_list = g_list_append(collection_window_list, cw);
1349 cw->cd = collection_new(path);
1351 cw->window = window_new("collection", PIXBUF_INLINE_ICON_BOOK, nullptr, nullptr);
1352 DEBUG_NAME(cw->window);
1354 geometry.min_width = DEFAULT_MINIMAL_WINDOW_SIZE;
1355 geometry.min_height = DEFAULT_MINIMAL_WINDOW_SIZE;
1356 geometry.base_width = COLLECT_DEF_WIDTH;
1357 geometry.base_height = COLLECT_DEF_HEIGHT;
1358 gtk_window_set_geometry_hints(GTK_WINDOW(cw->window), nullptr, &geometry,
1359 static_cast<GdkWindowHints>(GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE));
1361 if (options->collections_on_top)
1363 gq_gtk_window_set_keep_above(GTK_WINDOW(cw->window), TRUE);
1366 if (options->save_window_positions && path && collection_load_only_geometry(cw->cd, path))
1368 gtk_window_set_default_size(GTK_WINDOW(cw->window), cw->cd->window.width, cw->cd->window.height);
1369 gq_gtk_window_move(GTK_WINDOW(cw->window), cw->cd->window.x, cw->cd->window.y);
1373 gtk_window_set_default_size(GTK_WINDOW(cw->window), COLLECT_DEF_WIDTH, COLLECT_DEF_HEIGHT);
1376 gtk_window_set_resizable(GTK_WINDOW(cw->window), TRUE);
1377 collection_window_update_title(cw);
1378 gtk_container_set_border_width(GTK_CONTAINER(cw->window), 0);
1380 g_signal_connect(G_OBJECT(cw->window), "delete_event",
1381 G_CALLBACK(collection_window_delete), cw);
1383 g_signal_connect(G_OBJECT(cw->window), "key_press_event",
1384 G_CALLBACK(collection_window_keypress), cw);
1386 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
1387 gq_gtk_container_add(GTK_WIDGET(cw->window), vbox);
1388 gtk_widget_show(vbox);
1390 cw->table = collection_table_new(cw->cd);
1391 gq_gtk_box_pack_start(GTK_BOX(vbox), cw->table->scrolled, TRUE, TRUE, 0);
1392 gtk_widget_show(cw->table->scrolled);
1394 cw->status_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
1395 gq_gtk_box_pack_start(GTK_BOX(vbox), cw->status_box, FALSE, FALSE, 0);
1396 gtk_widget_show(cw->status_box);
1398 frame = gtk_frame_new(nullptr);
1400 gq_gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
1401 gq_gtk_box_pack_start(GTK_BOX(cw->status_box), frame, TRUE, TRUE, 0);
1402 gtk_widget_show(frame);
1404 status_label = gtk_label_new("");
1405 gq_gtk_container_add(GTK_WIDGET(frame), status_label);
1406 gtk_widget_show(status_label);
1408 extra_label = gtk_progress_bar_new();
1409 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(extra_label), 0.0);
1410 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(extra_label), "");
1411 gtk_progress_bar_set_show_text(GTK_PROGRESS_BAR(extra_label), TRUE);
1413 gq_gtk_box_pack_start(GTK_BOX(cw->status_box), extra_label, TRUE, TRUE, 0);
1414 gtk_widget_show(extra_label);
1416 collection_table_set_labels(cw->table, status_label, extra_label);
1418 gtk_widget_show(cw->window);
1419 gtk_widget_grab_focus(cw->table->listview);
1421 collection_set_update_info_func(cw->cd, collection_window_update_info, cw);
1423 if (path && *path == G_DIR_SEPARATOR) collection_load_begin(cw->cd, nullptr, COLLECTION_LOAD_NONE);
1427 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */