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"
38 struct _GUnionVolumeMonitor {
39 GVolumeMonitor parent;
44 static void g_union_volume_monitor_remove_monitor (GUnionVolumeMonitor *union_monitor,
45 GVolumeMonitor *child_monitor);
48 G_DEFINE_TYPE (GUnionVolumeMonitor, g_union_volume_monitor, G_TYPE_VOLUME_MONITOR);
51 G_LOCK_DEFINE_STATIC(the_volume_monitor);
52 static GUnionVolumeMonitor *the_volume_monitor = NULL;
55 g_union_volume_monitor_finalize (GObject *object)
57 GUnionVolumeMonitor *monitor;
59 monitor = G_UNION_VOLUME_MONITOR (object);
61 while (monitor->monitors != NULL)
62 g_union_volume_monitor_remove_monitor (monitor,
63 monitor->monitors->data);
65 if (G_OBJECT_CLASS (g_union_volume_monitor_parent_class)->finalize)
66 (*G_OBJECT_CLASS (g_union_volume_monitor_parent_class)->finalize) (object);
70 g_union_volume_monitor_dispose (GObject *object)
72 GUnionVolumeMonitor *monitor;
74 monitor = G_UNION_VOLUME_MONITOR (object);
76 G_LOCK (the_volume_monitor);
77 the_volume_monitor = NULL;
78 G_UNLOCK (the_volume_monitor);
80 if (G_OBJECT_CLASS (g_union_volume_monitor_parent_class)->dispose)
81 (*G_OBJECT_CLASS (g_union_volume_monitor_parent_class)->dispose) (object);
85 get_mounted_volumes (GVolumeMonitor *volume_monitor)
87 GUnionVolumeMonitor *monitor;
88 GVolumeMonitor *child_monitor;
92 monitor = G_UNION_VOLUME_MONITOR (volume_monitor);
96 G_LOCK (the_volume_monitor);
98 for (l = monitor->monitors; l != NULL; l = l->next)
100 child_monitor = l->data;
102 res = g_list_concat (res,
103 g_volume_monitor_get_mounted_volumes (child_monitor));
106 G_UNLOCK (the_volume_monitor);
112 get_connected_drives (GVolumeMonitor *volume_monitor)
114 GUnionVolumeMonitor *monitor;
115 GVolumeMonitor *child_monitor;
119 monitor = G_UNION_VOLUME_MONITOR (volume_monitor);
123 G_LOCK (the_volume_monitor);
125 for (l = monitor->monitors; l != NULL; l = l->next)
127 child_monitor = l->data;
129 res = g_list_concat (res,
130 g_volume_monitor_get_connected_drives (child_monitor));
133 G_UNLOCK (the_volume_monitor);
139 g_union_volume_monitor_class_init (GUnionVolumeMonitorClass *klass)
141 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
142 GVolumeMonitorClass *monitor_class = G_VOLUME_MONITOR_CLASS (klass);
144 gobject_class->finalize = g_union_volume_monitor_finalize;
145 gobject_class->dispose = g_union_volume_monitor_dispose;
147 monitor_class->get_mounted_volumes = get_mounted_volumes;
148 monitor_class->get_connected_drives = get_connected_drives;
152 child_volume_mounted (GVolumeMonitor *child_monitor,
153 GVolume *child_volume,
154 GUnionVolumeMonitor *union_monitor)
156 g_signal_emit_by_name (union_monitor,
162 child_volume_pre_unmount (GVolumeMonitor *child_monitor,
163 GVolume *child_volume,
164 GUnionVolumeMonitor *union_monitor)
166 g_signal_emit_by_name (union_monitor,
167 "volume_pre_unmount",
172 child_volume_unmounted (GVolumeMonitor *child_monitor,
173 GVolume *child_volume,
174 GUnionVolumeMonitor *union_monitor)
176 g_signal_emit_by_name (union_monitor,
182 child_drive_connected (GVolumeMonitor *child_monitor,
184 GUnionVolumeMonitor *union_monitor)
186 g_signal_emit_by_name (union_monitor,
192 child_drive_disconnected (GVolumeMonitor *child_monitor,
194 GUnionVolumeMonitor *union_monitor)
196 g_signal_emit_by_name (union_monitor,
197 "drive_disconnected",
202 g_union_volume_monitor_add_monitor (GUnionVolumeMonitor *union_monitor,
203 GVolumeMonitor *volume_monitor)
205 if (g_list_find (union_monitor->monitors, volume_monitor))
208 union_monitor->monitors =
209 g_list_prepend (union_monitor->monitors,
210 g_object_ref (volume_monitor));
212 g_signal_connect (volume_monitor, "volume_mounted", (GCallback)child_volume_mounted, union_monitor);
213 g_signal_connect (volume_monitor, "volume_pre_unmount", (GCallback)child_volume_pre_unmount, union_monitor);
214 g_signal_connect (volume_monitor, "volume_unmounted", (GCallback)child_volume_unmounted, union_monitor);
215 g_signal_connect (volume_monitor, "drive_connected", (GCallback)child_drive_connected, union_monitor);
216 g_signal_connect (volume_monitor, "drive_disconnected", (GCallback)child_drive_disconnected, union_monitor);
220 g_union_volume_monitor_remove_monitor (GUnionVolumeMonitor *union_monitor,
221 GVolumeMonitor *child_monitor)
225 l = g_list_find (union_monitor->monitors, child_monitor);
229 union_monitor->monitors = g_list_delete_link (union_monitor->monitors, l);
231 g_signal_handlers_disconnect_by_func (child_monitor, child_volume_mounted, union_monitor);
232 g_signal_handlers_disconnect_by_func (child_monitor, child_volume_pre_unmount, union_monitor);
233 g_signal_handlers_disconnect_by_func (child_monitor, child_volume_unmounted, union_monitor);
234 g_signal_handlers_disconnect_by_func (child_monitor, child_drive_connected, union_monitor);
235 g_signal_handlers_disconnect_by_func (child_monitor, child_drive_disconnected, union_monitor);
239 get_default_native_type (gpointer data)
241 GNativeVolumeMonitorClass *klass;
245 GType *ret = (GType *) data;
250 /* Ensure GUnixVolumeMonitor type is available */
252 volatile GType unix_type;
253 /* volatile is required to avoid any G_GNUC_CONST optimizations */
254 unix_type = g_unix_volume_monitor_get_type ();
258 /* Ensure vfs in modules loaded */
259 g_io_modules_ensure_loaded (GIO_MODULE_DIR);
261 monitors = g_type_children (G_TYPE_NATIVE_VOLUME_MONITOR, &n_monitors);
265 for (i = 0; i < n_monitors; i++)
267 klass = G_NATIVE_VOLUME_MONITOR_CLASS (g_type_class_ref (monitors[i]));
268 if (klass->priority > native_prio)
270 native_prio = klass->priority;
271 native_type = monitors[i];
274 g_type_class_unref (klass);
285 get_native_type (void)
287 static GOnce once_init = G_ONCE_INIT;
288 static GType type = G_TYPE_INVALID;
290 g_once (&once_init, get_default_native_type, &type);
296 g_union_volume_monitor_init (GUnionVolumeMonitor *union_monitor)
298 GVolumeMonitor *monitor;
304 native_type = get_native_type ();
306 if (native_type != G_TYPE_INVALID)
308 monitor = g_object_new (native_type, NULL);
309 g_union_volume_monitor_add_monitor (union_monitor, monitor);
310 g_object_unref (monitor);
313 monitors = g_type_children (G_TYPE_VOLUME_MONITOR, &n_monitors);
315 for (i = 0; i < n_monitors; i++)
317 if (monitors[i] == G_TYPE_UNION_VOLUME_MONITOR ||
318 g_type_is_a (monitors[i], G_TYPE_NATIVE_VOLUME_MONITOR))
321 monitor = g_object_new (monitors[i], NULL);
322 g_union_volume_monitor_add_monitor (union_monitor, monitor);
323 g_object_unref (monitor);
329 static GUnionVolumeMonitor *
330 g_union_volume_monitor_new (void)
332 GUnionVolumeMonitor *monitor;
334 monitor = g_object_new (G_TYPE_UNION_VOLUME_MONITOR, NULL);
341 * g_volume_monitor_get:
343 * Returns: a #GVolumeMonitor.
346 g_volume_monitor_get (void)
350 G_LOCK (the_volume_monitor);
352 if (the_volume_monitor )
353 vm = G_VOLUME_MONITOR (g_object_ref (the_volume_monitor));
356 the_volume_monitor = g_union_volume_monitor_new ();
357 vm = G_VOLUME_MONITOR (the_volume_monitor);
360 G_UNLOCK (the_volume_monitor);
366 * g_volume_get_for_mount_path:
367 * @mountpoint: a string.
369 * Returns: a #GVolume for given @mountpoint or %NULL.
372 g_volume_get_for_mount_path (const char *mountpoint)
375 GNativeVolumeMonitorClass *klass;
378 native_type = get_native_type ();
380 if (native_type == G_TYPE_INVALID)
385 klass = G_NATIVE_VOLUME_MONITOR_CLASS (g_type_class_ref (native_type));
386 if (klass->get_volume_for_mountpoint)
387 volume = klass->get_volume_for_mountpoint (mountpoint);
389 g_type_class_unref (klass);