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);
149 printf_term(_("Unable to write history lists to: %s\n"), path);
153 secure_fprintf(ssi, "#History lists\n");
154 secure_fprintf(ssi, "\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 hd->list = g_list_remove(hd->list, buf);
261 hd->list = g_list_prepend(hd->list, buf);
266 hd->list = g_list_prepend(hd->list, g_strdup(path));
268 if (max == -1) max = HISTORY_DEFAULT_KEY_COUNT;
271 while(hd->list && g_list_length(hd->list) > max)
273 GList *work = g_list_last(hd->list);
274 gchar *buf = work->data;
275 hd->list = g_list_remove(hd->list, buf);
281 void history_list_item_change(const gchar *key, const gchar *oldpath, const gchar *newpath)
286 if (!oldpath) return;
287 hd = history_list_find_by_key(key);
293 gchar *buf = work->data;
294 if (strcmp(buf, oldpath) == 0)
298 work->data = g_strdup(newpath);
302 hd->list = g_list_remove(hd->list, buf);
311 void history_list_item_move(const gchar *key, const gchar *path, gint direction)
318 hd = history_list_find_by_key(key);
324 gchar *buf = work->data;
325 if (strcmp(buf, path) == 0)
329 hd->list = g_list_remove(hd->list, buf);
330 hd->list = g_list_insert(hd->list, buf, p);
338 void history_list_item_remove(const gchar *key, const gchar *path)
340 history_list_item_change(key, path, NULL);
343 GList *history_list_get_by_key(const gchar *key)
347 hd = history_list_find_by_key(key);
348 if (!hd) return NULL;
354 *-----------------------------------------------------------------------------
356 *-----------------------------------------------------------------------------
359 #define BOOKMARK_DATA_KEY "bookmarkdata"
360 #define MARKER_PATH "[path]"
361 #define MARKER_ICON "[icon]"
363 typedef struct _BookMarkData BookMarkData;
364 typedef struct _BookButtonData BookButtonData;
365 typedef struct _BookPropData BookPropData;
373 void (*select_func)(const gchar *path, gpointer data);
374 gpointer select_data;
379 BookButtonData *active_button;
382 struct _BookButtonData
397 GtkWidget *name_entry;
398 GtkWidget *path_entry;
399 GtkWidget *icon_entry;
410 static GtkTargetEntry bookmark_drop_types[] = {
411 { "text/uri-list", 0, TARGET_URI_LIST },
412 { "x-url/http", 0, TARGET_X_URL },
413 { "_NETSCAPE_URL", 0, TARGET_X_URL }
415 #define bookmark_drop_types_n 3
417 static GtkTargetEntry bookmark_drag_types[] = {
418 { "text/uri-list", 0, TARGET_URI_LIST },
419 { "text/plain", 0, TARGET_TEXT_PLAIN }
421 #define bookmark_drag_types_n 2
424 static GList *bookmark_widget_list = NULL;
425 static GList *bookmark_default_list = NULL;
428 static void bookmark_populate_all(const gchar *key);
431 static BookButtonData *bookmark_from_string(const gchar *text)
434 const gchar *path_ptr;
435 const gchar *icon_ptr;
437 b = g_new0(BookButtonData, 1);
441 b->name = g_strdup(_("New Bookmark"));
442 b->path = g_strdup(homedir());
447 b->key = g_strdup(text);
449 path_ptr = strstr(text, MARKER_PATH);
450 icon_ptr = strstr(text, MARKER_ICON);
452 if (path_ptr && icon_ptr && icon_ptr < path_ptr)
454 printf("warning, bookmark icon must be after path\n");
463 b->name = g_strndup(text, l);
464 path_ptr += strlen(MARKER_PATH);
467 l = icon_ptr - path_ptr;
468 b->path = g_strndup(path_ptr, l);
472 b->path = g_strdup(path_ptr);
477 b->name = g_strdup(text);
478 b->path = g_strdup("");
483 icon_ptr += strlen(MARKER_ICON);
484 b->icon = g_strdup(icon_ptr);
490 static void bookmark_free(BookButtonData *b)
502 static gchar *bookmark_string(const gchar *name, const gchar *path, const gchar *icon)
504 if (!name) name = _("New Bookmark");
505 if (icon && strncmp(icon, "/", 1) != 0) icon = NULL;
509 return g_strdup_printf("%s"MARKER_PATH"%s"MARKER_ICON"%s", name, path, icon);
512 return g_strdup_printf("%s"MARKER_PATH"%s", name, path);
515 static void bookmark_select_cb(GtkWidget *button, gpointer data)
517 BookMarkData *bm = data;
520 b = g_object_get_data(G_OBJECT(button), "bookbuttondata");
523 if (bm->select_func) bm->select_func(b->path, bm->select_data);
526 static void bookmark_edit_destroy_cb(GtkWidget *widget, gpointer data)
528 BookPropData *p = data;
530 bookmark_free(p->bb);
534 static void bookmark_edit_cancel_cb(GenericDialog *gd, gpointer data)
538 static void bookmark_edit_ok_cb(GenericDialog *gd, gpointer data)
540 BookPropData *p = data;
546 name = gtk_entry_get_text(GTK_ENTRY(p->name_entry));
547 path = remove_trailing_slash(gtk_entry_get_text(GTK_ENTRY(p->path_entry)));
548 icon = gtk_entry_get_text(GTK_ENTRY(p->icon_entry));
550 new = bookmark_string(name, path, icon);
554 history_list_item_change(p->bb->parent, p->bb->key, new);
558 history_list_add_to_key(p->bb->parent, new, 0);
561 if (path && strlen(path) > 0) tab_completion_append_to_history(p->path_entry, path);
562 if (icon && strlen(icon) > 0) tab_completion_append_to_history(p->icon_entry, icon);
567 bookmark_populate_all(p->bb->parent);
570 /* simply pass NULL for text to turn this into a 'new bookmark' dialog */
572 static void bookmark_edit(const gchar *key, const gchar *text, GtkWidget *parent)
580 if (!key) key = "bookmarks";
582 p = g_new0(BookPropData, 1);
584 p->bb = bookmark_from_string(text);
585 p->bb->parent = g_strdup(key);
587 gd = generic_dialog_new(_("Edit Bookmark"), GQ_WMCLASS, "bookmark_edit",
589 bookmark_edit_cancel_cb, p);
590 g_signal_connect(G_OBJECT(gd->dialog), "destroy",
591 G_CALLBACK(bookmark_edit_destroy_cb), p);
593 generic_dialog_add_message(gd, NULL, _("Edit Bookmark"), NULL);
595 generic_dialog_add_button(gd, GTK_STOCK_OK, NULL,
596 bookmark_edit_ok_cb, TRUE);
598 table = pref_table_new(gd->vbox, 3, 2, FALSE, TRUE);
599 pref_table_label(table, 0, 0, _("Name:"), 1.0);
601 p->name_entry = gtk_entry_new();
602 gtk_widget_set_size_request(p->name_entry, 300, -1);
603 if (p->bb->name) gtk_entry_set_text(GTK_ENTRY(p->name_entry), p->bb->name);
604 gtk_table_attach_defaults(GTK_TABLE(table), p->name_entry, 1, 2, 0, 1);
605 generic_dialog_attach_default(gd, p->name_entry);
606 gtk_widget_show(p->name_entry);
608 pref_table_label(table, 0, 1, _("Path:"), 1.0);
610 label = tab_completion_new_with_history(&p->path_entry, p->bb->path,
611 "bookmark_path", -1, NULL, NULL);
612 tab_completion_add_select_button(p->path_entry, NULL, TRUE);
613 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 1, 2);
614 generic_dialog_attach_default(gd, p->path_entry);
615 gtk_widget_show(label);
617 pref_table_label(table, 0, 2, _("Icon:"), 1.0);
620 if (!icon) icon = "";
621 label = tab_completion_new_with_history(&p->icon_entry, icon,
622 "bookmark_icons", -1, NULL, NULL);
623 tab_completion_add_select_button(p->icon_entry, _("Select icon"), FALSE);
624 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 2, 3);
625 generic_dialog_attach_default(gd, p->icon_entry);
626 gtk_widget_show(label);
628 gtk_widget_show(gd->dialog);
631 static void bookmark_move(BookMarkData *bm, GtkWidget *button, gint direction)
638 if (!bm->editable) return;
640 b = g_object_get_data(G_OBJECT(button), "bookbuttondata");
643 list = gtk_container_get_children(GTK_CONTAINER(bm->box));
644 p = g_list_index(list, button);
647 if (p < 0 || p + direction < 0) return;
649 key_holder = bm->key;
650 bm->key = "_TEMPHOLDER";
651 history_list_item_move(key_holder, b->key, -direction);
652 bookmark_populate_all(key_holder);
653 bm->key = key_holder;
655 gtk_box_reorder_child(GTK_BOX(bm->box), button, p + direction);
658 static void bookmark_menu_prop_cb(GtkWidget *widget, gpointer data)
660 BookMarkData *bm = data;
662 if (!bm->active_button) return;
664 bookmark_edit(bm->key, bm->active_button->key, widget);
667 static void bookmark_menu_move(BookMarkData *bm, gint direction)
669 if (!bm->active_button) return;
671 bookmark_move(bm, bm->active_button->button, direction);
674 static void bookmark_menu_up_cb(GtkWidget *widget, gpointer data)
676 bookmark_menu_move(data, -1);
679 static void bookmark_menu_down_cb(GtkWidget *widget, gpointer data)
681 bookmark_menu_move(data, 1);
684 static void bookmark_menu_remove_cb(GtkWidget *widget, gpointer data)
686 BookMarkData *bm = data;
688 if (!bm->active_button) return;
690 history_list_item_remove(bm->key, bm->active_button->key);
691 bookmark_populate_all(bm->key);
694 static void bookmark_menu_position_cb(GtkMenu *menu, gint *x, gint *y, gint *pushed_in, gpointer data)
696 GtkWidget *button = data;
698 gdk_window_get_origin(button->window, x, y);
699 *y += button->allocation.y + button->allocation.height;
702 static void bookmark_menu_popup(BookMarkData *bm, GtkWidget *button,
703 gint button_n, guint32 time, gint local)
708 b = g_object_get_data(G_OBJECT(button), "bookbuttondata");
711 bm->active_button = b;
713 menu = popup_menu_short_lived();
714 menu_item_add_stock_sensitive(menu, _("_Properties..."), GTK_STOCK_PROPERTIES, bm->editable,
715 G_CALLBACK(bookmark_menu_prop_cb), bm);
716 menu_item_add_stock_sensitive(menu, _("Move _up"), GTK_STOCK_GO_UP, bm->editable,
717 G_CALLBACK(bookmark_menu_up_cb), bm);
718 menu_item_add_stock_sensitive(menu, _("Move _down"), GTK_STOCK_GO_DOWN, bm->editable,
719 G_CALLBACK(bookmark_menu_down_cb), bm);
720 menu_item_add_stock_sensitive(menu, _("_Remove"), GTK_STOCK_REMOVE, bm->editable,
721 G_CALLBACK(bookmark_menu_remove_cb), bm);
725 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
726 bookmark_menu_position_cb, button, button_n, time);
730 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, button_n, time);
734 static gint bookmark_press_cb(GtkWidget *button, GdkEventButton *event, gpointer data)
736 BookMarkData *bm = data;
738 if (event->button != 3) return FALSE;
740 bookmark_menu_popup(bm, button, event->button, event->time, FALSE);
745 static gint bookmark_keypress_cb(GtkWidget *button, GdkEventKey *event, gpointer data)
747 BookMarkData *bm = data;
749 switch (event->keyval)
752 if (!(event->state & GDK_CONTROL_MASK)) return FALSE;
754 bookmark_menu_popup(bm, button, 0, event->time, TRUE);
758 if (event->state & GDK_SHIFT_MASK)
760 bookmark_move(bm, button, -1);
765 if (event->state & GDK_SHIFT_MASK)
767 bookmark_move(bm, button, 1);
776 static void bookmark_drag_set_data(GtkWidget *button,
777 GdkDragContext *context, GtkSelectionData *selection_data,
778 guint info, guint time, gpointer data)
780 BookMarkData *bm = data;
782 gchar *uri_text = NULL;
786 if (context->dest_window == bm->widget->window) return;
788 b = g_object_get_data(G_OBJECT(button), "bookbuttondata");
791 list = g_list_append(list, b->path);
795 case TARGET_URI_LIST:
796 uri_text = uri_text_from_list(list, &length, FALSE);
798 case TARGET_TEXT_PLAIN:
799 uri_text = uri_text_from_list(list, &length, TRUE);
805 if (!uri_text) return;
807 gtk_selection_data_set(selection_data, selection_data->target,
808 8, (guchar *)uri_text, length);
812 static void bookmark_drag_begin(GtkWidget *button, GdkDragContext *context, gpointer data)
815 GdkModifierType mask;
818 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8,
819 button->allocation.width, button->allocation.height);
820 gdk_pixbuf_get_from_drawable(pixbuf, button->window, NULL,
821 button->allocation.x, button->allocation.y,
822 0, 0, button->allocation.width, button->allocation.height);
824 gdk_window_get_pointer(button->window, &x, &y, &mask);
826 gtk_drag_set_icon_pixbuf(context, pixbuf,
827 x - button->allocation.x, y - button->allocation.y);
828 g_object_unref(pixbuf);
831 static void bookmark_populate(BookMarkData *bm)
837 box = GTK_BOX(bm->box);
838 children = gtk_container_get_children(GTK_CONTAINER(box));
842 GtkWidget *widget = GTK_WIDGET(work->data);
844 gtk_widget_destroy(widget);
847 if (!bm->no_defaults && !history_list_get_by_key(bm->key))
852 if (!bookmark_default_list)
854 buf = bookmark_string(_("Home"), homedir(), NULL);
855 history_list_add_to_key(bm->key, buf, 0);
858 path = concat_dir_and_file(homedir(), "Desktop");
861 buf = bookmark_string(_("Desktop"), path, NULL);
862 history_list_add_to_key(bm->key, buf, 0);
868 work = bookmark_default_list;
869 while (work && work->next)
878 buf = bookmark_string(name, path, NULL);
879 history_list_add_to_key(bm->key, buf, 0);
884 work = history_list_get_by_key(bm->key);
885 work = g_list_last(work);
890 b = bookmark_from_string(work->data);
895 b->button = gtk_button_new();
896 gtk_button_set_relief(GTK_BUTTON(b->button), GTK_RELIEF_NONE);
897 gtk_box_pack_start(GTK_BOX(bm->box), b->button, FALSE, FALSE, 0);
898 gtk_widget_show(b->button);
900 g_object_set_data_full(G_OBJECT(b->button), "bookbuttondata",
901 b, (GDestroyNotify)bookmark_free);
903 box = gtk_hbox_new(FALSE, PREF_PAD_BUTTON_GAP);
904 gtk_container_add(GTK_CONTAINER(b->button), box);
905 gtk_widget_show(box);
912 iconl = path_from_utf8(b->icon);
913 pixbuf = gdk_pixbuf_new_from_file(iconl, NULL);
921 gtk_icon_size_lookup(GTK_ICON_SIZE_BUTTON, &w, &h);
923 scaled = gdk_pixbuf_scale_simple(pixbuf, w, h,
924 GDK_INTERP_BILINEAR);
925 b->image = gtk_image_new_from_pixbuf(scaled);
926 g_object_unref(scaled);
927 g_object_unref(pixbuf);
931 b->image = gtk_image_new_from_stock(GTK_STOCK_MISSING_IMAGE,
932 GTK_ICON_SIZE_BUTTON);
937 b->image = gtk_image_new_from_stock(GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_BUTTON);
939 gtk_box_pack_start(GTK_BOX(box), b->image, FALSE, FALSE, 0);
940 gtk_widget_show(b->image);
942 b->label = gtk_label_new(b->name);
943 gtk_box_pack_start(GTK_BOX(box), b->label, FALSE, FALSE, 0);
944 gtk_widget_show(b->label);
946 g_signal_connect(G_OBJECT(b->button), "clicked",
947 G_CALLBACK(bookmark_select_cb), bm);
948 g_signal_connect(G_OBJECT(b->button), "button_press_event",
949 G_CALLBACK(bookmark_press_cb), bm);
950 g_signal_connect(G_OBJECT(b->button), "key_press_event",
951 G_CALLBACK(bookmark_keypress_cb), bm);
953 gtk_drag_source_set(b->button, GDK_BUTTON1_MASK,
954 bookmark_drag_types, bookmark_drag_types_n,
955 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK);
956 g_signal_connect(G_OBJECT(b->button), "drag_data_get",
957 G_CALLBACK(bookmark_drag_set_data), bm);
958 g_signal_connect(G_OBJECT(b->button), "drag_begin",
959 G_CALLBACK(bookmark_drag_begin), bm);
966 static void bookmark_populate_all(const gchar *key)
972 work = bookmark_widget_list;
980 if (strcmp(bm->key, key) == 0)
982 bookmark_populate(bm);
987 static void bookmark_dnd_get_data(GtkWidget *widget,
988 GdkDragContext *context, gint x, gint y,
989 GtkSelectionData *selection_data, guint info,
990 guint time, gpointer data)
992 BookMarkData *bm = data;
996 if (!bm->editable) return;
1000 case TARGET_URI_LIST:
1002 list = uri_list_from_text((gchar *)selection_data->data, FALSE);
1009 gchar *path = work->data;
1012 buf = bookmark_string(filename_from_path(path), path, NULL);
1013 history_list_add_to_key(bm->key, buf, 0);
1019 string_list_free(list);
1021 bookmark_populate_all(bm->key);
1024 static void bookmark_list_destroy(GtkWidget *widget, gpointer data)
1026 BookMarkData *bm = data;
1028 bookmark_widget_list = g_list_remove(bookmark_widget_list, bm);
1034 GtkWidget *bookmark_list_new(const gchar *key,
1035 void (*select_func)(const gchar *path, gpointer data), gpointer select_data)
1037 GtkWidget *scrolled;
1040 if (!key) key = "bookmarks";
1042 bm = g_new0(BookMarkData, 1);
1043 bm->key = g_strdup(key);
1045 bm->select_func = select_func;
1046 bm->select_data = select_data;
1048 bm->no_defaults = FALSE;
1049 bm->editable = TRUE;
1051 scrolled = gtk_scrolled_window_new(NULL, NULL);
1052 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
1053 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1055 bm->box = gtk_vbox_new(FALSE, 0);
1056 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled), bm->box);
1057 gtk_widget_show(bm->box);
1059 bookmark_populate(bm);
1061 g_signal_connect(G_OBJECT(bm->box), "destroy",
1062 G_CALLBACK(bookmark_list_destroy), bm);
1063 g_object_set_data(G_OBJECT(bm->box), BOOKMARK_DATA_KEY, bm);
1064 g_object_set_data(G_OBJECT(scrolled), BOOKMARK_DATA_KEY, bm);
1065 bm->widget = scrolled;
1067 gtk_drag_dest_set(scrolled,
1068 GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP,
1069 bookmark_drop_types, bookmark_drop_types_n,
1070 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK);
1071 g_signal_connect(G_OBJECT(scrolled), "drag_data_received",
1072 G_CALLBACK(bookmark_dnd_get_data), bm);
1074 bookmark_widget_list = g_list_append(bookmark_widget_list, bm);
1079 void bookmark_list_set_key(GtkWidget *list, const gchar *key)
1083 if (!list || !key) return;
1085 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
1088 if (bm->key && strcmp(bm->key, key) == 0) return;
1091 bm->key = g_strdup(key);
1093 bookmark_populate(bm);
1096 void bookmark_list_set_no_defaults(GtkWidget *list, gint no_defaults)
1100 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
1103 bm->no_defaults = no_defaults;
1106 void bookmark_list_set_editable(GtkWidget *list, gint editable)
1110 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
1113 bm->editable = editable;
1116 void bookmark_list_add(GtkWidget *list, const gchar *name, const gchar *path)
1121 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
1124 buf = bookmark_string(name, path, NULL);
1125 history_list_add_to_key(bm->key, buf, 0);
1128 bookmark_populate_all(bm->key);
1131 void bookmark_add_default(const gchar *name, const gchar *path)
1133 if (!name || !path) return;
1134 bookmark_default_list = g_list_append(bookmark_default_list, g_strdup(name));
1135 bookmark_default_list = g_list_append(bookmark_default_list, g_strdup(path));
1139 *-----------------------------------------------------------------------------
1140 * combo with history key
1141 *-----------------------------------------------------------------------------
1144 typedef struct _HistoryComboData HistoryComboData;
1145 struct _HistoryComboData
1150 gint history_levels;
1153 static void history_combo_destroy(GtkWidget *widget, gpointer data)
1155 HistoryComboData *hc = data;
1157 g_free(hc->history_key);
1161 /* if text is NULL, entry is set to the most recent item */
1162 GtkWidget *history_combo_new(GtkWidget **entry, const gchar *text,
1163 const gchar *history_key, gint max_levels)
1165 HistoryComboData *hc;
1169 hc = g_new0(HistoryComboData, 1);
1170 hc->history_key = g_strdup(history_key);
1171 hc->history_levels = max_levels;
1173 hc->combo = gtk_combo_box_entry_new_text();
1175 gtk_combo_set_case_sensitive(GTK_COMBO(hc->combo), TRUE);
1176 gtk_combo_set_use_arrows(GTK_COMBO(hc->combo), FALSE);
1179 hc->entry = GTK_BIN(hc->combo)->child;
1181 g_object_set_data(G_OBJECT(hc->combo), "history_combo_data", hc);
1182 g_object_set_data(G_OBJECT(hc->entry), "history_combo_data", hc);
1183 g_signal_connect(G_OBJECT(hc->combo), "destroy",
1184 G_CALLBACK(history_combo_destroy), hc);
1186 work = history_list_get_by_key(hc->history_key);
1189 gtk_combo_box_append_text(GTK_COMBO_BOX(hc->combo), (gchar *)work->data);
1196 gtk_entry_set_text(GTK_ENTRY(hc->entry), text);
1200 gtk_combo_box_set_active(GTK_COMBO_BOX(hc->combo), 0);
1203 if (entry) *entry = hc->entry;
1207 /* if text is NULL, current entry text is used
1208 * widget can be the combo or entry widget
1210 void history_combo_append_history(GtkWidget *widget, const gchar *text)
1212 HistoryComboData *hc;
1215 hc = g_object_get_data(G_OBJECT(widget), "history_combo_data");
1218 printf("widget is not a history combo\n");
1224 new_text = g_strdup(text);
1228 new_text = g_strdup(gtk_entry_get_text(GTK_ENTRY(hc->entry)));
1231 if (new_text && strlen(new_text) > 0)
1233 GtkTreeModel *store;
1236 history_list_add_to_key(hc->history_key, new_text, hc->history_levels);
1238 gtk_combo_box_set_active(GTK_COMBO_BOX(hc->combo), -1);
1240 store = gtk_combo_box_get_model(GTK_COMBO_BOX(hc->combo));
1241 gtk_list_store_clear(GTK_LIST_STORE(store));
1243 work = history_list_get_by_key(hc->history_key);
1246 gtk_combo_box_append_text(GTK_COMBO_BOX(hc->combo), (gchar *)work->data);
1255 *-----------------------------------------------------------------------------
1256 * drag and drop uri utils
1257 *-----------------------------------------------------------------------------
1260 /* the following characters are allowed to be unencoded for pathnames:
1263 static gint escape_char_list[] = {
1264 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0 */
1265 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 */
1266 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 20 */
1267 /* spc ! " # $ % & ' */
1268 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, /* 30 */
1269 /* ( ) * + , - . / 0 1 */
1270 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 */
1271 /* 2 3 4 5 6 7 8 9 : ; */
1272 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, /* 50 */
1273 /* < = > ? @ A B C D E */
1274 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, /* 60 */
1275 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 70 */
1276 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 */
1277 /* Z [ \ ] ^ _ ` a b c */
1278 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, /* 90 */
1279 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 100 */
1280 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 110 */
1281 /* x y z { | } ~ del */
1282 0, 0, 0, 1, 1, 1, 0, 0 /* 120, 127 is end */
1285 static gchar *hex_char = "0123456789ABCDEF";
1287 static gint escape_test(guchar c)
1289 if (c < 32 || c > 127) return TRUE;
1290 return (escape_char_list[c] != 0);
1293 static const gchar *escape_code(guchar c)
1295 static gchar text[4];
1298 text[1] = hex_char[c>>4];
1299 text[2] = hex_char[c%16];
1305 gchar *uri_text_escape(const gchar *text)
1311 if (!text) return NULL;
1313 string = g_string_new("");
1318 if (escape_test(*p))
1320 g_string_append(string, escape_code(*p));
1324 g_string_append_c(string, *p);
1329 result = string->str;
1330 g_string_free(string, FALSE);
1332 /* dropped filenames are expected to be utf-8 compatible */
1333 if (!g_utf8_validate(result, -1, NULL))
1337 tmp = g_locale_to_utf8(result, -1, NULL, NULL, NULL);
1348 /* this operates on the passed string, decoding escaped characters */
1349 void uri_text_decode(gchar *text)
1351 if (strchr(text, '%'))
1360 if (*r == '%' && *(r + 1) != '\0' && *(r + 2) != '\0')
1370 n = (gint)strtol(t, NULL, 16);
1371 if (n > 0 && n < 256)
1377 /* invalid number, rewind and ignore this escape */
1389 if (*w != '\0') *w = '\0';
1393 static void uri_list_parse_encoded_chars(GList *list)
1399 gchar *text = work->data;
1401 uri_text_decode(text);
1407 GList *uri_list_from_text(gchar *data, gint files_only)
1414 while (data[b] != '\0')
1416 while (data[e] != '\r' && data[e] != '\n' && data[e] != '\0') e++;
1417 if (strncmp(data + b, "file:", 5) == 0)
1421 while (data[b] == '/' && data[b+1] == '/') b++;
1422 path = g_strndup(data + b, e - b);
1423 list = g_list_append(list, path_to_utf8(path));
1426 else if (!files_only && strncmp(data + b, "http:", 5) == 0)
1428 list = g_list_append(list, g_strndup(data + b, e - b));
1430 else if (!files_only && strncmp(data + b, "ftp:", 3) == 0)
1432 list = g_list_append(list, g_strndup(data + b, e - b));
1434 while (data[e] == '\r' || data[e] == '\n') e++;
1438 uri_list_parse_encoded_chars(list);
1443 GList *uri_filelist_from_text(gchar *data, gint files_only)
1445 GList *path_list = uri_list_from_text(data, files_only);
1446 GList *filelist = filelist_from_path_list(path_list);
1447 string_list_free(path_list);
1451 gchar *uri_text_from_list(GList *list, gint *len, gint plain_text)
1453 gchar *uri_text = NULL;
1463 string = g_string_new("");
1468 const gchar *name8; /* dnd filenames are in utf-8 */
1476 escaped = uri_text_escape(name8);
1477 g_string_append(string, "file:");
1478 g_string_append(string, escaped);
1481 g_string_append(string, "\r\n");
1485 g_string_append(string, name8);
1486 if (work->next) g_string_append(string, "\n");
1492 uri_text = string->str;
1493 if (len) *len = string->len;
1494 g_string_free(string, FALSE);
1499 gchar *uri_text_from_filelist(GList *list, gint *len, gint plain_text)
1501 GList *path_list = filelist_to_path_list(list);
1502 gchar *ret = uri_text_from_list(path_list, len, plain_text);
1503 string_list_free(path_list);