2 * (SLIK) SimpLIstic sKin functions
4 * Copyright (C) 2008 The Geeqie Team
8 * This software is released under the GNU General Public License (GNU GPL).
9 * Please read the included file COPYING for more information.
10 * This software comes with no warranty of any kind, use at your own risk!
24 #include <gdk/gdkkeysyms.h> /* for key values */
29 #include "secure_save.h"
30 #include "ui_bookmark.h"
31 #include "ui_fileops.h"
34 #include "ui_utildlg.h"
35 #include "ui_tabcomp.h"
39 *-----------------------------------------------------------------------------
41 *-----------------------------------------------------------------------------
44 #define HISTORY_DEFAULT_KEY_COUNT 16
47 typedef struct _HistoryData HistoryData;
54 static GList *history_list = NULL;
57 static gchar *quoted_from_text(const gchar *text)
61 gint l = strlen(text);
63 if (l == 0) return NULL;
65 while (c < l && text[c] !='"') c++;
72 while (e < l && text[e] !='"') e++;
77 return g_strndup(ptr, e - c);
84 gint history_list_load(const gchar *path)
91 pathl = path_from_utf8(path);
92 f = fopen(pathl, "r");
96 /* first line must start with History comment */
97 if (!fgets(s_buf, sizeof(s_buf), f) ||
98 strncmp(s_buf, "#History", 8) != 0)
104 while (fgets(s_buf, sizeof(s_buf), f))
106 if (s_buf[0]=='#') continue;
114 while (ptr[c] != ']' && ptr[c] != '\n' && ptr[c] != '\0') c++;
117 key = g_strndup(ptr, c);
123 value = quoted_from_text(s_buf);
126 history_list_add_to_key(key, value, 0);
139 gint history_list_save(const gchar *path)
145 pathl = path_from_utf8(path);
146 ssi = secure_open(pathl);
150 log_printf(_("Unable to write history lists to: %s\n"), path);
154 secure_fprintf(ssi, "#History lists\n\n");
156 list = g_list_last(history_list);
157 while (list && secsave_errno == SS_ERR_NONE)
165 secure_fprintf(ssi, "[%s]\n", hd->key);
167 /* save them inverted (oldest to newest)
168 * so that when reading they are added correctly
170 work = g_list_last(hd->list);
171 while (work && secsave_errno == SS_ERR_NONE)
173 secure_fprintf(ssi, "\"%s\"\n", (gchar *)work->data);
176 secure_fputc(ssi, '\n');
179 secure_fprintf(ssi, "#end\n");
181 return (secure_close(ssi) == 0);
184 static void history_list_free(HistoryData *hd)
201 static HistoryData *history_list_find_by_key(const gchar* key)
203 GList *work = history_list;
205 if (!key) return NULL;
209 HistoryData *hd = work->data;
210 if (strcmp(hd->key, key) == 0) return hd;
216 const gchar *history_list_find_last_path_by_key(const gchar* key)
220 hd = history_list_find_by_key(key);
221 if (!hd || !hd->list) return NULL;
223 return hd->list->data;
226 void history_list_free_key(const gchar *key)
229 hd = history_list_find_by_key(key);
232 history_list = g_list_remove(history_list, hd);
233 history_list_free(hd);
236 void history_list_add_to_key(const gchar *key, const gchar *path, gint max)
241 if (!key || !path) return;
243 hd = history_list_find_by_key(key);
246 hd = g_new(HistoryData, 1);
247 hd->key = g_strdup(key);
249 history_list = g_list_prepend(history_list, hd);
252 /* if already in the list, simply move it to the top */
256 gchar *buf = work->data;
258 if (strcmp(buf, path) == 0)
260 /* if not first, move it */
261 if (work != hd->list)
263 hd->list = g_list_remove(hd->list, buf);
264 hd->list = g_list_prepend(hd->list, buf);
271 hd->list = g_list_prepend(hd->list, g_strdup(path));
273 if (max == -1) max = HISTORY_DEFAULT_KEY_COUNT;
277 GList *work = hd->list;
288 while (work && len > max)
294 hd->list = g_list_delete_link(hd->list, node);
300 void history_list_item_change(const gchar *key, const gchar *oldpath, const gchar *newpath)
305 if (!oldpath) return;
306 hd = history_list_find_by_key(key);
312 gchar *buf = work->data;
313 if (strcmp(buf, oldpath) == 0)
317 work->data = g_strdup(newpath);
321 hd->list = g_list_remove(hd->list, buf);
330 void history_list_item_move(const gchar *key, const gchar *path, gint direction)
337 hd = history_list_find_by_key(key);
343 gchar *buf = work->data;
344 if (strcmp(buf, path) == 0)
348 hd->list = g_list_remove(hd->list, buf);
349 hd->list = g_list_insert(hd->list, buf, p);
357 void history_list_item_remove(const gchar *key, const gchar *path)
359 history_list_item_change(key, path, NULL);
362 GList *history_list_get_by_key(const gchar *key)
366 hd = history_list_find_by_key(key);
367 if (!hd) return NULL;
373 *-----------------------------------------------------------------------------
375 *-----------------------------------------------------------------------------
378 #define BOOKMARK_DATA_KEY "bookmarkdata"
379 #define MARKER_PATH "[path]"
380 #define MARKER_ICON "[icon]"
382 typedef struct _BookMarkData BookMarkData;
383 typedef struct _BookButtonData BookButtonData;
384 typedef struct _BookPropData BookPropData;
392 void (*select_func)(const gchar *path, gpointer data);
393 gpointer select_data;
397 gint only_directories;
399 BookButtonData *active_button;
402 struct _BookButtonData
417 GtkWidget *name_entry;
418 GtkWidget *path_entry;
419 GtkWidget *icon_entry;
430 static GtkTargetEntry bookmark_drop_types[] = {
431 { "text/uri-list", 0, TARGET_URI_LIST },
432 { "x-url/http", 0, TARGET_X_URL },
433 { "_NETSCAPE_URL", 0, TARGET_X_URL }
435 #define bookmark_drop_types_n 3
437 static GtkTargetEntry bookmark_drag_types[] = {
438 { "text/uri-list", 0, TARGET_URI_LIST },
439 { "text/plain", 0, TARGET_TEXT_PLAIN }
441 #define bookmark_drag_types_n 2
444 static GList *bookmark_widget_list = NULL;
445 static GList *bookmark_default_list = NULL;
448 static void bookmark_populate_all(const gchar *key);
451 static BookButtonData *bookmark_from_string(const gchar *text)
454 const gchar *path_ptr;
455 const gchar *icon_ptr;
457 b = g_new0(BookButtonData, 1);
461 b->name = g_strdup(_("New Bookmark"));
462 b->path = g_strdup(homedir());
467 b->key = g_strdup(text);
469 path_ptr = strstr(text, MARKER_PATH);
470 icon_ptr = strstr(text, MARKER_ICON);
472 if (path_ptr && icon_ptr && icon_ptr < path_ptr)
474 log_printf("warning, bookmark icon must be after path\n");
483 b->name = g_strndup(text, l);
484 path_ptr += strlen(MARKER_PATH);
487 l = icon_ptr - path_ptr;
488 b->path = g_strndup(path_ptr, l);
492 b->path = g_strdup(path_ptr);
497 b->name = g_strdup(text);
498 b->path = g_strdup("");
503 icon_ptr += strlen(MARKER_ICON);
504 b->icon = g_strdup(icon_ptr);
510 static void bookmark_free(BookButtonData *b)
522 static gchar *bookmark_string(const gchar *name, const gchar *path, const gchar *icon)
524 if (!name) name = _("New Bookmark");
525 if (icon && strncmp(icon, G_DIR_SEPARATOR_S, 1) != 0) icon = NULL;
529 return g_strdup_printf("%s"MARKER_PATH"%s"MARKER_ICON"%s", name, path, icon);
532 return g_strdup_printf("%s"MARKER_PATH"%s", name, path);
535 static void bookmark_select_cb(GtkWidget *button, gpointer data)
537 BookMarkData *bm = data;
540 b = g_object_get_data(G_OBJECT(button), "bookbuttondata");
543 if (bm->select_func) bm->select_func(b->path, bm->select_data);
546 static void bookmark_edit_destroy_cb(GtkWidget *widget, gpointer data)
548 BookPropData *p = data;
550 bookmark_free(p->bb);
554 static void bookmark_edit_cancel_cb(GenericDialog *gd, gpointer data)
558 static void bookmark_edit_ok_cb(GenericDialog *gd, gpointer data)
560 BookPropData *p = data;
566 name = gtk_entry_get_text(GTK_ENTRY(p->name_entry));
567 path = remove_trailing_slash(gtk_entry_get_text(GTK_ENTRY(p->path_entry)));
568 icon = gtk_entry_get_text(GTK_ENTRY(p->icon_entry));
570 new = bookmark_string(name, path, icon);
574 history_list_item_change(p->bb->parent, p->bb->key, new);
578 history_list_add_to_key(p->bb->parent, new, 0);
581 if (path && strlen(path) > 0) tab_completion_append_to_history(p->path_entry, path);
582 if (icon && strlen(icon) > 0) tab_completion_append_to_history(p->icon_entry, icon);
587 bookmark_populate_all(p->bb->parent);
590 /* simply pass NULL for text to turn this into a 'new bookmark' dialog */
592 static void bookmark_edit(const gchar *key, const gchar *text, GtkWidget *parent)
600 if (!key) key = "bookmarks";
602 p = g_new0(BookPropData, 1);
604 p->bb = bookmark_from_string(text);
605 p->bb->parent = g_strdup(key);
607 gd = generic_dialog_new(_("Edit Bookmark"), GQ_WMCLASS, "bookmark_edit",
609 bookmark_edit_cancel_cb, p);
610 g_signal_connect(G_OBJECT(gd->dialog), "destroy",
611 G_CALLBACK(bookmark_edit_destroy_cb), p);
613 generic_dialog_add_message(gd, NULL, _("Edit Bookmark"), NULL);
615 generic_dialog_add_button(gd, GTK_STOCK_OK, NULL,
616 bookmark_edit_ok_cb, TRUE);
618 table = pref_table_new(gd->vbox, 3, 2, FALSE, TRUE);
619 pref_table_label(table, 0, 0, _("Name:"), 1.0);
621 p->name_entry = gtk_entry_new();
622 gtk_widget_set_size_request(p->name_entry, 300, -1);
623 if (p->bb->name) gtk_entry_set_text(GTK_ENTRY(p->name_entry), p->bb->name);
624 gtk_table_attach_defaults(GTK_TABLE(table), p->name_entry, 1, 2, 0, 1);
625 generic_dialog_attach_default(gd, p->name_entry);
626 gtk_widget_show(p->name_entry);
628 pref_table_label(table, 0, 1, _("Path:"), 1.0);
630 label = tab_completion_new_with_history(&p->path_entry, p->bb->path,
631 "bookmark_path", -1, NULL, NULL);
632 tab_completion_add_select_button(p->path_entry, NULL, TRUE);
633 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 1, 2);
634 generic_dialog_attach_default(gd, p->path_entry);
635 gtk_widget_show(label);
637 pref_table_label(table, 0, 2, _("Icon:"), 1.0);
640 if (!icon) icon = "";
641 label = tab_completion_new_with_history(&p->icon_entry, icon,
642 "bookmark_icons", -1, NULL, NULL);
643 tab_completion_add_select_button(p->icon_entry, _("Select icon"), FALSE);
644 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 2, 3);
645 generic_dialog_attach_default(gd, p->icon_entry);
646 gtk_widget_show(label);
648 gtk_widget_show(gd->dialog);
651 static void bookmark_move(BookMarkData *bm, GtkWidget *button, gint direction)
658 if (!bm->editable) return;
660 b = g_object_get_data(G_OBJECT(button), "bookbuttondata");
663 list = gtk_container_get_children(GTK_CONTAINER(bm->box));
664 p = g_list_index(list, button);
667 if (p < 0 || p + direction < 0) return;
669 key_holder = bm->key;
670 bm->key = "_TEMPHOLDER";
671 history_list_item_move(key_holder, b->key, -direction);
672 bookmark_populate_all(key_holder);
673 bm->key = key_holder;
675 gtk_box_reorder_child(GTK_BOX(bm->box), button, p + direction);
678 static void bookmark_menu_prop_cb(GtkWidget *widget, gpointer data)
680 BookMarkData *bm = data;
682 if (!bm->active_button) return;
684 bookmark_edit(bm->key, bm->active_button->key, widget);
687 static void bookmark_menu_move(BookMarkData *bm, gint direction)
689 if (!bm->active_button) return;
691 bookmark_move(bm, bm->active_button->button, direction);
694 static void bookmark_menu_up_cb(GtkWidget *widget, gpointer data)
696 bookmark_menu_move(data, -1);
699 static void bookmark_menu_down_cb(GtkWidget *widget, gpointer data)
701 bookmark_menu_move(data, 1);
704 static void bookmark_menu_remove_cb(GtkWidget *widget, gpointer data)
706 BookMarkData *bm = data;
708 if (!bm->active_button) return;
710 history_list_item_remove(bm->key, bm->active_button->key);
711 bookmark_populate_all(bm->key);
714 static void bookmark_menu_position_cb(GtkMenu *menu, gint *x, gint *y, gint *pushed_in, gpointer data)
716 GtkWidget *button = data;
718 gdk_window_get_origin(button->window, x, y);
719 *y += button->allocation.y + button->allocation.height;
722 static void bookmark_menu_popup(BookMarkData *bm, GtkWidget *button,
723 gint button_n, guint32 time, gint local)
728 b = g_object_get_data(G_OBJECT(button), "bookbuttondata");
731 bm->active_button = b;
733 menu = popup_menu_short_lived();
734 menu_item_add_stock_sensitive(menu, _("_Properties..."), GTK_STOCK_PROPERTIES, bm->editable,
735 G_CALLBACK(bookmark_menu_prop_cb), bm);
736 menu_item_add_stock_sensitive(menu, _("Move _up"), GTK_STOCK_GO_UP, bm->editable,
737 G_CALLBACK(bookmark_menu_up_cb), bm);
738 menu_item_add_stock_sensitive(menu, _("Move _down"), GTK_STOCK_GO_DOWN, bm->editable,
739 G_CALLBACK(bookmark_menu_down_cb), bm);
740 menu_item_add_stock_sensitive(menu, _("_Remove"), GTK_STOCK_REMOVE, bm->editable,
741 G_CALLBACK(bookmark_menu_remove_cb), bm);
745 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
746 bookmark_menu_position_cb, button, button_n, time);
750 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, button_n, time);
754 static gint bookmark_press_cb(GtkWidget *button, GdkEventButton *event, gpointer data)
756 BookMarkData *bm = data;
758 if (event->button != MOUSE_BUTTON_RIGHT) return FALSE;
760 bookmark_menu_popup(bm, button, event->button, event->time, FALSE);
765 static gint bookmark_keypress_cb(GtkWidget *button, GdkEventKey *event, gpointer data)
767 BookMarkData *bm = data;
769 switch (event->keyval)
772 if (!(event->state & GDK_CONTROL_MASK)) return FALSE;
774 bookmark_menu_popup(bm, button, 0, event->time, TRUE);
778 if (event->state & GDK_SHIFT_MASK)
780 bookmark_move(bm, button, -1);
785 if (event->state & GDK_SHIFT_MASK)
787 bookmark_move(bm, button, 1);
796 static void bookmark_drag_set_data(GtkWidget *button,
797 GdkDragContext *context, GtkSelectionData *selection_data,
798 guint info, guint time, gpointer data)
800 BookMarkData *bm = data;
802 gchar *uri_text = NULL;
806 if (context->dest_window == bm->widget->window) return;
808 b = g_object_get_data(G_OBJECT(button), "bookbuttondata");
811 list = g_list_append(list, b->path);
815 case TARGET_URI_LIST:
816 uri_text = uri_text_from_list(list, &length, FALSE);
818 case TARGET_TEXT_PLAIN:
819 uri_text = uri_text_from_list(list, &length, TRUE);
825 if (!uri_text) return;
827 gtk_selection_data_set(selection_data, selection_data->target,
828 8, (guchar *)uri_text, length);
832 static void bookmark_drag_begin(GtkWidget *button, GdkDragContext *context, gpointer data)
835 GdkModifierType mask;
838 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8,
839 button->allocation.width, button->allocation.height);
840 gdk_pixbuf_get_from_drawable(pixbuf, button->window, NULL,
841 button->allocation.x, button->allocation.y,
842 0, 0, button->allocation.width, button->allocation.height);
844 gdk_window_get_pointer(button->window, &x, &y, &mask);
846 gtk_drag_set_icon_pixbuf(context, pixbuf,
847 x - button->allocation.x, y - button->allocation.y);
848 g_object_unref(pixbuf);
851 static void bookmark_populate(BookMarkData *bm)
857 box = GTK_BOX(bm->box);
858 children = gtk_container_get_children(GTK_CONTAINER(box));
862 GtkWidget *widget = GTK_WIDGET(work->data);
864 gtk_widget_destroy(widget);
867 if (!bm->no_defaults && !history_list_get_by_key(bm->key))
872 if (!bookmark_default_list)
874 buf = bookmark_string(_("Home"), homedir(), NULL);
875 history_list_add_to_key(bm->key, buf, 0);
878 path = g_build_filename(homedir(), "Desktop", NULL);
881 buf = bookmark_string(_("Desktop"), path, NULL);
882 history_list_add_to_key(bm->key, buf, 0);
888 work = bookmark_default_list;
889 while (work && work->next)
898 buf = bookmark_string(name, path, NULL);
899 history_list_add_to_key(bm->key, buf, 0);
904 work = history_list_get_by_key(bm->key);
905 work = g_list_last(work);
910 b = bookmark_from_string(work->data);
915 b->button = gtk_button_new();
916 gtk_button_set_relief(GTK_BUTTON(b->button), GTK_RELIEF_NONE);
917 gtk_box_pack_start(GTK_BOX(bm->box), b->button, FALSE, FALSE, 0);
918 gtk_widget_show(b->button);
920 g_object_set_data_full(G_OBJECT(b->button), "bookbuttondata",
921 b, (GDestroyNotify)bookmark_free);
923 box = gtk_hbox_new(FALSE, PREF_PAD_BUTTON_GAP);
924 gtk_container_add(GTK_CONTAINER(b->button), box);
925 gtk_widget_show(box);
932 iconl = path_from_utf8(b->icon);
933 pixbuf = gdk_pixbuf_new_from_file(iconl, NULL);
941 gtk_icon_size_lookup(GTK_ICON_SIZE_BUTTON, &w, &h);
943 scaled = gdk_pixbuf_scale_simple(pixbuf, w, h,
944 GDK_INTERP_BILINEAR);
945 b->image = gtk_image_new_from_pixbuf(scaled);
946 g_object_unref(scaled);
947 g_object_unref(pixbuf);
951 b->image = gtk_image_new_from_stock(GTK_STOCK_MISSING_IMAGE,
952 GTK_ICON_SIZE_BUTTON);
957 b->image = gtk_image_new_from_stock(GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_BUTTON);
959 gtk_box_pack_start(GTK_BOX(box), b->image, FALSE, FALSE, 0);
960 gtk_widget_show(b->image);
962 b->label = gtk_label_new(b->name);
963 gtk_box_pack_start(GTK_BOX(box), b->label, FALSE, FALSE, 0);
964 gtk_widget_show(b->label);
966 g_signal_connect(G_OBJECT(b->button), "clicked",
967 G_CALLBACK(bookmark_select_cb), bm);
968 g_signal_connect(G_OBJECT(b->button), "button_press_event",
969 G_CALLBACK(bookmark_press_cb), bm);
970 g_signal_connect(G_OBJECT(b->button), "key_press_event",
971 G_CALLBACK(bookmark_keypress_cb), bm);
973 gtk_drag_source_set(b->button, GDK_BUTTON1_MASK,
974 bookmark_drag_types, bookmark_drag_types_n,
975 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK);
976 g_signal_connect(G_OBJECT(b->button), "drag_data_get",
977 G_CALLBACK(bookmark_drag_set_data), bm);
978 g_signal_connect(G_OBJECT(b->button), "drag_begin",
979 G_CALLBACK(bookmark_drag_begin), bm);
986 static void bookmark_populate_all(const gchar *key)
992 work = bookmark_widget_list;
1000 if (strcmp(bm->key, key) == 0)
1002 bookmark_populate(bm);
1007 static void bookmark_dnd_get_data(GtkWidget *widget,
1008 GdkDragContext *context, gint x, gint y,
1009 GtkSelectionData *selection_data, guint info,
1010 guint time, gpointer data)
1012 BookMarkData *bm = data;
1016 if (!bm->editable) return;
1020 case TARGET_URI_LIST:
1022 list = uri_list_from_text((gchar *)selection_data->data, FALSE);
1029 gchar *path = work->data;
1034 if (bm->only_directories && !isdir(path)) continue;
1035 buf = bookmark_string(filename_from_path(path), path, NULL);
1036 history_list_add_to_key(bm->key, buf, 0);
1040 string_list_free(list);
1042 bookmark_populate_all(bm->key);
1045 static void bookmark_list_destroy(GtkWidget *widget, gpointer data)
1047 BookMarkData *bm = data;
1049 bookmark_widget_list = g_list_remove(bookmark_widget_list, bm);
1055 GtkWidget *bookmark_list_new(const gchar *key,
1056 void (*select_func)(const gchar *path, gpointer data), gpointer select_data)
1058 GtkWidget *scrolled;
1061 if (!key) key = "bookmarks";
1063 bm = g_new0(BookMarkData, 1);
1064 bm->key = g_strdup(key);
1066 bm->select_func = select_func;
1067 bm->select_data = select_data;
1069 bm->no_defaults = FALSE;
1070 bm->editable = TRUE;
1071 bm->only_directories = FALSE;
1073 scrolled = gtk_scrolled_window_new(NULL, NULL);
1074 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
1075 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1077 bm->box = gtk_vbox_new(FALSE, 0);
1078 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled), bm->box);
1079 gtk_widget_show(bm->box);
1081 bookmark_populate(bm);
1083 g_signal_connect(G_OBJECT(bm->box), "destroy",
1084 G_CALLBACK(bookmark_list_destroy), bm);
1085 g_object_set_data(G_OBJECT(bm->box), BOOKMARK_DATA_KEY, bm);
1086 g_object_set_data(G_OBJECT(scrolled), BOOKMARK_DATA_KEY, bm);
1087 bm->widget = scrolled;
1089 gtk_drag_dest_set(scrolled,
1090 GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP,
1091 bookmark_drop_types, bookmark_drop_types_n,
1092 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK);
1093 g_signal_connect(G_OBJECT(scrolled), "drag_data_received",
1094 G_CALLBACK(bookmark_dnd_get_data), bm);
1096 bookmark_widget_list = g_list_append(bookmark_widget_list, bm);
1101 void bookmark_list_set_key(GtkWidget *list, const gchar *key)
1105 if (!list || !key) return;
1107 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
1110 if (bm->key && strcmp(bm->key, key) == 0) return;
1113 bm->key = g_strdup(key);
1115 bookmark_populate(bm);
1118 void bookmark_list_set_no_defaults(GtkWidget *list, gint no_defaults)
1122 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
1125 bm->no_defaults = no_defaults;
1128 void bookmark_list_set_editable(GtkWidget *list, gint editable)
1132 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
1135 bm->editable = editable;
1138 void bookmark_list_set_only_directories(GtkWidget *list, gint only_directories)
1142 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
1145 bm->only_directories = only_directories;
1148 void bookmark_list_add(GtkWidget *list, const gchar *name, const gchar *path)
1153 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
1156 buf = bookmark_string(name, path, NULL);
1157 history_list_add_to_key(bm->key, buf, 0);
1160 bookmark_populate_all(bm->key);
1163 void bookmark_add_default(const gchar *name, const gchar *path)
1165 if (!name || !path) return;
1166 bookmark_default_list = g_list_append(bookmark_default_list, g_strdup(name));
1167 bookmark_default_list = g_list_append(bookmark_default_list, g_strdup(path));
1171 *-----------------------------------------------------------------------------
1172 * combo with history key
1173 *-----------------------------------------------------------------------------
1176 typedef struct _HistoryComboData HistoryComboData;
1177 struct _HistoryComboData
1182 gint history_levels;
1185 static void history_combo_destroy(GtkWidget *widget, gpointer data)
1187 HistoryComboData *hc = data;
1189 g_free(hc->history_key);
1193 /* if text is NULL, entry is set to the most recent item */
1194 GtkWidget *history_combo_new(GtkWidget **entry, const gchar *text,
1195 const gchar *history_key, gint max_levels)
1197 HistoryComboData *hc;
1201 hc = g_new0(HistoryComboData, 1);
1202 hc->history_key = g_strdup(history_key);
1203 hc->history_levels = max_levels;
1205 hc->combo = gtk_combo_box_entry_new_text();
1207 gtk_combo_set_case_sensitive(GTK_COMBO(hc->combo), TRUE);
1208 gtk_combo_set_use_arrows(GTK_COMBO(hc->combo), FALSE);
1211 hc->entry = GTK_BIN(hc->combo)->child;
1213 g_object_set_data(G_OBJECT(hc->combo), "history_combo_data", hc);
1214 g_object_set_data(G_OBJECT(hc->entry), "history_combo_data", hc);
1215 g_signal_connect(G_OBJECT(hc->combo), "destroy",
1216 G_CALLBACK(history_combo_destroy), hc);
1218 work = history_list_get_by_key(hc->history_key);
1221 gtk_combo_box_append_text(GTK_COMBO_BOX(hc->combo), (gchar *)work->data);
1228 gtk_entry_set_text(GTK_ENTRY(hc->entry), text);
1232 gtk_combo_box_set_active(GTK_COMBO_BOX(hc->combo), 0);
1235 if (entry) *entry = hc->entry;
1239 /* if text is NULL, current entry text is used
1240 * widget can be the combo or entry widget
1242 void history_combo_append_history(GtkWidget *widget, const gchar *text)
1244 HistoryComboData *hc;
1247 hc = g_object_get_data(G_OBJECT(widget), "history_combo_data");
1250 log_printf("widget is not a history combo\n");
1256 new_text = g_strdup(text);
1260 new_text = g_strdup(gtk_entry_get_text(GTK_ENTRY(hc->entry)));
1263 if (new_text && strlen(new_text) > 0)
1265 GtkTreeModel *store;
1268 history_list_add_to_key(hc->history_key, new_text, hc->history_levels);
1270 gtk_combo_box_set_active(GTK_COMBO_BOX(hc->combo), -1);
1272 store = gtk_combo_box_get_model(GTK_COMBO_BOX(hc->combo));
1273 gtk_list_store_clear(GTK_LIST_STORE(store));
1275 work = history_list_get_by_key(hc->history_key);
1278 gtk_combo_box_append_text(GTK_COMBO_BOX(hc->combo), (gchar *)work->data);
1287 *-----------------------------------------------------------------------------
1288 * drag and drop uri utils
1289 *-----------------------------------------------------------------------------
1292 /* the following characters are allowed to be unencoded for pathnames:
1295 static gint escape_char_list[] = {
1296 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0 */
1297 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 */
1298 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 20 */
1299 /* spc ! " # $ % & ' */
1300 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, /* 30 */
1301 /* ( ) * + , - . / 0 1 */
1302 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 */
1303 /* 2 3 4 5 6 7 8 9 : ; */
1304 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, /* 50 */
1305 /* < = > ? @ A B C D E */
1306 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, /* 60 */
1307 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 70 */
1308 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 */
1309 /* Z [ \ ] ^ _ ` a b c */
1310 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, /* 90 */
1311 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 100 */
1312 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 110 */
1313 /* x y z { | } ~ del */
1314 0, 0, 0, 1, 1, 1, 0, 0 /* 120, 127 is end */
1317 static gchar *hex_char = "0123456789ABCDEF";
1319 static gint escape_test(guchar c)
1321 if (c < 32 || c > 127) return TRUE;
1322 return (escape_char_list[c] != 0);
1325 static const gchar *escape_code(guchar c)
1327 static gchar text[4];
1330 text[1] = hex_char[c>>4];
1331 text[2] = hex_char[c%16];
1337 gchar *uri_text_escape(const gchar *text)
1343 if (!text) return NULL;
1345 string = g_string_new("");
1350 if (escape_test(*p))
1352 g_string_append(string, escape_code(*p));
1356 g_string_append_c(string, *p);
1361 result = string->str;
1362 g_string_free(string, FALSE);
1364 /* dropped filenames are expected to be utf-8 compatible */
1365 if (!g_utf8_validate(result, -1, NULL))
1369 tmp = g_locale_to_utf8(result, -1, NULL, NULL, NULL);
1380 /* this operates on the passed string, decoding escaped characters */
1381 void uri_text_decode(gchar *text)
1383 if (strchr(text, '%'))
1392 if (*r == '%' && *(r + 1) != '\0' && *(r + 2) != '\0')
1402 n = (gint)strtol(t, NULL, 16);
1403 if (n > 0 && n < 256)
1409 /* invalid number, rewind and ignore this escape */
1421 if (*w != '\0') *w = '\0';
1425 static void uri_list_parse_encoded_chars(GList *list)
1431 gchar *text = work->data;
1433 uri_text_decode(text);
1439 GList *uri_list_from_text(gchar *data, gint files_only)
1446 while (data[b] != '\0')
1448 while (data[e] != '\r' && data[e] != '\n' && data[e] != '\0') e++;
1449 if (strncmp(data + b, "file:", 5) == 0)
1453 while (data[b] == '/' && data[b+1] == '/') b++;
1454 path = g_strndup(data + b, e - b);
1455 list = g_list_append(list, path_to_utf8(path));
1458 else if (!files_only && strncmp(data + b, "http:", 5) == 0)
1460 list = g_list_append(list, g_strndup(data + b, e - b));
1462 else if (!files_only && strncmp(data + b, "ftp:", 3) == 0)
1464 list = g_list_append(list, g_strndup(data + b, e - b));
1466 while (data[e] == '\r' || data[e] == '\n') e++;
1470 uri_list_parse_encoded_chars(list);
1475 GList *uri_filelist_from_text(gchar *data, gint files_only)
1477 GList *path_list = uri_list_from_text(data, files_only);
1478 GList *filelist = filelist_from_path_list(path_list);
1479 string_list_free(path_list);
1483 gchar *uri_text_from_list(GList *list, gint *len, gint plain_text)
1485 gchar *uri_text = NULL;
1495 string = g_string_new("");
1500 const gchar *name8; /* dnd filenames are in utf-8 */
1508 escaped = uri_text_escape(name8);
1509 g_string_append(string, "file:");
1510 g_string_append(string, escaped);
1513 g_string_append(string, "\r\n");
1517 g_string_append(string, name8);
1518 if (work->next) g_string_append(string, "\n");
1524 uri_text = string->str;
1525 if (len) *len = string->len;
1526 g_string_free(string, FALSE);
1531 gchar *uri_text_from_filelist(GList *list, gint *len, gint plain_text)
1533 GList *path_list = filelist_to_path_list(list);
1534 gchar *ret = uri_text_from_list(path_list, len, plain_text);
1535 string_list_free(path_list);