Merge remote branch 'gvdb/master'
[platform/upstream/glib.git] / gio / gvolume.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 #include "gmount.h"
26 #include "gvolume.h"
27 #include "gasyncresult.h"
28 #include "gsimpleasyncresult.h"
29 #include "gioerror.h"
30 #include "glibintl.h"
31
32
33 /**
34  * SECTION:gvolume
35  * @short_description: Volume management
36  * @include: gio/gio.h
37  * 
38  * The #GVolume interface represents user-visible objects that can be
39  * mounted. Note, when porting from GnomeVFS, #GVolume is the moral
40  * equivalent of #GnomeVFSDrive.
41  *
42  * Mounting a #GVolume instance is an asynchronous operation. For more
43  * information about asynchronous operations, see #GAsyncReady and
44  * #GSimpleAsyncReady. To mount a #GVolume, first call
45  * g_volume_mount() with (at least) the #GVolume instance, optionally
46  * a #GMountOperation object and a #GAsyncReadyCallback. 
47  *
48  * Typically, one will only want to pass %NULL for the
49  * #GMountOperation if automounting all volumes when a desktop session
50  * starts since it's not desirable to put up a lot of dialogs asking
51  * for credentials.
52  *
53  * The callback will be fired when the operation has resolved (either
54  * with success or failure), and a #GAsyncReady structure will be
55  * passed to the callback.  That callback should then call
56  * g_volume_mount_finish() with the #GVolume instance and the
57  * #GAsyncReady data to see if the operation was completed
58  * successfully.  If an @error is present when g_volume_mount_finish()
59  * is called, then it will be filled with any error information.
60  *
61  * <para id="volume-identifier">
62  * It is sometimes necessary to directly access the underlying
63  * operating system object behind a volume (e.g. for passing a volume
64  * to an application via the commandline). For this purpose, GIO
65  * allows to obtain an 'identifier' for the volume. There can be
66  * different kinds of identifiers, such as Hal UDIs, filesystem labels,
67  * traditional Unix devices (e.g. <filename>/dev/sda2</filename>),
68  * uuids. GIO uses predefind strings as names for the different kinds
69  * of identifiers: #G_VOLUME_IDENTIFIER_KIND_HAL_UDI,
70  * #G_VOLUME_IDENTIFIER_KIND_LABEL, etc. Use g_volume_get_identifier()
71  * to obtain an identifier for a volume.
72  * </para>
73  *
74  * Note that #G_VOLUME_IDENTIFIER_KIND_HAL_UDI will only be available
75  * when the gvfs hal volume monitor is in use. Other volume monitors
76  * will generally be able to provide the #G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE
77  * identifier, which can be used to obtain a hal device by means of
78  * libhal_manger_find_device_string_match().
79  */
80
81 typedef GVolumeIface GVolumeInterface;
82 G_DEFINE_INTERFACE(GVolume, g_volume, G_TYPE_OBJECT)
83
84 static void
85 g_volume_default_init (GVolumeInterface *iface)
86 {
87   /**
88    * GVolume::changed:
89    * 
90    * Emitted when the volume has been changed.
91    **/
92   g_signal_new (I_("changed"),
93                 G_TYPE_VOLUME,
94                 G_SIGNAL_RUN_LAST,
95                 G_STRUCT_OFFSET (GVolumeIface, changed),
96                 NULL, NULL,
97                 g_cclosure_marshal_VOID__VOID,
98                 G_TYPE_NONE, 0);
99
100   /**
101    * GVolume::removed:
102    * 
103    * This signal is emitted when the #GVolume have been removed. If
104    * the recipient is holding references to the object they should
105    * release them so the object can be finalized.
106    **/
107   g_signal_new (I_("removed"),
108                 G_TYPE_VOLUME,
109                 G_SIGNAL_RUN_LAST,
110                 G_STRUCT_OFFSET (GVolumeIface, removed),
111                 NULL, NULL,
112                 g_cclosure_marshal_VOID__VOID,
113                 G_TYPE_NONE, 0);
114 }
115
116 /**
117  * g_volume_get_name:
118  * @volume: a #GVolume.
119  * 
120  * Gets the name of @volume.
121  * 
122  * Returns: the name for the given @volume. The returned string should 
123  * be freed with g_free() when no longer needed.
124  **/
125 char *
126 g_volume_get_name (GVolume *volume)
127 {
128   GVolumeIface *iface;
129
130   g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
131
132   iface = G_VOLUME_GET_IFACE (volume);
133
134   return (* iface->get_name) (volume);
135 }
136
137 /**
138  * g_volume_get_icon:
139  * @volume: a #GVolume.
140  * 
141  * Gets the icon for @volume.
142  * 
143  * Returns: (transfer full): a #GIcon.
144  *     The returned object should be unreffed with g_object_unref()
145  *     when no longer needed.
146  **/
147 GIcon *
148 g_volume_get_icon (GVolume *volume)
149 {
150   GVolumeIface *iface;
151
152   g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
153
154   iface = G_VOLUME_GET_IFACE (volume);
155
156   return (* iface->get_icon) (volume);
157 }
158
159 /**
160  * g_volume_get_uuid:
161  * @volume: a #GVolume.
162  * 
163  * Gets the UUID for the @volume. The reference is typically based on
164  * the file system UUID for the volume in question and should be
165  * considered an opaque string. Returns %NULL if there is no UUID
166  * available.
167  * 
168  * Returns: the UUID for @volume or %NULL if no UUID can be computed.
169  *     The returned string should be freed with g_free() 
170  *     when no longer needed.
171  **/
172 char *
173 g_volume_get_uuid (GVolume *volume)
174 {
175   GVolumeIface *iface;
176
177   g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
178
179   iface = G_VOLUME_GET_IFACE (volume);
180
181   return (* iface->get_uuid) (volume);
182 }
183   
184 /**
185  * g_volume_get_drive:
186  * @volume: a #GVolume.
187  * 
188  * Gets the drive for the @volume.
189  * 
190  * Returns: (transfer full): a #GDrive or %NULL if @volume is not associated with a drive.
191  *     The returned object should be unreffed with g_object_unref()
192  *     when no longer needed.
193  **/
194 GDrive *
195 g_volume_get_drive (GVolume *volume)
196 {
197   GVolumeIface *iface;
198
199   g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
200
201   iface = G_VOLUME_GET_IFACE (volume);
202
203   return (* iface->get_drive) (volume);
204 }
205
206 /**
207  * g_volume_get_mount:
208  * @volume: a #GVolume.
209  * 
210  * Gets the mount for the @volume.
211  * 
212  * Returns: (transfer full): a #GMount or %NULL if @volume isn't mounted.
213  *     The returned object should be unreffed with g_object_unref()
214  *     when no longer needed.
215  **/
216 GMount *
217 g_volume_get_mount (GVolume *volume)
218 {
219   GVolumeIface *iface;
220
221   g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
222
223   iface = G_VOLUME_GET_IFACE (volume);
224
225   return (* iface->get_mount) (volume);
226 }
227
228
229 /**
230  * g_volume_can_mount:
231  * @volume: a #GVolume.
232  * 
233  * Checks if a volume can be mounted.
234  * 
235  * Returns: %TRUE if the @volume can be mounted. %FALSE otherwise.
236  **/
237 gboolean
238 g_volume_can_mount (GVolume *volume)
239 {
240   GVolumeIface *iface;
241
242   g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
243
244   iface = G_VOLUME_GET_IFACE (volume);
245
246   if (iface->can_mount == NULL)
247     return FALSE;
248
249   return (* iface->can_mount) (volume);
250 }
251
252 /**
253  * g_volume_can_eject:
254  * @volume: a #GVolume.
255  * 
256  * Checks if a volume can be ejected.
257  * 
258  * Returns: %TRUE if the @volume can be ejected. %FALSE otherwise.
259  **/
260 gboolean
261 g_volume_can_eject (GVolume *volume)
262 {
263   GVolumeIface *iface;
264
265   g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
266
267   iface = G_VOLUME_GET_IFACE (volume);
268
269   if (iface->can_eject == NULL)
270     return FALSE;
271
272   return (* iface->can_eject) (volume);
273 }
274
275 /**
276  * g_volume_should_automount:
277  * @volume: a #GVolume
278  *
279  * Returns whether the volume should be automatically mounted.
280  * 
281  * Returns: %TRUE if the volume should be automatically mounted.
282  */
283 gboolean
284 g_volume_should_automount (GVolume *volume)
285 {
286   GVolumeIface *iface;
287
288   g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
289
290   iface = G_VOLUME_GET_IFACE (volume);
291
292   if (iface->should_automount == NULL)
293     return FALSE;
294
295   return (* iface->should_automount) (volume);
296 }
297
298
299 /**
300  * g_volume_mount:
301  * @volume: a #GVolume.
302  * @flags: flags affecting the operation
303  * @mount_operation: (allow-none): a #GMountOperation or %NULL to avoid user interaction.
304  * @cancellable: optional #GCancellable object, %NULL to ignore.
305  * @callback: a #GAsyncReadyCallback, or %NULL.
306  * @user_data: user data that gets passed to @callback
307  * 
308  * Mounts a volume. This is an asynchronous operation, and is
309  * finished by calling g_volume_mount_finish() with the @volume
310  * and #GAsyncResult returned in the @callback.
311  *
312  * Virtual: mount_fn
313  **/
314 void
315 g_volume_mount (GVolume             *volume,
316                 GMountMountFlags     flags,
317                 GMountOperation     *mount_operation,
318                 GCancellable        *cancellable,
319                 GAsyncReadyCallback  callback,
320                 gpointer             user_data)
321 {
322   GVolumeIface *iface;
323
324   g_return_if_fail (G_IS_VOLUME (volume));
325
326   iface = G_VOLUME_GET_IFACE (volume);
327
328   if (iface->mount_fn == NULL)
329     {
330       g_simple_async_report_error_in_idle (G_OBJECT (volume), callback, user_data,
331                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
332                                            _("volume doesn't implement mount"));
333       
334       return;
335     }
336   
337   (* iface->mount_fn) (volume, flags, mount_operation, cancellable, callback, user_data);
338 }
339
340 /**
341  * g_volume_mount_finish:
342  * @volume: a #GVolume
343  * @result: a #GAsyncResult
344  * @error: a #GError location to store an error, or %NULL to ignore
345  * 
346  * Finishes mounting a volume. If any errors occured during the operation,
347  * @error will be set to contain the errors and %FALSE will be returned.
348  *
349  * If the mount operation succeeded, g_volume_get_mount() on @volume
350  * is guaranteed to return the mount right after calling this
351  * function; there's no need to listen for the 'mount-added' signal on
352  * #GVolumeMonitor.
353  * 
354  * Returns: %TRUE, %FALSE if operation failed.
355  **/
356 gboolean
357 g_volume_mount_finish (GVolume       *volume,
358                        GAsyncResult  *result,
359                        GError       **error)
360 {
361   GVolumeIface *iface;
362
363   g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
364   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
365
366   if (G_IS_SIMPLE_ASYNC_RESULT (result))
367     {
368       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
369       if (g_simple_async_result_propagate_error (simple, error))
370         return FALSE;
371     }
372   
373   iface = G_VOLUME_GET_IFACE (volume);
374   return (* iface->mount_finish) (volume, result, error);
375 }
376
377 /**
378  * g_volume_eject:
379  * @volume: a #GVolume.
380  * @flags: flags affecting the unmount if required for eject
381  * @cancellable: optional #GCancellable object, %NULL to ignore.
382  * @callback: a #GAsyncReadyCallback, or %NULL.
383  * @user_data: user data that gets passed to @callback
384  * 
385  * Ejects a volume. This is an asynchronous operation, and is
386  * finished by calling g_volume_eject_finish() with the @volume
387  * and #GAsyncResult returned in the @callback.
388  *
389  * Deprecated: 2.22: Use g_volume_eject_with_operation() instead.
390  **/
391 void
392 g_volume_eject (GVolume             *volume,
393                 GMountUnmountFlags   flags,
394                 GCancellable        *cancellable,
395                 GAsyncReadyCallback  callback,
396                 gpointer             user_data)
397 {
398   GVolumeIface *iface;
399
400   g_return_if_fail (G_IS_VOLUME (volume));
401
402   iface = G_VOLUME_GET_IFACE (volume);
403
404   if (iface->eject == NULL)
405     {
406       g_simple_async_report_error_in_idle (G_OBJECT (volume), callback, user_data,
407                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
408                                            _("volume doesn't implement eject"));
409       
410       return;
411     }
412   
413   (* iface->eject) (volume, flags, cancellable, callback, user_data);
414 }
415
416 /**
417  * g_volume_eject_finish:
418  * @volume: pointer to a #GVolume.
419  * @result: a #GAsyncResult.
420  * @error: a #GError location to store an error, or %NULL to ignore
421  * 
422  * Finishes ejecting a volume. If any errors occured during the operation,
423  * @error will be set to contain the errors and %FALSE will be returned.
424  * 
425  * Returns: %TRUE, %FALSE if operation failed.
426  *
427  * Deprecated: 2.22: Use g_volume_eject_with_operation_finish() instead.
428  **/
429 gboolean
430 g_volume_eject_finish (GVolume       *volume,
431                        GAsyncResult  *result,
432                        GError       **error)
433 {
434   GVolumeIface *iface;
435
436   g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
437   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
438
439   if (G_IS_SIMPLE_ASYNC_RESULT (result))
440     {
441       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
442       if (g_simple_async_result_propagate_error (simple, error))
443         return FALSE;
444     }
445   
446   iface = G_VOLUME_GET_IFACE (volume);
447   return (* iface->eject_finish) (volume, result, error);
448 }
449
450 /**
451  * g_volume_eject_with_operation:
452  * @volume: a #GVolume.
453  * @flags: flags affecting the unmount if required for eject
454  * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
455  * @cancellable: optional #GCancellable object, %NULL to ignore.
456  * @callback: a #GAsyncReadyCallback, or %NULL.
457  * @user_data: user data passed to @callback.
458  *
459  * Ejects a volume. This is an asynchronous operation, and is
460  * finished by calling g_volume_eject_with_operation_finish() with the @volume
461  * and #GAsyncResult data returned in the @callback.
462  *
463  * Since: 2.22
464  **/
465 void
466 g_volume_eject_with_operation (GVolume              *volume,
467                                GMountUnmountFlags   flags,
468                                GMountOperation     *mount_operation,
469                                GCancellable        *cancellable,
470                                GAsyncReadyCallback  callback,
471                                gpointer             user_data)
472 {
473   GVolumeIface *iface;
474
475   g_return_if_fail (G_IS_VOLUME (volume));
476
477   iface = G_VOLUME_GET_IFACE (volume);
478
479   if (iface->eject == NULL && iface->eject_with_operation == NULL)
480     {
481       g_simple_async_report_error_in_idle (G_OBJECT (volume),
482                                            callback, user_data,
483                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
484                                            /* Translators: This is an error
485                                             * message for volume objects that
486                                             * don't implement any of eject or eject_with_operation. */
487                                            _("volume doesn't implement eject or eject_with_operation"));
488       return;
489     }
490
491   if (iface->eject_with_operation != NULL)
492     (* iface->eject_with_operation) (volume, flags, mount_operation, cancellable, callback, user_data);
493   else
494     (* iface->eject) (volume, flags, cancellable, callback, user_data);
495 }
496
497 /**
498  * g_volume_eject_with_operation_finish:
499  * @volume: a #GVolume.
500  * @result: a #GAsyncResult.
501  * @error: a #GError location to store the error occuring, or %NULL to
502  *     ignore.
503  *
504  * Finishes ejecting a volume. If any errors occurred during the operation,
505  * @error will be set to contain the errors and %FALSE will be returned.
506  *
507  * Returns: %TRUE if the volume was successfully ejected. %FALSE otherwise.
508  *
509  * Since: 2.22
510  **/
511 gboolean
512 g_volume_eject_with_operation_finish (GVolume        *volume,
513                                       GAsyncResult  *result,
514                                       GError       **error)
515 {
516   GVolumeIface *iface;
517
518   g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
519   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
520
521   if (G_IS_SIMPLE_ASYNC_RESULT (result))
522     {
523       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
524       if (g_simple_async_result_propagate_error (simple, error))
525         return FALSE;
526     }
527
528   iface = G_VOLUME_GET_IFACE (volume);
529   if (iface->eject_with_operation_finish != NULL)
530     return (* iface->eject_with_operation_finish) (volume, result, error);
531   else
532     return (* iface->eject_finish) (volume, result, error);
533 }
534
535 /**
536  * g_volume_get_identifier:
537  * @volume: a #GVolume
538  * @kind: the kind of identifier to return
539  *
540  * Gets the identifier of the given kind for @volume. 
541  * See the <link linkend="volume-identifier">introduction</link>
542  * for more information about volume identifiers.
543  *
544  * Returns: a newly allocated string containing the
545  *   requested identfier, or %NULL if the #GVolume
546  *   doesn't have this kind of identifier
547  */
548 char *
549 g_volume_get_identifier (GVolume    *volume,
550                          const char *kind)
551 {
552   GVolumeIface *iface;
553
554   g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
555   g_return_val_if_fail (kind != NULL, NULL);
556
557   iface = G_VOLUME_GET_IFACE (volume);
558
559   if (iface->get_identifier == NULL)
560     return NULL;
561   
562   return (* iface->get_identifier) (volume, kind);
563 }
564
565 /**
566  * g_volume_enumerate_identifiers:
567  * @volume: a #GVolume
568  * 
569  * Gets the kinds of <link linkend="volume-identifier">identifiers</link>
570  * that @volume has. Use g_volume_get_identifer() to obtain 
571  * the identifiers themselves.
572  *
573  * Returns: (array zero-terminated=1) (transfer full): a %NULL-terminated array
574  *   of strings containing kinds of identifiers. Use g_strfreev() to free.
575  */
576 char **
577 g_volume_enumerate_identifiers (GVolume *volume)
578 {
579   GVolumeIface *iface;
580
581   g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
582   iface = G_VOLUME_GET_IFACE (volume);
583
584   if (iface->enumerate_identifiers == NULL)
585     return NULL;
586   
587   return (* iface->enumerate_identifiers) (volume);
588 }
589
590 /**
591  * g_volume_get_activation_root:
592  * @volume: a #GVolume
593  *
594  * Gets the activation root for a #GVolume if it is known ahead of
595  * mount time. Returns %NULL otherwise. If not %NULL and if @volume
596  * is mounted, then the result of g_mount_get_root() on the
597  * #GMount object obtained from g_volume_get_mount() will always
598  * either be equal or a prefix of what this function returns. In
599  * other words, in code
600  *
601  * <programlisting>
602  *   GMount *mount;
603  *   GFile *mount_root
604  *   GFile *volume_activation_root;
605  *
606  *   mount = g_volume_get_mount (volume); /&ast; mounted, so never NULL &ast;/
607  *   mount_root = g_mount_get_root (mount);
608  *   volume_activation_root = g_volume_get_activation_root(volume); /&ast; assume not NULL &ast;/
609  * </programlisting>
610  *
611  * then the expression
612  *
613  * <programlisting>
614  *   (g_file_has_prefix (volume_activation_root, mount_root) ||
615       g_file_equal (volume_activation_root, mount_root))
616  * </programlisting>
617  *
618  * will always be %TRUE.
619  *
620  * Activation roots are typically used in #GVolumeMonitor
621  * implementations to find the underlying mount to shadow, see
622  * g_mount_is_shadowed() for more details.
623  *
624  * Returns: (transfer full): the activation root of @volume or %NULL. Use
625  * g_object_unref() to free.
626  *
627  * Since: 2.18
628  **/
629 GFile *
630 g_volume_get_activation_root (GVolume *volume)
631 {
632   GVolumeIface *iface;
633
634   g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
635   iface = G_VOLUME_GET_IFACE (volume);
636
637   if (iface->get_activation_root == NULL)
638     return NULL;
639
640   return (* iface->get_activation_root) (volume);
641 }