1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
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.1 of the License, or (at your option) any later version.
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.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Author: Alexander Larsson <alexl@redhat.com>
21 * David Zeuthen <davidz@redhat.com>
27 #include "gthemedicon.h"
28 #include "gasyncresult.h"
37 * The `GVolume` interface represents user-visible objects that can be
38 * mounted. Note, when [porting from GnomeVFS](migrating-gnome-vfs.html),
39 * `GVolume` is the moral equivalent of `GnomeVFSDrive`.
41 * Mounting a `GVolume` instance is an asynchronous operation. For more
42 * information about asynchronous operations, see [iface@Gio.AsyncResult] and
43 * [class@Gio.Task]. To mount a `GVolume`, first call [method@Gio.Volume.mount]
44 * with (at least) the `GVolume` instance, optionally a
45 * [class@Gio.MountOperation] object and a [type@Gio.AsyncReadyCallback].
47 * Typically, one will only want to pass `NULL` for the
48 * [class@Gio.MountOperation] if automounting all volumes when a desktop session
49 * starts since it’s not desirable to put up a lot of dialogs asking
52 * The callback will be fired when the operation has resolved (either
53 * with success or failure), and a [iface@Gio.AsyncResult] instance will be
54 * passed to the callback. That callback should then call
55 * [method@Gio.Volume.mount_finish] with the `GVolume` instance and the
56 * [iface@Gio.AsyncResult] data to see if the operation was completed
57 * successfully. If a [type@GLib.Error] is present when
58 * [method@Gio.Volume.mount_finish] is called, then it will be filled with any
61 * ## Volume Identifiers
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 command line). 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. `/dev/sda2`), UUIDs. GIO uses predefined
69 * strings as names for the different kinds of identifiers:
70 * `G_VOLUME_IDENTIFIER_KIND_UUID`, `G_VOLUME_IDENTIFIER_KIND_LABEL`, etc.
71 * Use [method@Gio.Volume.get_identifier] to obtain an identifier for a volume.
73 * Note that `G_VOLUME_IDENTIFIER_KIND_HAL_UDI` will only be available
74 * when the GVFS hal volume monitor is in use. Other volume monitors
75 * will generally be able to provide the `G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE`
76 * identifier, which can be used to obtain a hal device by means of
77 * `libhal_manager_find_device_string_match()`.
80 typedef GVolumeIface GVolumeInterface;
81 G_DEFINE_INTERFACE(GVolume, g_volume, G_TYPE_OBJECT)
84 g_volume_default_init (GVolumeInterface *iface)
89 * Emitted when the volume has been changed.
91 g_signal_new (I_("changed"),
94 G_STRUCT_OFFSET (GVolumeIface, changed),
102 * This signal is emitted when the #GVolume have been removed. If
103 * the recipient is holding references to the object they should
104 * release them so the object can be finalized.
106 g_signal_new (I_("removed"),
109 G_STRUCT_OFFSET (GVolumeIface, removed),
117 * @volume: a #GVolume
119 * Gets the name of @volume.
121 * Returns: the name for the given @volume. The returned string should
122 * be freed with g_free() when no longer needed.
125 g_volume_get_name (GVolume *volume)
129 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
131 iface = G_VOLUME_GET_IFACE (volume);
133 return (* iface->get_name) (volume);
138 * @volume: a #GVolume
140 * Gets the icon for @volume.
142 * Returns: (transfer full): a #GIcon.
143 * The returned object should be unreffed with g_object_unref()
144 * when no longer needed.
147 g_volume_get_icon (GVolume *volume)
151 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
153 iface = G_VOLUME_GET_IFACE (volume);
155 return (* iface->get_icon) (volume);
159 * g_volume_get_symbolic_icon:
160 * @volume: a #GVolume
162 * Gets the symbolic icon for @volume.
164 * Returns: (transfer full): a #GIcon.
165 * The returned object should be unreffed with g_object_unref()
166 * when no longer needed.
171 g_volume_get_symbolic_icon (GVolume *volume)
176 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
178 iface = G_VOLUME_GET_IFACE (volume);
180 if (iface->get_symbolic_icon != NULL)
181 ret = iface->get_symbolic_icon (volume);
183 ret = g_themed_icon_new_with_default_fallbacks ("folder-remote-symbolic");
191 * @volume: a #GVolume
193 * Gets the UUID for the @volume. The reference is typically based on
194 * the file system UUID for the volume in question and should be
195 * considered an opaque string. Returns %NULL if there is no UUID
198 * Returns: (nullable) (transfer full): the UUID for @volume or %NULL if no UUID
200 * The returned string should be freed with g_free()
201 * when no longer needed.
204 g_volume_get_uuid (GVolume *volume)
208 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
210 iface = G_VOLUME_GET_IFACE (volume);
212 return (* iface->get_uuid) (volume);
216 * g_volume_get_drive:
217 * @volume: a #GVolume
219 * Gets the drive for the @volume.
221 * Returns: (transfer full) (nullable): a #GDrive or %NULL if @volume is not
222 * associated with a drive. The returned object should be unreffed
223 * with g_object_unref() when no longer needed.
226 g_volume_get_drive (GVolume *volume)
230 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
232 iface = G_VOLUME_GET_IFACE (volume);
234 return (* iface->get_drive) (volume);
238 * g_volume_get_mount:
239 * @volume: a #GVolume
241 * Gets the mount for the @volume.
243 * Returns: (transfer full) (nullable): a #GMount or %NULL if @volume isn't mounted.
244 * The returned object should be unreffed with g_object_unref()
245 * when no longer needed.
248 g_volume_get_mount (GVolume *volume)
252 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
254 iface = G_VOLUME_GET_IFACE (volume);
256 return (* iface->get_mount) (volume);
261 * g_volume_can_mount:
262 * @volume: a #GVolume
264 * Checks if a volume can be mounted.
266 * Returns: %TRUE if the @volume can be mounted. %FALSE otherwise
269 g_volume_can_mount (GVolume *volume)
273 g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
275 iface = G_VOLUME_GET_IFACE (volume);
277 if (iface->can_mount == NULL)
280 return (* iface->can_mount) (volume);
284 * g_volume_can_eject:
285 * @volume: a #GVolume
287 * Checks if a volume can be ejected.
289 * Returns: %TRUE if the @volume can be ejected. %FALSE otherwise
292 g_volume_can_eject (GVolume *volume)
296 g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
298 iface = G_VOLUME_GET_IFACE (volume);
300 if (iface->can_eject == NULL)
303 return (* iface->can_eject) (volume);
307 * g_volume_should_automount:
308 * @volume: a #GVolume
310 * Returns whether the volume should be automatically mounted.
312 * Returns: %TRUE if the volume should be automatically mounted
315 g_volume_should_automount (GVolume *volume)
319 g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
321 iface = G_VOLUME_GET_IFACE (volume);
323 if (iface->should_automount == NULL)
326 return (* iface->should_automount) (volume);
331 * g_volume_mount: (virtual mount_fn)
332 * @volume: a #GVolume
333 * @flags: flags affecting the operation
334 * @mount_operation: (nullable): a #GMountOperation or %NULL to avoid user interaction
335 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore
336 * @callback: (nullable): a #GAsyncReadyCallback, or %NULL
337 * @user_data: user data that gets passed to @callback
339 * Mounts a volume. This is an asynchronous operation, and is
340 * finished by calling g_volume_mount_finish() with the @volume
341 * and #GAsyncResult returned in the @callback.
344 g_volume_mount (GVolume *volume,
345 GMountMountFlags flags,
346 GMountOperation *mount_operation,
347 GCancellable *cancellable,
348 GAsyncReadyCallback callback,
353 g_return_if_fail (G_IS_VOLUME (volume));
355 iface = G_VOLUME_GET_IFACE (volume);
357 if (iface->mount_fn == NULL)
359 g_task_report_new_error (volume, callback, user_data,
361 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
362 _("volume doesn’t implement mount"));
366 (* iface->mount_fn) (volume, flags, mount_operation, cancellable, callback, user_data);
370 * g_volume_mount_finish:
371 * @volume: a #GVolume
372 * @result: a #GAsyncResult
373 * @error: a #GError location to store an error, or %NULL to ignore
375 * Finishes mounting a volume. If any errors occurred during the operation,
376 * @error will be set to contain the errors and %FALSE will be returned.
378 * If the mount operation succeeded, g_volume_get_mount() on @volume
379 * is guaranteed to return the mount right after calling this
380 * function; there's no need to listen for the 'mount-added' signal on
383 * Returns: %TRUE, %FALSE if operation failed
386 g_volume_mount_finish (GVolume *volume,
387 GAsyncResult *result,
392 g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
393 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
395 if (g_async_result_legacy_propagate_error (result, error))
397 else if (g_async_result_is_tagged (result, g_volume_mount))
398 return g_task_propagate_boolean (G_TASK (result), error);
400 iface = G_VOLUME_GET_IFACE (volume);
401 return (* iface->mount_finish) (volume, result, error);
406 * @volume: a #GVolume
407 * @flags: flags affecting the unmount if required for eject
408 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore
409 * @callback: (nullable): a #GAsyncReadyCallback, or %NULL
410 * @user_data: user data that gets passed to @callback
412 * Ejects a volume. This is an asynchronous operation, and is
413 * finished by calling g_volume_eject_finish() with the @volume
414 * and #GAsyncResult returned in the @callback.
416 * Deprecated: 2.22: Use g_volume_eject_with_operation() instead.
419 g_volume_eject (GVolume *volume,
420 GMountUnmountFlags flags,
421 GCancellable *cancellable,
422 GAsyncReadyCallback callback,
427 g_return_if_fail (G_IS_VOLUME (volume));
429 iface = G_VOLUME_GET_IFACE (volume);
431 if (iface->eject == NULL)
433 g_task_report_new_error (volume, callback, user_data,
434 g_volume_eject_with_operation,
435 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
436 _("volume doesn’t implement eject"));
440 (* iface->eject) (volume, flags, cancellable, callback, user_data);
444 * g_volume_eject_finish:
445 * @volume: pointer to a #GVolume
446 * @result: a #GAsyncResult
447 * @error: a #GError location to store an error, or %NULL to ignore
449 * Finishes ejecting a volume. If any errors occurred during the operation,
450 * @error will be set to contain the errors and %FALSE will be returned.
452 * Returns: %TRUE, %FALSE if operation failed
454 * Deprecated: 2.22: Use g_volume_eject_with_operation_finish() instead.
457 g_volume_eject_finish (GVolume *volume,
458 GAsyncResult *result,
463 g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
464 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
466 if (g_async_result_legacy_propagate_error (result, error))
468 if (g_async_result_is_tagged (result, g_volume_eject_with_operation))
469 return g_task_propagate_boolean (G_TASK (result), error);
471 iface = G_VOLUME_GET_IFACE (volume);
472 return (* iface->eject_finish) (volume, result, error);
476 * g_volume_eject_with_operation:
477 * @volume: a #GVolume
478 * @flags: flags affecting the unmount if required for eject
479 * @mount_operation: (nullable): a #GMountOperation or %NULL to
480 * avoid user interaction
481 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore
482 * @callback: (nullable): a #GAsyncReadyCallback, or %NULL
483 * @user_data: user data passed to @callback
485 * Ejects a volume. This is an asynchronous operation, and is
486 * finished by calling g_volume_eject_with_operation_finish() with the @volume
487 * and #GAsyncResult data returned in the @callback.
492 g_volume_eject_with_operation (GVolume *volume,
493 GMountUnmountFlags flags,
494 GMountOperation *mount_operation,
495 GCancellable *cancellable,
496 GAsyncReadyCallback callback,
501 g_return_if_fail (G_IS_VOLUME (volume));
503 iface = G_VOLUME_GET_IFACE (volume);
505 if (iface->eject == NULL && iface->eject_with_operation == NULL)
507 g_task_report_new_error (volume, callback, user_data,
508 g_volume_eject_with_operation,
509 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
510 /* Translators: This is an error
511 * message for volume objects that
512 * don't implement any of eject or eject_with_operation. */
513 _("volume doesn’t implement eject or eject_with_operation"));
517 if (iface->eject_with_operation != NULL)
518 (* iface->eject_with_operation) (volume, flags, mount_operation, cancellable, callback, user_data);
520 (* iface->eject) (volume, flags, cancellable, callback, user_data);
524 * g_volume_eject_with_operation_finish:
525 * @volume: a #GVolume
526 * @result: a #GAsyncResult
527 * @error: a #GError location to store the error occurring, or %NULL
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))
548 else if (g_async_result_is_tagged (result, g_volume_eject_with_operation))
549 return g_task_propagate_boolean (G_TASK (result), error);
551 iface = G_VOLUME_GET_IFACE (volume);
552 if (iface->eject_with_operation_finish != NULL)
553 return (* iface->eject_with_operation_finish) (volume, result, error);
555 return (* iface->eject_finish) (volume, result, error);
559 * g_volume_get_identifier:
560 * @volume: a #GVolume
561 * @kind: the kind of identifier to return
563 * Gets the identifier of the given kind for @volume.
564 * See the [introduction](#volume-identifiers) for more
565 * information about volume identifiers.
567 * Returns: (nullable) (transfer full): a newly allocated string containing the
568 * requested identifier, or %NULL if the #GVolume
569 * doesn't have this kind of identifier
572 g_volume_get_identifier (GVolume *volume,
577 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
578 g_return_val_if_fail (kind != NULL, NULL);
580 iface = G_VOLUME_GET_IFACE (volume);
582 if (iface->get_identifier == NULL)
585 return (* iface->get_identifier) (volume, kind);
589 * g_volume_enumerate_identifiers:
590 * @volume: a #GVolume
592 * Gets the kinds of [identifiers](#volume-identifiers) that @volume has.
593 * Use g_volume_get_identifier() to obtain the identifiers themselves.
595 * Returns: (array zero-terminated=1) (transfer full): a %NULL-terminated array
596 * of strings containing kinds of identifiers. Use g_strfreev() to free.
599 g_volume_enumerate_identifiers (GVolume *volume)
603 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
604 iface = G_VOLUME_GET_IFACE (volume);
606 if (iface->enumerate_identifiers == NULL)
609 return (* iface->enumerate_identifiers) (volume);
613 * g_volume_get_activation_root:
614 * @volume: a #GVolume
616 * Gets the activation root for a #GVolume if it is known ahead of
617 * mount time. Returns %NULL otherwise. If not %NULL and if @volume
618 * is mounted, then the result of g_mount_get_root() on the
619 * #GMount object obtained from g_volume_get_mount() will always
620 * either be equal or a prefix of what this function returns. In
621 * other words, in code
623 * |[<!-- language="C" -->
626 * GFile *volume_activation_root;
628 * mount = g_volume_get_mount (volume); // mounted, so never NULL
629 * mount_root = g_mount_get_root (mount);
630 * volume_activation_root = g_volume_get_activation_root (volume); // assume not NULL
632 * then the expression
633 * |[<!-- language="C" -->
634 * (g_file_has_prefix (volume_activation_root, mount_root) ||
635 * g_file_equal (volume_activation_root, mount_root))
637 * will always be %TRUE.
639 * Activation roots are typically used in #GVolumeMonitor
640 * implementations to find the underlying mount to shadow, see
641 * g_mount_is_shadowed() for more details.
643 * Returns: (nullable) (transfer full): the activation root of @volume
644 * or %NULL. Use g_object_unref() to free.
649 g_volume_get_activation_root (GVolume *volume)
653 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
654 iface = G_VOLUME_GET_IFACE (volume);
656 if (iface->get_activation_root == NULL)
659 return (* iface->get_activation_root) (volume);
663 * g_volume_get_sort_key:
664 * @volume: a #GVolume
666 * Gets the sort key for @volume, if any.
668 * Returns: (nullable): Sorting key for @volume or %NULL if no such key is available
673 g_volume_get_sort_key (GVolume *volume)
675 const gchar *ret = NULL;
678 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
680 iface = G_VOLUME_GET_IFACE (volume);
681 if (iface->get_sort_key != NULL)
682 ret = iface->get_sort_key (volume);