2 * (SLIK) SimpLIstic sKin functions
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!
23 #include <gdk/gdkkeysyms.h> /* for key values */
28 #include "secure_save.h"
29 #include "ui_bookmark.h"
30 #include "ui_fileops.h"
33 #include "ui_utildlg.h"
34 #include "ui_tabcomp.h"
38 *-----------------------------------------------------------------------------
40 *-----------------------------------------------------------------------------
43 #define HISTORY_DEFAULT_KEY_COUNT 16
46 typedef struct _HistoryData HistoryData;
53 static GList *history_list = NULL;
56 static gchar *quoted_from_text(const gchar *text)
60 gint l = strlen(text);
62 if (l == 0) return NULL;
64 while (c < l && text[c] !='"') c++;
71 while (e < l && text[e] !='"') e++;
76 return g_strndup(ptr, e - c);
83 gint history_list_load(const gchar *path)
90 pathl = path_from_utf8(path);
91 f = fopen(pathl, "r");
95 /* first line must start with History comment */
96 if (!fgets(s_buf,1024,f) ||
97 strncmp(s_buf, "#History", 8) != 0)
103 while (fgets(s_buf,1024,f))
105 if (s_buf[0]=='#') continue;
113 while(ptr[c] != ']' && ptr[c] != '\n' && ptr[c] != '\0') c++;
116 key = g_strndup(ptr, c);
122 value = quoted_from_text(s_buf);
125 history_list_add_to_key(key, value, 0);
138 gint history_list_save(const gchar *path)
144 pathl = path_from_utf8(path);
145 ssi = secure_open(pathl);
151 buf = g_strdup_printf(_("Unable to write history lists to: %s\n"), path);
158 secure_fprintf(ssi, "#History lists\n");
159 secure_fprintf(ssi, "\n");
161 list = g_list_last(history_list);
162 while(list && secsave_errno == SS_ERR_NONE)
170 secure_fprintf(ssi, "[%s]\n", hd->key);
172 /* save them inverted (oldest to newest)
173 * so that when reading they are added correctly
175 work = g_list_last(hd->list);
176 while(work && secsave_errno == SS_ERR_NONE)
178 secure_fprintf(ssi, "\"%s\"\n", (gchar *)work->data);
181 secure_fputc(ssi, '\n');
184 secure_fprintf(ssi, "#end\n");
186 return (secure_close(ssi) == 0);
189 static void history_list_free(HistoryData *hd)
206 static HistoryData *history_list_find_by_key(const gchar* key)
208 GList *work = history_list;
210 if (!key) return NULL;
214 HistoryData *hd = work->data;
215 if (strcmp(hd->key, key) == 0) return hd;
221 const gchar *history_list_find_last_path_by_key(const gchar* key)
225 hd = history_list_find_by_key(key);
226 if (!hd || !hd->list) return NULL;
228 return hd->list->data;
231 void history_list_free_key(const gchar *key)
234 hd = history_list_find_by_key(key);
237 history_list = g_list_remove(history_list, hd);
238 history_list_free(hd);
241 void history_list_add_to_key(const gchar *key, const gchar *path, gint max)
246 if (!key || !path) return;
248 hd = history_list_find_by_key(key);
251 hd = g_new(HistoryData, 1);
252 hd->key = g_strdup(key);
254 history_list = g_list_prepend(history_list, hd);
257 /* if already in the list, simply move it to the top */
261 gchar *buf = work->data;
263 if (strcmp(buf, path) == 0)
265 hd->list = g_list_remove(hd->list, buf);
266 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;
276 while(hd->list && g_list_length(hd->list) > max)
278 GList *work = g_list_last(hd->list);
279 gchar *buf = work->data;
280 hd->list = g_list_remove(hd->list, buf);
286 void history_list_item_change(const gchar *key, const gchar *oldpath, const gchar *newpath)
291 if (!oldpath) return;
292 hd = history_list_find_by_key(key);
298 gchar *buf = work->data;
299 if (strcmp(buf, oldpath) == 0)
303 work->data = g_strdup(newpath);
307 hd->list = g_list_remove(hd->list, buf);
316 void history_list_item_move(const gchar *key, const gchar *path, gint direction)
323 hd = history_list_find_by_key(key);
329 gchar *buf = work->data;
330 if (strcmp(buf, path) == 0)
334 hd->list = g_list_remove(hd->list, buf);
335 hd->list = g_list_insert(hd->list, buf, p);
343 void history_list_item_remove(const gchar *key, const gchar *path)
345 history_list_item_change(key, path, NULL);
348 GList *history_list_get_by_key(const gchar *key)
352 hd = history_list_find_by_key(key);
353 if (!hd) return NULL;
359 *-----------------------------------------------------------------------------
361 *-----------------------------------------------------------------------------
364 #define BOOKMARK_DATA_KEY "bookmarkdata"
365 #define MARKER_PATH "[path]"
366 #define MARKER_ICON "[icon]"
368 typedef struct _BookMarkData BookMarkData;
369 typedef struct _BookButtonData BookButtonData;
370 typedef struct _BookPropData BookPropData;
378 void (*select_func)(const gchar *path, gpointer data);
379 gpointer select_data;
384 BookButtonData *active_button;
387 struct _BookButtonData
402 GtkWidget *name_entry;
403 GtkWidget *path_entry;
404 GtkWidget *icon_entry;
415 static GtkTargetEntry bookmark_drop_types[] = {
416 { "text/uri-list", 0, TARGET_URI_LIST },
417 { "x-url/http", 0, TARGET_X_URL },
418 { "_NETSCAPE_URL", 0, TARGET_X_URL }
420 #define bookmark_drop_types_n 3
422 static GtkTargetEntry bookmark_drag_types[] = {
423 { "text/uri-list", 0, TARGET_URI_LIST },
424 { "text/plain", 0, TARGET_TEXT_PLAIN }
426 #define bookmark_drag_types_n 2
429 static GList *bookmark_widget_list = NULL;
430 static GList *bookmark_default_list = NULL;
433 static void bookmark_populate_all(const gchar *key);
436 static BookButtonData *bookmark_from_string(const gchar *text)
439 const gchar *path_ptr;
440 const gchar *icon_ptr;
442 b = g_new0(BookButtonData, 1);
446 b->name = g_strdup(_("New Bookmark"));
447 b->path = g_strdup(homedir());
452 b->key = g_strdup(text);
454 path_ptr = strstr(text, MARKER_PATH);
455 icon_ptr = strstr(text, MARKER_ICON);
457 if (path_ptr && icon_ptr && icon_ptr < path_ptr)
459 printf("warning, bookmark icon must be after path\n");
468 b->name = g_strndup(text, l);
469 path_ptr += strlen(MARKER_PATH);
472 l = icon_ptr - path_ptr;
473 b->path = g_strndup(path_ptr, l);
477 b->path = g_strdup(path_ptr);
482 b->name = g_strdup(text);
483 b->path = g_strdup("");
488 icon_ptr += strlen(MARKER_ICON);
489 b->icon = g_strdup(icon_ptr);
495 static void bookmark_free(BookButtonData *b)
507 static gchar *bookmark_string(const gchar *name, const gchar *path, const gchar *icon)
509 if (!name) name = _("New Bookmark");
510 if (icon && strncmp(icon, "/", 1) != 0) icon = NULL;
514 return g_strdup_printf("%s"MARKER_PATH"%s"MARKER_ICON"%s", name, path, icon);
517 return g_strdup_printf("%s"MARKER_PATH"%s", name, path);
520 static void bookmark_select_cb(GtkWidget *button, gpointer data)
522 BookMarkData *bm = data;
525 b = g_object_get_data(G_OBJECT(button), "bookbuttondata");
528 if (bm->select_func) bm->select_func(b->path, bm->select_data);
531 static void bookmark_edit_destroy_cb(GtkWidget *widget, gpointer data)
533 BookPropData *p = data;
535 bookmark_free(p->bb);
539 static void bookmark_edit_cancel_cb(GenericDialog *gd, gpointer data)
543 static void bookmark_edit_ok_cb(GenericDialog *gd, gpointer data)
545 BookPropData *p = data;
551 name = gtk_entry_get_text(GTK_ENTRY(p->name_entry));
552 path = remove_trailing_slash(gtk_entry_get_text(GTK_ENTRY(p->path_entry)));
553 icon = gtk_entry_get_text(GTK_ENTRY(p->icon_entry));
555 new = bookmark_string(name, path, icon);
559 history_list_item_change(p->bb->parent, p->bb->key, new);
563 history_list_add_to_key(p->bb->parent, new, 0);
566 if (path && strlen(path) > 0) tab_completion_append_to_history(p->path_entry, path);
567 if (icon && strlen(icon) > 0) tab_completion_append_to_history(p->icon_entry, icon);
572 bookmark_populate_all(p->bb->parent);
575 /* simply pass NULL for text to turn this into a 'new bookmark' dialog */
577 static void bookmark_edit(const gchar *key, const gchar *text, GtkWidget *parent)
585 if (!key) key = "bookmarks";
587 p = g_new0(BookPropData, 1);
589 p->bb = bookmark_from_string(text);
590 p->bb->parent = g_strdup(key);
592 gd = generic_dialog_new(_("Edit Bookmark"), GQ_WMCLASS, "bookmark_edit",
594 bookmark_edit_cancel_cb, p);
595 g_signal_connect(G_OBJECT(gd->dialog), "destroy",
596 G_CALLBACK(bookmark_edit_destroy_cb), p);
598 generic_dialog_add_message(gd, NULL, _("Edit Bookmark"), NULL);
600 generic_dialog_add_button(gd, GTK_STOCK_OK, NULL,
601 bookmark_edit_ok_cb, TRUE);
603 table = pref_table_new(gd->vbox, 3, 2, FALSE, TRUE);
604 pref_table_label(table, 0, 0, _("Name:"), 1.0);
606 p->name_entry = gtk_entry_new();
607 gtk_widget_set_size_request(p->name_entry, 300, -1);
608 if (p->bb->name) gtk_entry_set_text(GTK_ENTRY(p->name_entry), p->bb->name);
609 gtk_table_attach_defaults(GTK_TABLE(table), p->name_entry, 1, 2, 0, 1);
610 generic_dialog_attach_default(gd, p->name_entry);
611 gtk_widget_show(p->name_entry);
613 pref_table_label(table, 0, 1, _("Path:"), 1.0);
615 label = tab_completion_new_with_history(&p->path_entry, p->bb->path,
616 "bookmark_path", -1, NULL, NULL);
617 tab_completion_add_select_button(p->path_entry, NULL, TRUE);
618 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 1, 2);
619 generic_dialog_attach_default(gd, p->path_entry);
620 gtk_widget_show(label);
622 pref_table_label(table, 0, 2, _("Icon:"), 1.0);
625 if (!icon) icon = "";
626 label = tab_completion_new_with_history(&p->icon_entry, icon,
627 "bookmark_icons", -1, NULL, NULL);
628 tab_completion_add_select_button(p->icon_entry, _("Select icon"), FALSE);
629 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 2, 3);
630 generic_dialog_attach_default(gd, p->icon_entry);
631 gtk_widget_show(label);
633 gtk_widget_show(gd->dialog);
636 static void bookmark_move(BookMarkData *bm, GtkWidget *button, gint direction)
643 if (!bm->editable) return;
645 b = g_object_get_data(G_OBJECT(button), "bookbuttondata");
648 list = gtk_container_get_children(GTK_CONTAINER(bm->box));
649 p = g_list_index(list, button);
652 if (p < 0 || p + direction < 0) return;
654 key_holder = bm->key;
655 bm->key = "_TEMPHOLDER";
656 history_list_item_move(key_holder, b->key, -direction);
657 bookmark_populate_all(key_holder);
658 bm->key = key_holder;
660 gtk_box_reorder_child(GTK_BOX(bm->box), button, p + direction);
663 static void bookmark_menu_prop_cb(GtkWidget *widget, gpointer data)
665 BookMarkData *bm = data;
667 if (!bm->active_button) return;
669 bookmark_edit(bm->key, bm->active_button->key, widget);
672 static void bookmark_menu_move(BookMarkData *bm, gint direction)
674 if (!bm->active_button) return;
676 bookmark_move(bm, bm->active_button->button, direction);
679 static void bookmark_menu_up_cb(GtkWidget *widget, gpointer data)
681 bookmark_menu_move(data, -1);
684 static void bookmark_menu_down_cb(GtkWidget *widget, gpointer data)
686 bookmark_menu_move(data, 1);
689 static void bookmark_menu_remove_cb(GtkWidget *widget, gpointer data)
691 BookMarkData *bm = data;
693 if (!bm->active_button) return;
695 history_list_item_remove(bm->key, bm->active_button->key);
696 bookmark_populate_all(bm->key);
699 static void bookmark_menu_position_cb(GtkMenu *menu, gint *x, gint *y, gint *pushed_in, gpointer data)
701 GtkWidget *button = data;
703 gdk_window_get_origin(button->window, x, y);
704 *y += button->allocation.y + button->allocation.height;
707 static void bookmark_menu_popup(BookMarkData *bm, GtkWidget *button,
708 gint button_n, guint32 time, gint local)
713 b = g_object_get_data(G_OBJECT(button), "bookbuttondata");
716 bm->active_button = b;
718 menu = popup_menu_short_lived();
719 menu_item_add_stock_sensitive(menu, _("_Properties..."), GTK_STOCK_PROPERTIES, bm->editable,
720 G_CALLBACK(bookmark_menu_prop_cb), bm);
721 menu_item_add_stock_sensitive(menu, _("Move _up"), GTK_STOCK_GO_UP, bm->editable,
722 G_CALLBACK(bookmark_menu_up_cb), bm);
723 menu_item_add_stock_sensitive(menu, _("Move _down"), GTK_STOCK_GO_DOWN, bm->editable,
724 G_CALLBACK(bookmark_menu_down_cb), bm);
725 menu_item_add_stock_sensitive(menu, _("_Remove"), GTK_STOCK_REMOVE, bm->editable,
726 G_CALLBACK(bookmark_menu_remove_cb), bm);
730 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
731 bookmark_menu_position_cb, button, button_n, time);
735 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, button_n, time);
739 static gint bookmark_press_cb(GtkWidget *button, GdkEventButton *event, gpointer data)
741 BookMarkData *bm = data;
743 if (event->button != 3) return FALSE;
745 bookmark_menu_popup(bm, button, event->button, event->time, FALSE);
750 static gint bookmark_keypress_cb(GtkWidget *button, GdkEventKey *event, gpointer data)
752 BookMarkData *bm = data;
754 switch (event->keyval)
757 if (!(event->state & GDK_CONTROL_MASK)) return FALSE;
759 bookmark_menu_popup(bm, button, 0, event->time, TRUE);
763 if (event->state & GDK_SHIFT_MASK)
765 bookmark_move(bm, button, -1);
770 if (event->state & GDK_SHIFT_MASK)
772 bookmark_move(bm, button, 1);
781 static void bookmark_drag_set_data(GtkWidget *button,
782 GdkDragContext *context, GtkSelectionData *selection_data,
783 guint info, guint time, gpointer data)
785 BookMarkData *bm = data;
787 gchar *uri_text = NULL;
791 if (context->dest_window == bm->widget->window) return;
793 b = g_object_get_data(G_OBJECT(button), "bookbuttondata");
796 list = g_list_append(list, b->path);
800 case TARGET_URI_LIST:
801 uri_text = uri_text_from_list(list, &length, FALSE);
803 case TARGET_TEXT_PLAIN:
804 uri_text = uri_text_from_list(list, &length, TRUE);
810 if (!uri_text) return;
812 gtk_selection_data_set(selection_data, selection_data->target,
813 8, (guchar *)uri_text, length);
817 static void bookmark_drag_begin(GtkWidget *button, GdkDragContext *context, gpointer data)
820 GdkModifierType mask;
823 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8,
824 button->allocation.width, button->allocation.height);
825 gdk_pixbuf_get_from_drawable(pixbuf, button->window, NULL,
826 button->allocation.x, button->allocation.y,
827 0, 0, button->allocation.width, button->allocation.height);
829 gdk_window_get_pointer(button->window, &x, &y, &mask);
831 gtk_drag_set_icon_pixbuf(context, pixbuf,
832 x - button->allocation.x, y - button->allocation.y);
833 g_object_unref(pixbuf);
836 static void bookmark_populate(BookMarkData *bm)
842 box = GTK_BOX(bm->box);
843 children = gtk_container_get_children(GTK_CONTAINER(box));
847 GtkWidget *widget = GTK_WIDGET(work->data);
849 gtk_widget_destroy(widget);
852 if (!bm->no_defaults && !history_list_get_by_key(bm->key))
857 if (!bookmark_default_list)
859 buf = bookmark_string(_("Home"), homedir(), NULL);
860 history_list_add_to_key(bm->key, buf, 0);
863 path = concat_dir_and_file(homedir(), "Desktop");
866 buf = bookmark_string(_("Desktop"), path, NULL);
867 history_list_add_to_key(bm->key, buf, 0);
873 work = bookmark_default_list;
874 while (work && work->next)
883 buf = bookmark_string(name, path, NULL);
884 history_list_add_to_key(bm->key, buf, 0);
889 work = history_list_get_by_key(bm->key);
890 work = g_list_last(work);
895 b = bookmark_from_string(work->data);
900 b->button = gtk_button_new();
901 gtk_button_set_relief(GTK_BUTTON(b->button), GTK_RELIEF_NONE);
902 gtk_box_pack_start(GTK_BOX(bm->box), b->button, FALSE, FALSE, 0);
903 gtk_widget_show(b->button);
905 g_object_set_data_full(G_OBJECT(b->button), "bookbuttondata",
906 b, (GDestroyNotify)bookmark_free);
908 box = gtk_hbox_new(FALSE, PREF_PAD_BUTTON_GAP);
909 gtk_container_add(GTK_CONTAINER(b->button), box);
910 gtk_widget_show(box);
917 iconl = path_from_utf8(b->icon);
918 pixbuf = gdk_pixbuf_new_from_file(iconl, NULL);
926 gtk_icon_size_lookup(GTK_ICON_SIZE_BUTTON, &w, &h);
928 scaled = gdk_pixbuf_scale_simple(pixbuf, w, h,
929 GDK_INTERP_BILINEAR);
930 b->image = gtk_image_new_from_pixbuf(scaled);
931 g_object_unref(scaled);
932 g_object_unref(pixbuf);
936 b->image = gtk_image_new_from_stock(GTK_STOCK_MISSING_IMAGE,
937 GTK_ICON_SIZE_BUTTON);
942 b->image = gtk_image_new_from_stock(GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_BUTTON);
944 gtk_box_pack_start(GTK_BOX(box), b->image, FALSE, FALSE, 0);
945 gtk_widget_show(b->image);
947 b->label = gtk_label_new(b->name);
948 gtk_box_pack_start(GTK_BOX(box), b->label, FALSE, FALSE, 0);
949 gtk_widget_show(b->label);
951 g_signal_connect(G_OBJECT(b->button), "clicked",
952 G_CALLBACK(bookmark_select_cb), bm);
953 g_signal_connect(G_OBJECT(b->button), "button_press_event",
954 G_CALLBACK(bookmark_press_cb), bm);
955 g_signal_connect(G_OBJECT(b->button), "key_press_event",
956 G_CALLBACK(bookmark_keypress_cb), bm);
958 gtk_drag_source_set(b->button, GDK_BUTTON1_MASK,
959 bookmark_drag_types, bookmark_drag_types_n,
960 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK);
961 g_signal_connect(G_OBJECT(b->button), "drag_data_get",
962 G_CALLBACK(bookmark_drag_set_data), bm);
963 g_signal_connect(G_OBJECT(b->button), "drag_begin",
964 G_CALLBACK(bookmark_drag_begin), bm);
971 static void bookmark_populate_all(const gchar *key)
977 work = bookmark_widget_list;
985 if (strcmp(bm->key, key) == 0)
987 bookmark_populate(bm);
992 static void bookmark_dnd_get_data(GtkWidget *widget,
993 GdkDragContext *context, gint x, gint y,
994 GtkSelectionData *selection_data, guint info,
995 guint time, gpointer data)
997 BookMarkData *bm = data;
1001 if (!bm->editable) return;
1005 case TARGET_URI_LIST:
1007 list = uri_list_from_text((gchar *)selection_data->data, FALSE);
1014 gchar *path = work->data;
1017 buf = bookmark_string(filename_from_path(path), path, NULL);
1018 history_list_add_to_key(bm->key, buf, 0);
1024 string_list_free(list);
1026 bookmark_populate_all(bm->key);
1029 static void bookmark_list_destroy(GtkWidget *widget, gpointer data)
1031 BookMarkData *bm = data;
1033 bookmark_widget_list = g_list_remove(bookmark_widget_list, bm);
1039 GtkWidget *bookmark_list_new(const gchar *key,
1040 void (*select_func)(const gchar *path, gpointer data), gpointer select_data)
1042 GtkWidget *scrolled;
1045 if (!key) key = "bookmarks";
1047 bm = g_new0(BookMarkData, 1);
1048 bm->key = g_strdup(key);
1050 bm->select_func = select_func;
1051 bm->select_data = select_data;
1053 bm->no_defaults = FALSE;
1054 bm->editable = TRUE;
1056 scrolled = gtk_scrolled_window_new(NULL, NULL);
1057 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
1058 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1060 bm->box = gtk_vbox_new(FALSE, 0);
1061 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled), bm->box);
1062 gtk_widget_show(bm->box);
1064 bookmark_populate(bm);
1066 g_signal_connect(G_OBJECT(bm->box), "destroy",
1067 G_CALLBACK(bookmark_list_destroy), bm);
1068 g_object_set_data(G_OBJECT(bm->box), BOOKMARK_DATA_KEY, bm);
1069 g_object_set_data(G_OBJECT(scrolled), BOOKMARK_DATA_KEY, bm);
1070 bm->widget = scrolled;
1072 gtk_drag_dest_set(scrolled,
1073 GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP,
1074 bookmark_drop_types, bookmark_drop_types_n,
1075 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK);
1076 g_signal_connect(G_OBJECT(scrolled), "drag_data_received",
1077 G_CALLBACK(bookmark_dnd_get_data), bm);
1079 bookmark_widget_list = g_list_append(bookmark_widget_list, bm);
1084 void bookmark_list_set_key(GtkWidget *list, const gchar *key)
1088 if (!list || !key) return;
1090 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
1093 if (bm->key && strcmp(bm->key, key) == 0) return;
1096 bm->key = g_strdup(key);
1098 bookmark_populate(bm);
1101 void bookmark_list_set_no_defaults(GtkWidget *list, gint no_defaults)
1105 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
1108 bm->no_defaults = no_defaults;
1111 void bookmark_list_set_editable(GtkWidget *list, gint editable)
1115 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
1118 bm->editable = editable;
1121 void bookmark_list_add(GtkWidget *list, const gchar *name, const gchar *path)
1126 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
1129 buf = bookmark_string(name, path, NULL);
1130 history_list_add_to_key(bm->key, buf, 0);
1133 bookmark_populate_all(bm->key);
1136 void bookmark_add_default(const gchar *name, const gchar *path)
1138 if (!name || !path) return;
1139 bookmark_default_list = g_list_append(bookmark_default_list, g_strdup(name));
1140 bookmark_default_list = g_list_append(bookmark_default_list, g_strdup(path));
1144 *-----------------------------------------------------------------------------
1145 * combo with history key
1146 *-----------------------------------------------------------------------------
1149 typedef struct _HistoryComboData HistoryComboData;
1150 struct _HistoryComboData
1155 gint history_levels;
1158 static void history_combo_destroy(GtkWidget *widget, gpointer data)
1160 HistoryComboData *hc = data;
1162 g_free(hc->history_key);
1166 /* if text is NULL, entry is set to the most recent item */
1167 GtkWidget *history_combo_new(GtkWidget **entry, const gchar *text,
1168 const gchar *history_key, gint max_levels)
1170 HistoryComboData *hc;
1174 hc = g_new0(HistoryComboData, 1);
1175 hc->history_key = g_strdup(history_key);
1176 hc->history_levels = max_levels;
1178 hc->combo = gtk_combo_box_entry_new_text();
1180 gtk_combo_set_case_sensitive(GTK_COMBO(hc->combo), TRUE);
1181 gtk_combo_set_use_arrows(GTK_COMBO(hc->combo), FALSE);
1184 hc->entry = GTK_BIN(hc->combo)->child;
1186 g_object_set_data(G_OBJECT(hc->combo), "history_combo_data", hc);
1187 g_object_set_data(G_OBJECT(hc->entry), "history_combo_data", hc);
1188 g_signal_connect(G_OBJECT(hc->combo), "destroy",
1189 G_CALLBACK(history_combo_destroy), hc);
1191 work = history_list_get_by_key(hc->history_key);
1194 gtk_combo_box_append_text(GTK_COMBO_BOX(hc->combo), (gchar *)work->data);
1201 gtk_entry_set_text(GTK_ENTRY(hc->entry), text);
1205 gtk_combo_box_set_active(GTK_COMBO_BOX(hc->combo), 0);
1208 if (entry) *entry = hc->entry;
1212 /* if text is NULL, current entry text is used
1213 * widget can be the combo or entry widget
1215 void history_combo_append_history(GtkWidget *widget, const gchar *text)
1217 HistoryComboData *hc;
1220 hc = g_object_get_data(G_OBJECT(widget), "history_combo_data");
1223 printf("widget is not a history combo\n");
1229 new_text = g_strdup(text);
1233 new_text = g_strdup(gtk_entry_get_text(GTK_ENTRY(hc->entry)));
1236 if (new_text && strlen(new_text) > 0)
1238 GtkTreeModel *store;
1241 history_list_add_to_key(hc->history_key, new_text, hc->history_levels);
1243 gtk_combo_box_set_active(GTK_COMBO_BOX(hc->combo), -1);
1245 store = gtk_combo_box_get_model(GTK_COMBO_BOX(hc->combo));
1246 gtk_list_store_clear(GTK_LIST_STORE(store));
1248 work = history_list_get_by_key(hc->history_key);
1251 gtk_combo_box_append_text(GTK_COMBO_BOX(hc->combo), (gchar *)work->data);
1260 *-----------------------------------------------------------------------------
1261 * drag and drop uri utils
1262 *-----------------------------------------------------------------------------
1265 /* the following characters are allowed to be unencoded for pathnames:
1268 static gint escape_char_list[] = {
1269 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0 */
1270 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 */
1271 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 20 */
1272 /* spc ! " # $ % & ' */
1273 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, /* 30 */
1274 /* ( ) * + , - . / 0 1 */
1275 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 */
1276 /* 2 3 4 5 6 7 8 9 : ; */
1277 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, /* 50 */
1278 /* < = > ? @ A B C D E */
1279 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, /* 60 */
1280 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 70 */
1281 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 */
1282 /* Z [ \ ] ^ _ ` a b c */
1283 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, /* 90 */
1284 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 100 */
1285 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 110 */
1286 /* x y z { | } ~ del */
1287 0, 0, 0, 1, 1, 1, 0, 0 /* 120, 127 is end */
1290 static gchar *hex_char = "0123456789ABCDEF";
1292 static gint escape_test(guchar c)
1294 if (c < 32 || c > 127) return TRUE;
1295 return (escape_char_list[c] != 0);
1298 static const gchar *escape_code(guchar c)
1300 static gchar text[4];
1303 text[1] = hex_char[c>>4];
1304 text[2] = hex_char[c%16];
1310 gchar *uri_text_escape(const gchar *text)
1316 if (!text) return NULL;
1318 string = g_string_new("");
1323 if (escape_test(*p))
1325 g_string_append(string, escape_code(*p));
1329 g_string_append_c(string, *p);
1334 result = string->str;
1335 g_string_free(string, FALSE);
1337 /* dropped filenames are expected to be utf-8 compatible */
1338 if (!g_utf8_validate(result, -1, NULL))
1342 tmp = g_locale_to_utf8(result, -1, NULL, NULL, NULL);
1353 /* this operates on the passed string, decoding escaped characters */
1354 void uri_text_decode(gchar *text)
1356 if (strchr(text, '%'))
1365 if (*r == '%' && *(r + 1) != '\0' && *(r + 2) != '\0')
1375 n = (gint)strtol(t, NULL, 16);
1376 if (n > 0 && n < 256)
1382 /* invalid number, rewind and ignore this escape */
1394 if (*w != '\0') *w = '\0';
1398 static void uri_list_parse_encoded_chars(GList *list)
1404 gchar *text = work->data;
1406 uri_text_decode(text);
1412 GList *uri_list_from_text(gchar *data, gint files_only)
1419 while (data[b] != '\0')
1421 while (data[e] != '\r' && data[e] != '\n' && data[e] != '\0') e++;
1422 if (strncmp(data + b, "file:", 5) == 0)
1426 while (data[b] == '/' && data[b+1] == '/') b++;
1427 path = g_strndup(data + b, e - b);
1428 list = g_list_append(list, path_to_utf8(path));
1431 else if (!files_only && strncmp(data + b, "http:", 5) == 0)
1433 list = g_list_append(list, g_strndup(data + b, e - b));
1435 else if (!files_only && strncmp(data + b, "ftp:", 3) == 0)
1437 list = g_list_append(list, g_strndup(data + b, e - b));
1439 while (data[e] == '\r' || data[e] == '\n') e++;
1443 uri_list_parse_encoded_chars(list);
1448 GList *uri_filelist_from_text(gchar *data, gint files_only)
1450 GList *path_list = uri_list_from_text(data, files_only);
1451 GList *filelist = filelist_from_path_list(path_list);
1452 string_list_free(path_list);
1456 gchar *uri_text_from_list(GList *list, gint *len, gint plain_text)
1458 gchar *uri_text = NULL;
1468 string = g_string_new("");
1473 const gchar *name8; /* dnd filenames are in utf-8 */
1481 escaped = uri_text_escape(name8);
1482 g_string_append(string, "file:");
1483 g_string_append(string, escaped);
1486 g_string_append(string, "\r\n");
1490 g_string_append(string, name8);
1491 if (work->next) g_string_append(string, "\n");
1497 uri_text = string->str;
1498 if (len) *len = string->len;
1499 g_string_free(string, FALSE);
1504 gchar *uri_text_from_filelist(GList *list, gint *len, gint plain_text)
1506 GList *path_list = filelist_to_path_list(list);
1507 gchar *ret = uri_text_from_list(path_list, len, plain_text);
1508 string_list_free(path_list);