r3247: Experimental extended attributes support for setting MIME types
[rox-filer.git] / ROX-Filer / src / bookmarks.c
blob3924c0cf7b14a7e5a082f7df1cbc27f108c00d0c
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2003, the ROX-Filer team.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* bookmarks.c - handles the bookmarks menu */
24 #include "config.h"
26 #include <stdlib.h>
27 #include <gtk/gtk.h>
28 #include <string.h>
30 #include "global.h"
32 #include "bookmarks.h"
33 #include "choices.h"
34 #include "filer.h"
35 #include "xml.h"
36 #include "support.h"
37 #include "gui_support.h"
38 #include "main.h"
39 #include "mount.h"
40 #include "action.h"
42 static GList *history = NULL; /* Most recent first */
43 static GList *history_tail = NULL; /* Oldest item */
44 static GHashTable *history_hash = NULL; /* Path -> GList link */
45 static gint history_free = 30; /* Space left in history */
47 static XMLwrapper *bookmarks = NULL;
48 static GtkWidget *bookmarks_window = NULL;
50 /* Static prototypes */
51 static void update_bookmarks(void);
52 static xmlNode *bookmark_find(const gchar *mark);
53 static void bookmarks_save(void);
54 static void bookmarks_add(GtkMenuItem *menuitem, gpointer user_data);
55 static void bookmarks_activate(GtkMenuShell *item, FilerWindow *filer_window);
56 static GtkWidget *bookmarks_build_menu(FilerWindow *filer_window);
57 static void position_menu(GtkMenu *menu, gint *x, gint *y,
58 gboolean *push_in, gpointer data);
59 static void cell_edited(GtkCellRendererText *cell,
60 const gchar *path_string,
61 const gchar *new_text,
62 gpointer data);
63 static void reorder_up(GtkButton *button, GtkTreeView *view);
64 static void reorder_down(GtkButton *button, GtkTreeView *view);
65 static void edit_response(GtkWidget *window, gint response,
66 GtkTreeModel *model);
67 static void edit_delete(GtkButton *button, GtkTreeView *view);
68 static gboolean dir_dropped(GtkWidget *window, GdkDragContext *context,
69 int x, int y,
70 GtkSelectionData *selection_data, guint info,
71 guint time, GtkTreeView *view);
72 static void bookmarks_add_dir(const guchar *dir);
73 static void commit_edits(GtkTreeModel *model);
76 /****************************************************************
77 * EXTERNAL INTERFACE *
78 ****************************************************************/
80 /* Shows the bookmarks menu */
81 void bookmarks_show_menu(FilerWindow *filer_window)
83 GdkEvent *event;
84 GtkMenu *menu;
85 int button = 0;
87 event = gtk_get_current_event();
88 if (event->type == GDK_BUTTON_RELEASE ||
89 event->type == GDK_BUTTON_PRESS)
90 button = ((GdkEventButton *) event)->button;
92 menu = GTK_MENU(bookmarks_build_menu(filer_window));
93 gtk_menu_popup(menu, NULL, NULL, position_menu, filer_window,
94 button, gtk_get_current_event_time());
97 /* Show the Edit Bookmarks dialog */
98 void bookmarks_edit(void)
100 GtkListStore *model;
101 GtkWidget *list, *hbox, *button, *swin;
102 GtkTreeSelection *selection;
103 GtkCellRenderer *cell;
104 xmlNode *node;
105 GtkTreeIter iter;
107 if (bookmarks_window)
109 gtk_window_present(GTK_WINDOW(bookmarks_window));
110 return;
113 update_bookmarks();
115 bookmarks_window = gtk_dialog_new();
116 number_of_windows++;
118 gtk_dialog_add_button(GTK_DIALOG(bookmarks_window),
119 GTK_STOCK_CLOSE, GTK_RESPONSE_OK);
121 g_signal_connect(bookmarks_window, "destroy",
122 G_CALLBACK(gtk_widget_destroyed), &bookmarks_window);
123 g_signal_connect(bookmarks_window, "destroy",
124 G_CALLBACK(one_less_window), NULL);
126 swin = gtk_scrolled_window_new(NULL, NULL);
127 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(swin),
128 GTK_SHADOW_IN);
129 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swin),
130 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
131 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(bookmarks_window)->vbox),
132 swin, TRUE, TRUE, 0);
134 model = gtk_list_store_new(1, G_TYPE_STRING);
136 list = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
138 cell = gtk_cell_renderer_text_new();
139 g_signal_connect(G_OBJECT(cell), "edited",
140 G_CALLBACK(cell_edited), model);
141 g_object_set(G_OBJECT(cell), "editable", TRUE, NULL);
142 gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(list), -1,
143 NULL, cell, "text", 0, NULL);
144 gtk_tree_view_set_reorderable(GTK_TREE_VIEW(list), TRUE);
145 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(list), FALSE);
147 node = xmlDocGetRootElement(bookmarks->doc);
148 for (node = node->xmlChildrenNode; node; node = node->next)
150 GtkTreeIter iter;
151 guchar *mark;
153 if (node->type != XML_ELEMENT_NODE)
154 continue;
155 if (strcmp(node->name, "bookmark") != 0)
156 continue;
158 mark = xmlNodeListGetString(bookmarks->doc,
159 node->xmlChildrenNode, 1);
160 if (!mark)
161 continue;
163 gtk_list_store_append(model, &iter);
164 gtk_list_store_set(model, &iter, 0, mark, -1);
166 xmlFree(mark);
169 gtk_widget_set_size_request(list, 300, 300);
170 gtk_container_add(GTK_CONTAINER(swin), list);
172 hbox = gtk_hbutton_box_new();
173 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(bookmarks_window)->vbox),
174 hbox, FALSE, TRUE, 0);
175 gtk_container_set_border_width(GTK_CONTAINER(hbox), 5);
177 button = gtk_button_new_from_stock(GTK_STOCK_DELETE);
178 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, TRUE, 0);
179 g_signal_connect(button, "clicked", G_CALLBACK(edit_delete), list);
181 button = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
182 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, TRUE, 0);
183 g_signal_connect(button, "clicked", G_CALLBACK(reorder_up), list);
184 button = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
185 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, TRUE, 0);
186 g_signal_connect(button, "clicked", G_CALLBACK(reorder_down), list);
188 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
189 gtk_tree_selection_set_mode(selection, GTK_SELECTION_MULTIPLE);
191 /* Select the first item, otherwise the first click starts edit
192 * mode, which is very confusing!
194 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(model), &iter))
195 gtk_tree_selection_select_iter(selection, &iter);
197 g_signal_connect(bookmarks_window, "response",
198 G_CALLBACK(edit_response), model);
200 /* Allow directories to be dropped in */
202 GtkTargetEntry targets[] = { {"text/uri-list", 0, 0} };
203 gtk_drag_dest_set(bookmarks_window, GTK_DEST_DEFAULT_ALL,
204 targets, G_N_ELEMENTS(targets),
205 GDK_ACTION_COPY |GDK_ACTION_PRIVATE);
206 g_signal_connect(bookmarks_window, "drag-data-received",
207 G_CALLBACK(dir_dropped), list);
210 g_signal_connect_swapped(model, "row-changed",
211 G_CALLBACK(commit_edits), model);
212 g_signal_connect_swapped(model, "row-inserted",
213 G_CALLBACK(commit_edits), model);
214 g_signal_connect_swapped(model, "row-deleted",
215 G_CALLBACK(commit_edits), model);
216 g_signal_connect_swapped(model, "rows-reordered",
217 G_CALLBACK(commit_edits), model);
219 gtk_widget_show_all(bookmarks_window);
222 static void history_remove(const char *path)
224 GList *old;
226 old = g_hash_table_lookup(history_hash, path);
227 if (old)
229 g_hash_table_remove(history_hash, path);
231 if (history_tail == old)
232 history_tail = old->prev;
233 g_free(old->data);
234 history = g_list_delete_link(history, old);
236 history_free++;
240 /* Add this path to the global history of visited directories. If it
241 * already exists there, make it the most recent.
243 void bookmarks_add_history(const gchar *path)
245 char *new;
247 new = g_strdup(path);
248 ensure_utf8(&new);
250 if (!history_hash)
251 history_hash = g_hash_table_new(g_str_hash, g_str_equal);
253 history_remove(new);
255 history = g_list_prepend(history, new);
256 if (!history_tail)
257 history_tail = history;
258 g_hash_table_insert(history_hash, new, history);
260 history_free--;
261 if (history_free == -1)
263 g_return_if_fail(history_tail != NULL);
264 history_remove((char *) history_tail->data);
268 void bookmarks_add_uri(const gchar *uri)
270 char *path;
271 struct stat info;
273 path = get_local_path(uri);
275 if (!path)
277 delayed_error(_("Can't bookmark non-local resource '%s'\n"),
278 uri);
279 return;
282 if (mc_stat(path, &info) == 0 && S_ISDIR(info.st_mode))
283 bookmarks_add_dir(path);
284 else
285 delayed_error(_("'%s' isn't a directory"), path);
286 g_free(path);
289 /****************************************************************
290 * INTERNAL FUNCTIONS *
291 ****************************************************************/
293 /* Initialise the bookmarks document to be empty. Does not save. */
294 static void bookmarks_new(void)
296 if (bookmarks)
297 g_object_unref(G_OBJECT(bookmarks));
298 bookmarks = xml_new(NULL);
299 bookmarks->doc = xmlNewDoc("1.0");
300 xmlDocSetRootElement(bookmarks->doc,
301 xmlNewDocNode(bookmarks->doc, NULL, "bookmarks", NULL));
304 static void position_menu(GtkMenu *menu, gint *x, gint *y,
305 gboolean *push_in, gpointer data)
307 FilerWindow *filer_window = (FilerWindow *) data;
309 gdk_window_get_origin(GTK_WIDGET(filer_window->view)->window, x, y);
312 /* Makes sure that 'bookmarks' is up-to-date, reloading from file if it has
313 * changed. If no bookmarks were loaded and there is no file then initialise
314 * bookmarks to an empty document.
316 static void update_bookmarks()
318 gchar *path;
320 /* Update the bookmarks, if possible */
321 path = choices_find_path_load("Bookmarks.xml", PROJECT);
322 if (path)
324 XMLwrapper *wrapper;
325 wrapper = xml_cache_load(path);
326 if (wrapper)
328 if (bookmarks)
329 g_object_unref(bookmarks);
330 bookmarks = wrapper;
333 g_free(path);
336 if (!bookmarks)
337 bookmarks_new();
340 /* Return the node for the 'mark' bookmark */
341 static xmlNode *bookmark_find(const gchar *mark)
343 xmlNode *node;
345 update_bookmarks();
347 node = xmlDocGetRootElement(bookmarks->doc);
349 for (node = node->xmlChildrenNode; node; node = node->next)
351 gchar *path;
352 gboolean same;
354 if (node->type != XML_ELEMENT_NODE)
355 continue;
356 if (strcmp(node->name, "bookmark") != 0)
357 continue;
359 path = xmlNodeListGetString(bookmarks->doc,
360 node->xmlChildrenNode, 1);
361 if (!path)
362 continue;
364 same = strcmp(mark, path) == 0;
365 xmlFree(path);
367 if (same)
368 return node;
371 return NULL;
374 /* Save the bookmarks to a file */
375 static void bookmarks_save()
377 guchar *save_path;
379 save_path = choices_find_path_save("Bookmarks.xml", PROJECT, TRUE);
380 if (save_path)
382 save_xml_file(bookmarks->doc, save_path);
383 g_free(save_path);
387 /* Add a bookmark if it doesn't already exist, and save the
388 * bookmarks.
390 static void bookmarks_add(GtkMenuItem *menuitem, gpointer user_data)
392 FilerWindow *filer_window = (FilerWindow *) user_data;
394 bookmarks_add_dir(filer_window->sym_path);
397 static void bookmarks_add_dir(const guchar *dir)
399 xmlNode *bookmark;
401 if (bookmark_find(dir))
402 return;
404 bookmark = xmlNewTextChild(xmlDocGetRootElement(bookmarks->doc),
405 NULL, "bookmark", dir);
407 bookmarks_save();
409 if (bookmarks_window)
410 gtk_widget_destroy(bookmarks_window);
413 /* Called when a bookmark has been chosen */
414 static void bookmarks_activate(GtkMenuShell *item, FilerWindow *filer_window)
416 const gchar *mark;
417 GtkLabel *label;
419 label = GTK_LABEL(GTK_BIN(item)->child);
420 mark = gtk_label_get_text(label);
422 if (strcmp(mark, filer_window->sym_path) != 0)
423 filer_change_to(filer_window, mark, NULL);
424 if (g_hash_table_lookup(fstab_mounts, filer_window->real_path) &&
425 !mount_is_mounted(filer_window->real_path, NULL, NULL))
427 GList *paths;
429 paths = g_list_prepend(NULL, filer_window->real_path);
430 action_mount(paths, FALSE, -1);
431 g_list_free(paths);
435 static void edit_delete(GtkButton *button, GtkTreeView *view)
437 GtkTreeModel *model;
438 GtkListStore *list;
439 GtkTreeSelection *selection;
440 GtkTreeIter iter;
441 gboolean more, any = FALSE;
443 model = gtk_tree_view_get_model(view);
444 list = GTK_LIST_STORE(model);
446 selection = gtk_tree_view_get_selection(view);
448 more = gtk_tree_model_get_iter_first(model, &iter);
450 while (more)
452 GtkTreeIter old = iter;
454 more = gtk_tree_model_iter_next(model, &iter);
456 if (gtk_tree_selection_iter_is_selected(selection, &old))
458 any = TRUE;
459 gtk_list_store_remove(list, &old);
463 if (!any)
465 report_error(_("You should first select some rows to delete"));
466 return;
470 static void reorder(GtkTreeView *view, int dir)
472 GtkTreeModel *model;
473 GtkListStore *list;
474 GtkTreePath *cursor = NULL;
475 GtkTreeIter iter, old, new;
476 GValue value = {0};
477 gboolean ok;
479 g_return_if_fail(view != NULL);
480 g_return_if_fail(dir == 1 || dir == -1);
482 model = gtk_tree_view_get_model(view);
483 list = GTK_LIST_STORE(model);
485 gtk_tree_view_get_cursor(view, &cursor, NULL);
486 if (!cursor)
488 report_error(_("Put the cursor on an entry in the "
489 "list to move it"));
490 return;
493 gtk_tree_model_get_iter(model, &old, cursor);
494 if (dir > 0)
496 gtk_tree_path_next(cursor);
497 ok = gtk_tree_model_get_iter(model, &iter, cursor);
499 else
501 ok = gtk_tree_path_prev(cursor);
502 if (ok)
503 gtk_tree_model_get_iter(model, &iter, cursor);
505 if (!ok)
507 gtk_tree_path_free(cursor);
508 report_error(_("This item is already at the end"));
509 return;
512 gtk_tree_model_get_value(model, &old, 0, &value);
513 if (dir > 0)
514 gtk_list_store_insert_after(list, &new, &iter);
515 else
516 gtk_list_store_insert_before(list, &new, &iter);
517 gtk_list_store_set(list, &new, 0, g_value_get_string(&value), -1);
518 gtk_list_store_remove(list, &old);
520 g_value_unset(&value);
522 gtk_tree_view_set_cursor(view, cursor, 0, FALSE);
523 gtk_tree_path_free(cursor);
526 static void reorder_up(GtkButton *button, GtkTreeView *view)
528 reorder(view, -1);
531 static void reorder_down(GtkButton *button, GtkTreeView *view)
533 reorder(view, 1);
536 static gboolean dir_dropped(GtkWidget *window, GdkDragContext *context,
537 int x, int y,
538 GtkSelectionData *selection_data, guint info,
539 guint time, GtkTreeView *view)
541 GtkListStore *model;
542 GList *uris, *next;
544 if (!selection_data->data)
546 /* Timeout? */
547 gtk_drag_finish(context, FALSE, FALSE, time); /* Failure */
548 return TRUE;
551 model = GTK_LIST_STORE(gtk_tree_view_get_model(view));
553 uris = uri_list_to_glist(selection_data->data);
555 for (next = uris; next; next = next->next)
557 guchar *path;
559 path = get_local_path((gchar *) next->data);
561 if (path)
563 GtkTreeIter iter;
564 struct stat info;
566 if (mc_stat(path, &info) == 0 && S_ISDIR(info.st_mode))
568 gtk_list_store_append(model, &iter);
569 gtk_list_store_set(model, &iter, 0, path, -1);
571 else
572 delayed_error(_("'%s' isn't a directory"),
573 path);
575 g_free(path);
577 else
578 delayed_error(_("Can't bookmark non-local directories "
579 "like '%s'"), (gchar *) next->data);
582 destroy_glist(&uris);
584 return TRUE;
587 static void commit_edits(GtkTreeModel *model)
589 GtkTreeIter iter;
591 bookmarks_new();
593 if (gtk_tree_model_get_iter_first(model, &iter))
595 GValue value = {0};
596 xmlNode *root = xmlDocGetRootElement(bookmarks->doc);
600 xmlNode *bookmark;
602 gtk_tree_model_get_value(model, &iter, 0, &value);
603 bookmark = xmlNewTextChild(root, NULL, "bookmark",
604 g_value_get_string(&value));
605 g_value_unset(&value);
606 } while (gtk_tree_model_iter_next(model, &iter));
609 bookmarks_save();
612 static void edit_response(GtkWidget *window, gint response, GtkTreeModel *model)
614 commit_edits(model);
616 gtk_widget_destroy(window);
619 static void cell_edited(GtkCellRendererText *cell,
620 const gchar *path_string,
621 const gchar *new_text,
622 gpointer data)
624 GtkTreeModel *model = (GtkTreeModel *) data;
625 GtkTreePath *path;
626 GtkTreeIter iter;
628 path = gtk_tree_path_new_from_string(path_string);
629 gtk_tree_model_get_iter(model, &iter, path);
630 gtk_tree_path_free(path);
632 gtk_list_store_set(GTK_LIST_STORE(model), &iter, 0, new_text, -1);
635 static void activate_edit(GtkMenuShell *item, gpointer data)
637 bookmarks_edit();
640 static gint cmp_dirname(gconstpointer a, gconstpointer b)
642 return g_utf8_collate(*(gchar **) a, *(gchar **) b);
645 static GtkWidget *build_history_menu(FilerWindow *filer_window)
647 GtkWidget *menu;
648 GPtrArray *items;
649 GList *next;
650 int i;
652 menu = gtk_menu_new();
654 if (!history)
655 return menu;
657 g_return_val_if_fail(history_hash != NULL, menu);
658 g_return_val_if_fail(history_tail != NULL, menu);
660 items = g_ptr_array_new();
662 for (next = history; next; next = next->next)
663 g_ptr_array_add(items, next->data);
665 g_ptr_array_sort(items, cmp_dirname);
667 for (i = 0; i < items->len; i++)
669 GtkWidget *item;
670 const char *path = (char *) items->pdata[i];
672 item = gtk_menu_item_new_with_label(path);
674 if (strcmp(path, filer_window->sym_path) == 0)
675 gtk_widget_set_sensitive(item, FALSE);
676 else
677 g_signal_connect(item, "activate",
678 G_CALLBACK(bookmarks_activate),
679 filer_window);
681 gtk_widget_show(item);
682 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
685 g_ptr_array_free(items, TRUE);
687 return menu;
690 /* Builds the bookmarks' menu. Done whenever the bookmarks icon has been
691 * clicked.
693 static GtkWidget *bookmarks_build_menu(FilerWindow *filer_window)
695 GtkWidget *menu;
696 GtkWidget *item;
697 xmlNode *node;
698 gboolean need_separator = TRUE;
700 menu = gtk_menu_new();
702 item = gtk_menu_item_new_with_label(_("Add New Bookmark"));
703 g_signal_connect(item, "activate",
704 G_CALLBACK(bookmarks_add), filer_window);
705 gtk_widget_show(item);
706 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
707 gtk_menu_shell_select_item(GTK_MENU_SHELL(menu), item);
709 item = gtk_menu_item_new_with_label(_("Edit Bookmarks"));
710 g_signal_connect(item, "activate", G_CALLBACK(activate_edit), NULL);
711 gtk_widget_show(item);
712 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
714 item = gtk_menu_item_new_with_label(_("Recently Visited"));
715 gtk_widget_show(item);
716 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
717 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),
718 build_history_menu(filer_window));
720 /* Now add all the bookmarks to the menu */
722 update_bookmarks();
724 node = xmlDocGetRootElement(bookmarks->doc);
726 for (node = node->xmlChildrenNode; node; node = node->next)
728 gchar *mark;
730 if (node->type != XML_ELEMENT_NODE)
731 continue;
732 if (strcmp(node->name, "bookmark") != 0)
733 continue;
735 mark = xmlNodeListGetString(bookmarks->doc,
736 node->xmlChildrenNode, 1);
737 if (!mark)
738 continue;
739 item = gtk_menu_item_new_with_label(mark);
740 xmlFree(mark);
742 g_signal_connect(item, "activate",
743 G_CALLBACK(bookmarks_activate),
744 filer_window);
746 gtk_widget_show(item);
748 if (need_separator)
750 GtkWidget *sep;
751 sep = gtk_separator_menu_item_new();
752 gtk_widget_show(sep);
753 gtk_menu_shell_append(GTK_MENU_SHELL(menu), sep);
754 need_separator = FALSE;
757 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
761 return menu;