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, 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>
27 #include "gasyncresult.h"
28 #include "gsimpleasyncresult.h"
35 * @short_description: Volume management
38 * The #GVolume interface represents user-visible objects that can be
39 * mounted. Note, when porting from GnomeVFS, #GVolume is the moral
40 * equivalent of #GnomeVFSDrive.
42 * Mounting a #GVolume instance is an asynchronous operation. For more
43 * information about asynchronous operations, see #GAsyncResult and
44 * #GSimpleAsyncResult. To mount a #GVolume, first call
45 * g_volume_mount() with (at least) the #GVolume instance, optionally
46 * a #GMountOperation object and a #GAsyncReadyCallback.
48 * Typically, one will only want to pass %NULL for the
49 * #GMountOperation if automounting all volumes when a desktop session
50 * starts since it's not desirable to put up a lot of dialogs asking
53 * The callback will be fired when the operation has resolved (either
54 * with success or failure), and a #GAsyncReady structure will be
55 * passed to the callback. That callback should then call
56 * g_volume_mount_finish() with the #GVolume instance and the
57 * #GAsyncReady data to see if the operation was completed
58 * successfully. If an @error is present when g_volume_mount_finish()
59 * is called, then it will be filled with any error information.
61 * <para id="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. <filename>/dev/sda2</filename>),
68 * uuids. GIO uses predefind strings as names for the different kinds
69 * of identifiers: #G_VOLUME_IDENTIFIER_KIND_HAL_UDI,
70 * #G_VOLUME_IDENTIFIER_KIND_LABEL, etc. Use g_volume_get_identifier()
71 * to obtain an identifier for a volume.
74 * Note that #G_VOLUME_IDENTIFIER_KIND_HAL_UDI will only be available
75 * when the gvfs hal volume monitor is in use. Other volume monitors
76 * will generally be able to provide the #G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE
77 * identifier, which can be used to obtain a hal device by means of
78 * libhal_manager_find_device_string_match().
81 typedef GVolumeIface GVolumeInterface
;
82 G_DEFINE_INTERFACE(GVolume
, g_volume
, G_TYPE_OBJECT
)
85 g_volume_default_init (GVolumeInterface
*iface
)
90 * Emitted when the volume has been changed.
92 g_signal_new (I_("changed"),
95 G_STRUCT_OFFSET (GVolumeIface
, changed
),
97 g_cclosure_marshal_VOID__VOID
,
103 * This signal is emitted when the #GVolume have been removed. If
104 * the recipient is holding references to the object they should
105 * release them so the object can be finalized.
107 g_signal_new (I_("removed"),
110 G_STRUCT_OFFSET (GVolumeIface
, removed
),
112 g_cclosure_marshal_VOID__VOID
,
118 * @volume: a #GVolume.
120 * Gets the name of @volume.
122 * Returns: the name for the given @volume. The returned string should
123 * be freed with g_free() when no longer needed.
126 g_volume_get_name (GVolume
*volume
)
130 g_return_val_if_fail (G_IS_VOLUME (volume
), NULL
);
132 iface
= G_VOLUME_GET_IFACE (volume
);
134 return (* iface
->get_name
) (volume
);
139 * @volume: a #GVolume.
141 * Gets the icon for @volume.
143 * Returns: (transfer full): a #GIcon.
144 * The returned object should be unreffed with g_object_unref()
145 * when no longer needed.
148 g_volume_get_icon (GVolume
*volume
)
152 g_return_val_if_fail (G_IS_VOLUME (volume
), NULL
);
154 iface
= G_VOLUME_GET_IFACE (volume
);
156 return (* iface
->get_icon
) (volume
);
161 * @volume: a #GVolume.
163 * Gets the UUID for the @volume. The reference is typically based on
164 * the file system UUID for the volume in question and should be
165 * considered an opaque string. Returns %NULL if there is no UUID
168 * Returns: the UUID for @volume or %NULL if no UUID can be computed.
169 * The returned string should be freed with g_free()
170 * when no longer needed.
173 g_volume_get_uuid (GVolume
*volume
)
177 g_return_val_if_fail (G_IS_VOLUME (volume
), NULL
);
179 iface
= G_VOLUME_GET_IFACE (volume
);
181 return (* iface
->get_uuid
) (volume
);
185 * g_volume_get_drive:
186 * @volume: a #GVolume.
188 * Gets the drive for the @volume.
190 * Returns: (transfer full): a #GDrive or %NULL if @volume is not associated with a drive.
191 * The returned object should be unreffed with g_object_unref()
192 * when no longer needed.
195 g_volume_get_drive (GVolume
*volume
)
199 g_return_val_if_fail (G_IS_VOLUME (volume
), NULL
);
201 iface
= G_VOLUME_GET_IFACE (volume
);
203 return (* iface
->get_drive
) (volume
);
207 * g_volume_get_mount:
208 * @volume: a #GVolume.
210 * Gets the mount for the @volume.
212 * Returns: (transfer full): a #GMount or %NULL if @volume isn't mounted.
213 * The returned object should be unreffed with g_object_unref()
214 * when no longer needed.
217 g_volume_get_mount (GVolume
*volume
)
221 g_return_val_if_fail (G_IS_VOLUME (volume
), NULL
);
223 iface
= G_VOLUME_GET_IFACE (volume
);
225 return (* iface
->get_mount
) (volume
);
230 * g_volume_can_mount:
231 * @volume: a #GVolume.
233 * Checks if a volume can be mounted.
235 * Returns: %TRUE if the @volume can be mounted. %FALSE otherwise.
238 g_volume_can_mount (GVolume
*volume
)
242 g_return_val_if_fail (G_IS_VOLUME (volume
), FALSE
);
244 iface
= G_VOLUME_GET_IFACE (volume
);
246 if (iface
->can_mount
== NULL
)
249 return (* iface
->can_mount
) (volume
);
253 * g_volume_can_eject:
254 * @volume: a #GVolume.
256 * Checks if a volume can be ejected.
258 * Returns: %TRUE if the @volume can be ejected. %FALSE otherwise.
261 g_volume_can_eject (GVolume
*volume
)
265 g_return_val_if_fail (G_IS_VOLUME (volume
), FALSE
);
267 iface
= G_VOLUME_GET_IFACE (volume
);
269 if (iface
->can_eject
== NULL
)
272 return (* iface
->can_eject
) (volume
);
276 * g_volume_should_automount:
277 * @volume: a #GVolume
279 * Returns whether the volume should be automatically mounted.
281 * Returns: %TRUE if the volume should be automatically mounted.
284 g_volume_should_automount (GVolume
*volume
)
288 g_return_val_if_fail (G_IS_VOLUME (volume
), FALSE
);
290 iface
= G_VOLUME_GET_IFACE (volume
);
292 if (iface
->should_automount
== NULL
)
295 return (* iface
->should_automount
) (volume
);
301 * @volume: a #GVolume.
302 * @flags: flags affecting the operation
303 * @mount_operation: (allow-none): a #GMountOperation or %NULL to avoid user interaction.
304 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
305 * @callback: (allow-none): a #GAsyncReadyCallback, or %NULL.
306 * @user_data: user data that gets passed to @callback
308 * Mounts a volume. This is an asynchronous operation, and is
309 * finished by calling g_volume_mount_finish() with the @volume
310 * and #GAsyncResult returned in the @callback.
315 g_volume_mount (GVolume
*volume
,
316 GMountMountFlags flags
,
317 GMountOperation
*mount_operation
,
318 GCancellable
*cancellable
,
319 GAsyncReadyCallback callback
,
324 g_return_if_fail (G_IS_VOLUME (volume
));
326 iface
= G_VOLUME_GET_IFACE (volume
);
328 if (iface
->mount_fn
== NULL
)
330 g_simple_async_report_error_in_idle (G_OBJECT (volume
), callback
, user_data
,
331 G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
332 _("volume doesn't implement mount"));
337 (* iface
->mount_fn
) (volume
, flags
, mount_operation
, cancellable
, callback
, user_data
);
341 * g_volume_mount_finish:
342 * @volume: a #GVolume
343 * @result: a #GAsyncResult
344 * @error: a #GError location to store an error, or %NULL to ignore
346 * Finishes mounting a volume. If any errors occurred during the operation,
347 * @error will be set to contain the errors and %FALSE will be returned.
349 * If the mount operation succeeded, g_volume_get_mount() on @volume
350 * is guaranteed to return the mount right after calling this
351 * function; there's no need to listen for the 'mount-added' signal on
354 * Returns: %TRUE, %FALSE if operation failed.
357 g_volume_mount_finish (GVolume
*volume
,
358 GAsyncResult
*result
,
363 g_return_val_if_fail (G_IS_VOLUME (volume
), FALSE
);
364 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), FALSE
);
366 if (G_IS_SIMPLE_ASYNC_RESULT (result
))
368 GSimpleAsyncResult
*simple
= G_SIMPLE_ASYNC_RESULT (result
);
369 if (g_simple_async_result_propagate_error (simple
, error
))
373 iface
= G_VOLUME_GET_IFACE (volume
);
374 return (* iface
->mount_finish
) (volume
, result
, error
);
379 * @volume: a #GVolume.
380 * @flags: flags affecting the unmount if required for eject
381 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
382 * @callback: (allow-none): a #GAsyncReadyCallback, or %NULL.
383 * @user_data: user data that gets passed to @callback
385 * Ejects a volume. This is an asynchronous operation, and is
386 * finished by calling g_volume_eject_finish() with the @volume
387 * and #GAsyncResult returned in the @callback.
389 * Deprecated: 2.22: Use g_volume_eject_with_operation() instead.
392 g_volume_eject (GVolume
*volume
,
393 GMountUnmountFlags flags
,
394 GCancellable
*cancellable
,
395 GAsyncReadyCallback callback
,
400 g_return_if_fail (G_IS_VOLUME (volume
));
402 iface
= G_VOLUME_GET_IFACE (volume
);
404 if (iface
->eject
== NULL
)
406 g_simple_async_report_error_in_idle (G_OBJECT (volume
), callback
, user_data
,
407 G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
408 _("volume doesn't implement eject"));
413 (* iface
->eject
) (volume
, flags
, cancellable
, callback
, user_data
);
417 * g_volume_eject_finish:
418 * @volume: pointer to a #GVolume.
419 * @result: a #GAsyncResult.
420 * @error: a #GError location to store an error, or %NULL to ignore
422 * Finishes ejecting a volume. If any errors occurred during the operation,
423 * @error will be set to contain the errors and %FALSE will be returned.
425 * Returns: %TRUE, %FALSE if operation failed.
427 * Deprecated: 2.22: Use g_volume_eject_with_operation_finish() instead.
430 g_volume_eject_finish (GVolume
*volume
,
431 GAsyncResult
*result
,
436 g_return_val_if_fail (G_IS_VOLUME (volume
), FALSE
);
437 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), FALSE
);
439 if (G_IS_SIMPLE_ASYNC_RESULT (result
))
441 GSimpleAsyncResult
*simple
= G_SIMPLE_ASYNC_RESULT (result
);
442 if (g_simple_async_result_propagate_error (simple
, error
))
446 iface
= G_VOLUME_GET_IFACE (volume
);
447 return (* iface
->eject_finish
) (volume
, result
, error
);
451 * g_volume_eject_with_operation:
452 * @volume: a #GVolume.
453 * @flags: flags affecting the unmount if required for eject
454 * @mount_operation: (allow-none): a #GMountOperation or %NULL to
455 * avoid user interaction.
456 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
457 * @callback: a #GAsyncReadyCallback, or %NULL.
458 * @user_data: user data passed to @callback.
460 * Ejects a volume. This is an asynchronous operation, and is
461 * finished by calling g_volume_eject_with_operation_finish() with the @volume
462 * and #GAsyncResult data returned in the @callback.
467 g_volume_eject_with_operation (GVolume
*volume
,
468 GMountUnmountFlags flags
,
469 GMountOperation
*mount_operation
,
470 GCancellable
*cancellable
,
471 GAsyncReadyCallback callback
,
476 g_return_if_fail (G_IS_VOLUME (volume
));
478 iface
= G_VOLUME_GET_IFACE (volume
);
480 if (iface
->eject
== NULL
&& iface
->eject_with_operation
== NULL
)
482 g_simple_async_report_error_in_idle (G_OBJECT (volume
),
484 G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
485 /* Translators: This is an error
486 * message for volume objects that
487 * don't implement any of eject or eject_with_operation. */
488 _("volume doesn't implement eject or eject_with_operation"));
492 if (iface
->eject_with_operation
!= NULL
)
493 (* iface
->eject_with_operation
) (volume
, flags
, mount_operation
, cancellable
, callback
, user_data
);
495 (* iface
->eject
) (volume
, flags
, cancellable
, callback
, user_data
);
499 * g_volume_eject_with_operation_finish:
500 * @volume: a #GVolume.
501 * @result: a #GAsyncResult.
502 * @error: a #GError location to store the error occurring, or %NULL to
505 * Finishes ejecting a volume. If any errors occurred during the operation,
506 * @error will be set to contain the errors and %FALSE will be returned.
508 * Returns: %TRUE if the volume was successfully ejected. %FALSE otherwise.
513 g_volume_eject_with_operation_finish (GVolume
*volume
,
514 GAsyncResult
*result
,
519 g_return_val_if_fail (G_IS_VOLUME (volume
), FALSE
);
520 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), FALSE
);
522 if (G_IS_SIMPLE_ASYNC_RESULT (result
))
524 GSimpleAsyncResult
*simple
= G_SIMPLE_ASYNC_RESULT (result
);
525 if (g_simple_async_result_propagate_error (simple
, error
))
529 iface
= G_VOLUME_GET_IFACE (volume
);
530 if (iface
->eject_with_operation_finish
!= NULL
)
531 return (* iface
->eject_with_operation_finish
) (volume
, result
, error
);
533 return (* iface
->eject_finish
) (volume
, result
, error
);
537 * g_volume_get_identifier:
538 * @volume: a #GVolume
539 * @kind: the kind of identifier to return
541 * Gets the identifier of the given kind for @volume.
542 * See the <link linkend="volume-identifier">introduction</link>
543 * for more information about volume identifiers.
545 * Returns: a newly allocated string containing the
546 * requested identfier, or %NULL if the #GVolume
547 * doesn't have this kind of identifier
550 g_volume_get_identifier (GVolume
*volume
,
555 g_return_val_if_fail (G_IS_VOLUME (volume
), NULL
);
556 g_return_val_if_fail (kind
!= NULL
, NULL
);
558 iface
= G_VOLUME_GET_IFACE (volume
);
560 if (iface
->get_identifier
== NULL
)
563 return (* iface
->get_identifier
) (volume
, kind
);
567 * g_volume_enumerate_identifiers:
568 * @volume: a #GVolume
570 * Gets the kinds of <link linkend="volume-identifier">identifiers</link>
571 * that @volume has. Use g_volume_get_identifier() to obtain
572 * the identifiers themselves.
574 * Returns: (array zero-terminated=1) (transfer full): a %NULL-terminated array
575 * of strings containing kinds of identifiers. Use g_strfreev() to free.
578 g_volume_enumerate_identifiers (GVolume
*volume
)
582 g_return_val_if_fail (G_IS_VOLUME (volume
), NULL
);
583 iface
= G_VOLUME_GET_IFACE (volume
);
585 if (iface
->enumerate_identifiers
== NULL
)
588 return (* iface
->enumerate_identifiers
) (volume
);
592 * g_volume_get_activation_root:
593 * @volume: a #GVolume
595 * Gets the activation root for a #GVolume if it is known ahead of
596 * mount time. Returns %NULL otherwise. If not %NULL and if @volume
597 * is mounted, then the result of g_mount_get_root() on the
598 * #GMount object obtained from g_volume_get_mount() will always
599 * either be equal or a prefix of what this function returns. In
600 * other words, in code
605 * GFile *volume_activation_root;
607 * mount = g_volume_get_mount (volume); /* mounted, so never NULL */
608 * mount_root = g_mount_get_root (mount);
609 * volume_activation_root = g_volume_get_activation_root(volume); /* assume not NULL */
612 * then the expression
615 * (g_file_has_prefix (volume_activation_root, mount_root) ||
616 g_file_equal (volume_activation_root, mount_root))
619 * will always be %TRUE.
621 * Activation roots are typically used in #GVolumeMonitor
622 * implementations to find the underlying mount to shadow, see
623 * g_mount_is_shadowed() for more details.
625 * Returns: (transfer full): the activation root of @volume or %NULL. Use
626 * g_object_unref() to free.
631 g_volume_get_activation_root (GVolume
*volume
)
635 g_return_val_if_fail (G_IS_VOLUME (volume
), NULL
);
636 iface
= G_VOLUME_GET_IFACE (volume
);
638 if (iface
->get_activation_root
== NULL
)
641 return (* iface
->get_activation_root
) (volume
);
645 * g_volume_get_sort_key:
646 * @volume: A #GVolume.
648 * Gets the sort key for @volume, if any.
650 * Returns: Sorting key for @volume or %NULL if no such key is available.
655 g_volume_get_sort_key (GVolume
*volume
)
657 const gchar
*ret
= NULL
;
660 g_return_val_if_fail (G_IS_VOLUME (volume
), NULL
);
662 iface
= G_VOLUME_GET_IFACE (volume
);
663 if (iface
->get_sort_key
!= NULL
)
664 ret
= iface
->get_sort_key (volume
);