gtestdbus: Fix non-const use of const variables
[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
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   
109   monitor = G_UNIX_VOLUME_MONITOR (volume_monitor);
110
111   return g_list_copy_deep (monitor->mounts, (GCopyFunc) g_object_ref, NULL);
112 }
113
114 static GList *
115 get_volumes (GVolumeMonitor *volume_monitor)
116 {
117   GUnixVolumeMonitor *monitor;
118   
119   monitor = G_UNIX_VOLUME_MONITOR (volume_monitor);
120
121   return g_list_copy_deep (monitor->volumes, (GCopyFunc) g_object_ref, NULL);
122 }
123
124 static GList *
125 get_connected_drives (GVolumeMonitor *volume_monitor)
126 {
127   return NULL;
128 }
129
130 static GVolume *
131 get_volume_for_uuid (GVolumeMonitor *volume_monitor, const char *uuid)
132 {
133   return NULL;
134 }
135
136 static GMount *
137 get_mount_for_uuid (GVolumeMonitor *volume_monitor, const char *uuid)
138 {
139   return NULL;
140 }
141
142 static gboolean
143 is_supported (void)
144 {
145   return TRUE;
146 }
147
148 static GMount *
149 get_mount_for_mount_path (const char *mount_path,
150                           GCancellable *cancellable)
151 {
152   GUnixMountEntry *mount_entry;
153   GUnixMount *mount;
154
155   mount_entry = g_unix_mount_at (mount_path, NULL);
156
157   if (!mount_entry)
158     return NULL;
159
160   /* TODO: Set mountable volume? */
161   mount = _g_unix_mount_new (NULL, mount_entry, NULL);
162
163   g_unix_mount_free (mount_entry);
164
165   return G_MOUNT (mount);
166 }
167
168 static void
169 g_unix_volume_monitor_class_init (GUnixVolumeMonitorClass *klass)
170 {
171   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
172   GVolumeMonitorClass *monitor_class = G_VOLUME_MONITOR_CLASS (klass);
173   GNativeVolumeMonitorClass *native_class = G_NATIVE_VOLUME_MONITOR_CLASS (klass);
174   
175   gobject_class->finalize = g_unix_volume_monitor_finalize;
176   gobject_class->dispose = g_unix_volume_monitor_dispose;
177
178   monitor_class->get_mounts = get_mounts;
179   monitor_class->get_volumes = get_volumes;
180   monitor_class->get_connected_drives = get_connected_drives;
181   monitor_class->get_volume_for_uuid = get_volume_for_uuid;
182   monitor_class->get_mount_for_uuid = get_mount_for_uuid;
183   monitor_class->is_supported = is_supported;
184
185   native_class->get_mount_for_mount_path = get_mount_for_mount_path;
186 }
187
188 static void
189 mountpoints_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 mounts_changed (GUnixMountMonitor *mount_monitor,
201                 gpointer           user_data)
202 {
203   GUnixVolumeMonitor *unix_monitor = user_data;
204
205   /* Update both to make sure volumes are created before mounts */
206   update_volumes (unix_monitor);
207   update_mounts (unix_monitor);
208 }
209
210 static void
211 g_unix_volume_monitor_init (GUnixVolumeMonitor *unix_monitor)
212 {
213
214   unix_monitor->mount_monitor = g_unix_mount_monitor_new ();
215
216   g_signal_connect (unix_monitor->mount_monitor,
217                     "mounts-changed", G_CALLBACK (mounts_changed),
218                     unix_monitor);
219   
220   g_signal_connect (unix_monitor->mount_monitor,
221                     "mountpoints-changed", G_CALLBACK (mountpoints_changed),
222                     unix_monitor);
223                     
224   update_volumes (unix_monitor);
225   update_mounts (unix_monitor);
226 }
227
228 GVolumeMonitor *
229 _g_unix_volume_monitor_new (void)
230 {
231   GUnixVolumeMonitor *monitor;
232
233   monitor = g_object_new (G_TYPE_UNIX_VOLUME_MONITOR, NULL);
234   
235   return G_VOLUME_MONITOR (monitor);
236 }
237
238 static void
239 diff_sorted_lists (GList         *list1, 
240                    GList         *list2, 
241                    GCompareFunc   compare,
242                    GList        **added, 
243                    GList        **removed)
244 {
245   int order;
246   
247   *added = *removed = NULL;
248   
249   while (list1 != NULL &&
250          list2 != NULL)
251     {
252       order = (*compare) (list1->data, list2->data);
253       if (order < 0)
254         {
255           *removed = g_list_prepend (*removed, list1->data);
256           list1 = list1->next;
257         }
258       else if (order > 0)
259         {
260           *added = g_list_prepend (*added, list2->data);
261           list2 = list2->next;
262         }
263       else
264         { /* same item */
265           list1 = list1->next;
266           list2 = list2->next;
267         }
268     }
269
270   while (list1 != NULL)
271     {
272       *removed = g_list_prepend (*removed, list1->data);
273       list1 = list1->next;
274     }
275   while (list2 != NULL)
276     {
277       *added = g_list_prepend (*added, list2->data);
278       list2 = list2->next;
279     }
280 }
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_free_full (monitor->last_mountpoints, (GDestroyNotify) g_unix_mount_point_free);
363   monitor->last_mountpoints = new_mountpoints;
364 }
365
366 static void
367 update_mounts (GUnixVolumeMonitor *monitor)
368 {
369   GList *new_mounts;
370   GList *removed, *added;
371   GList *l;
372   GUnixMount *mount;
373   GUnixVolume *volume;
374   const char *mount_path;
375   
376   new_mounts = g_unix_mounts_get (NULL);
377   
378   new_mounts = g_list_sort (new_mounts, (GCompareFunc) g_unix_mount_compare);
379   
380   diff_sorted_lists (monitor->last_mounts,
381                      new_mounts, (GCompareFunc) g_unix_mount_compare,
382                      &added, &removed);
383   
384   for (l = removed; l != NULL; l = l->next)
385     {
386       GUnixMountEntry *mount_entry = l->data;
387       
388       mount = find_mount_by_mountpath (monitor, g_unix_mount_get_mount_path (mount_entry));
389       if (mount)
390         {
391           _g_unix_mount_unmounted (mount);
392           monitor->mounts = g_list_remove (monitor->mounts, mount);
393           g_signal_emit_by_name (monitor, "mount-removed", mount);
394           g_signal_emit_by_name (mount, "unmounted");
395           g_object_unref (mount);
396         }
397     }
398   
399   for (l = added; l != NULL; l = l->next)
400     {
401       GUnixMountEntry *mount_entry = l->data;
402
403       mount_path = g_unix_mount_get_mount_path (mount_entry);
404       
405       volume = _g_unix_volume_monitor_lookup_volume_for_mount_path (monitor, mount_path);
406       mount = _g_unix_mount_new (G_VOLUME_MONITOR (monitor), mount_entry, volume);
407       if (mount)
408         {
409           monitor->mounts = g_list_prepend (monitor->mounts, mount);
410           g_signal_emit_by_name (monitor, "mount-added", mount);
411         }
412     }
413   
414   g_list_free (added);
415   g_list_free (removed);
416   g_list_free_full (monitor->last_mounts, (GDestroyNotify) g_unix_mount_free);
417   monitor->last_mounts = new_mounts;
418 }