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, g_strdup(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_w = COLLECT_DEF_WIDTH;
459 cd->window_h = 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);
511 void collection_ref(CollectionData *cd)
515 DEBUG_1("collection \"%s\" ref count = %d", cd->name, cd->ref);
518 void collection_unref(CollectionData *cd)
522 DEBUG_1("collection \"%s\" ref count = %d", cd->name, cd->ref);
530 void collection_path_changed(CollectionData *cd)
532 collection_window_update_title(collection_window_find(cd));
535 gint collection_to_number(CollectionData *cd)
537 return g_list_index(collection_list, cd);
540 CollectionData *collection_from_number(gint n)
542 return static_cast<CollectionData *>(g_list_nth_data(collection_list, n));
545 CollectionData *collection_from_dnd_data(const gchar *data, GList **list, GList **info_list)
548 gint collection_number;
551 if (list) *list = nullptr;
552 if (info_list) *info_list = nullptr;
554 if (strncmp(data, "COLLECTION:", 11) != 0) return nullptr;
558 collection_number = atoi(ptr);
559 cd = collection_from_number(collection_number);
560 if (!cd) return nullptr;
562 if (!list && !info_list) return cd;
564 while (*ptr != '\0' && *ptr != '\n' ) ptr++;
565 if (*ptr == '\0') return cd;
573 item_number = static_cast<guint>(atoi(ptr));
574 while (*ptr != '\n' && *ptr != '\0') ptr++;
578 while (*ptr == '\n') ptr++;
580 info = static_cast<CollectInfo *>(g_list_nth_data(cd->list, item_number));
583 if (list) *list = g_list_append(*list, file_data_ref(info->fd));
584 if (info_list) *info_list = g_list_append(*info_list, info);
590 gchar *collection_info_list_to_dnd_data(CollectionData *cd, GList *list, gint *length)
593 GList *temp = nullptr;
597 gint collection_number;
600 if (!list) return nullptr;
602 collection_number = collection_to_number(cd);
603 if (collection_number < 0) return nullptr;
605 text = g_strdup_printf("COLLECTION:%d\n", collection_number);
606 *length += strlen(text);
607 temp = g_list_prepend(temp, text);
612 gint item_number = g_list_index(cd->list, work->data);
616 if (item_number < 0) continue;
618 text = g_strdup_printf("%d\n", item_number);
619 temp = g_list_prepend(temp, text);
620 *length += strlen(text);
623 *length += 1; /* ending nul char */
625 uri_text = static_cast<gchar *>(g_malloc(*length));
628 work = g_list_last(temp);
632 auto text = static_cast<gchar *>(work->data);
637 memcpy(ptr, text, len);
643 g_list_free_full(temp, g_free);
648 gint collection_info_valid(CollectionData *cd, CollectInfo *info)
650 if (collection_to_number(cd) < 0) return FALSE;
652 return (g_list_index(cd->list, info) != 0);
655 CollectInfo *collection_next_by_info(CollectionData *cd, CollectInfo *info)
659 work = g_list_find(cd->list, info);
661 if (!work) return nullptr;
663 if (work) return static_cast<CollectInfo *>(work->data);
667 CollectInfo *collection_prev_by_info(CollectionData *cd, CollectInfo *info)
671 work = g_list_find(cd->list, info);
673 if (!work) return nullptr;
675 if (work) return static_cast<CollectInfo *>(work->data);
679 CollectInfo *collection_get_first(CollectionData *cd)
681 if (cd->list) return static_cast<CollectInfo *>(cd->list->data);
686 CollectInfo *collection_get_last(CollectionData *cd)
690 list = g_list_last(cd->list);
692 if (list) return static_cast<CollectInfo *>(list->data);
697 void collection_set_sort_method(CollectionData *cd, SortType method)
701 if (cd->sort_method == method) return;
703 cd->sort_method = method;
704 cd->list = collection_list_sort(cd->list, cd->sort_method);
705 if (cd->list) cd->changed = TRUE;
707 collection_window_refresh(collection_window_find(cd));
710 void collection_randomize(CollectionData *cd)
714 cd->list = collection_list_randomize(cd->list);
715 cd->sort_method = SORT_NONE;
716 if (cd->list) cd->changed = TRUE;
718 collection_window_refresh(collection_window_find(cd));
721 void collection_set_update_info_func(CollectionData *cd,
722 void (*func)(CollectionData *, CollectInfo *, gpointer), gpointer data)
724 cd->info_updated_func = func;
725 cd->info_updated_data = data;
728 static CollectInfo *collection_info_new_if_not_exists(CollectionData *cd, struct stat *st, FileData *fd)
732 if (g_hash_table_lookup(cd->existence, fd->path)) return nullptr;
734 ci = collection_info_new(fd, st, nullptr);
735 if (ci) g_hash_table_insert(cd->existence, fd->path, g_strdup(""));
739 gboolean collection_add_check(CollectionData *cd, FileData *fd, gboolean sorted, gboolean must_exist)
744 if (!fd) return FALSE;
746 g_assert(fd->magick == FD_MAGICK);
750 valid = (stat_utf8(fd->path, &st) && !S_ISDIR(st.st_mode));
763 ci = collection_info_new_if_not_exists(cd, &st, fd);
764 if (!ci) return FALSE;
765 DEBUG_3("add to collection: %s", fd->path);
767 cd->list = collection_list_add(cd->list, ci, sorted ? cd->sort_method : SORT_NONE);
770 if (!sorted || cd->sort_method == SORT_NONE)
772 collection_window_add(collection_window_find(cd), ci);
776 collection_window_insert(collection_window_find(cd), ci);
783 gboolean collection_add(CollectionData *cd, FileData *fd, gboolean sorted)
785 return collection_add_check(cd, fd, sorted, TRUE);
788 gboolean collection_insert(CollectionData *cd, FileData *fd, CollectInfo *insert_ci, gboolean sorted)
792 if (!insert_ci) return collection_add(cd, fd, sorted);
794 if (stat_utf8(fd->path, &st) >= 0 && !S_ISDIR(st.st_mode))
798 ci = collection_info_new_if_not_exists(cd, &st, fd);
799 if (!ci) return FALSE;
801 DEBUG_3("insert in collection: %s", fd->path);
803 cd->list = collection_list_insert(cd->list, ci, insert_ci, sorted ? cd->sort_method : SORT_NONE);
806 collection_window_insert(collection_window_find(cd), ci);
814 gboolean collection_remove(CollectionData *cd, FileData *fd)
818 ci = collection_list_find_fd(cd->list, fd);
820 if (!ci) return FALSE;
822 g_hash_table_remove(cd->existence, fd->path);
824 cd->list = g_list_remove(cd->list, ci);
827 collection_window_remove(collection_window_find(cd), ci);
828 collection_info_free(ci);
833 static void collection_remove_by_info(CollectionData *cd, CollectInfo *info)
835 if (!info || !g_list_find(cd->list, info)) return;
837 cd->list = g_list_remove(cd->list, info);
838 cd->changed = (cd->list != nullptr);
840 collection_window_remove(collection_window_find(cd), info);
841 collection_info_free(info);
844 void collection_remove_by_info_list(CollectionData *cd, GList *list)
852 /* more efficient (in collect-table) to remove a single item this way */
853 collection_remove_by_info(cd, static_cast<CollectInfo *>(list->data));
860 cd->list = collection_list_remove(cd->list, static_cast<CollectInfo *>(work->data));
863 cd->changed = (cd->list != nullptr);
865 collection_window_refresh(collection_window_find(cd));
868 gboolean collection_rename(CollectionData *cd, FileData *fd)
871 ci = collection_list_find_fd(cd->list, fd);
873 if (!ci) return FALSE;
877 collection_window_update(collection_window_find(cd), ci);
882 void collection_update_geometry(CollectionData *cd)
884 collection_window_get_geometry(collection_window_find(cd));
888 *-------------------------------------------------------------------
889 * simple maintenance for renaming, deleting
890 *-------------------------------------------------------------------
893 static void collection_notify_cb(FileData *fd, NotifyType type, gpointer data)
895 auto cd = static_cast<CollectionData *>(data);
897 if (!(type & NOTIFY_CHANGE) || !fd->change) return;
899 DEBUG_1("Notify collection: %s %04x", fd->path, type);
901 switch (fd->change->type)
903 case FILEDATA_CHANGE_MOVE:
904 case FILEDATA_CHANGE_RENAME:
905 collection_rename(cd, fd);
907 case FILEDATA_CHANGE_COPY:
909 case FILEDATA_CHANGE_DELETE:
910 while (collection_remove(cd, fd));
912 case FILEDATA_CHANGE_UNSPECIFIED:
913 case FILEDATA_CHANGE_WRITE_METADATA:
921 *-------------------------------------------------------------------
923 *-------------------------------------------------------------------
926 static gboolean collection_window_keypress(GtkWidget *, GdkEventKey *event, gpointer data)
928 auto cw = static_cast<CollectWindow *>(data);
929 gboolean stop_signal = FALSE;
932 if (event->state & GDK_CONTROL_MASK)
935 switch (event->keyval)
949 if (event->state & GDK_SHIFT_MASK)
951 collection_table_unselect_all(cw->table);
955 collection_table_select_all(cw->table);
959 list = layout_list(nullptr);
962 collection_table_add_filelist(cw->table, list);
967 file_util_copy(nullptr, collection_table_selection_get_list(cw->table), nullptr, cw->window);
970 file_util_move(nullptr, collection_table_selection_get_list(cw->table), nullptr, cw->window);
973 file_util_rename(nullptr, collection_table_selection_get_list(cw->table), cw->window);
976 options->file_ops.safe_delete_enable = TRUE;
977 file_util_delete(nullptr, collection_table_selection_get_list(cw->table), cw->window);
980 collection_dialog_save_as(nullptr, cw->cd);
983 collection_window_close(cw);
993 switch (event->keyval)
995 case GDK_KEY_Return: case GDK_KEY_KP_Enter:
996 layout_image_set_collection(nullptr, cw->cd,
997 collection_table_get_focus_info(cw->table));
1000 view_window_new_from_collection(cw->cd,
1001 collection_table_get_focus_info(cw->table));
1006 collection_dialog_save_as(nullptr, cw->cd);
1008 else if (!collection_save(cw->cd, cw->cd->path))
1010 log_printf("failed saving to collection path: %s\n", cw->cd->path);
1014 collection_dialog_append(nullptr, cw->cd);
1017 collection_set_sort_method(cw->cd, SORT_NAME);
1020 collection_set_sort_method(cw->cd, SORT_TIME);
1023 collection_set_sort_method(cw->cd, SORT_SIZE);
1026 if (event->state & GDK_SHIFT_MASK)
1030 info = collection_table_get_focus_info(cw->table);
1032 print_window_new(info->fd, collection_table_selection_get_list(cw->table),
1033 collection_list_to_filelist(cw->cd->list), cw->window);
1037 collection_set_sort_method(cw->cd, SORT_PATH);
1041 if (event->state & GDK_MOD1_MASK)
1043 options->collections.rectangular_selection = !(options->collections.rectangular_selection);
1046 case GDK_KEY_Delete: case GDK_KEY_KP_Delete:
1047 list = g_list_copy(cw->table->selection);
1050 collection_remove_by_info_list(cw->cd, list);
1051 collection_table_refresh(cw->table);
1056 collection_remove_by_info(cw->cd, collection_table_get_focus_info(cw->table));
1060 stop_signal = FALSE;
1064 if (!stop_signal && is_help_key(event))
1066 help_window_show("GuideCollections.html");
1074 *-------------------------------------------------------------------
1076 *-------------------------------------------------------------------
1078 static void collection_window_get_geometry(CollectWindow *cw)
1086 window = gtk_widget_get_window(cw->window);
1087 gdk_window_get_position(window, &cd->window_x, &cd->window_y);
1088 cd->window_w = gdk_window_get_width(window);
1089 cd->window_h = gdk_window_get_height(window);
1090 cd->window_read = TRUE;
1093 static void collection_window_refresh(CollectWindow *cw)
1097 collection_table_refresh(cw->table);
1100 static void collection_window_update_title(CollectWindow *cw)
1102 gboolean free_name = FALSE;
1108 if (file_extension_match(cw->cd->name, GQ_COLLECTION_EXT))
1110 name = remove_extension_from_path(cw->cd->name);
1115 name = cw->cd->name;
1118 buf = g_strdup_printf(_("%s - Collection - %s"), name, GQ_APPNAME);
1119 if (free_name) g_free(name);
1120 gtk_window_set_title(GTK_WINDOW(cw->window), buf);
1124 static void collection_window_update_info(CollectionData *, CollectInfo *ci, gpointer data)
1126 auto cw = static_cast<CollectWindow *>(data);
1128 collection_table_file_update(cw->table, ci);
1131 static void collection_window_add(CollectWindow *cw, CollectInfo *ci)
1135 if (!ci->pixbuf) collection_load_thumb_idle(cw->cd);
1136 collection_table_file_add(cw->table, ci);
1139 static void collection_window_insert(CollectWindow *cw, CollectInfo *ci)
1143 if (!ci->pixbuf) collection_load_thumb_idle(cw->cd);
1144 collection_table_file_insert(cw->table, ci);
1148 static void collection_window_remove(CollectWindow *cw, CollectInfo *ci)
1152 collection_table_file_remove(cw->table, ci);
1155 static void collection_window_update(CollectWindow *cw, CollectInfo *ci)
1159 collection_table_file_update(cw->table, ci);
1160 collection_table_file_update(cw->table, nullptr);
1163 static void collection_window_close_final(CollectWindow *cw)
1165 if (cw->close_dialog) return;
1167 collection_window_list = g_list_remove(collection_window_list, cw);
1168 collection_window_get_geometry(cw);
1170 gtk_widget_destroy(cw->window);
1172 collection_set_update_info_func(cw->cd, nullptr, nullptr);
1173 collection_unref(cw->cd);
1178 static void collection_close_save_cb(GenericDialog *gd, gpointer data)
1180 auto cw = static_cast<CollectWindow *>(data);
1182 cw->close_dialog = nullptr;
1183 generic_dialog_close(gd);
1187 collection_dialog_save_close(nullptr, cw->cd);
1190 else if (!collection_save(cw->cd, cw->cd->path))
1193 buf = g_strdup_printf(_("Failed to save the collection:\n%s"), cw->cd->path);
1194 warning_dialog(_("Save Failed"), buf, GQ_ICON_DIALOG_ERROR, cw->window);
1199 collection_window_close_final(cw);
1202 static void collection_close_close_cb(GenericDialog *gd, gpointer data)
1204 auto cw = static_cast<CollectWindow *>(data);
1206 cw->close_dialog = nullptr;
1207 generic_dialog_close(gd);
1209 collection_window_close_final(cw);
1212 static void collection_close_cancel_cb(GenericDialog *gd, gpointer data)
1214 auto cw = static_cast<CollectWindow *>(data);
1216 cw->close_dialog = nullptr;
1217 generic_dialog_close(gd);
1220 static void collection_close_dlg_show(CollectWindow *cw)
1224 if (cw->close_dialog)
1226 gtk_window_present(GTK_WINDOW(cw->close_dialog));
1230 gd = generic_dialog_new(_("Close collection"),
1231 "close_collection", cw->window, FALSE,
1232 collection_close_cancel_cb, cw);
1233 generic_dialog_add_message(gd, GQ_ICON_DIALOG_QUESTION,
1234 _("Close collection"),
1235 _("Collection has been modified.\nSave first?"), TRUE);
1237 generic_dialog_add_button(gd, GQ_ICON_SAVE, _("Save"), collection_close_save_cb, TRUE);
1238 generic_dialog_add_button(gd, GQ_ICON_DELETE, _("_Discard"), collection_close_close_cb, FALSE);
1240 cw->close_dialog = gd->dialog;
1242 gtk_widget_show(gd->dialog);
1245 static void collection_window_close(CollectWindow *cw)
1247 if (!cw->cd->changed && !cw->close_dialog)
1249 collection_window_close_final(cw);
1253 collection_close_dlg_show(cw);
1256 void collection_window_close_by_collection(CollectionData *cd)
1260 cw = collection_window_find(cd);
1261 if (cw) collection_window_close_final(cw);
1265 * @brief Check if any Collection windows have unsaved data
1266 * @returns TRUE if unsaved data exists
1268 * Also saves window geometry for Collection windows that have
1271 gboolean collection_window_modified_exists()
1278 work = collection_window_list;
1281 auto cw = static_cast<CollectWindow *>(work->data);
1282 if (cw->cd->changed)
1288 if (!collection_save(cw->table->cd, cw->table->cd->path))
1290 log_printf("failed saving to collection path: %s\n", cw->table->cd->path);
1299 static gboolean collection_window_delete(GtkWidget *, GdkEvent *, gpointer data)
1301 auto cw = static_cast<CollectWindow *>(data);
1302 collection_window_close(cw);
1307 CollectWindow *collection_window_new(const gchar *path)
1312 GtkWidget *status_label;
1313 GtkWidget *extra_label;
1314 GdkGeometry geometry;
1316 /* If the collection is already opened in another window, return that one */
1317 cw = collection_window_find_by_path(path);
1323 cw = g_new0(CollectWindow, 1);
1325 collection_window_list = g_list_append(collection_window_list, cw);
1327 cw->cd = collection_new(path);
1329 cw->window = window_new(GTK_WINDOW_TOPLEVEL, "collection", PIXBUF_INLINE_ICON_BOOK, nullptr, nullptr);
1330 DEBUG_NAME(cw->window);
1332 geometry.min_width = DEFAULT_MINIMAL_WINDOW_SIZE;
1333 geometry.min_height = DEFAULT_MINIMAL_WINDOW_SIZE;
1334 geometry.base_width = COLLECT_DEF_WIDTH;
1335 geometry.base_height = COLLECT_DEF_HEIGHT;
1336 gtk_window_set_geometry_hints(GTK_WINDOW(cw->window), nullptr, &geometry,
1337 static_cast<GdkWindowHints>(GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE));
1339 if (options->collections_on_top)
1341 gtk_window_set_keep_above(GTK_WINDOW(cw->window), TRUE);
1344 if (options->save_window_positions && path && collection_load_only_geometry(cw->cd, path))
1346 gtk_window_set_default_size(GTK_WINDOW(cw->window), cw->cd->window_w, cw->cd->window_h);
1347 gtk_window_move(GTK_WINDOW(cw->window), cw->cd->window_x, cw->cd->window_y);
1351 gtk_window_set_default_size(GTK_WINDOW(cw->window), COLLECT_DEF_WIDTH, COLLECT_DEF_HEIGHT);
1354 gtk_window_set_resizable(GTK_WINDOW(cw->window), TRUE);
1355 collection_window_update_title(cw);
1356 gtk_container_set_border_width(GTK_CONTAINER(cw->window), 0);
1358 g_signal_connect(G_OBJECT(cw->window), "delete_event",
1359 G_CALLBACK(collection_window_delete), cw);
1361 g_signal_connect(G_OBJECT(cw->window), "key_press_event",
1362 G_CALLBACK(collection_window_keypress), cw);
1364 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
1365 gtk_container_add(GTK_CONTAINER(cw->window), vbox);
1366 gtk_widget_show(vbox);
1368 cw->table = collection_table_new(cw->cd);
1369 gtk_box_pack_start(GTK_BOX(vbox), cw->table->scrolled, TRUE, TRUE, 0);
1370 gtk_widget_show(cw->table->scrolled);
1372 cw->status_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
1373 gtk_box_pack_start(GTK_BOX(vbox), cw->status_box, FALSE, FALSE, 0);
1374 gtk_widget_show(cw->status_box);
1376 frame = gtk_frame_new(nullptr);
1378 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
1379 gtk_box_pack_start(GTK_BOX(cw->status_box), frame, TRUE, TRUE, 0);
1380 gtk_widget_show(frame);
1382 status_label = gtk_label_new("");
1383 gtk_container_add(GTK_CONTAINER(frame), status_label);
1384 gtk_widget_show(status_label);
1386 extra_label = gtk_progress_bar_new();
1387 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(extra_label), 0.0);
1388 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(extra_label), "");
1389 gtk_progress_bar_set_show_text(GTK_PROGRESS_BAR(extra_label), TRUE);
1391 gtk_box_pack_start(GTK_BOX(cw->status_box), extra_label, TRUE, TRUE, 0);
1392 gtk_widget_show(extra_label);
1394 collection_table_set_labels(cw->table, status_label, extra_label);
1396 gtk_widget_show(cw->window);
1397 gtk_widget_grab_focus(cw->table->listview);
1399 collection_set_update_info_func(cw->cd, collection_window_update_info, cw);
1401 if (path && *path == G_DIR_SEPARATOR) collection_load_begin(cw->cd, nullptr, COLLECTION_LOAD_NONE);
1405 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */