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);
77 if (G_OBJECT_CLASS (g_union_volume_monitor_parent_class)->finalize)
78 (*G_OBJECT_CLASS (g_union_volume_monitor_parent_class)->finalize) (object);
82 g_union_volume_monitor_dispose (GObject *object)
84 GUnionVolumeMonitor *monitor;
86 monitor = G_UNION_VOLUME_MONITOR (object);
88 g_static_rec_mutex_lock (&the_volume_monitor_mutex);
89 the_volume_monitor = NULL;
90 g_static_rec_mutex_unlock (&the_volume_monitor_mutex);
92 if (G_OBJECT_CLASS (g_union_volume_monitor_parent_class)->dispose)
93 (*G_OBJECT_CLASS (g_union_volume_monitor_parent_class)->dispose) (object);
97 get_mounts (GVolumeMonitor *volume_monitor)
99 GUnionVolumeMonitor *monitor;
100 GVolumeMonitor *child_monitor;
104 monitor = G_UNION_VOLUME_MONITOR (volume_monitor);
108 g_static_rec_mutex_lock (&the_volume_monitor_mutex);
110 for (l = monitor->monitors; l != NULL; l = l->next)
112 child_monitor = l->data;
114 res = g_list_concat (res, g_volume_monitor_get_mounts (child_monitor));
117 g_static_rec_mutex_unlock (&the_volume_monitor_mutex);
123 get_volumes (GVolumeMonitor *volume_monitor)
125 GUnionVolumeMonitor *monitor;
126 GVolumeMonitor *child_monitor;
130 monitor = G_UNION_VOLUME_MONITOR (volume_monitor);
134 g_static_rec_mutex_lock (&the_volume_monitor_mutex);
136 for (l = monitor->monitors; l != NULL; l = l->next)
138 child_monitor = l->data;
140 res = g_list_concat (res, g_volume_monitor_get_volumes (child_monitor));
143 g_static_rec_mutex_unlock (&the_volume_monitor_mutex);
149 get_connected_drives (GVolumeMonitor *volume_monitor)
151 GUnionVolumeMonitor *monitor;
152 GVolumeMonitor *child_monitor;
156 monitor = G_UNION_VOLUME_MONITOR (volume_monitor);
160 g_static_rec_mutex_lock (&the_volume_monitor_mutex);
162 for (l = monitor->monitors; l != NULL; l = l->next)
164 child_monitor = l->data;
166 res = g_list_concat (res, g_volume_monitor_get_connected_drives (child_monitor));
169 g_static_rec_mutex_unlock (&the_volume_monitor_mutex);
175 get_volume_for_uuid (GVolumeMonitor *volume_monitor, const char *uuid)
177 GUnionVolumeMonitor *monitor;
178 GVolumeMonitor *child_monitor;
182 monitor = G_UNION_VOLUME_MONITOR (volume_monitor);
186 g_static_rec_mutex_lock (&the_volume_monitor_mutex);
188 for (l = monitor->monitors; l != NULL; l = l->next)
190 child_monitor = l->data;
192 volume = g_volume_monitor_get_volume_for_uuid (child_monitor, uuid);
198 g_static_rec_mutex_unlock (&the_volume_monitor_mutex);
204 get_mount_for_uuid (GVolumeMonitor *volume_monitor, const char *uuid)
206 GUnionVolumeMonitor *monitor;
207 GVolumeMonitor *child_monitor;
211 monitor = G_UNION_VOLUME_MONITOR (volume_monitor);
215 g_static_rec_mutex_lock (&the_volume_monitor_mutex);
217 for (l = monitor->monitors; l != NULL; l = l->next)
219 child_monitor = l->data;
221 mount = g_volume_monitor_get_mount_for_uuid (child_monitor, uuid);
227 g_static_rec_mutex_unlock (&the_volume_monitor_mutex);
233 g_union_volume_monitor_class_init (GUnionVolumeMonitorClass *klass)
235 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
236 GVolumeMonitorClass *monitor_class = G_VOLUME_MONITOR_CLASS (klass);
238 gobject_class->finalize = g_union_volume_monitor_finalize;
239 gobject_class->dispose = g_union_volume_monitor_dispose;
241 monitor_class->get_connected_drives = get_connected_drives;
242 monitor_class->get_volumes = get_volumes;
243 monitor_class->get_mounts = get_mounts;
244 monitor_class->get_volume_for_uuid = get_volume_for_uuid;
245 monitor_class->get_mount_for_uuid = get_mount_for_uuid;
249 child_volume_added (GVolumeMonitor *child_monitor,
250 GVolume *child_volume,
251 GUnionVolumeMonitor *union_monitor)
253 g_signal_emit_by_name (union_monitor,
259 child_volume_removed (GVolumeMonitor *child_monitor,
260 GVolume *child_volume,
261 GUnionVolumeMonitor *union_monitor)
263 g_signal_emit_by_name (union_monitor,
269 child_volume_changed (GVolumeMonitor *child_monitor,
270 GVolume *child_volume,
271 GUnionVolumeMonitor *union_monitor)
273 g_signal_emit_by_name (union_monitor,
279 child_mount_added (GVolumeMonitor *child_monitor,
281 GUnionVolumeMonitor *union_monitor)
283 g_signal_emit_by_name (union_monitor,
289 child_mount_removed (GVolumeMonitor *child_monitor,
291 GUnionVolumeMonitor *union_monitor)
293 g_signal_emit_by_name (union_monitor,
299 child_mount_pre_unmount (GVolumeMonitor *child_monitor,
301 GUnionVolumeMonitor *union_monitor)
303 g_signal_emit_by_name (union_monitor,
310 child_mount_changed (GVolumeMonitor *child_monitor,
312 GUnionVolumeMonitor *union_monitor)
314 g_signal_emit_by_name (union_monitor,
320 child_drive_connected (GVolumeMonitor *child_monitor,
322 GUnionVolumeMonitor *union_monitor)
324 g_signal_emit_by_name (union_monitor,
330 child_drive_disconnected (GVolumeMonitor *child_monitor,
332 GUnionVolumeMonitor *union_monitor)
334 g_signal_emit_by_name (union_monitor,
335 "drive_disconnected",
340 child_drive_changed (GVolumeMonitor *child_monitor,
342 GUnionVolumeMonitor *union_monitor)
344 g_signal_emit_by_name (union_monitor,
350 g_union_volume_monitor_add_monitor (GUnionVolumeMonitor *union_monitor,
351 GVolumeMonitor *volume_monitor)
353 if (g_list_find (union_monitor->monitors, volume_monitor))
356 union_monitor->monitors =
357 g_list_prepend (union_monitor->monitors,
358 g_object_ref (volume_monitor));
360 g_signal_connect (volume_monitor, "volume_added", (GCallback)child_volume_added, union_monitor);
361 g_signal_connect (volume_monitor, "volume_removed", (GCallback)child_volume_removed, union_monitor);
362 g_signal_connect (volume_monitor, "volume_changed", (GCallback)child_volume_changed, union_monitor);
363 g_signal_connect (volume_monitor, "mount_added", (GCallback)child_mount_added, union_monitor);
364 g_signal_connect (volume_monitor, "mount_removed", (GCallback)child_mount_removed, union_monitor);
365 g_signal_connect (volume_monitor, "mount_pre_unmount", (GCallback)child_mount_pre_unmount, union_monitor);
366 g_signal_connect (volume_monitor, "mount_changed", (GCallback)child_mount_changed, union_monitor);
367 g_signal_connect (volume_monitor, "drive_connected", (GCallback)child_drive_connected, union_monitor);
368 g_signal_connect (volume_monitor, "drive_disconnected", (GCallback)child_drive_disconnected, union_monitor);
369 g_signal_connect (volume_monitor, "drive_changed", (GCallback)child_drive_changed, union_monitor);
373 g_union_volume_monitor_remove_monitor (GUnionVolumeMonitor *union_monitor,
374 GVolumeMonitor *child_monitor)
378 l = g_list_find (union_monitor->monitors, child_monitor);
382 union_monitor->monitors = g_list_delete_link (union_monitor->monitors, l);
384 g_signal_handlers_disconnect_by_func (child_monitor, child_volume_added, union_monitor);
385 g_signal_handlers_disconnect_by_func (child_monitor, child_volume_removed, union_monitor);
386 g_signal_handlers_disconnect_by_func (child_monitor, child_volume_changed, union_monitor);
387 g_signal_handlers_disconnect_by_func (child_monitor, child_mount_added, union_monitor);
388 g_signal_handlers_disconnect_by_func (child_monitor, child_mount_removed, union_monitor);
389 g_signal_handlers_disconnect_by_func (child_monitor, child_mount_pre_unmount, union_monitor);
390 g_signal_handlers_disconnect_by_func (child_monitor, child_mount_changed, union_monitor);
391 g_signal_handlers_disconnect_by_func (child_monitor, child_drive_connected, union_monitor);
392 g_signal_handlers_disconnect_by_func (child_monitor, child_drive_disconnected, union_monitor);
393 g_signal_handlers_disconnect_by_func (child_monitor, child_drive_changed, union_monitor);
397 get_default_native_class (gpointer data)
399 GNativeVolumeMonitorClass *klass, *native_class, **native_class_out;
400 const char *use_this;
401 GIOExtensionPoint *ep;
402 GIOExtension *extension;
405 native_class_out = data;
407 use_this = g_getenv ("GIO_USE_VOLUME_MONITOR");
409 /* Ensure vfs in modules loaded */
410 _g_io_modules_ensure_loaded ();
412 ep = g_io_extension_point_lookup (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME);
417 extension = g_io_extension_point_get_extension_by_name (ep, use_this);
420 klass = G_NATIVE_VOLUME_MONITOR_CLASS (g_io_extension_ref_class (extension));
421 if (G_VOLUME_MONITOR_CLASS (klass)->is_supported())
422 native_class = klass;
424 g_type_class_unref (klass);
428 if (native_class == NULL)
430 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
433 klass = G_NATIVE_VOLUME_MONITOR_CLASS (g_io_extension_ref_class (extension));
434 if (G_VOLUME_MONITOR_CLASS (klass)->is_supported())
436 native_class = klass;
440 g_type_class_unref (klass);
446 *native_class_out = native_class;
447 return G_TYPE_FROM_CLASS (native_class);
450 return G_TYPE_INVALID;
453 /* We return the class, with a ref taken.
454 * This way we avoid unloading the class/module
455 * between selecting the type and creating the
456 * instance on the first call.
458 static GNativeVolumeMonitorClass *
461 static GOnce once_init = G_ONCE_INIT;
462 GTypeClass *type_class;
465 g_once (&once_init, (GThreadFunc)get_default_native_class, &type_class);
467 if (type_class == NULL && once_init.retval != G_TYPE_INVALID)
468 type_class = g_type_class_ref ((GType)once_init.retval);
470 return (GNativeVolumeMonitorClass *)type_class;
474 g_union_volume_monitor_init (GUnionVolumeMonitor *union_monitor)
479 populate_union_monitor (GUnionVolumeMonitor *union_monitor)
481 GVolumeMonitor *monitor;
482 GNativeVolumeMonitorClass *native_class;
483 GVolumeMonitorClass *klass;
484 GIOExtensionPoint *ep;
485 GIOExtension *extension;
488 native_class = get_native_class ();
490 if (native_class != NULL)
492 monitor = g_object_new (G_TYPE_FROM_CLASS (native_class), NULL);
493 g_union_volume_monitor_add_monitor (union_monitor, monitor);
494 g_object_unref (monitor);
495 g_type_class_unref (native_class);
498 ep = g_io_extension_point_lookup (G_VOLUME_MONITOR_EXTENSION_POINT_NAME);
499 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
503 klass = G_VOLUME_MONITOR_CLASS (g_io_extension_ref_class (extension));
504 if (klass->is_supported == NULL || klass->is_supported())
506 monitor = g_object_new (g_io_extension_get_type (extension), NULL);
507 g_union_volume_monitor_add_monitor (union_monitor, monitor);
508 g_object_unref (monitor);
510 g_type_class_unref (klass);
514 static GUnionVolumeMonitor *
515 g_union_volume_monitor_new (void)
517 GUnionVolumeMonitor *monitor;
519 monitor = g_object_new (G_TYPE_UNION_VOLUME_MONITOR, NULL);
525 * g_volume_monitor_get:
527 * Gets the volume monitor used by gio.
529 * Returns: a reference to the #GVolumeMonitor used by gio. Call
530 * g_object_unref() when done with it.
533 g_volume_monitor_get (void)
537 g_static_rec_mutex_lock (&the_volume_monitor_mutex);
539 if (the_volume_monitor)
540 vm = G_VOLUME_MONITOR (g_object_ref (the_volume_monitor));
543 the_volume_monitor = g_union_volume_monitor_new ();
544 populate_union_monitor (the_volume_monitor);
545 vm = G_VOLUME_MONITOR (the_volume_monitor);
548 g_static_rec_mutex_unlock (&the_volume_monitor_mutex);
554 * _g_mount_get_for_mount_path:
555 * @mountpoint: a string.
556 * @cancellable: a #GCancellable, or %NULL
558 * Returns: a #GMount for given @mount_path or %NULL.
561 _g_mount_get_for_mount_path (const char *mount_path,
562 GCancellable *cancellable)
564 GNativeVolumeMonitorClass *klass;
567 klass = get_native_class ();
573 if (klass->get_mount_for_mount_path)
575 g_static_rec_mutex_lock (&the_volume_monitor_mutex);
576 mount = klass->get_mount_for_mount_path (mount_path, cancellable);
577 g_static_rec_mutex_unlock (&the_volume_monitor_mutex);
580 /* TODO: How do we know this succeeded? Keep in mind that the native
581 * volume monitor may fail (e.g. not being able to connect to
582 * hald). Is the get_mount_for_mount_path() method allowed to
583 * return NULL? Seems like it is ... probably the method needs
584 * to take a boolean and write if it succeeds or not.. Messy.
588 g_type_class_unref (klass);
594 * g_volume_monitor_adopt_orphan_mount:
595 * @mount: a #GMount object to find a parent for
597 * This function should be called by any #GVolumeMonitor
598 * implementation when a new #GMount object is created that is not
599 * associated with a #GVolume object. It must be called just before
600 * emitting the @mount_added signal.
602 * If the return value is not %NULL, the caller must associate the
603 * returned #GVolume object with the #GMount. This involves returning
604 * it in it's g_mount_get_volume() implementation. The caller must
605 * also listen for the "removed" signal on the returned object
606 * and give up it's reference when handling that signal
608 * Similary, if implementing g_volume_monitor_adopt_orphan_mount(),
609 * the implementor must take a reference to @mount and return it in
610 * it's g_volume_get_mount() implemented. Also, the implementor must
611 * listen for the "unmounted" signal on @mount and give up it's
612 * reference upon handling that signal.
614 * There are two main use cases for this function.
616 * One is when implementing a user space file system driver that reads
617 * blocks of a block device that is already represented by the native
618 * volume monitor (for example a CD Audio file system driver). Such
619 * a driver will generate it's own #GMount object that needs to be
620 * assoicated with the #GVolume object that represents the volume.
622 * The other is for implementing a #GVolumeMonitor whose sole purpose
623 * is to return #GVolume objects representing entries in the users
624 * "favorite servers" list or similar.
626 * Returns: the #GVolume object that is the parent for @mount or %NULL
627 * if no wants to adopt the #GMount.
630 g_volume_monitor_adopt_orphan_mount (GMount *mount)
632 GVolumeMonitor *child_monitor;
633 GVolumeMonitorClass *child_monitor_class;
637 g_return_val_if_fail (mount != NULL, NULL);
639 if (the_volume_monitor == NULL)
644 g_static_rec_mutex_lock (&the_volume_monitor_mutex);
646 for (l = the_volume_monitor->monitors; l != NULL; l = l->next)
648 child_monitor = l->data;
649 child_monitor_class = G_VOLUME_MONITOR_GET_CLASS (child_monitor);
651 if (child_monitor_class->adopt_orphan_mount != NULL)
653 volume = child_monitor_class->adopt_orphan_mount (mount, child_monitor);
659 g_static_rec_mutex_unlock (&the_volume_monitor_mutex);
665 #define __G_UNION_VOLUME_MONITOR_C__
666 #include "gioaliasdef.c"