Merge branch '1175-source-names' into 'master'
[glib.git] / gio / gvolume.c
blobd3bd3b288aea6159c5382ac8cd8b314984bc59a9
1 /* GIO - GLib Input, Output and Streaming Library
2 *
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>
22 #include "config.h"
23 #include "gmount.h"
24 #include "gvolume.h"
25 #include "gthemedicon.h"
26 #include "gasyncresult.h"
27 #include "gtask.h"
28 #include "gioerror.h"
29 #include "glibintl.h"
32 /**
33 * SECTION:gvolume
34 * @short_description: Volume management
35 * @include: gio/gio.h
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
50 * for credentials.
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)
83 static void
84 g_volume_default_init (GVolumeInterface *iface)
86 /**
87 * GVolume::changed:
89 * Emitted when the volume has been changed.
91 g_signal_new (I_("changed"),
92 G_TYPE_VOLUME,
93 G_SIGNAL_RUN_LAST,
94 G_STRUCT_OFFSET (GVolumeIface, changed),
95 NULL, NULL,
96 g_cclosure_marshal_VOID__VOID,
97 G_TYPE_NONE, 0);
99 /**
100 * GVolume::removed:
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"),
107 G_TYPE_VOLUME,
108 G_SIGNAL_RUN_LAST,
109 G_STRUCT_OFFSET (GVolumeIface, removed),
110 NULL, NULL,
111 g_cclosure_marshal_VOID__VOID,
112 G_TYPE_NONE, 0);
116 * g_volume_get_name:
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.
124 char *
125 g_volume_get_name (GVolume *volume)
127 GVolumeIface *iface;
129 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
131 iface = G_VOLUME_GET_IFACE (volume);
133 return (* iface->get_name) (volume);
137 * g_volume_get_icon:
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.
146 GIcon *
147 g_volume_get_icon (GVolume *volume)
149 GVolumeIface *iface;
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.
168 * Since: 2.34
170 GIcon *
171 g_volume_get_symbolic_icon (GVolume *volume)
173 GVolumeIface *iface;
174 GIcon *ret;
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);
182 else
183 ret = g_themed_icon_new_with_default_fallbacks ("folder-remote-symbolic");
185 return ret;
190 * g_volume_get_uuid:
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
196 * available.
198 * Returns: (nullable) (transfer full): the UUID for @volume or %NULL if no UUID
199 * can be computed.
200 * The returned string should be freed with g_free()
201 * when no longer needed.
203 char *
204 g_volume_get_uuid (GVolume *volume)
206 GVolumeIface *iface;
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.
225 GDrive *
226 g_volume_get_drive (GVolume *volume)
228 GVolumeIface *iface;
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.
247 GMount *
248 g_volume_get_mount (GVolume *volume)
250 GVolumeIface *iface;
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
268 gboolean
269 g_volume_can_mount (GVolume *volume)
271 GVolumeIface *iface;
273 g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
275 iface = G_VOLUME_GET_IFACE (volume);
277 if (iface->can_mount == NULL)
278 return FALSE;
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
291 gboolean
292 g_volume_can_eject (GVolume *volume)
294 GVolumeIface *iface;
296 g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
298 iface = G_VOLUME_GET_IFACE (volume);
300 if (iface->can_eject == NULL)
301 return FALSE;
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
314 gboolean
315 g_volume_should_automount (GVolume *volume)
317 GVolumeIface *iface;
319 g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
321 iface = G_VOLUME_GET_IFACE (volume);
323 if (iface->should_automount == NULL)
324 return FALSE;
326 return (* iface->should_automount) (volume);
331 * g_volume_mount:
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.
343 * Virtual: mount_fn
345 void
346 g_volume_mount (GVolume *volume,
347 GMountMountFlags flags,
348 GMountOperation *mount_operation,
349 GCancellable *cancellable,
350 GAsyncReadyCallback callback,
351 gpointer user_data)
353 GVolumeIface *iface;
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,
362 g_volume_mount,
363 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
364 _("volume doesn’t implement mount"));
365 return;
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
383 * #GVolumeMonitor.
385 * Returns: %TRUE, %FALSE if operation failed
387 gboolean
388 g_volume_mount_finish (GVolume *volume,
389 GAsyncResult *result,
390 GError **error)
392 GVolumeIface *iface;
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))
398 return FALSE;
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);
407 * g_volume_eject:
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.
420 void
421 g_volume_eject (GVolume *volume,
422 GMountUnmountFlags flags,
423 GCancellable *cancellable,
424 GAsyncReadyCallback callback,
425 gpointer user_data)
427 GVolumeIface *iface;
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"));
439 return;
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.
458 gboolean
459 g_volume_eject_finish (GVolume *volume,
460 GAsyncResult *result,
461 GError **error)
463 GVolumeIface *iface;
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))
469 return FALSE;
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.
491 * Since: 2.22
493 void
494 g_volume_eject_with_operation (GVolume *volume,
495 GMountUnmountFlags flags,
496 GMountOperation *mount_operation,
497 GCancellable *cancellable,
498 GAsyncReadyCallback callback,
499 gpointer user_data)
501 GVolumeIface *iface;
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"));
516 return;
519 if (iface->eject_with_operation != NULL)
520 (* iface->eject_with_operation) (volume, flags, mount_operation, cancellable, callback, user_data);
521 else
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
536 * Since: 2.22
538 gboolean
539 g_volume_eject_with_operation_finish (GVolume *volume,
540 GAsyncResult *result,
541 GError **error)
543 GVolumeIface *iface;
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))
549 return FALSE;
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);
556 else
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
573 char *
574 g_volume_get_identifier (GVolume *volume,
575 const char *kind)
577 GVolumeIface *iface;
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)
585 return 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.
600 char **
601 g_volume_enumerate_identifiers (GVolume *volume)
603 GVolumeIface *iface;
605 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
606 iface = G_VOLUME_GET_IFACE (volume);
608 if (iface->enumerate_identifiers == NULL)
609 return 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" -->
626 * GMount *mount;
627 * GFile *mount_root
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
633 * ]|
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))
638 * ]|
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.
648 * Since: 2.18
650 GFile *
651 g_volume_get_activation_root (GVolume *volume)
653 GVolumeIface *iface;
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)
659 return 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
672 * Since: 2.32
674 const gchar *
675 g_volume_get_sort_key (GVolume *volume)
677 const gchar *ret = NULL;
678 GVolumeIface *iface;
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);
686 return ret;