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 */
30 #include "secure_save.h"
31 #include "ui_bookmark.h"
32 #include "ui_fileops.h"
35 #include "ui_utildlg.h"
36 #include "ui_tabcomp.h"
40 *-----------------------------------------------------------------------------
42 *-----------------------------------------------------------------------------
45 #define HISTORY_DEFAULT_KEY_COUNT 16
48 typedef struct _HistoryData HistoryData;
55 static GList *history_list = NULL;
58 static gchar *quoted_from_text(const gchar *text)
62 gint l = strlen(text);
64 if (l == 0) return NULL;
66 while (c < l && text[c] !='"') c++;
73 while (e < l && text[e] !='"') e++;
78 return g_strndup(ptr, e - c);
85 gint history_list_load(const gchar *path)
92 pathl = path_from_utf8(path);
93 f = fopen(pathl, "r");
97 /* first line must start with History comment */
98 if (!fgets(s_buf, sizeof(s_buf), f) ||
99 strncmp(s_buf, "#History", 8) != 0)
105 while (fgets(s_buf, sizeof(s_buf), f))
107 if (s_buf[0]=='#') continue;
115 while (ptr[c] != ']' && ptr[c] != '\n' && ptr[c] != '\0') c++;
118 key = g_strndup(ptr, c);
124 value = quoted_from_text(s_buf);
127 history_list_add_to_key(key, value, 0);
140 gint history_list_save(const gchar *path)
146 pathl = path_from_utf8(path);
147 ssi = secure_open(pathl);
151 printf_term(_("Unable to write history lists to: %s\n"), path);
155 secure_fprintf(ssi, "#History lists\n\n");
157 list = g_list_last(history_list);
158 while (list && secsave_errno == SS_ERR_NONE)
166 secure_fprintf(ssi, "[%s]\n", hd->key);
168 /* save them inverted (oldest to newest)
169 * so that when reading they are added correctly
171 work = g_list_last(hd->list);
172 while (work && secsave_errno == SS_ERR_NONE)
174 secure_fprintf(ssi, "\"%s\"\n", (gchar *)work->data);
177 secure_fputc(ssi, '\n');
180 secure_fprintf(ssi, "#end\n");
182 return (secure_close(ssi) == 0);
185 static void history_list_free(HistoryData *hd)
202 static HistoryData *history_list_find_by_key(const gchar* key)
204 GList *work = history_list;
206 if (!key) return NULL;
210 HistoryData *hd = work->data;
211 if (strcmp(hd->key, key) == 0) return hd;
217 const gchar *history_list_find_last_path_by_key(const gchar* key)
221 hd = history_list_find_by_key(key);
222 if (!hd || !hd->list) return NULL;
224 return hd->list->data;
227 void history_list_free_key(const gchar *key)
230 hd = history_list_find_by_key(key);
233 history_list = g_list_remove(history_list, hd);
234 history_list_free(hd);
237 void history_list_add_to_key(const gchar *key, const gchar *path, gint max)
242 if (!key || !path) return;
244 hd = history_list_find_by_key(key);
247 hd = g_new(HistoryData, 1);
248 hd->key = g_strdup(key);
250 history_list = g_list_prepend(history_list, hd);
253 /* if already in the list, simply move it to the top */
257 gchar *buf = work->data;
259 if (strcmp(buf, path) == 0)
261 hd->list = g_list_remove(hd->list, buf);
262 hd->list = g_list_prepend(hd->list, buf);
267 hd->list = g_list_prepend(hd->list, g_strdup(path));
269 if (max == -1) max = HISTORY_DEFAULT_KEY_COUNT;
272 while (hd->list && g_list_length(hd->list) > max)
274 GList *work = g_list_last(hd->list);
275 gchar *buf = work->data;
276 hd->list = g_list_remove(hd->list, buf);
282 void history_list_item_change(const gchar *key, const gchar *oldpath, const gchar *newpath)
287 if (!oldpath) return;
288 hd = history_list_find_by_key(key);
294 gchar *buf = work->data;
295 if (strcmp(buf, oldpath) == 0)
299 work->data = g_strdup(newpath);
303 hd->list = g_list_remove(hd->list, buf);
312 void history_list_item_move(const gchar *key, const gchar *path, gint direction)
319 hd = history_list_find_by_key(key);
325 gchar *buf = work->data;
326 if (strcmp(buf, path) == 0)
330 hd->list = g_list_remove(hd->list, buf);
331 hd->list = g_list_insert(hd->list, buf, p);
339 void history_list_item_remove(const gchar *key, const gchar *path)
341 history_list_item_change(key, path, NULL);
344 GList *history_list_get_by_key(const gchar *key)
348 hd = history_list_find_by_key(key);
349 if (!hd) return NULL;
355 *-----------------------------------------------------------------------------
357 *-----------------------------------------------------------------------------
360 #define BOOKMARK_DATA_KEY "bookmarkdata"
361 #define MARKER_PATH "[path]"
362 #define MARKER_ICON "[icon]"
364 typedef struct _BookMarkData BookMarkData;
365 typedef struct _BookButtonData BookButtonData;
366 typedef struct _BookPropData BookPropData;
374 void (*select_func)(const gchar *path, gpointer data);
375 gpointer select_data;
379 gint only_directories;
381 BookButtonData *active_button;
384 struct _BookButtonData
399 GtkWidget *name_entry;
400 GtkWidget *path_entry;
401 GtkWidget *icon_entry;
412 static GtkTargetEntry bookmark_drop_types[] = {
413 { "text/uri-list", 0, TARGET_URI_LIST },
414 { "x-url/http", 0, TARGET_X_URL },
415 { "_NETSCAPE_URL", 0, TARGET_X_URL }
417 #define bookmark_drop_types_n 3
419 static GtkTargetEntry bookmark_drag_types[] = {
420 { "text/uri-list", 0, TARGET_URI_LIST },
421 { "text/plain", 0, TARGET_TEXT_PLAIN }
423 #define bookmark_drag_types_n 2
426 static GList *bookmark_widget_list = NULL;
427 static GList *bookmark_default_list = NULL;
430 static void bookmark_populate_all(const gchar *key);
433 static BookButtonData *bookmark_from_string(const gchar *text)
436 const gchar *path_ptr;
437 const gchar *icon_ptr;
439 b = g_new0(BookButtonData, 1);
443 b->name = g_strdup(_("New Bookmark"));
444 b->path = g_strdup(homedir());
449 b->key = g_strdup(text);
451 path_ptr = strstr(text, MARKER_PATH);
452 icon_ptr = strstr(text, MARKER_ICON);
454 if (path_ptr && icon_ptr && icon_ptr < path_ptr)
456 printf("warning, bookmark icon must be after path\n");
465 b->name = g_strndup(text, l);
466 path_ptr += strlen(MARKER_PATH);
469 l = icon_ptr - path_ptr;
470 b->path = g_strndup(path_ptr, l);
474 b->path = g_strdup(path_ptr);
479 b->name = g_strdup(text);
480 b->path = g_strdup("");
485 icon_ptr += strlen(MARKER_ICON);
486 b->icon = g_strdup(icon_ptr);
492 static void bookmark_free(BookButtonData *b)
504 static gchar *bookmark_string(const gchar *name, const gchar *path, const gchar *icon)
506 if (!name) name = _("New Bookmark");
507 if (icon && strncmp(icon, "/", 1) != 0) icon = NULL;
511 return g_strdup_printf("%s"MARKER_PATH"%s"MARKER_ICON"%s", name, path, icon);
514 return g_strdup_printf("%s"MARKER_PATH"%s", name, path);
517 static void bookmark_select_cb(GtkWidget *button, gpointer data)
519 BookMarkData *bm = data;
522 b = g_object_get_data(G_OBJECT(button), "bookbuttondata");
525 if (bm->select_func) bm->select_func(b->path, bm->select_data);
528 static void bookmark_edit_destroy_cb(GtkWidget *widget, gpointer data)
530 BookPropData *p = data;
532 bookmark_free(p->bb);
536 static void bookmark_edit_cancel_cb(GenericDialog *gd, gpointer data)
540 static void bookmark_edit_ok_cb(GenericDialog *gd, gpointer data)
542 BookPropData *p = data;
548 name = gtk_entry_get_text(GTK_ENTRY(p->name_entry));
549 path = remove_trailing_slash(gtk_entry_get_text(GTK_ENTRY(p->path_entry)));
550 icon = gtk_entry_get_text(GTK_ENTRY(p->icon_entry));
552 new = bookmark_string(name, path, icon);
556 history_list_item_change(p->bb->parent, p->bb->key, new);
560 history_list_add_to_key(p->bb->parent, new, 0);
563 if (path && strlen(path) > 0) tab_completion_append_to_history(p->path_entry, path);
564 if (icon && strlen(icon) > 0) tab_completion_append_to_history(p->icon_entry, icon);
569 bookmark_populate_all(p->bb->parent);
572 /* simply pass NULL for text to turn this into a 'new bookmark' dialog */
574 static void bookmark_edit(const gchar *key, const gchar *text, GtkWidget *parent)
582 if (!key) key = "bookmarks";
584 p = g_new0(BookPropData, 1);
586 p->bb = bookmark_from_string(text);
587 p->bb->parent = g_strdup(key);
589 gd = generic_dialog_new(_("Edit Bookmark"), GQ_WMCLASS, "bookmark_edit",
591 bookmark_edit_cancel_cb, p);
592 g_signal_connect(G_OBJECT(gd->dialog), "destroy",
593 G_CALLBACK(bookmark_edit_destroy_cb), p);
595 generic_dialog_add_message(gd, NULL, _("Edit Bookmark"), NULL);
597 generic_dialog_add_button(gd, GTK_STOCK_OK, NULL,
598 bookmark_edit_ok_cb, TRUE);
600 table = pref_table_new(gd->vbox, 3, 2, FALSE, TRUE);
601 pref_table_label(table, 0, 0, _("Name:"), 1.0);
603 p->name_entry = gtk_entry_new();
604 gtk_widget_set_size_request(p->name_entry, 300, -1);
605 if (p->bb->name) gtk_entry_set_text(GTK_ENTRY(p->name_entry), p->bb->name);
606 gtk_table_attach_defaults(GTK_TABLE(table), p->name_entry, 1, 2, 0, 1);
607 generic_dialog_attach_default(gd, p->name_entry);
608 gtk_widget_show(p->name_entry);
610 pref_table_label(table, 0, 1, _("Path:"), 1.0);
612 label = tab_completion_new_with_history(&p->path_entry, p->bb->path,
613 "bookmark_path", -1, NULL, NULL);
614 tab_completion_add_select_button(p->path_entry, NULL, TRUE);
615 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 1, 2);
616 generic_dialog_attach_default(gd, p->path_entry);
617 gtk_widget_show(label);
619 pref_table_label(table, 0, 2, _("Icon:"), 1.0);
622 if (!icon) icon = "";
623 label = tab_completion_new_with_history(&p->icon_entry, icon,
624 "bookmark_icons", -1, NULL, NULL);
625 tab_completion_add_select_button(p->icon_entry, _("Select icon"), FALSE);
626 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 2, 3);
627 generic_dialog_attach_default(gd, p->icon_entry);
628 gtk_widget_show(label);
630 gtk_widget_show(gd->dialog);
633 static void bookmark_move(BookMarkData *bm, GtkWidget *button, gint direction)
640 if (!bm->editable) return;
642 b = g_object_get_data(G_OBJECT(button), "bookbuttondata");
645 list = gtk_container_get_children(GTK_CONTAINER(bm->box));
646 p = g_list_index(list, button);
649 if (p < 0 || p + direction < 0) return;
651 key_holder = bm->key;
652 bm->key = "_TEMPHOLDER";
653 history_list_item_move(key_holder, b->key, -direction);
654 bookmark_populate_all(key_holder);
655 bm->key = key_holder;
657 gtk_box_reorder_child(GTK_BOX(bm->box), button, p + direction);
660 static void bookmark_menu_prop_cb(GtkWidget *widget, gpointer data)
662 BookMarkData *bm = data;
664 if (!bm->active_button) return;
666 bookmark_edit(bm->key, bm->active_button->key, widget);
669 static void bookmark_menu_move(BookMarkData *bm, gint direction)
671 if (!bm->active_button) return;
673 bookmark_move(bm, bm->active_button->button, direction);
676 static void bookmark_menu_up_cb(GtkWidget *widget, gpointer data)
678 bookmark_menu_move(data, -1);
681 static void bookmark_menu_down_cb(GtkWidget *widget, gpointer data)
683 bookmark_menu_move(data, 1);
686 static void bookmark_menu_remove_cb(GtkWidget *widget, gpointer data)
688 BookMarkData *bm = data;
690 if (!bm->active_button) return;
692 history_list_item_remove(bm->key, bm->active_button->key);
693 bookmark_populate_all(bm->key);
696 static void bookmark_menu_position_cb(GtkMenu *menu, gint *x, gint *y, gint *pushed_in, gpointer data)
698 GtkWidget *button = data;
700 gdk_window_get_origin(button->window, x, y);
701 *y += button->allocation.y + button->allocation.height;
704 static void bookmark_menu_popup(BookMarkData *bm, GtkWidget *button,
705 gint button_n, guint32 time, gint local)
710 b = g_object_get_data(G_OBJECT(button), "bookbuttondata");
713 bm->active_button = b;
715 menu = popup_menu_short_lived();
716 menu_item_add_stock_sensitive(menu, _("_Properties..."), GTK_STOCK_PROPERTIES, bm->editable,
717 G_CALLBACK(bookmark_menu_prop_cb), bm);
718 menu_item_add_stock_sensitive(menu, _("Move _up"), GTK_STOCK_GO_UP, bm->editable,
719 G_CALLBACK(bookmark_menu_up_cb), bm);
720 menu_item_add_stock_sensitive(menu, _("Move _down"), GTK_STOCK_GO_DOWN, bm->editable,
721 G_CALLBACK(bookmark_menu_down_cb), bm);
722 menu_item_add_stock_sensitive(menu, _("_Remove"), GTK_STOCK_REMOVE, bm->editable,
723 G_CALLBACK(bookmark_menu_remove_cb), bm);
727 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
728 bookmark_menu_position_cb, button, button_n, time);
732 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, button_n, time);
736 static gint bookmark_press_cb(GtkWidget *button, GdkEventButton *event, gpointer data)
738 BookMarkData *bm = data;
740 if (event->button != MOUSE_BUTTON_RIGHT) return FALSE;
742 bookmark_menu_popup(bm, button, event->button, event->time, FALSE);
747 static gint bookmark_keypress_cb(GtkWidget *button, GdkEventKey *event, gpointer data)
749 BookMarkData *bm = data;
751 switch (event->keyval)
754 if (!(event->state & GDK_CONTROL_MASK)) return FALSE;
756 bookmark_menu_popup(bm, button, 0, event->time, TRUE);
760 if (event->state & GDK_SHIFT_MASK)
762 bookmark_move(bm, button, -1);
767 if (event->state & GDK_SHIFT_MASK)
769 bookmark_move(bm, button, 1);
778 static void bookmark_drag_set_data(GtkWidget *button,
779 GdkDragContext *context, GtkSelectionData *selection_data,
780 guint info, guint time, gpointer data)
782 BookMarkData *bm = data;
784 gchar *uri_text = NULL;
788 if (context->dest_window == bm->widget->window) return;
790 b = g_object_get_data(G_OBJECT(button), "bookbuttondata");
793 list = g_list_append(list, b->path);
797 case TARGET_URI_LIST:
798 uri_text = uri_text_from_list(list, &length, FALSE);
800 case TARGET_TEXT_PLAIN:
801 uri_text = uri_text_from_list(list, &length, TRUE);
807 if (!uri_text) return;
809 gtk_selection_data_set(selection_data, selection_data->target,
810 8, (guchar *)uri_text, length);
814 static void bookmark_drag_begin(GtkWidget *button, GdkDragContext *context, gpointer data)
817 GdkModifierType mask;
820 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8,
821 button->allocation.width, button->allocation.height);
822 gdk_pixbuf_get_from_drawable(pixbuf, button->window, NULL,
823 button->allocation.x, button->allocation.y,
824 0, 0, button->allocation.width, button->allocation.height);
826 gdk_window_get_pointer(button->window, &x, &y, &mask);
828 gtk_drag_set_icon_pixbuf(context, pixbuf,
829 x - button->allocation.x, y - button->allocation.y);
830 g_object_unref(pixbuf);
833 static void bookmark_populate(BookMarkData *bm)
839 box = GTK_BOX(bm->box);
840 children = gtk_container_get_children(GTK_CONTAINER(box));
844 GtkWidget *widget = GTK_WIDGET(work->data);
846 gtk_widget_destroy(widget);
849 if (!bm->no_defaults && !history_list_get_by_key(bm->key))
854 if (!bookmark_default_list)
856 buf = bookmark_string(_("Home"), homedir(), NULL);
857 history_list_add_to_key(bm->key, buf, 0);
860 path = concat_dir_and_file(homedir(), "Desktop");
863 buf = bookmark_string(_("Desktop"), path, NULL);
864 history_list_add_to_key(bm->key, buf, 0);
870 work = bookmark_default_list;
871 while (work && work->next)
880 buf = bookmark_string(name, path, NULL);
881 history_list_add_to_key(bm->key, buf, 0);
886 work = history_list_get_by_key(bm->key);
887 work = g_list_last(work);
892 b = bookmark_from_string(work->data);
897 b->button = gtk_button_new();
898 gtk_button_set_relief(GTK_BUTTON(b->button), GTK_RELIEF_NONE);
899 gtk_box_pack_start(GTK_BOX(bm->box), b->button, FALSE, FALSE, 0);
900 gtk_widget_show(b->button);
902 g_object_set_data_full(G_OBJECT(b->button), "bookbuttondata",
903 b, (GDestroyNotify)bookmark_free);
905 box = gtk_hbox_new(FALSE, PREF_PAD_BUTTON_GAP);
906 gtk_container_add(GTK_CONTAINER(b->button), box);
907 gtk_widget_show(box);
914 iconl = path_from_utf8(b->icon);
915 pixbuf = gdk_pixbuf_new_from_file(iconl, NULL);
923 gtk_icon_size_lookup(GTK_ICON_SIZE_BUTTON, &w, &h);
925 scaled = gdk_pixbuf_scale_simple(pixbuf, w, h,
926 GDK_INTERP_BILINEAR);
927 b->image = gtk_image_new_from_pixbuf(scaled);
928 g_object_unref(scaled);
929 g_object_unref(pixbuf);
933 b->image = gtk_image_new_from_stock(GTK_STOCK_MISSING_IMAGE,
934 GTK_ICON_SIZE_BUTTON);
939 b->image = gtk_image_new_from_stock(GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_BUTTON);
941 gtk_box_pack_start(GTK_BOX(box), b->image, FALSE, FALSE, 0);
942 gtk_widget_show(b->image);
944 b->label = gtk_label_new(b->name);
945 gtk_box_pack_start(GTK_BOX(box), b->label, FALSE, FALSE, 0);
946 gtk_widget_show(b->label);
948 g_signal_connect(G_OBJECT(b->button), "clicked",
949 G_CALLBACK(bookmark_select_cb), bm);
950 g_signal_connect(G_OBJECT(b->button), "button_press_event",
951 G_CALLBACK(bookmark_press_cb), bm);
952 g_signal_connect(G_OBJECT(b->button), "key_press_event",
953 G_CALLBACK(bookmark_keypress_cb), bm);
955 gtk_drag_source_set(b->button, GDK_BUTTON1_MASK,
956 bookmark_drag_types, bookmark_drag_types_n,
957 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK);
958 g_signal_connect(G_OBJECT(b->button), "drag_data_get",
959 G_CALLBACK(bookmark_drag_set_data), bm);
960 g_signal_connect(G_OBJECT(b->button), "drag_begin",
961 G_CALLBACK(bookmark_drag_begin), bm);
968 static void bookmark_populate_all(const gchar *key)
974 work = bookmark_widget_list;
982 if (strcmp(bm->key, key) == 0)
984 bookmark_populate(bm);
989 static void bookmark_dnd_get_data(GtkWidget *widget,
990 GdkDragContext *context, gint x, gint y,
991 GtkSelectionData *selection_data, guint info,
992 guint time, gpointer data)
994 BookMarkData *bm = data;
998 if (!bm->editable) return;
1002 case TARGET_URI_LIST:
1004 list = uri_list_from_text((gchar *)selection_data->data, FALSE);
1011 gchar *path = work->data;
1016 if (bm->only_directories && !isdir(path)) continue;
1017 buf = bookmark_string(filename_from_path(path), path, NULL);
1018 history_list_add_to_key(bm->key, buf, 0);
1022 string_list_free(list);
1024 bookmark_populate_all(bm->key);
1027 static void bookmark_list_destroy(GtkWidget *widget, gpointer data)
1029 BookMarkData *bm = data;
1031 bookmark_widget_list = g_list_remove(bookmark_widget_list, bm);
1037 GtkWidget *bookmark_list_new(const gchar *key,
1038 void (*select_func)(const gchar *path, gpointer data), gpointer select_data)
1040 GtkWidget *scrolled;
1043 if (!key) key = "bookmarks";
1045 bm = g_new0(BookMarkData, 1);
1046 bm->key = g_strdup(key);
1048 bm->select_func = select_func;
1049 bm->select_data = select_data;
1051 bm->no_defaults = FALSE;
1052 bm->editable = TRUE;
1053 bm->only_directories = FALSE;
1055 scrolled = gtk_scrolled_window_new(NULL, NULL);
1056 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
1057 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1059 bm->box = gtk_vbox_new(FALSE, 0);
1060 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled), bm->box);
1061 gtk_widget_show(bm->box);
1063 bookmark_populate(bm);
1065 g_signal_connect(G_OBJECT(bm->box), "destroy",
1066 G_CALLBACK(bookmark_list_destroy), bm);
1067 g_object_set_data(G_OBJECT(bm->box), BOOKMARK_DATA_KEY, bm);
1068 g_object_set_data(G_OBJECT(scrolled), BOOKMARK_DATA_KEY, bm);
1069 bm->widget = scrolled;
1071 gtk_drag_dest_set(scrolled,
1072 GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP,
1073 bookmark_drop_types, bookmark_drop_types_n,
1074 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK);
1075 g_signal_connect(G_OBJECT(scrolled), "drag_data_received",
1076 G_CALLBACK(bookmark_dnd_get_data), bm);
1078 bookmark_widget_list = g_list_append(bookmark_widget_list, bm);
1083 void bookmark_list_set_key(GtkWidget *list, const gchar *key)
1087 if (!list || !key) return;
1089 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
1092 if (bm->key && strcmp(bm->key, key) == 0) return;
1095 bm->key = g_strdup(key);
1097 bookmark_populate(bm);
1100 void bookmark_list_set_no_defaults(GtkWidget *list, gint no_defaults)
1104 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
1107 bm->no_defaults = no_defaults;
1110 void bookmark_list_set_editable(GtkWidget *list, gint editable)
1114 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
1117 bm->editable = editable;
1120 void bookmark_list_set_only_directories(GtkWidget *list, gint only_directories)
1124 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
1127 bm->only_directories = only_directories;
1130 void bookmark_list_add(GtkWidget *list, const gchar *name, const gchar *path)
1135 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
1138 buf = bookmark_string(name, path, NULL);
1139 history_list_add_to_key(bm->key, buf, 0);
1142 bookmark_populate_all(bm->key);
1145 void bookmark_add_default(const gchar *name, const gchar *path)
1147 if (!name || !path) return;
1148 bookmark_default_list = g_list_append(bookmark_default_list, g_strdup(name));
1149 bookmark_default_list = g_list_append(bookmark_default_list, g_strdup(path));
1153 *-----------------------------------------------------------------------------
1154 * combo with history key
1155 *-----------------------------------------------------------------------------
1158 typedef struct _HistoryComboData HistoryComboData;
1159 struct _HistoryComboData
1164 gint history_levels;
1167 static void history_combo_destroy(GtkWidget *widget, gpointer data)
1169 HistoryComboData *hc = data;
1171 g_free(hc->history_key);
1175 /* if text is NULL, entry is set to the most recent item */
1176 GtkWidget *history_combo_new(GtkWidget **entry, const gchar *text,
1177 const gchar *history_key, gint max_levels)
1179 HistoryComboData *hc;
1183 hc = g_new0(HistoryComboData, 1);
1184 hc->history_key = g_strdup(history_key);
1185 hc->history_levels = max_levels;
1187 hc->combo = gtk_combo_box_entry_new_text();
1189 gtk_combo_set_case_sensitive(GTK_COMBO(hc->combo), TRUE);
1190 gtk_combo_set_use_arrows(GTK_COMBO(hc->combo), FALSE);
1193 hc->entry = GTK_BIN(hc->combo)->child;
1195 g_object_set_data(G_OBJECT(hc->combo), "history_combo_data", hc);
1196 g_object_set_data(G_OBJECT(hc->entry), "history_combo_data", hc);
1197 g_signal_connect(G_OBJECT(hc->combo), "destroy",
1198 G_CALLBACK(history_combo_destroy), hc);
1200 work = history_list_get_by_key(hc->history_key);
1203 gtk_combo_box_append_text(GTK_COMBO_BOX(hc->combo), (gchar *)work->data);
1210 gtk_entry_set_text(GTK_ENTRY(hc->entry), text);
1214 gtk_combo_box_set_active(GTK_COMBO_BOX(hc->combo), 0);
1217 if (entry) *entry = hc->entry;
1221 /* if text is NULL, current entry text is used
1222 * widget can be the combo or entry widget
1224 void history_combo_append_history(GtkWidget *widget, const gchar *text)
1226 HistoryComboData *hc;
1229 hc = g_object_get_data(G_OBJECT(widget), "history_combo_data");
1232 printf("widget is not a history combo\n");
1238 new_text = g_strdup(text);
1242 new_text = g_strdup(gtk_entry_get_text(GTK_ENTRY(hc->entry)));
1245 if (new_text && strlen(new_text) > 0)
1247 GtkTreeModel *store;
1250 history_list_add_to_key(hc->history_key, new_text, hc->history_levels);
1252 gtk_combo_box_set_active(GTK_COMBO_BOX(hc->combo), -1);
1254 store = gtk_combo_box_get_model(GTK_COMBO_BOX(hc->combo));
1255 gtk_list_store_clear(GTK_LIST_STORE(store));
1257 work = history_list_get_by_key(hc->history_key);
1260 gtk_combo_box_append_text(GTK_COMBO_BOX(hc->combo), (gchar *)work->data);
1269 *-----------------------------------------------------------------------------
1270 * drag and drop uri utils
1271 *-----------------------------------------------------------------------------
1274 /* the following characters are allowed to be unencoded for pathnames:
1277 static gint escape_char_list[] = {
1278 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0 */
1279 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 */
1280 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 20 */
1281 /* spc ! " # $ % & ' */
1282 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, /* 30 */
1283 /* ( ) * + , - . / 0 1 */
1284 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 */
1285 /* 2 3 4 5 6 7 8 9 : ; */
1286 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, /* 50 */
1287 /* < = > ? @ A B C D E */
1288 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, /* 60 */
1289 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 70 */
1290 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 */
1291 /* Z [ \ ] ^ _ ` a b c */
1292 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, /* 90 */
1293 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 100 */
1294 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 110 */
1295 /* x y z { | } ~ del */
1296 0, 0, 0, 1, 1, 1, 0, 0 /* 120, 127 is end */
1299 static gchar *hex_char = "0123456789ABCDEF";
1301 static gint escape_test(guchar c)
1303 if (c < 32 || c > 127) return TRUE;
1304 return (escape_char_list[c] != 0);
1307 static const gchar *escape_code(guchar c)
1309 static gchar text[4];
1312 text[1] = hex_char[c>>4];
1313 text[2] = hex_char[c%16];
1319 gchar *uri_text_escape(const gchar *text)
1325 if (!text) return NULL;
1327 string = g_string_new("");
1332 if (escape_test(*p))
1334 g_string_append(string, escape_code(*p));
1338 g_string_append_c(string, *p);
1343 result = string->str;
1344 g_string_free(string, FALSE);
1346 /* dropped filenames are expected to be utf-8 compatible */
1347 if (!g_utf8_validate(result, -1, NULL))
1351 tmp = g_locale_to_utf8(result, -1, NULL, NULL, NULL);
1362 /* this operates on the passed string, decoding escaped characters */
1363 void uri_text_decode(gchar *text)
1365 if (strchr(text, '%'))
1374 if (*r == '%' && *(r + 1) != '\0' && *(r + 2) != '\0')
1384 n = (gint)strtol(t, NULL, 16);
1385 if (n > 0 && n < 256)
1391 /* invalid number, rewind and ignore this escape */
1403 if (*w != '\0') *w = '\0';
1407 static void uri_list_parse_encoded_chars(GList *list)
1413 gchar *text = work->data;
1415 uri_text_decode(text);
1421 GList *uri_list_from_text(gchar *data, gint files_only)
1428 while (data[b] != '\0')
1430 while (data[e] != '\r' && data[e] != '\n' && data[e] != '\0') e++;
1431 if (strncmp(data + b, "file:", 5) == 0)
1435 while (data[b] == '/' && data[b+1] == '/') b++;
1436 path = g_strndup(data + b, e - b);
1437 list = g_list_append(list, path_to_utf8(path));
1440 else if (!files_only && strncmp(data + b, "http:", 5) == 0)
1442 list = g_list_append(list, g_strndup(data + b, e - b));
1444 else if (!files_only && strncmp(data + b, "ftp:", 3) == 0)
1446 list = g_list_append(list, g_strndup(data + b, e - b));
1448 while (data[e] == '\r' || data[e] == '\n') e++;
1452 uri_list_parse_encoded_chars(list);
1457 GList *uri_filelist_from_text(gchar *data, gint files_only)
1459 GList *path_list = uri_list_from_text(data, files_only);
1460 GList *filelist = filelist_from_path_list(path_list);
1461 string_list_free(path_list);
1465 gchar *uri_text_from_list(GList *list, gint *len, gint plain_text)
1467 gchar *uri_text = NULL;
1477 string = g_string_new("");
1482 const gchar *name8; /* dnd filenames are in utf-8 */
1490 escaped = uri_text_escape(name8);
1491 g_string_append(string, "file:");
1492 g_string_append(string, escaped);
1495 g_string_append(string, "\r\n");
1499 g_string_append(string, name8);
1500 if (work->next) g_string_append(string, "\n");
1506 uri_text = string->str;
1507 if (len) *len = string->len;
1508 g_string_free(string, FALSE);
1513 gchar *uri_text_from_filelist(GList *list, gint *len, gint plain_text)
1515 GList *path_list = filelist_to_path_list(list);
1516 gchar *ret = uri_text_from_list(path_list, len, plain_text);
1517 string_list_free(path_list);