1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
5 * Copyright (C) 2006-2007 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library 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 GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
22 * Author: Alexander Larsson <alexl@redhat.com>
23 * David Zeuthen <davidz@redhat.com>
31 #include "gunionvolumemonitor.h"
32 #include "gmountprivate.h"
33 #include "giomodule-priv.h"
35 #include "gunixvolumemonitor.h"
37 #include "gnativevolumemonitor.h"
42 struct _GUnionVolumeMonitor {
43 GVolumeMonitor parent;
48 static void g_union_volume_monitor_remove_monitor (GUnionVolumeMonitor *union_monitor,
49 GVolumeMonitor *child_monitor);
52 #define g_union_volume_monitor_get_type _g_union_volume_monitor_get_type
53 G_DEFINE_TYPE (GUnionVolumeMonitor, g_union_volume_monitor, G_TYPE_VOLUME_MONITOR);
55 static GStaticRecMutex the_volume_monitor_mutex = G_STATIC_REC_MUTEX_INIT;
57 static GUnionVolumeMonitor *the_volume_monitor = NULL;
60 g_union_volume_monitor_finalize (GObject *object)
62 GUnionVolumeMonitor *monitor;
63 GVolumeMonitor *child_monitor;
65 monitor = G_UNION_VOLUME_MONITOR (object);
67 while (monitor->monitors != NULL)
69 child_monitor = monitor->monitors->data;
70 g_union_volume_monitor_remove_monitor (monitor,
72 g_object_unref (child_monitor);
75 G_OBJECT_CLASS (g_union_volume_monitor_parent_class)->finalize (object);
79 g_union_volume_monitor_dispose (GObject *object)
81 GUnionVolumeMonitor *monitor;
82 GVolumeMonitor *child_monitor;
85 monitor = G_UNION_VOLUME_MONITOR (object);
87 g_static_rec_mutex_lock (&the_volume_monitor_mutex);
88 the_volume_monitor = NULL;
90 for (l = monitor->monitors; l != NULL; l = l->next)
92 child_monitor = l->data;
93 g_object_run_dispose (G_OBJECT (child_monitor));
96 g_static_rec_mutex_unlock (&the_volume_monitor_mutex);
98 G_OBJECT_CLASS (g_union_volume_monitor_parent_class)->dispose (object);
102 get_mounts (GVolumeMonitor *volume_monitor)
104 GUnionVolumeMonitor *monitor;
105 GVolumeMonitor *child_monitor;
109 monitor = G_UNION_VOLUME_MONITOR (volume_monitor);
113 g_static_rec_mutex_lock (&the_volume_monitor_mutex);
115 for (l = monitor->monitors; l != NULL; l = l->next)
117 child_monitor = l->data;
119 res = g_list_concat (res, g_volume_monitor_get_mounts (child_monitor));
122 g_static_rec_mutex_unlock (&the_volume_monitor_mutex);
128 get_volumes (GVolumeMonitor *volume_monitor)
130 GUnionVolumeMonitor *monitor;
131 GVolumeMonitor *child_monitor;
135 monitor = G_UNION_VOLUME_MONITOR (volume_monitor);
139 g_static_rec_mutex_lock (&the_volume_monitor_mutex);
141 for (l = monitor->monitors; l != NULL; l = l->next)
143 child_monitor = l->data;
145 res = g_list_concat (res, g_volume_monitor_get_volumes (child_monitor));
148 g_static_rec_mutex_unlock (&the_volume_monitor_mutex);
154 get_connected_drives (GVolumeMonitor *volume_monitor)
156 GUnionVolumeMonitor *monitor;
157 GVolumeMonitor *child_monitor;
161 monitor = G_UNION_VOLUME_MONITOR (volume_monitor);
165 g_static_rec_mutex_lock (&the_volume_monitor_mutex);
167 for (l = monitor->monitors; l != NULL; l = l->next)
169 child_monitor = l->data;
171 res = g_list_concat (res, g_volume_monitor_get_connected_drives (child_monitor));
174 g_static_rec_mutex_unlock (&the_volume_monitor_mutex);
180 get_volume_for_uuid (GVolumeMonitor *volume_monitor, const char *uuid)
182 GUnionVolumeMonitor *monitor;
183 GVolumeMonitor *child_monitor;
187 monitor = G_UNION_VOLUME_MONITOR (volume_monitor);
191 g_static_rec_mutex_lock (&the_volume_monitor_mutex);
193 for (l = monitor->monitors; l != NULL; l = l->next)
195 child_monitor = l->data;
197 volume = g_volume_monitor_get_volume_for_uuid (child_monitor, uuid);
203 g_static_rec_mutex_unlock (&the_volume_monitor_mutex);
209 get_mount_for_uuid (GVolumeMonitor *volume_monitor, const char *uuid)
211 GUnionVolumeMonitor *monitor;
212 GVolumeMonitor *child_monitor;
216 monitor = G_UNION_VOLUME_MONITOR (volume_monitor);
220 g_static_rec_mutex_lock (&the_volume_monitor_mutex);
222 for (l = monitor->monitors; l != NULL; l = l->next)
224 child_monitor = l->data;
226 mount = g_volume_monitor_get_mount_for_uuid (child_monitor, uuid);
232 g_static_rec_mutex_unlock (&the_volume_monitor_mutex);
238 g_union_volume_monitor_class_init (GUnionVolumeMonitorClass *klass)
240 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
241 GVolumeMonitorClass *monitor_class = G_VOLUME_MONITOR_CLASS (klass);
243 gobject_class->finalize = g_union_volume_monitor_finalize;
244 gobject_class->dispose = g_union_volume_monitor_dispose;
246 monitor_class->get_connected_drives = get_connected_drives;
247 monitor_class->get_volumes = get_volumes;
248 monitor_class->get_mounts = get_mounts;
249 monitor_class->get_volume_for_uuid = get_volume_for_uuid;
250 monitor_class->get_mount_for_uuid = get_mount_for_uuid;
254 child_volume_added (GVolumeMonitor *child_monitor,
255 GVolume *child_volume,
256 GUnionVolumeMonitor *union_monitor)
258 g_signal_emit_by_name (union_monitor,
264 child_volume_removed (GVolumeMonitor *child_monitor,
265 GVolume *child_volume,
266 GUnionVolumeMonitor *union_monitor)
268 g_signal_emit_by_name (union_monitor,
274 child_volume_changed (GVolumeMonitor *child_monitor,
275 GVolume *child_volume,
276 GUnionVolumeMonitor *union_monitor)
278 g_signal_emit_by_name (union_monitor,
284 child_mount_added (GVolumeMonitor *child_monitor,
286 GUnionVolumeMonitor *union_monitor)
288 g_signal_emit_by_name (union_monitor,
294 child_mount_removed (GVolumeMonitor *child_monitor,
296 GUnionVolumeMonitor *union_monitor)
298 g_signal_emit_by_name (union_monitor,
304 child_mount_pre_unmount (GVolumeMonitor *child_monitor,
306 GUnionVolumeMonitor *union_monitor)
308 g_signal_emit_by_name (union_monitor,
315 child_mount_changed (GVolumeMonitor *child_monitor,
317 GUnionVolumeMonitor *union_monitor)
319 g_signal_emit_by_name (union_monitor,
325 child_drive_connected (GVolumeMonitor *child_monitor,
327 GUnionVolumeMonitor *union_monitor)
329 g_signal_emit_by_name (union_monitor,
335 child_drive_disconnected (GVolumeMonitor *child_monitor,
337 GUnionVolumeMonitor *union_monitor)
339 g_signal_emit_by_name (union_monitor,
340 "drive-disconnected",
345 child_drive_changed (GVolumeMonitor *child_monitor,
347 GUnionVolumeMonitor *union_monitor)
349 g_signal_emit_by_name (union_monitor,
355 child_drive_eject_button (GVolumeMonitor *child_monitor,
357 GUnionVolumeMonitor *union_monitor)
359 g_signal_emit_by_name (union_monitor,
360 "drive-eject-button",
365 child_drive_stop_button (GVolumeMonitor *child_monitor,
367 GUnionVolumeMonitor *union_monitor)
369 g_signal_emit_by_name (union_monitor,
375 g_union_volume_monitor_add_monitor (GUnionVolumeMonitor *union_monitor,
376 GVolumeMonitor *volume_monitor)
378 if (g_list_find (union_monitor->monitors, volume_monitor))
381 union_monitor->monitors =
382 g_list_prepend (union_monitor->monitors,
383 g_object_ref (volume_monitor));
385 g_signal_connect (volume_monitor, "volume-added", (GCallback)child_volume_added, union_monitor);
386 g_signal_connect (volume_monitor, "volume-removed", (GCallback)child_volume_removed, union_monitor);
387 g_signal_connect (volume_monitor, "volume-changed", (GCallback)child_volume_changed, union_monitor);
388 g_signal_connect (volume_monitor, "mount-added", (GCallback)child_mount_added, union_monitor);
389 g_signal_connect (volume_monitor, "mount-removed", (GCallback)child_mount_removed, union_monitor);
390 g_signal_connect (volume_monitor, "mount-pre-unmount", (GCallback)child_mount_pre_unmount, union_monitor);
391 g_signal_connect (volume_monitor, "mount-changed", (GCallback)child_mount_changed, union_monitor);
392 g_signal_connect (volume_monitor, "drive-connected", (GCallback)child_drive_connected, union_monitor);
393 g_signal_connect (volume_monitor, "drive-disconnected", (GCallback)child_drive_disconnected, union_monitor);
394 g_signal_connect (volume_monitor, "drive-changed", (GCallback)child_drive_changed, union_monitor);
395 g_signal_connect (volume_monitor, "drive-eject-button", (GCallback)child_drive_eject_button, union_monitor);
396 g_signal_connect (volume_monitor, "drive-stop-button", (GCallback)child_drive_stop_button, union_monitor);
400 g_union_volume_monitor_remove_monitor (GUnionVolumeMonitor *union_monitor,
401 GVolumeMonitor *child_monitor)
405 l = g_list_find (union_monitor->monitors, child_monitor);
409 union_monitor->monitors = g_list_delete_link (union_monitor->monitors, l);
411 g_signal_handlers_disconnect_by_func (child_monitor, child_volume_added, union_monitor);
412 g_signal_handlers_disconnect_by_func (child_monitor, child_volume_removed, union_monitor);
413 g_signal_handlers_disconnect_by_func (child_monitor, child_volume_changed, union_monitor);
414 g_signal_handlers_disconnect_by_func (child_monitor, child_mount_added, union_monitor);
415 g_signal_handlers_disconnect_by_func (child_monitor, child_mount_removed, union_monitor);
416 g_signal_handlers_disconnect_by_func (child_monitor, child_mount_pre_unmount, union_monitor);
417 g_signal_handlers_disconnect_by_func (child_monitor, child_mount_changed, union_monitor);
418 g_signal_handlers_disconnect_by_func (child_monitor, child_drive_connected, union_monitor);
419 g_signal_handlers_disconnect_by_func (child_monitor, child_drive_disconnected, union_monitor);
420 g_signal_handlers_disconnect_by_func (child_monitor, child_drive_changed, union_monitor);
421 g_signal_handlers_disconnect_by_func (child_monitor, child_drive_eject_button, union_monitor);
422 g_signal_handlers_disconnect_by_func (child_monitor, child_drive_stop_button, union_monitor);
426 get_default_native_class (gpointer data)
428 GNativeVolumeMonitorClass *klass, *native_class, **native_class_out;
429 const char *use_this;
430 GIOExtensionPoint *ep;
431 GIOExtension *extension;
434 native_class_out = data;
436 use_this = g_getenv ("GIO_USE_VOLUME_MONITOR");
438 /* Ensure vfs in modules loaded */
439 _g_io_modules_ensure_loaded ();
441 ep = g_io_extension_point_lookup (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME);
446 extension = g_io_extension_point_get_extension_by_name (ep, use_this);
449 klass = G_NATIVE_VOLUME_MONITOR_CLASS (g_io_extension_ref_class (extension));
450 if (G_VOLUME_MONITOR_CLASS (klass)->is_supported())
451 native_class = klass;
453 g_type_class_unref (klass);
457 if (native_class == NULL)
459 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
462 klass = G_NATIVE_VOLUME_MONITOR_CLASS (g_io_extension_ref_class (extension));
463 if (G_VOLUME_MONITOR_CLASS (klass)->is_supported())
465 native_class = klass;
469 g_type_class_unref (klass);
475 *native_class_out = native_class;
476 return G_TYPE_FROM_CLASS (native_class);
479 return G_TYPE_INVALID;
482 /* We return the class, with a ref taken.
483 * This way we avoid unloading the class/module
484 * between selecting the type and creating the
485 * instance on the first call.
487 static GNativeVolumeMonitorClass *
488 get_native_class (void)
490 static GOnce once_init = G_ONCE_INIT;
491 GTypeClass *type_class;
494 g_once (&once_init, (GThreadFunc)get_default_native_class, &type_class);
496 if (type_class == NULL && once_init.retval != GUINT_TO_POINTER(G_TYPE_INVALID))
497 type_class = g_type_class_ref ((GType)once_init.retval);
499 return (GNativeVolumeMonitorClass *)type_class;
503 g_union_volume_monitor_init (GUnionVolumeMonitor *union_monitor)
508 populate_union_monitor (GUnionVolumeMonitor *union_monitor)
510 GVolumeMonitor *monitor;
511 GNativeVolumeMonitorClass *native_class;
512 GVolumeMonitorClass *klass;
513 GIOExtensionPoint *ep;
514 GIOExtension *extension;
517 native_class = get_native_class ();
519 if (native_class != NULL)
521 monitor = g_object_new (G_TYPE_FROM_CLASS (native_class), NULL);
522 g_union_volume_monitor_add_monitor (union_monitor, monitor);
523 g_object_unref (monitor);
524 g_type_class_unref (native_class);
527 ep = g_io_extension_point_lookup (G_VOLUME_MONITOR_EXTENSION_POINT_NAME);
528 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
532 klass = G_VOLUME_MONITOR_CLASS (g_io_extension_ref_class (extension));
533 if (klass->is_supported == NULL || klass->is_supported())
535 monitor = g_object_new (g_io_extension_get_type (extension), NULL);
536 g_union_volume_monitor_add_monitor (union_monitor, monitor);
537 g_object_unref (monitor);
539 g_type_class_unref (klass);
543 static GUnionVolumeMonitor *
544 g_union_volume_monitor_new (void)
546 GUnionVolumeMonitor *monitor;
548 monitor = g_object_new (G_TYPE_UNION_VOLUME_MONITOR, NULL);
554 * g_volume_monitor_get:
556 * Gets the volume monitor used by gio.
558 * Returns: a reference to the #GVolumeMonitor used by gio. Call
559 * g_object_unref() when done with it.
562 g_volume_monitor_get (void)
566 g_static_rec_mutex_lock (&the_volume_monitor_mutex);
568 if (the_volume_monitor)
569 vm = G_VOLUME_MONITOR (g_object_ref (the_volume_monitor));
572 the_volume_monitor = g_union_volume_monitor_new ();
573 populate_union_monitor (the_volume_monitor);
574 vm = G_VOLUME_MONITOR (the_volume_monitor);
577 g_static_rec_mutex_unlock (&the_volume_monitor_mutex);
583 _g_mount_get_for_mount_path (const gchar *mount_path,
584 GCancellable *cancellable)
586 GNativeVolumeMonitorClass *klass;
589 klass = get_native_class ();
595 if (klass->get_mount_for_mount_path)
597 g_static_rec_mutex_lock (&the_volume_monitor_mutex);
598 mount = klass->get_mount_for_mount_path (mount_path, cancellable);
599 g_static_rec_mutex_unlock (&the_volume_monitor_mutex);
602 /* TODO: How do we know this succeeded? Keep in mind that the native
603 * volume monitor may fail (e.g. not being able to connect to
604 * hald). Is the get_mount_for_mount_path() method allowed to
605 * return NULL? Seems like it is ... probably the method needs
606 * to take a boolean and write if it succeeds or not.. Messy.
610 g_type_class_unref (klass);
616 * g_volume_monitor_adopt_orphan_mount:
617 * @mount: a #GMount object to find a parent for
619 * This function should be called by any #GVolumeMonitor
620 * implementation when a new #GMount object is created that is not
621 * associated with a #GVolume object. It must be called just before
622 * emitting the @mount_added signal.
624 * If the return value is not %NULL, the caller must associate the
625 * returned #GVolume object with the #GMount. This involves returning
626 * it in its g_mount_get_volume() implementation. The caller must
627 * also listen for the "removed" signal on the returned object
628 * and give up its reference when handling that signal
630 * Similary, if implementing g_volume_monitor_adopt_orphan_mount(),
631 * the implementor must take a reference to @mount and return it in
632 * its g_volume_get_mount() implemented. Also, the implementor must
633 * listen for the "unmounted" signal on @mount and give up its
634 * reference upon handling that signal.
636 * There are two main use cases for this function.
638 * One is when implementing a user space file system driver that reads
639 * blocks of a block device that is already represented by the native
640 * volume monitor (for example a CD Audio file system driver). Such
641 * a driver will generate its own #GMount object that needs to be
642 * assoicated with the #GVolume object that represents the volume.
644 * The other is for implementing a #GVolumeMonitor whose sole purpose
645 * is to return #GVolume objects representing entries in the users
646 * "favorite servers" list or similar.
648 * Returns: (transfer full): the #GVolume object that is the parent for @mount or %NULL
649 * if no wants to adopt the #GMount.
651 * Deprecated: 2.20: Instead of using this function, #GVolumeMonitor
652 * implementations should instead create shadow mounts with the URI of
653 * the mount they intend to adopt. See the proxy volume monitor in
654 * gvfs for an example of this. Also see g_mount_is_shadowed(),
655 * g_mount_shadow() and g_mount_unshadow() functions.
658 g_volume_monitor_adopt_orphan_mount (GMount *mount)
660 GVolumeMonitor *child_monitor;
661 GVolumeMonitorClass *child_monitor_class;
665 g_return_val_if_fail (mount != NULL, NULL);
667 if (the_volume_monitor == NULL)
672 g_static_rec_mutex_lock (&the_volume_monitor_mutex);
674 for (l = the_volume_monitor->monitors; l != NULL; l = l->next)
676 child_monitor = l->data;
677 child_monitor_class = G_VOLUME_MONITOR_GET_CLASS (child_monitor);
679 if (child_monitor_class->adopt_orphan_mount != NULL)
681 volume = child_monitor_class->adopt_orphan_mount (mount, child_monitor);
687 g_static_rec_mutex_unlock (&the_volume_monitor_mutex);