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