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 "gunixvolumemonitor.h"
29 #include "gunixmounts.h"
30 #include "gunixvolume.h"
31 #include "gunixdrive.h"
32 #include "gvolumeprivate.h"
37 struct _GUnixVolumeMonitor {
38 GNativeVolumeMonitor parent;
40 GUnixMountMonitor *mount_monitor;
42 GList *last_mountpoints;
49 static void mountpoints_changed (GUnixMountMonitor *mount_monitor,
51 static void mounts_changed (GUnixMountMonitor *mount_monitor,
53 static void update_drives (GUnixVolumeMonitor *monitor);
54 static void update_volumes (GUnixVolumeMonitor *monitor);
56 #define g_unix_volume_monitor_get_type _g_unix_volume_monitor_get_type
57 G_DEFINE_TYPE (GUnixVolumeMonitor, g_unix_volume_monitor, G_TYPE_NATIVE_VOLUME_MONITOR);
60 g_unix_volume_monitor_finalize (GObject *object)
62 GUnixVolumeMonitor *monitor;
64 monitor = G_UNIX_VOLUME_MONITOR (object);
66 g_signal_handlers_disconnect_by_func (monitor->mount_monitor, mountpoints_changed, monitor);
67 g_signal_handlers_disconnect_by_func (monitor->mount_monitor, mounts_changed, monitor);
69 g_object_unref (monitor->mount_monitor);
71 g_list_foreach (monitor->last_mounts, (GFunc)g_unix_mount_free, NULL);
72 g_list_free (monitor->last_mounts);
74 g_list_foreach (monitor->volumes, (GFunc)g_object_unref, NULL);
75 g_list_free (monitor->volumes);
76 g_list_foreach (monitor->drives, (GFunc)g_object_unref, NULL);
77 g_list_free (monitor->drives);
79 if (G_OBJECT_CLASS (g_unix_volume_monitor_parent_class)->finalize)
80 (*G_OBJECT_CLASS (g_unix_volume_monitor_parent_class)->finalize) (object);
84 get_mounted_volumes (GVolumeMonitor *volume_monitor)
86 GUnixVolumeMonitor *monitor;
89 monitor = G_UNIX_VOLUME_MONITOR (volume_monitor);
91 l = g_list_copy (monitor->volumes);
92 g_list_foreach (l, (GFunc)g_object_ref, NULL);
98 get_connected_drives (GVolumeMonitor *volume_monitor)
100 GUnixVolumeMonitor *monitor;
103 monitor = G_UNIX_VOLUME_MONITOR (volume_monitor);
105 l = g_list_copy (monitor->drives);
106 g_list_foreach (l, (GFunc)g_object_ref, NULL);
112 get_volume_for_mountpoint (const char *mountpoint)
117 mount = g_get_unix_mount_at (mountpoint, NULL);
119 /* TODO: Set drive? */
120 volume = _g_unix_volume_new (mount, NULL);
122 return G_VOLUME (volume);
126 g_unix_volume_monitor_class_init (GUnixVolumeMonitorClass *klass)
128 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
129 GVolumeMonitorClass *monitor_class = G_VOLUME_MONITOR_CLASS (klass);
130 GNativeVolumeMonitorClass *native_class = G_NATIVE_VOLUME_MONITOR_CLASS (klass);
132 gobject_class->finalize = g_unix_volume_monitor_finalize;
134 monitor_class->get_mounted_volumes = get_mounted_volumes;
135 monitor_class->get_connected_drives = get_connected_drives;
137 native_class->priority = 0;
138 native_class->get_volume_for_mountpoint = get_volume_for_mountpoint;
142 mountpoints_changed (GUnixMountMonitor *mount_monitor,
145 GUnixVolumeMonitor *unix_monitor = user_data;
147 /* Update both to make sure drives are created before volumes */
148 update_drives (unix_monitor);
149 update_volumes (unix_monitor);
153 mounts_changed (GUnixMountMonitor *mount_monitor,
156 GUnixVolumeMonitor *unix_monitor = user_data;
158 /* Update both to make sure drives are created before volumes */
159 update_drives (unix_monitor);
160 update_volumes (unix_monitor);
164 g_unix_volume_monitor_init (GUnixVolumeMonitor *unix_monitor)
167 unix_monitor->mount_monitor = g_unix_mount_monitor_new ();
169 g_signal_connect (unix_monitor->mount_monitor,
170 "mounts_changed", G_CALLBACK (mounts_changed),
173 g_signal_connect (unix_monitor->mount_monitor,
174 "mountpoints_changed", G_CALLBACK (mountpoints_changed),
177 update_drives (unix_monitor);
178 update_volumes (unix_monitor);
183 * g_unix_volume_monitor_new:
185 * Returns: a new #GVolumeMonitor.
188 _g_unix_volume_monitor_new (void)
190 GUnixVolumeMonitor *monitor;
192 monitor = g_object_new (G_TYPE_UNIX_VOLUME_MONITOR, NULL);
194 return G_VOLUME_MONITOR (monitor);
198 diff_sorted_lists (GList *list1, GList *list2, GCompareFunc compare,
199 GList **added, GList **removed)
203 *added = *removed = NULL;
205 while (list1 != NULL &&
208 order = (*compare) (list1->data, list2->data);
211 *removed = g_list_prepend (*removed, list1->data);
216 *added = g_list_prepend (*added, list2->data);
226 while (list1 != NULL)
228 *removed = g_list_prepend (*removed, list1->data);
231 while (list2 != NULL)
233 *added = g_list_prepend (*added, list2->data);
239 * g_unix_volume_lookup_drive_for_mountpoint:
243 * Returns: #GUnixDrive for the given @mountpoint.
246 _g_unix_volume_monitor_lookup_drive_for_mountpoint (GUnixVolumeMonitor *monitor,
247 const char *mountpoint)
251 for (l = monitor->drives; l != NULL; l = l->next)
253 GUnixDrive *drive = l->data;
255 if (_g_unix_drive_has_mountpoint (drive, mountpoint))
263 find_volume_by_mountpoint (GUnixVolumeMonitor *monitor,
264 const char *mountpoint)
268 for (l = monitor->volumes; l != NULL; l = l->next)
270 GUnixVolume *volume = l->data;
272 if (_g_unix_volume_has_mountpoint (volume, mountpoint))
280 update_drives (GUnixVolumeMonitor *monitor)
282 GList *new_mountpoints;
283 GList *removed, *added;
287 new_mountpoints = g_get_unix_mount_points (NULL);
289 new_mountpoints = g_list_sort (new_mountpoints, (GCompareFunc) g_unix_mount_point_compare);
291 diff_sorted_lists (monitor->last_mountpoints,
292 new_mountpoints, (GCompareFunc) g_unix_mount_point_compare,
295 for (l = removed; l != NULL; l = l->next)
297 GUnixMountPoint *mountpoint = l->data;
299 drive = _g_unix_volume_monitor_lookup_drive_for_mountpoint (monitor,
300 g_unix_mount_point_get_mount_path (mountpoint));
303 _g_unix_drive_disconnected (drive);
304 monitor->drives = g_list_remove (monitor->drives, drive);
305 g_signal_emit_by_name (monitor, "drive_disconnected", drive);
306 g_object_unref (drive);
310 for (l = added; l != NULL; l = l->next)
312 GUnixMountPoint *mountpoint = l->data;
314 drive = _g_unix_drive_new (G_VOLUME_MONITOR (monitor), mountpoint);
317 monitor->drives = g_list_prepend (monitor->drives, drive);
318 g_signal_emit_by_name (monitor, "drive_connected", drive);
323 g_list_free (removed);
324 g_list_foreach (monitor->last_mountpoints,
325 (GFunc)g_unix_mount_point_free, NULL);
326 g_list_free (monitor->last_mountpoints);
327 monitor->last_mountpoints = new_mountpoints;
331 update_volumes (GUnixVolumeMonitor *monitor)
334 GList *removed, *added;
338 const char *mount_path;
340 new_mounts = g_get_unix_mounts (NULL);
342 new_mounts = g_list_sort (new_mounts, (GCompareFunc) g_unix_mount_compare);
344 diff_sorted_lists (monitor->last_mounts,
345 new_mounts, (GCompareFunc) g_unix_mount_compare,
348 for (l = removed; l != NULL; l = l->next)
350 GUnixMount *mount = l->data;
352 volume = find_volume_by_mountpoint (monitor, g_unix_mount_get_mount_path (mount));
355 _g_unix_volume_unmounted (volume);
356 monitor->volumes = g_list_remove (monitor->volumes, volume);
357 g_signal_emit_by_name (monitor, "volume_unmounted", volume);
358 g_object_unref (volume);
362 for (l = added; l != NULL; l = l->next)
364 GUnixMount *mount = l->data;
366 mount_path = g_unix_mount_get_mount_path (mount);
368 drive = _g_unix_volume_monitor_lookup_drive_for_mountpoint (monitor,
370 volume = _g_unix_volume_new (mount, drive);
373 monitor->volumes = g_list_prepend (monitor->volumes, volume);
374 g_signal_emit_by_name (monitor, "volume_mounted", volume);
379 g_list_free (removed);
380 g_list_foreach (monitor->last_mounts,
381 (GFunc)g_unix_mount_free, NULL);
382 g_list_free (monitor->last_mounts);
383 monitor->last_mounts = new_mounts;