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"
28 #include "ui_fileops.h"
29 #include "ui_tree_edit.h"
31 #include <gdk/gdkkeysyms.h> /* for keyboard values */
34 #define COLLECT_DEF_WIDTH 440
35 #define COLLECT_DEF_HEIGHT 450
37 static GList *collection_list = NULL;
38 static GList *collection_window_list = NULL;
40 static void collection_window_get_geometry(CollectWindow *cw);
41 static void collection_window_refresh(CollectWindow *cw);
42 static void collection_window_update_title(CollectWindow *cw);
43 static void collection_window_add(CollectWindow *cw, CollectInfo *ci);
44 static void collection_window_insert(CollectWindow *cw, CollectInfo *ci);
45 static void collection_window_remove(CollectWindow *cw, CollectInfo *ci);
46 static void collection_window_update(CollectWindow *cw, CollectInfo *ci);
48 static void collection_window_close(CollectWindow *cw);
51 *-------------------------------------------------------------------
53 *-------------------------------------------------------------------
56 CollectInfo *collection_info_new(FileData *fd, struct stat *st, GdkPixbuf *pixbuf)
62 ci = g_new0(CollectInfo, 1);
63 ci->fd = file_data_ref(fd);
66 if (ci->pixbuf) g_object_ref(ci->pixbuf);
71 void collection_info_free_thumb(CollectInfo *ci)
73 if (ci->pixbuf) g_object_unref(ci->pixbuf);
77 void collection_info_free(CollectInfo *ci)
81 file_data_unref(ci->fd);
82 collection_info_free_thumb(ci);
86 void collection_info_set_thumb(CollectInfo *ci, GdkPixbuf *pixbuf)
88 if (pixbuf) g_object_ref(pixbuf);
89 collection_info_free_thumb(ci);
93 gint collection_info_load_thumb(CollectInfo *ci)
95 if (!ci) return FALSE;
97 collection_info_free_thumb(ci);
99 printf("collection_info_load_thumb not implemented!\n(because an instant thumb loader not implemented)");
102 if (create_thumbnail(ci->fd->path, &ci->pixmap, &ci->mask) < 0) return FALSE;
104 if (ci->pixmap) gdk_pixmap_ref(ci->pixmap);
105 if (ci->mask) gdk_bitmap_ref(ci->mask);
111 void collection_list_free(GList *list)
117 collection_info_free((CollectInfo *)work->data);
123 /* an ugly static var, well what ya gonna do ? */
124 static SortType collection_list_sort_method = SORT_NAME;
126 static gint collection_list_sort_cb(gconstpointer a, gconstpointer b)
128 const CollectInfo *cia = a;
129 const CollectInfo *cib = b;
131 switch(collection_list_sort_method)
137 if (cia->fd->size < cib->fd->size) return -1;
138 if (cia->fd->size > cib->fd->size) return 1;
142 if (cia->fd->date < cib->fd->date) return -1;
143 if (cia->fd->date > cib->fd->date) return 1;
147 return CASE_SORT(cia->fd->path, cib->fd->path);
149 #ifdef HAVE_STRVERSCMP
151 return strverscmp(cia->fd->name, cib->fd->name);
156 return CASE_SORT(cia->fd->name, cib->fd->name);
163 GList *collection_list_sort(GList *list, SortType method)
165 if (method == SORT_NONE) return list;
167 collection_list_sort_method = method;
169 return g_list_sort(list, collection_list_sort_cb);
172 GList *collection_list_add(GList *list, CollectInfo *ci, SortType method)
174 if (method != SORT_NONE)
176 collection_list_sort_method = method;
177 list = g_list_insert_sorted(list, ci, collection_list_sort_cb);
181 list = g_list_append(list, ci);
187 GList *collection_list_insert(GList *list, CollectInfo *ci, CollectInfo *insert_ci, SortType method)
189 if (method != SORT_NONE)
191 collection_list_sort_method = method;
192 list = g_list_insert_sorted(list, ci, collection_list_sort_cb);
198 point = g_list_find(list, insert_ci);
199 list = uig_list_insert_link(list, point, ci);
205 GList *collection_list_remove(GList *list, CollectInfo *ci)
207 list = g_list_remove(list, ci);
208 collection_info_free(ci);
212 CollectInfo *collection_list_find(GList *list, const gchar *path)
218 CollectInfo *ci = work->data;
219 if (strcmp(ci->fd->path, path) == 0) return ci;
227 static GList *collection_list_find_link(GList *list, gchar *path)
233 CollectInfo *ci = work->data;
234 if (strcmp(ci->fd->path, path) == 0) return work;
241 static gint collection_list_find_index(GList *list, gchar *path)
248 CollectInfo *ci = work->data;
249 if (strcmp(ci->fd->path, path) == 0) return c;
258 GList *collection_list_to_filelist(GList *list)
260 GList *filelist = NULL;
265 CollectInfo *info = work->data;
266 filelist = g_list_prepend(filelist, file_data_ref(info->fd));
270 filelist = g_list_reverse(filelist);
274 CollectWindow *collection_window_find(CollectionData *cd)
278 work = collection_window_list;
281 CollectWindow *cw = work->data;
282 if (cw->cd == cd) return cw;
289 CollectWindow *collection_window_find_by_path(const gchar *path)
293 if (!path) return NULL;
295 work = collection_window_list;
298 CollectWindow *cw = work->data;
299 if (cw->cd->path && strcmp(cw->cd->path, path) == 0) return cw;
307 *-------------------------------------------------------------------
308 * please use these to actually add/remove stuff
309 *-------------------------------------------------------------------
312 CollectionData *collection_new(const gchar *path)
315 static gint untitled_counter = 0;
317 cd = g_new0(CollectionData, 1);
319 collection_list = g_list_append(collection_list, cd);
321 cd->ref = 1; /* starts with a ref of 1 */
324 cd->sort_method = SORT_NONE;
325 cd->thumb_loader = NULL;
326 cd->info_updated_func = NULL;
328 cd->window_read = FALSE;
331 cd->window_w = COLLECT_DEF_WIDTH;
332 cd->window_h = COLLECT_DEF_HEIGHT;
338 cd->path = g_strdup(path);
339 cd->name = g_strdup(filename_from_path(cd->path));
346 if (untitled_counter == 0)
348 cd->name = g_strdup(_("Untitled"));
352 cd->name = g_strdup_printf(_("Untitled (%d)"), untitled_counter + 1);
361 void collection_free(CollectionData *cd)
365 if (debug) printf("collection \"%s\" freed\n", cd->name);
367 collection_load_stop(cd);
368 collection_list_free(cd->list);
370 collection_list = g_list_remove(collection_list, cd);
378 void collection_ref(CollectionData *cd)
382 if (debug) printf("collection \"%s\" ref count = %d\n", cd->name, cd->ref);
385 void collection_unref(CollectionData *cd)
389 if (debug) printf("collection \"%s\" ref count = %d\n", cd->name, cd->ref);
397 void collection_path_changed(CollectionData *cd)
399 collection_window_update_title(collection_window_find(cd));
402 gint collection_to_number(CollectionData *cd)
404 return g_list_index(collection_list, cd);
407 CollectionData *collection_from_number(gint n)
409 return g_list_nth_data(collection_list, n);
412 CollectionData *collection_from_dnd_data(const gchar *data, GList **list, GList **info_list)
417 if (strncmp(data, "COLLECTION:", 11) != 0) return NULL;
419 n = (gint)strtol(data + 11, NULL, 10);
420 cd = collection_from_number(n);
422 if (!cd || (!list && !info_list))
433 while(data[b] != '\0' && data[b] != '\n' ) b++;
437 while (data[b] != '\0')
441 while (data[e] != '\n' && data[e] != '\0') e++;
442 n = (gint)strtol(data + b, NULL, 10);
444 info = g_list_nth_data(cd->list, n);
445 if (info && list) work = g_list_append(work, file_data_ref(info->fd));
446 if (info && info_list) infol = g_list_append(infol, info);
448 while (data[e] == '\n') e++;
451 if (list) *list = work;
452 if (info_list) *info_list = infol;
458 gchar *collection_info_list_to_dnd_data(CollectionData *cd, GList *list, gint *length)
460 gchar *uri_text = NULL;
467 n = collection_to_number(cd);
476 temp = g_list_prepend(temp, g_strdup_printf("COLLECTION:%d\n", n));
480 n = g_list_index(cd->list, work->data);
483 temp = g_list_prepend(temp, g_strdup_printf("%d\n", n));
492 total += strlen((gchar *)work->data);
497 uri_text = g_malloc(total);
500 work = g_list_last(temp);
503 gchar *text = work->data;
513 string_list_free(temp);
520 gint collection_info_valid(CollectionData *cd, CollectInfo *info)
522 if (collection_to_number(cd) < 0) return FALSE;
524 return (g_list_index(cd->list, info) != 0);
527 CollectInfo *collection_next_by_info(CollectionData *cd, CollectInfo *info)
531 work = g_list_find(cd->list, info);
533 if (!work) return NULL;
535 if (work) return work->data;
539 CollectInfo *collection_prev_by_info(CollectionData *cd, CollectInfo *info)
543 work = g_list_find(cd->list, info);
545 if (!work) return NULL;
547 if (work) return work->data;
551 CollectInfo *collection_get_first(CollectionData *cd)
553 if (cd->list) return cd->list->data;
558 CollectInfo *collection_get_last(CollectionData *cd)
562 list = g_list_last(cd->list);
564 if (list) return list->data;
569 void collection_set_sort_method(CollectionData *cd, SortType method)
573 if (cd->sort_method == method) return;
575 cd->sort_method = method;
576 cd->list = collection_list_sort(cd->list, cd->sort_method);
577 if (cd->list) cd->changed = TRUE;
579 collection_window_refresh(collection_window_find(cd));
582 void collection_set_update_info_func(CollectionData *cd,
583 void (*func)(CollectionData *, CollectInfo *, gpointer), gpointer data)
585 cd->info_updated_func = func;
586 cd->info_updated_data = data;
589 gint collection_add_check(CollectionData *cd, FileData *fd, gint sorted, gint must_exist)
596 valid = (stat_utf8(fd->path, &st) && !S_ISDIR(st.st_mode));
608 ci = collection_info_new(fd, &st, NULL);
609 cd->list = collection_list_add(cd->list, ci, sorted ? cd->sort_method : SORT_NONE);
612 if (!sorted || cd->sort_method == SORT_NONE)
614 collection_window_add(collection_window_find(cd), ci);
618 collection_window_insert(collection_window_find(cd), ci);
625 gint collection_add(CollectionData *cd, FileData *fd, gint sorted)
627 return collection_add_check(cd, fd, sorted, TRUE);
630 gint collection_insert(CollectionData *cd, FileData *fd, CollectInfo *insert_ci, gint sorted)
634 if (!insert_ci) return collection_add(cd, fd, sorted);
636 if (stat_utf8(fd->path, &st) >= 0 && !S_ISDIR(st.st_mode))
639 ci = collection_info_new(fd, &st, NULL);
640 cd->list = collection_list_insert(cd->list, ci, insert_ci, sorted ? cd->sort_method : SORT_NONE);
643 collection_window_insert(collection_window_find(cd), ci);
651 gint collection_remove(CollectionData *cd, FileData *fd)
655 ci = collection_list_find(cd->list, fd->path);
657 if (!ci) return FALSE;
659 cd->list = g_list_remove(cd->list, ci);
662 collection_window_remove(collection_window_find(cd), ci);
663 collection_info_free(ci);
668 static void collection_remove_by_info(CollectionData *cd, CollectInfo *info)
670 if (!info || !g_list_find(cd->list, info)) return;
672 cd->list = g_list_remove(cd->list, info);
673 cd->changed = (cd->list != NULL);
675 collection_window_remove(collection_window_find(cd), info);
676 collection_info_free(info);
679 void collection_remove_by_info_list(CollectionData *cd, GList *list)
687 /* more efficient (in collect-table) to remove a single item this way */
688 collection_remove_by_info(cd, (CollectInfo *)list->data);
695 cd->list = collection_list_remove(cd->list, work->data);
698 cd->changed = (cd->list != NULL);
700 collection_window_refresh(collection_window_find(cd));
703 gint collection_rename(CollectionData *cd, FileData *fd)
706 const gchar *source = fd->change->source;
707 // const gchar *dest = fd->change->dest;
708 ci = collection_list_find(cd->list, source);
710 if (!ci) return FALSE;
713 // ci->path = g_strdup(dest); FIXME
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(FileData *fd)
736 work = collection_list;
739 CollectionData *cd = work->data;
742 while(collection_remove(cd, fd));
745 /* Do we really need to do this? removed files are
746 * automatically ignored when loading a collection.
748 collect_manager_moved(fd, NULL);
752 void collection_maint_renamed(FileData *fd)
756 work = collection_list;
759 CollectionData *cd = work->data;
762 while(collection_rename(cd, fd));
765 collect_manager_moved(fd);
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_filelist(cw->table, 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 if (event->state & GDK_SHIFT_MASK)
905 info = collection_table_get_focus_info(cw->table);
907 print_window_new(info->fd, collection_table_selection_get_list(cw->table),
908 collection_list_to_filelist(cw->cd->list), cw->window);
912 collection_set_sort_method(cw->cd, SORT_PATH);
915 case GDK_Delete: case GDK_KP_Delete:
916 list = g_list_copy(cw->table->selection);
919 collection_remove_by_info_list(cw->cd, list);
924 collection_remove_by_info(cw->cd, collection_table_get_focus_info(cw->table));
935 list = collection_table_selection_get_list(cw->table);
936 start_editor_from_filelist(edit_val, list);
944 *-------------------------------------------------------------------
946 *-------------------------------------------------------------------
948 static void collection_window_get_geometry(CollectWindow *cw)
955 gdk_window_get_position (cw->window->window, &cd->window_x, &cd->window_y);
956 gdk_drawable_get_size(cw->window->window, &cd->window_w, &cd->window_h);
957 cd->window_read = TRUE;
960 static void collection_window_refresh(CollectWindow *cw)
964 collection_table_refresh(cw->table);
967 static void collection_window_update_title(CollectWindow *cw)
973 buf = g_strdup_printf(_("%s - Geeqie Collection"), cw->cd->name);
974 gtk_window_set_title(GTK_WINDOW(cw->window), buf);
978 static void collection_window_update_info(CollectionData *cd, CollectInfo *ci, gpointer data)
980 CollectWindow *cw = data;
982 collection_table_file_update(cw->table, ci);
985 static void collection_window_add(CollectWindow *cw, CollectInfo *ci)
989 if (!ci->pixbuf) collection_load_thumb_idle(cw->cd);
990 collection_table_file_add(cw->table, ci);
993 static void collection_window_insert(CollectWindow *cw, CollectInfo *ci)
997 if (!ci->pixbuf) collection_load_thumb_idle(cw->cd);
998 collection_table_file_insert(cw->table, ci);
1003 static void collection_window_move(CollectWindow *cw, CollectInfo *ci)
1009 static void collection_window_remove(CollectWindow *cw, CollectInfo *ci)
1013 collection_table_file_remove(cw->table, ci);
1016 static void collection_window_update(CollectWindow *cw, CollectInfo *ci)
1020 collection_table_file_update(cw->table, ci);
1021 collection_table_file_update(cw->table, NULL);
1024 static void collection_window_close_final(CollectWindow *cw)
1026 if (cw->close_dialog) return;
1028 collection_window_list = g_list_remove(collection_window_list, cw);
1029 collection_window_get_geometry(cw);
1031 gtk_widget_destroy(cw->window);
1033 collection_set_update_info_func(cw->cd, NULL, NULL);
1034 collection_unref(cw->cd);
1039 static void collection_close_save_cb(GenericDialog *gd, gpointer data)
1041 CollectWindow *cw = data;
1043 cw->close_dialog = NULL;
1044 generic_dialog_close(gd);
1048 collection_dialog_save_close(NULL, cw->cd);
1051 else if (!collection_save(cw->cd, cw->cd->path))
1054 buf = g_strdup_printf(_("Failed to save the collection:\n%s"), cw->cd->path);
1055 warning_dialog(_("Save Failed"), buf, GTK_STOCK_DIALOG_ERROR, cw->window);
1060 collection_window_close_final(cw);
1063 static void collection_close_close_cb(GenericDialog *gd, gpointer data)
1065 CollectWindow *cw = data;
1067 cw->close_dialog = NULL;
1068 generic_dialog_close(gd);
1070 collection_window_close_final(cw);
1073 static void collection_close_cancel_cb(GenericDialog *gd, gpointer data)
1075 CollectWindow *cw = data;
1077 cw->close_dialog = NULL;
1078 generic_dialog_close(gd);
1081 static void collection_close_dlg_show(CollectWindow *cw)
1085 if (cw->close_dialog)
1087 gtk_window_present(GTK_WINDOW(cw->close_dialog));
1091 gd = generic_dialog_new(_("Close collection"),
1092 GQ_WMCLASS, "close_collection", cw->window, FALSE,
1093 collection_close_cancel_cb, cw);
1094 generic_dialog_add_message(gd, GTK_STOCK_DIALOG_QUESTION,
1095 _("Close collection"),
1096 _("Collection has been modified.\nSave first?"));
1098 generic_dialog_add_button(gd, GTK_STOCK_SAVE, NULL, collection_close_save_cb, TRUE);
1099 generic_dialog_add_button(gd, GTK_STOCK_DELETE, _("_Discard"), collection_close_close_cb, FALSE);
1101 cw->close_dialog = gd->dialog;
1103 gtk_widget_show(gd->dialog);
1106 static void collection_window_close(CollectWindow *cw)
1108 if (!cw->cd->changed && !cw->close_dialog)
1110 collection_window_close_final(cw);
1114 collection_close_dlg_show(cw);
1117 void collection_window_close_by_collection(CollectionData *cd)
1121 cw = collection_window_find(cd);
1122 if (cw) collection_window_close_final(cw);
1125 gint collection_window_modified_exists(void)
1129 work = collection_window_list;
1132 CollectWindow *cw = work->data;
1133 if (cw->cd->changed) return TRUE;
1140 static gint collection_window_delete(GtkWidget *widget, GdkEvent *event, gpointer data)
1142 CollectWindow *cw = data;
1143 collection_window_close(cw);
1148 CollectWindow *collection_window_new(const gchar *path)
1153 GtkWidget *status_label;
1154 GtkWidget *extra_label;
1155 GdkGeometry geometry;
1157 cw = g_new0(CollectWindow, 1);
1159 cw->close_dialog = NULL;
1161 collection_window_list = g_list_append(collection_window_list, cw);
1163 cw->cd = collection_new(path);
1165 cw->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1166 window_set_icon(cw->window, PIXBUF_INLINE_ICON_BOOK, NULL);
1168 geometry.min_width = 32;
1169 geometry.min_height = 32;
1170 geometry.base_width = COLLECT_DEF_WIDTH;
1171 geometry.base_height = COLLECT_DEF_HEIGHT;
1172 gtk_window_set_geometry_hints(GTK_WINDOW(cw->window), NULL, &geometry,
1173 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE);
1176 if (save_window_positions && path && collection_load_only_geometry(cw->cd, path))
1178 /* FIXME: x, y is not implemented */
1179 gtk_window_set_default_size(GTK_WINDOW(cw->window), cw->cd->window_w, cw->cd->window_h);
1183 gtk_window_set_default_size(GTK_WINDOW(cw->window), COLLECT_DEF_WIDTH, COLLECT_DEF_HEIGHT);
1186 gtk_window_set_resizable(GTK_WINDOW(cw->window), TRUE);
1187 collection_window_update_title(cw);
1188 gtk_window_set_wmclass(GTK_WINDOW(cw->window), "collection", GQ_WMCLASS);
1189 gtk_container_set_border_width (GTK_CONTAINER (cw->window), 0);
1191 g_signal_connect(G_OBJECT(cw->window), "delete_event",
1192 G_CALLBACK(collection_window_delete), cw);
1194 g_signal_connect(G_OBJECT(cw->window),"key_press_event",
1195 G_CALLBACK(collection_window_keypress), cw);
1197 vbox = gtk_vbox_new(FALSE, 0);
1198 gtk_container_add(GTK_CONTAINER(cw->window), vbox);
1199 gtk_widget_show(vbox);
1201 cw->table = collection_table_new(cw->cd);
1202 gtk_box_pack_start(GTK_BOX(vbox), cw->table->scrolled, TRUE, TRUE, 0);
1203 gtk_widget_show(cw->table->scrolled);
1205 cw->status_box = gtk_hbox_new(TRUE, 0);
1206 gtk_box_pack_start(GTK_BOX(vbox), cw->status_box, FALSE, FALSE, 0);
1207 gtk_widget_show(cw->status_box);
1209 frame = gtk_frame_new(NULL);
1210 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
1211 gtk_box_pack_start(GTK_BOX(cw->status_box), frame, TRUE, TRUE, 0);
1212 gtk_widget_show(frame);
1214 status_label = gtk_label_new("");
1215 gtk_container_add(GTK_CONTAINER(frame), status_label);
1216 gtk_widget_show(status_label);
1218 extra_label = gtk_progress_bar_new();
1219 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(extra_label), 0.0);
1220 gtk_box_pack_start(GTK_BOX(cw->status_box), extra_label, TRUE, TRUE, 0);
1221 gtk_widget_show(extra_label);
1223 collection_table_set_labels(cw->table, status_label, extra_label);
1225 gtk_widget_show(cw->window);
1226 gtk_widget_grab_focus(cw->table->listview);
1228 collection_set_update_info_func(cw->cd, collection_window_update_info, cw);
1230 if (path && *path == '/') collection_load_begin(cw->cd, NULL, FALSE);