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.1 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, see <http://www.gnu.org/licenses/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
19 * David Zeuthen <davidz@redhat.com>
25 #include "gthemedicon.h"
26 #include "gasyncresult.h"
34 * @short_description: Volume management
37 * The #GVolume interface represents user-visible objects that can be
38 * mounted. Note, when porting from GnomeVFS, #GVolume is the moral
39 * equivalent of #GnomeVFSDrive.
41 * Mounting a #GVolume instance is an asynchronous operation. For more
42 * information about asynchronous operations, see #GAsyncResult and
43 * #GTask. To mount a #GVolume, first call g_volume_mount() with (at
44 * least) the #GVolume instance, optionally a #GMountOperation object
45 * and a #GAsyncReadyCallback.
47 * Typically, one will only want to pass %NULL for the
48 * #GMountOperation 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 #GAsyncReady structure will be
54 * passed to the callback. That callback should then call
55 * g_volume_mount_finish() with the #GVolume instance and the
56 * #GAsyncReady data to see if the operation was completed
57 * successfully. If an @error is present when g_volume_mount_finish()
58 * is called, then it will be filled with any error information.
60 * ## Volume Identifiers # {#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. `/dev/sda2`), UUIDs. GIO uses predefined
68 * strings as names for the different kinds of identifiers:
69 * #G_VOLUME_IDENTIFIER_KIND_UUID, #G_VOLUME_IDENTIFIER_KIND_LABEL, etc.
70 * Use g_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
),
96 g_cclosure_marshal_VOID__VOID
,
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
),
111 g_cclosure_marshal_VOID__VOID
,
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
);
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.
346 g_volume_mount (GVolume
*volume
,
347 GMountMountFlags flags
,
348 GMountOperation
*mount_operation
,
349 GCancellable
*cancellable
,
350 GAsyncReadyCallback callback
,
355 g_return_if_fail (G_IS_VOLUME (volume
));
357 iface
= G_VOLUME_GET_IFACE (volume
);
359 if (iface
->mount_fn
== NULL
)
361 g_task_report_new_error (volume
, callback
, user_data
,
363 G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
364 _("volume doesn’t implement mount"));
368 (* iface
->mount_fn
) (volume
, flags
, mount_operation
, cancellable
, callback
, user_data
);
372 * g_volume_mount_finish:
373 * @volume: a #GVolume
374 * @result: a #GAsyncResult
375 * @error: a #GError location to store an error, or %NULL to ignore
377 * Finishes mounting a volume. If any errors occurred during the operation,
378 * @error will be set to contain the errors and %FALSE will be returned.
380 * If the mount operation succeeded, g_volume_get_mount() on @volume
381 * is guaranteed to return the mount right after calling this
382 * function; there's no need to listen for the 'mount-added' signal on
385 * Returns: %TRUE, %FALSE if operation failed
388 g_volume_mount_finish (GVolume
*volume
,
389 GAsyncResult
*result
,
394 g_return_val_if_fail (G_IS_VOLUME (volume
), FALSE
);
395 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), FALSE
);
397 if (g_async_result_legacy_propagate_error (result
, error
))
399 else if (g_async_result_is_tagged (result
, g_volume_mount
))
400 return g_task_propagate_boolean (G_TASK (result
), error
);
402 iface
= G_VOLUME_GET_IFACE (volume
);
403 return (* iface
->mount_finish
) (volume
, result
, error
);
408 * @volume: a #GVolume
409 * @flags: flags affecting the unmount if required for eject
410 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore
411 * @callback: (nullable): a #GAsyncReadyCallback, or %NULL
412 * @user_data: user data that gets passed to @callback
414 * Ejects a volume. This is an asynchronous operation, and is
415 * finished by calling g_volume_eject_finish() with the @volume
416 * and #GAsyncResult returned in the @callback.
418 * Deprecated: 2.22: Use g_volume_eject_with_operation() instead.
421 g_volume_eject (GVolume
*volume
,
422 GMountUnmountFlags flags
,
423 GCancellable
*cancellable
,
424 GAsyncReadyCallback callback
,
429 g_return_if_fail (G_IS_VOLUME (volume
));
431 iface
= G_VOLUME_GET_IFACE (volume
);
433 if (iface
->eject
== NULL
)
435 g_task_report_new_error (volume
, callback
, user_data
,
436 g_volume_eject_with_operation
,
437 G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
438 _("volume doesn’t implement eject"));
442 (* iface
->eject
) (volume
, flags
, cancellable
, callback
, user_data
);
446 * g_volume_eject_finish:
447 * @volume: pointer to a #GVolume
448 * @result: a #GAsyncResult
449 * @error: a #GError location to store an error, or %NULL to ignore
451 * Finishes ejecting a volume. If any errors occurred during the operation,
452 * @error will be set to contain the errors and %FALSE will be returned.
454 * Returns: %TRUE, %FALSE if operation failed
456 * Deprecated: 2.22: Use g_volume_eject_with_operation_finish() instead.
459 g_volume_eject_finish (GVolume
*volume
,
460 GAsyncResult
*result
,
465 g_return_val_if_fail (G_IS_VOLUME (volume
), FALSE
);
466 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), FALSE
);
468 if (g_async_result_legacy_propagate_error (result
, error
))
470 if (g_async_result_is_tagged (result
, g_volume_eject_with_operation
))
471 return g_task_propagate_boolean (G_TASK (result
), error
);
473 iface
= G_VOLUME_GET_IFACE (volume
);
474 return (* iface
->eject_finish
) (volume
, result
, error
);
478 * g_volume_eject_with_operation:
479 * @volume: a #GVolume
480 * @flags: flags affecting the unmount if required for eject
481 * @mount_operation: (nullable): a #GMountOperation or %NULL to
482 * avoid user interaction
483 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore
484 * @callback: (nullable): a #GAsyncReadyCallback, or %NULL
485 * @user_data: user data passed to @callback
487 * Ejects a volume. This is an asynchronous operation, and is
488 * finished by calling g_volume_eject_with_operation_finish() with the @volume
489 * and #GAsyncResult data returned in the @callback.
494 g_volume_eject_with_operation (GVolume
*volume
,
495 GMountUnmountFlags flags
,
496 GMountOperation
*mount_operation
,
497 GCancellable
*cancellable
,
498 GAsyncReadyCallback callback
,
503 g_return_if_fail (G_IS_VOLUME (volume
));
505 iface
= G_VOLUME_GET_IFACE (volume
);
507 if (iface
->eject
== NULL
&& iface
->eject_with_operation
== NULL
)
509 g_task_report_new_error (volume
, callback
, user_data
,
510 g_volume_eject_with_operation
,
511 G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
512 /* Translators: This is an error
513 * message for volume objects that
514 * don't implement any of eject or eject_with_operation. */
515 _("volume doesn’t implement eject or eject_with_operation"));
519 if (iface
->eject_with_operation
!= NULL
)
520 (* iface
->eject_with_operation
) (volume
, flags
, mount_operation
, cancellable
, callback
, user_data
);
522 (* iface
->eject
) (volume
, flags
, cancellable
, callback
, user_data
);
526 * g_volume_eject_with_operation_finish:
527 * @volume: a #GVolume
528 * @result: a #GAsyncResult
529 * @error: a #GError location to store the error occurring, or %NULL
531 * Finishes ejecting a volume. If any errors occurred during the operation,
532 * @error will be set to contain the errors and %FALSE will be returned.
534 * Returns: %TRUE if the volume was successfully ejected. %FALSE otherwise
539 g_volume_eject_with_operation_finish (GVolume
*volume
,
540 GAsyncResult
*result
,
545 g_return_val_if_fail (G_IS_VOLUME (volume
), FALSE
);
546 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), FALSE
);
548 if (g_async_result_legacy_propagate_error (result
, error
))
550 else if (g_async_result_is_tagged (result
, g_volume_eject_with_operation
))
551 return g_task_propagate_boolean (G_TASK (result
), error
);
553 iface
= G_VOLUME_GET_IFACE (volume
);
554 if (iface
->eject_with_operation_finish
!= NULL
)
555 return (* iface
->eject_with_operation_finish
) (volume
, result
, error
);
557 return (* iface
->eject_finish
) (volume
, result
, error
);
561 * g_volume_get_identifier:
562 * @volume: a #GVolume
563 * @kind: the kind of identifier to return
565 * Gets the identifier of the given kind for @volume.
566 * See the [introduction][volume-identifier] for more
567 * information about volume identifiers.
569 * Returns: (nullable) (transfer full): a newly allocated string containing the
570 * requested identifier, or %NULL if the #GVolume
571 * doesn't have this kind of identifier
574 g_volume_get_identifier (GVolume
*volume
,
579 g_return_val_if_fail (G_IS_VOLUME (volume
), NULL
);
580 g_return_val_if_fail (kind
!= NULL
, NULL
);
582 iface
= G_VOLUME_GET_IFACE (volume
);
584 if (iface
->get_identifier
== NULL
)
587 return (* iface
->get_identifier
) (volume
, kind
);
591 * g_volume_enumerate_identifiers:
592 * @volume: a #GVolume
594 * Gets the kinds of [identifiers][volume-identifier] that @volume has.
595 * Use g_volume_get_identifier() to obtain the identifiers themselves.
597 * Returns: (array zero-terminated=1) (transfer full): a %NULL-terminated array
598 * of strings containing kinds of identifiers. Use g_strfreev() to free.
601 g_volume_enumerate_identifiers (GVolume
*volume
)
605 g_return_val_if_fail (G_IS_VOLUME (volume
), NULL
);
606 iface
= G_VOLUME_GET_IFACE (volume
);
608 if (iface
->enumerate_identifiers
== NULL
)
611 return (* iface
->enumerate_identifiers
) (volume
);
615 * g_volume_get_activation_root:
616 * @volume: a #GVolume
618 * Gets the activation root for a #GVolume if it is known ahead of
619 * mount time. Returns %NULL otherwise. If not %NULL and if @volume
620 * is mounted, then the result of g_mount_get_root() on the
621 * #GMount object obtained from g_volume_get_mount() will always
622 * either be equal or a prefix of what this function returns. In
623 * other words, in code
625 * |[<!-- language="C" -->
628 * GFile *volume_activation_root;
630 * mount = g_volume_get_mount (volume); // mounted, so never NULL
631 * mount_root = g_mount_get_root (mount);
632 * volume_activation_root = g_volume_get_activation_root (volume); // assume not NULL
634 * then the expression
635 * |[<!-- language="C" -->
636 * (g_file_has_prefix (volume_activation_root, mount_root) ||
637 * 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: (nullable) (transfer full): the activation root of @volume
646 * or %NULL. Use 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: (nullable): 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
);