7 * This software is released under the GNU General Public License (GNU GPL).
8 * Please read the included file COPYING for more information.
9 * This software comes with no warranty of any kind, use at your own risk!
16 #include "collect-dlg.h"
17 #include "collect-io.h"
18 #include "collect-table.h"
24 #include "layout_image.h"
25 #include "pixbuf_util.h"
27 #include "ui_fileops.h"
28 #include "ui_tree_edit.h"
30 #include <gdk/gdkkeysyms.h> /* for keyboard values */
33 #define COLLECT_DEF_WIDTH 440
34 #define COLLECT_DEF_HEIGHT 450
36 static GList *collection_list = NULL;
37 static GList *collection_window_list = NULL;
39 static void collection_window_get_geometry(CollectWindow *cw);
40 static void collection_window_refresh(CollectWindow *cw);
41 static void collection_window_update_title(CollectWindow *cw);
42 static void collection_window_add(CollectWindow *cw, CollectInfo *ci);
43 static void collection_window_insert(CollectWindow *cw, CollectInfo *ci);
44 static void collection_window_remove(CollectWindow *cw, CollectInfo *ci);
45 static void collection_window_update(CollectWindow *cw, CollectInfo *ci);
47 static void collection_window_close(CollectWindow *cw);
50 *-------------------------------------------------------------------
52 *-------------------------------------------------------------------
55 CollectInfo *collection_info_new(const gchar *path, struct stat *st, GdkPixbuf *pixbuf)
59 if (!path) return NULL;
61 ci = g_new0(CollectInfo, 1);
62 ci->path = g_strdup(path);
63 ci->size = st->st_size;
64 ci->date = st->st_mtime;
67 if (ci->pixbuf) g_object_ref(ci->pixbuf);
72 void collection_info_free_thumb(CollectInfo *ci)
74 if (ci->pixbuf) g_object_unref(ci->pixbuf);
78 void collection_info_free(CollectInfo *ci)
83 collection_info_free_thumb(ci);
87 void collection_info_set_thumb(CollectInfo *ci, GdkPixbuf *pixbuf)
89 if (pixbuf) g_object_ref(pixbuf);
90 collection_info_free_thumb(ci);
94 gint collection_info_load_thumb(CollectInfo *ci)
96 if (!ci) return FALSE;
98 collection_info_free_thumb(ci);
100 printf("collection_info_load_thumb not implemented!\n(because an instant thumb loader not implemented)");
103 if (create_thumbnail(ci->path, &ci->pixmap, &ci->mask) < 0) return FALSE;
105 if (ci->pixmap) gdk_pixmap_ref(ci->pixmap);
106 if (ci->mask) gdk_bitmap_ref(ci->mask);
112 void collection_list_free(GList *list)
118 collection_info_free((CollectInfo *)work->data);
124 /* an ugly static var, well what ya gonna do ? */
125 static SortType collection_list_sort_method = SORT_NAME;
127 static gint collection_list_sort_cb(gconstpointer a, gconstpointer b)
129 const CollectInfo *cia = a;
130 const CollectInfo *cib = b;
132 switch(collection_list_sort_method)
138 if (cia->size < cib->size) return -1;
139 if (cia->size > cib->size) return 1;
143 if (cia->date < cib->date) return -1;
144 if (cia->date > cib->date) return 1;
148 return CASE_SORT(cia->path, cib->path);
150 #ifdef HAVE_STRVERSCMP
152 return strverscmp(filename_from_path(cia->path), filename_from_path(cib->path));
157 return CASE_SORT(filename_from_path(cia->path), filename_from_path(cib->path));
164 GList *collection_list_sort(GList *list, SortType method)
166 if (method == SORT_NONE) return list;
168 collection_list_sort_method = method;
170 return g_list_sort(list, collection_list_sort_cb);
173 GList *collection_list_add(GList *list, CollectInfo *ci, SortType method)
175 if (method != SORT_NONE)
177 collection_list_sort_method = method;
178 list = g_list_insert_sorted(list, ci, collection_list_sort_cb);
182 list = g_list_append(list, ci);
188 GList *collection_list_insert(GList *list, CollectInfo *ci, CollectInfo *insert_ci, SortType method)
190 if (method != SORT_NONE)
192 collection_list_sort_method = method;
193 list = g_list_insert_sorted(list, ci, collection_list_sort_cb);
199 point = g_list_find(list, insert_ci);
200 list = uig_list_insert_link(list, point, ci);
206 GList *collection_list_remove(GList *list, CollectInfo *ci)
208 list = g_list_remove(list, ci);
209 collection_info_free(ci);
213 CollectInfo *collection_list_find(GList *list, const gchar *path)
219 CollectInfo *ci = work->data;
220 if (strcmp(ci->path, path) == 0) return ci;
228 static GList *collection_list_find_link(GList *list, gchar *path)
234 CollectInfo *ci = work->data;
235 if (strcmp(ci->path, path) == 0) return work;
242 static gint collection_list_find_index(GList *list, gchar *path)
249 CollectInfo *ci = work->data;
250 if (strcmp(ci->path, path) == 0) return c;
259 GList *collection_list_to_path_list(GList *list)
261 GList *pathlist = NULL;
266 CollectInfo *info = work->data;
267 pathlist = g_list_prepend(pathlist, g_strdup(info->path));
271 pathlist = g_list_reverse(pathlist);
275 CollectWindow *collection_window_find(CollectionData *cd)
279 work = collection_window_list;
282 CollectWindow *cw = work->data;
283 if (cw->cd == cd) return cw;
290 CollectWindow *collection_window_find_by_path(const gchar *path)
294 if (!path) return NULL;
296 work = collection_window_list;
299 CollectWindow *cw = work->data;
300 if (cw->cd->path && strcmp(cw->cd->path, path) == 0) return cw;
308 *-------------------------------------------------------------------
309 * please use these to actually add/remove stuff
310 *-------------------------------------------------------------------
313 CollectionData *collection_new(const gchar *path)
316 static gint untitled_counter = 0;
318 cd = g_new0(CollectionData, 1);
320 collection_list = g_list_append(collection_list, cd);
322 cd->ref = 1; /* starts with a ref of 1 */
325 cd->sort_method = SORT_NONE;
326 cd->thumb_loader = NULL;
327 cd->info_updated_func = NULL;
329 cd->window_read = FALSE;
332 cd->window_w = COLLECT_DEF_WIDTH;
333 cd->window_h = COLLECT_DEF_HEIGHT;
339 cd->path = g_strdup(path);
340 cd->name = g_strdup(filename_from_path(cd->path));
347 if (untitled_counter == 0)
349 cd->name = g_strdup(_("Untitled"));
353 cd->name = g_strdup_printf(_("Untitled (%d)"), untitled_counter + 1);
362 void collection_free(CollectionData *cd)
366 if (debug) printf("collection \"%s\" freed\n", cd->name);
368 collection_load_stop(cd);
369 collection_list_free(cd->list);
371 collection_list = g_list_remove(collection_list, cd);
379 void collection_ref(CollectionData *cd)
383 if (debug) printf("collection \"%s\" ref count = %d\n", cd->name, cd->ref);
386 void collection_unref(CollectionData *cd)
390 if (debug) printf("collection \"%s\" ref count = %d\n", cd->name, cd->ref);
398 void collection_path_changed(CollectionData *cd)
400 collection_window_update_title(collection_window_find(cd));
403 gint collection_to_number(CollectionData *cd)
405 return g_list_index(collection_list, cd);
408 CollectionData *collection_from_number(gint n)
410 return g_list_nth_data(collection_list, n);
413 CollectionData *collection_from_dnd_data(const gchar *data, GList **list, GList **info_list)
418 if (strncmp(data, "COLLECTION:", 11) != 0) return NULL;
420 n = (gint)strtol(data + 11, NULL, 10);
421 cd = collection_from_number(n);
423 if (!cd || (!list && !info_list))
434 while(data[b] != '\0' && data[b] != '\n' ) b++;
438 while (data[b] != '\0')
442 while (data[e] != '\n' && data[e] != '\0') e++;
443 n = (gint)strtol(data + b, NULL, 10);
445 info = g_list_nth_data(cd->list, n);
446 if (info && list) work = g_list_append(work, g_strdup(info->path));
447 if (info && info_list) infol = g_list_append(infol, info);
449 while (data[e] == '\n') e++;
452 if (list) *list = work;
453 if (info_list) *info_list = infol;
459 gchar *collection_info_list_to_dnd_data(CollectionData *cd, GList *list, gint *length)
461 gchar *uri_text = NULL;
468 n = collection_to_number(cd);
477 temp = g_list_prepend(temp, g_strdup_printf("COLLECTION:%d\n", n));
481 n = g_list_index(cd->list, work->data);
484 temp = g_list_prepend(temp, g_strdup_printf("%d\n", n));
493 total += strlen((gchar *)work->data);
498 uri_text = g_malloc(total);
501 work = g_list_last(temp);
504 gchar *text = work->data;
514 path_list_free(temp);
521 gint collection_info_valid(CollectionData *cd, CollectInfo *info)
523 if (collection_to_number(cd) < 0) return FALSE;
525 return (g_list_index(cd->list, info) != 0);
528 CollectInfo *collection_next_by_info(CollectionData *cd, CollectInfo *info)
532 work = g_list_find(cd->list, info);
534 if (!work) return NULL;
536 if (work) return work->data;
540 CollectInfo *collection_prev_by_info(CollectionData *cd, CollectInfo *info)
544 work = g_list_find(cd->list, info);
546 if (!work) return NULL;
548 if (work) return work->data;
552 CollectInfo *collection_get_first(CollectionData *cd)
554 if (cd->list) return cd->list->data;
559 CollectInfo *collection_get_last(CollectionData *cd)
563 list = g_list_last(cd->list);
565 if (list) return list->data;
570 void collection_set_sort_method(CollectionData *cd, SortType method)
574 if (cd->sort_method == method) return;
576 cd->sort_method = method;
577 cd->list = collection_list_sort(cd->list, cd->sort_method);
578 if (cd->list) cd->changed = TRUE;
580 collection_window_refresh(collection_window_find(cd));
583 void collection_set_update_info_func(CollectionData *cd,
584 void (*func)(CollectionData *, CollectInfo *, gpointer), gpointer data)
586 cd->info_updated_func = func;
587 cd->info_updated_data = data;
590 gint collection_add_check(CollectionData *cd, const gchar *path, gint sorted, gint must_exist)
597 valid = (stat_utf8(path, &st) && !S_ISDIR(st.st_mode));
609 ci = collection_info_new(path, &st, NULL);
610 cd->list = collection_list_add(cd->list, ci, sorted ? cd->sort_method : SORT_NONE);
613 if (!sorted || cd->sort_method == SORT_NONE)
615 collection_window_add(collection_window_find(cd), ci);
619 collection_window_insert(collection_window_find(cd), ci);
626 gint collection_add(CollectionData *cd, const gchar *path, gint sorted)
628 return collection_add_check(cd, path, sorted, TRUE);
631 gint collection_insert(CollectionData *cd, const gchar *path, CollectInfo *insert_ci, gint sorted)
635 if (!insert_ci) return collection_add(cd, path, sorted);
637 if (stat_utf8(path, &st) >= 0 && !S_ISDIR(st.st_mode))
640 ci = collection_info_new(path, &st, NULL);
641 cd->list = collection_list_insert(cd->list, ci, insert_ci, sorted ? cd->sort_method : SORT_NONE);
644 collection_window_insert(collection_window_find(cd), ci);
652 gint collection_remove(CollectionData *cd, const gchar *path)
656 ci = collection_list_find(cd->list, path);
658 if (!ci) return FALSE;
660 cd->list = g_list_remove(cd->list, ci);
663 collection_window_remove(collection_window_find(cd), ci);
664 collection_info_free(ci);
669 static void collection_remove_by_info(CollectionData *cd, CollectInfo *info)
671 if (!info || !g_list_find(cd->list, info)) return;
673 cd->list = g_list_remove(cd->list, info);
674 cd->changed = (cd->list != NULL);
676 collection_window_remove(collection_window_find(cd), info);
677 collection_info_free(info);
680 void collection_remove_by_info_list(CollectionData *cd, GList *list)
688 /* more efficient (in collect-table) to remove a single item this way */
689 collection_remove_by_info(cd, (CollectInfo *)list->data);
696 cd->list = collection_list_remove(cd->list, work->data);
699 cd->changed = (cd->list != NULL);
701 collection_window_refresh(collection_window_find(cd));
704 gint collection_rename(CollectionData *cd, const gchar *source, const gchar *dest)
708 ci = collection_list_find(cd->list, source);
710 if (!ci) return FALSE;
713 ci->path = g_strdup(dest);
716 collection_window_update(collection_window_find(cd), ci);
721 void collection_update_geometry(CollectionData *cd)
723 collection_window_get_geometry(collection_window_find(cd));
727 *-------------------------------------------------------------------
728 * simple maintenance for renaming, deleting
729 *-------------------------------------------------------------------
732 void collection_maint_removed(const gchar *path)
736 work = collection_list;
739 CollectionData *cd = work->data;
742 while(collection_remove(cd, path));
745 /* Do we really need to do this? removed files are
746 * automatically ignored when loading a collection.
748 collect_manager_moved(path, NULL);
752 void collection_maint_renamed(const gchar *source, const gchar *dest)
756 work = collection_list;
759 CollectionData *cd = work->data;
762 while(collection_rename(cd, source, dest));
765 collect_manager_moved(source, dest);
769 *-------------------------------------------------------------------
771 *-------------------------------------------------------------------
774 static gint collection_window_keypress(GtkWidget *widget, GdkEventKey *event, gpointer data)
776 CollectWindow *cw = data;
777 gint stop_signal = FALSE;
781 if (event->state & GDK_CONTROL_MASK)
784 switch (event->keyval)
817 if (event->state & GDK_SHIFT_MASK)
819 collection_table_unselect_all(cw->table);
823 collection_table_select_all(cw->table);
827 list = layout_list(NULL);
830 collection_table_add_path_list(cw->table, list);
831 path_list_free(list);
835 file_util_copy(NULL, collection_table_selection_get_list(cw->table), NULL, cw->window);
838 file_util_move(NULL, collection_table_selection_get_list(cw->table), NULL, cw->window);
841 file_util_rename(NULL, collection_table_selection_get_list(cw->table), cw->window);
844 file_util_delete(NULL, collection_table_selection_get_list(cw->table), cw->window);
847 info_window_new(NULL, collection_table_selection_get_list(cw->table));
850 collection_dialog_save_as(NULL, cw->cd);
853 collection_window_close(cw);
863 switch (event->keyval)
865 case GDK_Return: case GDK_KP_Enter:
866 layout_image_set_collection(NULL, cw->cd,
867 collection_table_get_focus_info(cw->table));
870 view_window_new_from_collection(cw->cd,
871 collection_table_get_focus_info(cw->table));
876 collection_dialog_save_as(NULL, cw->cd);
878 else if (!collection_save(cw->cd, cw->cd->path))
880 printf("failed saving to collection path: %s\n", cw->cd->path);
884 collection_dialog_append(NULL, cw->cd);
887 collection_set_sort_method(cw->cd, SORT_NAME);
889 #ifdef HAVE_STRVERSCMP
891 collection_set_sort_method(cw->cd, SORT_NUMBER);
895 collection_set_sort_method(cw->cd, SORT_TIME);
898 collection_set_sort_method(cw->cd, SORT_SIZE);
901 collection_set_sort_method(cw->cd, SORT_PATH);
903 case GDK_Delete: case GDK_KP_Delete:
904 list = g_list_copy(cw->table->selection);
907 collection_remove_by_info_list(cw->cd, list);
912 collection_remove_by_info(cw->cd, collection_table_get_focus_info(cw->table));
923 list = collection_table_selection_get_list(cw->table);
924 start_editor_from_path_list(edit_val, list);
925 path_list_free(list);
932 *-------------------------------------------------------------------
934 *-------------------------------------------------------------------
936 static void collection_window_get_geometry(CollectWindow *cw)
943 gdk_window_get_position (cw->window->window, &cd->window_x, &cd->window_y);
944 gdk_drawable_get_size(cw->window->window, &cd->window_w, &cd->window_h);
945 cd->window_read = TRUE;
948 static void collection_window_refresh(CollectWindow *cw)
952 collection_table_refresh(cw->table);
955 static void collection_window_update_title(CollectWindow *cw)
961 buf = g_strdup_printf(_("%s - GQview Collection"), cw->cd->name);
962 gtk_window_set_title(GTK_WINDOW(cw->window), buf);
966 static void collection_window_update_info(CollectionData *cd, CollectInfo *ci, gpointer data)
968 CollectWindow *cw = data;
970 collection_table_file_update(cw->table, ci);
973 static void collection_window_add(CollectWindow *cw, CollectInfo *ci)
977 if (!ci->pixbuf) collection_load_thumb_idle(cw->cd);
978 collection_table_file_add(cw->table, ci);
981 static void collection_window_insert(CollectWindow *cw, CollectInfo *ci)
985 if (!ci->pixbuf) collection_load_thumb_idle(cw->cd);
986 collection_table_file_insert(cw->table, ci);
991 static void collection_window_move(CollectWindow *cw, CollectInfo *ci)
997 static void collection_window_remove(CollectWindow *cw, CollectInfo *ci)
1001 collection_table_file_remove(cw->table, ci);
1004 static void collection_window_update(CollectWindow *cw, CollectInfo *ci)
1008 collection_table_file_update(cw->table, ci);
1009 collection_table_file_update(cw->table, NULL);
1012 static void collection_window_close_final(CollectWindow *cw)
1014 if (cw->close_dialog) return;
1016 collection_window_list = g_list_remove(collection_window_list, cw);
1017 collection_window_get_geometry(cw);
1019 gtk_widget_destroy(cw->window);
1021 collection_set_update_info_func(cw->cd, NULL, NULL);
1022 collection_unref(cw->cd);
1027 static void collection_close_save_cb(GenericDialog *gd, gpointer data)
1029 CollectWindow *cw = data;
1031 cw->close_dialog = NULL;
1032 generic_dialog_close(gd);
1036 collection_dialog_save_close(NULL, cw->cd);
1039 else if (!collection_save(cw->cd, cw->cd->path))
1042 buf = g_strdup_printf(_("Failed to save the collection:\n%s"), cw->cd->path);
1043 warning_dialog(_("Save Failed"), buf, GTK_STOCK_DIALOG_ERROR, cw->window);
1048 collection_window_close_final(cw);
1051 static void collection_close_close_cb(GenericDialog *gd, gpointer data)
1053 CollectWindow *cw = data;
1055 cw->close_dialog = NULL;
1056 generic_dialog_close(gd);
1058 collection_window_close_final(cw);
1061 static void collection_close_cancel_cb(GenericDialog *gd, gpointer data)
1063 CollectWindow *cw = data;
1065 cw->close_dialog = NULL;
1066 generic_dialog_close(gd);
1069 static void collection_close_dlg_show(CollectWindow *cw)
1073 if (cw->close_dialog)
1075 gtk_window_present(GTK_WINDOW(cw->close_dialog));
1079 gd = generic_dialog_new(_("Close collection"),
1080 "GQview", "close_collection", cw->window, FALSE,
1081 collection_close_cancel_cb, cw);
1082 generic_dialog_add_message(gd, GTK_STOCK_DIALOG_QUESTION,
1083 _("Close collection"),
1084 _("Collection has been modified.\nSave first?"));
1086 generic_dialog_add_button(gd, GTK_STOCK_SAVE, NULL, collection_close_save_cb, TRUE);
1087 generic_dialog_add_button(gd, GTK_STOCK_DELETE, _("_Discard"), collection_close_close_cb, FALSE);
1089 cw->close_dialog = gd->dialog;
1091 gtk_widget_show(gd->dialog);
1094 static void collection_window_close(CollectWindow *cw)
1096 if (!cw->cd->changed && !cw->close_dialog)
1098 collection_window_close_final(cw);
1102 collection_close_dlg_show(cw);
1105 void collection_window_close_by_collection(CollectionData *cd)
1109 cw = collection_window_find(cd);
1110 if (cw) collection_window_close_final(cw);
1113 gint collection_window_modified_exists(void)
1117 work = collection_window_list;
1120 CollectWindow *cw = work->data;
1121 if (cw->cd->changed) return TRUE;
1128 static gint collection_window_delete(GtkWidget *widget, GdkEvent *event, gpointer data)
1130 CollectWindow *cw = data;
1131 collection_window_close(cw);
1136 CollectWindow *collection_window_new(const gchar *path)
1141 GtkWidget *status_label;
1142 GtkWidget *extra_label;
1143 GdkGeometry geometry;
1145 cw = g_new0(CollectWindow, 1);
1147 cw->close_dialog = NULL;
1149 collection_window_list = g_list_append(collection_window_list, cw);
1151 cw->cd = collection_new(path);
1153 cw->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1154 window_set_icon(cw->window, PIXBUF_INLINE_ICON_BOOK, NULL);
1156 geometry.min_width = 32;
1157 geometry.min_height = 32;
1158 geometry.base_width = COLLECT_DEF_WIDTH;
1159 geometry.base_height = COLLECT_DEF_HEIGHT;
1160 gtk_window_set_geometry_hints(GTK_WINDOW(cw->window), NULL, &geometry,
1161 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE);
1164 if (save_window_positions && path && collection_load_only_geometry(cw->cd, path))
1166 /* FIXME: x, y is not implemented */
1167 gtk_window_set_default_size(GTK_WINDOW(cw->window), cw->cd->window_w, cw->cd->window_h);
1171 gtk_window_set_default_size(GTK_WINDOW(cw->window), COLLECT_DEF_WIDTH, COLLECT_DEF_HEIGHT);
1174 gtk_window_set_resizable(GTK_WINDOW(cw->window), TRUE);
1175 collection_window_update_title(cw);
1176 gtk_window_set_wmclass(GTK_WINDOW(cw->window), "collection", "GQview");
1177 gtk_container_set_border_width (GTK_CONTAINER (cw->window), 0);
1179 g_signal_connect(G_OBJECT(cw->window), "delete_event",
1180 G_CALLBACK(collection_window_delete), cw);
1182 g_signal_connect(G_OBJECT(cw->window),"key_press_event",
1183 G_CALLBACK(collection_window_keypress), cw);
1185 vbox = gtk_vbox_new(FALSE, 0);
1186 gtk_container_add(GTK_CONTAINER(cw->window), vbox);
1187 gtk_widget_show(vbox);
1189 cw->table = collection_table_new(cw->cd);
1190 gtk_box_pack_start(GTK_BOX(vbox), cw->table->scrolled, TRUE, TRUE, 0);
1191 gtk_widget_show(cw->table->scrolled);
1193 cw->status_box = gtk_hbox_new(TRUE, 0);
1194 gtk_box_pack_start(GTK_BOX(vbox), cw->status_box, FALSE, FALSE, 0);
1195 gtk_widget_show(cw->status_box);
1197 frame = gtk_frame_new(NULL);
1198 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
1199 gtk_box_pack_start(GTK_BOX(cw->status_box), frame, TRUE, TRUE, 0);
1200 gtk_widget_show(frame);
1202 status_label = gtk_label_new("");
1203 gtk_container_add(GTK_CONTAINER(frame), status_label);
1204 gtk_widget_show(status_label);
1206 extra_label = gtk_progress_bar_new();
1207 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(extra_label), 0.0);
1208 gtk_box_pack_start(GTK_BOX(cw->status_box), extra_label, TRUE, TRUE, 0);
1209 gtk_widget_show(extra_label);
1211 collection_table_set_labels(cw->table, status_label, extra_label);
1213 gtk_widget_show(cw->window);
1214 gtk_widget_grab_focus(cw->table->listview);
1216 collection_set_update_info_func(cw->cd, collection_window_update_info, cw);
1218 if (path && *path == '/') collection_load_begin(cw->cd, NULL, FALSE);