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>
28 #include "gunionvolumemonitor.h"
29 #include "gvolumeprivate.h"
30 #include "giomodule.h"
32 #include "gunixvolumemonitor.h"
34 #include "gnativevolumemonitor.h"
40 struct _GUnionVolumeMonitor {
41 GVolumeMonitor parent;
46 static void g_union_volume_monitor_remove_monitor (GUnionVolumeMonitor *union_monitor,
47 GVolumeMonitor *child_monitor);
50 #define g_union_volume_monitor_get_type _g_union_volume_monitor_get_type
51 G_DEFINE_TYPE (GUnionVolumeMonitor, g_union_volume_monitor, G_TYPE_VOLUME_MONITOR);
54 G_LOCK_DEFINE_STATIC(the_volume_monitor);
55 static GUnionVolumeMonitor *the_volume_monitor = NULL;
58 g_union_volume_monitor_finalize (GObject *object)
60 GUnionVolumeMonitor *monitor;
62 monitor = G_UNION_VOLUME_MONITOR (object);
64 while (monitor->monitors != NULL)
65 g_union_volume_monitor_remove_monitor (monitor,
66 monitor->monitors->data);
68 if (G_OBJECT_CLASS (g_union_volume_monitor_parent_class)->finalize)
69 (*G_OBJECT_CLASS (g_union_volume_monitor_parent_class)->finalize) (object);
73 g_union_volume_monitor_dispose (GObject *object)
75 GUnionVolumeMonitor *monitor;
77 monitor = G_UNION_VOLUME_MONITOR (object);
79 G_LOCK (the_volume_monitor);
80 the_volume_monitor = NULL;
81 G_UNLOCK (the_volume_monitor);
83 if (G_OBJECT_CLASS (g_union_volume_monitor_parent_class)->dispose)
84 (*G_OBJECT_CLASS (g_union_volume_monitor_parent_class)->dispose) (object);
88 get_mounted_volumes (GVolumeMonitor *volume_monitor)
90 GUnionVolumeMonitor *monitor;
91 GVolumeMonitor *child_monitor;
95 monitor = G_UNION_VOLUME_MONITOR (volume_monitor);
99 G_LOCK (the_volume_monitor);
101 for (l = monitor->monitors; l != NULL; l = l->next)
103 child_monitor = l->data;
105 res = g_list_concat (res,
106 g_volume_monitor_get_mounted_volumes (child_monitor));
109 G_UNLOCK (the_volume_monitor);
115 get_connected_drives (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,
133 g_volume_monitor_get_connected_drives (child_monitor));
136 G_UNLOCK (the_volume_monitor);
142 g_union_volume_monitor_class_init (GUnionVolumeMonitorClass *klass)
144 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
145 GVolumeMonitorClass *monitor_class = G_VOLUME_MONITOR_CLASS (klass);
147 gobject_class->finalize = g_union_volume_monitor_finalize;
148 gobject_class->dispose = g_union_volume_monitor_dispose;
150 monitor_class->get_mounted_volumes = get_mounted_volumes;
151 monitor_class->get_connected_drives = get_connected_drives;
155 child_volume_mounted (GVolumeMonitor *child_monitor,
156 GVolume *child_volume,
157 GUnionVolumeMonitor *union_monitor)
159 g_signal_emit_by_name (union_monitor,
165 child_volume_pre_unmount (GVolumeMonitor *child_monitor,
166 GVolume *child_volume,
167 GUnionVolumeMonitor *union_monitor)
169 g_signal_emit_by_name (union_monitor,
170 "volume_pre_unmount",
175 child_volume_unmounted (GVolumeMonitor *child_monitor,
176 GVolume *child_volume,
177 GUnionVolumeMonitor *union_monitor)
179 g_signal_emit_by_name (union_monitor,
185 child_drive_connected (GVolumeMonitor *child_monitor,
187 GUnionVolumeMonitor *union_monitor)
189 g_signal_emit_by_name (union_monitor,
195 child_drive_disconnected (GVolumeMonitor *child_monitor,
197 GUnionVolumeMonitor *union_monitor)
199 g_signal_emit_by_name (union_monitor,
200 "drive_disconnected",
205 g_union_volume_monitor_add_monitor (GUnionVolumeMonitor *union_monitor,
206 GVolumeMonitor *volume_monitor)
208 if (g_list_find (union_monitor->monitors, volume_monitor))
211 union_monitor->monitors =
212 g_list_prepend (union_monitor->monitors,
213 g_object_ref (volume_monitor));
215 g_signal_connect (volume_monitor, "volume_mounted", (GCallback)child_volume_mounted, union_monitor);
216 g_signal_connect (volume_monitor, "volume_pre_unmount", (GCallback)child_volume_pre_unmount, union_monitor);
217 g_signal_connect (volume_monitor, "volume_unmounted", (GCallback)child_volume_unmounted, union_monitor);
218 g_signal_connect (volume_monitor, "drive_connected", (GCallback)child_drive_connected, union_monitor);
219 g_signal_connect (volume_monitor, "drive_disconnected", (GCallback)child_drive_disconnected, union_monitor);
223 g_union_volume_monitor_remove_monitor (GUnionVolumeMonitor *union_monitor,
224 GVolumeMonitor *child_monitor)
228 l = g_list_find (union_monitor->monitors, child_monitor);
232 union_monitor->monitors = g_list_delete_link (union_monitor->monitors, l);
234 g_signal_handlers_disconnect_by_func (child_monitor, child_volume_mounted, union_monitor);
235 g_signal_handlers_disconnect_by_func (child_monitor, child_volume_pre_unmount, union_monitor);
236 g_signal_handlers_disconnect_by_func (child_monitor, child_volume_unmounted, union_monitor);
237 g_signal_handlers_disconnect_by_func (child_monitor, child_drive_connected, union_monitor);
238 g_signal_handlers_disconnect_by_func (child_monitor, child_drive_disconnected, union_monitor);
242 get_default_native_type (gpointer data)
244 GNativeVolumeMonitorClass *klass;
248 GType *ret = (GType *) data;
253 /* Ensure GUnixVolumeMonitor type is available */
255 volatile GType unix_type;
256 /* volatile is required to avoid any G_GNUC_CONST optimizations */
257 unix_type = _g_unix_volume_monitor_get_type ();
261 /* Ensure vfs in modules loaded */
262 g_io_modules_ensure_loaded (GIO_MODULE_DIR);
264 monitors = g_type_children (G_TYPE_NATIVE_VOLUME_MONITOR, &n_monitors);
268 for (i = 0; i < n_monitors; i++)
270 klass = G_NATIVE_VOLUME_MONITOR_CLASS (g_type_class_ref (monitors[i]));
271 if (klass->priority > native_prio)
273 native_prio = klass->priority;
274 native_type = monitors[i];
277 g_type_class_unref (klass);
288 get_native_type (void)
290 static GOnce once_init = G_ONCE_INIT;
291 static GType type = G_TYPE_INVALID;
293 g_once (&once_init, get_default_native_type, &type);
299 g_union_volume_monitor_init (GUnionVolumeMonitor *union_monitor)
301 GVolumeMonitor *monitor;
307 native_type = get_native_type ();
309 if (native_type != G_TYPE_INVALID)
311 monitor = g_object_new (native_type, NULL);
312 g_union_volume_monitor_add_monitor (union_monitor, monitor);
313 g_object_unref (monitor);
316 monitors = g_type_children (G_TYPE_VOLUME_MONITOR, &n_monitors);
318 for (i = 0; i < n_monitors; i++)
320 if (monitors[i] == G_TYPE_UNION_VOLUME_MONITOR ||
321 g_type_is_a (monitors[i], G_TYPE_NATIVE_VOLUME_MONITOR))
324 monitor = g_object_new (monitors[i], NULL);
325 g_union_volume_monitor_add_monitor (union_monitor, monitor);
326 g_object_unref (monitor);
332 static GUnionVolumeMonitor *
333 g_union_volume_monitor_new (void)
335 GUnionVolumeMonitor *monitor;
337 monitor = g_object_new (G_TYPE_UNION_VOLUME_MONITOR, NULL);
344 * g_volume_monitor_get:
346 * Gets the volume monitor used by gio.
348 * Returns: a reference to the #GVolumeMonitor used by gio. Call
349 * g_object_unref() when done with it.
352 g_volume_monitor_get (void)
356 G_LOCK (the_volume_monitor);
358 if (the_volume_monitor)
359 vm = G_VOLUME_MONITOR (g_object_ref (the_volume_monitor));
362 the_volume_monitor = g_union_volume_monitor_new ();
363 vm = G_VOLUME_MONITOR (the_volume_monitor);
366 G_UNLOCK (the_volume_monitor);
372 * g_volume_get_for_mount_path:
373 * @mountpoint: a string.
375 * Returns: a #GVolume for given @mountpoint or %NULL.
378 _g_volume_get_for_mount_path (const char *mountpoint)
381 GNativeVolumeMonitorClass *klass;
384 native_type = get_native_type ();
386 if (native_type == G_TYPE_INVALID)
391 klass = G_NATIVE_VOLUME_MONITOR_CLASS (g_type_class_ref (native_type));
392 if (klass->get_volume_for_mountpoint)
393 volume = klass->get_volume_for_mountpoint (mountpoint);
395 g_type_class_unref (klass);
400 #define __G_UNION_VOLUME_MONITOR_C__
401 #include "gioaliasdef.c"