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"
35 struct _GUnixVolumeMonitor {
36 GNativeVolumeMonitor parent;
38 GUnixMountMonitor *mount_monitor;
40 GList *last_mountpoints;
47 static void mountpoints_changed (GUnixMountMonitor *mount_monitor,
49 static void mounts_changed (GUnixMountMonitor *mount_monitor,
51 static void update_drives (GUnixVolumeMonitor *monitor);
52 static void update_volumes (GUnixVolumeMonitor *monitor);
54 G_DEFINE_TYPE (GUnixVolumeMonitor, g_unix_volume_monitor, G_TYPE_NATIVE_VOLUME_MONITOR);
57 g_unix_volume_monitor_finalize (GObject *object)
59 GUnixVolumeMonitor *monitor;
61 monitor = G_UNIX_VOLUME_MONITOR (object);
63 g_signal_handlers_disconnect_by_func (monitor->mount_monitor, mountpoints_changed, monitor);
64 g_signal_handlers_disconnect_by_func (monitor->mount_monitor, mounts_changed, monitor);
66 g_object_unref (monitor->mount_monitor);
68 g_list_foreach (monitor->last_mounts, (GFunc)g_unix_mount_free, NULL);
69 g_list_free (monitor->last_mounts);
71 g_list_foreach (monitor->volumes, (GFunc)g_object_unref, NULL);
72 g_list_free (monitor->volumes);
73 g_list_foreach (monitor->drives, (GFunc)g_object_unref, NULL);
74 g_list_free (monitor->drives);
76 if (G_OBJECT_CLASS (g_unix_volume_monitor_parent_class)->finalize)
77 (*G_OBJECT_CLASS (g_unix_volume_monitor_parent_class)->finalize) (object);
81 get_mounted_volumes (GVolumeMonitor *volume_monitor)
83 GUnixVolumeMonitor *monitor;
86 monitor = G_UNIX_VOLUME_MONITOR (volume_monitor);
88 l = g_list_copy (monitor->volumes);
89 g_list_foreach (l, (GFunc)g_object_ref, NULL);
95 get_connected_drives (GVolumeMonitor *volume_monitor)
97 GUnixVolumeMonitor *monitor;
100 monitor = G_UNIX_VOLUME_MONITOR (volume_monitor);
102 l = g_list_copy (monitor->drives);
103 g_list_foreach (l, (GFunc)g_object_ref, NULL);
109 get_volume_for_mountpoint (const char *mountpoint)
114 mount = g_get_unix_mount_at (mountpoint, NULL);
116 /* TODO: Set drive? */
117 volume = g_unix_volume_new (mount, NULL);
119 return G_VOLUME (volume);
123 g_unix_volume_monitor_class_init (GUnixVolumeMonitorClass *klass)
125 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
126 GVolumeMonitorClass *monitor_class = G_VOLUME_MONITOR_CLASS (klass);
127 GNativeVolumeMonitorClass *native_class = G_NATIVE_VOLUME_MONITOR_CLASS (klass);
129 gobject_class->finalize = g_unix_volume_monitor_finalize;
131 monitor_class->get_mounted_volumes = get_mounted_volumes;
132 monitor_class->get_connected_drives = get_connected_drives;
134 native_class->priority = 0;
135 native_class->get_volume_for_mountpoint = get_volume_for_mountpoint;
139 mountpoints_changed (GUnixMountMonitor *mount_monitor,
142 GUnixVolumeMonitor *unix_monitor = user_data;
144 /* Update both to make sure drives are created before volumes */
145 update_drives (unix_monitor);
146 update_volumes (unix_monitor);
150 mounts_changed (GUnixMountMonitor *mount_monitor,
153 GUnixVolumeMonitor *unix_monitor = user_data;
155 /* Update both to make sure drives are created before volumes */
156 update_drives (unix_monitor);
157 update_volumes (unix_monitor);
161 g_unix_volume_monitor_init (GUnixVolumeMonitor *unix_monitor)
164 unix_monitor->mount_monitor = g_unix_mount_monitor_new ();
166 g_signal_connect (unix_monitor->mount_monitor,
167 "mounts_changed", G_CALLBACK (mounts_changed),
170 g_signal_connect (unix_monitor->mount_monitor,
171 "mountpoints_changed", G_CALLBACK (mountpoints_changed),
174 update_drives (unix_monitor);
175 update_volumes (unix_monitor);
180 * g_unix_volume_monitor_new:
182 * Returns: a new #GVolumeMonitor.
185 g_unix_volume_monitor_new (void)
187 GUnixVolumeMonitor *monitor;
189 monitor = g_object_new (G_TYPE_UNIX_VOLUME_MONITOR, NULL);
191 return G_VOLUME_MONITOR (monitor);
195 diff_sorted_lists (GList *list1, GList *list2, GCompareFunc compare,
196 GList **added, GList **removed)
200 *added = *removed = NULL;
202 while (list1 != NULL &&
205 order = (*compare) (list1->data, list2->data);
208 *removed = g_list_prepend (*removed, list1->data);
213 *added = g_list_prepend (*added, list2->data);
223 while (list1 != NULL)
225 *removed = g_list_prepend (*removed, list1->data);
228 while (list2 != NULL)
230 *added = g_list_prepend (*added, list2->data);
236 * g_unix_volume_lookup_drive_for_mountpoint:
240 * Returns: #GUnixDrive for the given @mountpoint.
243 g_unix_volume_monitor_lookup_drive_for_mountpoint (GUnixVolumeMonitor *monitor,
244 const char *mountpoint)
248 for (l = monitor->drives; l != NULL; l = l->next)
250 GUnixDrive *drive = l->data;
252 if (g_unix_drive_has_mountpoint (drive, mountpoint))
260 find_volume_by_mountpoint (GUnixVolumeMonitor *monitor,
261 const char *mountpoint)
265 for (l = monitor->volumes; l != NULL; l = l->next)
267 GUnixVolume *volume = l->data;
269 if (g_unix_volume_has_mountpoint (volume, mountpoint))
277 update_drives (GUnixVolumeMonitor *monitor)
279 GList *new_mountpoints;
280 GList *removed, *added;
284 new_mountpoints = g_get_unix_mount_points (NULL);
286 new_mountpoints = g_list_sort (new_mountpoints, (GCompareFunc) g_unix_mount_point_compare);
288 diff_sorted_lists (monitor->last_mountpoints,
289 new_mountpoints, (GCompareFunc) g_unix_mount_point_compare,
292 for (l = removed; l != NULL; l = l->next)
294 GUnixMountPoint *mountpoint = l->data;
296 drive = g_unix_volume_monitor_lookup_drive_for_mountpoint (monitor,
297 g_unix_mount_point_get_mount_path (mountpoint));
300 g_unix_drive_disconnected (drive);
301 monitor->drives = g_list_remove (monitor->drives, drive);
302 g_signal_emit_by_name (monitor, "drive_disconnected", drive);
303 g_object_unref (drive);
307 for (l = added; l != NULL; l = l->next)
309 GUnixMountPoint *mountpoint = l->data;
311 drive = g_unix_drive_new (G_VOLUME_MONITOR (monitor), mountpoint);
314 monitor->drives = g_list_prepend (monitor->drives, drive);
315 g_signal_emit_by_name (monitor, "drive_connected", drive);
320 g_list_free (removed);
321 g_list_foreach (monitor->last_mountpoints,
322 (GFunc)g_unix_mount_point_free, NULL);
323 g_list_free (monitor->last_mountpoints);
324 monitor->last_mountpoints = new_mountpoints;
328 update_volumes (GUnixVolumeMonitor *monitor)
331 GList *removed, *added;
335 const char *mount_path;
337 new_mounts = g_get_unix_mounts (NULL);
339 new_mounts = g_list_sort (new_mounts, (GCompareFunc) g_unix_mount_compare);
341 diff_sorted_lists (monitor->last_mounts,
342 new_mounts, (GCompareFunc) g_unix_mount_compare,
345 for (l = removed; l != NULL; l = l->next)
347 GUnixMount *mount = l->data;
349 volume = find_volume_by_mountpoint (monitor, g_unix_mount_get_mount_path (mount));
352 g_unix_volume_unmounted (volume);
353 monitor->volumes = g_list_remove (monitor->volumes, volume);
354 g_signal_emit_by_name (monitor, "volume_unmounted", volume);
355 g_object_unref (volume);
359 for (l = added; l != NULL; l = l->next)
361 GUnixMount *mount = l->data;
363 mount_path = g_unix_mount_get_mount_path (mount);
365 drive = g_unix_volume_monitor_lookup_drive_for_mountpoint (monitor,
367 volume = g_unix_volume_new (mount, drive);
370 monitor->volumes = g_list_prepend (monitor->volumes, volume);
371 g_signal_emit_by_name (monitor, "volume_mounted", volume);
376 g_list_free (removed);
377 g_list_foreach (monitor->last_mounts,
378 (GFunc)g_unix_mount_free, NULL);
379 g_list_free (monitor->last_mounts);
380 monitor->last_mounts = new_mounts;