2008-05-05 Paolo Borelli <pborelli@katamail.com>
[nautilus.git] / src / nautilus-history-sidebar.c
blob61a30b3e59301cf4339a7bb82f0d77a5d0ad5f71
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
3 /*
4 * Nautilus
6 * Copyright (C) 1999, 2000 Red Hat, Inc.
7 * Copyright (C) 2000, 2001 Eazel, Inc.
9 * This library 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 * This library 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 library; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * Authors: Elliot Lee <sopwith@redhat.com>
24 * Darin Adler <darin@bentspoon.com>
28 #include <config.h>
30 #include <eel/eel-debug.h>
31 #include <eel/eel-gtk-extensions.h>
32 #include <eel/eel-glib-extensions.h>
33 #include <eel/eel-preferences.h>
34 #include <gtk/gtkcellrendererpixbuf.h>
35 #include <gtk/gtkcellrenderertext.h>
36 #include <gtk/gtkenums.h>
37 #include <gtk/gtkliststore.h>
38 #include <gtk/gtktreemodel.h>
39 #include <gtk/gtktreeselection.h>
40 #include <libgnome/gnome-macros.h>
41 #include <glib/gi18n.h>
42 #include <libnautilus-private/nautilus-bookmark.h>
43 #include <libnautilus-private/nautilus-global-preferences.h>
44 #include <libnautilus-private/nautilus-sidebar-provider.h>
45 #include <libnautilus-private/nautilus-module.h>
46 #include <libnautilus-private/nautilus-signaller.h>
48 #include "nautilus-history-sidebar.h"
50 #define NAUTILUS_HISTORY_SIDEBAR_CLASS(klass) (GTK_CHECK_CLASS_CAST ((klass), NAUTILUS_TYPE_HISTORY_SIDEBAR, NautilusHistorySidebarClass))
51 #define NAUTILUS_IS_HISTORY_SIDEBAR(obj) (GTK_CHECK_TYPE ((obj), NAUTILUS_TYPE_HISTORY_SIDEBAR))
52 #define NAUTILUS_IS_HISTORY_SIDEBAR_CLASS(klass) (GTK_CHECK_CLASS_TYPE ((klass), NAUTILUS_TYPE_HISTORY_SIDEBAR))
54 typedef struct {
55 GtkScrolledWindowClass parent;
56 } NautilusHistorySidebarClass;
58 typedef struct {
59 GObject parent;
60 } NautilusHistorySidebarProvider;
62 typedef struct {
63 GObjectClass parent;
64 } NautilusHistorySidebarProviderClass;
67 enum {
68 HISTORY_SIDEBAR_COLUMN_ICON,
69 HISTORY_SIDEBAR_COLUMN_NAME,
70 HISTORY_SIDEBAR_COLUMN_BOOKMARK,
71 HISTORY_SIDEBAR_COLUMN_COUNT
74 static void nautilus_history_sidebar_iface_init (NautilusSidebarIface *iface);
75 static void sidebar_provider_iface_init (NautilusSidebarProviderIface *iface);
76 static GType nautilus_history_sidebar_provider_get_type (void);
77 static void nautilus_history_sidebar_style_set (GtkWidget *widget,
78 GtkStyle *previous_style);
80 G_DEFINE_TYPE_WITH_CODE (NautilusHistorySidebar, nautilus_history_sidebar, GTK_TYPE_SCROLLED_WINDOW,
81 G_IMPLEMENT_INTERFACE (NAUTILUS_TYPE_SIDEBAR,
82 nautilus_history_sidebar_iface_init));
84 G_DEFINE_TYPE_WITH_CODE (NautilusHistorySidebarProvider, nautilus_history_sidebar_provider, G_TYPE_OBJECT,
85 G_IMPLEMENT_INTERFACE (NAUTILUS_TYPE_SIDEBAR_PROVIDER,
86 sidebar_provider_iface_init));
88 static void
89 update_history (NautilusHistorySidebar *sidebar)
91 GtkListStore *store;
92 GtkTreeSelection *selection;
93 NautilusBookmark *bookmark;
94 GdkPixbuf *pixbuf;
95 GtkTreeIter iter;
96 char *name;
97 GList *l, *history;
99 store = GTK_LIST_STORE (gtk_tree_view_get_model (sidebar->tree_view));
101 gtk_list_store_clear (store);
103 history = nautilus_window_info_get_history (sidebar->window);
104 for (l = history; l != NULL; l = l->next) {
105 bookmark = nautilus_bookmark_copy (l->data);
107 pixbuf = nautilus_bookmark_get_pixbuf (bookmark, GTK_ICON_SIZE_MENU);
108 name = nautilus_bookmark_get_name (bookmark);
109 gtk_list_store_append (store, &iter);
110 gtk_list_store_set (store, &iter,
111 HISTORY_SIDEBAR_COLUMN_ICON, pixbuf,
112 HISTORY_SIDEBAR_COLUMN_NAME, name,
113 HISTORY_SIDEBAR_COLUMN_BOOKMARK, bookmark,
114 -1);
115 g_object_unref (bookmark);
117 if (pixbuf != NULL) {
118 g_object_unref (pixbuf);
120 g_free (name);
122 eel_g_object_list_free (history);
124 selection = GTK_TREE_SELECTION (gtk_tree_view_get_selection (sidebar->tree_view));
126 if (gtk_tree_model_get_iter_root (GTK_TREE_MODEL (store), &iter)) {
127 gtk_tree_selection_select_iter (selection, &iter);
131 static void
132 history_changed_callback (GObject *signaller,
133 NautilusHistorySidebar *sidebar)
135 update_history (sidebar);
138 static void
139 row_activated_callback (GtkTreeView *tree_view,
140 GtkTreePath *path,
141 GtkTreeViewColumn *column,
142 gpointer user_data)
144 NautilusHistorySidebar *sidebar;
145 GtkTreeModel *model;
146 GtkTreeIter iter;
147 NautilusBookmark *bookmark;
148 GFile *location;
150 sidebar = NAUTILUS_HISTORY_SIDEBAR (user_data);
151 model = gtk_tree_view_get_model (tree_view);
153 if (!gtk_tree_model_get_iter (model, &iter, path)) {
154 return;
157 gtk_tree_model_get
158 (model, &iter, HISTORY_SIDEBAR_COLUMN_BOOKMARK, &bookmark, -1);
160 /* Navigate to the clicked location. */
161 location = nautilus_bookmark_get_location (NAUTILUS_BOOKMARK (bookmark));
162 nautilus_window_info_open_location
163 (sidebar->window,
164 location, NAUTILUS_WINDOW_OPEN_ACCORDING_TO_MODE, 0, NULL);
165 g_object_unref (location);
168 static void
169 update_click_policy (NautilusHistorySidebar *sidebar)
171 int policy;
173 policy = eel_preferences_get_enum (NAUTILUS_PREFERENCES_CLICK_POLICY);
175 eel_gtk_tree_view_set_activate_on_single_click
176 (sidebar->tree_view, policy == NAUTILUS_CLICK_POLICY_SINGLE);
179 static void
180 click_policy_changed_callback (gpointer user_data)
182 NautilusHistorySidebar *sidebar;
184 sidebar = NAUTILUS_HISTORY_SIDEBAR (user_data);
186 update_click_policy (sidebar);
189 static void
190 nautilus_history_sidebar_init (NautilusHistorySidebar *sidebar)
192 GtkTreeView *tree_view;
193 GtkTreeViewColumn *col;
194 GtkCellRenderer *cell;
195 GtkListStore *store;
196 GtkTreeSelection *selection;
198 tree_view = GTK_TREE_VIEW (gtk_tree_view_new ());
199 gtk_tree_view_set_headers_visible (tree_view, FALSE);
200 gtk_widget_show (GTK_WIDGET (tree_view));
202 col = GTK_TREE_VIEW_COLUMN (gtk_tree_view_column_new ());
204 cell = gtk_cell_renderer_pixbuf_new ();
205 gtk_tree_view_column_pack_start (col, cell, FALSE);
206 gtk_tree_view_column_set_attributes (col, cell,
207 "pixbuf", HISTORY_SIDEBAR_COLUMN_ICON,
208 NULL);
210 cell = gtk_cell_renderer_text_new ();
211 gtk_tree_view_column_pack_start (col, cell, TRUE);
212 gtk_tree_view_column_set_attributes (col, cell,
213 "text", HISTORY_SIDEBAR_COLUMN_NAME,
214 NULL);
216 gtk_tree_view_column_set_fixed_width (col, NAUTILUS_ICON_SIZE_SMALLER);
217 gtk_tree_view_append_column (tree_view, col);
219 store = gtk_list_store_new (HISTORY_SIDEBAR_COLUMN_COUNT,
220 GDK_TYPE_PIXBUF,
221 G_TYPE_STRING,
222 NAUTILUS_TYPE_BOOKMARK);
224 gtk_tree_view_set_model (tree_view, GTK_TREE_MODEL (store));
225 g_object_unref (store);
227 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sidebar),
228 GTK_POLICY_AUTOMATIC,
229 GTK_POLICY_AUTOMATIC);
230 gtk_scrolled_window_set_hadjustment (GTK_SCROLLED_WINDOW (sidebar), NULL);
231 gtk_scrolled_window_set_vadjustment (GTK_SCROLLED_WINDOW (sidebar), NULL);
232 gtk_container_add (GTK_CONTAINER (sidebar), GTK_WIDGET (tree_view));
233 gtk_widget_show (GTK_WIDGET (sidebar));
235 sidebar->tree_view = tree_view;
237 selection = gtk_tree_view_get_selection (tree_view);
238 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
240 g_signal_connect_object
241 (tree_view, "row_activated",
242 G_CALLBACK (row_activated_callback), sidebar, 0);
244 g_signal_connect_object (nautilus_signaller_get_current (),
245 "history_list_changed",
246 G_CALLBACK (history_changed_callback), sidebar, 0);
248 eel_preferences_add_callback (NAUTILUS_PREFERENCES_CLICK_POLICY,
249 click_policy_changed_callback,
250 sidebar);
251 update_click_policy (sidebar);
254 static void
255 nautilus_history_sidebar_finalize (GObject *object)
257 NautilusHistorySidebar *sidebar;
259 sidebar = NAUTILUS_HISTORY_SIDEBAR (object);
261 eel_preferences_remove_callback (NAUTILUS_PREFERENCES_CLICK_POLICY,
262 click_policy_changed_callback,
263 sidebar);
265 G_OBJECT_CLASS (nautilus_history_sidebar_parent_class)->finalize (object);
268 static void
269 nautilus_history_sidebar_class_init (NautilusHistorySidebarClass *class)
271 G_OBJECT_CLASS (class)->finalize = nautilus_history_sidebar_finalize;
273 GTK_WIDGET_CLASS (class)->style_set = nautilus_history_sidebar_style_set;
276 static const char *
277 nautilus_history_sidebar_get_sidebar_id (NautilusSidebar *sidebar)
279 return NAUTILUS_HISTORY_SIDEBAR_ID;
282 static char *
283 nautilus_history_sidebar_get_tab_label (NautilusSidebar *sidebar)
285 return g_strdup (_("History"));
288 static char *
289 nautilus_history_sidebar_get_tab_tooltip (NautilusSidebar *sidebar)
291 return g_strdup (_("Show History"));
294 static GdkPixbuf *
295 nautilus_history_sidebar_get_tab_icon (NautilusSidebar *sidebar)
297 return NULL;
300 static void
301 nautilus_history_sidebar_is_visible_changed (NautilusSidebar *sidebar,
302 gboolean is_visible)
304 /* Do nothing */
307 static void
308 nautilus_history_sidebar_iface_init (NautilusSidebarIface *iface)
310 iface->get_sidebar_id = nautilus_history_sidebar_get_sidebar_id;
311 iface->get_tab_label = nautilus_history_sidebar_get_tab_label;
312 iface->get_tab_tooltip = nautilus_history_sidebar_get_tab_tooltip;
313 iface->get_tab_icon = nautilus_history_sidebar_get_tab_icon;
314 iface->is_visible_changed = nautilus_history_sidebar_is_visible_changed;
317 static void
318 nautilus_history_sidebar_set_parent_window (NautilusHistorySidebar *sidebar,
319 NautilusWindowInfo *window)
321 sidebar->window = window;
322 update_history (sidebar);
325 static void
326 nautilus_history_sidebar_style_set (GtkWidget *widget,
327 GtkStyle *previous_style)
329 NautilusHistorySidebar *sidebar;
331 sidebar = NAUTILUS_HISTORY_SIDEBAR (widget);
333 update_history (sidebar);
336 static NautilusSidebar *
337 nautilus_history_sidebar_create (NautilusSidebarProvider *provider,
338 NautilusWindowInfo *window)
340 NautilusHistorySidebar *sidebar;
342 sidebar = g_object_new (nautilus_history_sidebar_get_type (), NULL);
343 nautilus_history_sidebar_set_parent_window (sidebar, window);
344 g_object_ref (sidebar);
345 gtk_object_sink (GTK_OBJECT (sidebar));
347 return NAUTILUS_SIDEBAR (sidebar);
350 static void
351 sidebar_provider_iface_init (NautilusSidebarProviderIface *iface)
353 iface->create = nautilus_history_sidebar_create;
356 static void
357 nautilus_history_sidebar_provider_init (NautilusHistorySidebarProvider *sidebar)
361 static void
362 nautilus_history_sidebar_provider_class_init (NautilusHistorySidebarProviderClass *class)
366 void
367 nautilus_history_sidebar_register (void)
369 nautilus_module_add_type (nautilus_history_sidebar_provider_get_type ());