1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
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.
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.
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.
20 * Author: Alexander Larsson <alexl@redhat.com>
21 * David Zeuthen <davidz@redhat.com>
27 #include "gthemedicon.h"
28 #include "gasyncresult.h"
29 #include "gsimpleasyncresult.h"
36 * @short_description: Volume management
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.
43 * Mounting a #GVolume instance is an asynchronous operation. For more
44 * information about asynchronous operations, see #GAsyncResult and
45 * #GSimpleAsyncResult. To mount a #GVolume, first call
46 * g_volume_mount() with (at least) the #GVolume instance, optionally
47 * a #GMountOperation object and a #GAsyncReadyCallback.
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
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.
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.
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_manager_find_device_string_match().
82 typedef GVolumeIface GVolumeInterface;
83 G_DEFINE_INTERFACE(GVolume, g_volume, G_TYPE_OBJECT)
86 g_volume_default_init (GVolumeInterface *iface)
91 * Emitted when the volume has been changed.
93 g_signal_new (I_("changed"),
96 G_STRUCT_OFFSET (GVolumeIface, changed),
98 g_cclosure_marshal_VOID__VOID,
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.
108 g_signal_new (I_("removed"),
111 G_STRUCT_OFFSET (GVolumeIface, removed),
113 g_cclosure_marshal_VOID__VOID,
119 * @volume: a #GVolume.
121 * Gets the name of @volume.
123 * Returns: the name for the given @volume. The returned string should
124 * be freed with g_free() when no longer needed.
127 g_volume_get_name (GVolume *volume)
131 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
133 iface = G_VOLUME_GET_IFACE (volume);
135 return (* iface->get_name) (volume);
140 * @volume: a #GVolume.
142 * Gets the icon for @volume.
144 * Returns: (transfer full): a #GIcon.
145 * The returned object should be unreffed with g_object_unref()
146 * when no longer needed.
149 g_volume_get_icon (GVolume *volume)
153 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
155 iface = G_VOLUME_GET_IFACE (volume);
157 return (* iface->get_icon) (volume);
161 * g_volume_get_symbolic_icon:
162 * @volume: a #GVolume.
164 * Gets the symbolic icon for @volume.
166 * Returns: (transfer full): a #GIcon.
167 * The returned object should be unreffed with g_object_unref()
168 * when no longer needed.
173 g_volume_get_symbolic_icon (GVolume *volume)
178 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
180 iface = G_VOLUME_GET_IFACE (volume);
182 if (iface->get_symbolic_icon != NULL)
183 ret = iface->get_symbolic_icon (volume);
185 ret = g_themed_icon_new_with_default_fallbacks ("folder-remote-symbolic");
193 * @volume: a #GVolume.
195 * Gets the UUID for the @volume. The reference is typically based on
196 * the file system UUID for the volume in question and should be
197 * considered an opaque string. Returns %NULL if there is no UUID
200 * Returns: the UUID for @volume or %NULL if no UUID can be computed.
201 * The returned string should be freed with g_free()
202 * when no longer needed.
205 g_volume_get_uuid (GVolume *volume)
209 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
211 iface = G_VOLUME_GET_IFACE (volume);
213 return (* iface->get_uuid) (volume);
217 * g_volume_get_drive:
218 * @volume: a #GVolume.
220 * Gets the drive for the @volume.
222 * Returns: (transfer full): a #GDrive or %NULL if @volume is not associated with a drive.
223 * The returned object should be unreffed with g_object_unref()
224 * when no longer needed.
227 g_volume_get_drive (GVolume *volume)
231 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
233 iface = G_VOLUME_GET_IFACE (volume);
235 return (* iface->get_drive) (volume);
239 * g_volume_get_mount:
240 * @volume: a #GVolume.
242 * Gets the mount for the @volume.
244 * Returns: (transfer full): a #GMount or %NULL if @volume isn't mounted.
245 * The returned object should be unreffed with g_object_unref()
246 * when no longer needed.
249 g_volume_get_mount (GVolume *volume)
253 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
255 iface = G_VOLUME_GET_IFACE (volume);
257 return (* iface->get_mount) (volume);
262 * g_volume_can_mount:
263 * @volume: a #GVolume.
265 * Checks if a volume can be mounted.
267 * Returns: %TRUE if the @volume can be mounted. %FALSE otherwise.
270 g_volume_can_mount (GVolume *volume)
274 g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
276 iface = G_VOLUME_GET_IFACE (volume);
278 if (iface->can_mount == NULL)
281 return (* iface->can_mount) (volume);
285 * g_volume_can_eject:
286 * @volume: a #GVolume.
288 * Checks if a volume can be ejected.
290 * Returns: %TRUE if the @volume can be ejected. %FALSE otherwise.
293 g_volume_can_eject (GVolume *volume)
297 g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
299 iface = G_VOLUME_GET_IFACE (volume);
301 if (iface->can_eject == NULL)
304 return (* iface->can_eject) (volume);
308 * g_volume_should_automount:
309 * @volume: a #GVolume
311 * Returns whether the volume should be automatically mounted.
313 * Returns: %TRUE if the volume should be automatically mounted.
316 g_volume_should_automount (GVolume *volume)
320 g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
322 iface = G_VOLUME_GET_IFACE (volume);
324 if (iface->should_automount == NULL)
327 return (* iface->should_automount) (volume);
333 * @volume: a #GVolume.
334 * @flags: flags affecting the operation
335 * @mount_operation: (allow-none): a #GMountOperation or %NULL to avoid user interaction.
336 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
337 * @callback: (allow-none): a #GAsyncReadyCallback, or %NULL.
338 * @user_data: user data that gets passed to @callback
340 * Mounts a volume. This is an asynchronous operation, and is
341 * finished by calling g_volume_mount_finish() with the @volume
342 * and #GAsyncResult returned in the @callback.
347 g_volume_mount (GVolume *volume,
348 GMountMountFlags flags,
349 GMountOperation *mount_operation,
350 GCancellable *cancellable,
351 GAsyncReadyCallback callback,
356 g_return_if_fail (G_IS_VOLUME (volume));
358 iface = G_VOLUME_GET_IFACE (volume);
360 if (iface->mount_fn == NULL)
362 g_simple_async_report_error_in_idle (G_OBJECT (volume), callback, user_data,
363 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
364 _("volume doesn't implement mount"));
369 (* iface->mount_fn) (volume, flags, mount_operation, cancellable, callback, user_data);
373 * g_volume_mount_finish:
374 * @volume: a #GVolume
375 * @result: a #GAsyncResult
376 * @error: a #GError location to store an error, or %NULL to ignore
378 * Finishes mounting a volume. If any errors occurred during the operation,
379 * @error will be set to contain the errors and %FALSE will be returned.
381 * If the mount operation succeeded, g_volume_get_mount() on @volume
382 * is guaranteed to return the mount right after calling this
383 * function; there's no need to listen for the 'mount-added' signal on
386 * Returns: %TRUE, %FALSE if operation failed.
389 g_volume_mount_finish (GVolume *volume,
390 GAsyncResult *result,
395 g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
396 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
398 if (g_async_result_legacy_propagate_error (result, error))
401 iface = G_VOLUME_GET_IFACE (volume);
402 return (* iface->mount_finish) (volume, result, error);
407 * @volume: a #GVolume.
408 * @flags: flags affecting the unmount if required for eject
409 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
410 * @callback: (allow-none): a #GAsyncReadyCallback, or %NULL.
411 * @user_data: user data that gets passed to @callback
413 * Ejects a volume. This is an asynchronous operation, and is
414 * finished by calling g_volume_eject_finish() with the @volume
415 * and #GAsyncResult returned in the @callback.
417 * Deprecated: 2.22: Use g_volume_eject_with_operation() instead.
420 g_volume_eject (GVolume *volume,
421 GMountUnmountFlags flags,
422 GCancellable *cancellable,
423 GAsyncReadyCallback callback,
428 g_return_if_fail (G_IS_VOLUME (volume));
430 iface = G_VOLUME_GET_IFACE (volume);
432 if (iface->eject == NULL)
434 g_simple_async_report_error_in_idle (G_OBJECT (volume), callback, user_data,
435 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
436 _("volume doesn't implement eject"));
441 (* iface->eject) (volume, flags, cancellable, callback, user_data);
445 * g_volume_eject_finish:
446 * @volume: pointer to a #GVolume.
447 * @result: a #GAsyncResult.
448 * @error: a #GError location to store an error, or %NULL to ignore
450 * Finishes ejecting a volume. If any errors occurred during the operation,
451 * @error will be set to contain the errors and %FALSE will be returned.
453 * Returns: %TRUE, %FALSE if operation failed.
455 * Deprecated: 2.22: Use g_volume_eject_with_operation_finish() instead.
458 g_volume_eject_finish (GVolume *volume,
459 GAsyncResult *result,
464 g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
465 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
467 if (g_async_result_legacy_propagate_error (result, error))
470 iface = G_VOLUME_GET_IFACE (volume);
471 return (* iface->eject_finish) (volume, result, error);
475 * g_volume_eject_with_operation:
476 * @volume: a #GVolume.
477 * @flags: flags affecting the unmount if required for eject
478 * @mount_operation: (allow-none): a #GMountOperation or %NULL to
479 * avoid user interaction.
480 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
481 * @callback: (allow-none): a #GAsyncReadyCallback, or %NULL.
482 * @user_data: user data passed to @callback.
484 * Ejects a volume. This is an asynchronous operation, and is
485 * finished by calling g_volume_eject_with_operation_finish() with the @volume
486 * and #GAsyncResult data returned in the @callback.
491 g_volume_eject_with_operation (GVolume *volume,
492 GMountUnmountFlags flags,
493 GMountOperation *mount_operation,
494 GCancellable *cancellable,
495 GAsyncReadyCallback callback,
500 g_return_if_fail (G_IS_VOLUME (volume));
502 iface = G_VOLUME_GET_IFACE (volume);
504 if (iface->eject == NULL && iface->eject_with_operation == NULL)
506 g_simple_async_report_error_in_idle (G_OBJECT (volume),
508 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
509 /* Translators: This is an error
510 * message for volume objects that
511 * don't implement any of eject or eject_with_operation. */
512 _("volume doesn't implement eject or eject_with_operation"));
516 if (iface->eject_with_operation != NULL)
517 (* iface->eject_with_operation) (volume, flags, mount_operation, cancellable, callback, user_data);
519 (* iface->eject) (volume, flags, cancellable, callback, user_data);
523 * g_volume_eject_with_operation_finish:
524 * @volume: a #GVolume.
525 * @result: a #GAsyncResult.
526 * @error: a #GError location to store the error occurring, or %NULL to
529 * Finishes ejecting a volume. If any errors occurred during the operation,
530 * @error will be set to contain the errors and %FALSE will be returned.
532 * Returns: %TRUE if the volume was successfully ejected. %FALSE otherwise.
537 g_volume_eject_with_operation_finish (GVolume *volume,
538 GAsyncResult *result,
543 g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
544 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
546 if (g_async_result_legacy_propagate_error (result, error))
549 iface = G_VOLUME_GET_IFACE (volume);
550 if (iface->eject_with_operation_finish != NULL)
551 return (* iface->eject_with_operation_finish) (volume, result, error);
553 return (* iface->eject_finish) (volume, result, error);
557 * g_volume_get_identifier:
558 * @volume: a #GVolume
559 * @kind: the kind of identifier to return
561 * Gets the identifier of the given kind for @volume.
562 * See the <link linkend="volume-identifier">introduction</link>
563 * for more information about volume identifiers.
565 * Returns: a newly allocated string containing the
566 * requested identfier, or %NULL if the #GVolume
567 * doesn't have this kind of identifier
570 g_volume_get_identifier (GVolume *volume,
575 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
576 g_return_val_if_fail (kind != NULL, NULL);
578 iface = G_VOLUME_GET_IFACE (volume);
580 if (iface->get_identifier == NULL)
583 return (* iface->get_identifier) (volume, kind);
587 * g_volume_enumerate_identifiers:
588 * @volume: a #GVolume
590 * Gets the kinds of <link linkend="volume-identifier">identifiers</link>
591 * that @volume has. Use g_volume_get_identifier() to obtain
592 * the identifiers themselves.
594 * Returns: (array zero-terminated=1) (transfer full): a %NULL-terminated array
595 * of strings containing kinds of identifiers. Use g_strfreev() to free.
598 g_volume_enumerate_identifiers (GVolume *volume)
602 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
603 iface = G_VOLUME_GET_IFACE (volume);
605 if (iface->enumerate_identifiers == NULL)
608 return (* iface->enumerate_identifiers) (volume);
612 * g_volume_get_activation_root:
613 * @volume: a #GVolume
615 * Gets the activation root for a #GVolume if it is known ahead of
616 * mount time. Returns %NULL otherwise. If not %NULL and if @volume
617 * is mounted, then the result of g_mount_get_root() on the
618 * #GMount object obtained from g_volume_get_mount() will always
619 * either be equal or a prefix of what this function returns. In
620 * other words, in code
625 * GFile *volume_activation_root;
627 * mount = g_volume_get_mount (volume); /* mounted, so never NULL */
628 * mount_root = g_mount_get_root (mount);
629 * volume_activation_root = g_volume_get_activation_root(volume); /* assume not NULL */
632 * then the expression
635 * (g_file_has_prefix (volume_activation_root, mount_root) ||
636 g_file_equal (volume_activation_root, mount_root))
639 * will always be %TRUE.
641 * Activation roots are typically used in #GVolumeMonitor
642 * implementations to find the underlying mount to shadow, see
643 * g_mount_is_shadowed() for more details.
645 * Returns: (transfer full): the activation root of @volume or %NULL. Use
646 * g_object_unref() to free.
651 g_volume_get_activation_root (GVolume *volume)
655 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
656 iface = G_VOLUME_GET_IFACE (volume);
658 if (iface->get_activation_root == NULL)
661 return (* iface->get_activation_root) (volume);
665 * g_volume_get_sort_key:
666 * @volume: A #GVolume.
668 * Gets the sort key for @volume, if any.
670 * Returns: Sorting key for @volume or %NULL if no such key is available.
675 g_volume_get_sort_key (GVolume *volume)
677 const gchar *ret = NULL;
680 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
682 iface = G_VOLUME_GET_IFACE (volume);
683 if (iface->get_sort_key != NULL)
684 ret = iface->get_sort_key (volume);