1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Author: Alexander Larsson <alexl@redhat.com>
21 * David Zeuthen <davidz@redhat.com>
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, determining 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 potentially 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),
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),
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),
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),
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 capable of automatically detecting media changes.
256 * Returns: %TRUE if the @drive is capable 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_removable:
275 * Checks if the #GDrive and/or its media is considered removable by the user.
276 * See g_drive_is_media_removable().
278 * Returns: %TRUE if @drive and/or its media is considered removable, %FALSE otherwise.
283 g_drive_is_removable (GDrive *drive)
287 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
289 iface = G_DRIVE_GET_IFACE (drive);
290 if (iface->is_removable != NULL)
291 return iface->is_removable (drive);
297 * g_drive_is_media_removable:
300 * Checks if the @drive supports removable media.
302 * Returns: %TRUE if @drive supports removable media, %FALSE otherwise.
305 g_drive_is_media_removable (GDrive *drive)
309 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
311 iface = G_DRIVE_GET_IFACE (drive);
313 return (* iface->is_media_removable) (drive);
320 * Checks if the @drive has media. Note that the OS may not be polling
321 * the drive for media changes; see g_drive_is_media_check_automatic()
324 * Returns: %TRUE if @drive has media, %FALSE otherwise.
327 g_drive_has_media (GDrive *drive)
331 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
333 iface = G_DRIVE_GET_IFACE (drive);
335 return (* iface->has_media) (drive);
342 * Checks if a drive can be ejected.
344 * Returns: %TRUE if the @drive can be ejected, %FALSE otherwise.
347 g_drive_can_eject (GDrive *drive)
351 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
353 iface = G_DRIVE_GET_IFACE (drive);
355 if (iface->can_eject == NULL)
358 return (* iface->can_eject) (drive);
362 * g_drive_can_poll_for_media:
365 * Checks if a drive can be polled for media changes.
367 * Returns: %TRUE if the @drive can be polled for media changes,
371 g_drive_can_poll_for_media (GDrive *drive)
375 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
377 iface = G_DRIVE_GET_IFACE (drive);
379 if (iface->poll_for_media == NULL)
382 return (* iface->can_poll_for_media) (drive);
388 * @flags: flags affecting the unmount if required for eject
389 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
390 * @callback: (nullable): a #GAsyncReadyCallback, or %NULL.
391 * @user_data: user data to pass to @callback
393 * Asynchronously ejects a drive.
395 * When the operation is finished, @callback will be called.
396 * You can then call g_drive_eject_finish() to obtain the
397 * result of the operation.
399 * Deprecated: 2.22: Use g_drive_eject_with_operation() instead.
402 g_drive_eject (GDrive *drive,
403 GMountUnmountFlags flags,
404 GCancellable *cancellable,
405 GAsyncReadyCallback callback,
410 g_return_if_fail (G_IS_DRIVE (drive));
412 iface = G_DRIVE_GET_IFACE (drive);
414 if (iface->eject == NULL)
416 g_task_report_new_error (drive, callback, user_data,
417 g_drive_eject_with_operation,
418 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
419 _("drive doesn’t implement eject"));
423 (* iface->eject) (drive, flags, cancellable, callback, user_data);
427 * g_drive_eject_finish:
429 * @result: a #GAsyncResult.
430 * @error: a #GError, or %NULL
432 * Finishes ejecting a drive.
434 * Returns: %TRUE if the drive has been ejected successfully,
437 * Deprecated: 2.22: Use g_drive_eject_with_operation_finish() instead.
440 g_drive_eject_finish (GDrive *drive,
441 GAsyncResult *result,
446 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
447 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
449 if (g_async_result_legacy_propagate_error (result, error))
451 else if (g_async_result_is_tagged (result, g_drive_eject_with_operation))
452 return g_task_propagate_boolean (G_TASK (result), error);
454 iface = G_DRIVE_GET_IFACE (drive);
456 return (* iface->eject_finish) (drive, result, error);
460 * g_drive_eject_with_operation:
462 * @flags: flags affecting the unmount if required for eject
463 * @mount_operation: (nullable): a #GMountOperation or %NULL to avoid
465 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
466 * @callback: (nullable): a #GAsyncReadyCallback, or %NULL.
467 * @user_data: user data passed to @callback.
469 * Ejects a drive. This is an asynchronous operation, and is
470 * finished by calling g_drive_eject_with_operation_finish() with the @drive
471 * and #GAsyncResult data returned in the @callback.
476 g_drive_eject_with_operation (GDrive *drive,
477 GMountUnmountFlags flags,
478 GMountOperation *mount_operation,
479 GCancellable *cancellable,
480 GAsyncReadyCallback callback,
485 g_return_if_fail (G_IS_DRIVE (drive));
487 iface = G_DRIVE_GET_IFACE (drive);
489 if (iface->eject == NULL && iface->eject_with_operation == NULL)
491 g_task_report_new_error (drive, callback, user_data,
492 g_drive_eject_with_operation,
493 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
494 /* Translators: This is an error
495 * message for drive objects that
496 * don't implement any of eject or eject_with_operation. */
497 _("drive doesn’t implement eject or eject_with_operation"));
501 if (iface->eject_with_operation != NULL)
502 (* iface->eject_with_operation) (drive, flags, mount_operation, cancellable, callback, user_data);
504 (* iface->eject) (drive, flags, cancellable, callback, user_data);
508 * g_drive_eject_with_operation_finish:
510 * @result: a #GAsyncResult.
511 * @error: a #GError location to store the error occurring, or %NULL to
514 * Finishes ejecting a drive. If any errors occurred during the operation,
515 * @error will be set to contain the errors and %FALSE will be returned.
517 * Returns: %TRUE if the drive was successfully ejected. %FALSE otherwise.
522 g_drive_eject_with_operation_finish (GDrive *drive,
523 GAsyncResult *result,
528 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
529 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
531 if (g_async_result_legacy_propagate_error (result, error))
533 else if (g_async_result_is_tagged (result, g_drive_eject_with_operation))
534 return g_task_propagate_boolean (G_TASK (result), error);
536 iface = G_DRIVE_GET_IFACE (drive);
537 if (iface->eject_with_operation_finish != NULL)
538 return (* iface->eject_with_operation_finish) (drive, result, error);
540 return (* iface->eject_finish) (drive, result, error);
544 * g_drive_poll_for_media:
546 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
547 * @callback: (nullable): a #GAsyncReadyCallback, or %NULL.
548 * @user_data: user data to pass to @callback
550 * Asynchronously polls @drive to see if media has been inserted or removed.
552 * When the operation is finished, @callback will be called.
553 * You can then call g_drive_poll_for_media_finish() to obtain the
554 * result of the operation.
557 g_drive_poll_for_media (GDrive *drive,
558 GCancellable *cancellable,
559 GAsyncReadyCallback callback,
564 g_return_if_fail (G_IS_DRIVE (drive));
566 iface = G_DRIVE_GET_IFACE (drive);
568 if (iface->poll_for_media == NULL)
570 g_task_report_new_error (drive, callback, user_data,
571 g_drive_poll_for_media,
572 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
573 _("drive doesn’t implement polling for media"));
577 (* iface->poll_for_media) (drive, cancellable, callback, user_data);
581 * g_drive_poll_for_media_finish:
583 * @result: a #GAsyncResult.
584 * @error: a #GError, or %NULL
586 * Finishes an operation started with g_drive_poll_for_media() on a drive.
588 * Returns: %TRUE if the drive has been poll_for_mediaed successfully,
592 g_drive_poll_for_media_finish (GDrive *drive,
593 GAsyncResult *result,
598 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
599 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
601 if (g_async_result_legacy_propagate_error (result, error))
603 else if (g_async_result_is_tagged (result, g_drive_poll_for_media))
604 return g_task_propagate_boolean (G_TASK (result), error);
606 iface = G_DRIVE_GET_IFACE (drive);
608 return (* iface->poll_for_media_finish) (drive, result, error);
612 * g_drive_get_identifier:
614 * @kind: the kind of identifier to return
616 * Gets the identifier of the given kind for @drive. The only
617 * identifier currently available is
618 * %G_DRIVE_IDENTIFIER_KIND_UNIX_DEVICE.
620 * Returns: (nullable) (transfer full): a newly allocated string containing the
621 * requested identifier, or %NULL if the #GDrive
622 * doesn't have this kind of identifier.
625 g_drive_get_identifier (GDrive *drive,
630 g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
631 g_return_val_if_fail (kind != NULL, NULL);
633 iface = G_DRIVE_GET_IFACE (drive);
635 if (iface->get_identifier == NULL)
638 return (* iface->get_identifier) (drive, kind);
642 * g_drive_enumerate_identifiers:
645 * Gets the kinds of identifiers that @drive has.
646 * Use g_drive_get_identifier() to obtain the identifiers
649 * Returns: (transfer full) (array zero-terminated=1): a %NULL-terminated
650 * array of strings containing kinds of identifiers. Use g_strfreev()
654 g_drive_enumerate_identifiers (GDrive *drive)
658 g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
659 iface = G_DRIVE_GET_IFACE (drive);
661 if (iface->enumerate_identifiers == NULL)
664 return (* iface->enumerate_identifiers) (drive);
668 * g_drive_get_start_stop_type:
671 * Gets a hint about how a drive can be started/stopped.
673 * Returns: A value from the #GDriveStartStopType enumeration.
678 g_drive_get_start_stop_type (GDrive *drive)
682 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
684 iface = G_DRIVE_GET_IFACE (drive);
686 if (iface->get_start_stop_type == NULL)
687 return G_DRIVE_START_STOP_TYPE_UNKNOWN;
689 return (* iface->get_start_stop_type) (drive);
697 * Checks if a drive can be started.
699 * Returns: %TRUE if the @drive can be started, %FALSE otherwise.
704 g_drive_can_start (GDrive *drive)
708 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
710 iface = G_DRIVE_GET_IFACE (drive);
712 if (iface->can_start == NULL)
715 return (* iface->can_start) (drive);
719 * g_drive_can_start_degraded:
722 * Checks if a drive can be started degraded.
724 * Returns: %TRUE if the @drive can be started degraded, %FALSE otherwise.
729 g_drive_can_start_degraded (GDrive *drive)
733 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
735 iface = G_DRIVE_GET_IFACE (drive);
737 if (iface->can_start_degraded == NULL)
740 return (* iface->can_start_degraded) (drive);
746 * @flags: flags affecting the start operation.
747 * @mount_operation: (nullable): a #GMountOperation or %NULL to avoid
749 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
750 * @callback: (nullable): a #GAsyncReadyCallback, or %NULL.
751 * @user_data: user data to pass to @callback
753 * Asynchronously starts a drive.
755 * When the operation is finished, @callback will be called.
756 * You can then call g_drive_start_finish() to obtain the
757 * result of the operation.
762 g_drive_start (GDrive *drive,
763 GDriveStartFlags flags,
764 GMountOperation *mount_operation,
765 GCancellable *cancellable,
766 GAsyncReadyCallback callback,
771 g_return_if_fail (G_IS_DRIVE (drive));
773 iface = G_DRIVE_GET_IFACE (drive);
775 if (iface->start == NULL)
777 g_task_report_new_error (drive, callback, user_data,
779 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
780 _("drive doesn’t implement start"));
784 (* iface->start) (drive, flags, mount_operation, cancellable, callback, user_data);
788 * g_drive_start_finish:
790 * @result: a #GAsyncResult.
791 * @error: a #GError, or %NULL
793 * Finishes starting a drive.
795 * Returns: %TRUE if the drive has been started successfully,
801 g_drive_start_finish (GDrive *drive,
802 GAsyncResult *result,
807 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
808 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
810 if (g_async_result_legacy_propagate_error (result, error))
812 else if (g_async_result_is_tagged (result, g_drive_start))
813 return g_task_propagate_boolean (G_TASK (result), error);
815 iface = G_DRIVE_GET_IFACE (drive);
817 return (* iface->start_finish) (drive, result, error);
824 * Checks if a drive can be stopped.
826 * Returns: %TRUE if the @drive can be stopped, %FALSE otherwise.
831 g_drive_can_stop (GDrive *drive)
835 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
837 iface = G_DRIVE_GET_IFACE (drive);
839 if (iface->can_stop == NULL)
842 return (* iface->can_stop) (drive);
848 * @flags: flags affecting the unmount if required for stopping.
849 * @mount_operation: (nullable): a #GMountOperation or %NULL to avoid
851 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
852 * @callback: (nullable): a #GAsyncReadyCallback, or %NULL.
853 * @user_data: user data to pass to @callback
855 * Asynchronously stops a drive.
857 * When the operation is finished, @callback will be called.
858 * You can then call g_drive_stop_finish() to obtain the
859 * result of the operation.
864 g_drive_stop (GDrive *drive,
865 GMountUnmountFlags flags,
866 GMountOperation *mount_operation,
867 GCancellable *cancellable,
868 GAsyncReadyCallback callback,
873 g_return_if_fail (G_IS_DRIVE (drive));
875 iface = G_DRIVE_GET_IFACE (drive);
877 if (iface->stop == NULL)
879 g_task_report_new_error (drive, callback, user_data,
881 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
882 _("drive doesn’t implement stop"));
886 (* iface->stop) (drive, flags, mount_operation, cancellable, callback, user_data);
890 * g_drive_stop_finish:
892 * @result: a #GAsyncResult.
893 * @error: a #GError, or %NULL
895 * Finishes stopping a drive.
897 * Returns: %TRUE if the drive has been stopped successfully,
903 g_drive_stop_finish (GDrive *drive,
904 GAsyncResult *result,
909 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
910 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
912 if (g_async_result_legacy_propagate_error (result, error))
914 else if (g_async_result_is_tagged (result, g_drive_start))
915 return g_task_propagate_boolean (G_TASK (result), error);
917 iface = G_DRIVE_GET_IFACE (drive);
919 return (* iface->stop_finish) (drive, result, error);
923 * g_drive_get_sort_key:
926 * Gets the sort key for @drive, if any.
928 * Returns: (nullable): Sorting key for @drive or %NULL if no such key is available.
933 g_drive_get_sort_key (GDrive *drive)
935 const gchar *ret = NULL;
938 g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
940 iface = G_DRIVE_GET_IFACE (drive);
941 if (iface->get_sort_key != NULL)
942 ret = iface->get_sort_key (drive);