Bumps documentation to 93% symbol coverage, touching most
[platform/upstream/glib.git] / gio / gunixvolumemonitor.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include <config.h>
24
25 #include <string.h>
26
27 #include <glib.h>
28 #include "gunixvolumemonitor.h"
29 #include "gunixmounts.h"
30 #include "gunixvolume.h"
31 #include "gunixdrive.h"
32 #include "gvolumeprivate.h"
33 #include "glibintl.h"
34
35 struct _GUnixVolumeMonitor {
36   GNativeVolumeMonitor parent;
37
38   GUnixMountMonitor *mount_monitor;
39
40   GList *last_mountpoints;
41   GList *last_mounts;
42
43   GList *drives;
44   GList *volumes;
45 };
46
47 static void mountpoints_changed (GUnixMountMonitor  *mount_monitor,
48                                  gpointer            user_data);
49 static void mounts_changed      (GUnixMountMonitor  *mount_monitor,
50                                  gpointer            user_data);
51 static void update_drives       (GUnixVolumeMonitor *monitor);
52 static void update_volumes      (GUnixVolumeMonitor *monitor);
53
54 G_DEFINE_TYPE (GUnixVolumeMonitor, g_unix_volume_monitor, G_TYPE_NATIVE_VOLUME_MONITOR);
55
56 static void
57 g_unix_volume_monitor_finalize (GObject *object)
58 {
59   GUnixVolumeMonitor *monitor;
60   
61   monitor = G_UNIX_VOLUME_MONITOR (object);
62
63   g_signal_handlers_disconnect_by_func (monitor->mount_monitor, mountpoints_changed, monitor);
64   g_signal_handlers_disconnect_by_func (monitor->mount_monitor, mounts_changed, monitor);
65                                         
66   g_object_unref (monitor->mount_monitor);
67
68   g_list_foreach (monitor->last_mounts, (GFunc)g_unix_mount_free, NULL);
69   g_list_free (monitor->last_mounts);
70
71   g_list_foreach (monitor->volumes, (GFunc)g_object_unref, NULL);
72   g_list_free (monitor->volumes);
73   g_list_foreach (monitor->drives, (GFunc)g_object_unref, NULL);
74   g_list_free (monitor->drives);
75   
76   if (G_OBJECT_CLASS (g_unix_volume_monitor_parent_class)->finalize)
77     (*G_OBJECT_CLASS (g_unix_volume_monitor_parent_class)->finalize) (object);
78 }
79
80 static GList *
81 get_mounted_volumes (GVolumeMonitor *volume_monitor)
82 {
83   GUnixVolumeMonitor *monitor;
84   GList *l;
85   
86   monitor = G_UNIX_VOLUME_MONITOR (volume_monitor);
87
88   l = g_list_copy (monitor->volumes);
89   g_list_foreach (l, (GFunc)g_object_ref, NULL);
90
91   return l;
92 }
93
94 static GList *
95 get_connected_drives (GVolumeMonitor *volume_monitor)
96 {
97   GUnixVolumeMonitor *monitor;
98   GList *l;
99   
100   monitor = G_UNIX_VOLUME_MONITOR (volume_monitor);
101
102   l = g_list_copy (monitor->drives);
103   g_list_foreach (l, (GFunc)g_object_ref, NULL);
104
105   return l;
106 }
107
108 static GVolume *
109 get_volume_for_mountpoint (const char *mountpoint)
110 {
111   GUnixMount *mount;
112   GUnixVolume *volume;
113
114   mount = g_get_unix_mount_at (mountpoint, NULL);
115   
116   /* TODO: Set drive? */
117   volume = g_unix_volume_new (mount, NULL);
118
119   return G_VOLUME (volume);
120 }
121
122 static void
123 g_unix_volume_monitor_class_init (GUnixVolumeMonitorClass *klass)
124 {
125   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
126   GVolumeMonitorClass *monitor_class = G_VOLUME_MONITOR_CLASS (klass);
127   GNativeVolumeMonitorClass *native_class = G_NATIVE_VOLUME_MONITOR_CLASS (klass);
128   
129   gobject_class->finalize = g_unix_volume_monitor_finalize;
130
131   monitor_class->get_mounted_volumes = get_mounted_volumes;
132   monitor_class->get_connected_drives = get_connected_drives;
133
134   native_class->priority = 0;
135   native_class->get_volume_for_mountpoint = get_volume_for_mountpoint;
136 }
137
138 static void
139 mountpoints_changed (GUnixMountMonitor *mount_monitor,
140                      gpointer user_data)
141 {
142   GUnixVolumeMonitor *unix_monitor = user_data;
143
144   /* Update both to make sure drives are created before volumes */
145   update_drives (unix_monitor);
146   update_volumes (unix_monitor);
147 }
148
149 static void
150 mounts_changed (GUnixMountMonitor *mount_monitor,
151                 gpointer user_data)
152 {
153   GUnixVolumeMonitor *unix_monitor = user_data;
154
155   /* Update both to make sure drives are created before volumes */
156   update_drives (unix_monitor);
157   update_volumes (unix_monitor);
158 }
159
160 static void
161 g_unix_volume_monitor_init (GUnixVolumeMonitor *unix_monitor)
162 {
163
164   unix_monitor->mount_monitor = g_unix_mount_monitor_new ();
165
166   g_signal_connect (unix_monitor->mount_monitor,
167                     "mounts_changed", G_CALLBACK (mounts_changed),
168                     unix_monitor);
169   
170   g_signal_connect (unix_monitor->mount_monitor,
171                     "mountpoints_changed", G_CALLBACK (mountpoints_changed),
172                     unix_monitor);
173                     
174   update_drives (unix_monitor);
175   update_volumes (unix_monitor);
176
177 }
178
179 /**
180  * g_unix_volume_monitor_new:
181  * 
182  * Returns:  a new #GVolumeMonitor.
183  **/
184 GVolumeMonitor *
185 g_unix_volume_monitor_new (void)
186 {
187   GUnixVolumeMonitor *monitor;
188
189   monitor = g_object_new (G_TYPE_UNIX_VOLUME_MONITOR, NULL);
190   
191   return G_VOLUME_MONITOR (monitor);
192 }
193
194 static void
195 diff_sorted_lists (GList *list1, GList *list2, GCompareFunc compare,
196                    GList **added, GList **removed)
197 {
198   int order;
199   
200   *added = *removed = NULL;
201   
202   while (list1 != NULL &&
203          list2 != NULL)
204     {
205       order = (*compare) (list1->data, list2->data);
206       if (order < 0)
207         {
208           *removed = g_list_prepend (*removed, list1->data);
209           list1 = list1->next;
210         }
211       else if (order > 0)
212         {
213           *added = g_list_prepend (*added, list2->data);
214           list2 = list2->next;
215         }
216       else
217         { /* same item */
218           list1 = list1->next;
219           list2 = list2->next;
220         }
221     }
222
223   while (list1 != NULL)
224     {
225       *removed = g_list_prepend (*removed, list1->data);
226       list1 = list1->next;
227     }
228   while (list2 != NULL)
229     {
230       *added = g_list_prepend (*added, list2->data);
231       list2 = list2->next;
232     }
233 }
234
235 /**
236  * g_unix_volume_lookup_drive_for_mountpoint: 
237  * @monitor:
238  * @mountpoint:
239  * 
240  * Returns:  #GUnixDrive for the given @mountpoint.
241  **/
242 GUnixDrive *
243 g_unix_volume_monitor_lookup_drive_for_mountpoint (GUnixVolumeMonitor *monitor,
244                                                    const char *mountpoint)
245 {
246   GList *l;
247
248   for (l = monitor->drives; l != NULL; l = l->next)
249     {
250       GUnixDrive *drive = l->data;
251
252       if (g_unix_drive_has_mountpoint (drive, mountpoint))
253         return drive;
254     }
255   
256   return NULL;
257 }
258
259 static GUnixVolume *
260 find_volume_by_mountpoint (GUnixVolumeMonitor *monitor,
261                            const char *mountpoint)
262 {
263   GList *l;
264
265   for (l = monitor->volumes; l != NULL; l = l->next)
266     {
267       GUnixVolume *volume = l->data;
268
269       if (g_unix_volume_has_mountpoint (volume, mountpoint))
270         return volume;
271     }
272   
273   return NULL;
274 }
275
276 static void
277 update_drives (GUnixVolumeMonitor *monitor)
278 {
279   GList *new_mountpoints;
280   GList *removed, *added;
281   GList *l;
282   GUnixDrive *drive;
283   
284   new_mountpoints = g_get_unix_mount_points (NULL);
285   
286   new_mountpoints = g_list_sort (new_mountpoints, (GCompareFunc) g_unix_mount_point_compare);
287   
288   diff_sorted_lists (monitor->last_mountpoints,
289                      new_mountpoints, (GCompareFunc) g_unix_mount_point_compare,
290                      &added, &removed);
291   
292   for (l = removed; l != NULL; l = l->next)
293     {
294       GUnixMountPoint *mountpoint = l->data;
295       
296       drive = g_unix_volume_monitor_lookup_drive_for_mountpoint (monitor,
297                                                                  g_unix_mount_point_get_mount_path (mountpoint));
298       if (drive)
299         {
300           g_unix_drive_disconnected (drive);
301           monitor->drives = g_list_remove (monitor->drives, drive);
302           g_signal_emit_by_name (monitor, "drive_disconnected", drive);
303           g_object_unref (drive);
304         }
305     }
306   
307   for (l = added; l != NULL; l = l->next)
308     {
309       GUnixMountPoint *mountpoint = l->data;
310       
311       drive = g_unix_drive_new (G_VOLUME_MONITOR (monitor), mountpoint);
312       if (drive)
313         {
314           monitor->drives = g_list_prepend (monitor->drives, drive);
315           g_signal_emit_by_name (monitor, "drive_connected", drive);
316         }
317     }
318   
319   g_list_free (added);
320   g_list_free (removed);
321   g_list_foreach (monitor->last_mountpoints,
322                   (GFunc)g_unix_mount_point_free, NULL);
323   g_list_free (monitor->last_mountpoints);
324   monitor->last_mountpoints = new_mountpoints;
325 }
326
327 static void
328 update_volumes (GUnixVolumeMonitor *monitor)
329 {
330   GList *new_mounts;
331   GList *removed, *added;
332   GList *l;
333   GUnixVolume *volume;
334   GUnixDrive *drive;
335   const char *mount_path;
336   
337   new_mounts = g_get_unix_mounts (NULL);
338   
339   new_mounts = g_list_sort (new_mounts, (GCompareFunc) g_unix_mount_compare);
340   
341   diff_sorted_lists (monitor->last_mounts,
342                      new_mounts, (GCompareFunc) g_unix_mount_compare,
343                      &added, &removed);
344   
345   for (l = removed; l != NULL; l = l->next)
346     {
347       GUnixMount *mount = l->data;
348       
349       volume = find_volume_by_mountpoint (monitor, g_unix_mount_get_mount_path (mount));
350       if (volume)
351         {
352           g_unix_volume_unmounted (volume);
353           monitor->volumes = g_list_remove (monitor->volumes, volume);
354           g_signal_emit_by_name (monitor, "volume_unmounted", volume);
355           g_object_unref (volume);
356         }
357     }
358   
359   for (l = added; l != NULL; l = l->next)
360     {
361       GUnixMount *mount = l->data;
362
363       mount_path = g_unix_mount_get_mount_path (mount);
364       
365       drive = g_unix_volume_monitor_lookup_drive_for_mountpoint (monitor,
366                                                                  mount_path);
367       volume = g_unix_volume_new (mount, drive);
368       if (volume)
369         {
370           monitor->volumes = g_list_prepend (monitor->volumes, volume);
371           g_signal_emit_by_name (monitor, "volume_mounted", volume);
372         }
373     }
374   
375   g_list_free (added);
376   g_list_free (removed);
377   g_list_foreach (monitor->last_mounts,
378                   (GFunc)g_unix_mount_free, NULL);
379   g_list_free (monitor->last_mounts);
380   monitor->last_mounts = new_mounts;
381 }