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"
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 * #GTask. To mount a #GVolume, first call g_volume_mount() with (at
46 * least) the #GVolume instance, optionally a #GMountOperation object
47 * 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_task_report_new_error (volume, callback, user_data,
364 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
365 _("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))
400 else if (g_async_result_is_tagged (result, g_volume_mount))
401 return g_task_propagate_boolean (G_TASK (result), error);
403 iface = G_VOLUME_GET_IFACE (volume);
404 return (* iface->mount_finish) (volume, result, error);
409 * @volume: a #GVolume.
410 * @flags: flags affecting the unmount if required for eject
411 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
412 * @callback: (allow-none): a #GAsyncReadyCallback, or %NULL.
413 * @user_data: user data that gets passed to @callback
415 * Ejects a volume. This is an asynchronous operation, and is
416 * finished by calling g_volume_eject_finish() with the @volume
417 * and #GAsyncResult returned in the @callback.
419 * Deprecated: 2.22: Use g_volume_eject_with_operation() instead.
422 g_volume_eject (GVolume *volume,
423 GMountUnmountFlags flags,
424 GCancellable *cancellable,
425 GAsyncReadyCallback callback,
430 g_return_if_fail (G_IS_VOLUME (volume));
432 iface = G_VOLUME_GET_IFACE (volume);
434 if (iface->eject == NULL)
436 g_task_report_new_error (volume, callback, user_data,
437 g_volume_eject_with_operation,
438 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
439 _("volume doesn't implement eject"));
443 (* iface->eject) (volume, flags, cancellable, callback, user_data);
447 * g_volume_eject_finish:
448 * @volume: pointer to a #GVolume.
449 * @result: a #GAsyncResult.
450 * @error: a #GError location to store an error, or %NULL to ignore
452 * Finishes ejecting a volume. If any errors occurred during the operation,
453 * @error will be set to contain the errors and %FALSE will be returned.
455 * Returns: %TRUE, %FALSE if operation failed.
457 * Deprecated: 2.22: Use g_volume_eject_with_operation_finish() instead.
460 g_volume_eject_finish (GVolume *volume,
461 GAsyncResult *result,
466 g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
467 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
469 if (g_async_result_legacy_propagate_error (result, error))
471 if (g_async_result_is_tagged (result, g_volume_eject_with_operation))
472 return g_task_propagate_boolean (G_TASK (result), error);
474 iface = G_VOLUME_GET_IFACE (volume);
475 return (* iface->eject_finish) (volume, result, error);
479 * g_volume_eject_with_operation:
480 * @volume: a #GVolume.
481 * @flags: flags affecting the unmount if required for eject
482 * @mount_operation: (allow-none): a #GMountOperation or %NULL to
483 * avoid user interaction.
484 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
485 * @callback: (allow-none): a #GAsyncReadyCallback, or %NULL.
486 * @user_data: user data passed to @callback.
488 * Ejects a volume. This is an asynchronous operation, and is
489 * finished by calling g_volume_eject_with_operation_finish() with the @volume
490 * and #GAsyncResult data returned in the @callback.
495 g_volume_eject_with_operation (GVolume *volume,
496 GMountUnmountFlags flags,
497 GMountOperation *mount_operation,
498 GCancellable *cancellable,
499 GAsyncReadyCallback callback,
504 g_return_if_fail (G_IS_VOLUME (volume));
506 iface = G_VOLUME_GET_IFACE (volume);
508 if (iface->eject == NULL && iface->eject_with_operation == NULL)
510 g_task_report_new_error (volume, callback, user_data,
511 g_volume_eject_with_operation,
512 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
513 /* Translators: This is an error
514 * message for volume objects that
515 * don't implement any of eject or eject_with_operation. */
516 _("volume doesn't implement eject or eject_with_operation"));
520 if (iface->eject_with_operation != NULL)
521 (* iface->eject_with_operation) (volume, flags, mount_operation, cancellable, callback, user_data);
523 (* iface->eject) (volume, flags, cancellable, callback, user_data);
527 * g_volume_eject_with_operation_finish:
528 * @volume: a #GVolume.
529 * @result: a #GAsyncResult.
530 * @error: a #GError location to store the error occurring, or %NULL to
533 * Finishes ejecting a volume. If any errors occurred during the operation,
534 * @error will be set to contain the errors and %FALSE will be returned.
536 * Returns: %TRUE if the volume was successfully ejected. %FALSE otherwise.
541 g_volume_eject_with_operation_finish (GVolume *volume,
542 GAsyncResult *result,
547 g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
548 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
550 if (g_async_result_legacy_propagate_error (result, error))
552 else if (g_async_result_is_tagged (result, g_volume_eject_with_operation))
553 return g_task_propagate_boolean (G_TASK (result), error);
555 iface = G_VOLUME_GET_IFACE (volume);
556 if (iface->eject_with_operation_finish != NULL)
557 return (* iface->eject_with_operation_finish) (volume, result, error);
559 return (* iface->eject_finish) (volume, result, error);
563 * g_volume_get_identifier:
564 * @volume: a #GVolume
565 * @kind: the kind of identifier to return
567 * Gets the identifier of the given kind for @volume.
568 * See the <link linkend="volume-identifier">introduction</link>
569 * for more information about volume identifiers.
571 * Returns: a newly allocated string containing the
572 * requested identfier, or %NULL if the #GVolume
573 * doesn't have this kind of identifier
576 g_volume_get_identifier (GVolume *volume,
581 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
582 g_return_val_if_fail (kind != NULL, NULL);
584 iface = G_VOLUME_GET_IFACE (volume);
586 if (iface->get_identifier == NULL)
589 return (* iface->get_identifier) (volume, kind);
593 * g_volume_enumerate_identifiers:
594 * @volume: a #GVolume
596 * Gets the kinds of <link linkend="volume-identifier">identifiers</link>
597 * that @volume has. Use g_volume_get_identifier() to obtain
598 * the identifiers themselves.
600 * Returns: (array zero-terminated=1) (transfer full): a %NULL-terminated array
601 * of strings containing kinds of identifiers. Use g_strfreev() to free.
604 g_volume_enumerate_identifiers (GVolume *volume)
608 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
609 iface = G_VOLUME_GET_IFACE (volume);
611 if (iface->enumerate_identifiers == NULL)
614 return (* iface->enumerate_identifiers) (volume);
618 * g_volume_get_activation_root:
619 * @volume: a #GVolume
621 * Gets the activation root for a #GVolume if it is known ahead of
622 * mount time. Returns %NULL otherwise. If not %NULL and if @volume
623 * is mounted, then the result of g_mount_get_root() on the
624 * #GMount object obtained from g_volume_get_mount() will always
625 * either be equal or a prefix of what this function returns. In
626 * other words, in code
631 * GFile *volume_activation_root;
633 * mount = g_volume_get_mount (volume); /* mounted, so never NULL */
634 * mount_root = g_mount_get_root (mount);
635 * volume_activation_root = g_volume_get_activation_root(volume); /* assume not NULL */
638 * then the expression
641 * (g_file_has_prefix (volume_activation_root, mount_root) ||
642 g_file_equal (volume_activation_root, mount_root))
645 * will always be %TRUE.
647 * Activation roots are typically used in #GVolumeMonitor
648 * implementations to find the underlying mount to shadow, see
649 * g_mount_is_shadowed() for more details.
651 * Returns: (transfer full): the activation root of @volume or %NULL. Use
652 * g_object_unref() to free.
657 g_volume_get_activation_root (GVolume *volume)
661 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
662 iface = G_VOLUME_GET_IFACE (volume);
664 if (iface->get_activation_root == NULL)
667 return (* iface->get_activation_root) (volume);
671 * g_volume_get_sort_key:
672 * @volume: A #GVolume.
674 * Gets the sort key for @volume, if any.
676 * Returns: Sorting key for @volume or %NULL if no such key is available.
681 g_volume_get_sort_key (GVolume *volume)
683 const gchar *ret = NULL;
686 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
688 iface = G_VOLUME_GET_IFACE (volume);
689 if (iface->get_sort_key != NULL)
690 ret = iface->get_sort_key (volume);