1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
5 * Copyright (C) 2006-2007 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
22 * Author: Alexander Larsson <alexl@redhat.com>
23 * David Zeuthen <davidz@redhat.com>
31 #include "gunixvolumemonitor.h"
32 #include "gunixmounts.h"
33 #include "gunixmount.h"
34 #include "gunixvolume.h"
36 #include "gmountprivate.h"
37 #include "giomodule.h"
41 struct _GUnixVolumeMonitor {
42 GNativeVolumeMonitor parent;
44 GUnixMountMonitor *mount_monitor;
46 GList *last_mountpoints;
53 static void mountpoints_changed (GUnixMountMonitor *mount_monitor,
55 static void mounts_changed (GUnixMountMonitor *mount_monitor,
57 static void update_volumes (GUnixVolumeMonitor *monitor);
58 static void update_mounts (GUnixVolumeMonitor *monitor);
60 #define g_unix_volume_monitor_get_type _g_unix_volume_monitor_get_type
61 G_DEFINE_TYPE_WITH_CODE (GUnixVolumeMonitor, g_unix_volume_monitor, G_TYPE_NATIVE_VOLUME_MONITOR,
62 g_io_extension_point_implement (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME,
68 g_unix_volume_monitor_finalize (GObject *object)
70 GUnixVolumeMonitor *monitor;
72 monitor = G_UNIX_VOLUME_MONITOR (object);
74 g_signal_handlers_disconnect_by_func (monitor->mount_monitor, mountpoints_changed, monitor);
75 g_signal_handlers_disconnect_by_func (monitor->mount_monitor, mounts_changed, monitor);
77 g_object_unref (monitor->mount_monitor);
79 g_list_foreach (monitor->last_mountpoints, (GFunc)g_unix_mount_point_free, NULL);
80 g_list_free (monitor->last_mountpoints);
81 g_list_foreach (monitor->last_mounts, (GFunc)g_unix_mount_free, NULL);
82 g_list_free (monitor->last_mounts);
84 g_list_foreach (monitor->volumes, (GFunc)g_object_unref, NULL);
85 g_list_free (monitor->volumes);
86 g_list_foreach (monitor->mounts, (GFunc)g_object_unref, NULL);
87 g_list_free (monitor->mounts);
89 G_OBJECT_CLASS (g_unix_volume_monitor_parent_class)->finalize (object);
93 g_unix_volume_monitor_dispose (GObject *object)
95 GUnixVolumeMonitor *monitor;
97 monitor = G_UNIX_VOLUME_MONITOR (object);
98 g_list_foreach (monitor->volumes, (GFunc)g_object_unref, NULL);
99 g_list_free (monitor->volumes);
100 monitor->volumes = NULL;
102 g_list_foreach (monitor->mounts, (GFunc)g_object_unref, NULL);
103 g_list_free (monitor->mounts);
104 monitor->mounts = NULL;
106 G_OBJECT_CLASS (g_unix_volume_monitor_parent_class)->dispose (object);
110 get_mounts (GVolumeMonitor *volume_monitor)
112 GUnixVolumeMonitor *monitor;
115 monitor = G_UNIX_VOLUME_MONITOR (volume_monitor);
117 l = g_list_copy (monitor->mounts);
118 g_list_foreach (l, (GFunc)g_object_ref, NULL);
124 get_volumes (GVolumeMonitor *volume_monitor)
126 GUnixVolumeMonitor *monitor;
129 monitor = G_UNIX_VOLUME_MONITOR (volume_monitor);
131 l = g_list_copy (monitor->volumes);
132 g_list_foreach (l, (GFunc)g_object_ref, NULL);
138 get_connected_drives (GVolumeMonitor *volume_monitor)
144 get_volume_for_uuid (GVolumeMonitor *volume_monitor, const char *uuid)
150 get_mount_for_uuid (GVolumeMonitor *volume_monitor, const char *uuid)
162 get_mount_for_mount_path (const char *mount_path,
163 GCancellable *cancellable)
165 GUnixMountEntry *mount_entry;
168 mount_entry = g_unix_mount_at (mount_path, NULL);
173 /* TODO: Set mountable volume? */
174 mount = _g_unix_mount_new (NULL, mount_entry, NULL);
176 g_unix_mount_free (mount_entry);
178 return G_MOUNT (mount);
182 g_unix_volume_monitor_class_init (GUnixVolumeMonitorClass *klass)
184 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
185 GVolumeMonitorClass *monitor_class = G_VOLUME_MONITOR_CLASS (klass);
186 GNativeVolumeMonitorClass *native_class = G_NATIVE_VOLUME_MONITOR_CLASS (klass);
188 gobject_class->finalize = g_unix_volume_monitor_finalize;
189 gobject_class->dispose = g_unix_volume_monitor_dispose;
191 monitor_class->get_mounts = get_mounts;
192 monitor_class->get_volumes = get_volumes;
193 monitor_class->get_connected_drives = get_connected_drives;
194 monitor_class->get_volume_for_uuid = get_volume_for_uuid;
195 monitor_class->get_mount_for_uuid = get_mount_for_uuid;
196 monitor_class->is_supported = is_supported;
198 native_class->get_mount_for_mount_path = get_mount_for_mount_path;
202 mountpoints_changed (GUnixMountMonitor *mount_monitor,
205 GUnixVolumeMonitor *unix_monitor = user_data;
207 /* Update both to make sure volumes are created before mounts */
208 update_volumes (unix_monitor);
209 update_mounts (unix_monitor);
213 mounts_changed (GUnixMountMonitor *mount_monitor,
216 GUnixVolumeMonitor *unix_monitor = user_data;
218 /* Update both to make sure volumes are created before mounts */
219 update_volumes (unix_monitor);
220 update_mounts (unix_monitor);
224 g_unix_volume_monitor_init (GUnixVolumeMonitor *unix_monitor)
227 unix_monitor->mount_monitor = g_unix_mount_monitor_new ();
229 g_signal_connect (unix_monitor->mount_monitor,
230 "mounts-changed", G_CALLBACK (mounts_changed),
233 g_signal_connect (unix_monitor->mount_monitor,
234 "mountpoints-changed", G_CALLBACK (mountpoints_changed),
237 update_volumes (unix_monitor);
238 update_mounts (unix_monitor);
242 _g_unix_volume_monitor_new (void)
244 GUnixVolumeMonitor *monitor;
246 monitor = g_object_new (G_TYPE_UNIX_VOLUME_MONITOR, NULL);
248 return G_VOLUME_MONITOR (monitor);
252 diff_sorted_lists (GList *list1,
254 GCompareFunc compare,
260 *added = *removed = NULL;
262 while (list1 != NULL &&
265 order = (*compare) (list1->data, list2->data);
268 *removed = g_list_prepend (*removed, list1->data);
273 *added = g_list_prepend (*added, list2->data);
283 while (list1 != NULL)
285 *removed = g_list_prepend (*removed, list1->data);
288 while (list2 != NULL)
290 *added = g_list_prepend (*added, list2->data);
296 _g_unix_volume_monitor_lookup_volume_for_mount_path (GUnixVolumeMonitor *monitor,
297 const char *mount_path)
301 for (l = monitor->volumes; l != NULL; l = l->next)
303 GUnixVolume *volume = l->data;
305 if (_g_unix_volume_has_mount_path (volume, mount_path))
313 find_mount_by_mountpath (GUnixVolumeMonitor *monitor,
314 const char *mount_path)
318 for (l = monitor->mounts; l != NULL; l = l->next)
320 GUnixMount *mount = l->data;
322 if (_g_unix_mount_has_mount_path (mount, mount_path))
330 update_volumes (GUnixVolumeMonitor *monitor)
332 GList *new_mountpoints;
333 GList *removed, *added;
337 new_mountpoints = g_unix_mount_points_get (NULL);
339 new_mountpoints = g_list_sort (new_mountpoints, (GCompareFunc) g_unix_mount_point_compare);
341 diff_sorted_lists (monitor->last_mountpoints,
342 new_mountpoints, (GCompareFunc) g_unix_mount_point_compare,
345 for (l = removed; l != NULL; l = l->next)
347 GUnixMountPoint *mountpoint = l->data;
349 volume = _g_unix_volume_monitor_lookup_volume_for_mount_path (monitor,
350 g_unix_mount_point_get_mount_path (mountpoint));
353 _g_unix_volume_disconnected (volume);
354 monitor->volumes = g_list_remove (monitor->volumes, volume);
355 g_signal_emit_by_name (monitor, "volume-removed", volume);
356 g_signal_emit_by_name (volume, "removed");
357 g_object_unref (volume);
361 for (l = added; l != NULL; l = l->next)
363 GUnixMountPoint *mountpoint = l->data;
365 volume = _g_unix_volume_new (G_VOLUME_MONITOR (monitor), mountpoint);
368 monitor->volumes = g_list_prepend (monitor->volumes, volume);
369 g_signal_emit_by_name (monitor, "volume-added", volume);
374 g_list_free (removed);
375 g_list_foreach (monitor->last_mountpoints,
376 (GFunc)g_unix_mount_point_free, NULL);
377 g_list_free (monitor->last_mountpoints);
378 monitor->last_mountpoints = new_mountpoints;
382 update_mounts (GUnixVolumeMonitor *monitor)
385 GList *removed, *added;
389 const char *mount_path;
391 new_mounts = g_unix_mounts_get (NULL);
393 new_mounts = g_list_sort (new_mounts, (GCompareFunc) g_unix_mount_compare);
395 diff_sorted_lists (monitor->last_mounts,
396 new_mounts, (GCompareFunc) g_unix_mount_compare,
399 for (l = removed; l != NULL; l = l->next)
401 GUnixMountEntry *mount_entry = l->data;
403 mount = find_mount_by_mountpath (monitor, g_unix_mount_get_mount_path (mount_entry));
406 _g_unix_mount_unmounted (mount);
407 monitor->mounts = g_list_remove (monitor->mounts, mount);
408 g_signal_emit_by_name (monitor, "mount-removed", mount);
409 g_signal_emit_by_name (mount, "unmounted");
410 g_object_unref (mount);
414 for (l = added; l != NULL; l = l->next)
416 GUnixMountEntry *mount_entry = l->data;
418 mount_path = g_unix_mount_get_mount_path (mount_entry);
420 volume = _g_unix_volume_monitor_lookup_volume_for_mount_path (monitor, mount_path);
421 mount = _g_unix_mount_new (G_VOLUME_MONITOR (monitor), mount_entry, volume);
424 monitor->mounts = g_list_prepend (monitor->mounts, mount);
425 g_signal_emit_by_name (monitor, "mount-added", mount);
430 g_list_free (removed);
431 g_list_foreach (monitor->last_mounts,
432 (GFunc)g_unix_mount_free, NULL);
433 g_list_free (monitor->last_mounts);
434 monitor->last_mounts = new_mounts;