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"
36 struct _GUnixVolumeMonitor {
37 GNativeVolumeMonitor parent;
39 GUnixMountMonitor *mount_monitor;
41 GList *last_mountpoints;
48 static void mountpoints_changed (GUnixMountMonitor *mount_monitor,
50 static void mounts_changed (GUnixMountMonitor *mount_monitor,
52 static void update_drives (GUnixVolumeMonitor *monitor);
53 static void update_volumes (GUnixVolumeMonitor *monitor);
55 G_DEFINE_TYPE (GUnixVolumeMonitor, g_unix_volume_monitor, G_TYPE_NATIVE_VOLUME_MONITOR);
58 g_unix_volume_monitor_finalize (GObject *object)
60 GUnixVolumeMonitor *monitor;
62 monitor = G_UNIX_VOLUME_MONITOR (object);
64 g_signal_handlers_disconnect_by_func (monitor->mount_monitor, mountpoints_changed, monitor);
65 g_signal_handlers_disconnect_by_func (monitor->mount_monitor, mounts_changed, monitor);
67 g_object_unref (monitor->mount_monitor);
69 g_list_foreach (monitor->last_mounts, (GFunc)g_unix_mount_free, NULL);
70 g_list_free (monitor->last_mounts);
72 g_list_foreach (monitor->volumes, (GFunc)g_object_unref, NULL);
73 g_list_free (monitor->volumes);
74 g_list_foreach (monitor->drives, (GFunc)g_object_unref, NULL);
75 g_list_free (monitor->drives);
77 if (G_OBJECT_CLASS (g_unix_volume_monitor_parent_class)->finalize)
78 (*G_OBJECT_CLASS (g_unix_volume_monitor_parent_class)->finalize) (object);
82 get_mounted_volumes (GVolumeMonitor *volume_monitor)
84 GUnixVolumeMonitor *monitor;
87 monitor = G_UNIX_VOLUME_MONITOR (volume_monitor);
89 l = g_list_copy (monitor->volumes);
90 g_list_foreach (l, (GFunc)g_object_ref, NULL);
96 get_connected_drives (GVolumeMonitor *volume_monitor)
98 GUnixVolumeMonitor *monitor;
101 monitor = G_UNIX_VOLUME_MONITOR (volume_monitor);
103 l = g_list_copy (monitor->drives);
104 g_list_foreach (l, (GFunc)g_object_ref, NULL);
110 get_volume_for_mountpoint (const char *mountpoint)
115 mount = g_get_unix_mount_at (mountpoint, NULL);
117 /* TODO: Set drive? */
118 volume = g_unix_volume_new (mount, NULL);
120 return G_VOLUME (volume);
124 g_unix_volume_monitor_class_init (GUnixVolumeMonitorClass *klass)
126 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
127 GVolumeMonitorClass *monitor_class = G_VOLUME_MONITOR_CLASS (klass);
128 GNativeVolumeMonitorClass *native_class = G_NATIVE_VOLUME_MONITOR_CLASS (klass);
130 gobject_class->finalize = g_unix_volume_monitor_finalize;
132 monitor_class->get_mounted_volumes = get_mounted_volumes;
133 monitor_class->get_connected_drives = get_connected_drives;
135 native_class->priority = 0;
136 native_class->get_volume_for_mountpoint = get_volume_for_mountpoint;
140 mountpoints_changed (GUnixMountMonitor *mount_monitor,
143 GUnixVolumeMonitor *unix_monitor = user_data;
145 /* Update both to make sure drives are created before volumes */
146 update_drives (unix_monitor);
147 update_volumes (unix_monitor);
151 mounts_changed (GUnixMountMonitor *mount_monitor,
154 GUnixVolumeMonitor *unix_monitor = user_data;
156 /* Update both to make sure drives are created before volumes */
157 update_drives (unix_monitor);
158 update_volumes (unix_monitor);
162 g_unix_volume_monitor_init (GUnixVolumeMonitor *unix_monitor)
165 unix_monitor->mount_monitor = g_unix_mount_monitor_new ();
167 g_signal_connect (unix_monitor->mount_monitor,
168 "mounts_changed", G_CALLBACK (mounts_changed),
171 g_signal_connect (unix_monitor->mount_monitor,
172 "mountpoints_changed", G_CALLBACK (mountpoints_changed),
175 update_drives (unix_monitor);
176 update_volumes (unix_monitor);
181 * g_unix_volume_monitor_new:
183 * Returns: a new #GVolumeMonitor.
186 g_unix_volume_monitor_new (void)
188 GUnixVolumeMonitor *monitor;
190 monitor = g_object_new (G_TYPE_UNIX_VOLUME_MONITOR, NULL);
192 return G_VOLUME_MONITOR (monitor);
196 diff_sorted_lists (GList *list1, GList *list2, GCompareFunc compare,
197 GList **added, GList **removed)
201 *added = *removed = NULL;
203 while (list1 != NULL &&
206 order = (*compare) (list1->data, list2->data);
209 *removed = g_list_prepend (*removed, list1->data);
214 *added = g_list_prepend (*added, list2->data);
224 while (list1 != NULL)
226 *removed = g_list_prepend (*removed, list1->data);
229 while (list2 != NULL)
231 *added = g_list_prepend (*added, list2->data);
237 * g_unix_volume_lookup_drive_for_mountpoint:
241 * Returns: #GUnixDrive for the given @mountpoint.
244 g_unix_volume_monitor_lookup_drive_for_mountpoint (GUnixVolumeMonitor *monitor,
245 const char *mountpoint)
249 for (l = monitor->drives; l != NULL; l = l->next)
251 GUnixDrive *drive = l->data;
253 if (g_unix_drive_has_mountpoint (drive, mountpoint))
261 find_volume_by_mountpoint (GUnixVolumeMonitor *monitor,
262 const char *mountpoint)
266 for (l = monitor->volumes; l != NULL; l = l->next)
268 GUnixVolume *volume = l->data;
270 if (g_unix_volume_has_mountpoint (volume, mountpoint))
278 update_drives (GUnixVolumeMonitor *monitor)
280 GList *new_mountpoints;
281 GList *removed, *added;
285 new_mountpoints = g_get_unix_mount_points (NULL);
287 new_mountpoints = g_list_sort (new_mountpoints, (GCompareFunc) g_unix_mount_point_compare);
289 diff_sorted_lists (monitor->last_mountpoints,
290 new_mountpoints, (GCompareFunc) g_unix_mount_point_compare,
293 for (l = removed; l != NULL; l = l->next)
295 GUnixMountPoint *mountpoint = l->data;
297 drive = g_unix_volume_monitor_lookup_drive_for_mountpoint (monitor,
298 g_unix_mount_point_get_mount_path (mountpoint));
301 g_unix_drive_disconnected (drive);
302 monitor->drives = g_list_remove (monitor->drives, drive);
303 g_signal_emit_by_name (monitor, "drive_disconnected", drive);
304 g_object_unref (drive);
308 for (l = added; l != NULL; l = l->next)
310 GUnixMountPoint *mountpoint = l->data;
312 drive = g_unix_drive_new (G_VOLUME_MONITOR (monitor), mountpoint);
315 monitor->drives = g_list_prepend (monitor->drives, drive);
316 g_signal_emit_by_name (monitor, "drive_connected", drive);
321 g_list_free (removed);
322 g_list_foreach (monitor->last_mountpoints,
323 (GFunc)g_unix_mount_point_free, NULL);
324 g_list_free (monitor->last_mountpoints);
325 monitor->last_mountpoints = new_mountpoints;
329 update_volumes (GUnixVolumeMonitor *monitor)
332 GList *removed, *added;
336 const char *mount_path;
338 new_mounts = g_get_unix_mounts (NULL);
340 new_mounts = g_list_sort (new_mounts, (GCompareFunc) g_unix_mount_compare);
342 diff_sorted_lists (monitor->last_mounts,
343 new_mounts, (GCompareFunc) g_unix_mount_compare,
346 for (l = removed; l != NULL; l = l->next)
348 GUnixMount *mount = l->data;
350 volume = find_volume_by_mountpoint (monitor, g_unix_mount_get_mount_path (mount));
353 g_unix_volume_unmounted (volume);
354 monitor->volumes = g_list_remove (monitor->volumes, volume);
355 g_signal_emit_by_name (monitor, "volume_unmounted", volume);
356 g_object_unref (volume);
360 for (l = added; l != NULL; l = l->next)
362 GUnixMount *mount = l->data;
364 mount_path = g_unix_mount_get_mount_path (mount);
366 drive = g_unix_volume_monitor_lookup_drive_for_mountpoint (monitor,
368 volume = g_unix_volume_new (mount, drive);
371 monitor->volumes = g_list_prepend (monitor->volumes, volume);
372 g_signal_emit_by_name (monitor, "volume_mounted", volume);
377 g_list_free (removed);
378 g_list_foreach (monitor->last_mounts,
379 (GFunc)g_unix_mount_free, NULL);
380 g_list_free (monitor->last_mounts);
381 monitor->last_mounts = new_mounts;