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