Debian Codegraph @

libbrasero-media/brasero-drive-selection.c

raw symbols SCIP
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /*
3 * Libbrasero-media
4 * Copyright (C) Philippe Rouquier 2005-2009 <[email protected]>
5 *
6 * Libbrasero-media 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.
10 *
11 * The Libbrasero-media authors hereby grant permission for non-GPL compatible
12 * GStreamer plugins to be used and distributed together with GStreamer
13 * and Libbrasero-media. This permission is above and beyond the permissions granted
14 * by the GPL license by which Libbrasero-media is covered. If you modify this code
15 * you may extend this exception to your version of the code, but you are not
16 * obligated to do so. If you do not wish to do so, delete this exception
17 * statement from your version.
18 *
19 * Libbrasero-media is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to:
26 * The Free Software Foundation, Inc.,
27 * 51 Franklin Street, Fifth Floor
28 * Boston, MA 02110-1301, USA.
29 */
30
31 #ifdef HAVE_CONFIG_H
32 # include <config.h>
33 #endif
34
35 #include <glib.h>
36 #include <glib-object.h>
37 #include <glib/gi18n-lib.h>
38
39 #include <gtk/gtk.h>
40
41 #include "brasero-drive-selection.h"
42 #include "brasero-medium-monitor.h"
43 #include "brasero-drive.h"
44 #include "brasero-units.h"
45
46 typedef struct _BraseroDriveSelectionPrivate BraseroDriveSelectionPrivate;
47 struct _BraseroDriveSelectionPrivate
48 {
49 BraseroDrive *active;
50
51 BraseroDriveType type;
52 gulong added_sig;
53 gulong removed_sig;
54 };
55
56 #define BRASERO_DRIVE_SELECTION_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), BRASERO_TYPE_DRIVE_SELECTION, BraseroDriveSelectionPrivate))
57
58 typedef enum {
59 CHANGED_SIGNAL,
60 LAST_SIGNAL
61 } BraseroDriveSelectionSignalType;
62
63 /* GtkBuildable */
64 static GtkBuildableIface *parent_buildable_iface;
65
66 static guint brasero_drive_selection_signals [LAST_SIGNAL] = { 0 };
67
68 enum {
69 PROP_0,
70 PROP_DRIVE,
71 PROP_DRIVE_TYPE
72 };
73
74 enum {
75 DRIVE_COL,
76 NAME_COL,
77 ICON_COL,
78 NUM_COL
79 };
80
81
82 static void
83 brasero_drive_selection_buildable_init (GtkBuildableIface *iface)
84 {
85 parent_buildable_iface = g_type_interface_peek_parent (iface);
86 }
87
88 G_DEFINE_TYPE_WITH_CODE (BraseroDriveSelection, brasero_drive_selection, GTK_TYPE_COMBO_BOX, G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE, brasero_drive_selection_buildable_init));
89
90 static void
91 brasero_drive_selection_set_current_drive (BraseroDriveSelection *self,
92 GtkTreeIter *iter)
93 {
94 BraseroDriveSelectionPrivate *priv;
95 BraseroDrive *drive;
96 GtkTreeModel *model;
97
98 priv = BRASERO_DRIVE_SELECTION_PRIVATE (self);
99
100 model = gtk_combo_box_get_model (GTK_COMBO_BOX (self));
101 gtk_tree_model_get (model, iter,
102 DRIVE_COL, &drive,
103 -1);
104
105 if (drive)
106 gtk_widget_set_sensitive (GTK_WIDGET (self), TRUE);
107 else
108 gtk_widget_set_sensitive (GTK_WIDGET (self), FALSE);
109
110 if (priv->active == drive)
111 return;
112
113 if (priv->active)
114 g_object_unref (priv->active);
115
116 priv->active = drive;
117
118 if (priv->active)
119 g_object_ref (priv->active);
120
121 g_signal_emit (self,
122 brasero_drive_selection_signals [CHANGED_SIGNAL],
123 0,
124 priv->active);
125 }
126
127 static void
128 brasero_drive_selection_changed (GtkComboBox *combo)
129 {
130 GtkTreeIter iter;
131
132 if (!gtk_combo_box_get_active_iter (combo, &iter))
133 return;
134
135 brasero_drive_selection_set_current_drive (BRASERO_DRIVE_SELECTION (combo), &iter);
136 }
137
138 /**
139 * brasero_drive_selection_set_active:
140 * @selector: a #BraseroDriveSelection
141 * @drive: a #BraseroDrive to set as the active one in the selector
142 *
143 * Sets the active drive. Emits the ::drive-changed signal.
144 *
145 * Return value: a #gboolean. TRUE if it succeeded, FALSE otherwise.
146 **/
147 gboolean
148 brasero_drive_selection_set_active (BraseroDriveSelection *selector,
149 BraseroDrive *drive)
150 {
151 BraseroDriveSelectionPrivate *priv;
152 gboolean result = FALSE;
153 GtkTreeModel *model;
154 GtkTreeIter iter;
155
156 g_return_val_if_fail (selector != NULL, FALSE);
157 g_return_val_if_fail (BRASERO_IS_DRIVE_SELECTION (selector), FALSE);
158
159 priv = BRASERO_DRIVE_SELECTION_PRIVATE (selector);
160
161 if (priv->active == drive)
162 return TRUE;
163
164 model = gtk_combo_box_get_model (GTK_COMBO_BOX (selector));
165 if (!gtk_tree_model_get_iter_first (model, &iter))
166 return FALSE;
167
168 do {
169 BraseroDrive *iter_drive;
170
171 gtk_tree_model_get (model, &iter,
172 DRIVE_COL, &iter_drive,
173 -1);
174
175 if (drive == iter_drive) {
176 if (iter_drive)
177 g_object_unref (iter_drive);
178
179 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (selector), &iter);
180 brasero_drive_selection_set_current_drive (selector, &iter);
181 result = TRUE;
182 break;
183 }
184
185 g_object_unref (iter_drive);
186 } while (gtk_tree_model_iter_next (model, &iter));
187
188 return result;
189 }
190
191 /**
192 * brasero_drive_selection_get_active:
193 * @selector: a #BraseroDriveSelection
194 *
195 * Gets the active drive.
196 *
197 * Return value: a #BraseroDrive or NULL. Unref when it is not needed anymore.
198 **/
199 BraseroDrive *
200 brasero_drive_selection_get_active (BraseroDriveSelection *selector)
201 {
202 BraseroDriveSelectionPrivate *priv;
203
204 g_return_val_if_fail (selector != NULL, NULL);
205 g_return_val_if_fail (BRASERO_IS_DRIVE_SELECTION (selector), NULL);
206
207 priv = BRASERO_DRIVE_SELECTION_PRIVATE (selector);
208 if (!priv->active)
209 return NULL;
210
211 return g_object_ref (priv->active);
212 }
213
214 static void
215 brasero_drive_selection_update_no_disc_entry (BraseroDriveSelection *self,
216 GtkTreeModel *model,
217 GtkTreeIter *iter)
218 {
219 GIcon *icon;
220
221 icon = g_themed_icon_new_with_default_fallbacks ("drive-optical");
222
223 /* FIXME: that needs a string */
224 gtk_list_store_set (GTK_LIST_STORE (model), iter,
225 NAME_COL, NULL,
226 ICON_COL, NULL,
227 -1);
228
229 g_object_unref (icon);
230
231 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (self), iter);
232 brasero_drive_selection_set_current_drive (self, iter);
233 }
234
235 static void
236 brasero_drive_selection_add_no_disc_entry (BraseroDriveSelection *self)
237 {
238 GtkTreeIter iter;
239 GtkTreeModel *model;
240
241 /* Nothing's available. Say it. Two cases here, either we're
242 * still probing drives or there isn't actually any available
243 * drive. */
244 model = gtk_combo_box_get_model (GTK_COMBO_BOX (self));
245 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
246 brasero_drive_selection_update_no_disc_entry (self, model, &iter);
247 }
248
249 /**
250 * brasero_drive_selection_show_type:
251 * @selector: a #BraseroDriveSelection
252 * @type: a #BraseroDriveType
253 *
254 * Filters and displays drive corresponding to @type.
255 *
256 **/
257 void
258 brasero_drive_selection_show_type (BraseroDriveSelection *selector,
259 BraseroDriveType type)
260 {
261 BraseroDriveSelectionPrivate *priv;
262 BraseroMediumMonitor *monitor;
263 GtkTreeModel *model;
264 GtkTreeIter iter;
265 GSList *list;
266 GSList *item;
267
268 g_return_if_fail (selector != NULL);
269 g_return_if_fail (BRASERO_IS_DRIVE_SELECTION (selector));
270
271 priv = BRASERO_DRIVE_SELECTION_PRIVATE (selector);
272
273 priv->type = type;
274
275 monitor = brasero_medium_monitor_get_default ();
276 list = brasero_medium_monitor_get_drives (monitor, type);
277 g_object_unref (monitor);
278
279 model = gtk_combo_box_get_model (GTK_COMBO_BOX (selector));
280 if (gtk_tree_model_get_iter_first (model, &iter)) {
281 /* First filter */
282 do {
283 GSList *node;
284 BraseroDrive *drive;
285
286 gtk_tree_model_get (model, &iter,
287 DRIVE_COL, &drive,
288 -1);
289
290 if (!drive) {
291 /* That's the dummy line saying there isn't any
292 * available drive for whatever action it is */
293 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
294 break;
295 }
296
297 node = g_slist_find (list, drive);
298 g_object_unref (drive);
299
300 if (!node) {
301 if (gtk_list_store_remove (GTK_LIST_STORE (model), &iter))
302 continue;
303
304 /* no more iter in the tree get out */
305 break;
306 }
307
308 g_object_unref (node->data);
309 list = g_slist_delete_link (list, node);
310 } while (gtk_tree_model_iter_next (model, &iter));
311 }
312
313 if (list) {
314 /* add remaining drive */
315 for (item = list; item; item = item->next) {
316 gchar *drive_name;
317 BraseroDrive *drive;
318 GIcon *drive_icon = NULL;
319
320 drive = item->data;
321
322 drive_name = brasero_drive_get_display_name (drive);
323
324 if (!brasero_drive_is_fake (drive)) {
325 GDrive *gdrive;
326
327 gdrive = brasero_drive_get_gdrive (drive);
328 if (gdrive) {
329 drive_icon = g_drive_get_icon (gdrive);
330 g_object_unref (gdrive);
331 }
332 else
333 drive_icon = g_themed_icon_new_with_default_fallbacks ("drive-optical");
334 }
335 else
336 drive_icon = g_themed_icon_new_with_default_fallbacks ("iso-image-new");
337
338 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
339 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
340 DRIVE_COL, drive,
341 NAME_COL, drive_name?drive_name:_("Unnamed CD/DVD Drive"),
342 ICON_COL, drive_icon,
343 -1);
344 g_free (drive_name);
345 g_object_unref (drive_icon);
346 }
347 g_slist_foreach (list, (GFunc) g_object_unref, NULL);
348 g_slist_free (list);
349 }
350
351 if (!gtk_tree_model_get_iter_first (model, &iter)) {
352 brasero_drive_selection_add_no_disc_entry (selector);
353 return;
354 }
355
356 gtk_widget_set_sensitive (GTK_WIDGET (selector), TRUE);
357 if (gtk_combo_box_get_active (GTK_COMBO_BOX (selector)) == -1) {
358 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (selector), &iter);
359 brasero_drive_selection_set_current_drive (selector, &iter);
360 }
361 }
362
363 static void
364 brasero_drive_selection_drive_added_cb (BraseroMediumMonitor *monitor,
365 BraseroDrive *drive,
366 BraseroDriveSelection *self)
367 {
368 BraseroDriveSelectionPrivate *priv;
369 gchar *drive_name = NULL;
370 gboolean add = FALSE;
371 GtkTreeModel *model;
372 GIcon *drive_icon;
373 GtkTreeIter iter;
374
375 priv = BRASERO_DRIVE_SELECTION_PRIVATE (self);
376
377 if ((priv->type & BRASERO_DRIVE_TYPE_WRITER)
378 && (brasero_drive_can_write (drive)))
379 add = TRUE;
380 else if (priv->type & BRASERO_DRIVE_TYPE_READER)
381 add = TRUE;
382
383 model = gtk_combo_box_get_model (GTK_COMBO_BOX (self));
384
385 if (!add) {
386 /* Try to get the first iter (it shouldn't fail) */
387 if (!gtk_tree_model_get_iter_first (model, &iter)) {
388 brasero_drive_selection_add_no_disc_entry (self);
389 return;
390 }
391
392 /* See if that's a real drive or not; if so, return. */
393 drive = NULL;
394 gtk_tree_model_get (model, &iter,
395 DRIVE_COL, &drive,
396 -1);
397 if (drive)
398 return;
399
400 brasero_drive_selection_update_no_disc_entry (self, model, &iter);
401 return;
402 }
403
404 /* remove warning message */
405 if (gtk_tree_model_get_iter_first (model, &iter)) {
406 BraseroDrive *tmp;
407
408 gtk_tree_model_get (model, &iter,
409 DRIVE_COL, &tmp,
410 -1);
411 if (!tmp)
412 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
413 else
414 g_object_unref (tmp);
415 }
416
417 if (!brasero_drive_is_fake (drive)) {
418 GDrive *gdrive;
419
420 gdrive = brasero_drive_get_gdrive (drive);
421 if (gdrive) {
422 drive_icon = g_drive_get_icon (gdrive);
423 g_object_unref (gdrive);
424 }
425 else
426 drive_icon = g_themed_icon_new_with_default_fallbacks ("drive-optical");
427 }
428 else
429 drive_icon = g_themed_icon_new_with_default_fallbacks ("iso-image-new");
430
431 drive_name = brasero_drive_get_display_name (drive);
432
433 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
434 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
435 DRIVE_COL, drive,
436 NAME_COL, drive_name?drive_name:_("Unnamed CD/DVD Drive"),
437 ICON_COL, drive_icon,
438 -1);
439 g_free (drive_name);
440 g_object_unref (drive_icon);
441
442 gtk_widget_set_sensitive (GTK_WIDGET (self), TRUE);
443 if (gtk_combo_box_get_active (GTK_COMBO_BOX (self)) == -1) {
444 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (self), &iter);
445 brasero_drive_selection_set_current_drive (self, &iter);
446 }
447 }
448
449 static void
450 brasero_drive_selection_drive_removed_cb (BraseroMediumMonitor *monitor,
451 BraseroDrive *drive,
452 BraseroDriveSelection *self)
453 {
454 GtkTreeModel *model;
455 GtkTreeIter iter;
456
457 model = gtk_combo_box_get_model (GTK_COMBO_BOX (self));
458 if (!gtk_tree_model_get_iter_first (model, &iter))
459 return;
460
461 do {
462 BraseroDrive *iter_drive;
463
464 gtk_tree_model_get (model, &iter,
465 DRIVE_COL, &iter_drive,
466 -1);
467
468 if (drive == iter_drive) {
469 g_object_unref (iter_drive);
470 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
471 break;
472 }
473
474 /* Could be NULL if a message "there is no drive ..." is on */
475 if (iter_drive)
476 g_object_unref (iter_drive);
477
478 } while (gtk_tree_model_iter_next (model, &iter));
479
480 if (!gtk_tree_model_get_iter_first (model, &iter)) {
481 brasero_drive_selection_add_no_disc_entry (self);
482 return;
483 }
484
485 if (gtk_combo_box_get_active (GTK_COMBO_BOX (self)) == -1) {
486 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (self), &iter);
487 brasero_drive_selection_set_current_drive (self, &iter);
488 }
489 }
490
491 static void
492 brasero_drive_selection_init (BraseroDriveSelection *object)
493 {
494 GtkListStore *model;
495 GtkCellRenderer *renderer;
496 BraseroMediumMonitor *monitor;
497 BraseroDriveSelectionPrivate *priv;
498
499 priv = BRASERO_DRIVE_SELECTION_PRIVATE (object);
500
501 monitor = brasero_medium_monitor_get_default ();
502 priv->added_sig = g_signal_connect (monitor,
503 "drive-added",
504 G_CALLBACK (brasero_drive_selection_drive_added_cb),
505 object);
506 priv->removed_sig = g_signal_connect (monitor,
507 "drive-removed",
508 G_CALLBACK (brasero_drive_selection_drive_removed_cb),
509 object);
510
511 g_object_unref (monitor);
512
513 /* get the list and fill the model */
514 model = gtk_list_store_new (NUM_COL,
515 G_TYPE_OBJECT,
516 G_TYPE_STRING,
517 G_TYPE_ICON);
518
519 gtk_combo_box_set_model (GTK_COMBO_BOX (object), GTK_TREE_MODEL (model));
520 g_object_unref (model);
521
522 renderer = gtk_cell_renderer_pixbuf_new ();
523 g_object_set (renderer, "follow-state", TRUE, NULL);
524 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, FALSE);
525 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
526 "gicon", ICON_COL,
527 NULL);
528
529 renderer = gtk_cell_renderer_text_new ();
530 g_object_set (renderer, "xpad", 8, NULL);
531 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, TRUE);
532 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
533 "markup", NAME_COL,
534 NULL);
535 }
536
537 static void
538 brasero_drive_selection_constructed (GObject *object)
539 {
540 G_OBJECT_CLASS (brasero_drive_selection_parent_class)->constructed (object);
541
542 brasero_drive_selection_show_type (BRASERO_DRIVE_SELECTION (object),
543 BRASERO_DRIVE_TYPE_ALL_BUT_FILE);
544 }
545
546 static void
547 brasero_drive_selection_finalize (GObject *object)
548 {
549 BraseroDriveSelectionPrivate *priv;
550 BraseroMediumMonitor *monitor;
551
552 priv = BRASERO_DRIVE_SELECTION_PRIVATE (object);
553
554 monitor = brasero_medium_monitor_get_default ();
555
556 g_signal_handler_disconnect (monitor, priv->added_sig);
557 g_signal_handler_disconnect (monitor, priv->removed_sig);
558 priv->removed_sig = 0;
559 priv->added_sig = 0;
560
561 g_object_unref (monitor);
562
563 G_OBJECT_CLASS (brasero_drive_selection_parent_class)->finalize (object);
564 }
565
566 static void
567 brasero_drive_selection_set_property (GObject *object,
568 guint prop_id,
569 const GValue *value,
570 GParamSpec *pspec)
571 {
572 g_return_if_fail (BRASERO_IS_DRIVE_SELECTION (object));
573
574 switch (prop_id)
575 {
576 case PROP_DRIVE_TYPE:
577 brasero_drive_selection_show_type (BRASERO_DRIVE_SELECTION (object),
578 g_value_get_uint (value));
579 break;
580 case PROP_DRIVE:
581 brasero_drive_selection_set_active (BRASERO_DRIVE_SELECTION (object),
582 BRASERO_DRIVE (g_value_get_object (value)));
583 break;
584 default:
585 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
586 break;
587 }
588 }
589
590 static void
591 brasero_drive_selection_get_property (GObject *object,
592 guint prop_id,
593 GValue *value,
594 GParamSpec *pspec)
595 {
596 BraseroDriveSelectionPrivate *priv;
597
598 g_return_if_fail (BRASERO_IS_DRIVE_SELECTION (object));
599
600 priv = BRASERO_DRIVE_SELECTION_PRIVATE (object);
601
602 switch (prop_id)
603 {
604 case PROP_DRIVE_TYPE:
605 g_value_set_uint (value, priv->type);
606 break;
607 case PROP_DRIVE:
608 g_value_set_object (value, brasero_drive_selection_get_active (BRASERO_DRIVE_SELECTION (object)));
609 break;
610 default:
611 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
612 break;
613 }
614 }
615
616 static void
617 brasero_drive_selection_class_init (BraseroDriveSelectionClass *klass)
618 {
619 GObjectClass* object_class = G_OBJECT_CLASS (klass);
620 GtkComboBoxClass *combo_class = GTK_COMBO_BOX_CLASS (klass);
621
622 g_type_class_add_private (klass, sizeof (BraseroDriveSelectionPrivate));
623
624 object_class->finalize = brasero_drive_selection_finalize;
625 object_class->set_property = brasero_drive_selection_set_property;
626 object_class->get_property = brasero_drive_selection_get_property;
627 object_class->constructed = brasero_drive_selection_constructed;
628
629 combo_class->changed = brasero_drive_selection_changed;
630
631 g_object_class_install_property (object_class, PROP_DRIVE,
632 g_param_spec_object ("Drive",
633 "Selected drive",
634 "The currently selected drive",
635 BRASERO_TYPE_DRIVE, G_PARAM_READWRITE));
636
637 g_object_class_install_property (object_class, PROP_DRIVE_TYPE,
638 g_param_spec_uint ("drive-type",
639 "The type of drives",
640 "The type of drives displayed",
641 0, BRASERO_DRIVE_TYPE_ALL,
642 BRASERO_DRIVE_TYPE_ALL_BUT_FILE,
643 G_PARAM_READWRITE));
644 /**
645 * BraseroDriveSelection::drive-changed:
646 * @selector: the object which received the signal
647 * @drive: the drive which is now selected
648 *
649 * This signal gets emitted when the selected medium has changed
650 *
651 */
652 brasero_drive_selection_signals [CHANGED_SIGNAL] =
653 g_signal_new ("drive_changed",
654 BRASERO_TYPE_DRIVE_SELECTION,
655 G_SIGNAL_RUN_FIRST|G_SIGNAL_ACTION|G_SIGNAL_NO_RECURSE,
656 G_STRUCT_OFFSET (BraseroDriveSelectionClass, drive_changed),
657 NULL,
658 NULL,
659 g_cclosure_marshal_VOID__OBJECT,
660 G_TYPE_NONE,
661 1,
662 BRASERO_TYPE_DRIVE);
663 }
664
665 /**
666 * brasero_drive_selection_new:
667 *
668 * Creates a new #BraseroDriveSelection object
669 *
670 * Return value: a #GtkWidget. Unref when it is not needed anymore.
671 **/
672
673 GtkWidget *
674 brasero_drive_selection_new (void)
675 {
676 return g_object_new (BRASERO_TYPE_DRIVE_SELECTION, NULL);
677 }