Moving files to packaging and extracing new tarball.
[profile/ivi/glib2.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
41 struct _GUnixVolumeMonitor {
42   GNativeVolumeMonitor parent;
43
44   GUnixMountMonitor *mount_monitor;
45
46   GList *last_mountpoints;
47   GList *last_mounts;
48
49   GList *volumes;
50   GList *mounts;
51 };
52
53 static void mountpoints_changed      (GUnixMountMonitor  *mount_monitor,
54                                       gpointer            user_data);
55 static void mounts_changed           (GUnixMountMonitor  *mount_monitor,
56                                       gpointer            user_data);
57 static void update_volumes           (GUnixVolumeMonitor *monitor);
58 static void update_mounts            (GUnixVolumeMonitor *monitor);
59
60 #define g_unix_volume_monitor_get_type _g_unix_volume_monitor_get_type
61 G_DEFINE_TYPE_WITH_CODE (GUnixVolumeMonitor, g_unix_volume_monitor, G_TYPE_NATIVE_VOLUME_MONITOR,
62                          g_io_extension_point_implement (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME,
63                                                          g_define_type_id,
64                                                          "unix",
65                                                          0));
66
67 static void
68 g_unix_volume_monitor_finalize (GObject *object)
69 {
70   GUnixVolumeMonitor *monitor;
71   
72   monitor = G_UNIX_VOLUME_MONITOR (object);
73
74   g_signal_handlers_disconnect_by_func (monitor->mount_monitor, mountpoints_changed, monitor);
75   g_signal_handlers_disconnect_by_func (monitor->mount_monitor, mounts_changed, monitor);
76                                         
77   g_object_unref (monitor->mount_monitor);
78
79   g_list_free_full (monitor->last_mountpoints, (GDestroyNotify) g_unix_mount_point_free);
80   g_list_free_full (monitor->last_mounts, (GDestroyNotify) g_unix_mount_free);
81
82   g_list_free_full (monitor->volumes, g_object_unref);
83   g_list_free_full (monitor->mounts, g_object_unref);
84
85   G_OBJECT_CLASS (g_unix_volume_monitor_parent_class)->finalize (object);
86 }
87
88 static void
89 g_unix_volume_monitor_dispose (GObject *object)
90 {
91   GUnixVolumeMonitor *monitor;
92
93   monitor = G_UNIX_VOLUME_MONITOR (object);
94
95   g_list_free_full (monitor->volumes, g_object_unref);
96   monitor->volumes = NULL;
97   
98   g_list_free_full (monitor->mounts, g_object_unref);
99   monitor->mounts = NULL;
100   
101   G_OBJECT_CLASS (g_unix_volume_monitor_parent_class)->dispose (object);
102 }
103
104 static GList *
105 get_mounts (GVolumeMonitor *volume_monitor)
106 {
107   GUnixVolumeMonitor *monitor;
108   GList *l;
109   
110   monitor = G_UNIX_VOLUME_MONITOR (volume_monitor);
111
112   l = g_list_copy (monitor->mounts);
113   g_list_foreach (l, (GFunc)g_object_ref, NULL);
114
115   return l;
116 }
117
118 static GList *
119 get_volumes (GVolumeMonitor *volume_monitor)
120 {
121   GUnixVolumeMonitor *monitor;
122   GList *l;
123   
124   monitor = G_UNIX_VOLUME_MONITOR (volume_monitor);
125
126   l = g_list_copy (monitor->volumes);
127   g_list_foreach (l, (GFunc)g_object_ref, NULL);
128
129   return l;
130 }
131
132 static GList *
133 get_connected_drives (GVolumeMonitor *volume_monitor)
134 {
135   return NULL;
136 }
137
138 static GVolume *
139 get_volume_for_uuid (GVolumeMonitor *volume_monitor, const char *uuid)
140 {
141   return NULL;
142 }
143
144 static GMount *
145 get_mount_for_uuid (GVolumeMonitor *volume_monitor, const char *uuid)
146 {
147   return NULL;
148 }
149
150 static gboolean
151 is_supported (void)
152 {
153   return TRUE;
154 }
155
156 static GMount *
157 get_mount_for_mount_path (const char *mount_path,
158                           GCancellable *cancellable)
159 {
160   GUnixMountEntry *mount_entry;
161   GUnixMount *mount;
162
163   mount_entry = g_unix_mount_at (mount_path, NULL);
164
165   if (!mount_entry)
166     return NULL;
167
168   /* TODO: Set mountable volume? */
169   mount = _g_unix_mount_new (NULL, mount_entry, NULL);
170
171   g_unix_mount_free (mount_entry);
172
173   return G_MOUNT (mount);
174 }
175
176 static void
177 g_unix_volume_monitor_class_init (GUnixVolumeMonitorClass *klass)
178 {
179   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
180   GVolumeMonitorClass *monitor_class = G_VOLUME_MONITOR_CLASS (klass);
181   GNativeVolumeMonitorClass *native_class = G_NATIVE_VOLUME_MONITOR_CLASS (klass);
182   
183   gobject_class->finalize = g_unix_volume_monitor_finalize;
184   gobject_class->dispose = g_unix_volume_monitor_dispose;
185
186   monitor_class->get_mounts = get_mounts;
187   monitor_class->get_volumes = get_volumes;
188   monitor_class->get_connected_drives = get_connected_drives;
189   monitor_class->get_volume_for_uuid = get_volume_for_uuid;
190   monitor_class->get_mount_for_uuid = get_mount_for_uuid;
191   monitor_class->is_supported = is_supported;
192
193   native_class->get_mount_for_mount_path = get_mount_for_mount_path;
194 }
195
196 static void
197 mountpoints_changed (GUnixMountMonitor *mount_monitor,
198                      gpointer           user_data)
199 {
200   GUnixVolumeMonitor *unix_monitor = user_data;
201
202   /* Update both to make sure volumes are created before mounts */
203   update_volumes (unix_monitor);
204   update_mounts (unix_monitor);
205 }
206
207 static void
208 mounts_changed (GUnixMountMonitor *mount_monitor,
209                 gpointer           user_data)
210 {
211   GUnixVolumeMonitor *unix_monitor = user_data;
212
213   /* Update both to make sure volumes are created before mounts */
214   update_volumes (unix_monitor);
215   update_mounts (unix_monitor);
216 }
217
218 static void
219 g_unix_volume_monitor_init (GUnixVolumeMonitor *unix_monitor)
220 {
221
222   unix_monitor->mount_monitor = g_unix_mount_monitor_new ();
223
224   g_signal_connect (unix_monitor->mount_monitor,
225                     "mounts-changed", G_CALLBACK (mounts_changed),
226                     unix_monitor);
227   
228   g_signal_connect (unix_monitor->mount_monitor,
229                     "mountpoints-changed", G_CALLBACK (mountpoints_changed),
230                     unix_monitor);
231                     
232   update_volumes (unix_monitor);
233   update_mounts (unix_monitor);
234 }
235
236 GVolumeMonitor *
237 _g_unix_volume_monitor_new (void)
238 {
239   GUnixVolumeMonitor *monitor;
240
241   monitor = g_object_new (G_TYPE_UNIX_VOLUME_MONITOR, NULL);
242   
243   return G_VOLUME_MONITOR (monitor);
244 }
245
246 static void
247 diff_sorted_lists (GList         *list1, 
248                    GList         *list2, 
249                    GCompareFunc   compare,
250                    GList        **added, 
251                    GList        **removed)
252 {
253   int order;
254   
255   *added = *removed = NULL;
256   
257   while (list1 != NULL &&
258          list2 != NULL)
259     {
260       order = (*compare) (list1->data, list2->data);
261       if (order < 0)
262         {
263           *removed = g_list_prepend (*removed, list1->data);
264           list1 = list1->next;
265         }
266       else if (order > 0)
267         {
268           *added = g_list_prepend (*added, list2->data);
269           list2 = list2->next;
270         }
271       else
272         { /* same item */
273           list1 = list1->next;
274           list2 = list2->next;
275         }
276     }
277
278   while (list1 != NULL)
279     {
280       *removed = g_list_prepend (*removed, list1->data);
281       list1 = list1->next;
282     }
283   while (list2 != NULL)
284     {
285       *added = g_list_prepend (*added, list2->data);
286       list2 = list2->next;
287     }
288 }
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_free_full (monitor->last_mountpoints, (GDestroyNotify) g_unix_mount_point_free);
371   monitor->last_mountpoints = new_mountpoints;
372 }
373
374 static void
375 update_mounts (GUnixVolumeMonitor *monitor)
376 {
377   GList *new_mounts;
378   GList *removed, *added;
379   GList *l;
380   GUnixMount *mount;
381   GUnixVolume *volume;
382   const char *mount_path;
383   
384   new_mounts = g_unix_mounts_get (NULL);
385   
386   new_mounts = g_list_sort (new_mounts, (GCompareFunc) g_unix_mount_compare);
387   
388   diff_sorted_lists (monitor->last_mounts,
389                      new_mounts, (GCompareFunc) g_unix_mount_compare,
390                      &added, &removed);
391   
392   for (l = removed; l != NULL; l = l->next)
393     {
394       GUnixMountEntry *mount_entry = l->data;
395       
396       mount = find_mount_by_mountpath (monitor, g_unix_mount_get_mount_path (mount_entry));
397       if (mount)
398         {
399           _g_unix_mount_unmounted (mount);
400           monitor->mounts = g_list_remove (monitor->mounts, mount);
401           g_signal_emit_by_name (monitor, "mount-removed", mount);
402           g_signal_emit_by_name (mount, "unmounted");
403           g_object_unref (mount);
404         }
405     }
406   
407   for (l = added; l != NULL; l = l->next)
408     {
409       GUnixMountEntry *mount_entry = l->data;
410
411       mount_path = g_unix_mount_get_mount_path (mount_entry);
412       
413       volume = _g_unix_volume_monitor_lookup_volume_for_mount_path (monitor, mount_path);
414       mount = _g_unix_mount_new (G_VOLUME_MONITOR (monitor), mount_entry, volume);
415       if (mount)
416         {
417           monitor->mounts = g_list_prepend (monitor->mounts, mount);
418           g_signal_emit_by_name (monitor, "mount-added", mount);
419         }
420     }
421   
422   g_list_free (added);
423   g_list_free (removed);
424   g_list_free_full (monitor->last_mounts, (GDestroyNotify) g_unix_mount_free);
425   monitor->last_mounts = new_mounts;
426 }