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)
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
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 */
32 #include "bookmarks.h"
37 #include "gui_support.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
,
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
,
67 static void edit_delete(GtkButton
*button
, GtkTreeView
*view
);
68 static gboolean
dir_dropped(GtkWidget
*window
, GdkDragContext
*context
,
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
)
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)
101 GtkWidget
*list
, *hbox
, *button
, *swin
;
102 GtkTreeSelection
*selection
;
103 GtkCellRenderer
*cell
;
107 if (bookmarks_window
)
109 gtk_window_present(GTK_WINDOW(bookmarks_window
));
115 bookmarks_window
= gtk_dialog_new();
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
),
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
)
153 if (node
->type
!= XML_ELEMENT_NODE
)
155 if (strcmp(node
->name
, "bookmark") != 0)
158 mark
= xmlNodeListGetString(bookmarks
->doc
,
159 node
->xmlChildrenNode
, 1);
163 gtk_list_store_append(model
, &iter
);
164 gtk_list_store_set(model
, &iter
, 0, mark
, -1);
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
)
226 old
= g_hash_table_lookup(history_hash
, path
);
229 g_hash_table_remove(history_hash
, path
);
231 if (history_tail
== old
)
232 history_tail
= old
->prev
;
234 history
= g_list_delete_link(history
, old
);
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
)
247 new = g_strdup(path
);
251 history_hash
= g_hash_table_new(g_str_hash
, g_str_equal
);
255 history
= g_list_prepend(history
, new);
257 history_tail
= history
;
258 g_hash_table_insert(history_hash
, new, history
);
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
)
273 path
= get_local_path(uri
);
277 delayed_error(_("Can't bookmark non-local resource '%s'\n"),
282 if (mc_stat(path
, &info
) == 0 && S_ISDIR(info
.st_mode
))
283 bookmarks_add_dir(path
);
285 delayed_error(_("'%s' isn't a directory"), path
);
289 /****************************************************************
290 * INTERNAL FUNCTIONS *
291 ****************************************************************/
293 /* Initialise the bookmarks document to be empty. Does not save. */
294 static void bookmarks_new(void)
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()
320 /* Update the bookmarks, if possible */
321 path
= choices_find_path_load("Bookmarks.xml", PROJECT
);
325 wrapper
= xml_cache_load(path
);
329 g_object_unref(bookmarks
);
340 /* Return the node for the 'mark' bookmark */
341 static xmlNode
*bookmark_find(const gchar
*mark
)
347 node
= xmlDocGetRootElement(bookmarks
->doc
);
349 for (node
= node
->xmlChildrenNode
; node
; node
= node
->next
)
354 if (node
->type
!= XML_ELEMENT_NODE
)
356 if (strcmp(node
->name
, "bookmark") != 0)
359 path
= xmlNodeListGetString(bookmarks
->doc
,
360 node
->xmlChildrenNode
, 1);
364 same
= strcmp(mark
, path
) == 0;
374 /* Save the bookmarks to a file */
375 static void bookmarks_save()
379 save_path
= choices_find_path_save("Bookmarks.xml", PROJECT
, TRUE
);
382 save_xml_file(bookmarks
->doc
, save_path
);
387 /* Add a bookmark if it doesn't already exist, and save the
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
)
401 if (bookmark_find(dir
))
404 bookmark
= xmlNewTextChild(xmlDocGetRootElement(bookmarks
->doc
),
405 NULL
, "bookmark", dir
);
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
)
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
))
429 paths
= g_list_prepend(NULL
, filer_window
->real_path
);
430 action_mount(paths
, FALSE
, -1);
435 static void edit_delete(GtkButton
*button
, GtkTreeView
*view
)
439 GtkTreeSelection
*selection
;
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
);
452 GtkTreeIter old
= iter
;
454 more
= gtk_tree_model_iter_next(model
, &iter
);
456 if (gtk_tree_selection_iter_is_selected(selection
, &old
))
459 gtk_list_store_remove(list
, &old
);
465 report_error(_("You should first select some rows to delete"));
470 static void reorder(GtkTreeView
*view
, int dir
)
474 GtkTreePath
*cursor
= NULL
;
475 GtkTreeIter iter
, old
, new;
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
);
488 report_error(_("Put the cursor on an entry in the "
493 gtk_tree_model_get_iter(model
, &old
, cursor
);
496 gtk_tree_path_next(cursor
);
497 ok
= gtk_tree_model_get_iter(model
, &iter
, cursor
);
501 ok
= gtk_tree_path_prev(cursor
);
503 gtk_tree_model_get_iter(model
, &iter
, cursor
);
507 gtk_tree_path_free(cursor
);
508 report_error(_("This item is already at the end"));
512 gtk_tree_model_get_value(model
, &old
, 0, &value
);
514 gtk_list_store_insert_after(list
, &new, &iter
);
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
)
531 static void reorder_down(GtkButton
*button
, GtkTreeView
*view
)
536 static gboolean
dir_dropped(GtkWidget
*window
, GdkDragContext
*context
,
538 GtkSelectionData
*selection_data
, guint info
,
539 guint time
, GtkTreeView
*view
)
544 if (!selection_data
->data
)
547 gtk_drag_finish(context
, FALSE
, FALSE
, time
); /* Failure */
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
)
559 path
= get_local_path((gchar
*) next
->data
);
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);
572 delayed_error(_("'%s' isn't a directory"),
578 delayed_error(_("Can't bookmark non-local directories "
579 "like '%s'"), (gchar
*) next
->data
);
582 destroy_glist(&uris
);
587 static void commit_edits(GtkTreeModel
*model
)
593 if (gtk_tree_model_get_iter_first(model
, &iter
))
596 xmlNode
*root
= xmlDocGetRootElement(bookmarks
->doc
);
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
));
612 static void edit_response(GtkWidget
*window
, gint response
, GtkTreeModel
*model
)
616 gtk_widget_destroy(window
);
619 static void cell_edited(GtkCellRendererText
*cell
,
620 const gchar
*path_string
,
621 const gchar
*new_text
,
624 GtkTreeModel
*model
= (GtkTreeModel
*) data
;
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
)
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
)
652 menu
= gtk_menu_new();
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
++)
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
);
677 g_signal_connect(item
, "activate",
678 G_CALLBACK(bookmarks_activate
),
681 gtk_widget_show(item
);
682 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), item
);
685 g_ptr_array_free(items
, TRUE
);
690 /* Builds the bookmarks' menu. Done whenever the bookmarks icon has been
693 static GtkWidget
*bookmarks_build_menu(FilerWindow
*filer_window
)
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 */
724 node
= xmlDocGetRootElement(bookmarks
->doc
);
726 for (node
= node
->xmlChildrenNode
; node
; node
= node
->next
)
730 if (node
->type
!= XML_ELEMENT_NODE
)
732 if (strcmp(node
->name
, "bookmark") != 0)
735 mark
= xmlNodeListGetString(bookmarks
->doc
,
736 node
->xmlChildrenNode
, 1);
739 item
= gtk_menu_item_new_with_label(mark
);
742 g_signal_connect(item
, "activate",
743 G_CALLBACK(bookmarks_activate
),
746 gtk_widget_show(item
);
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
);