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(const gchar *path, struct stat *st, GdkPixbuf *pixbuf)
60 if (!path) return NULL;
62 ci = g_new0(CollectInfo, 1);
63 ci->path = g_strdup(path);
64 ci->size = st->st_size;
65 ci->date = st->st_mtime;
68 if (ci->pixbuf) g_object_ref(ci->pixbuf);
73 void collection_info_free_thumb(CollectInfo *ci)
75 if (ci->pixbuf) g_object_unref(ci->pixbuf);
79 void collection_info_free(CollectInfo *ci)
84 collection_info_free_thumb(ci);
88 void collection_info_set_thumb(CollectInfo *ci, GdkPixbuf *pixbuf)
90 if (pixbuf) g_object_ref(pixbuf);
91 collection_info_free_thumb(ci);
95 gint collection_info_load_thumb(CollectInfo *ci)
97 if (!ci) return FALSE;
99 collection_info_free_thumb(ci);
101 printf("collection_info_load_thumb not implemented!\n(because an instant thumb loader not implemented)");
104 if (create_thumbnail(ci->path, &ci->pixmap, &ci->mask) < 0) return FALSE;
106 if (ci->pixmap) gdk_pixmap_ref(ci->pixmap);
107 if (ci->mask) gdk_bitmap_ref(ci->mask);
113 void collection_list_free(GList *list)
119 collection_info_free((CollectInfo *)work->data);
125 /* an ugly static var, well what ya gonna do ? */
126 static SortType collection_list_sort_method = SORT_NAME;
128 static gint collection_list_sort_cb(gconstpointer a, gconstpointer b)
130 const CollectInfo *cia = a;
131 const CollectInfo *cib = b;
133 switch(collection_list_sort_method)
139 if (cia->size < cib->size) return -1;
140 if (cia->size > cib->size) return 1;
144 if (cia->date < cib->date) return -1;
145 if (cia->date > cib->date) return 1;
149 return CASE_SORT(cia->path, cib->path);
151 #ifdef HAVE_STRVERSCMP
153 return strverscmp(filename_from_path(cia->path), filename_from_path(cib->path));
158 return CASE_SORT(filename_from_path(cia->path), filename_from_path(cib->path));
165 GList *collection_list_sort(GList *list, SortType method)
167 if (method == SORT_NONE) return list;
169 collection_list_sort_method = method;
171 return g_list_sort(list, collection_list_sort_cb);
174 GList *collection_list_add(GList *list, CollectInfo *ci, SortType method)
176 if (method != SORT_NONE)
178 collection_list_sort_method = method;
179 list = g_list_insert_sorted(list, ci, collection_list_sort_cb);
183 list = g_list_append(list, ci);
189 GList *collection_list_insert(GList *list, CollectInfo *ci, CollectInfo *insert_ci, SortType method)
191 if (method != SORT_NONE)
193 collection_list_sort_method = method;
194 list = g_list_insert_sorted(list, ci, collection_list_sort_cb);
200 point = g_list_find(list, insert_ci);
201 list = uig_list_insert_link(list, point, ci);
207 GList *collection_list_remove(GList *list, CollectInfo *ci)
209 list = g_list_remove(list, ci);
210 collection_info_free(ci);
214 CollectInfo *collection_list_find(GList *list, const gchar *path)
220 CollectInfo *ci = work->data;
221 if (strcmp(ci->path, path) == 0) return ci;
229 static GList *collection_list_find_link(GList *list, gchar *path)
235 CollectInfo *ci = work->data;
236 if (strcmp(ci->path, path) == 0) return work;
243 static gint collection_list_find_index(GList *list, gchar *path)
250 CollectInfo *ci = work->data;
251 if (strcmp(ci->path, path) == 0) return c;
260 GList *collection_list_to_path_list(GList *list)
262 GList *pathlist = NULL;
267 CollectInfo *info = work->data;
268 pathlist = g_list_prepend(pathlist, g_strdup(info->path));
272 pathlist = g_list_reverse(pathlist);
276 CollectWindow *collection_window_find(CollectionData *cd)
280 work = collection_window_list;
283 CollectWindow *cw = work->data;
284 if (cw->cd == cd) return cw;
291 CollectWindow *collection_window_find_by_path(const gchar *path)
295 if (!path) return NULL;
297 work = collection_window_list;
300 CollectWindow *cw = work->data;
301 if (cw->cd->path && strcmp(cw->cd->path, path) == 0) return cw;
309 *-------------------------------------------------------------------
310 * please use these to actually add/remove stuff
311 *-------------------------------------------------------------------
314 CollectionData *collection_new(const gchar *path)
317 static gint untitled_counter = 0;
319 cd = g_new0(CollectionData, 1);
321 collection_list = g_list_append(collection_list, cd);
323 cd->ref = 1; /* starts with a ref of 1 */
326 cd->sort_method = SORT_NONE;
327 cd->thumb_loader = NULL;
328 cd->info_updated_func = NULL;
330 cd->window_read = FALSE;
333 cd->window_w = COLLECT_DEF_WIDTH;
334 cd->window_h = COLLECT_DEF_HEIGHT;
340 cd->path = g_strdup(path);
341 cd->name = g_strdup(filename_from_path(cd->path));
348 if (untitled_counter == 0)
350 cd->name = g_strdup(_("Untitled"));
354 cd->name = g_strdup_printf(_("Untitled (%d)"), untitled_counter + 1);
363 void collection_free(CollectionData *cd)
367 if (debug) printf("collection \"%s\" freed\n", cd->name);
369 collection_load_stop(cd);
370 collection_list_free(cd->list);
372 collection_list = g_list_remove(collection_list, cd);
380 void collection_ref(CollectionData *cd)
384 if (debug) printf("collection \"%s\" ref count = %d\n", cd->name, cd->ref);
387 void collection_unref(CollectionData *cd)
391 if (debug) printf("collection \"%s\" ref count = %d\n", cd->name, cd->ref);
399 void collection_path_changed(CollectionData *cd)
401 collection_window_update_title(collection_window_find(cd));
404 gint collection_to_number(CollectionData *cd)
406 return g_list_index(collection_list, cd);
409 CollectionData *collection_from_number(gint n)
411 return g_list_nth_data(collection_list, n);
414 CollectionData *collection_from_dnd_data(const gchar *data, GList **list, GList **info_list)
419 if (strncmp(data, "COLLECTION:", 11) != 0) return NULL;
421 n = (gint)strtol(data + 11, NULL, 10);
422 cd = collection_from_number(n);
424 if (!cd || (!list && !info_list))
435 while(data[b] != '\0' && data[b] != '\n' ) b++;
439 while (data[b] != '\0')
443 while (data[e] != '\n' && data[e] != '\0') e++;
444 n = (gint)strtol(data + b, NULL, 10);
446 info = g_list_nth_data(cd->list, n);
447 if (info && list) work = g_list_append(work, g_strdup(info->path));
448 if (info && info_list) infol = g_list_append(infol, info);
450 while (data[e] == '\n') e++;
453 if (list) *list = work;
454 if (info_list) *info_list = infol;
460 gchar *collection_info_list_to_dnd_data(CollectionData *cd, GList *list, gint *length)
462 gchar *uri_text = NULL;
469 n = collection_to_number(cd);
478 temp = g_list_prepend(temp, g_strdup_printf("COLLECTION:%d\n", n));
482 n = g_list_index(cd->list, work->data);
485 temp = g_list_prepend(temp, g_strdup_printf("%d\n", n));
494 total += strlen((gchar *)work->data);
499 uri_text = g_malloc(total);
502 work = g_list_last(temp);
505 gchar *text = work->data;
515 path_list_free(temp);
522 gint collection_info_valid(CollectionData *cd, CollectInfo *info)
524 if (collection_to_number(cd) < 0) return FALSE;
526 return (g_list_index(cd->list, info) != 0);
529 CollectInfo *collection_next_by_info(CollectionData *cd, CollectInfo *info)
533 work = g_list_find(cd->list, info);
535 if (!work) return NULL;
537 if (work) return work->data;
541 CollectInfo *collection_prev_by_info(CollectionData *cd, CollectInfo *info)
545 work = g_list_find(cd->list, info);
547 if (!work) return NULL;
549 if (work) return work->data;
553 CollectInfo *collection_get_first(CollectionData *cd)
555 if (cd->list) return cd->list->data;
560 CollectInfo *collection_get_last(CollectionData *cd)
564 list = g_list_last(cd->list);
566 if (list) return list->data;
571 void collection_set_sort_method(CollectionData *cd, SortType method)
575 if (cd->sort_method == method) return;
577 cd->sort_method = method;
578 cd->list = collection_list_sort(cd->list, cd->sort_method);
579 if (cd->list) cd->changed = TRUE;
581 collection_window_refresh(collection_window_find(cd));
584 void collection_set_update_info_func(CollectionData *cd,
585 void (*func)(CollectionData *, CollectInfo *, gpointer), gpointer data)
587 cd->info_updated_func = func;
588 cd->info_updated_data = data;
591 gint collection_add_check(CollectionData *cd, const gchar *path, gint sorted, gint must_exist)
598 valid = (stat_utf8(path, &st) && !S_ISDIR(st.st_mode));
610 ci = collection_info_new(path, &st, NULL);
611 cd->list = collection_list_add(cd->list, ci, sorted ? cd->sort_method : SORT_NONE);
614 if (!sorted || cd->sort_method == SORT_NONE)
616 collection_window_add(collection_window_find(cd), ci);
620 collection_window_insert(collection_window_find(cd), ci);
627 gint collection_add(CollectionData *cd, const gchar *path, gint sorted)
629 return collection_add_check(cd, path, sorted, TRUE);
632 gint collection_insert(CollectionData *cd, const gchar *path, CollectInfo *insert_ci, gint sorted)
636 if (!insert_ci) return collection_add(cd, path, sorted);
638 if (stat_utf8(path, &st) >= 0 && !S_ISDIR(st.st_mode))
641 ci = collection_info_new(path, &st, NULL);
642 cd->list = collection_list_insert(cd->list, ci, insert_ci, sorted ? cd->sort_method : SORT_NONE);
645 collection_window_insert(collection_window_find(cd), ci);
653 gint collection_remove(CollectionData *cd, const gchar *path)
657 ci = collection_list_find(cd->list, path);
659 if (!ci) return FALSE;
661 cd->list = g_list_remove(cd->list, ci);
664 collection_window_remove(collection_window_find(cd), ci);
665 collection_info_free(ci);
670 static void collection_remove_by_info(CollectionData *cd, CollectInfo *info)
672 if (!info || !g_list_find(cd->list, info)) return;
674 cd->list = g_list_remove(cd->list, info);
675 cd->changed = (cd->list != NULL);
677 collection_window_remove(collection_window_find(cd), info);
678 collection_info_free(info);
681 void collection_remove_by_info_list(CollectionData *cd, GList *list)
689 /* more efficient (in collect-table) to remove a single item this way */
690 collection_remove_by_info(cd, (CollectInfo *)list->data);
697 cd->list = collection_list_remove(cd->list, work->data);
700 cd->changed = (cd->list != NULL);
702 collection_window_refresh(collection_window_find(cd));
705 gint collection_rename(CollectionData *cd, const gchar *source, const gchar *dest)
709 ci = collection_list_find(cd->list, source);
711 if (!ci) return FALSE;
714 ci->path = g_strdup(dest);
717 collection_window_update(collection_window_find(cd), ci);
722 void collection_update_geometry(CollectionData *cd)
724 collection_window_get_geometry(collection_window_find(cd));
728 *-------------------------------------------------------------------
729 * simple maintenance for renaming, deleting
730 *-------------------------------------------------------------------
733 void collection_maint_removed(const gchar *path)
737 work = collection_list;
740 CollectionData *cd = work->data;
743 while(collection_remove(cd, path));
746 /* Do we really need to do this? removed files are
747 * automatically ignored when loading a collection.
749 collect_manager_moved(path, NULL);
753 void collection_maint_renamed(const gchar *source, const gchar *dest)
757 work = collection_list;
760 CollectionData *cd = work->data;
763 while(collection_rename(cd, source, dest));
766 collect_manager_moved(source, dest);
770 *-------------------------------------------------------------------
772 *-------------------------------------------------------------------
775 static gint collection_window_keypress(GtkWidget *widget, GdkEventKey *event, gpointer data)
777 CollectWindow *cw = data;
778 gint stop_signal = FALSE;
782 if (event->state & GDK_CONTROL_MASK)
785 switch (event->keyval)
818 if (event->state & GDK_SHIFT_MASK)
820 collection_table_unselect_all(cw->table);
824 collection_table_select_all(cw->table);
828 list = layout_list(NULL);
831 collection_table_add_path_list(cw->table, list);
832 path_list_free(list);
836 file_util_copy(NULL, collection_table_selection_get_list(cw->table), NULL, cw->window);
839 file_util_move(NULL, collection_table_selection_get_list(cw->table), NULL, cw->window);
842 file_util_rename(NULL, collection_table_selection_get_list(cw->table), cw->window);
845 file_util_delete(NULL, collection_table_selection_get_list(cw->table), cw->window);
848 info_window_new(NULL, collection_table_selection_get_list(cw->table));
851 collection_dialog_save_as(NULL, cw->cd);
854 collection_window_close(cw);
864 switch (event->keyval)
866 case GDK_Return: case GDK_KP_Enter:
867 layout_image_set_collection(NULL, cw->cd,
868 collection_table_get_focus_info(cw->table));
871 view_window_new_from_collection(cw->cd,
872 collection_table_get_focus_info(cw->table));
877 collection_dialog_save_as(NULL, cw->cd);
879 else if (!collection_save(cw->cd, cw->cd->path))
881 printf("failed saving to collection path: %s\n", cw->cd->path);
885 collection_dialog_append(NULL, cw->cd);
888 collection_set_sort_method(cw->cd, SORT_NAME);
890 #ifdef HAVE_STRVERSCMP
892 collection_set_sort_method(cw->cd, SORT_NUMBER);
896 collection_set_sort_method(cw->cd, SORT_TIME);
899 collection_set_sort_method(cw->cd, SORT_SIZE);
902 if (event->state & GDK_SHIFT_MASK)
907 info = collection_table_get_focus_info(cw->table);
908 path = (info) ? info->path : NULL;
910 print_window_new(path, collection_table_selection_get_list(cw->table),
911 collection_list_to_path_list(cw->cd->list), cw->window);
915 collection_set_sort_method(cw->cd, SORT_PATH);
918 case GDK_Delete: case GDK_KP_Delete:
919 list = g_list_copy(cw->table->selection);
922 collection_remove_by_info_list(cw->cd, list);
927 collection_remove_by_info(cw->cd, collection_table_get_focus_info(cw->table));
938 list = collection_table_selection_get_list(cw->table);
939 start_editor_from_path_list(edit_val, list);
940 path_list_free(list);
947 *-------------------------------------------------------------------
949 *-------------------------------------------------------------------
951 static void collection_window_get_geometry(CollectWindow *cw)
958 gdk_window_get_position (cw->window->window, &cd->window_x, &cd->window_y);
959 gdk_drawable_get_size(cw->window->window, &cd->window_w, &cd->window_h);
960 cd->window_read = TRUE;
963 static void collection_window_refresh(CollectWindow *cw)
967 collection_table_refresh(cw->table);
970 static void collection_window_update_title(CollectWindow *cw)
976 buf = g_strdup_printf(_("%s - GQview Collection"), cw->cd->name);
977 gtk_window_set_title(GTK_WINDOW(cw->window), buf);
981 static void collection_window_update_info(CollectionData *cd, CollectInfo *ci, gpointer data)
983 CollectWindow *cw = data;
985 collection_table_file_update(cw->table, ci);
988 static void collection_window_add(CollectWindow *cw, CollectInfo *ci)
992 if (!ci->pixbuf) collection_load_thumb_idle(cw->cd);
993 collection_table_file_add(cw->table, ci);
996 static void collection_window_insert(CollectWindow *cw, CollectInfo *ci)
1000 if (!ci->pixbuf) collection_load_thumb_idle(cw->cd);
1001 collection_table_file_insert(cw->table, ci);
1006 static void collection_window_move(CollectWindow *cw, CollectInfo *ci)
1012 static void collection_window_remove(CollectWindow *cw, CollectInfo *ci)
1016 collection_table_file_remove(cw->table, ci);
1019 static void collection_window_update(CollectWindow *cw, CollectInfo *ci)
1023 collection_table_file_update(cw->table, ci);
1024 collection_table_file_update(cw->table, NULL);
1027 static void collection_window_close_final(CollectWindow *cw)
1029 if (cw->close_dialog) return;
1031 collection_window_list = g_list_remove(collection_window_list, cw);
1032 collection_window_get_geometry(cw);
1034 gtk_widget_destroy(cw->window);
1036 collection_set_update_info_func(cw->cd, NULL, NULL);
1037 collection_unref(cw->cd);
1042 static void collection_close_save_cb(GenericDialog *gd, gpointer data)
1044 CollectWindow *cw = data;
1046 cw->close_dialog = NULL;
1047 generic_dialog_close(gd);
1051 collection_dialog_save_close(NULL, cw->cd);
1054 else if (!collection_save(cw->cd, cw->cd->path))
1057 buf = g_strdup_printf(_("Failed to save the collection:\n%s"), cw->cd->path);
1058 warning_dialog(_("Save Failed"), buf, GTK_STOCK_DIALOG_ERROR, cw->window);
1063 collection_window_close_final(cw);
1066 static void collection_close_close_cb(GenericDialog *gd, gpointer data)
1068 CollectWindow *cw = data;
1070 cw->close_dialog = NULL;
1071 generic_dialog_close(gd);
1073 collection_window_close_final(cw);
1076 static void collection_close_cancel_cb(GenericDialog *gd, gpointer data)
1078 CollectWindow *cw = data;
1080 cw->close_dialog = NULL;
1081 generic_dialog_close(gd);
1084 static void collection_close_dlg_show(CollectWindow *cw)
1088 if (cw->close_dialog)
1090 gtk_window_present(GTK_WINDOW(cw->close_dialog));
1094 gd = generic_dialog_new(_("Close collection"),
1095 "GQview", "close_collection", cw->window, FALSE,
1096 collection_close_cancel_cb, cw);
1097 generic_dialog_add_message(gd, GTK_STOCK_DIALOG_QUESTION,
1098 _("Close collection"),
1099 _("Collection has been modified.\nSave first?"));
1101 generic_dialog_add_button(gd, GTK_STOCK_SAVE, NULL, collection_close_save_cb, TRUE);
1102 generic_dialog_add_button(gd, GTK_STOCK_DELETE, _("_Discard"), collection_close_close_cb, FALSE);
1104 cw->close_dialog = gd->dialog;
1106 gtk_widget_show(gd->dialog);
1109 static void collection_window_close(CollectWindow *cw)
1111 if (!cw->cd->changed && !cw->close_dialog)
1113 collection_window_close_final(cw);
1117 collection_close_dlg_show(cw);
1120 void collection_window_close_by_collection(CollectionData *cd)
1124 cw = collection_window_find(cd);
1125 if (cw) collection_window_close_final(cw);
1128 gint collection_window_modified_exists(void)
1132 work = collection_window_list;
1135 CollectWindow *cw = work->data;
1136 if (cw->cd->changed) return TRUE;
1143 static gint collection_window_delete(GtkWidget *widget, GdkEvent *event, gpointer data)
1145 CollectWindow *cw = data;
1146 collection_window_close(cw);
1151 CollectWindow *collection_window_new(const gchar *path)
1156 GtkWidget *status_label;
1157 GtkWidget *extra_label;
1158 GdkGeometry geometry;
1160 cw = g_new0(CollectWindow, 1);
1162 cw->close_dialog = NULL;
1164 collection_window_list = g_list_append(collection_window_list, cw);
1166 cw->cd = collection_new(path);
1168 cw->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1169 window_set_icon(cw->window, PIXBUF_INLINE_ICON_BOOK, NULL);
1171 geometry.min_width = 32;
1172 geometry.min_height = 32;
1173 geometry.base_width = COLLECT_DEF_WIDTH;
1174 geometry.base_height = COLLECT_DEF_HEIGHT;
1175 gtk_window_set_geometry_hints(GTK_WINDOW(cw->window), NULL, &geometry,
1176 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE);
1179 if (save_window_positions && path && collection_load_only_geometry(cw->cd, path))
1181 /* FIXME: x, y is not implemented */
1182 gtk_window_set_default_size(GTK_WINDOW(cw->window), cw->cd->window_w, cw->cd->window_h);
1186 gtk_window_set_default_size(GTK_WINDOW(cw->window), COLLECT_DEF_WIDTH, COLLECT_DEF_HEIGHT);
1189 gtk_window_set_resizable(GTK_WINDOW(cw->window), TRUE);
1190 collection_window_update_title(cw);
1191 gtk_window_set_wmclass(GTK_WINDOW(cw->window), "collection", "GQview");
1192 gtk_container_set_border_width (GTK_CONTAINER (cw->window), 0);
1194 g_signal_connect(G_OBJECT(cw->window), "delete_event",
1195 G_CALLBACK(collection_window_delete), cw);
1197 g_signal_connect(G_OBJECT(cw->window),"key_press_event",
1198 G_CALLBACK(collection_window_keypress), cw);
1200 vbox = gtk_vbox_new(FALSE, 0);
1201 gtk_container_add(GTK_CONTAINER(cw->window), vbox);
1202 gtk_widget_show(vbox);
1204 cw->table = collection_table_new(cw->cd);
1205 gtk_box_pack_start(GTK_BOX(vbox), cw->table->scrolled, TRUE, TRUE, 0);
1206 gtk_widget_show(cw->table->scrolled);
1208 cw->status_box = gtk_hbox_new(TRUE, 0);
1209 gtk_box_pack_start(GTK_BOX(vbox), cw->status_box, FALSE, FALSE, 0);
1210 gtk_widget_show(cw->status_box);
1212 frame = gtk_frame_new(NULL);
1213 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
1214 gtk_box_pack_start(GTK_BOX(cw->status_box), frame, TRUE, TRUE, 0);
1215 gtk_widget_show(frame);
1217 status_label = gtk_label_new("");
1218 gtk_container_add(GTK_CONTAINER(frame), status_label);
1219 gtk_widget_show(status_label);
1221 extra_label = gtk_progress_bar_new();
1222 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(extra_label), 0.0);
1223 gtk_box_pack_start(GTK_BOX(cw->status_box), extra_label, TRUE, TRUE, 0);
1224 gtk_widget_show(extra_label);
1226 collection_table_set_labels(cw->table, status_label, extra_label);
1228 gtk_widget_show(cw->window);
1229 gtk_widget_grab_focus(cw->table->listview);
1231 collection_set_update_info_func(cw->cd, collection_window_update_info, cw);
1233 if (path && *path == '/') collection_load_begin(cw->cd, NULL, FALSE);