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"
35 #include "gmountprivate.h"
40 struct _GUnixVolumeMonitor {
41 GNativeVolumeMonitor parent;
43 GUnixMountMonitor *mount_monitor;
45 GList *last_mountpoints;
52 static void mountpoints_changed (GUnixMountMonitor *mount_monitor,
54 static void mounts_changed (GUnixMountMonitor *mount_monitor,
56 static void update_volumes (GUnixVolumeMonitor *monitor);
57 static void update_mounts (GUnixVolumeMonitor *monitor);
59 #define g_unix_volume_monitor_get_type _g_unix_volume_monitor_get_type
60 G_DEFINE_TYPE (GUnixVolumeMonitor, g_unix_volume_monitor, G_TYPE_NATIVE_VOLUME_MONITOR);
63 g_unix_volume_monitor_finalize (GObject *object)
65 GUnixVolumeMonitor *monitor;
67 monitor = G_UNIX_VOLUME_MONITOR (object);
69 g_signal_handlers_disconnect_by_func (monitor->mount_monitor, mountpoints_changed, monitor);
70 g_signal_handlers_disconnect_by_func (monitor->mount_monitor, mounts_changed, monitor);
72 g_object_unref (monitor->mount_monitor);
74 g_list_foreach (monitor->last_mountpoints, (GFunc)g_unix_mount_point_free, NULL);
75 g_list_free (monitor->last_mountpoints);
76 g_list_foreach (monitor->last_mounts, (GFunc)g_unix_mount_free, NULL);
77 g_list_free (monitor->last_mounts);
79 g_list_foreach (monitor->volumes, (GFunc)g_object_unref, NULL);
80 g_list_free (monitor->volumes);
81 g_list_foreach (monitor->mounts, (GFunc)g_object_unref, NULL);
82 g_list_free (monitor->mounts);
84 if (G_OBJECT_CLASS (g_unix_volume_monitor_parent_class)->finalize)
85 (*G_OBJECT_CLASS (g_unix_volume_monitor_parent_class)->finalize) (object);
89 get_mounts (GVolumeMonitor *volume_monitor)
91 GUnixVolumeMonitor *monitor;
94 monitor = G_UNIX_VOLUME_MONITOR (volume_monitor);
96 l = g_list_copy (monitor->mounts);
97 g_list_foreach (l, (GFunc)g_object_ref, NULL);
103 get_volumes (GVolumeMonitor *volume_monitor)
105 GUnixVolumeMonitor *monitor;
108 monitor = G_UNIX_VOLUME_MONITOR (volume_monitor);
110 l = g_list_copy (monitor->volumes);
111 g_list_foreach (l, (GFunc)g_object_ref, NULL);
117 get_connected_drives (GVolumeMonitor *volume_monitor)
123 get_mount_for_mount_path (const char *mount_path)
125 GUnixMountEntry *mount_entry;
128 mount_entry = g_get_unix_mount_at (mount_path, NULL);
130 /* TODO: Set mountable volume? */
131 mount = _g_unix_mount_new (NULL, mount_entry, NULL);
133 return G_MOUNT (mount);
137 g_unix_volume_monitor_class_init (GUnixVolumeMonitorClass *klass)
139 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
140 GVolumeMonitorClass *monitor_class = G_VOLUME_MONITOR_CLASS (klass);
141 GNativeVolumeMonitorClass *native_class = G_NATIVE_VOLUME_MONITOR_CLASS (klass);
143 gobject_class->finalize = g_unix_volume_monitor_finalize;
145 monitor_class->get_mounts = get_mounts;
146 monitor_class->get_volumes = get_volumes;
147 monitor_class->get_connected_drives = get_connected_drives;
149 native_class->priority = 0;
150 native_class->get_mount_for_mount_path = get_mount_for_mount_path;
154 mountpoints_changed (GUnixMountMonitor *mount_monitor,
157 GUnixVolumeMonitor *unix_monitor = user_data;
159 /* Update both to make sure volumes are created before mounts */
160 update_volumes (unix_monitor);
161 update_mounts (unix_monitor);
165 mounts_changed (GUnixMountMonitor *mount_monitor,
168 GUnixVolumeMonitor *unix_monitor = user_data;
170 /* Update both to make sure volumes are created before mounts */
171 update_volumes (unix_monitor);
172 update_mounts (unix_monitor);
176 g_unix_volume_monitor_init (GUnixVolumeMonitor *unix_monitor)
179 unix_monitor->mount_monitor = g_unix_mount_monitor_new ();
181 g_signal_connect (unix_monitor->mount_monitor,
182 "mounts_changed", G_CALLBACK (mounts_changed),
185 g_signal_connect (unix_monitor->mount_monitor,
186 "mountpoints_changed", G_CALLBACK (mountpoints_changed),
189 update_volumes (unix_monitor);
190 update_mounts (unix_monitor);
194 * g_unix_volume_monitor_new:
196 * Returns: a new #GVolumeMonitor.
199 _g_unix_volume_monitor_new (void)
201 GUnixVolumeMonitor *monitor;
203 monitor = g_object_new (G_TYPE_UNIX_VOLUME_MONITOR, NULL);
205 return G_VOLUME_MONITOR (monitor);
209 diff_sorted_lists (GList *list1,
211 GCompareFunc compare,
217 *added = *removed = NULL;
219 while (list1 != NULL &&
222 order = (*compare) (list1->data, list2->data);
225 *removed = g_list_prepend (*removed, list1->data);
230 *added = g_list_prepend (*added, list2->data);
240 while (list1 != NULL)
242 *removed = g_list_prepend (*removed, list1->data);
245 while (list2 != NULL)
247 *added = g_list_prepend (*added, list2->data);
253 * _g_unix_volume_monitor_lookup_volume_for_mount_path:
257 * Returns: #GUnixVolume for the given @mount_path.
260 _g_unix_volume_monitor_lookup_volume_for_mount_path (GUnixVolumeMonitor *monitor,
261 const char *mount_path)
265 for (l = monitor->volumes; l != NULL; l = l->next)
267 GUnixVolume *volume = l->data;
269 if (_g_unix_volume_has_mount_path (volume, mount_path))
277 find_mount_by_mountpath (GUnixVolumeMonitor *monitor,
278 const char *mount_path)
282 for (l = monitor->mounts; l != NULL; l = l->next)
284 GUnixMount *mount = l->data;
286 if (_g_unix_mount_has_mount_path (mount, mount_path))
294 update_volumes (GUnixVolumeMonitor *monitor)
296 GList *new_mountpoints;
297 GList *removed, *added;
301 new_mountpoints = g_get_unix_mount_points (NULL);
303 new_mountpoints = g_list_sort (new_mountpoints, (GCompareFunc) g_unix_mount_point_compare);
305 diff_sorted_lists (monitor->last_mountpoints,
306 new_mountpoints, (GCompareFunc) g_unix_mount_point_compare,
309 for (l = removed; l != NULL; l = l->next)
311 GUnixMountPoint *mountpoint = l->data;
313 volume = _g_unix_volume_monitor_lookup_volume_for_mount_path (monitor,
314 g_unix_mount_point_get_mount_path (mountpoint));
317 _g_unix_volume_disconnected (volume);
318 monitor->volumes = g_list_remove (monitor->volumes, volume);
319 g_signal_emit_by_name (monitor, "volume_removed", volume);
320 g_object_unref (volume);
324 for (l = added; l != NULL; l = l->next)
326 GUnixMountPoint *mountpoint = l->data;
328 volume = _g_unix_volume_new (G_VOLUME_MONITOR (monitor), mountpoint);
331 monitor->volumes = g_list_prepend (monitor->volumes, volume);
332 g_signal_emit_by_name (monitor, "volume_added", volume);
337 g_list_free (removed);
338 g_list_foreach (monitor->last_mountpoints,
339 (GFunc)g_unix_mount_point_free, NULL);
340 g_list_free (monitor->last_mountpoints);
341 monitor->last_mountpoints = new_mountpoints;
345 update_mounts (GUnixVolumeMonitor *monitor)
348 GList *removed, *added;
352 const char *mount_path;
354 new_mounts = g_get_unix_mounts (NULL);
356 new_mounts = g_list_sort (new_mounts, (GCompareFunc) g_unix_mount_compare);
358 diff_sorted_lists (monitor->last_mounts,
359 new_mounts, (GCompareFunc) g_unix_mount_compare,
362 for (l = removed; l != NULL; l = l->next)
364 GUnixMountEntry *mount_entry = l->data;
366 g_warning ("%s %s removed",
367 g_unix_mount_get_mount_path (mount_entry),
368 g_unix_mount_get_device_path (mount_entry));
370 mount = find_mount_by_mountpath (monitor, g_unix_mount_get_mount_path (mount_entry));
373 _g_unix_mount_unmounted (mount);
374 monitor->mounts = g_list_remove (monitor->mounts, mount);
375 g_signal_emit_by_name (monitor, "mount_removed", mount);
376 g_object_unref (mount);
380 for (l = added; l != NULL; l = l->next)
382 GUnixMountEntry *mount_entry = l->data;
384 mount_path = g_unix_mount_get_mount_path (mount_entry);
386 volume = _g_unix_volume_monitor_lookup_volume_for_mount_path (monitor, mount_path);
387 mount = _g_unix_mount_new (G_VOLUME_MONITOR (monitor), mount_entry, volume);
390 monitor->mounts = g_list_prepend (monitor->mounts, mount);
391 g_signal_emit_by_name (monitor, "mount_added", mount);
396 g_list_free (removed);
397 g_list_foreach (monitor->last_mounts,
398 (GFunc)g_unix_mount_free, NULL);
399 g_list_free (monitor->last_mounts);
400 monitor->last_mounts = new_mounts;