GIcon: add g_icon_[de]serialize()
[glib.git] / gio / gvolume.c
blobaa5e40e3339a23a5d5ba4bc18294a3a4f8202fec
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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
21 * David Zeuthen <davidz@redhat.com>
24 #include "config.h"
25 #include "gmount.h"
26 #include "gvolume.h"
27 #include "gthemedicon.h"
28 #include "gasyncresult.h"
29 #include "gtask.h"
30 #include "gioerror.h"
31 #include "glibintl.h"
34 /**
35 * SECTION:gvolume
36 * @short_description: Volume management
37 * @include: gio/gio.h
39 * The #GVolume interface represents user-visible objects that can be
40 * mounted. Note, when porting from GnomeVFS, #GVolume is the moral
41 * equivalent of #GnomeVFSDrive.
43 * Mounting a #GVolume instance is an asynchronous operation. For more
44 * information about asynchronous operations, see #GAsyncResult and
45 * #GTask. To mount a #GVolume, first call g_volume_mount() with (at
46 * least) the #GVolume instance, optionally a #GMountOperation object
47 * and a #GAsyncReadyCallback.
49 * Typically, one will only want to pass %NULL for the
50 * #GMountOperation if automounting all volumes when a desktop session
51 * starts since it's not desirable to put up a lot of dialogs asking
52 * for credentials.
54 * The callback will be fired when the operation has resolved (either
55 * with success or failure), and a #GAsyncReady structure will be
56 * passed to the callback. That callback should then call
57 * g_volume_mount_finish() with the #GVolume instance and the
58 * #GAsyncReady data to see if the operation was completed
59 * successfully. If an @error is present when g_volume_mount_finish()
60 * is called, then it will be filled with any error information.
62 * <para id="volume-identifier">
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 commandline). 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. <filename>/dev/sda2</filename>),
69 * uuids. GIO uses predefind strings as names for the different kinds
70 * of identifiers: #G_VOLUME_IDENTIFIER_KIND_HAL_UDI,
71 * #G_VOLUME_IDENTIFIER_KIND_LABEL, etc. Use g_volume_get_identifier()
72 * to obtain an identifier for a volume.
73 * </para>
75 * Note that #G_VOLUME_IDENTIFIER_KIND_HAL_UDI will only be available
76 * when the gvfs hal volume monitor is in use. Other volume monitors
77 * will generally be able to provide the #G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE
78 * identifier, which can be used to obtain a hal device by means of
79 * libhal_manager_find_device_string_match().
82 typedef GVolumeIface GVolumeInterface;
83 G_DEFINE_INTERFACE(GVolume, g_volume, G_TYPE_OBJECT)
85 static void
86 g_volume_default_init (GVolumeInterface *iface)
88 /**
89 * GVolume::changed:
91 * Emitted when the volume has been changed.
92 **/
93 g_signal_new (I_("changed"),
94 G_TYPE_VOLUME,
95 G_SIGNAL_RUN_LAST,
96 G_STRUCT_OFFSET (GVolumeIface, changed),
97 NULL, NULL,
98 g_cclosure_marshal_VOID__VOID,
99 G_TYPE_NONE, 0);
102 * GVolume::removed:
104 * This signal is emitted when the #GVolume have been removed. If
105 * the recipient is holding references to the object they should
106 * release them so the object can be finalized.
108 g_signal_new (I_("removed"),
109 G_TYPE_VOLUME,
110 G_SIGNAL_RUN_LAST,
111 G_STRUCT_OFFSET (GVolumeIface, removed),
112 NULL, NULL,
113 g_cclosure_marshal_VOID__VOID,
114 G_TYPE_NONE, 0);
118 * g_volume_get_name:
119 * @volume: a #GVolume.
121 * Gets the name of @volume.
123 * Returns: the name for the given @volume. The returned string should
124 * be freed with g_free() when no longer needed.
126 char *
127 g_volume_get_name (GVolume *volume)
129 GVolumeIface *iface;
131 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
133 iface = G_VOLUME_GET_IFACE (volume);
135 return (* iface->get_name) (volume);
139 * g_volume_get_icon:
140 * @volume: a #GVolume.
142 * Gets the icon for @volume.
144 * Returns: (transfer full): a #GIcon.
145 * The returned object should be unreffed with g_object_unref()
146 * when no longer needed.
148 GIcon *
149 g_volume_get_icon (GVolume *volume)
151 GVolumeIface *iface;
153 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
155 iface = G_VOLUME_GET_IFACE (volume);
157 return (* iface->get_icon) (volume);
161 * g_volume_get_symbolic_icon:
162 * @volume: a #GVolume.
164 * Gets the symbolic icon for @volume.
166 * Returns: (transfer full): a #GIcon.
167 * The returned object should be unreffed with g_object_unref()
168 * when no longer needed.
170 * Since: 2.34
172 GIcon *
173 g_volume_get_symbolic_icon (GVolume *volume)
175 GVolumeIface *iface;
176 GIcon *ret;
178 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
180 iface = G_VOLUME_GET_IFACE (volume);
182 if (iface->get_symbolic_icon != NULL)
183 ret = iface->get_symbolic_icon (volume);
184 else
185 ret = g_themed_icon_new_with_default_fallbacks ("folder-remote-symbolic");
187 return ret;
192 * g_volume_get_uuid:
193 * @volume: a #GVolume.
195 * Gets the UUID for the @volume. The reference is typically based on
196 * the file system UUID for the volume in question and should be
197 * considered an opaque string. Returns %NULL if there is no UUID
198 * available.
200 * Returns: the UUID for @volume or %NULL if no UUID can be computed.
201 * The returned string should be freed with g_free()
202 * when no longer needed.
204 char *
205 g_volume_get_uuid (GVolume *volume)
207 GVolumeIface *iface;
209 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
211 iface = G_VOLUME_GET_IFACE (volume);
213 return (* iface->get_uuid) (volume);
217 * g_volume_get_drive:
218 * @volume: a #GVolume.
220 * Gets the drive for the @volume.
222 * Returns: (transfer full): a #GDrive or %NULL if @volume is not associated with a drive.
223 * The returned object should be unreffed with g_object_unref()
224 * when no longer needed.
226 GDrive *
227 g_volume_get_drive (GVolume *volume)
229 GVolumeIface *iface;
231 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
233 iface = G_VOLUME_GET_IFACE (volume);
235 return (* iface->get_drive) (volume);
239 * g_volume_get_mount:
240 * @volume: a #GVolume.
242 * Gets the mount for the @volume.
244 * Returns: (transfer full): a #GMount or %NULL if @volume isn't mounted.
245 * The returned object should be unreffed with g_object_unref()
246 * when no longer needed.
248 GMount *
249 g_volume_get_mount (GVolume *volume)
251 GVolumeIface *iface;
253 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
255 iface = G_VOLUME_GET_IFACE (volume);
257 return (* iface->get_mount) (volume);
262 * g_volume_can_mount:
263 * @volume: a #GVolume.
265 * Checks if a volume can be mounted.
267 * Returns: %TRUE if the @volume can be mounted. %FALSE otherwise.
269 gboolean
270 g_volume_can_mount (GVolume *volume)
272 GVolumeIface *iface;
274 g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
276 iface = G_VOLUME_GET_IFACE (volume);
278 if (iface->can_mount == NULL)
279 return FALSE;
281 return (* iface->can_mount) (volume);
285 * g_volume_can_eject:
286 * @volume: a #GVolume.
288 * Checks if a volume can be ejected.
290 * Returns: %TRUE if the @volume can be ejected. %FALSE otherwise.
292 gboolean
293 g_volume_can_eject (GVolume *volume)
295 GVolumeIface *iface;
297 g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
299 iface = G_VOLUME_GET_IFACE (volume);
301 if (iface->can_eject == NULL)
302 return FALSE;
304 return (* iface->can_eject) (volume);
308 * g_volume_should_automount:
309 * @volume: a #GVolume
311 * Returns whether the volume should be automatically mounted.
313 * Returns: %TRUE if the volume should be automatically mounted.
315 gboolean
316 g_volume_should_automount (GVolume *volume)
318 GVolumeIface *iface;
320 g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
322 iface = G_VOLUME_GET_IFACE (volume);
324 if (iface->should_automount == NULL)
325 return FALSE;
327 return (* iface->should_automount) (volume);
332 * g_volume_mount:
333 * @volume: a #GVolume.
334 * @flags: flags affecting the operation
335 * @mount_operation: (allow-none): a #GMountOperation or %NULL to avoid user interaction.
336 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
337 * @callback: (allow-none): a #GAsyncReadyCallback, or %NULL.
338 * @user_data: user data that gets passed to @callback
340 * Mounts a volume. This is an asynchronous operation, and is
341 * finished by calling g_volume_mount_finish() with the @volume
342 * and #GAsyncResult returned in the @callback.
344 * Virtual: mount_fn
346 void
347 g_volume_mount (GVolume *volume,
348 GMountMountFlags flags,
349 GMountOperation *mount_operation,
350 GCancellable *cancellable,
351 GAsyncReadyCallback callback,
352 gpointer user_data)
354 GVolumeIface *iface;
356 g_return_if_fail (G_IS_VOLUME (volume));
358 iface = G_VOLUME_GET_IFACE (volume);
360 if (iface->mount_fn == NULL)
362 g_task_report_new_error (volume, callback, user_data,
363 g_volume_mount,
364 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
365 _("volume doesn't implement mount"));
366 return;
369 (* iface->mount_fn) (volume, flags, mount_operation, cancellable, callback, user_data);
373 * g_volume_mount_finish:
374 * @volume: a #GVolume
375 * @result: a #GAsyncResult
376 * @error: a #GError location to store an error, or %NULL to ignore
378 * Finishes mounting a volume. If any errors occurred during the operation,
379 * @error will be set to contain the errors and %FALSE will be returned.
381 * If the mount operation succeeded, g_volume_get_mount() on @volume
382 * is guaranteed to return the mount right after calling this
383 * function; there's no need to listen for the 'mount-added' signal on
384 * #GVolumeMonitor.
386 * Returns: %TRUE, %FALSE if operation failed.
388 gboolean
389 g_volume_mount_finish (GVolume *volume,
390 GAsyncResult *result,
391 GError **error)
393 GVolumeIface *iface;
395 g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
396 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
398 if (g_async_result_legacy_propagate_error (result, error))
399 return FALSE;
400 else if (g_async_result_is_tagged (result, g_volume_mount))
401 return g_task_propagate_boolean (G_TASK (result), error);
403 iface = G_VOLUME_GET_IFACE (volume);
404 return (* iface->mount_finish) (volume, result, error);
408 * g_volume_eject:
409 * @volume: a #GVolume.
410 * @flags: flags affecting the unmount if required for eject
411 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
412 * @callback: (allow-none): a #GAsyncReadyCallback, or %NULL.
413 * @user_data: user data that gets passed to @callback
415 * Ejects a volume. This is an asynchronous operation, and is
416 * finished by calling g_volume_eject_finish() with the @volume
417 * and #GAsyncResult returned in the @callback.
419 * Deprecated: 2.22: Use g_volume_eject_with_operation() instead.
421 void
422 g_volume_eject (GVolume *volume,
423 GMountUnmountFlags flags,
424 GCancellable *cancellable,
425 GAsyncReadyCallback callback,
426 gpointer user_data)
428 GVolumeIface *iface;
430 g_return_if_fail (G_IS_VOLUME (volume));
432 iface = G_VOLUME_GET_IFACE (volume);
434 if (iface->eject == NULL)
436 g_task_report_new_error (volume, callback, user_data,
437 g_volume_eject_with_operation,
438 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
439 _("volume doesn't implement eject"));
440 return;
443 (* iface->eject) (volume, flags, cancellable, callback, user_data);
447 * g_volume_eject_finish:
448 * @volume: pointer to a #GVolume.
449 * @result: a #GAsyncResult.
450 * @error: a #GError location to store an error, or %NULL to ignore
452 * Finishes ejecting a volume. If any errors occurred during the operation,
453 * @error will be set to contain the errors and %FALSE will be returned.
455 * Returns: %TRUE, %FALSE if operation failed.
457 * Deprecated: 2.22: Use g_volume_eject_with_operation_finish() instead.
459 gboolean
460 g_volume_eject_finish (GVolume *volume,
461 GAsyncResult *result,
462 GError **error)
464 GVolumeIface *iface;
466 g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
467 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
469 if (g_async_result_legacy_propagate_error (result, error))
470 return FALSE;
471 if (g_async_result_is_tagged (result, g_volume_eject_with_operation))
472 return g_task_propagate_boolean (G_TASK (result), error);
474 iface = G_VOLUME_GET_IFACE (volume);
475 return (* iface->eject_finish) (volume, result, error);
479 * g_volume_eject_with_operation:
480 * @volume: a #GVolume.
481 * @flags: flags affecting the unmount if required for eject
482 * @mount_operation: (allow-none): a #GMountOperation or %NULL to
483 * avoid user interaction.
484 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
485 * @callback: (allow-none): a #GAsyncReadyCallback, or %NULL.
486 * @user_data: user data passed to @callback.
488 * Ejects a volume. This is an asynchronous operation, and is
489 * finished by calling g_volume_eject_with_operation_finish() with the @volume
490 * and #GAsyncResult data returned in the @callback.
492 * Since: 2.22
494 void
495 g_volume_eject_with_operation (GVolume *volume,
496 GMountUnmountFlags flags,
497 GMountOperation *mount_operation,
498 GCancellable *cancellable,
499 GAsyncReadyCallback callback,
500 gpointer user_data)
502 GVolumeIface *iface;
504 g_return_if_fail (G_IS_VOLUME (volume));
506 iface = G_VOLUME_GET_IFACE (volume);
508 if (iface->eject == NULL && iface->eject_with_operation == NULL)
510 g_task_report_new_error (volume, callback, user_data,
511 g_volume_eject_with_operation,
512 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
513 /* Translators: This is an error
514 * message for volume objects that
515 * don't implement any of eject or eject_with_operation. */
516 _("volume doesn't implement eject or eject_with_operation"));
517 return;
520 if (iface->eject_with_operation != NULL)
521 (* iface->eject_with_operation) (volume, flags, mount_operation, cancellable, callback, user_data);
522 else
523 (* iface->eject) (volume, flags, cancellable, callback, user_data);
527 * g_volume_eject_with_operation_finish:
528 * @volume: a #GVolume.
529 * @result: a #GAsyncResult.
530 * @error: a #GError location to store the error occurring, or %NULL to
531 * ignore.
533 * Finishes ejecting a volume. If any errors occurred during the operation,
534 * @error will be set to contain the errors and %FALSE will be returned.
536 * Returns: %TRUE if the volume was successfully ejected. %FALSE otherwise.
538 * Since: 2.22
540 gboolean
541 g_volume_eject_with_operation_finish (GVolume *volume,
542 GAsyncResult *result,
543 GError **error)
545 GVolumeIface *iface;
547 g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
548 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
550 if (g_async_result_legacy_propagate_error (result, error))
551 return FALSE;
552 else if (g_async_result_is_tagged (result, g_volume_eject_with_operation))
553 return g_task_propagate_boolean (G_TASK (result), error);
555 iface = G_VOLUME_GET_IFACE (volume);
556 if (iface->eject_with_operation_finish != NULL)
557 return (* iface->eject_with_operation_finish) (volume, result, error);
558 else
559 return (* iface->eject_finish) (volume, result, error);
563 * g_volume_get_identifier:
564 * @volume: a #GVolume
565 * @kind: the kind of identifier to return
567 * Gets the identifier of the given kind for @volume.
568 * See the <link linkend="volume-identifier">introduction</link>
569 * for more information about volume identifiers.
571 * Returns: a newly allocated string containing the
572 * requested identfier, or %NULL if the #GVolume
573 * doesn't have this kind of identifier
575 char *
576 g_volume_get_identifier (GVolume *volume,
577 const char *kind)
579 GVolumeIface *iface;
581 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
582 g_return_val_if_fail (kind != NULL, NULL);
584 iface = G_VOLUME_GET_IFACE (volume);
586 if (iface->get_identifier == NULL)
587 return NULL;
589 return (* iface->get_identifier) (volume, kind);
593 * g_volume_enumerate_identifiers:
594 * @volume: a #GVolume
596 * Gets the kinds of <link linkend="volume-identifier">identifiers</link>
597 * that @volume has. Use g_volume_get_identifier() to obtain
598 * the identifiers themselves.
600 * Returns: (array zero-terminated=1) (transfer full): a %NULL-terminated array
601 * of strings containing kinds of identifiers. Use g_strfreev() to free.
603 char **
604 g_volume_enumerate_identifiers (GVolume *volume)
606 GVolumeIface *iface;
608 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
609 iface = G_VOLUME_GET_IFACE (volume);
611 if (iface->enumerate_identifiers == NULL)
612 return NULL;
614 return (* iface->enumerate_identifiers) (volume);
618 * g_volume_get_activation_root:
619 * @volume: a #GVolume
621 * Gets the activation root for a #GVolume if it is known ahead of
622 * mount time. Returns %NULL otherwise. If not %NULL and if @volume
623 * is mounted, then the result of g_mount_get_root() on the
624 * #GMount object obtained from g_volume_get_mount() will always
625 * either be equal or a prefix of what this function returns. In
626 * other words, in code
628 * <programlisting>
629 * GMount *mount;
630 * GFile *mount_root
631 * GFile *volume_activation_root;
633 * mount = g_volume_get_mount (volume); /&ast; mounted, so never NULL &ast;/
634 * mount_root = g_mount_get_root (mount);
635 * volume_activation_root = g_volume_get_activation_root(volume); /&ast; assume not NULL &ast;/
636 * </programlisting>
638 * then the expression
640 * <programlisting>
641 * (g_file_has_prefix (volume_activation_root, mount_root) ||
642 g_file_equal (volume_activation_root, mount_root))
643 * </programlisting>
645 * will always be %TRUE.
647 * Activation roots are typically used in #GVolumeMonitor
648 * implementations to find the underlying mount to shadow, see
649 * g_mount_is_shadowed() for more details.
651 * Returns: (transfer full): the activation root of @volume or %NULL. Use
652 * g_object_unref() to free.
654 * Since: 2.18
656 GFile *
657 g_volume_get_activation_root (GVolume *volume)
659 GVolumeIface *iface;
661 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
662 iface = G_VOLUME_GET_IFACE (volume);
664 if (iface->get_activation_root == NULL)
665 return NULL;
667 return (* iface->get_activation_root) (volume);
671 * g_volume_get_sort_key:
672 * @volume: A #GVolume.
674 * Gets the sort key for @volume, if any.
676 * Returns: Sorting key for @volume or %NULL if no such key is available.
678 * Since: 2.32
680 const gchar *
681 g_volume_get_sort_key (GVolume *volume)
683 const gchar *ret = NULL;
684 GVolumeIface *iface;
686 g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
688 iface = G_VOLUME_GET_IFACE (volume);
689 if (iface->get_sort_key != NULL)
690 ret = iface->get_sort_key (volume);
692 return ret;