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, 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 * <para id="volume-identifier">
61 * It is sometimes necessary to directly access the underlying
62 * operating system object behind a volume (e.g. for passing a volume
63 * to an application via the commandline). For this purpose, GIO
64 * allows to obtain an 'identifier' for the volume. There can be
65 * different kinds of identifiers, such as Hal UDIs, filesystem labels,
66 * traditional Unix devices (e.g. `/dev/sda2`), UUIDs. GIO uses predefined
67 * strings as names for the different kinds of identifiers:
68 * #G_VOLUME_IDENTIFIER_KIND_HAL_UDI, #G_VOLUME_IDENTIFIER_KIND_LABEL, etc.
69 * Use g_volume_get_identifier() to obtain an identifier for a volume.
72 * Note that #G_VOLUME_IDENTIFIER_KIND_HAL_UDI will only be available
73 * when the gvfs hal volume monitor is in use. Other volume monitors
74 * will generally be able to provide the #G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE
75 * identifier, which can be used to obtain a hal device by means of
76 * libhal_manager_find_device_string_match().
79 typedef GVolumeIface GVolumeInterface;
80 G_DEFINE_INTERFACE(GVolume, g_volume, G_TYPE_OBJECT)
83 g_volume_default_init (GVolumeInterface *iface)
88 * Emitted when the volume has been changed.
90 g_signal_new (I_("changed"),
93 G_STRUCT_OFFSET (GVolumeIface, changed),
95 g_cclosure_marshal_VOID__VOID,
101 * This signal is emitted when the #GVolume have been removed. If
102 * the recipient is holding references to the object they should
103 * release them so the object can be finalized.
105 g_signal_new (I_("removed"),
108 G_STRUCT_OFFSET (GVolumeIface, removed),
110 g_cclosure_marshal_VOID__VOID,
116 * @volume: a #GVolume
118 * Gets the name of @volume.
120 * Returns: the name for the given @volume. The returned string should
121 * be freed with g_free() when no longer needed.
124 g_volume_get_name (GVolume *volume)
128 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
130 iface = G_VOLUME_GET_IFACE (volume);
132 return (* iface->get_name) (volume);
137 * @volume: a #GVolume
139 * Gets the icon for @volume.
141 * Returns: (transfer full): a #GIcon.
142 * The returned object should be unreffed with g_object_unref()
143 * when no longer needed.
146 g_volume_get_icon (GVolume *volume)
150 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
152 iface = G_VOLUME_GET_IFACE (volume);
154 return (* iface->get_icon) (volume);
158 * g_volume_get_symbolic_icon:
159 * @volume: a #GVolume
161 * Gets the symbolic icon for @volume.
163 * Returns: (transfer full): a #GIcon.
164 * The returned object should be unreffed with g_object_unref()
165 * when no longer needed.
170 g_volume_get_symbolic_icon (GVolume *volume)
175 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
177 iface = G_VOLUME_GET_IFACE (volume);
179 if (iface->get_symbolic_icon != NULL)
180 ret = iface->get_symbolic_icon (volume);
182 ret = g_themed_icon_new_with_default_fallbacks ("folder-remote-symbolic");
190 * @volume: a #GVolume
192 * Gets the UUID for the @volume. The reference is typically based on
193 * the file system UUID for the volume in question and should be
194 * considered an opaque string. Returns %NULL if there is no UUID
197 * Returns: the UUID for @volume or %NULL if no UUID can be computed.
198 * The returned string should be freed with g_free()
199 * when no longer needed.
202 g_volume_get_uuid (GVolume *volume)
206 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
208 iface = G_VOLUME_GET_IFACE (volume);
210 return (* iface->get_uuid) (volume);
214 * g_volume_get_drive:
215 * @volume: a #GVolume
217 * Gets the drive for the @volume.
219 * Returns: (transfer full): a #GDrive or %NULL if @volume is not
220 * associated with a drive. The returned object should be unreffed
221 * with g_object_unref() when no longer needed.
224 g_volume_get_drive (GVolume *volume)
228 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
230 iface = G_VOLUME_GET_IFACE (volume);
232 return (* iface->get_drive) (volume);
236 * g_volume_get_mount:
237 * @volume: a #GVolume
239 * Gets the mount for the @volume.
241 * Returns: (transfer full): a #GMount or %NULL if @volume isn't mounted.
242 * The returned object should be unreffed with g_object_unref()
243 * when no longer needed.
246 g_volume_get_mount (GVolume *volume)
250 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
252 iface = G_VOLUME_GET_IFACE (volume);
254 return (* iface->get_mount) (volume);
259 * g_volume_can_mount:
260 * @volume: a #GVolume
262 * Checks if a volume can be mounted.
264 * Returns: %TRUE if the @volume can be mounted. %FALSE otherwise
267 g_volume_can_mount (GVolume *volume)
271 g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
273 iface = G_VOLUME_GET_IFACE (volume);
275 if (iface->can_mount == NULL)
278 return (* iface->can_mount) (volume);
282 * g_volume_can_eject:
283 * @volume: a #GVolume
285 * Checks if a volume can be ejected.
287 * Returns: %TRUE if the @volume can be ejected. %FALSE otherwise
290 g_volume_can_eject (GVolume *volume)
294 g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
296 iface = G_VOLUME_GET_IFACE (volume);
298 if (iface->can_eject == NULL)
301 return (* iface->can_eject) (volume);
305 * g_volume_should_automount:
306 * @volume: a #GVolume
308 * Returns whether the volume should be automatically mounted.
310 * Returns: %TRUE if the volume should be automatically mounted
313 g_volume_should_automount (GVolume *volume)
317 g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
319 iface = G_VOLUME_GET_IFACE (volume);
321 if (iface->should_automount == NULL)
324 return (* iface->should_automount) (volume);
330 * @volume: a #GVolume
331 * @flags: flags affecting the operation
332 * @mount_operation: (allow-none): a #GMountOperation or %NULL to avoid user interaction
333 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
334 * @callback: (allow-none): a #GAsyncReadyCallback, or %NULL
335 * @user_data: user data that gets passed to @callback
337 * Mounts a volume. This is an asynchronous operation, and is
338 * finished by calling g_volume_mount_finish() with the @volume
339 * 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: (allow-none): optional #GCancellable object, %NULL to ignore
409 * @callback: (allow-none): 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: (allow-none): a #GMountOperation or %NULL to
480 * avoid user interaction
481 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
482 * @callback: (allow-none): 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 <link linkend="volume-identifier">introduction</link>
565 * for more information about volume identifiers.
567 * Returns: a newly allocated string containing the
568 * requested identfier, 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 <link linkend="volume-identifier">identifiers</link>
593 * that @volume has. Use g_volume_get_identifier() to obtain
594 * the identifiers themselves.
596 * Returns: (array zero-terminated=1) (transfer full): a %NULL-terminated array
597 * of strings containing kinds of identifiers. Use g_strfreev() to free.
600 g_volume_enumerate_identifiers (GVolume *volume)
604 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
605 iface = G_VOLUME_GET_IFACE (volume);
607 if (iface->enumerate_identifiers == NULL)
610 return (* iface->enumerate_identifiers) (volume);
614 * g_volume_get_activation_root:
615 * @volume: a #GVolume
617 * Gets the activation root for a #GVolume if it is known ahead of
618 * mount time. Returns %NULL otherwise. If not %NULL and if @volume
619 * is mounted, then the result of g_mount_get_root() on the
620 * #GMount object obtained from g_volume_get_mount() will always
621 * either be equal or a prefix of what this function returns. In
622 * other words, in code
624 * |[<!-- language="C" -->
627 * GFile *volume_activation_root;
629 * mount = g_volume_get_mount (volume); /* mounted, so never NULL */
630 * mount_root = g_mount_get_root (mount);
631 * volume_activation_root = g_volume_get_activation_root (volume); /* assume not NULL */
633 * then the expression
634 * |[<!-- language="C" -->
635 * (g_file_has_prefix (volume_activation_root, mount_root) ||
636 g_file_equal (volume_activation_root, mount_root))
638 * will always be %TRUE.
640 * Activation roots are typically used in #GVolumeMonitor
641 * implementations to find the underlying mount to shadow, see
642 * g_mount_is_shadowed() for more details.
644 * Returns: (transfer full): the activation root of @volume or %NULL. Use
645 * g_object_unref() to free.
650 g_volume_get_activation_root (GVolume *volume)
654 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
655 iface = G_VOLUME_GET_IFACE (volume);
657 if (iface->get_activation_root == NULL)
660 return (* iface->get_activation_root) (volume);
664 * g_volume_get_sort_key:
665 * @volume: a #GVolume
667 * Gets the sort key for @volume, if any.
669 * Returns: Sorting key for @volume or %NULL if no such key is available
674 g_volume_get_sort_key (GVolume *volume)
676 const gchar *ret = NULL;
679 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
681 iface = G_VOLUME_GET_IFACE (volume);
682 if (iface->get_sort_key != NULL)
683 ret = iface->get_sort_key (volume);