2008-05-05 Paolo Borelli <pborelli@katamail.com>
[nautilus.git] / src / nautilus-window-bookmarks.c
blob2b0d54d4eae87f283df2c6fea72eceab24b5a66f
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
3 /*
4 * Nautilus
6 * Copyright (C) 2000, 2001 Eazel, Inc.
7 * Copyright (C) 2005 Red Hat, Inc.
9 * Nautilus is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
14 * Nautilus is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * Author: John Sullivan <sullivan@eazel.com>
24 * Alexander Larsson <alexl@redhat.com>
27 #include <config.h>
29 #include <locale.h>
31 #include "nautilus-actions.h"
32 #include "nautilus-bookmark-list.h"
33 #include "nautilus-bookmarks-window.h"
34 #include "nautilus-window-bookmarks.h"
35 #include "nautilus-window-private.h"
36 #include <libnautilus-private/nautilus-undo-manager.h>
37 #include <eel/eel-debug.h>
38 #include <eel/eel-stock-dialogs.h>
39 #include <eel/eel-vfs-extensions.h>
40 #include <eel/eel-gtk-extensions.h>
42 #define MENU_ITEM_MAX_WIDTH_CHARS 32
44 static GtkWindow *bookmarks_window = NULL;
45 static NautilusBookmarkList *bookmarks = NULL;
47 static void schedule_refresh_bookmarks_menu (NautilusWindow *window);
49 static void
50 free_bookmark_list (void)
52 g_object_unref (bookmarks);
55 NautilusBookmarkList *
56 nautilus_get_bookmark_list (void)
58 if (bookmarks == NULL) {
59 bookmarks = nautilus_bookmark_list_new ();
60 eel_debug_call_at_shutdown (free_bookmark_list);
63 return bookmarks;
67 static void
68 remove_bookmarks_for_uri_if_yes (GtkDialog *dialog, int response, gpointer callback_data)
70 const char *uri;
72 g_assert (GTK_IS_DIALOG (dialog));
73 g_assert (callback_data != NULL);
75 if (response == GTK_RESPONSE_YES) {
76 uri = callback_data;
77 nautilus_bookmark_list_delete_items_with_uri (nautilus_get_bookmark_list (), uri);
80 gtk_object_destroy (GTK_OBJECT (dialog));
83 static void
84 show_bogus_bookmark_window (NautilusWindow *window,
85 NautilusBookmark *bookmark)
87 GtkDialog *dialog;
88 GFile *location;
89 char *uri_for_display;
90 char *prompt;
91 char *detail;
93 location = nautilus_bookmark_get_location (bookmark);
94 uri_for_display = g_file_get_parse_name (location);
96 prompt = _("Do you want to remove any bookmarks with the "
97 "non-existing location from your list?");
98 detail = g_strdup_printf (_("The location \"%s\" does not exist."), uri_for_display);
100 dialog = eel_show_yes_no_dialog (prompt, detail,
101 _("Bookmark for Nonexistent Location"),
102 GTK_STOCK_CANCEL,
103 GTK_WINDOW (window));
105 g_signal_connect_data (dialog,
106 "response",
107 G_CALLBACK (remove_bookmarks_for_uri_if_yes),
108 g_file_get_uri (location),
109 (GClosureNotify)g_free,
112 gtk_dialog_set_default_response (dialog, GTK_RESPONSE_NO);
114 g_object_unref (location);
115 g_free (uri_for_display);
116 g_free (detail);
119 static GtkWindow *
120 get_or_create_bookmarks_window (GObject *undo_manager_source)
122 if (bookmarks_window == NULL) {
123 bookmarks_window = create_bookmarks_window (nautilus_get_bookmark_list(), undo_manager_source);
124 } else {
125 edit_bookmarks_dialog_set_signals (undo_manager_source);
128 return bookmarks_window;
132 * nautilus_bookmarks_exiting:
134 * Last chance to save state before app exits.
135 * Called when application exits; don't call from anywhere else.
137 void
138 nautilus_bookmarks_exiting (void)
140 if (bookmarks_window != NULL) {
141 nautilus_bookmarks_window_save_geometry (bookmarks_window);
142 gtk_widget_destroy (GTK_WIDGET (bookmarks_window));
147 * add_bookmark_for_current_location
149 * Add a bookmark for the displayed location to the bookmarks menu.
150 * Does nothing if there's already a bookmark for the displayed location.
152 void
153 nautilus_window_add_bookmark_for_current_location (NautilusWindow *window)
155 NautilusBookmark *bookmark;
157 g_return_if_fail (NAUTILUS_IS_WINDOW (window));
159 bookmark = window->current_location_bookmark;
161 if (!nautilus_bookmark_list_contains (nautilus_get_bookmark_list (), bookmark)) {
162 nautilus_bookmark_list_append (nautilus_get_bookmark_list (), bookmark);
166 void
167 nautilus_window_edit_bookmarks (NautilusWindow *window)
169 GtkWindow *dialog;
171 dialog = get_or_create_bookmarks_window (G_OBJECT (window));
173 gtk_window_set_screen (
174 dialog, gtk_window_get_screen (GTK_WINDOW (window)));
175 gtk_window_present (dialog);
178 void
179 nautilus_window_remove_bookmarks_menu_callback (NautilusWindow *window)
181 if (window->details->refresh_bookmarks_menu_idle_id != 0) {
182 g_source_remove (window->details->refresh_bookmarks_menu_idle_id);
183 window->details->refresh_bookmarks_menu_idle_id = 0;
187 static void
188 remove_bookmarks_menu_items (NautilusWindow *window)
190 GtkUIManager *ui_manager;
192 ui_manager = nautilus_window_get_ui_manager (window);
193 if (window->details->bookmarks_merge_id != 0) {
194 gtk_ui_manager_remove_ui (ui_manager,
195 window->details->bookmarks_merge_id);
196 window->details->bookmarks_merge_id = 0;
198 if (window->details->bookmarks_action_group != NULL) {
199 gtk_ui_manager_remove_action_group (ui_manager,
200 window->details->bookmarks_action_group);
201 window->details->bookmarks_action_group = NULL;
205 static void
206 connect_proxy_cb (GtkActionGroup *action_group,
207 GtkAction *action,
208 GtkWidget *proxy,
209 gpointer dummy)
211 GtkLabel *label;
213 if (!GTK_IS_MENU_ITEM (proxy))
214 return;
216 label = GTK_LABEL (GTK_BIN (proxy)->child);
218 gtk_label_set_use_underline (label, FALSE);
219 gtk_label_set_ellipsize (label, PANGO_ELLIPSIZE_END);
220 gtk_label_set_max_width_chars (label, MENU_ITEM_MAX_WIDTH_CHARS);
223 static void
224 update_bookmarks (NautilusWindow *window)
226 NautilusBookmarkList *bookmarks;
227 NautilusBookmark *bookmark;
228 guint bookmark_count;
229 guint index;
230 GtkUIManager *ui_manager;
232 g_assert (NAUTILUS_IS_WINDOW (window));
233 g_assert (window->details->bookmarks_merge_id == 0);
234 g_assert (window->details->bookmarks_action_group == NULL);
236 bookmarks = nautilus_get_bookmark_list ();
238 ui_manager = nautilus_window_get_ui_manager (NAUTILUS_WINDOW (window));
240 window->details->bookmarks_merge_id = gtk_ui_manager_new_merge_id (ui_manager);
241 window->details->bookmarks_action_group = gtk_action_group_new ("BookmarksGroup");
242 g_signal_connect (window->details->bookmarks_action_group, "connect-proxy",
243 G_CALLBACK (connect_proxy_cb), NULL);
245 gtk_ui_manager_insert_action_group (ui_manager,
246 window->details->bookmarks_action_group,
247 -1);
248 g_object_unref (window->details->bookmarks_action_group);
250 /* append new set of bookmarks */
251 bookmark_count = nautilus_bookmark_list_length (bookmarks);
252 for (index = 0; index < bookmark_count; ++index) {
253 bookmark = nautilus_bookmark_list_item_at (bookmarks, index);
255 if (nautilus_bookmark_uri_known_not_to_exist (bookmark)) {
256 continue;
259 nautilus_menus_append_bookmark_to_menu
260 (NAUTILUS_WINDOW (window),
261 bookmark,
262 NAUTILUS_WINDOW_GET_CLASS (window)->bookmarks_placeholder,
263 "dynamic",
264 index,
265 window->details->bookmarks_action_group,
266 window->details->bookmarks_merge_id,
267 G_CALLBACK (schedule_refresh_bookmarks_menu),
268 show_bogus_bookmark_window);
272 static void
273 refresh_bookmarks_menu (NautilusWindow *window)
275 g_assert (NAUTILUS_IS_WINDOW (window));
277 /* Unregister any pending call to this function. */
278 nautilus_window_remove_bookmarks_menu_callback (window);
280 remove_bookmarks_menu_items (window);
281 update_bookmarks (window);
285 static gboolean
286 refresh_bookmarks_menu_idle_callback (gpointer data)
288 g_assert (NAUTILUS_IS_WINDOW (data));
290 refresh_bookmarks_menu (NAUTILUS_WINDOW (data));
292 /* Don't call this again (unless rescheduled) */
293 return FALSE;
296 static void
297 schedule_refresh_bookmarks_menu (NautilusWindow *window)
299 g_assert (NAUTILUS_IS_WINDOW (window));
301 if (window->details->refresh_bookmarks_menu_idle_id == 0) {
302 window->details->refresh_bookmarks_menu_idle_id
303 = g_idle_add (refresh_bookmarks_menu_idle_callback,
304 window);
309 * nautilus_window_initialize_bookmarks_menu
311 * Fill in bookmarks menu with stored bookmarks, and wire up signals
312 * so we'll be notified when bookmark list changes.
314 void
315 nautilus_window_initialize_bookmarks_menu (NautilusWindow *window)
317 g_assert (NAUTILUS_IS_WINDOW (window));
319 /* Construct the initial set of bookmarks. */
320 /* We do this in an idle, since this is called in the constructor
321 * where we don't know the real type of the window */
322 schedule_refresh_bookmarks_menu (window);
324 /* Recreate dynamic part of menu if bookmark list changes */
325 g_signal_connect_object (nautilus_get_bookmark_list (), "contents_changed",
326 G_CALLBACK (schedule_refresh_bookmarks_menu),
327 window, G_CONNECT_SWAPPED);