GCC Code Coverage Report


Directory: ./
File: panels/applications/cc-removable-media-settings.c
Date: 2024-05-04 07:58:27
Exec Total Coverage
Lines: 0 226 0.0%
Functions: 0 20 0.0%
Branches: 0 131 0.0%

Line Branch Exec Source
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2 *
3 * Copyright (C) 2017 Mohammed Sadiq <sadiq@sadiqpk.org>
4 * Copyright (C) 2010 Red Hat, Inc
5 * Copyright (C) 2008 William Jon McCann <jmccann@redhat.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22 #include <config.h>
23
24 #include <glib.h>
25 #include <glib/gi18n.h>
26
27 #include <shell/cc-panel.h>
28
29 #include "cc-applications-panel.h"
30 #include "cc-removable-media-settings.h"
31
32 /* Autorun options */
33 #define PREF_MEDIA_AUTORUN_NEVER "autorun-never"
34 #define PREF_MEDIA_AUTORUN_X_CONTENT_START_APP "autorun-x-content-start-app"
35 #define PREF_MEDIA_AUTORUN_X_CONTENT_IGNORE "autorun-x-content-ignore"
36 #define PREF_MEDIA_AUTORUN_X_CONTENT_OPEN_FOLDER "autorun-x-content-open-folder"
37
38 #define CUSTOM_ITEM_ASK "cc-item-ask"
39 #define CUSTOM_ITEM_DO_NOTHING "cc-item-do-nothing"
40 #define CUSTOM_ITEM_OPEN_FOLDER "cc-item-open-folder"
41
42 #define MEDIA_HANDLING_SCHEMA "org.gnome.desktop.media-handling"
43
44 struct _CcRemovableMediaSettings
45 {
46 AdwPreferencesGroup parent;
47
48 GtkWidget *cd_audio_row;
49 GtkWidget *dvd_video_row;
50 GtkWidget *music_player_row;
51 GtkWidget *photos_row;
52 GtkWidget *software_row;
53 GtkWidget *other_media_row;
54
55 GtkAppChooserButton *audio_cdda_chooser;
56 GtkAppChooserButton *dcf_chooser;
57 GtkAppChooserButton *music_player_chooser;
58 AdwDialog *other_type_dialog;
59 AdwActionRow *other_action_row;
60 GtkBox *other_action_box;
61 GtkComboBox *other_type_combo_box;
62 GtkListStore *other_type_list_store;
63 GtkAppChooserButton *software_chooser;
64 GtkAppChooserButton *video_dvd_chooser;
65
66 GtkAppChooserButton *other_application_chooser;
67 GSettings *settings;
68 };
69
70
71 G_DEFINE_TYPE (CcRemovableMediaSettings, cc_removable_media_settings, ADW_TYPE_PREFERENCES_GROUP)
72
73 static char **
74 remove_elem_from_str_array (char **v,
75 const char *s)
76 {
77 GPtrArray *array;
78 guint idx;
79
80 array = g_ptr_array_new ();
81
82 for (idx = 0; v[idx] != NULL; idx++) {
83 if (g_strcmp0 (v[idx], s) == 0) {
84 continue;
85 }
86
87 g_ptr_array_add (array, v[idx]);
88 }
89
90 g_ptr_array_add (array, NULL);
91
92 g_free (v);
93
94 return (char **) g_ptr_array_free (array, FALSE);
95 }
96
97 static char **
98 add_elem_to_str_array (char **v,
99 const char *s)
100 {
101 GPtrArray *array;
102 guint idx;
103
104 array = g_ptr_array_new ();
105
106 for (idx = 0; v[idx] != NULL; idx++) {
107 g_ptr_array_add (array, v[idx]);
108 }
109
110 g_ptr_array_add (array, g_strdup (s));
111 g_ptr_array_add (array, NULL);
112
113 g_free (v);
114
115 return (char **) g_ptr_array_free (array, FALSE);
116 }
117
118 static void
119 autorun_get_preferences (CcRemovableMediaSettings *self,
120 const char *x_content_type,
121 gboolean *pref_start_app,
122 gboolean *pref_ignore,
123 gboolean *pref_open_folder)
124 {
125 g_auto(GStrv) x_content_start_app = NULL;
126 g_auto(GStrv) x_content_ignore = NULL;
127 g_auto(GStrv) x_content_open_folder = NULL;
128
129 g_return_if_fail (pref_start_app != NULL);
130 g_return_if_fail (pref_ignore != NULL);
131 g_return_if_fail (pref_open_folder != NULL);
132
133 *pref_start_app = FALSE;
134 *pref_ignore = FALSE;
135 *pref_open_folder = FALSE;
136 x_content_start_app = g_settings_get_strv (self->settings,
137 PREF_MEDIA_AUTORUN_X_CONTENT_START_APP);
138 x_content_ignore = g_settings_get_strv (self->settings,
139 PREF_MEDIA_AUTORUN_X_CONTENT_IGNORE);
140 x_content_open_folder = g_settings_get_strv (self->settings,
141 PREF_MEDIA_AUTORUN_X_CONTENT_OPEN_FOLDER);
142 if (x_content_start_app != NULL) {
143 *pref_start_app = g_strv_contains ((const gchar * const *) x_content_start_app, x_content_type);
144 }
145 if (x_content_ignore != NULL) {
146 *pref_ignore = g_strv_contains ((const gchar * const *) x_content_ignore, x_content_type);
147 }
148 if (x_content_open_folder != NULL) {
149 *pref_open_folder = g_strv_contains ((const gchar * const *) x_content_open_folder, x_content_type);
150 }
151 }
152
153 static void
154 autorun_set_preferences (CcRemovableMediaSettings *self,
155 const char *x_content_type,
156 gboolean pref_start_app,
157 gboolean pref_ignore,
158 gboolean pref_open_folder)
159 {
160 g_auto(GStrv) x_content_start_app = NULL;
161 g_auto(GStrv) x_content_ignore = NULL;
162 g_auto(GStrv) x_content_open_folder = NULL;
163
164 g_assert (x_content_type != NULL);
165
166 x_content_start_app = g_settings_get_strv (self->settings,
167 PREF_MEDIA_AUTORUN_X_CONTENT_START_APP);
168 x_content_ignore = g_settings_get_strv (self->settings,
169 PREF_MEDIA_AUTORUN_X_CONTENT_IGNORE);
170 x_content_open_folder = g_settings_get_strv (self->settings,
171 PREF_MEDIA_AUTORUN_X_CONTENT_OPEN_FOLDER);
172
173 x_content_start_app = remove_elem_from_str_array (x_content_start_app, x_content_type);
174 if (pref_start_app) {
175 x_content_start_app = add_elem_to_str_array (x_content_start_app, x_content_type);
176 }
177 g_settings_set_strv (self->settings,
178 PREF_MEDIA_AUTORUN_X_CONTENT_START_APP, (const gchar * const*) x_content_start_app);
179
180 x_content_ignore = remove_elem_from_str_array (x_content_ignore, x_content_type);
181 if (pref_ignore) {
182 x_content_ignore = add_elem_to_str_array (x_content_ignore, x_content_type);
183 }
184 g_settings_set_strv (self->settings,
185 PREF_MEDIA_AUTORUN_X_CONTENT_IGNORE, (const gchar * const*) x_content_ignore);
186
187 x_content_open_folder = remove_elem_from_str_array (x_content_open_folder, x_content_type);
188 if (pref_open_folder) {
189 x_content_open_folder = add_elem_to_str_array (x_content_open_folder, x_content_type);
190 }
191 g_settings_set_strv (self->settings,
192 PREF_MEDIA_AUTORUN_X_CONTENT_OPEN_FOLDER, (const gchar * const*) x_content_open_folder);
193
194 }
195
196 static void
197 on_custom_item_activated_cb (CcRemovableMediaSettings *self,
198 const gchar *item,
199 GtkAppChooser *app_chooser)
200 {
201 g_autofree gchar *content_type = NULL;
202
203 content_type = gtk_app_chooser_get_content_type (app_chooser);
204
205 if (g_strcmp0 (item, CUSTOM_ITEM_ASK) == 0) {
206 autorun_set_preferences (self, content_type,
207 FALSE, FALSE, FALSE);
208 } else if (g_strcmp0 (item, CUSTOM_ITEM_OPEN_FOLDER) == 0) {
209 autorun_set_preferences (self, content_type,
210 FALSE, FALSE, TRUE);
211 } else if (g_strcmp0 (item, CUSTOM_ITEM_DO_NOTHING) == 0) {
212 autorun_set_preferences (self, content_type,
213 FALSE, TRUE, FALSE);
214 }
215 }
216
217 static void
218 on_chooser_changed_cb (CcRemovableMediaSettings *self,
219 GtkAppChooser *chooser)
220 {
221 g_autoptr(GAppInfo) info = NULL;
222 g_autofree gchar *content_type = NULL;
223
224 info = gtk_app_chooser_get_app_info (chooser);
225
226 if (info == NULL)
227 return;
228
229 content_type = gtk_app_chooser_get_content_type (chooser);
230 autorun_set_preferences (self, content_type,
231 TRUE, FALSE, FALSE);
232 g_app_info_set_as_default_for_type (info, content_type, NULL);
233 }
234
235 /* FIXME: Port away from GtkAppChooserButton entirely */
236 static void
237 ellipsize_app_chooser (GtkAppChooserButton *button)
238 {
239 GtkWidget *child;
240 g_autoptr (GList) cells = NULL;
241 GtkCellRenderer *renderer;
242
243 g_assert (GTK_IS_APP_CHOOSER_BUTTON (button));
244
245 child = gtk_widget_get_first_child (GTK_WIDGET (button));
246
247 g_assert (GTK_IS_CELL_LAYOUT (child));
248
249 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (child));
250
251 g_assert (g_list_length (cells) > 0);
252
253 renderer = g_list_last (cells)->data;
254
255 g_assert (GTK_IS_CELL_RENDERER_TEXT (renderer));
256
257 g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
258 }
259
260 static void
261 prepare_chooser (CcRemovableMediaSettings *self,
262 GtkAppChooserButton *button,
263 const gchar *heading)
264 {
265 gboolean pref_ask;
266 gboolean pref_start_app;
267 gboolean pref_ignore;
268 gboolean pref_open_folder;
269 g_autoptr(GAppInfo) info = NULL;
270 g_autofree gchar *content_type = NULL;
271
272 content_type = gtk_app_chooser_get_content_type (GTK_APP_CHOOSER (button));
273
274 /* fetch preferences for this content type */
275 autorun_get_preferences (self, content_type,
276 &pref_start_app, &pref_ignore, &pref_open_folder);
277 pref_ask = !pref_start_app && !pref_ignore && !pref_open_folder;
278
279 info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (button));
280
281 /* append the separator only if we have >= 1 apps in the chooser */
282 if (info != NULL) {
283 gtk_app_chooser_button_append_separator (button);
284 }
285
286 gtk_app_chooser_button_append_custom_item (button, CUSTOM_ITEM_ASK,
287 _("Ask what to do"),
288 NULL);
289
290 gtk_app_chooser_button_append_custom_item (button, CUSTOM_ITEM_DO_NOTHING,
291 _("Do nothing"),
292 NULL);
293
294 gtk_app_chooser_button_append_custom_item (button, CUSTOM_ITEM_OPEN_FOLDER,
295 _("Open folder"),
296 NULL);
297
298 gtk_app_chooser_button_set_show_dialog_item (button, TRUE);
299
300 if (heading)
301 gtk_app_chooser_button_set_heading (button, _(heading));
302
303 if (pref_ask) {
304 gtk_app_chooser_button_set_active_custom_item (button, CUSTOM_ITEM_ASK);
305 } else if (pref_ignore) {
306 gtk_app_chooser_button_set_active_custom_item (button, CUSTOM_ITEM_DO_NOTHING);
307 } else if (pref_open_folder) {
308 gtk_app_chooser_button_set_active_custom_item (button, CUSTOM_ITEM_OPEN_FOLDER);
309 }
310
311 g_signal_connect_object (button, "changed",
312 G_CALLBACK (on_chooser_changed_cb), self, G_CONNECT_SWAPPED);
313 g_signal_connect_object (button, "custom-item-activated",
314 G_CALLBACK (on_custom_item_activated_cb), self, G_CONNECT_SWAPPED);
315
316 ellipsize_app_chooser (button);
317 }
318
319 static void
320 on_other_type_combo_box_changed (CcRemovableMediaSettings *self)
321 {
322 GtkTreeIter iter;
323 g_autofree gchar *x_content_type = NULL;
324
325 if (!gtk_combo_box_get_active_iter (self->other_type_combo_box, &iter)) {
326 return;
327 }
328
329 gtk_tree_model_get (GTK_TREE_MODEL (self->other_type_list_store), &iter,
330 1, &x_content_type,
331 -1);
332
333 if (self->other_application_chooser != NULL) {
334 gtk_box_remove (self->other_action_box, GTK_WIDGET (self->other_application_chooser));
335 self->other_application_chooser = NULL;
336 }
337
338 self->other_application_chooser = GTK_APP_CHOOSER_BUTTON (gtk_app_chooser_button_new (x_content_type));
339 gtk_box_append (self->other_action_box, GTK_WIDGET (self->other_application_chooser));
340 prepare_chooser (self, self->other_application_chooser, NULL);
341
342 adw_action_row_set_activatable_widget (self->other_action_row, GTK_WIDGET (self->other_application_chooser));
343 }
344
345 static gboolean
346 on_extra_options_dialog_close_attempt (CcRemovableMediaSettings *self)
347 {
348 gtk_widget_set_visible (GTK_WIDGET (self->other_type_dialog), FALSE);
349
350 if (self->other_application_chooser != NULL) {
351 gtk_box_remove (self->other_action_box, GTK_WIDGET (self->other_application_chooser));
352 self->other_application_chooser = NULL;
353 }
354
355 return GDK_EVENT_PROPAGATE;
356 }
357
358 static void
359 on_extra_options_button_clicked (CcRemovableMediaSettings *self)
360 {
361 /* update other_application_chooser */
362 on_other_type_combo_box_changed (self);
363 adw_dialog_present (self->other_type_dialog, GTK_WIDGET (self));
364 }
365
366 #define OFFSET(x) (G_STRUCT_OFFSET (CcRemovableMediaSettings, x))
367 #define WIDGET_FROM_OFFSET(x) (G_STRUCT_MEMBER (GtkWidget*, self, x))
368
369 static void
370 info_panel_setup_media (CcRemovableMediaSettings *self)
371 {
372 guint n;
373 GList *l, *content_types;
374 GtkTreeIter iter;
375
376 struct {
377 gint widget_offset;
378 const gchar *content_type;
379 const gchar *heading;
380 } const defs[] = {
381 { OFFSET (audio_cdda_chooser), "x-content/audio-cdda", N_("Select an app for audio CDs") },
382 { OFFSET (video_dvd_chooser), "x-content/video-dvd", N_("Select an app for video DVDs") },
383 { OFFSET (music_player_chooser), "x-content/audio-player", N_("Select an app to run when a music player is connected") },
384 { OFFSET (dcf_chooser), "x-content/image-dcf", N_("Select an app to run when a camera is connected") },
385 { OFFSET (software_chooser), "x-content/unix-software", N_("Select an app for software CDs") },
386 };
387
388 struct {
389 const gchar *content_type;
390 const gchar *description;
391 } const other_defs[] = {
392 /* translators: these strings are duplicates of shared-mime-info
393 * strings, just here to fix capitalization of the English originals.
394 * If the shared-mime-info translation works for your language,
395 * simply leave these untranslated.
396 */
397 { "x-content/audio-dvd", N_("audio DVD") },
398 { "x-content/blank-bd", N_("blank Blu-ray disc") },
399 { "x-content/blank-cd", N_("blank CD disc") },
400 { "x-content/blank-dvd", N_("blank DVD disc") },
401 { "x-content/blank-hddvd", N_("blank HD DVD disc") },
402 { "x-content/video-bluray", N_("Blu-ray video disc") },
403 { "x-content/ebook-reader", N_("e-book reader") },
404 { "x-content/video-hddvd", N_("HD DVD video disc") },
405 { "x-content/image-picturecd", N_("Picture CD") },
406 { "x-content/video-svcd", N_("Super Video CD") },
407 { "x-content/video-vcd", N_("Video CD") },
408 { "x-content/win32-software", N_("Windows software") },
409 };
410
411 for (n = 0; n < G_N_ELEMENTS (defs); n++) {
412 prepare_chooser (self,
413 GTK_APP_CHOOSER_BUTTON (WIDGET_FROM_OFFSET (defs[n].widget_offset)),
414 defs[n].heading);
415 }
416
417 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (self->other_type_list_store),
418 1, GTK_SORT_ASCENDING);
419
420
421 content_types = g_content_types_get_registered ();
422
423 for (l = content_types; l != NULL; l = l->next) {
424 char *content_type = l->data;
425 g_autofree char *description = NULL;
426
427 if (!g_str_has_prefix (content_type, "x-content/"))
428 continue;
429
430 for (n = 0; n < G_N_ELEMENTS (defs); n++) {
431 if (g_content_type_is_a (content_type, defs[n].content_type)) {
432 goto skip;
433 }
434 }
435
436 for (n = 0; n < G_N_ELEMENTS (other_defs); n++) {
437 if (strcmp (content_type, other_defs[n].content_type) == 0) {
438 const gchar *s = other_defs[n].description;
439 if (s == _(s))
440 description = g_content_type_get_description (content_type);
441 else
442 description = g_strdup (_(s));
443
444 break;
445 }
446 }
447
448 if (description == NULL) {
449 g_debug ("Content type '%s' is missing from the info panel", content_type);
450 description = g_content_type_get_description (content_type);
451 }
452
453 gtk_list_store_append (self->other_type_list_store, &iter);
454
455 gtk_list_store_set (self->other_type_list_store, &iter,
456 0, description,
457 1, content_type,
458 -1);
459 skip:
460 ;
461 }
462
463 g_list_free_full (content_types, g_free);
464
465 gtk_combo_box_set_active (self->other_type_combo_box, 0);
466
467 g_settings_bind (self->settings,
468 PREF_MEDIA_AUTORUN_NEVER,
469 self->cd_audio_row,
470 "sensitive",
471 G_SETTINGS_BIND_INVERT_BOOLEAN);
472
473 g_settings_bind (self->settings,
474 PREF_MEDIA_AUTORUN_NEVER,
475 self->dvd_video_row,
476 "sensitive",
477 G_SETTINGS_BIND_INVERT_BOOLEAN);
478
479 g_settings_bind (self->settings,
480 PREF_MEDIA_AUTORUN_NEVER,
481 self->music_player_row,
482 "sensitive",
483 G_SETTINGS_BIND_INVERT_BOOLEAN);
484
485 g_settings_bind (self->settings,
486 PREF_MEDIA_AUTORUN_NEVER,
487 self->photos_row,
488 "sensitive",
489 G_SETTINGS_BIND_INVERT_BOOLEAN);
490
491 g_settings_bind (self->settings,
492 PREF_MEDIA_AUTORUN_NEVER,
493 self->software_row,
494 "sensitive",
495 G_SETTINGS_BIND_INVERT_BOOLEAN);
496
497 g_settings_bind (self->settings,
498 PREF_MEDIA_AUTORUN_NEVER,
499 self->other_media_row,
500 "sensitive",
501 G_SETTINGS_BIND_INVERT_BOOLEAN);
502 }
503
504
505 static void
506 cc_removable_media_settings_finalize (GObject *object)
507 {
508 CcRemovableMediaSettings *self = CC_REMOVABLE_MEDIA_SETTINGS (object);
509
510 g_clear_object (&self->settings);
511
512 G_OBJECT_CLASS (cc_removable_media_settings_parent_class)->finalize (object);
513 }
514
515 static void
516 cc_removable_media_settings_dispose (GObject *object)
517 {
518 CcRemovableMediaSettings *self = CC_REMOVABLE_MEDIA_SETTINGS (object);
519
520 g_clear_pointer ((AdwDialog **) &self->other_type_dialog, adw_dialog_force_close);
521
522 G_OBJECT_CLASS (cc_removable_media_settings_parent_class)->dispose (object);
523 }
524
525 static void
526 cc_removable_media_settings_class_init (CcRemovableMediaSettingsClass *klass)
527 {
528 GObjectClass *object_class = G_OBJECT_CLASS (klass);
529 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
530
531 object_class->finalize = cc_removable_media_settings_finalize;
532 object_class->dispose = cc_removable_media_settings_dispose;
533
534 gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/control-center/applications/cc-removable-media-settings.ui");
535
536 gtk_widget_class_bind_template_child (widget_class, CcRemovableMediaSettings, audio_cdda_chooser);
537 gtk_widget_class_bind_template_child (widget_class, CcRemovableMediaSettings, dcf_chooser);
538 gtk_widget_class_bind_template_child (widget_class, CcRemovableMediaSettings, music_player_chooser);
539 gtk_widget_class_bind_template_child (widget_class, CcRemovableMediaSettings, other_type_dialog);
540 gtk_widget_class_bind_template_child (widget_class, CcRemovableMediaSettings, other_action_row);
541 gtk_widget_class_bind_template_child (widget_class, CcRemovableMediaSettings, other_action_box);
542 gtk_widget_class_bind_template_child (widget_class, CcRemovableMediaSettings, other_type_combo_box);
543 gtk_widget_class_bind_template_child (widget_class, CcRemovableMediaSettings, other_type_list_store);
544 gtk_widget_class_bind_template_child (widget_class, CcRemovableMediaSettings, software_chooser);
545 gtk_widget_class_bind_template_child (widget_class, CcRemovableMediaSettings, video_dvd_chooser);
546 gtk_widget_class_bind_template_child (widget_class, CcRemovableMediaSettings, cd_audio_row);
547 gtk_widget_class_bind_template_child (widget_class, CcRemovableMediaSettings, dvd_video_row);
548 gtk_widget_class_bind_template_child (widget_class, CcRemovableMediaSettings, music_player_row);
549 gtk_widget_class_bind_template_child (widget_class, CcRemovableMediaSettings, photos_row);
550 gtk_widget_class_bind_template_child (widget_class, CcRemovableMediaSettings, software_row);
551 gtk_widget_class_bind_template_child (widget_class, CcRemovableMediaSettings, other_media_row);
552
553 gtk_widget_class_bind_template_callback (widget_class, on_extra_options_dialog_close_attempt);
554 gtk_widget_class_bind_template_callback (widget_class, on_extra_options_button_clicked);
555 gtk_widget_class_bind_template_callback (widget_class, on_other_type_combo_box_changed);
556 }
557
558 static void
559 cc_removable_media_settings_init (CcRemovableMediaSettings *self)
560 {
561 gtk_widget_init_template (GTK_WIDGET (self));
562 self->settings = g_settings_new (MEDIA_HANDLING_SCHEMA);
563
564 info_panel_setup_media (self);
565 }
566
567 CcRemovableMediaSettings *
568 cc_removable_media_settings_new (void)
569 {
570 return g_object_new (CC_TYPE_REMOVABLE_MEDIA_SETTINGS,
571 NULL);
572 }
573