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