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