Remove C99 comments
[platform/upstream/glib.git] / gio / gunionvolumemonitor.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  *         David Zeuthen <davidz@redhat.com>
22  */
23
24 #include <config.h>
25
26 #include <string.h>
27
28 #include <glib.h>
29 #include "gunionvolumemonitor.h"
30 #include "gmountprivate.h"
31 #include "giomodule-priv.h"
32 #ifdef G_OS_UNIX
33 #include "gunixvolumemonitor.h"
34 #endif
35 #include "gnativevolumemonitor.h"
36
37 #include "glibintl.h"
38
39 #include "gioalias.h"
40
41 struct _GUnionVolumeMonitor {
42   GVolumeMonitor parent;
43
44   GList *monitors;
45 };
46
47 static void g_union_volume_monitor_remove_monitor (GUnionVolumeMonitor *union_monitor,
48                                                    GVolumeMonitor *child_monitor);
49
50
51 #define g_union_volume_monitor_get_type _g_union_volume_monitor_get_type
52 G_DEFINE_TYPE (GUnionVolumeMonitor, g_union_volume_monitor, G_TYPE_VOLUME_MONITOR);
53
54
55 G_LOCK_DEFINE_STATIC(the_volume_monitor);
56 static GUnionVolumeMonitor *the_volume_monitor = NULL;
57
58 static void
59 g_union_volume_monitor_finalize (GObject *object)
60 {
61   GUnionVolumeMonitor *monitor;
62   GVolumeMonitor *child_monitor;
63
64   monitor = G_UNION_VOLUME_MONITOR (object);
65
66   while (monitor->monitors != NULL) {
67     child_monitor = monitor->monitors->data;
68     g_union_volume_monitor_remove_monitor (monitor, 
69                                            child_monitor);
70     g_object_unref (child_monitor);
71   }
72
73   
74   if (G_OBJECT_CLASS (g_union_volume_monitor_parent_class)->finalize)
75     (*G_OBJECT_CLASS (g_union_volume_monitor_parent_class)->finalize) (object);
76 }
77
78 static void
79 g_union_volume_monitor_dispose (GObject *object)
80 {
81   GUnionVolumeMonitor *monitor;
82   
83   monitor = G_UNION_VOLUME_MONITOR (object);
84
85   G_LOCK (the_volume_monitor);
86   the_volume_monitor = NULL;
87   G_UNLOCK (the_volume_monitor);
88   
89   if (G_OBJECT_CLASS (g_union_volume_monitor_parent_class)->dispose)
90     (*G_OBJECT_CLASS (g_union_volume_monitor_parent_class)->dispose) (object);
91 }
92
93 static GList *
94 get_mounts (GVolumeMonitor *volume_monitor)
95 {
96   GUnionVolumeMonitor *monitor;
97   GVolumeMonitor *child_monitor;
98   GList *res;
99   GList *l;
100   
101   monitor = G_UNION_VOLUME_MONITOR (volume_monitor);
102
103   res = NULL;
104   
105   G_LOCK (the_volume_monitor);
106
107   for (l = monitor->monitors; l != NULL; l = l->next)
108     {
109       child_monitor = l->data;
110
111       res = g_list_concat (res, g_volume_monitor_get_mounts (child_monitor));
112     }
113   
114   G_UNLOCK (the_volume_monitor);
115
116   return res;
117 }
118
119 static GList *
120 get_volumes (GVolumeMonitor *volume_monitor)
121 {
122   GUnionVolumeMonitor *monitor;
123   GVolumeMonitor *child_monitor;
124   GList *res;
125   GList *l;
126   
127   monitor = G_UNION_VOLUME_MONITOR (volume_monitor);
128
129   res = NULL;
130   
131   G_LOCK (the_volume_monitor);
132
133   for (l = monitor->monitors; l != NULL; l = l->next)
134     {
135       child_monitor = l->data;
136
137       res = g_list_concat (res, g_volume_monitor_get_volumes (child_monitor));
138     }
139   
140   G_UNLOCK (the_volume_monitor);
141
142   return res;
143 }
144
145 static GList *
146 get_connected_drives (GVolumeMonitor *volume_monitor)
147 {
148   GUnionVolumeMonitor *monitor;
149   GVolumeMonitor *child_monitor;
150   GList *res;
151   GList *l;
152   
153   monitor = G_UNION_VOLUME_MONITOR (volume_monitor);
154
155   res = NULL;
156   
157   G_LOCK (the_volume_monitor);
158
159   for (l = monitor->monitors; l != NULL; l = l->next)
160     {
161       child_monitor = l->data;
162
163       res = g_list_concat (res, g_volume_monitor_get_connected_drives (child_monitor));
164     }
165   
166   G_UNLOCK (the_volume_monitor);
167
168   return res;
169 }
170
171 static GVolume *
172 get_volume_for_uuid (GVolumeMonitor *volume_monitor, const char *uuid)
173 {
174   GUnionVolumeMonitor *monitor;
175   GVolumeMonitor *child_monitor;
176   GVolume *volume;
177   GList *l;
178   
179   monitor = G_UNION_VOLUME_MONITOR (volume_monitor);
180
181   volume = NULL;
182   
183   G_LOCK (the_volume_monitor);
184
185   for (l = monitor->monitors; l != NULL; l = l->next)
186     {
187       child_monitor = l->data;
188
189       volume = g_volume_monitor_get_volume_for_uuid (child_monitor, uuid);
190       if (volume != NULL)
191         break;
192
193     }
194   
195   G_UNLOCK (the_volume_monitor);
196
197   return volume;
198 }
199
200 static GMount *
201 get_mount_for_uuid (GVolumeMonitor *volume_monitor, const char *uuid)
202 {
203   GUnionVolumeMonitor *monitor;
204   GVolumeMonitor *child_monitor;
205   GMount *mount;
206   GList *l;
207   
208   monitor = G_UNION_VOLUME_MONITOR (volume_monitor);
209
210   mount = NULL;
211   
212   G_LOCK (the_volume_monitor);
213
214   for (l = monitor->monitors; l != NULL; l = l->next)
215     {
216       child_monitor = l->data;
217
218       mount = g_volume_monitor_get_mount_for_uuid (child_monitor, uuid);
219       if (mount != NULL)
220         break;
221
222     }
223   
224   G_UNLOCK (the_volume_monitor);
225
226   return mount;
227 }
228
229 static void
230 g_union_volume_monitor_class_init (GUnionVolumeMonitorClass *klass)
231 {
232   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
233   GVolumeMonitorClass *monitor_class = G_VOLUME_MONITOR_CLASS (klass);
234   
235   gobject_class->finalize = g_union_volume_monitor_finalize;
236   gobject_class->dispose = g_union_volume_monitor_dispose;
237
238   monitor_class->get_connected_drives = get_connected_drives;
239   monitor_class->get_volumes = get_volumes;
240   monitor_class->get_mounts = get_mounts;
241   monitor_class->get_volume_for_uuid = get_volume_for_uuid;
242   monitor_class->get_mount_for_uuid = get_mount_for_uuid;
243 }
244
245 static void
246 child_volume_added (GVolumeMonitor      *child_monitor,
247                     GVolume    *child_volume,
248                     GUnionVolumeMonitor *union_monitor)
249 {
250   g_signal_emit_by_name (union_monitor,
251                          "volume_added",
252                          child_volume);
253 }
254
255 static void
256 child_volume_removed (GVolumeMonitor      *child_monitor,
257                       GVolume    *child_volume,
258                       GUnionVolumeMonitor *union_monitor)
259 {
260   g_signal_emit_by_name (union_monitor,
261                          "volume_removed",
262                          child_volume);
263 }
264
265 static void
266 child_volume_changed (GVolumeMonitor      *child_monitor,
267                       GVolume    *child_volume,
268                       GUnionVolumeMonitor *union_monitor)
269 {
270   g_signal_emit_by_name (union_monitor,
271                          "volume_changed",
272                          child_volume);
273 }
274
275 static void
276 child_mount_added (GVolumeMonitor      *child_monitor,
277                    GMount              *child_mount,
278                    GUnionVolumeMonitor *union_monitor)
279 {
280   g_signal_emit_by_name (union_monitor,
281                          "mount_added",
282                          child_mount);
283 }
284
285 static void
286 child_mount_removed (GVolumeMonitor      *child_monitor,
287                      GMount              *child_mount,
288                      GUnionVolumeMonitor *union_monitor)
289 {
290   g_signal_emit_by_name (union_monitor,
291                          "mount_removed",
292                          child_mount);
293 }
294
295 static void
296 child_mount_pre_unmount (GVolumeMonitor       *child_monitor,
297                           GMount              *child_mount,
298                           GUnionVolumeMonitor *union_monitor)
299 {
300   g_signal_emit_by_name (union_monitor,
301                          "mount_pre_unmount",
302                          child_mount);
303 }
304
305
306 static void
307 child_mount_changed (GVolumeMonitor       *child_monitor,
308                       GMount              *child_mount,
309                       GUnionVolumeMonitor *union_monitor)
310 {
311   g_signal_emit_by_name (union_monitor,
312                          "mount_changed",
313                          child_mount);
314 }
315
316 static void
317 child_drive_connected (GVolumeMonitor      *child_monitor,
318                        GDrive              *child_drive,
319                        GUnionVolumeMonitor *union_monitor)
320 {
321   g_signal_emit_by_name (union_monitor,
322                          "drive_connected",
323                          child_drive);
324 }
325
326 static void
327 child_drive_disconnected (GVolumeMonitor      *child_monitor,
328                           GDrive              *child_drive,
329                           GUnionVolumeMonitor *union_monitor)
330 {
331   g_signal_emit_by_name (union_monitor,
332                          "drive_disconnected",
333                          child_drive);
334 }
335
336 static void
337 child_drive_changed (GVolumeMonitor      *child_monitor,
338                      GDrive             *child_drive,
339                      GUnionVolumeMonitor *union_monitor)
340 {
341   g_signal_emit_by_name (union_monitor,
342                          "drive_changed",
343                          child_drive);
344 }
345
346 static void
347 g_union_volume_monitor_add_monitor (GUnionVolumeMonitor *union_monitor,
348                                     GVolumeMonitor      *volume_monitor)
349 {
350   if (g_list_find (union_monitor->monitors, volume_monitor))
351     return;
352
353   union_monitor->monitors =
354     g_list_prepend (union_monitor->monitors,
355                     g_object_ref (volume_monitor));
356
357   g_signal_connect (volume_monitor, "volume_added", (GCallback)child_volume_added, union_monitor);
358   g_signal_connect (volume_monitor, "volume_removed", (GCallback)child_volume_removed, union_monitor);
359   g_signal_connect (volume_monitor, "volume_changed", (GCallback)child_volume_changed, union_monitor);
360   g_signal_connect (volume_monitor, "mount_added", (GCallback)child_mount_added, union_monitor);
361   g_signal_connect (volume_monitor, "mount_removed", (GCallback)child_mount_removed, union_monitor);
362   g_signal_connect (volume_monitor, "mount_pre_unmount", (GCallback)child_mount_pre_unmount, union_monitor);
363   g_signal_connect (volume_monitor, "mount_changed", (GCallback)child_mount_changed, union_monitor);
364   g_signal_connect (volume_monitor, "drive_connected", (GCallback)child_drive_connected, union_monitor);
365   g_signal_connect (volume_monitor, "drive_disconnected", (GCallback)child_drive_disconnected, union_monitor);
366   g_signal_connect (volume_monitor, "drive_changed", (GCallback)child_drive_changed, union_monitor);
367 }
368
369 static void
370 g_union_volume_monitor_remove_monitor (GUnionVolumeMonitor *union_monitor,
371                                        GVolumeMonitor      *child_monitor)
372 {
373   GList *l;
374
375   l = g_list_find (union_monitor->monitors, child_monitor);
376   if (l == NULL)
377     return;
378
379   union_monitor->monitors = g_list_delete_link (union_monitor->monitors, l);
380
381   g_signal_handlers_disconnect_by_func (child_monitor, child_volume_added, union_monitor);
382   g_signal_handlers_disconnect_by_func (child_monitor, child_volume_removed, union_monitor);
383   g_signal_handlers_disconnect_by_func (child_monitor, child_volume_changed, union_monitor);
384   g_signal_handlers_disconnect_by_func (child_monitor, child_mount_added, union_monitor);
385   g_signal_handlers_disconnect_by_func (child_monitor, child_mount_removed, union_monitor);
386   g_signal_handlers_disconnect_by_func (child_monitor, child_mount_pre_unmount, union_monitor);
387   g_signal_handlers_disconnect_by_func (child_monitor, child_mount_changed, union_monitor);
388   g_signal_handlers_disconnect_by_func (child_monitor, child_drive_connected, union_monitor);
389   g_signal_handlers_disconnect_by_func (child_monitor, child_drive_disconnected, union_monitor);
390   g_signal_handlers_disconnect_by_func (child_monitor, child_drive_changed, union_monitor);
391 }
392
393 /* Note: This compares in reverse order.
394    Higher prio -> sort first
395  */
396 static gint
397 compare_monitor_type (gconstpointer  a,
398                       gconstpointer  b,
399                       gpointer       user_data)
400 {
401   GType a_type, b_type;
402   char *a_name, *b_name;
403   int a_prio, b_prio;
404   gint res;
405   const char *use_this_monitor;
406   GQuark private_q, name_q;
407
408   private_q = g_quark_from_static_string ("gio-prio");
409   name_q = g_quark_from_static_string ("gio-name");
410   
411   use_this_monitor = user_data;
412   a_type = *(GType *)a;
413   b_type = *(GType *)b;
414   a_prio = GPOINTER_TO_INT (g_type_get_qdata (a_type, private_q));
415   a_name = g_type_get_qdata (a_type, name_q);
416   b_prio = GPOINTER_TO_INT (g_type_get_qdata (b_type, private_q));
417   b_name = g_type_get_qdata (b_type, name_q);
418
419   if (a_type == b_type)
420     res = 0;
421   else if (use_this_monitor != NULL &&
422            strcmp (a_name, use_this_monitor) == 0)
423     res = -1;
424   else if (use_this_monitor != NULL &&
425            strcmp (b_name, use_this_monitor) == 0)
426     res = 1;
427   else 
428     res = b_prio - a_prio;
429   
430   return res;
431 }
432
433 static GType
434 get_default_native_class (gpointer data)
435 {
436   GNativeVolumeMonitorClass *klass, *native_class, **native_class_out;
437   GType *monitors;
438   guint n_monitors;
439   const char *use_this;
440   int i;
441
442   native_class_out = data;
443   
444   use_this = g_getenv ("GIO_USE_VOLUME_MONITOR");
445   
446   /* Ensure vfs in modules loaded */
447   _g_io_modules_ensure_loaded ();
448
449   monitors = g_type_children (G_TYPE_NATIVE_VOLUME_MONITOR, &n_monitors);
450
451   g_qsort_with_data (monitors,
452                      n_monitors,
453                      sizeof (GType),
454                      compare_monitor_type,
455                      (gpointer)use_this);
456
457   native_class = NULL;
458   for (i = 0; i < n_monitors; i++)
459     {    
460       klass = g_type_class_ref (monitors[i]);
461       if (G_VOLUME_MONITOR_CLASS (klass)->is_supported())
462         {
463           native_class = klass;
464           break;
465         }
466       else
467         g_type_class_unref (klass);
468     }
469
470   g_free (monitors);
471  
472   if (native_class)
473     {
474       *native_class_out = native_class;
475       return G_TYPE_FROM_CLASS (native_class);
476     }
477   else
478     return G_TYPE_INVALID;
479 }
480
481 /* We return the class, with a ref taken.
482  * This way we avoid unloading the class/module
483  * between selecting the type and creating the
484  * instance on the first call.
485  */
486 static GNativeVolumeMonitorClass *
487 get_native_class ()
488 {
489   static GOnce once_init = G_ONCE_INIT;
490   GTypeClass *type_class;
491
492   type_class = NULL;
493   g_once (&once_init, (GThreadFunc)get_default_native_class, &type_class);
494
495   if (type_class == NULL && once_init.retval != G_TYPE_INVALID)
496     type_class = g_type_class_ref ((GType)once_init.retval);
497   
498   return (GNativeVolumeMonitorClass *)type_class;
499 }
500
501 static void
502 g_union_volume_monitor_init (GUnionVolumeMonitor *union_monitor)
503 {
504   GVolumeMonitor *monitor;
505   GType *monitors;
506   guint n_monitors;
507   GNativeVolumeMonitorClass *native_class;
508   GVolumeMonitorClass *klass;
509   int i;
510
511   native_class = get_native_class ();
512
513   if (native_class != NULL)
514     {
515       monitor = g_object_new (G_TYPE_FROM_CLASS (native_class), NULL);
516       g_union_volume_monitor_add_monitor (union_monitor, monitor);
517       g_object_unref (monitor);
518       g_type_class_unref (native_class);
519     }
520   
521   monitors = g_type_children (G_TYPE_VOLUME_MONITOR, &n_monitors);
522   
523   for (i = 0; i < n_monitors; i++)
524     {
525       if (monitors[i] == G_TYPE_UNION_VOLUME_MONITOR ||
526           g_type_is_a (monitors[i], G_TYPE_NATIVE_VOLUME_MONITOR))
527         continue;
528
529       klass = g_type_class_ref (monitors[i]);
530       if (klass->is_supported == NULL || klass->is_supported())
531         {
532           monitor = g_object_new (monitors[i], NULL);
533           g_union_volume_monitor_add_monitor (union_monitor, monitor);
534           g_object_unref (monitor);
535         }
536       g_type_class_unref (klass);
537     }
538       
539   g_free (monitors);
540 }
541
542 static GUnionVolumeMonitor *
543 g_union_volume_monitor_new (void)
544 {
545   GUnionVolumeMonitor *monitor;
546
547   monitor = g_object_new (G_TYPE_UNION_VOLUME_MONITOR, NULL);
548   
549   return monitor;
550 }
551
552 /**
553  * g_volume_monitor_get:
554  * 
555  * Gets the volume monitor used by gio.
556  *
557  * Returns: a reference to the #GVolumeMonitor used by gio. Call
558  *    g_object_unref() when done with it.
559  **/
560 GVolumeMonitor *
561 g_volume_monitor_get (void)
562 {
563   GVolumeMonitor *vm;
564   
565   G_LOCK (the_volume_monitor);
566
567   if (the_volume_monitor)
568     vm = G_VOLUME_MONITOR (g_object_ref (the_volume_monitor));
569   else
570     {
571       the_volume_monitor = g_union_volume_monitor_new ();
572       vm = G_VOLUME_MONITOR (the_volume_monitor);
573     }
574   
575   G_UNLOCK (the_volume_monitor);
576
577   return vm;
578 }
579
580 /**
581  * _g_mount_get_for_mount_path:
582  * @mountpoint: a string.
583  * @cancellable: a #GCancellable, or %NULL
584  * 
585  * Returns: a #GMount for given @mount_path or %NULL.  
586  **/
587 GMount *
588 _g_mount_get_for_mount_path (const char *mount_path,
589                              GCancellable *cancellable)
590 {
591   GNativeVolumeMonitorClass *klass;
592   GMount *mount;
593   
594   klass = get_native_class ();
595   if (klass == NULL)
596     return NULL;
597
598   mount = NULL;
599
600   if (klass->get_mount_for_mount_path)
601     {
602       G_LOCK (the_volume_monitor);
603       mount = klass->get_mount_for_mount_path (mount_path, cancellable);
604       G_UNLOCK (the_volume_monitor);
605     }
606
607   /* TODO: How do we know this succeeded? Keep in mind that the native
608    *       volume monitor may fail (e.g. not being able to connect to
609    *       hald). Is the get_mount_for_mount_path() method allowed to
610    *       return NULL? Seems like it is ... probably the method needs
611    *       to take a boolean and write if it succeeds or not.. Messy.
612    *       Very messy.
613    */
614   
615   g_type_class_unref (klass);
616
617   return mount;
618 }
619
620 /**
621  * g_volume_monitor_adopt_orphan_mount:
622  * @mount: a #GMount object to find a parent for
623  *
624  * This function should be called by any #GVolumeMonitor
625  * implementation when a new #GMount object is created that is not
626  * associated with a #GVolume object. It must be called just before
627  * emitting the @mount_added signal.
628  *
629  * If the return value is not %NULL, the caller must associate the
630  * returned #GVolume object with the #GMount. This involves returning
631  * it in it's g_mount_get_volume() implementation. The caller must
632  * also listen for the "removed" signal on the returned object
633  * and give up it's reference when handling that signal
634  * 
635  * Similary, if implementing g_volume_monitor_adopt_orphan_mount(),
636  * the implementor must take a reference to @mount and return it in
637  * it's g_volume_get_mount() implemented. Also, the implementor must
638  * listen for the "unmounted" signal on @mount and give up it's
639  * reference upon handling that signal.
640  *
641  * There are two main use cases for this function.
642  *
643  * One is when implementing a user space file system driver that reads
644  * blocks of a block device that is already represented by the native
645  * volume monitor (for example a CD Audio file system driver). Such
646  * a driver will generate it's own #GMount object that needs to be
647  * assoicated with the #GVolume object that represents the volume.
648  *
649  * The other is for implementing a #GVolumeMonitor whose sole purpose
650  * is to return #GVolume objects representing entries in the users
651  * "favorite servers" list or similar.
652  *
653  * Returns: the #GVolume object that is the parent for @mount or %NULL
654  * if no wants to adopt the #GMount.
655  */
656 GVolume *
657 g_volume_monitor_adopt_orphan_mount (GMount *mount)
658 {
659   GVolumeMonitor *child_monitor;
660   GVolumeMonitorClass *child_monitor_class;
661   GVolume *volume;
662   GList *l;
663
664   g_return_val_if_fail (mount != NULL, NULL);
665
666   if (the_volume_monitor == NULL)
667     return NULL;
668
669   volume = NULL;
670   
671   /* TODO: nasty locking issues because current VM's don't emit signals in idle */
672
673   /*G_LOCK (the_volume_monitor);*/
674
675   for (l = the_volume_monitor->monitors; l != NULL; l = l->next)
676     {
677       child_monitor = l->data;
678       child_monitor_class = G_VOLUME_MONITOR_GET_CLASS (child_monitor);
679
680       if (child_monitor_class->adopt_orphan_mount != NULL)
681         {
682           volume = child_monitor_class->adopt_orphan_mount (mount);
683           if (volume != NULL)
684             break;
685         }
686     }
687   
688   /*G_UNLOCK (the_volume_monitor);*/
689
690   return volume;
691 }
692
693
694 #define __G_UNION_VOLUME_MONITOR_C__
695 #include "gioaliasdef.c"