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>
26 #include "gsimpleasyncresult.h"
27 #include "gthemedicon.h"
28 #include "gasyncresult.h"
35 * @short_description: Drive management
38 * #GDrive - this represent a piece of hardware connected to the machine.
39 * It's generally only created for removable hardware or hardware with
42 * #GDrive is a container class for #GVolume objects that stem from
43 * the same piece of media. As such, #GDrive abstracts a drive with
44 * (or without) removable media and provides operations for querying
45 * whether media is available, determing whether media change is
46 * automatically detected and ejecting the media.
48 * If the #GDrive reports that media isn't automatically detected, one
49 * can poll for media; typically one should not do this periodically
50 * as a poll for media operation is potententially expensive and may
51 * spin up the drive creating noise.
53 * #GDrive supports starting and stopping drives with authentication
54 * support for the former. This can be used to support a diverse set
55 * of use cases including connecting/disconnecting iSCSI devices,
56 * powering down external disk enclosures and starting/stopping
57 * multi-disk devices such as RAID devices. Note that the actual
58 * semantics and side-effects of starting/stopping a #GDrive may vary
59 * according to implementation. To choose the correct verbs in e.g. a
60 * file manager, use g_drive_get_start_stop_type().
62 * For porting from GnomeVFS note that there is no equivalent of
63 * #GDrive in that API.
66 typedef GDriveIface GDriveInterface;
67 G_DEFINE_INTERFACE(GDrive, g_drive, G_TYPE_OBJECT)
70 g_drive_default_init (GDriveInterface *iface)
76 * Emitted when the drive's state has changed.
78 g_signal_new (I_("changed"),
81 G_STRUCT_OFFSET (GDriveIface, changed),
83 g_cclosure_marshal_VOID__VOID,
87 * GDrive::disconnected:
90 * This signal is emitted when the #GDrive have been
91 * disconnected. If the recipient is holding references to the
92 * object they should release them so the object can be
95 g_signal_new (I_("disconnected"),
98 G_STRUCT_OFFSET (GDriveIface, disconnected),
100 g_cclosure_marshal_VOID__VOID,
104 * GDrive::eject-button:
107 * Emitted when the physical eject button (if any) of a drive has
110 g_signal_new (I_("eject-button"),
113 G_STRUCT_OFFSET (GDriveIface, eject_button),
115 g_cclosure_marshal_VOID__VOID,
119 * GDrive::stop-button:
122 * Emitted when the physical stop button (if any) of a drive has
127 g_signal_new (I_("stop-button"),
130 G_STRUCT_OFFSET (GDriveIface, stop_button),
132 g_cclosure_marshal_VOID__VOID,
140 * Gets the name of @drive.
142 * Returns: a string containing @drive's name. The returned
143 * string should be freed when no longer needed.
146 g_drive_get_name (GDrive *drive)
150 g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
152 iface = G_DRIVE_GET_IFACE (drive);
154 return (* iface->get_name) (drive);
161 * Gets the icon for @drive.
163 * Returns: (transfer full): #GIcon for the @drive.
164 * Free the returned object with g_object_unref().
167 g_drive_get_icon (GDrive *drive)
171 g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
173 iface = G_DRIVE_GET_IFACE (drive);
175 return (* iface->get_icon) (drive);
179 * g_drive_get_symbolic_icon:
182 * Gets the icon for @drive.
184 * Returns: (transfer full): symbolic #GIcon for the @drive.
185 * Free the returned object with g_object_unref().
190 g_drive_get_symbolic_icon (GDrive *drive)
195 g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
197 iface = G_DRIVE_GET_IFACE (drive);
199 if (iface->get_symbolic_icon != NULL)
200 ret = iface->get_symbolic_icon (drive);
202 ret = g_themed_icon_new_with_default_fallbacks ("drive-removable-media-symbolic");
208 * g_drive_has_volumes:
211 * Check if @drive has any mountable volumes.
213 * Returns: %TRUE if the @drive contains volumes, %FALSE otherwise.
216 g_drive_has_volumes (GDrive *drive)
220 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
222 iface = G_DRIVE_GET_IFACE (drive);
224 return (* iface->has_volumes) (drive);
228 * g_drive_get_volumes:
231 * Get a list of mountable volumes for @drive.
233 * The returned list should be freed with g_list_free(), after
234 * its elements have been unreffed with g_object_unref().
236 * Returns: (element-type GVolume) (transfer full): #GList containing any #GVolume objects on the given @drive.
239 g_drive_get_volumes (GDrive *drive)
243 g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
245 iface = G_DRIVE_GET_IFACE (drive);
247 return (* iface->get_volumes) (drive);
251 * g_drive_is_media_check_automatic:
254 * Checks if @drive is capabable of automatically detecting media changes.
256 * Returns: %TRUE if the @drive is capabable of automatically detecting
257 * media changes, %FALSE otherwise.
260 g_drive_is_media_check_automatic (GDrive *drive)
264 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
266 iface = G_DRIVE_GET_IFACE (drive);
268 return (* iface->is_media_check_automatic) (drive);
272 * g_drive_is_media_removable:
275 * Checks if the @drive supports removable media.
277 * Returns: %TRUE if @drive supports removable media, %FALSE otherwise.
280 g_drive_is_media_removable (GDrive *drive)
284 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
286 iface = G_DRIVE_GET_IFACE (drive);
288 return (* iface->is_media_removable) (drive);
295 * Checks if the @drive has media. Note that the OS may not be polling
296 * the drive for media changes; see g_drive_is_media_check_automatic()
299 * Returns: %TRUE if @drive has media, %FALSE otherwise.
302 g_drive_has_media (GDrive *drive)
306 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
308 iface = G_DRIVE_GET_IFACE (drive);
310 return (* iface->has_media) (drive);
317 * Checks if a drive can be ejected.
319 * Returns: %TRUE if the @drive can be ejected, %FALSE otherwise.
322 g_drive_can_eject (GDrive *drive)
326 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
328 iface = G_DRIVE_GET_IFACE (drive);
330 if (iface->can_eject == NULL)
333 return (* iface->can_eject) (drive);
337 * g_drive_can_poll_for_media:
340 * Checks if a drive can be polled for media changes.
342 * Returns: %TRUE if the @drive can be polled for media changes,
346 g_drive_can_poll_for_media (GDrive *drive)
350 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
352 iface = G_DRIVE_GET_IFACE (drive);
354 if (iface->poll_for_media == NULL)
357 return (* iface->can_poll_for_media) (drive);
363 * @flags: flags affecting the unmount if required for eject
364 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
365 * @callback: (allow-none): a #GAsyncReadyCallback, or %NULL.
366 * @user_data: user data to pass to @callback
368 * Asynchronously ejects a drive.
370 * When the operation is finished, @callback will be called.
371 * You can then call g_drive_eject_finish() to obtain the
372 * result of the operation.
374 * Deprecated: 2.22: Use g_drive_eject_with_operation() instead.
377 g_drive_eject (GDrive *drive,
378 GMountUnmountFlags flags,
379 GCancellable *cancellable,
380 GAsyncReadyCallback callback,
385 g_return_if_fail (G_IS_DRIVE (drive));
387 iface = G_DRIVE_GET_IFACE (drive);
389 if (iface->eject == NULL)
391 g_simple_async_report_error_in_idle (G_OBJECT (drive), callback, user_data,
392 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
393 _("drive doesn't implement eject"));
398 (* iface->eject) (drive, flags, cancellable, callback, user_data);
402 * g_drive_eject_finish:
404 * @result: a #GAsyncResult.
405 * @error: a #GError, or %NULL
407 * Finishes ejecting a drive.
409 * Returns: %TRUE if the drive has been ejected successfully,
412 * Deprecated: 2.22: Use g_drive_eject_with_operation_finish() instead.
415 g_drive_eject_finish (GDrive *drive,
416 GAsyncResult *result,
421 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
422 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
424 if (g_async_result_legacy_propagate_error (result, error))
427 iface = G_DRIVE_GET_IFACE (drive);
429 return (* iface->eject_finish) (drive, result, error);
433 * g_drive_eject_with_operation:
435 * @flags: flags affecting the unmount if required for eject
436 * @mount_operation: (allow-none): a #GMountOperation or %NULL to avoid
438 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
439 * @callback: (allow-none): a #GAsyncReadyCallback, or %NULL.
440 * @user_data: user data passed to @callback.
442 * Ejects a drive. This is an asynchronous operation, and is
443 * finished by calling g_drive_eject_with_operation_finish() with the @drive
444 * and #GAsyncResult data returned in the @callback.
449 g_drive_eject_with_operation (GDrive *drive,
450 GMountUnmountFlags flags,
451 GMountOperation *mount_operation,
452 GCancellable *cancellable,
453 GAsyncReadyCallback callback,
458 g_return_if_fail (G_IS_DRIVE (drive));
460 iface = G_DRIVE_GET_IFACE (drive);
462 if (iface->eject == NULL && iface->eject_with_operation == NULL)
464 g_simple_async_report_error_in_idle (G_OBJECT (drive),
466 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
467 /* Translators: This is an error
468 * message for drive objects that
469 * don't implement any of eject or eject_with_operation. */
470 _("drive doesn't implement eject or eject_with_operation"));
474 if (iface->eject_with_operation != NULL)
475 (* iface->eject_with_operation) (drive, flags, mount_operation, cancellable, callback, user_data);
477 (* iface->eject) (drive, flags, cancellable, callback, user_data);
481 * g_drive_eject_with_operation_finish:
483 * @result: a #GAsyncResult.
484 * @error: a #GError location to store the error occurring, or %NULL to
487 * Finishes ejecting a drive. If any errors occurred during the operation,
488 * @error will be set to contain the errors and %FALSE will be returned.
490 * Returns: %TRUE if the drive was successfully ejected. %FALSE otherwise.
495 g_drive_eject_with_operation_finish (GDrive *drive,
496 GAsyncResult *result,
501 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
502 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
504 if (g_async_result_legacy_propagate_error (result, error))
507 iface = G_DRIVE_GET_IFACE (drive);
508 if (iface->eject_with_operation_finish != NULL)
509 return (* iface->eject_with_operation_finish) (drive, result, error);
511 return (* iface->eject_finish) (drive, result, error);
515 * g_drive_poll_for_media:
517 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
518 * @callback: (allow-none): a #GAsyncReadyCallback, or %NULL.
519 * @user_data: user data to pass to @callback
521 * Asynchronously polls @drive to see if media has been inserted or removed.
523 * When the operation is finished, @callback will be called.
524 * You can then call g_drive_poll_for_media_finish() to obtain the
525 * result of the operation.
528 g_drive_poll_for_media (GDrive *drive,
529 GCancellable *cancellable,
530 GAsyncReadyCallback callback,
535 g_return_if_fail (G_IS_DRIVE (drive));
537 iface = G_DRIVE_GET_IFACE (drive);
539 if (iface->poll_for_media == NULL)
541 g_simple_async_report_error_in_idle (G_OBJECT (drive), callback, user_data,
542 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
543 _("drive doesn't implement polling for media"));
548 (* iface->poll_for_media) (drive, cancellable, callback, user_data);
552 * g_drive_poll_for_media_finish:
554 * @result: a #GAsyncResult.
555 * @error: a #GError, or %NULL
557 * Finishes an operation started with g_drive_poll_for_media() on a drive.
559 * Returns: %TRUE if the drive has been poll_for_mediaed successfully,
563 g_drive_poll_for_media_finish (GDrive *drive,
564 GAsyncResult *result,
569 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
570 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
572 if (g_async_result_legacy_propagate_error (result, error))
575 iface = G_DRIVE_GET_IFACE (drive);
577 return (* iface->poll_for_media_finish) (drive, result, error);
581 * g_drive_get_identifier:
583 * @kind: the kind of identifier to return
585 * Gets the identifier of the given kind for @drive.
587 * Returns: a newly allocated string containing the
588 * requested identfier, or %NULL if the #GDrive
589 * doesn't have this kind of identifier.
592 g_drive_get_identifier (GDrive *drive,
597 g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
598 g_return_val_if_fail (kind != NULL, NULL);
600 iface = G_DRIVE_GET_IFACE (drive);
602 if (iface->get_identifier == NULL)
605 return (* iface->get_identifier) (drive, kind);
609 * g_drive_enumerate_identifiers:
612 * Gets the kinds of identifiers that @drive has.
613 * Use g_drive_get_identifier() to obtain the identifiers
616 * Returns: (transfer full) (array zero-terminated=1): a %NULL-terminated
617 * array of strings containing kinds of identifiers. Use g_strfreev()
621 g_drive_enumerate_identifiers (GDrive *drive)
625 g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
626 iface = G_DRIVE_GET_IFACE (drive);
628 if (iface->enumerate_identifiers == NULL)
631 return (* iface->enumerate_identifiers) (drive);
635 * g_drive_get_start_stop_type:
638 * Gets a hint about how a drive can be started/stopped.
640 * Returns: A value from the #GDriveStartStopType enumeration.
645 g_drive_get_start_stop_type (GDrive *drive)
649 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
651 iface = G_DRIVE_GET_IFACE (drive);
653 if (iface->get_start_stop_type == NULL)
654 return G_DRIVE_START_STOP_TYPE_UNKNOWN;
656 return (* iface->get_start_stop_type) (drive);
664 * Checks if a drive can be started.
666 * Returns: %TRUE if the @drive can be started, %FALSE otherwise.
671 g_drive_can_start (GDrive *drive)
675 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
677 iface = G_DRIVE_GET_IFACE (drive);
679 if (iface->can_start == NULL)
682 return (* iface->can_start) (drive);
686 * g_drive_can_start_degraded:
689 * Checks if a drive can be started degraded.
691 * Returns: %TRUE if the @drive can be started degraded, %FALSE otherwise.
696 g_drive_can_start_degraded (GDrive *drive)
700 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
702 iface = G_DRIVE_GET_IFACE (drive);
704 if (iface->can_start_degraded == NULL)
707 return (* iface->can_start_degraded) (drive);
713 * @flags: flags affecting the start operation.
714 * @mount_operation: (allow-none): a #GMountOperation or %NULL to avoid
716 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
717 * @callback: (allow-none): a #GAsyncReadyCallback, or %NULL.
718 * @user_data: user data to pass to @callback
720 * Asynchronously starts a drive.
722 * When the operation is finished, @callback will be called.
723 * You can then call g_drive_start_finish() to obtain the
724 * result of the operation.
729 g_drive_start (GDrive *drive,
730 GDriveStartFlags flags,
731 GMountOperation *mount_operation,
732 GCancellable *cancellable,
733 GAsyncReadyCallback callback,
738 g_return_if_fail (G_IS_DRIVE (drive));
740 iface = G_DRIVE_GET_IFACE (drive);
742 if (iface->start == NULL)
744 g_simple_async_report_error_in_idle (G_OBJECT (drive), callback, user_data,
745 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
746 _("drive doesn't implement start"));
750 (* iface->start) (drive, flags, mount_operation, cancellable, callback, user_data);
754 * g_drive_start_finish:
756 * @result: a #GAsyncResult.
757 * @error: a #GError, or %NULL
759 * Finishes starting a drive.
761 * Returns: %TRUE if the drive has been started successfully,
767 g_drive_start_finish (GDrive *drive,
768 GAsyncResult *result,
773 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
774 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
776 if (g_async_result_legacy_propagate_error (result, error))
779 iface = G_DRIVE_GET_IFACE (drive);
781 return (* iface->start_finish) (drive, result, error);
788 * Checks if a drive can be stopped.
790 * Returns: %TRUE if the @drive can be stopped, %FALSE otherwise.
795 g_drive_can_stop (GDrive *drive)
799 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
801 iface = G_DRIVE_GET_IFACE (drive);
803 if (iface->can_stop == NULL)
806 return (* iface->can_stop) (drive);
812 * @flags: flags affecting the unmount if required for stopping.
813 * @mount_operation: (allow-none): a #GMountOperation or %NULL to avoid
815 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
816 * @callback: (allow-none): a #GAsyncReadyCallback, or %NULL.
817 * @user_data: user data to pass to @callback
819 * Asynchronously stops a drive.
821 * When the operation is finished, @callback will be called.
822 * You can then call g_drive_stop_finish() to obtain the
823 * result of the operation.
828 g_drive_stop (GDrive *drive,
829 GMountUnmountFlags flags,
830 GMountOperation *mount_operation,
831 GCancellable *cancellable,
832 GAsyncReadyCallback callback,
837 g_return_if_fail (G_IS_DRIVE (drive));
839 iface = G_DRIVE_GET_IFACE (drive);
841 if (iface->stop == NULL)
843 g_simple_async_report_error_in_idle (G_OBJECT (drive), callback, user_data,
844 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
845 _("drive doesn't implement stop"));
849 (* iface->stop) (drive, flags, mount_operation, cancellable, callback, user_data);
853 * g_drive_stop_finish:
855 * @result: a #GAsyncResult.
856 * @error: a #GError, or %NULL
858 * Finishes stopping a drive.
860 * Returns: %TRUE if the drive has been stopped successfully,
866 g_drive_stop_finish (GDrive *drive,
867 GAsyncResult *result,
872 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
873 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
875 if (g_async_result_legacy_propagate_error (result, error))
878 iface = G_DRIVE_GET_IFACE (drive);
880 return (* iface->stop_finish) (drive, result, error);
884 * g_drive_get_sort_key:
887 * Gets the sort key for @drive, if any.
889 * Returns: Sorting key for @drive or %NULL if no such key is available.
894 g_drive_get_sort_key (GDrive *drive)
896 const gchar *ret = NULL;
899 g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
901 iface = G_DRIVE_GET_IFACE (drive);
902 if (iface->get_sort_key != NULL)
903 ret = iface->get_sort_key (drive);