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"
42 struct _GUnixVolumeMonitor {
43 GNativeVolumeMonitor parent;
45 GUnixMountMonitor *mount_monitor;
47 GList *last_mountpoints;
54 static void mountpoints_changed (GUnixMountMonitor *mount_monitor,
56 static void mounts_changed (GUnixMountMonitor *mount_monitor,
58 static void update_volumes (GUnixVolumeMonitor *monitor);
59 static void update_mounts (GUnixVolumeMonitor *monitor);
61 #define g_unix_volume_monitor_get_type _g_unix_volume_monitor_get_type
62 G_DEFINE_TYPE_WITH_CODE (GUnixVolumeMonitor, g_unix_volume_monitor, G_TYPE_NATIVE_VOLUME_MONITOR,
63 g_io_extension_point_implement (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME,
69 g_unix_volume_monitor_finalize (GObject *object)
71 GUnixVolumeMonitor *monitor;
73 monitor = G_UNIX_VOLUME_MONITOR (object);
75 g_signal_handlers_disconnect_by_func (monitor->mount_monitor, mountpoints_changed, monitor);
76 g_signal_handlers_disconnect_by_func (monitor->mount_monitor, mounts_changed, monitor);
78 g_object_unref (monitor->mount_monitor);
80 g_list_foreach (monitor->last_mountpoints, (GFunc)g_unix_mount_point_free, NULL);
81 g_list_free (monitor->last_mountpoints);
82 g_list_foreach (monitor->last_mounts, (GFunc)g_unix_mount_free, NULL);
83 g_list_free (monitor->last_mounts);
85 g_list_foreach (monitor->volumes, (GFunc)g_object_unref, NULL);
86 g_list_free (monitor->volumes);
87 g_list_foreach (monitor->mounts, (GFunc)g_object_unref, NULL);
88 g_list_free (monitor->mounts);
90 G_OBJECT_CLASS (g_unix_volume_monitor_parent_class)->finalize (object);
94 get_mounts (GVolumeMonitor *volume_monitor)
96 GUnixVolumeMonitor *monitor;
99 monitor = G_UNIX_VOLUME_MONITOR (volume_monitor);
101 l = g_list_copy (monitor->mounts);
102 g_list_foreach (l, (GFunc)g_object_ref, NULL);
108 get_volumes (GVolumeMonitor *volume_monitor)
110 GUnixVolumeMonitor *monitor;
113 monitor = G_UNIX_VOLUME_MONITOR (volume_monitor);
115 l = g_list_copy (monitor->volumes);
116 g_list_foreach (l, (GFunc)g_object_ref, NULL);
122 get_connected_drives (GVolumeMonitor *volume_monitor)
128 get_volume_for_uuid (GVolumeMonitor *volume_monitor, const char *uuid)
134 get_mount_for_uuid (GVolumeMonitor *volume_monitor, const char *uuid)
146 get_mount_for_mount_path (const char *mount_path,
147 GCancellable *cancellable)
149 GUnixMountEntry *mount_entry;
152 mount_entry = g_unix_mount_at (mount_path, NULL);
154 /* TODO: Set mountable volume? */
155 mount = _g_unix_mount_new (NULL, mount_entry, NULL);
157 g_unix_mount_free (mount_entry);
159 return G_MOUNT (mount);
163 g_unix_volume_monitor_class_init (GUnixVolumeMonitorClass *klass)
165 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
166 GVolumeMonitorClass *monitor_class = G_VOLUME_MONITOR_CLASS (klass);
167 GNativeVolumeMonitorClass *native_class = G_NATIVE_VOLUME_MONITOR_CLASS (klass);
169 gobject_class->finalize = g_unix_volume_monitor_finalize;
171 monitor_class->get_mounts = get_mounts;
172 monitor_class->get_volumes = get_volumes;
173 monitor_class->get_connected_drives = get_connected_drives;
174 monitor_class->get_volume_for_uuid = get_volume_for_uuid;
175 monitor_class->get_mount_for_uuid = get_mount_for_uuid;
176 monitor_class->is_supported = is_supported;
178 native_class->get_mount_for_mount_path = get_mount_for_mount_path;
182 mountpoints_changed (GUnixMountMonitor *mount_monitor,
185 GUnixVolumeMonitor *unix_monitor = user_data;
187 /* Update both to make sure volumes are created before mounts */
188 update_volumes (unix_monitor);
189 update_mounts (unix_monitor);
193 mounts_changed (GUnixMountMonitor *mount_monitor,
196 GUnixVolumeMonitor *unix_monitor = user_data;
198 /* Update both to make sure volumes are created before mounts */
199 update_volumes (unix_monitor);
200 update_mounts (unix_monitor);
204 g_unix_volume_monitor_init (GUnixVolumeMonitor *unix_monitor)
207 unix_monitor->mount_monitor = g_unix_mount_monitor_new ();
209 g_signal_connect (unix_monitor->mount_monitor,
210 "mounts_changed", G_CALLBACK (mounts_changed),
213 g_signal_connect (unix_monitor->mount_monitor,
214 "mountpoints_changed", G_CALLBACK (mountpoints_changed),
217 update_volumes (unix_monitor);
218 update_mounts (unix_monitor);
222 * g_unix_volume_monitor_new:
224 * Returns: a new #GVolumeMonitor.
227 _g_unix_volume_monitor_new (void)
229 GUnixVolumeMonitor *monitor;
231 monitor = g_object_new (G_TYPE_UNIX_VOLUME_MONITOR, NULL);
233 return G_VOLUME_MONITOR (monitor);
237 diff_sorted_lists (GList *list1,
239 GCompareFunc compare,
245 *added = *removed = NULL;
247 while (list1 != NULL &&
250 order = (*compare) (list1->data, list2->data);
253 *removed = g_list_prepend (*removed, list1->data);
258 *added = g_list_prepend (*added, list2->data);
268 while (list1 != NULL)
270 *removed = g_list_prepend (*removed, list1->data);
273 while (list2 != NULL)
275 *added = g_list_prepend (*added, list2->data);
281 * _g_unix_volume_monitor_lookup_volume_for_mount_path:
285 * Returns: #GUnixVolume for the given @mount_path.
288 _g_unix_volume_monitor_lookup_volume_for_mount_path (GUnixVolumeMonitor *monitor,
289 const char *mount_path)
293 for (l = monitor->volumes; l != NULL; l = l->next)
295 GUnixVolume *volume = l->data;
297 if (_g_unix_volume_has_mount_path (volume, mount_path))
305 find_mount_by_mountpath (GUnixVolumeMonitor *monitor,
306 const char *mount_path)
310 for (l = monitor->mounts; l != NULL; l = l->next)
312 GUnixMount *mount = l->data;
314 if (_g_unix_mount_has_mount_path (mount, mount_path))
322 update_volumes (GUnixVolumeMonitor *monitor)
324 GList *new_mountpoints;
325 GList *removed, *added;
329 new_mountpoints = g_unix_mount_points_get (NULL);
331 new_mountpoints = g_list_sort (new_mountpoints, (GCompareFunc) g_unix_mount_point_compare);
333 diff_sorted_lists (monitor->last_mountpoints,
334 new_mountpoints, (GCompareFunc) g_unix_mount_point_compare,
337 for (l = removed; l != NULL; l = l->next)
339 GUnixMountPoint *mountpoint = l->data;
341 volume = _g_unix_volume_monitor_lookup_volume_for_mount_path (monitor,
342 g_unix_mount_point_get_mount_path (mountpoint));
345 _g_unix_volume_disconnected (volume);
346 monitor->volumes = g_list_remove (monitor->volumes, volume);
347 g_signal_emit_by_name (monitor, "volume_removed", volume);
348 g_signal_emit_by_name (volume, "removed");
349 g_object_unref (volume);
353 for (l = added; l != NULL; l = l->next)
355 GUnixMountPoint *mountpoint = l->data;
357 volume = _g_unix_volume_new (G_VOLUME_MONITOR (monitor), mountpoint);
360 monitor->volumes = g_list_prepend (monitor->volumes, volume);
361 g_signal_emit_by_name (monitor, "volume_added", volume);
366 g_list_free (removed);
367 g_list_foreach (monitor->last_mountpoints,
368 (GFunc)g_unix_mount_point_free, NULL);
369 g_list_free (monitor->last_mountpoints);
370 monitor->last_mountpoints = new_mountpoints;
374 update_mounts (GUnixVolumeMonitor *monitor)
377 GList *removed, *added;
381 const char *mount_path;
383 new_mounts = g_unix_mounts_get (NULL);
385 new_mounts = g_list_sort (new_mounts, (GCompareFunc) g_unix_mount_compare);
387 diff_sorted_lists (monitor->last_mounts,
388 new_mounts, (GCompareFunc) g_unix_mount_compare,
391 for (l = removed; l != NULL; l = l->next)
393 GUnixMountEntry *mount_entry = l->data;
395 mount = find_mount_by_mountpath (monitor, g_unix_mount_get_mount_path (mount_entry));
398 _g_unix_mount_unmounted (mount);
399 monitor->mounts = g_list_remove (monitor->mounts, mount);
400 g_signal_emit_by_name (monitor, "mount_removed", mount);
401 g_signal_emit_by_name (mount, "unmounted");
402 g_object_unref (mount);
406 for (l = added; l != NULL; l = l->next)
408 GUnixMountEntry *mount_entry = l->data;
410 mount_path = g_unix_mount_get_mount_path (mount_entry);
412 volume = _g_unix_volume_monitor_lookup_volume_for_mount_path (monitor, mount_path);
413 mount = _g_unix_mount_new (G_VOLUME_MONITOR (monitor), mount_entry, volume);
416 monitor->mounts = g_list_prepend (monitor->mounts, mount);
417 g_signal_emit_by_name (monitor, "mount_added", mount);
422 g_list_free (removed);
423 g_list_foreach (monitor->last_mounts,
424 (GFunc)g_unix_mount_free, NULL);
425 g_list_free (monitor->last_mounts);
426 monitor->last_mounts = new_mounts;