1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
21 * David Zeuthen <davidz@redhat.com>
29 #include "gunionvolumemonitor.h"
30 #include "gmountprivate.h"
31 #include "giomodule-priv.h"
33 #include "gunixvolumemonitor.h"
35 #include "gnativevolumemonitor.h"
41 struct _GUnionVolumeMonitor {
42 GVolumeMonitor parent;
47 static void g_union_volume_monitor_remove_monitor (GUnionVolumeMonitor *union_monitor,
48 GVolumeMonitor *child_monitor);
51 #define g_union_volume_monitor_get_type _g_union_volume_monitor_get_type
52 G_DEFINE_TYPE (GUnionVolumeMonitor, g_union_volume_monitor, G_TYPE_VOLUME_MONITOR);
55 G_LOCK_DEFINE_STATIC(the_volume_monitor);
56 static GUnionVolumeMonitor *the_volume_monitor = NULL;
59 g_union_volume_monitor_finalize (GObject *object)
61 GUnionVolumeMonitor *monitor;
62 GVolumeMonitor *child_monitor;
64 monitor = G_UNION_VOLUME_MONITOR (object);
66 while (monitor->monitors != NULL) {
67 child_monitor = monitor->monitors->data;
68 g_union_volume_monitor_remove_monitor (monitor,
70 g_object_unref (child_monitor);
74 if (G_OBJECT_CLASS (g_union_volume_monitor_parent_class)->finalize)
75 (*G_OBJECT_CLASS (g_union_volume_monitor_parent_class)->finalize) (object);
79 g_union_volume_monitor_dispose (GObject *object)
81 GUnionVolumeMonitor *monitor;
83 monitor = G_UNION_VOLUME_MONITOR (object);
85 G_LOCK (the_volume_monitor);
86 the_volume_monitor = NULL;
87 G_UNLOCK (the_volume_monitor);
89 if (G_OBJECT_CLASS (g_union_volume_monitor_parent_class)->dispose)
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_LOCK (the_volume_monitor);
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_UNLOCK (the_volume_monitor);
120 get_volumes (GVolumeMonitor *volume_monitor)
122 GUnionVolumeMonitor *monitor;
123 GVolumeMonitor *child_monitor;
127 monitor = G_UNION_VOLUME_MONITOR (volume_monitor);
131 G_LOCK (the_volume_monitor);
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_UNLOCK (the_volume_monitor);
146 get_connected_drives (GVolumeMonitor *volume_monitor)
148 GUnionVolumeMonitor *monitor;
149 GVolumeMonitor *child_monitor;
153 monitor = G_UNION_VOLUME_MONITOR (volume_monitor);
157 G_LOCK (the_volume_monitor);
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_UNLOCK (the_volume_monitor);
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_LOCK (the_volume_monitor);
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_UNLOCK (the_volume_monitor);
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_LOCK (the_volume_monitor);
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_UNLOCK (the_volume_monitor);
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 g_union_volume_monitor_add_monitor (GUnionVolumeMonitor *union_monitor,
348 GVolumeMonitor *volume_monitor)
350 if (g_list_find (union_monitor->monitors, volume_monitor))
353 union_monitor->monitors =
354 g_list_prepend (union_monitor->monitors,
355 g_object_ref (volume_monitor));
357 g_signal_connect (volume_monitor, "volume_added", (GCallback)child_volume_added, union_monitor);
358 g_signal_connect (volume_monitor, "volume_removed", (GCallback)child_volume_removed, union_monitor);
359 g_signal_connect (volume_monitor, "volume_changed", (GCallback)child_volume_changed, union_monitor);
360 g_signal_connect (volume_monitor, "mount_added", (GCallback)child_mount_added, union_monitor);
361 g_signal_connect (volume_monitor, "mount_removed", (GCallback)child_mount_removed, union_monitor);
362 g_signal_connect (volume_monitor, "mount_pre_unmount", (GCallback)child_mount_pre_unmount, union_monitor);
363 g_signal_connect (volume_monitor, "mount_changed", (GCallback)child_mount_changed, union_monitor);
364 g_signal_connect (volume_monitor, "drive_connected", (GCallback)child_drive_connected, union_monitor);
365 g_signal_connect (volume_monitor, "drive_disconnected", (GCallback)child_drive_disconnected, union_monitor);
366 g_signal_connect (volume_monitor, "drive_changed", (GCallback)child_drive_changed, union_monitor);
370 g_union_volume_monitor_remove_monitor (GUnionVolumeMonitor *union_monitor,
371 GVolumeMonitor *child_monitor)
375 l = g_list_find (union_monitor->monitors, child_monitor);
379 union_monitor->monitors = g_list_delete_link (union_monitor->monitors, l);
381 g_signal_handlers_disconnect_by_func (child_monitor, child_volume_added, union_monitor);
382 g_signal_handlers_disconnect_by_func (child_monitor, child_volume_removed, union_monitor);
383 g_signal_handlers_disconnect_by_func (child_monitor, child_volume_changed, union_monitor);
384 g_signal_handlers_disconnect_by_func (child_monitor, child_mount_added, union_monitor);
385 g_signal_handlers_disconnect_by_func (child_monitor, child_mount_removed, union_monitor);
386 g_signal_handlers_disconnect_by_func (child_monitor, child_mount_pre_unmount, union_monitor);
387 g_signal_handlers_disconnect_by_func (child_monitor, child_mount_changed, union_monitor);
388 g_signal_handlers_disconnect_by_func (child_monitor, child_drive_connected, union_monitor);
389 g_signal_handlers_disconnect_by_func (child_monitor, child_drive_disconnected, union_monitor);
390 g_signal_handlers_disconnect_by_func (child_monitor, child_drive_changed, union_monitor);
393 /* Note: This compares in reverse order.
394 Higher prio -> sort first
397 compare_monitor_type (gconstpointer a,
401 GNativeVolumeMonitorClass *class_a, *class_b;
403 const char *use_this_monitor;
405 /* We ref:ed all the classes, so the peek is safe */
406 class_a = g_type_class_peek (*(GType *)a);
407 class_b = g_type_class_peek (*(GType *)b);
408 use_this_monitor = user_data;
410 if (class_a == class_b)
412 else if (use_this_monitor != NULL &&
413 strcmp (class_a->name, use_this_monitor) == 0)
415 else if (use_this_monitor != NULL &&
416 strcmp (class_b->name, use_this_monitor) == 0)
419 res = class_b->priority - class_a->priority;
425 get_default_native_class (gpointer data)
427 GNativeVolumeMonitorClass *klass;
430 GTypeClass *native_class;
431 const char *use_this;
434 use_this = g_getenv ("GIO_USE_VOLUME_MONITOR");
437 /* Ensure GUnixVolumeMonitor type is available */
439 volatile GType unix_type;
440 /* volatile is required to avoid any G_GNUC_CONST optimizations */
441 unix_type = _g_unix_volume_monitor_get_type ();
445 /* Ensure vfs in modules loaded */
446 _g_io_modules_ensure_loaded ();
448 monitors = g_type_children (G_TYPE_NATIVE_VOLUME_MONITOR, &n_monitors);
450 /* Ref all classes once so we don't load/unload them a lot */
451 for (i = 0; i < n_monitors; i++)
452 g_type_class_ref (monitors[i]);
454 g_qsort_with_data (monitors, n_monitors, sizeof (GType),
455 compare_monitor_type, (gpointer)use_this);
458 for (i = 0; i < n_monitors; i++)
460 klass = G_NATIVE_VOLUME_MONITOR_CLASS (g_type_class_ref (monitors[i]));
462 if (klass->is_supported ())
464 native_class = (GTypeClass *)klass;
467 g_type_class_unref (klass);
470 for (i = 0; i < n_monitors; i++)
471 g_type_class_unref (g_type_class_peek (monitors[i]));
482 static GOnce _once_init = G_ONCE_INIT;
484 g_once (&_once_init, (GThreadFunc)get_default_native_class, NULL);
486 return G_TYPE_FROM_CLASS (_once_init.retval);
490 g_union_volume_monitor_init (GUnionVolumeMonitor *union_monitor)
492 GVolumeMonitor *monitor;
498 native_type = get_native_type ();
500 if (native_type != G_TYPE_INVALID)
502 monitor = g_object_new (native_type, NULL);
503 g_union_volume_monitor_add_monitor (union_monitor, monitor);
504 g_object_unref (monitor);
507 monitors = g_type_children (G_TYPE_VOLUME_MONITOR, &n_monitors);
509 for (i = 0; i < n_monitors; i++)
511 if (monitors[i] == G_TYPE_UNION_VOLUME_MONITOR ||
512 g_type_is_a (monitors[i], G_TYPE_NATIVE_VOLUME_MONITOR))
515 monitor = g_object_new (monitors[i], NULL);
516 g_union_volume_monitor_add_monitor (union_monitor, monitor);
517 g_object_unref (monitor);
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_LOCK (the_volume_monitor);
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 vm = G_VOLUME_MONITOR (the_volume_monitor);
556 G_UNLOCK (the_volume_monitor);
562 * _g_mount_get_for_mount_path:
563 * @mountpoint: a string.
564 * @cancellable: a #GCancellable, or %NULL
566 * Returns: a #GMount for given @mount_path or %NULL.
569 _g_mount_get_for_mount_path (const char *mount_path,
570 GCancellable *cancellable)
573 GNativeVolumeMonitorClass *klass;
576 native_type = get_native_type ();
578 if (native_type == G_TYPE_INVALID)
583 klass = G_NATIVE_VOLUME_MONITOR_CLASS (g_type_class_ref (native_type));
584 if (klass->get_mount_for_mount_path)
586 G_LOCK (the_volume_monitor);
587 mount = klass->get_mount_for_mount_path (mount_path, cancellable);
588 G_UNLOCK (the_volume_monitor);
591 /* TODO: How do we know this succeeded? Keep in mind that the native
592 * volume monitor may fail (e.g. not being able to connect to
593 * hald). Is the get_mount_for_mount_path() method allowed to
594 * return NULL? Seems like it is ... probably the method needs
595 * to take a boolean and write if it succeeds or not.. Messy.
599 g_type_class_unref (klass);
604 #define __G_UNION_VOLUME_MONITOR_C__
605 #include "gioaliasdef.c"