Add some more cases to the app-id unit tests
[glib.git] / gio / gvolume.c
blob3f08ebc1d646e093a6f1f4f3989ee115e5b0320a
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 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_HAL_UDI, #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: the UUID for @volume or %NULL if no UUID can be computed.
199 * The returned string should be freed with g_free()
200 * when no longer needed.
202 char *
203 g_volume_get_uuid (GVolume *volume)
205 GVolumeIface *iface;
207 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
209 iface = G_VOLUME_GET_IFACE (volume);
211 return (* iface->get_uuid) (volume);
215 * g_volume_get_drive:
216 * @volume: a #GVolume
218 * Gets the drive for the @volume.
220 * Returns: (transfer full): a #GDrive or %NULL if @volume is not
221 * associated with a drive. The returned object should be unreffed
222 * with g_object_unref() when no longer needed.
224 GDrive *
225 g_volume_get_drive (GVolume *volume)
227 GVolumeIface *iface;
229 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
231 iface = G_VOLUME_GET_IFACE (volume);
233 return (* iface->get_drive) (volume);
237 * g_volume_get_mount:
238 * @volume: a #GVolume
240 * Gets the mount for the @volume.
242 * Returns: (transfer full): a #GMount or %NULL if @volume isn't mounted.
243 * The returned object should be unreffed with g_object_unref()
244 * when no longer needed.
246 GMount *
247 g_volume_get_mount (GVolume *volume)
249 GVolumeIface *iface;
251 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
253 iface = G_VOLUME_GET_IFACE (volume);
255 return (* iface->get_mount) (volume);
260 * g_volume_can_mount:
261 * @volume: a #GVolume
263 * Checks if a volume can be mounted.
265 * Returns: %TRUE if the @volume can be mounted. %FALSE otherwise
267 gboolean
268 g_volume_can_mount (GVolume *volume)
270 GVolumeIface *iface;
272 g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
274 iface = G_VOLUME_GET_IFACE (volume);
276 if (iface->can_mount == NULL)
277 return FALSE;
279 return (* iface->can_mount) (volume);
283 * g_volume_can_eject:
284 * @volume: a #GVolume
286 * Checks if a volume can be ejected.
288 * Returns: %TRUE if the @volume can be ejected. %FALSE otherwise
290 gboolean
291 g_volume_can_eject (GVolume *volume)
293 GVolumeIface *iface;
295 g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
297 iface = G_VOLUME_GET_IFACE (volume);
299 if (iface->can_eject == NULL)
300 return FALSE;
302 return (* iface->can_eject) (volume);
306 * g_volume_should_automount:
307 * @volume: a #GVolume
309 * Returns whether the volume should be automatically mounted.
311 * Returns: %TRUE if the volume should be automatically mounted
313 gboolean
314 g_volume_should_automount (GVolume *volume)
316 GVolumeIface *iface;
318 g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
320 iface = G_VOLUME_GET_IFACE (volume);
322 if (iface->should_automount == NULL)
323 return FALSE;
325 return (* iface->should_automount) (volume);
330 * g_volume_mount:
331 * @volume: a #GVolume
332 * @flags: flags affecting the operation
333 * @mount_operation: (nullable): a #GMountOperation or %NULL to avoid user interaction
334 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore
335 * @callback: (nullable): a #GAsyncReadyCallback, or %NULL
336 * @user_data: user data that gets passed to @callback
338 * Mounts a volume. This is an asynchronous operation, and is
339 * finished by calling g_volume_mount_finish() with the @volume
340 * and #GAsyncResult returned in the @callback.
342 * Virtual: mount_fn
344 void
345 g_volume_mount (GVolume *volume,
346 GMountMountFlags flags,
347 GMountOperation *mount_operation,
348 GCancellable *cancellable,
349 GAsyncReadyCallback callback,
350 gpointer user_data)
352 GVolumeIface *iface;
354 g_return_if_fail (G_IS_VOLUME (volume));
356 iface = G_VOLUME_GET_IFACE (volume);
358 if (iface->mount_fn == NULL)
360 g_task_report_new_error (volume, callback, user_data,
361 g_volume_mount,
362 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
363 _("volume doesn’t implement mount"));
364 return;
367 (* iface->mount_fn) (volume, flags, mount_operation, cancellable, callback, user_data);
371 * g_volume_mount_finish:
372 * @volume: a #GVolume
373 * @result: a #GAsyncResult
374 * @error: a #GError location to store an error, or %NULL to ignore
376 * Finishes mounting a volume. If any errors occurred during the operation,
377 * @error will be set to contain the errors and %FALSE will be returned.
379 * If the mount operation succeeded, g_volume_get_mount() on @volume
380 * is guaranteed to return the mount right after calling this
381 * function; there's no need to listen for the 'mount-added' signal on
382 * #GVolumeMonitor.
384 * Returns: %TRUE, %FALSE if operation failed
386 gboolean
387 g_volume_mount_finish (GVolume *volume,
388 GAsyncResult *result,
389 GError **error)
391 GVolumeIface *iface;
393 g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
394 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
396 if (g_async_result_legacy_propagate_error (result, error))
397 return FALSE;
398 else if (g_async_result_is_tagged (result, g_volume_mount))
399 return g_task_propagate_boolean (G_TASK (result), error);
401 iface = G_VOLUME_GET_IFACE (volume);
402 return (* iface->mount_finish) (volume, result, error);
406 * g_volume_eject:
407 * @volume: a #GVolume
408 * @flags: flags affecting the unmount if required for eject
409 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore
410 * @callback: (nullable): a #GAsyncReadyCallback, or %NULL
411 * @user_data: user data that gets passed to @callback
413 * Ejects a volume. This is an asynchronous operation, and is
414 * finished by calling g_volume_eject_finish() with the @volume
415 * and #GAsyncResult returned in the @callback.
417 * Deprecated: 2.22: Use g_volume_eject_with_operation() instead.
419 void
420 g_volume_eject (GVolume *volume,
421 GMountUnmountFlags flags,
422 GCancellable *cancellable,
423 GAsyncReadyCallback callback,
424 gpointer user_data)
426 GVolumeIface *iface;
428 g_return_if_fail (G_IS_VOLUME (volume));
430 iface = G_VOLUME_GET_IFACE (volume);
432 if (iface->eject == NULL)
434 g_task_report_new_error (volume, callback, user_data,
435 g_volume_eject_with_operation,
436 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
437 _("volume doesn’t implement eject"));
438 return;
441 (* iface->eject) (volume, flags, cancellable, callback, user_data);
445 * g_volume_eject_finish:
446 * @volume: pointer to a #GVolume
447 * @result: a #GAsyncResult
448 * @error: a #GError location to store an error, or %NULL to ignore
450 * Finishes ejecting a volume. If any errors occurred during the operation,
451 * @error will be set to contain the errors and %FALSE will be returned.
453 * Returns: %TRUE, %FALSE if operation failed
455 * Deprecated: 2.22: Use g_volume_eject_with_operation_finish() instead.
457 gboolean
458 g_volume_eject_finish (GVolume *volume,
459 GAsyncResult *result,
460 GError **error)
462 GVolumeIface *iface;
464 g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
465 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
467 if (g_async_result_legacy_propagate_error (result, error))
468 return FALSE;
469 if (g_async_result_is_tagged (result, g_volume_eject_with_operation))
470 return g_task_propagate_boolean (G_TASK (result), error);
472 iface = G_VOLUME_GET_IFACE (volume);
473 return (* iface->eject_finish) (volume, result, error);
477 * g_volume_eject_with_operation:
478 * @volume: a #GVolume
479 * @flags: flags affecting the unmount if required for eject
480 * @mount_operation: (nullable): a #GMountOperation or %NULL to
481 * avoid user interaction
482 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore
483 * @callback: (nullable): a #GAsyncReadyCallback, or %NULL
484 * @user_data: user data passed to @callback
486 * Ejects a volume. This is an asynchronous operation, and is
487 * finished by calling g_volume_eject_with_operation_finish() with the @volume
488 * and #GAsyncResult data returned in the @callback.
490 * Since: 2.22
492 void
493 g_volume_eject_with_operation (GVolume *volume,
494 GMountUnmountFlags flags,
495 GMountOperation *mount_operation,
496 GCancellable *cancellable,
497 GAsyncReadyCallback callback,
498 gpointer user_data)
500 GVolumeIface *iface;
502 g_return_if_fail (G_IS_VOLUME (volume));
504 iface = G_VOLUME_GET_IFACE (volume);
506 if (iface->eject == NULL && iface->eject_with_operation == NULL)
508 g_task_report_new_error (volume, callback, user_data,
509 g_volume_eject_with_operation,
510 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
511 /* Translators: This is an error
512 * message for volume objects that
513 * don't implement any of eject or eject_with_operation. */
514 _("volume doesn’t implement eject or eject_with_operation"));
515 return;
518 if (iface->eject_with_operation != NULL)
519 (* iface->eject_with_operation) (volume, flags, mount_operation, cancellable, callback, user_data);
520 else
521 (* iface->eject) (volume, flags, cancellable, callback, user_data);
525 * g_volume_eject_with_operation_finish:
526 * @volume: a #GVolume
527 * @result: a #GAsyncResult
528 * @error: a #GError location to store the error occurring, or %NULL
530 * Finishes ejecting a volume. If any errors occurred during the operation,
531 * @error will be set to contain the errors and %FALSE will be returned.
533 * Returns: %TRUE if the volume was successfully ejected. %FALSE otherwise
535 * Since: 2.22
537 gboolean
538 g_volume_eject_with_operation_finish (GVolume *volume,
539 GAsyncResult *result,
540 GError **error)
542 GVolumeIface *iface;
544 g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
545 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
547 if (g_async_result_legacy_propagate_error (result, error))
548 return FALSE;
549 else if (g_async_result_is_tagged (result, g_volume_eject_with_operation))
550 return g_task_propagate_boolean (G_TASK (result), error);
552 iface = G_VOLUME_GET_IFACE (volume);
553 if (iface->eject_with_operation_finish != NULL)
554 return (* iface->eject_with_operation_finish) (volume, result, error);
555 else
556 return (* iface->eject_finish) (volume, result, error);
560 * g_volume_get_identifier:
561 * @volume: a #GVolume
562 * @kind: the kind of identifier to return
564 * Gets the identifier of the given kind for @volume.
565 * See the [introduction][volume-identifier] for more
566 * information about volume identifiers.
568 * Returns: a newly allocated string containing the
569 * requested identfier, or %NULL if the #GVolume
570 * doesn't have this kind of identifier
572 char *
573 g_volume_get_identifier (GVolume *volume,
574 const char *kind)
576 GVolumeIface *iface;
578 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
579 g_return_val_if_fail (kind != NULL, NULL);
581 iface = G_VOLUME_GET_IFACE (volume);
583 if (iface->get_identifier == NULL)
584 return NULL;
586 return (* iface->get_identifier) (volume, kind);
590 * g_volume_enumerate_identifiers:
591 * @volume: a #GVolume
593 * Gets the kinds of [identifiers][volume-identifier] that @volume has.
594 * Use g_volume_get_identifier() to obtain 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.
599 char **
600 g_volume_enumerate_identifiers (GVolume *volume)
602 GVolumeIface *iface;
604 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
605 iface = G_VOLUME_GET_IFACE (volume);
607 if (iface->enumerate_identifiers == NULL)
608 return 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" -->
625 * GMount *mount;
626 * GFile *mount_root
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
632 * ]|
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))
637 * ]|
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: (nullable) (transfer full): the activation root of @volume
645 * or %NULL. Use g_object_unref() to free.
647 * Since: 2.18
649 GFile *
650 g_volume_get_activation_root (GVolume *volume)
652 GVolumeIface *iface;
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)
658 return 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
671 * Since: 2.32
673 const gchar *
674 g_volume_get_sort_key (GVolume *volume)
676 const gchar *ret = NULL;
677 GVolumeIface *iface;
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);
685 return ret;