1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
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>
27 #include <glib/gi18n-lib.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
52 G_DEFINE_TYPE (NautilusXContentBar
, nautilus_x_content_bar
, GTK_TYPE_HBOX
)
55 nautilus_x_content_bar_set_x_content_type (NautilusXContentBar
*bar
, const char *x_content_type
)
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."));
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
) {
106 icon
= g_app_info_get_icon (default_app
);
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
);
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
);
130 gtk_widget_hide (bar
->priv
->button
);
134 g_free (description
);
138 nautilus_x_content_bar_get_x_content_type (NautilusXContentBar
*bar
)
140 return bar
->priv
->x_content_type
;
144 nautilus_x_content_bar_get_mount (NautilusXContentBar
*bar
)
146 return bar
->priv
->mount
!= NULL
? g_object_ref (bar
->priv
->mount
) : NULL
;
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
;
160 nautilus_x_content_bar_set_property (GObject
*object
,
165 NautilusXContentBar
*bar
;
167 bar
= NAUTILUS_X_CONTENT_BAR (object
);
171 nautilus_x_content_bar_set_mount (bar
, G_MOUNT (g_value_get_object (value
)));
173 case PROP_X_CONTENT_TYPE
:
174 nautilus_x_content_bar_set_x_content_type (bar
, g_value_get_string (value
));
177 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
183 nautilus_x_content_bar_get_property (GObject
*object
,
188 NautilusXContentBar
*bar
;
190 bar
= NAUTILUS_X_CONTENT_BAR (object
);
194 g_value_set_object (value
, bar
->priv
->mount
);
196 case PROP_X_CONTENT_TYPE
:
197 g_value_set_string (value
, bar
->priv
->x_content_type
);
200 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
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
);
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
,
231 g_param_spec_object (
233 "The GMount to run programs for",
234 "The GMount to run programs for",
236 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT
));
238 g_object_class_install_property (object_class
,
240 g_param_spec_string (
242 "The x-content type for the cluebar",
243 "The x-content type for the cluebar",
245 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT
));
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
)
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
);
265 nautilus_x_content_bar_init (NautilusXContentBar
*bar
)
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
,
283 G_CALLBACK (button_clicked_callback
),
288 nautilus_x_content_bar_new (GMount
*mount
,
289 const char *x_content_type
)
293 bar
= g_object_new (NAUTILUS_TYPE_X_CONTENT_BAR
,
295 "x-content-type", x_content_type
,
298 return GTK_WIDGET (bar
);