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