2008-04-30 Cosimo Cecchi <cosimoc@gnome.org>
[nautilus.git] / src / nautilus-x-content-bar.c
blob25130e8b643ba79cb385cf4185293be1bf8aacae
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * Copyright (C) 2008 Red Hat, Inc.
4 * Copyright (C) 2006 Paolo Borelli <pborelli@katamail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * Authors: David Zeuthen <davidz@redhat.com>
21 * Paolo Borelli <pborelli@katamail.com>
25 #include "config.h"
27 #include <glib/gi18n-lib.h>
28 #include <gtk/gtk.h>
29 #include <string.h>
31 #include "nautilus-x-content-bar.h"
32 #include <libnautilus-private/nautilus-autorun.h>
33 #include <libnautilus-private/nautilus-icon-info.h>
35 #define NAUTILUS_X_CONTENT_BAR_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NAUTILUS_TYPE_X_CONTENT_BAR, NautilusXContentBarPrivate))
37 struct NautilusXContentBarPrivate
39 GtkWidget *label;
40 GtkWidget *button;
42 char *x_content_type;
43 GMount *mount;
46 enum {
47 PROP_0,
48 PROP_MOUNT,
49 PROP_X_CONTENT_TYPE,
52 G_DEFINE_TYPE (NautilusXContentBar, nautilus_x_content_bar, GTK_TYPE_HBOX)
54 void
55 nautilus_x_content_bar_set_x_content_type (NautilusXContentBar *bar, const char *x_content_type)
57 char *message;
58 char *description;
59 GAppInfo *default_app;
61 g_free (bar->priv->x_content_type);
62 bar->priv->x_content_type = g_strdup (x_content_type);
64 description = g_content_type_get_description (x_content_type);
66 /* Customize greeting for well-known x-content types */
67 if (strcmp (x_content_type, "x-content/audio-cdda") == 0) {
68 message = g_strdup (_("These files are on an Audio CD."));
69 } else if (strcmp (x_content_type, "x-content/audio-dvd") == 0) {
70 message = g_strdup (_("These files are on an Audio DVD."));
71 } else if (strcmp (x_content_type, "x-content/video-dvd") == 0) {
72 message = g_strdup (_("These files are on a Video DVD."));
73 } else if (strcmp (x_content_type, "x-content/video-vcd") == 0) {
74 message = g_strdup (_("These files are on a Video CD."));
75 } else if (strcmp (x_content_type, "x-content/video-svcd") == 0) {
76 message = g_strdup (_("These files are on a Super Video CD."));
77 } else if (strcmp (x_content_type, "x-content/image-photocd") == 0) {
78 message = g_strdup (_("These files are on a Photo CD."));
79 } else if (strcmp (x_content_type, "x-content/image-picturecd") == 0) {
80 message = g_strdup (_("These files are on a Picture CD."));
81 } else if (strcmp (x_content_type, "x-content/image-dcf") == 0) {
82 message = g_strdup (_("The media contains digital photos."));
83 } else if (strcmp (x_content_type, "x-content/audio-player") == 0) {
84 message = g_strdup (_("These files are on a digital audio player."));
85 } else if (strcmp (x_content_type, "x-content/software") == 0) {
86 message = g_strdup (_("The media contains software."));
87 } else {
88 /* fallback to generic greeting */
89 message = g_strdup_printf (_("The media has been detected as \"%s\"."), description);
93 gtk_label_set_text (GTK_LABEL (bar->priv->label), message);
94 gtk_widget_show (bar->priv->label);
96 /* TODO: We really need a GtkBrowserBackButton-ish widget here.. until then, we only
97 * show the default application. */
99 default_app = g_app_info_get_default_for_type (x_content_type, FALSE);
100 if (default_app != NULL) {
101 char *button_text;
102 const char *name;
103 GIcon *icon;
104 GtkWidget *image;
106 icon = g_app_info_get_icon (default_app);
107 if (icon != NULL) {
108 GdkPixbuf *pixbuf;
109 int icon_size;
110 NautilusIconInfo *icon_info;
111 icon_size = nautilus_get_icon_size_for_stock_size (GTK_ICON_SIZE_BUTTON);
112 icon_info = nautilus_icon_info_lookup (icon, icon_size);
113 pixbuf = nautilus_icon_info_get_pixbuf_at_size (icon_info, icon_size);
114 image = gtk_image_new_from_pixbuf (pixbuf);
115 g_object_unref (pixbuf);
116 g_object_unref (icon_info);
117 } else {
118 image = NULL;
121 name = g_app_info_get_name (default_app);
122 button_text = g_strdup_printf (_("Open %s"), name);
124 gtk_button_set_image (GTK_BUTTON (bar->priv->button), image);
125 gtk_button_set_label (GTK_BUTTON (bar->priv->button), button_text);
126 gtk_widget_show (bar->priv->button);
127 g_free (button_text);
128 g_object_unref (default_app);
129 } else {
130 gtk_widget_hide (bar->priv->button);
133 g_free (message);
134 g_free (description);
137 const char *
138 nautilus_x_content_bar_get_x_content_type (NautilusXContentBar *bar)
140 return bar->priv->x_content_type;
143 GMount *
144 nautilus_x_content_bar_get_mount (NautilusXContentBar *bar)
146 return bar->priv->mount != NULL ? g_object_ref (bar->priv->mount) : NULL;
149 void
150 nautilus_x_content_bar_set_mount (NautilusXContentBar *bar, GMount *mount)
152 if (bar->priv->mount != NULL) {
153 g_object_unref (bar->priv->mount);
155 bar->priv->mount = mount != NULL ? g_object_ref (mount) : NULL;
159 static void
160 nautilus_x_content_bar_set_property (GObject *object,
161 guint prop_id,
162 const GValue *value,
163 GParamSpec *pspec)
165 NautilusXContentBar *bar;
167 bar = NAUTILUS_X_CONTENT_BAR (object);
169 switch (prop_id) {
170 case PROP_MOUNT:
171 nautilus_x_content_bar_set_mount (bar, G_MOUNT (g_value_get_object (value)));
172 break;
173 case PROP_X_CONTENT_TYPE:
174 nautilus_x_content_bar_set_x_content_type (bar, g_value_get_string (value));
175 break;
176 default:
177 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
178 break;
182 static void
183 nautilus_x_content_bar_get_property (GObject *object,
184 guint prop_id,
185 GValue *value,
186 GParamSpec *pspec)
188 NautilusXContentBar *bar;
190 bar = NAUTILUS_X_CONTENT_BAR (object);
192 switch (prop_id) {
193 case PROP_MOUNT:
194 g_value_set_object (value, bar->priv->mount);
195 break;
196 case PROP_X_CONTENT_TYPE:
197 g_value_set_string (value, bar->priv->x_content_type);
198 break;
199 default:
200 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
201 break;
205 static void
206 nautilus_x_content_bar_finalize (GObject *object)
208 NautilusXContentBar *bar = NAUTILUS_X_CONTENT_BAR (object);
210 g_free (bar->priv->x_content_type);
211 if (bar->priv->mount != NULL)
212 g_object_unref (bar->priv->mount);
214 G_OBJECT_CLASS (nautilus_x_content_bar_parent_class)->finalize (object);
217 static void
218 nautilus_x_content_bar_class_init (NautilusXContentBarClass *klass)
220 GObjectClass *object_class;
222 object_class = G_OBJECT_CLASS (klass);
223 object_class->get_property = nautilus_x_content_bar_get_property;
224 object_class->set_property = nautilus_x_content_bar_set_property;
225 object_class->finalize = nautilus_x_content_bar_finalize;
227 g_type_class_add_private (klass, sizeof (NautilusXContentBarPrivate));
229 g_object_class_install_property (object_class,
230 PROP_MOUNT,
231 g_param_spec_object (
232 "mount",
233 "The GMount to run programs for",
234 "The GMount to run programs for",
235 G_TYPE_MOUNT,
236 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
238 g_object_class_install_property (object_class,
239 PROP_X_CONTENT_TYPE,
240 g_param_spec_string (
241 "x-content-type",
242 "The x-content type for the cluebar",
243 "The x-content type for the cluebar",
244 NULL,
245 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
248 static void
249 button_clicked_callback (GtkWidget *button, NautilusXContentBar *bar)
251 GAppInfo *default_app;
253 if (bar->priv->x_content_type == NULL ||
254 bar->priv->mount == NULL)
255 return;
257 default_app = g_app_info_get_default_for_type (bar->priv->x_content_type, FALSE);
258 if (default_app != NULL) {
259 nautilus_autorun_launch_for_mount (bar->priv->mount, default_app);
260 g_object_unref (default_app);
264 static void
265 nautilus_x_content_bar_init (NautilusXContentBar *bar)
267 GtkWidget *hbox;
269 bar->priv = NAUTILUS_X_CONTENT_BAR_GET_PRIVATE (bar);
271 hbox = GTK_WIDGET (bar);
273 bar->priv->label = gtk_label_new (NULL);
274 gtk_label_set_ellipsize (GTK_LABEL (bar->priv->label), PANGO_ELLIPSIZE_END);
275 gtk_misc_set_alignment (GTK_MISC (bar->priv->label), 0.0, 0.5);
276 gtk_box_pack_start (GTK_BOX (bar), bar->priv->label, TRUE, TRUE, 0);
278 bar->priv->button = gtk_button_new ();
279 gtk_box_pack_end (GTK_BOX (hbox), bar->priv->button, FALSE, FALSE, 0);
281 g_signal_connect (bar->priv->button,
282 "clicked",
283 G_CALLBACK (button_clicked_callback),
284 bar);
287 GtkWidget *
288 nautilus_x_content_bar_new (GMount *mount,
289 const char *x_content_type)
291 GObject *bar;
293 bar = g_object_new (NAUTILUS_TYPE_X_CONTENT_BAR,
294 "mount", mount,
295 "x-content-type", x_content_type,
296 NULL);
298 return GTK_WIDGET (bar);