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;
63 monitor = G_UNION_VOLUME_MONITOR (object);
65 while (monitor->monitors != NULL)
66 g_union_volume_monitor_remove_monitor (monitor,
67 monitor->monitors->data);
69 if (G_OBJECT_CLASS (g_union_volume_monitor_parent_class)->finalize)
70 (*G_OBJECT_CLASS (g_union_volume_monitor_parent_class)->finalize) (object);
74 g_union_volume_monitor_dispose (GObject *object)
76 GUnionVolumeMonitor *monitor;
78 monitor = G_UNION_VOLUME_MONITOR (object);
80 G_LOCK (the_volume_monitor);
81 the_volume_monitor = NULL;
82 G_UNLOCK (the_volume_monitor);
84 if (G_OBJECT_CLASS (g_union_volume_monitor_parent_class)->dispose)
85 (*G_OBJECT_CLASS (g_union_volume_monitor_parent_class)->dispose) (object);
89 get_mounts (GVolumeMonitor *volume_monitor)
91 GUnionVolumeMonitor *monitor;
92 GVolumeMonitor *child_monitor;
96 monitor = G_UNION_VOLUME_MONITOR (volume_monitor);
100 G_LOCK (the_volume_monitor);
102 for (l = monitor->monitors; l != NULL; l = l->next)
104 child_monitor = l->data;
106 res = g_list_concat (res, g_volume_monitor_get_mounts (child_monitor));
109 G_UNLOCK (the_volume_monitor);
115 get_volumes (GVolumeMonitor *volume_monitor)
117 GUnionVolumeMonitor *monitor;
118 GVolumeMonitor *child_monitor;
122 monitor = G_UNION_VOLUME_MONITOR (volume_monitor);
126 G_LOCK (the_volume_monitor);
128 for (l = monitor->monitors; l != NULL; l = l->next)
130 child_monitor = l->data;
132 res = g_list_concat (res, g_volume_monitor_get_volumes (child_monitor));
135 G_UNLOCK (the_volume_monitor);
141 get_connected_drives (GVolumeMonitor *volume_monitor)
143 GUnionVolumeMonitor *monitor;
144 GVolumeMonitor *child_monitor;
148 monitor = G_UNION_VOLUME_MONITOR (volume_monitor);
152 G_LOCK (the_volume_monitor);
154 for (l = monitor->monitors; l != NULL; l = l->next)
156 child_monitor = l->data;
158 res = g_list_concat (res, g_volume_monitor_get_connected_drives (child_monitor));
161 G_UNLOCK (the_volume_monitor);
167 g_union_volume_monitor_class_init (GUnionVolumeMonitorClass *klass)
169 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
170 GVolumeMonitorClass *monitor_class = G_VOLUME_MONITOR_CLASS (klass);
172 gobject_class->finalize = g_union_volume_monitor_finalize;
173 gobject_class->dispose = g_union_volume_monitor_dispose;
175 monitor_class->get_connected_drives = get_connected_drives;
176 monitor_class->get_volumes = get_volumes;
177 monitor_class->get_mounts = get_mounts;
181 child_volume_added (GVolumeMonitor *child_monitor,
182 GVolume *child_volume,
183 GUnionVolumeMonitor *union_monitor)
185 g_signal_emit_by_name (union_monitor,
191 child_volume_removed (GVolumeMonitor *child_monitor,
192 GVolume *child_volume,
193 GUnionVolumeMonitor *union_monitor)
195 g_signal_emit_by_name (union_monitor,
201 child_volume_changed (GVolumeMonitor *child_monitor,
202 GVolume *child_volume,
203 GUnionVolumeMonitor *union_monitor)
205 g_signal_emit_by_name (union_monitor,
211 child_mount_added (GVolumeMonitor *child_monitor,
213 GUnionVolumeMonitor *union_monitor)
215 g_signal_emit_by_name (union_monitor,
221 child_mount_removed (GVolumeMonitor *child_monitor,
223 GUnionVolumeMonitor *union_monitor)
225 g_signal_emit_by_name (union_monitor,
231 child_mount_pre_unmount (GVolumeMonitor *child_monitor,
233 GUnionVolumeMonitor *union_monitor)
235 g_signal_emit_by_name (union_monitor,
242 child_mount_changed (GVolumeMonitor *child_monitor,
244 GUnionVolumeMonitor *union_monitor)
246 g_signal_emit_by_name (union_monitor,
252 child_drive_connected (GVolumeMonitor *child_monitor,
254 GUnionVolumeMonitor *union_monitor)
256 g_signal_emit_by_name (union_monitor,
262 child_drive_disconnected (GVolumeMonitor *child_monitor,
264 GUnionVolumeMonitor *union_monitor)
266 g_signal_emit_by_name (union_monitor,
267 "drive_disconnected",
272 child_drive_changed (GVolumeMonitor *child_monitor,
274 GUnionVolumeMonitor *union_monitor)
276 g_signal_emit_by_name (union_monitor,
282 g_union_volume_monitor_add_monitor (GUnionVolumeMonitor *union_monitor,
283 GVolumeMonitor *volume_monitor)
285 if (g_list_find (union_monitor->monitors, volume_monitor))
288 union_monitor->monitors =
289 g_list_prepend (union_monitor->monitors,
290 g_object_ref (volume_monitor));
292 g_signal_connect (volume_monitor, "volume_added", (GCallback)child_volume_added, union_monitor);
293 g_signal_connect (volume_monitor, "volume_removed", (GCallback)child_volume_removed, union_monitor);
294 g_signal_connect (volume_monitor, "volume_changed", (GCallback)child_volume_changed, union_monitor);
295 g_signal_connect (volume_monitor, "mount_added", (GCallback)child_mount_added, union_monitor);
296 g_signal_connect (volume_monitor, "mount_removed", (GCallback)child_mount_removed, union_monitor);
297 g_signal_connect (volume_monitor, "mount_pre_unmount", (GCallback)child_mount_pre_unmount, union_monitor);
298 g_signal_connect (volume_monitor, "mount_changed", (GCallback)child_mount_changed, union_monitor);
299 g_signal_connect (volume_monitor, "drive_connected", (GCallback)child_drive_connected, union_monitor);
300 g_signal_connect (volume_monitor, "drive_disconnected", (GCallback)child_drive_disconnected, union_monitor);
301 g_signal_connect (volume_monitor, "drive_changed", (GCallback)child_drive_changed, union_monitor);
305 g_union_volume_monitor_remove_monitor (GUnionVolumeMonitor *union_monitor,
306 GVolumeMonitor *child_monitor)
310 l = g_list_find (union_monitor->monitors, child_monitor);
314 union_monitor->monitors = g_list_delete_link (union_monitor->monitors, l);
316 g_signal_handlers_disconnect_by_func (child_monitor, child_volume_added, union_monitor);
317 g_signal_handlers_disconnect_by_func (child_monitor, child_volume_removed, union_monitor);
318 g_signal_handlers_disconnect_by_func (child_monitor, child_volume_changed, union_monitor);
319 g_signal_handlers_disconnect_by_func (child_monitor, child_mount_added, union_monitor);
320 g_signal_handlers_disconnect_by_func (child_monitor, child_mount_removed, union_monitor);
321 g_signal_handlers_disconnect_by_func (child_monitor, child_mount_pre_unmount, union_monitor);
322 g_signal_handlers_disconnect_by_func (child_monitor, child_mount_changed, union_monitor);
323 g_signal_handlers_disconnect_by_func (child_monitor, child_drive_connected, union_monitor);
324 g_signal_handlers_disconnect_by_func (child_monitor, child_drive_disconnected, union_monitor);
325 g_signal_handlers_disconnect_by_func (child_monitor, child_drive_changed, union_monitor);
329 get_default_native_type (gpointer data)
331 GNativeVolumeMonitorClass *klass;
335 GType *ret = (GType *) data;
340 /* Ensure GUnixVolumeMonitor type is available */
342 volatile GType unix_type;
343 /* volatile is required to avoid any G_GNUC_CONST optimizations */
344 unix_type = _g_unix_volume_monitor_get_type ();
348 /* Ensure vfs in modules loaded */
349 _g_io_modules_ensure_loaded ();
351 monitors = g_type_children (G_TYPE_NATIVE_VOLUME_MONITOR, &n_monitors);
355 for (i = 0; i < n_monitors; i++)
357 klass = G_NATIVE_VOLUME_MONITOR_CLASS (g_type_class_ref (monitors[i]));
358 if (klass->priority > native_prio)
360 native_prio = klass->priority;
361 native_type = monitors[i];
364 g_type_class_unref (klass);
375 get_native_type (void)
377 static GOnce once_init = G_ONCE_INIT;
378 static GType type = G_TYPE_INVALID;
380 g_once (&once_init, get_default_native_type, &type);
386 g_union_volume_monitor_init (GUnionVolumeMonitor *union_monitor)
388 GVolumeMonitor *monitor;
394 native_type = get_native_type ();
396 if (native_type != G_TYPE_INVALID)
398 monitor = g_object_new (native_type, NULL);
399 g_union_volume_monitor_add_monitor (union_monitor, monitor);
400 g_object_unref (monitor);
403 monitors = g_type_children (G_TYPE_VOLUME_MONITOR, &n_monitors);
405 for (i = 0; i < n_monitors; i++)
407 if (monitors[i] == G_TYPE_UNION_VOLUME_MONITOR ||
408 g_type_is_a (monitors[i], G_TYPE_NATIVE_VOLUME_MONITOR))
411 monitor = g_object_new (monitors[i], NULL);
412 g_union_volume_monitor_add_monitor (union_monitor, monitor);
413 g_object_unref (monitor);
419 static GUnionVolumeMonitor *
420 g_union_volume_monitor_new (void)
422 GUnionVolumeMonitor *monitor;
424 monitor = g_object_new (G_TYPE_UNION_VOLUME_MONITOR, NULL);
431 * g_volume_monitor_get:
433 * Gets the volume monitor used by gio.
435 * Returns: a reference to the #GVolumeMonitor used by gio. Call
436 * g_object_unref() when done with it.
439 g_volume_monitor_get (void)
443 G_LOCK (the_volume_monitor);
445 if (the_volume_monitor)
446 vm = G_VOLUME_MONITOR (g_object_ref (the_volume_monitor));
449 the_volume_monitor = g_union_volume_monitor_new ();
450 vm = G_VOLUME_MONITOR (the_volume_monitor);
453 G_UNLOCK (the_volume_monitor);
459 * _g_mount_get_for_mount_path:
460 * @mountpoint: a string.
462 * Returns: a #GMount for given @mount_path or %NULL.
465 _g_mount_get_for_mount_path (const char *mount_path)
468 GNativeVolumeMonitorClass *klass;
471 native_type = get_native_type ();
473 if (native_type == G_TYPE_INVALID)
478 klass = G_NATIVE_VOLUME_MONITOR_CLASS (g_type_class_ref (native_type));
479 if (klass->get_mount_for_mount_path)
480 mount = klass->get_mount_for_mount_path (mount_path);
482 g_type_class_unref (klass);
487 #define __G_UNION_VOLUME_MONITOR_C__
488 #include "gioaliasdef.c"