69b8e3663f77d4b998ffea67d1da9b8e3ef0caa9
[platform/upstream/glib.git] / gio / gunixvolumemonitor.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2
3 /* GIO - GLib Input, Output and Streaming Library
4  * 
5  * Copyright (C) 2006-2007 Red Hat, Inc.
6  *
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.
11  *
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.
16  *
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.
21  *
22  * Author: Alexander Larsson <alexl@redhat.com>
23  *         David Zeuthen <davidz@redhat.com>
24  */
25
26 #include <config.h>
27
28 #include <string.h>
29
30 #include <glib.h>
31 #include "gunixvolumemonitor.h"
32 #include "gunixmounts.h"
33 #include "gunixmount.h"
34 #include "gunixvolume.h"
35 #include "gmountprivate.h"
36 #include "glibintl.h"
37
38 #include "gioalias.h"
39
40 struct _GUnixVolumeMonitor {
41   GNativeVolumeMonitor parent;
42
43   GUnixMountMonitor *mount_monitor;
44
45   GList *last_mountpoints;
46   GList *last_mounts;
47
48   GList *volumes;
49   GList *mounts;
50 };
51
52 static void mountpoints_changed      (GUnixMountMonitor  *mount_monitor,
53                                       gpointer            user_data);
54 static void mounts_changed           (GUnixMountMonitor  *mount_monitor,
55                                       gpointer            user_data);
56 static void update_volumes           (GUnixVolumeMonitor *monitor);
57 static void update_mounts            (GUnixVolumeMonitor *monitor);
58
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);
61
62 static void
63 g_unix_volume_monitor_finalize (GObject *object)
64 {
65   GUnixVolumeMonitor *monitor;
66   
67   monitor = G_UNIX_VOLUME_MONITOR (object);
68
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);
71                                         
72   g_object_unref (monitor->mount_monitor);
73
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);
78
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);
83   
84   if (G_OBJECT_CLASS (g_unix_volume_monitor_parent_class)->finalize)
85     (*G_OBJECT_CLASS (g_unix_volume_monitor_parent_class)->finalize) (object);
86 }
87
88 static GList *
89 get_mounts (GVolumeMonitor *volume_monitor)
90 {
91   GUnixVolumeMonitor *monitor;
92   GList *l;
93   
94   monitor = G_UNIX_VOLUME_MONITOR (volume_monitor);
95
96   l = g_list_copy (monitor->mounts);
97   g_list_foreach (l, (GFunc)g_object_ref, NULL);
98
99   return l;
100 }
101
102 static GList *
103 get_volumes (GVolumeMonitor *volume_monitor)
104 {
105   GUnixVolumeMonitor *monitor;
106   GList *l;
107   
108   monitor = G_UNIX_VOLUME_MONITOR (volume_monitor);
109
110   l = g_list_copy (monitor->volumes);
111   g_list_foreach (l, (GFunc)g_object_ref, NULL);
112
113   return l;
114 }
115
116 static GList *
117 get_connected_drives (GVolumeMonitor *volume_monitor)
118 {
119   return NULL;
120 }
121
122 static GVolume *
123 get_volume_for_uuid (GVolumeMonitor *volume_monitor, const char *uuid)
124 {
125   return NULL;
126 }
127
128 static GMount *
129 get_mount_for_uuid (GVolumeMonitor *volume_monitor, const char *uuid)
130 {
131   return NULL;
132 }
133
134 static gboolean
135 is_supported (void)
136 {
137   return TRUE;
138 }
139
140 static GMount *
141 get_mount_for_mount_path (const char *mount_path,
142                           GCancellable *cancellable)
143 {
144   GUnixMountEntry *mount_entry;
145   GUnixMount *mount;
146
147   mount_entry = g_unix_mount_at (mount_path, NULL);
148   
149   /* TODO: Set mountable volume? */
150   mount = _g_unix_mount_new (NULL, mount_entry, NULL);
151
152   return G_MOUNT (mount);
153 }
154
155 static void
156 g_unix_volume_monitor_class_init (GUnixVolumeMonitorClass *klass)
157 {
158   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
159   GVolumeMonitorClass *monitor_class = G_VOLUME_MONITOR_CLASS (klass);
160   GNativeVolumeMonitorClass *native_class = G_NATIVE_VOLUME_MONITOR_CLASS (klass);
161   
162   gobject_class->finalize = g_unix_volume_monitor_finalize;
163
164   monitor_class->get_mounts = get_mounts;
165   monitor_class->get_volumes = get_volumes;
166   monitor_class->get_connected_drives = get_connected_drives;
167   monitor_class->get_volume_for_uuid = get_volume_for_uuid;
168   monitor_class->get_mount_for_uuid = get_mount_for_uuid;
169   monitor_class->is_supported = is_supported;
170
171   native_class->priority = 0;
172   native_class->name = "unix";
173   native_class->get_mount_for_mount_path = get_mount_for_mount_path;
174 }
175
176 static void
177 mountpoints_changed (GUnixMountMonitor *mount_monitor,
178                      gpointer           user_data)
179 {
180   GUnixVolumeMonitor *unix_monitor = user_data;
181
182   /* Update both to make sure volumes are created before mounts */
183   update_volumes (unix_monitor);
184   update_mounts (unix_monitor);
185 }
186
187 static void
188 mounts_changed (GUnixMountMonitor *mount_monitor,
189                 gpointer           user_data)
190 {
191   GUnixVolumeMonitor *unix_monitor = user_data;
192
193   /* Update both to make sure volumes are created before mounts */
194   update_volumes (unix_monitor);
195   update_mounts (unix_monitor);
196 }
197
198 static void
199 g_unix_volume_monitor_init (GUnixVolumeMonitor *unix_monitor)
200 {
201
202   unix_monitor->mount_monitor = g_unix_mount_monitor_new ();
203
204   g_signal_connect (unix_monitor->mount_monitor,
205                     "mounts_changed", G_CALLBACK (mounts_changed),
206                     unix_monitor);
207   
208   g_signal_connect (unix_monitor->mount_monitor,
209                     "mountpoints_changed", G_CALLBACK (mountpoints_changed),
210                     unix_monitor);
211                     
212   update_volumes (unix_monitor);
213   update_mounts (unix_monitor);
214 }
215
216 /**
217  * g_unix_volume_monitor_new:
218  * 
219  * Returns:  a new #GVolumeMonitor.
220  **/
221 GVolumeMonitor *
222 _g_unix_volume_monitor_new (void)
223 {
224   GUnixVolumeMonitor *monitor;
225
226   monitor = g_object_new (G_TYPE_UNIX_VOLUME_MONITOR, NULL);
227   
228   return G_VOLUME_MONITOR (monitor);
229 }
230
231 static void
232 diff_sorted_lists (GList         *list1, 
233                    GList         *list2, 
234                    GCompareFunc   compare,
235                    GList        **added, 
236                    GList        **removed)
237 {
238   int order;
239   
240   *added = *removed = NULL;
241   
242   while (list1 != NULL &&
243          list2 != NULL)
244     {
245       order = (*compare) (list1->data, list2->data);
246       if (order < 0)
247         {
248           *removed = g_list_prepend (*removed, list1->data);
249           list1 = list1->next;
250         }
251       else if (order > 0)
252         {
253           *added = g_list_prepend (*added, list2->data);
254           list2 = list2->next;
255         }
256       else
257         { /* same item */
258           list1 = list1->next;
259           list2 = list2->next;
260         }
261     }
262
263   while (list1 != NULL)
264     {
265       *removed = g_list_prepend (*removed, list1->data);
266       list1 = list1->next;
267     }
268   while (list2 != NULL)
269     {
270       *added = g_list_prepend (*added, list2->data);
271       list2 = list2->next;
272     }
273 }
274
275 /**
276  * _g_unix_volume_monitor_lookup_volume_for_mount_path: 
277  * @monitor:
278  * @mount_path:
279  * 
280  * Returns:  #GUnixVolume for the given @mount_path.
281  **/
282 GUnixVolume *
283 _g_unix_volume_monitor_lookup_volume_for_mount_path (GUnixVolumeMonitor *monitor,
284                                                      const char         *mount_path)
285 {
286   GList *l;
287
288   for (l = monitor->volumes; l != NULL; l = l->next)
289     {
290       GUnixVolume *volume = l->data;
291
292       if (_g_unix_volume_has_mount_path (volume, mount_path))
293         return volume;
294     }
295   
296   return NULL;
297 }
298
299 static GUnixMount *
300 find_mount_by_mountpath (GUnixVolumeMonitor *monitor,
301                          const char *mount_path)
302 {
303   GList *l;
304
305   for (l = monitor->mounts; l != NULL; l = l->next)
306     {
307       GUnixMount *mount = l->data;
308
309       if (_g_unix_mount_has_mount_path (mount, mount_path))
310         return mount;
311     }
312   
313   return NULL;
314 }
315
316 static void
317 update_volumes (GUnixVolumeMonitor *monitor)
318 {
319   GList *new_mountpoints;
320   GList *removed, *added;
321   GList *l;
322   GUnixVolume *volume;
323   
324   new_mountpoints = g_unix_mount_points_get (NULL);
325   
326   new_mountpoints = g_list_sort (new_mountpoints, (GCompareFunc) g_unix_mount_point_compare);
327   
328   diff_sorted_lists (monitor->last_mountpoints,
329                      new_mountpoints, (GCompareFunc) g_unix_mount_point_compare,
330                      &added, &removed);
331   
332   for (l = removed; l != NULL; l = l->next)
333     {
334       GUnixMountPoint *mountpoint = l->data;
335       
336       volume = _g_unix_volume_monitor_lookup_volume_for_mount_path (monitor,
337                                                                     g_unix_mount_point_get_mount_path (mountpoint));
338       if (volume)
339         {
340           _g_unix_volume_disconnected (volume);
341           monitor->volumes = g_list_remove (monitor->volumes, volume);
342           g_signal_emit_by_name (monitor, "volume_removed", volume);
343           g_signal_emit_by_name (volume, "removed");
344           g_object_unref (volume);
345         }
346     }
347   
348   for (l = added; l != NULL; l = l->next)
349     {
350       GUnixMountPoint *mountpoint = l->data;
351       
352       volume = _g_unix_volume_new (G_VOLUME_MONITOR (monitor), mountpoint);
353       if (volume)
354         {
355           monitor->volumes = g_list_prepend (monitor->volumes, volume);
356           g_signal_emit_by_name (monitor, "volume_added", volume);
357         }
358     }
359   
360   g_list_free (added);
361   g_list_free (removed);
362   g_list_foreach (monitor->last_mountpoints,
363                   (GFunc)g_unix_mount_point_free, NULL);
364   g_list_free (monitor->last_mountpoints);
365   monitor->last_mountpoints = new_mountpoints;
366 }
367
368 static void
369 update_mounts (GUnixVolumeMonitor *monitor)
370 {
371   GList *new_mounts;
372   GList *removed, *added;
373   GList *l;
374   GUnixMount *mount;
375   GUnixVolume *volume;
376   const char *mount_path;
377   
378   new_mounts = g_unix_mounts_get (NULL);
379   
380   new_mounts = g_list_sort (new_mounts, (GCompareFunc) g_unix_mount_compare);
381   
382   diff_sorted_lists (monitor->last_mounts,
383                      new_mounts, (GCompareFunc) g_unix_mount_compare,
384                      &added, &removed);
385   
386   for (l = removed; l != NULL; l = l->next)
387     {
388       GUnixMountEntry *mount_entry = l->data;
389       
390       mount = find_mount_by_mountpath (monitor, g_unix_mount_get_mount_path (mount_entry));
391       if (mount)
392         {
393           _g_unix_mount_unmounted (mount);
394           monitor->mounts = g_list_remove (monitor->mounts, mount);
395           g_signal_emit_by_name (monitor, "mount_removed", mount);
396           g_signal_emit_by_name (mount, "unmounted");
397           g_object_unref (mount);
398         }
399     }
400   
401   for (l = added; l != NULL; l = l->next)
402     {
403       GUnixMountEntry *mount_entry = l->data;
404
405       mount_path = g_unix_mount_get_mount_path (mount_entry);
406       
407       volume = _g_unix_volume_monitor_lookup_volume_for_mount_path (monitor, mount_path);
408       mount = _g_unix_mount_new (G_VOLUME_MONITOR (monitor), mount_entry, volume);
409       if (mount)
410         {
411           monitor->mounts = g_list_prepend (monitor->mounts, mount);
412           g_signal_emit_by_name (monitor, "mount_added", mount);
413         }
414     }
415   
416   g_list_free (added);
417   g_list_free (removed);
418   g_list_foreach (monitor->last_mounts,
419                   (GFunc)g_unix_mount_free, NULL);
420   g_list_free (monitor->last_mounts);
421   monitor->last_mounts = new_mounts;
422 }