1
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
3
* Copyright (C) 2008-2010 Red Hat, Inc.
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
* Author: David Zeuthen <davidz@redhat.com>
20
* Cosimo Cecchi <cosimoc@gnome.org>
26
#include "cc-media-panel.h"
29
#include <glib/gi18n.h>
31
G_DEFINE_DYNAMIC_TYPE (CcMediaPanel, cc_media_panel, CC_TYPE_PANEL)
33
#define MEDIA_PANEL_PRIVATE(o) \
34
(G_TYPE_INSTANCE_GET_PRIVATE ((o), CC_TYPE_MEDIA_PANEL, CcMediaPanelPrivate))
37
#define PREF_MEDIA_AUTORUN_NEVER "autorun-never"
38
#define PREF_MEDIA_AUTORUN_X_CONTENT_START_APP "autorun-x-content-start-app"
39
#define PREF_MEDIA_AUTORUN_X_CONTENT_IGNORE "autorun-x-content-ignore"
40
#define PREF_MEDIA_AUTORUN_X_CONTENT_OPEN_FOLDER "autorun-x-content-open-folder"
42
#define CUSTOM_ITEM_ASK "cc-item-ask"
43
#define CUSTOM_ITEM_DO_NOTHING "cc-item-do-nothing"
44
#define CUSTOM_ITEM_OPEN_FOLDER "cc-item-open-folder"
46
struct _CcMediaPanelPrivate {
48
GSettings *preferences;
50
GtkWidget *other_application_combo;
54
cc_media_panel_dispose (GObject *object)
56
CcMediaPanel *self = CC_MEDIA_PANEL (object);
58
if (self->priv->builder != NULL) {
59
g_object_unref (self->priv->builder);
60
self->priv->builder = NULL;
63
if (self->priv->preferences != NULL) {
64
g_object_unref (self->priv->preferences);
65
self->priv->preferences = NULL;
68
G_OBJECT_CLASS (cc_media_panel_parent_class)->dispose (object);
72
cc_media_panel_finalize (GObject *object)
74
G_OBJECT_CLASS (cc_media_panel_parent_class)->finalize (object);
78
cc_media_panel_class_init (CcMediaPanelClass *klass)
80
GObjectClass *object_class = G_OBJECT_CLASS (klass);
82
g_type_class_add_private (klass, sizeof (CcMediaPanelPrivate));
84
object_class->dispose = cc_media_panel_dispose;
85
object_class->finalize = cc_media_panel_finalize;
89
cc_media_panel_class_finalize (CcMediaPanelClass *klass)
94
remove_elem_from_str_array (char **v,
100
array = g_ptr_array_new ();
102
for (idx = 0; v[idx] != NULL; idx++) {
103
if (g_strcmp0 (v[idx], s) == 0) {
107
g_ptr_array_add (array, v[idx]);
110
g_ptr_array_add (array, NULL);
114
return (char **) g_ptr_array_free (array, FALSE);
118
add_elem_to_str_array (char **v,
124
array = g_ptr_array_new ();
126
for (idx = 0; v[idx] != NULL; idx++) {
127
g_ptr_array_add (array, v[idx]);
130
g_ptr_array_add (array, g_strdup (s));
131
g_ptr_array_add (array, NULL);
135
return (char **) g_ptr_array_free (array, FALSE);
139
media_panel_g_strv_find (char **strv,
144
g_return_val_if_fail (find_me != NULL, -1);
146
for (index = 0; strv[index] != NULL; ++index) {
147
if (g_strcmp0 (strv[index], find_me) == 0) {
156
autorun_get_preferences (CcMediaPanel *self,
157
const char *x_content_type,
158
gboolean *pref_start_app,
159
gboolean *pref_ignore,
160
gboolean *pref_open_folder)
162
char **x_content_start_app;
163
char **x_content_ignore;
164
char **x_content_open_folder;
166
g_return_if_fail (pref_start_app != NULL);
167
g_return_if_fail (pref_ignore != NULL);
168
g_return_if_fail (pref_open_folder != NULL);
170
*pref_start_app = FALSE;
171
*pref_ignore = FALSE;
172
*pref_open_folder = FALSE;
173
x_content_start_app = g_settings_get_strv (self->priv->preferences,
174
PREF_MEDIA_AUTORUN_X_CONTENT_START_APP);
175
x_content_ignore = g_settings_get_strv (self->priv->preferences,
176
PREF_MEDIA_AUTORUN_X_CONTENT_IGNORE);
177
x_content_open_folder = g_settings_get_strv (self->priv->preferences,
178
PREF_MEDIA_AUTORUN_X_CONTENT_OPEN_FOLDER);
179
if (x_content_start_app != NULL) {
180
*pref_start_app = media_panel_g_strv_find (x_content_start_app, x_content_type) != -1;
182
if (x_content_ignore != NULL) {
183
*pref_ignore = media_panel_g_strv_find (x_content_ignore, x_content_type) != -1;
185
if (x_content_open_folder != NULL) {
186
*pref_open_folder = media_panel_g_strv_find (x_content_open_folder, x_content_type) != -1;
188
g_strfreev (x_content_ignore);
189
g_strfreev (x_content_start_app);
190
g_strfreev (x_content_open_folder);
194
autorun_set_preferences (CcMediaPanel *self,
195
const char *x_content_type,
196
gboolean pref_start_app,
197
gboolean pref_ignore,
198
gboolean pref_open_folder)
200
char **x_content_start_app;
201
char **x_content_ignore;
202
char **x_content_open_folder;
204
g_assert (x_content_type != NULL);
206
x_content_start_app = g_settings_get_strv (self->priv->preferences,
207
PREF_MEDIA_AUTORUN_X_CONTENT_START_APP);
208
x_content_ignore = g_settings_get_strv (self->priv->preferences,
209
PREF_MEDIA_AUTORUN_X_CONTENT_IGNORE);
210
x_content_open_folder = g_settings_get_strv (self->priv->preferences,
211
PREF_MEDIA_AUTORUN_X_CONTENT_OPEN_FOLDER);
213
x_content_start_app = remove_elem_from_str_array (x_content_start_app, x_content_type);
214
if (pref_start_app) {
215
x_content_start_app = add_elem_to_str_array (x_content_start_app, x_content_type);
217
g_settings_set_strv (self->priv->preferences,
218
PREF_MEDIA_AUTORUN_X_CONTENT_START_APP, (const gchar * const*) x_content_start_app);
220
x_content_ignore = remove_elem_from_str_array (x_content_ignore, x_content_type);
222
x_content_ignore = add_elem_to_str_array (x_content_ignore, x_content_type);
224
g_settings_set_strv (self->priv->preferences,
225
PREF_MEDIA_AUTORUN_X_CONTENT_IGNORE, (const gchar * const*) x_content_ignore);
227
x_content_open_folder = remove_elem_from_str_array (x_content_open_folder, x_content_type);
228
if (pref_open_folder) {
229
x_content_open_folder = add_elem_to_str_array (x_content_open_folder, x_content_type);
231
g_settings_set_strv (self->priv->preferences,
232
PREF_MEDIA_AUTORUN_X_CONTENT_OPEN_FOLDER, (const gchar * const*) x_content_open_folder);
234
g_strfreev (x_content_open_folder);
235
g_strfreev (x_content_ignore);
236
g_strfreev (x_content_start_app);
241
custom_item_activated_cb (GtkAppChooserButton *button,
245
CcMediaPanel *self = user_data;
248
content_type = gtk_app_chooser_get_content_type (GTK_APP_CHOOSER (button));
250
if (g_strcmp0 (item, CUSTOM_ITEM_ASK) == 0) {
251
autorun_set_preferences (self, content_type,
252
FALSE, FALSE, FALSE);
253
} else if (g_strcmp0 (item, CUSTOM_ITEM_OPEN_FOLDER) == 0) {
254
autorun_set_preferences (self, content_type,
256
} else if (g_strcmp0 (item, CUSTOM_ITEM_DO_NOTHING) == 0) {
257
autorun_set_preferences (self, content_type,
261
g_free (content_type);
265
combo_box_changed_cb (GtkComboBox *combo_box,
268
CcMediaPanel *self = user_data;
272
info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (combo_box));
277
content_type = gtk_app_chooser_get_content_type (GTK_APP_CHOOSER (combo_box));
278
autorun_set_preferences (self, content_type,
280
g_app_info_set_as_default_for_type (info, content_type, NULL);
282
g_object_unref (info);
283
g_free (content_type);
287
prepare_combo_box (CcMediaPanel *self,
288
GtkWidget *combo_box,
289
const gchar *heading)
291
GtkAppChooserButton *app_chooser = GTK_APP_CHOOSER_BUTTON (combo_box);
293
gboolean pref_start_app;
294
gboolean pref_ignore;
295
gboolean pref_open_folder;
299
content_type = gtk_app_chooser_get_content_type (GTK_APP_CHOOSER (app_chooser));
301
/* fetch preferences for this content type */
302
autorun_get_preferences (self, content_type,
303
&pref_start_app, &pref_ignore, &pref_open_folder);
304
pref_ask = !pref_start_app && !pref_ignore && !pref_open_folder;
306
info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (combo_box));
308
/* append the separator only if we have >= 1 apps in the chooser */
310
gtk_app_chooser_button_append_separator (app_chooser);
311
g_object_unref (info);
314
gtk_app_chooser_button_append_custom_item (app_chooser, CUSTOM_ITEM_ASK,
318
gtk_app_chooser_button_append_custom_item (app_chooser, CUSTOM_ITEM_DO_NOTHING,
322
gtk_app_chooser_button_append_custom_item (app_chooser, CUSTOM_ITEM_OPEN_FOLDER,
326
gtk_app_chooser_button_set_show_dialog_item (app_chooser, TRUE);
327
gtk_app_chooser_button_set_heading (app_chooser, _(heading));
330
gtk_app_chooser_button_set_active_custom_item (app_chooser, CUSTOM_ITEM_ASK);
331
} else if (pref_ignore) {
332
gtk_app_chooser_button_set_active_custom_item (app_chooser, CUSTOM_ITEM_DO_NOTHING);
333
} else if (pref_open_folder) {
334
gtk_app_chooser_button_set_active_custom_item (app_chooser, CUSTOM_ITEM_OPEN_FOLDER);
337
g_signal_connect (app_chooser, "changed",
338
G_CALLBACK (combo_box_changed_cb), self);
339
g_signal_connect (app_chooser, "custom-item-activated",
340
G_CALLBACK (custom_item_activated_cb), self);
342
g_free (content_type);
346
other_type_combo_box_changed (GtkComboBox *combo_box,
351
char *x_content_type;
352
GtkWidget *action_container;
354
x_content_type = NULL;
356
if (!gtk_combo_box_get_active_iter (combo_box, &iter)) {
360
model = gtk_combo_box_get_model (combo_box);
365
gtk_tree_model_get (model, &iter,
369
action_container = GTK_WIDGET (gtk_builder_get_object (self->priv->builder,
370
"media_other_action_container"));
371
if (self->priv->other_application_combo != NULL) {
372
gtk_widget_destroy (self->priv->other_application_combo);
375
self->priv->other_application_combo = gtk_app_chooser_button_new (x_content_type);
376
gtk_box_pack_start (GTK_BOX (action_container), self->priv->other_application_combo, TRUE, TRUE, 0);
377
prepare_combo_box (self, self->priv->other_application_combo, NULL);
378
gtk_widget_show (self->priv->other_application_combo);
380
g_free (x_content_type);
384
on_extra_options_dialog_response (GtkWidget *dialog,
388
gtk_widget_hide (dialog);
390
if (self->priv->other_application_combo != NULL) {
391
gtk_widget_destroy (self->priv->other_application_combo);
392
self->priv->other_application_combo = NULL;
397
on_extra_options_button_clicked (GtkWidget *button,
401
GtkWidget *combo_box;
403
dialog = GTK_WIDGET (gtk_builder_get_object (self->priv->builder, "extra_options_dialog"));
404
combo_box = GTK_WIDGET (gtk_builder_get_object (self->priv->builder, "media_other_type_combobox"));
405
gtk_window_set_transient_for (GTK_WINDOW (dialog),
406
GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (self))));
407
gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
408
g_signal_connect (dialog,
410
G_CALLBACK (on_extra_options_dialog_response),
412
/* update other_application_combo */
413
other_type_combo_box_changed (GTK_COMBO_BOX (combo_box), self);
414
gtk_window_present (GTK_WINDOW (dialog));
418
media_panel_setup (CcMediaPanel *self)
421
GList *l, *content_types;
422
GtkWidget *other_type_combo_box;
423
GtkWidget *extras_button;
424
GtkListStore *other_type_list_store;
425
GtkCellRenderer *renderer;
427
GtkBuilder *builder = self->priv->builder;
430
const gchar *widget_name;
431
const gchar *content_type;
432
const gchar *heading;
434
{ "media_audio_cdda_combobox", "x-content/audio-cdda", N_("Select an application for audio CDs") },
435
{ "media_video_dvd_combobox", "x-content/video-dvd", N_("Select an application for video DVDs") },
436
{ "media_music_player_combobox", "x-content/audio-player", N_("Select an application to run when a music player is connected") },
437
{ "media_dcf_combobox", "x-content/image-dcf", N_("Select an application to run when a camera is connected") },
438
{ "media_software_combobox", "x-content/software", N_("Select an application for software CDs") },
442
const gchar *content_type;
443
const gchar *description;
444
} const other_defs[] = {
445
/* translators: these strings are duplicates of shared-mime-info
446
* strings, just here to fix capitalization of the English originals.
447
* If the shared-mime-info translation works for your language,
448
* simply leave these untranslated.
450
{ "x-content/audio-dvd", N_("audio DVD") },
451
{ "x-content/blank-bd", N_("blank Blu-ray disc") },
452
{ "x-content/blank-cd", N_("blank CD disc") },
453
{ "x-content/blank-dvd", N_("blank DVD disc") },
454
{ "x-content/blank-hddvd", N_("blank HD DVD disc") },
455
{ "x-content/video-bluray", N_("Blu-ray video disc") },
456
{ "x-content/ebook-reader", N_("e-book reader") },
457
{ "x-content/video-hddvd", N_("HD DVD video disc") },
458
{ "x-content/image-picturecd", N_("Picture CD") },
459
{ "x-content/video-svcd", N_("Super Video CD") },
460
{ "x-content/video-vcd", N_("Video CD") }
463
for (n = 0; n < G_N_ELEMENTS (defs); n++) {
464
prepare_combo_box (self,
465
GTK_WIDGET (gtk_builder_get_object (builder, defs[n].widget_name)),
469
other_type_combo_box = GTK_WIDGET (gtk_builder_get_object (builder, "media_other_type_combobox"));
471
other_type_list_store = gtk_list_store_new (3,
476
gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (other_type_list_store),
477
1, GTK_SORT_ASCENDING);
480
content_types = g_content_types_get_registered ();
482
for (l = content_types; l != NULL; l = l->next) {
483
char *content_type = l->data;
484
char *description = NULL;
487
if (!g_str_has_prefix (content_type, "x-content/"))
490
for (n = 0; n < G_N_ELEMENTS (defs); n++) {
491
if (g_content_type_is_a (content_type, defs[n].content_type)) {
496
for (n = 0; n < G_N_ELEMENTS (other_defs); n++) {
497
if (strcmp (content_type, other_defs[n].content_type) == 0) {
498
const gchar *s = other_defs[n].description;
500
description = g_content_type_get_description (content_type);
502
description = g_strdup (_(s));
508
gtk_list_store_append (other_type_list_store, &iter);
509
icon = g_content_type_get_icon (content_type);
511
gtk_list_store_set (other_type_list_store, &iter,
516
g_free (description);
517
g_object_unref (icon);
522
g_list_free_full (content_types, g_free);
524
gtk_combo_box_set_model (GTK_COMBO_BOX (other_type_combo_box),
525
GTK_TREE_MODEL (other_type_list_store));
527
renderer = gtk_cell_renderer_pixbuf_new ();
528
gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (other_type_combo_box), renderer, FALSE);
529
gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (other_type_combo_box), renderer,
533
renderer = gtk_cell_renderer_text_new ();
534
gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (other_type_combo_box), renderer, TRUE);
535
gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (other_type_combo_box), renderer,
539
g_signal_connect (other_type_combo_box,
541
G_CALLBACK (other_type_combo_box_changed),
544
gtk_combo_box_set_active (GTK_COMBO_BOX (other_type_combo_box), 0);
546
extras_button = GTK_WIDGET (gtk_builder_get_object (builder, "extra_options_button"));
547
g_signal_connect (extras_button,
549
G_CALLBACK (on_extra_options_button_clicked),
552
g_settings_bind (self->priv->preferences,
553
PREF_MEDIA_AUTORUN_NEVER,
554
gtk_builder_get_object (self->priv->builder, "media_autorun_never_checkbutton"),
556
G_SETTINGS_BIND_DEFAULT);
558
g_settings_bind (self->priv->preferences,
559
PREF_MEDIA_AUTORUN_NEVER,
560
GTK_WIDGET (gtk_builder_get_object (self->priv->builder, "media_handling_vbox")),
562
G_SETTINGS_BIND_INVERT_BOOLEAN);
566
cc_media_panel_init (CcMediaPanel *self)
568
CcMediaPanelPrivate *priv;
569
GtkWidget *main_widget;
572
priv = self->priv = MEDIA_PANEL_PRIVATE (self);
573
priv->builder = gtk_builder_new ();
574
priv->preferences = g_settings_new ("org.gnome.desktop.media-handling");
576
res = gtk_builder_add_from_file (priv->builder, GNOMECC_UI_DIR "/gnome-media-properties.ui", NULL);
580
g_critical ("Unable to load the UI file!");
583
media_panel_setup (self);
585
main_widget = GTK_WIDGET (gtk_builder_get_object (priv->builder,
586
"media_preferences_vbox"));
587
gtk_widget_reparent (main_widget, (GtkWidget *) self);
591
cc_media_panel_register (GIOModule *module)
593
cc_media_panel_register_type (G_TYPE_MODULE (module));
594
g_io_extension_point_implement (CC_SHELL_PANEL_EXTENSION_POINT,