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"
43 struct _GUnionVolumeMonitor {
44 GVolumeMonitor parent;
49 static void g_union_volume_monitor_remove_monitor (GUnionVolumeMonitor *union_monitor,
50 GVolumeMonitor *child_monitor);
53 #define g_union_volume_monitor_get_type _g_union_volume_monitor_get_type
54 G_DEFINE_TYPE (GUnionVolumeMonitor, g_union_volume_monitor, G_TYPE_VOLUME_MONITOR);
56 static GStaticRecMutex the_volume_monitor_mutex = G_STATIC_REC_MUTEX_INIT;
58 static GUnionVolumeMonitor *the_volume_monitor = NULL;
61 g_union_volume_monitor_finalize (GObject *object)
63 GUnionVolumeMonitor *monitor;
64 GVolumeMonitor *child_monitor;
66 monitor = G_UNION_VOLUME_MONITOR (object);
68 while (monitor->monitors != NULL)
70 child_monitor = monitor->monitors->data;
71 g_union_volume_monitor_remove_monitor (monitor,
73 g_object_unref (child_monitor);
76 G_OBJECT_CLASS (g_union_volume_monitor_parent_class)->finalize (object);
80 g_union_volume_monitor_dispose (GObject *object)
82 GUnionVolumeMonitor *monitor;
84 monitor = G_UNION_VOLUME_MONITOR (object);
86 g_static_rec_mutex_lock (&the_volume_monitor_mutex);
87 the_volume_monitor = NULL;
88 g_static_rec_mutex_unlock (&the_volume_monitor_mutex);
90 G_OBJECT_CLASS (g_union_volume_monitor_parent_class)->dispose (object);
94 get_mounts (GVolumeMonitor *volume_monitor)
96 GUnionVolumeMonitor *monitor;
97 GVolumeMonitor *child_monitor;
101 monitor = G_UNION_VOLUME_MONITOR (volume_monitor);
105 g_static_rec_mutex_lock (&the_volume_monitor_mutex);
107 for (l = monitor->monitors; l != NULL; l = l->next)
109 child_monitor = l->data;
111 res = g_list_concat (res, g_volume_monitor_get_mounts (child_monitor));
114 g_static_rec_mutex_unlock (&the_volume_monitor_mutex);
120 get_volumes (GVolumeMonitor *volume_monitor)
122 GUnionVolumeMonitor *monitor;
123 GVolumeMonitor *child_monitor;
127 monitor = G_UNION_VOLUME_MONITOR (volume_monitor);
131 g_static_rec_mutex_lock (&the_volume_monitor_mutex);
133 for (l = monitor->monitors; l != NULL; l = l->next)
135 child_monitor = l->data;
137 res = g_list_concat (res, g_volume_monitor_get_volumes (child_monitor));
140 g_static_rec_mutex_unlock (&the_volume_monitor_mutex);
146 get_connected_drives (GVolumeMonitor *volume_monitor)
148 GUnionVolumeMonitor *monitor;
149 GVolumeMonitor *child_monitor;
153 monitor = G_UNION_VOLUME_MONITOR (volume_monitor);
157 g_static_rec_mutex_lock (&the_volume_monitor_mutex);
159 for (l = monitor->monitors; l != NULL; l = l->next)
161 child_monitor = l->data;
163 res = g_list_concat (res, g_volume_monitor_get_connected_drives (child_monitor));
166 g_static_rec_mutex_unlock (&the_volume_monitor_mutex);
172 get_volume_for_uuid (GVolumeMonitor *volume_monitor, const char *uuid)
174 GUnionVolumeMonitor *monitor;
175 GVolumeMonitor *child_monitor;
179 monitor = G_UNION_VOLUME_MONITOR (volume_monitor);
183 g_static_rec_mutex_lock (&the_volume_monitor_mutex);
185 for (l = monitor->monitors; l != NULL; l = l->next)
187 child_monitor = l->data;
189 volume = g_volume_monitor_get_volume_for_uuid (child_monitor, uuid);
195 g_static_rec_mutex_unlock (&the_volume_monitor_mutex);
201 get_mount_for_uuid (GVolumeMonitor *volume_monitor, const char *uuid)
203 GUnionVolumeMonitor *monitor;
204 GVolumeMonitor *child_monitor;
208 monitor = G_UNION_VOLUME_MONITOR (volume_monitor);
212 g_static_rec_mutex_lock (&the_volume_monitor_mutex);
214 for (l = monitor->monitors; l != NULL; l = l->next)
216 child_monitor = l->data;
218 mount = g_volume_monitor_get_mount_for_uuid (child_monitor, uuid);
224 g_static_rec_mutex_unlock (&the_volume_monitor_mutex);
230 g_union_volume_monitor_class_init (GUnionVolumeMonitorClass *klass)
232 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
233 GVolumeMonitorClass *monitor_class = G_VOLUME_MONITOR_CLASS (klass);
235 gobject_class->finalize = g_union_volume_monitor_finalize;
236 gobject_class->dispose = g_union_volume_monitor_dispose;
238 monitor_class->get_connected_drives = get_connected_drives;
239 monitor_class->get_volumes = get_volumes;
240 monitor_class->get_mounts = get_mounts;
241 monitor_class->get_volume_for_uuid = get_volume_for_uuid;
242 monitor_class->get_mount_for_uuid = get_mount_for_uuid;
246 child_volume_added (GVolumeMonitor *child_monitor,
247 GVolume *child_volume,
248 GUnionVolumeMonitor *union_monitor)
250 g_signal_emit_by_name (union_monitor,
256 child_volume_removed (GVolumeMonitor *child_monitor,
257 GVolume *child_volume,
258 GUnionVolumeMonitor *union_monitor)
260 g_signal_emit_by_name (union_monitor,
266 child_volume_changed (GVolumeMonitor *child_monitor,
267 GVolume *child_volume,
268 GUnionVolumeMonitor *union_monitor)
270 g_signal_emit_by_name (union_monitor,
276 child_mount_added (GVolumeMonitor *child_monitor,
278 GUnionVolumeMonitor *union_monitor)
280 g_signal_emit_by_name (union_monitor,
286 child_mount_removed (GVolumeMonitor *child_monitor,
288 GUnionVolumeMonitor *union_monitor)
290 g_signal_emit_by_name (union_monitor,
296 child_mount_pre_unmount (GVolumeMonitor *child_monitor,
298 GUnionVolumeMonitor *union_monitor)
300 g_signal_emit_by_name (union_monitor,
307 child_mount_changed (GVolumeMonitor *child_monitor,
309 GUnionVolumeMonitor *union_monitor)
311 g_signal_emit_by_name (union_monitor,
317 child_drive_connected (GVolumeMonitor *child_monitor,
319 GUnionVolumeMonitor *union_monitor)
321 g_signal_emit_by_name (union_monitor,
327 child_drive_disconnected (GVolumeMonitor *child_monitor,
329 GUnionVolumeMonitor *union_monitor)
331 g_signal_emit_by_name (union_monitor,
332 "drive-disconnected",
337 child_drive_changed (GVolumeMonitor *child_monitor,
339 GUnionVolumeMonitor *union_monitor)
341 g_signal_emit_by_name (union_monitor,
347 child_drive_eject_button (GVolumeMonitor *child_monitor,
349 GUnionVolumeMonitor *union_monitor)
351 g_signal_emit_by_name (union_monitor,
352 "drive-eject-button",
357 g_union_volume_monitor_add_monitor (GUnionVolumeMonitor *union_monitor,
358 GVolumeMonitor *volume_monitor)
360 if (g_list_find (union_monitor->monitors, volume_monitor))
363 union_monitor->monitors =
364 g_list_prepend (union_monitor->monitors,
365 g_object_ref (volume_monitor));
367 g_signal_connect (volume_monitor, "volume-added", (GCallback)child_volume_added, union_monitor);
368 g_signal_connect (volume_monitor, "volume-removed", (GCallback)child_volume_removed, union_monitor);
369 g_signal_connect (volume_monitor, "volume-changed", (GCallback)child_volume_changed, union_monitor);
370 g_signal_connect (volume_monitor, "mount-added", (GCallback)child_mount_added, union_monitor);
371 g_signal_connect (volume_monitor, "mount-removed", (GCallback)child_mount_removed, union_monitor);
372 g_signal_connect (volume_monitor, "mount-pre-unmount", (GCallback)child_mount_pre_unmount, union_monitor);
373 g_signal_connect (volume_monitor, "mount-changed", (GCallback)child_mount_changed, union_monitor);
374 g_signal_connect (volume_monitor, "drive-connected", (GCallback)child_drive_connected, union_monitor);
375 g_signal_connect (volume_monitor, "drive-disconnected", (GCallback)child_drive_disconnected, union_monitor);
376 g_signal_connect (volume_monitor, "drive-changed", (GCallback)child_drive_changed, union_monitor);
377 g_signal_connect (volume_monitor, "drive-eject-button", (GCallback)child_drive_eject_button, union_monitor);
381 g_union_volume_monitor_remove_monitor (GUnionVolumeMonitor *union_monitor,
382 GVolumeMonitor *child_monitor)
386 l = g_list_find (union_monitor->monitors, child_monitor);
390 union_monitor->monitors = g_list_delete_link (union_monitor->monitors, l);
392 g_signal_handlers_disconnect_by_func (child_monitor, child_volume_added, union_monitor);
393 g_signal_handlers_disconnect_by_func (child_monitor, child_volume_removed, union_monitor);
394 g_signal_handlers_disconnect_by_func (child_monitor, child_volume_changed, union_monitor);
395 g_signal_handlers_disconnect_by_func (child_monitor, child_mount_added, union_monitor);
396 g_signal_handlers_disconnect_by_func (child_monitor, child_mount_removed, union_monitor);
397 g_signal_handlers_disconnect_by_func (child_monitor, child_mount_pre_unmount, union_monitor);
398 g_signal_handlers_disconnect_by_func (child_monitor, child_mount_changed, union_monitor);
399 g_signal_handlers_disconnect_by_func (child_monitor, child_drive_connected, union_monitor);
400 g_signal_handlers_disconnect_by_func (child_monitor, child_drive_disconnected, union_monitor);
401 g_signal_handlers_disconnect_by_func (child_monitor, child_drive_changed, union_monitor);
402 g_signal_handlers_disconnect_by_func (child_monitor, child_drive_eject_button, union_monitor);
406 get_default_native_class (gpointer data)
408 GNativeVolumeMonitorClass *klass, *native_class, **native_class_out;
409 const char *use_this;
410 GIOExtensionPoint *ep;
411 GIOExtension *extension;
414 native_class_out = data;
416 use_this = g_getenv ("GIO_USE_VOLUME_MONITOR");
418 /* Ensure vfs in modules loaded */
419 _g_io_modules_ensure_loaded ();
421 ep = g_io_extension_point_lookup (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME);
426 extension = g_io_extension_point_get_extension_by_name (ep, use_this);
429 klass = G_NATIVE_VOLUME_MONITOR_CLASS (g_io_extension_ref_class (extension));
430 if (G_VOLUME_MONITOR_CLASS (klass)->is_supported())
431 native_class = klass;
433 g_type_class_unref (klass);
437 if (native_class == NULL)
439 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
442 klass = G_NATIVE_VOLUME_MONITOR_CLASS (g_io_extension_ref_class (extension));
443 if (G_VOLUME_MONITOR_CLASS (klass)->is_supported())
445 native_class = klass;
449 g_type_class_unref (klass);
455 *native_class_out = native_class;
456 return G_TYPE_FROM_CLASS (native_class);
459 return G_TYPE_INVALID;
462 /* We return the class, with a ref taken.
463 * This way we avoid unloading the class/module
464 * between selecting the type and creating the
465 * instance on the first call.
467 static GNativeVolumeMonitorClass *
468 get_native_class (void)
470 static GOnce once_init = G_ONCE_INIT;
471 GTypeClass *type_class;
474 g_once (&once_init, (GThreadFunc)get_default_native_class, &type_class);
476 if (type_class == NULL && once_init.retval != GUINT_TO_POINTER(G_TYPE_INVALID))
477 type_class = g_type_class_ref ((GType)once_init.retval);
479 return (GNativeVolumeMonitorClass *)type_class;
483 g_union_volume_monitor_init (GUnionVolumeMonitor *union_monitor)
488 populate_union_monitor (GUnionVolumeMonitor *union_monitor)
490 GVolumeMonitor *monitor;
491 GNativeVolumeMonitorClass *native_class;
492 GVolumeMonitorClass *klass;
493 GIOExtensionPoint *ep;
494 GIOExtension *extension;
497 native_class = get_native_class ();
499 if (native_class != NULL)
501 monitor = g_object_new (G_TYPE_FROM_CLASS (native_class), NULL);
502 g_union_volume_monitor_add_monitor (union_monitor, monitor);
503 g_object_unref (monitor);
504 g_type_class_unref (native_class);
507 ep = g_io_extension_point_lookup (G_VOLUME_MONITOR_EXTENSION_POINT_NAME);
508 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
512 klass = G_VOLUME_MONITOR_CLASS (g_io_extension_ref_class (extension));
513 if (klass->is_supported == NULL || klass->is_supported())
515 monitor = g_object_new (g_io_extension_get_type (extension), NULL);
516 g_union_volume_monitor_add_monitor (union_monitor, monitor);
517 g_object_unref (monitor);
519 g_type_class_unref (klass);
523 static GUnionVolumeMonitor *
524 g_union_volume_monitor_new (void)
526 GUnionVolumeMonitor *monitor;
528 monitor = g_object_new (G_TYPE_UNION_VOLUME_MONITOR, NULL);
534 * g_volume_monitor_get:
536 * Gets the volume monitor used by gio.
538 * Returns: a reference to the #GVolumeMonitor used by gio. Call
539 * g_object_unref() when done with it.
542 g_volume_monitor_get (void)
546 g_static_rec_mutex_lock (&the_volume_monitor_mutex);
548 if (the_volume_monitor)
549 vm = G_VOLUME_MONITOR (g_object_ref (the_volume_monitor));
552 the_volume_monitor = g_union_volume_monitor_new ();
553 populate_union_monitor (the_volume_monitor);
554 vm = G_VOLUME_MONITOR (the_volume_monitor);
557 g_static_rec_mutex_unlock (&the_volume_monitor_mutex);
563 * _g_mount_get_for_mount_path:
564 * @mountpoint: a string.
565 * @cancellable: a #GCancellable, or %NULL
567 * Returns: a #GMount for given @mount_path or %NULL.
570 _g_mount_get_for_mount_path (const char *mount_path,
571 GCancellable *cancellable)
573 GNativeVolumeMonitorClass *klass;
576 klass = get_native_class ();
582 if (klass->get_mount_for_mount_path)
584 g_static_rec_mutex_lock (&the_volume_monitor_mutex);
585 mount = klass->get_mount_for_mount_path (mount_path, cancellable);
586 g_static_rec_mutex_unlock (&the_volume_monitor_mutex);
589 /* TODO: How do we know this succeeded? Keep in mind that the native
590 * volume monitor may fail (e.g. not being able to connect to
591 * hald). Is the get_mount_for_mount_path() method allowed to
592 * return NULL? Seems like it is ... probably the method needs
593 * to take a boolean and write if it succeeds or not.. Messy.
597 g_type_class_unref (klass);
603 * g_volume_monitor_adopt_orphan_mount:
604 * @mount: a #GMount object to find a parent for
606 * This function should be called by any #GVolumeMonitor
607 * implementation when a new #GMount object is created that is not
608 * associated with a #GVolume object. It must be called just before
609 * emitting the @mount_added signal.
611 * If the return value is not %NULL, the caller must associate the
612 * returned #GVolume object with the #GMount. This involves returning
613 * it in it's g_mount_get_volume() implementation. The caller must
614 * also listen for the "removed" signal on the returned object
615 * and give up it's reference when handling that signal
617 * Similary, if implementing g_volume_monitor_adopt_orphan_mount(),
618 * the implementor must take a reference to @mount and return it in
619 * it's g_volume_get_mount() implemented. Also, the implementor must
620 * listen for the "unmounted" signal on @mount and give up it's
621 * reference upon handling that signal.
623 * There are two main use cases for this function.
625 * One is when implementing a user space file system driver that reads
626 * blocks of a block device that is already represented by the native
627 * volume monitor (for example a CD Audio file system driver). Such
628 * a driver will generate it's own #GMount object that needs to be
629 * assoicated with the #GVolume object that represents the volume.
631 * The other is for implementing a #GVolumeMonitor whose sole purpose
632 * is to return #GVolume objects representing entries in the users
633 * "favorite servers" list or similar.
635 * Returns: the #GVolume object that is the parent for @mount or %NULL
636 * if no wants to adopt the #GMount.
638 * Deprecated: 2.20: Instead of using this function, #GVolumeMonitor
639 * implementations should instead create shadow mounts with the URI of
640 * the mount they intend to adopt. See the proxy volume monitor in
641 * gvfs for an example of this. Also see g_mount_is_shadowed(),
642 * g_mount_shadow() and g_mount_unshadow() functions.
645 g_volume_monitor_adopt_orphan_mount (GMount *mount)
647 GVolumeMonitor *child_monitor;
648 GVolumeMonitorClass *child_monitor_class;
652 g_return_val_if_fail (mount != NULL, NULL);
654 if (the_volume_monitor == NULL)
659 g_static_rec_mutex_lock (&the_volume_monitor_mutex);
661 for (l = the_volume_monitor->monitors; l != NULL; l = l->next)
663 child_monitor = l->data;
664 child_monitor_class = G_VOLUME_MONITOR_GET_CLASS (child_monitor);
666 if (child_monitor_class->adopt_orphan_mount != NULL)
668 volume = child_monitor_class->adopt_orphan_mount (mount, child_monitor);
674 g_static_rec_mutex_unlock (&the_volume_monitor_mutex);
680 #define __G_UNION_VOLUME_MONITOR_C__
681 #include "gioaliasdef.c"