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 "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 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_task_report_new_error (drive, callback, user_data,
392 g_drive_eject_with_operation,
393 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
394 _("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))
426 else if (g_async_result_is_tagged (result, g_drive_eject_with_operation))
427 return g_task_propagate_boolean (G_TASK (result), error);
429 iface = G_DRIVE_GET_IFACE (drive);
431 return (* iface->eject_finish) (drive, result, error);
435 * g_drive_eject_with_operation:
437 * @flags: flags affecting the unmount if required for eject
438 * @mount_operation: (allow-none): a #GMountOperation or %NULL to avoid
440 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
441 * @callback: (allow-none): a #GAsyncReadyCallback, or %NULL.
442 * @user_data: user data passed to @callback.
444 * Ejects a drive. This is an asynchronous operation, and is
445 * finished by calling g_drive_eject_with_operation_finish() with the @drive
446 * and #GAsyncResult data returned in the @callback.
451 g_drive_eject_with_operation (GDrive *drive,
452 GMountUnmountFlags flags,
453 GMountOperation *mount_operation,
454 GCancellable *cancellable,
455 GAsyncReadyCallback callback,
460 g_return_if_fail (G_IS_DRIVE (drive));
462 iface = G_DRIVE_GET_IFACE (drive);
464 if (iface->eject == NULL && iface->eject_with_operation == NULL)
466 g_task_report_new_error (drive, callback, user_data,
467 g_drive_eject_with_operation,
468 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
469 /* Translators: This is an error
470 * message for drive objects that
471 * don't implement any of eject or eject_with_operation. */
472 _("drive doesn't implement eject or eject_with_operation"));
476 if (iface->eject_with_operation != NULL)
477 (* iface->eject_with_operation) (drive, flags, mount_operation, cancellable, callback, user_data);
479 (* iface->eject) (drive, flags, cancellable, callback, user_data);
483 * g_drive_eject_with_operation_finish:
485 * @result: a #GAsyncResult.
486 * @error: a #GError location to store the error occurring, or %NULL to
489 * Finishes ejecting a drive. If any errors occurred during the operation,
490 * @error will be set to contain the errors and %FALSE will be returned.
492 * Returns: %TRUE if the drive was successfully ejected. %FALSE otherwise.
497 g_drive_eject_with_operation_finish (GDrive *drive,
498 GAsyncResult *result,
503 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
504 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
506 if (g_async_result_legacy_propagate_error (result, error))
508 else if (g_async_result_is_tagged (result, g_drive_eject_with_operation))
509 return g_task_propagate_boolean (G_TASK (result), error);
511 iface = G_DRIVE_GET_IFACE (drive);
512 if (iface->eject_with_operation_finish != NULL)
513 return (* iface->eject_with_operation_finish) (drive, result, error);
515 return (* iface->eject_finish) (drive, result, error);
519 * g_drive_poll_for_media:
521 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
522 * @callback: (allow-none): a #GAsyncReadyCallback, or %NULL.
523 * @user_data: user data to pass to @callback
525 * Asynchronously polls @drive to see if media has been inserted or removed.
527 * When the operation is finished, @callback will be called.
528 * You can then call g_drive_poll_for_media_finish() to obtain the
529 * result of the operation.
532 g_drive_poll_for_media (GDrive *drive,
533 GCancellable *cancellable,
534 GAsyncReadyCallback callback,
539 g_return_if_fail (G_IS_DRIVE (drive));
541 iface = G_DRIVE_GET_IFACE (drive);
543 if (iface->poll_for_media == NULL)
545 g_task_report_new_error (drive, callback, user_data,
546 g_drive_poll_for_media,
547 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
548 _("drive doesn't implement polling for media"));
552 (* iface->poll_for_media) (drive, cancellable, callback, user_data);
556 * g_drive_poll_for_media_finish:
558 * @result: a #GAsyncResult.
559 * @error: a #GError, or %NULL
561 * Finishes an operation started with g_drive_poll_for_media() on a drive.
563 * Returns: %TRUE if the drive has been poll_for_mediaed successfully,
567 g_drive_poll_for_media_finish (GDrive *drive,
568 GAsyncResult *result,
573 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
574 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
576 if (g_async_result_legacy_propagate_error (result, error))
578 else if (g_async_result_is_tagged (result, g_drive_poll_for_media))
579 return g_task_propagate_boolean (G_TASK (result), error);
581 iface = G_DRIVE_GET_IFACE (drive);
583 return (* iface->poll_for_media_finish) (drive, result, error);
587 * g_drive_get_identifier:
589 * @kind: the kind of identifier to return
591 * Gets the identifier of the given kind for @drive.
593 * Returns: a newly allocated string containing the
594 * requested identfier, or %NULL if the #GDrive
595 * doesn't have this kind of identifier.
598 g_drive_get_identifier (GDrive *drive,
603 g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
604 g_return_val_if_fail (kind != NULL, NULL);
606 iface = G_DRIVE_GET_IFACE (drive);
608 if (iface->get_identifier == NULL)
611 return (* iface->get_identifier) (drive, kind);
615 * g_drive_enumerate_identifiers:
618 * Gets the kinds of identifiers that @drive has.
619 * Use g_drive_get_identifier() to obtain the identifiers
622 * Returns: (transfer full) (array zero-terminated=1): a %NULL-terminated
623 * array of strings containing kinds of identifiers. Use g_strfreev()
627 g_drive_enumerate_identifiers (GDrive *drive)
631 g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
632 iface = G_DRIVE_GET_IFACE (drive);
634 if (iface->enumerate_identifiers == NULL)
637 return (* iface->enumerate_identifiers) (drive);
641 * g_drive_get_start_stop_type:
644 * Gets a hint about how a drive can be started/stopped.
646 * Returns: A value from the #GDriveStartStopType enumeration.
651 g_drive_get_start_stop_type (GDrive *drive)
655 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
657 iface = G_DRIVE_GET_IFACE (drive);
659 if (iface->get_start_stop_type == NULL)
660 return G_DRIVE_START_STOP_TYPE_UNKNOWN;
662 return (* iface->get_start_stop_type) (drive);
670 * Checks if a drive can be started.
672 * Returns: %TRUE if the @drive can be started, %FALSE otherwise.
677 g_drive_can_start (GDrive *drive)
681 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
683 iface = G_DRIVE_GET_IFACE (drive);
685 if (iface->can_start == NULL)
688 return (* iface->can_start) (drive);
692 * g_drive_can_start_degraded:
695 * Checks if a drive can be started degraded.
697 * Returns: %TRUE if the @drive can be started degraded, %FALSE otherwise.
702 g_drive_can_start_degraded (GDrive *drive)
706 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
708 iface = G_DRIVE_GET_IFACE (drive);
710 if (iface->can_start_degraded == NULL)
713 return (* iface->can_start_degraded) (drive);
719 * @flags: flags affecting the start operation.
720 * @mount_operation: (allow-none): a #GMountOperation or %NULL to avoid
722 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
723 * @callback: (allow-none): a #GAsyncReadyCallback, or %NULL.
724 * @user_data: user data to pass to @callback
726 * Asynchronously starts a drive.
728 * When the operation is finished, @callback will be called.
729 * You can then call g_drive_start_finish() to obtain the
730 * result of the operation.
735 g_drive_start (GDrive *drive,
736 GDriveStartFlags flags,
737 GMountOperation *mount_operation,
738 GCancellable *cancellable,
739 GAsyncReadyCallback callback,
744 g_return_if_fail (G_IS_DRIVE (drive));
746 iface = G_DRIVE_GET_IFACE (drive);
748 if (iface->start == NULL)
750 g_task_report_new_error (drive, callback, user_data,
752 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
753 _("drive doesn't implement start"));
757 (* iface->start) (drive, flags, mount_operation, cancellable, callback, user_data);
761 * g_drive_start_finish:
763 * @result: a #GAsyncResult.
764 * @error: a #GError, or %NULL
766 * Finishes starting a drive.
768 * Returns: %TRUE if the drive has been started successfully,
774 g_drive_start_finish (GDrive *drive,
775 GAsyncResult *result,
780 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
781 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
783 if (g_async_result_legacy_propagate_error (result, error))
785 else if (g_async_result_is_tagged (result, g_drive_start))
786 return g_task_propagate_boolean (G_TASK (result), error);
788 iface = G_DRIVE_GET_IFACE (drive);
790 return (* iface->start_finish) (drive, result, error);
797 * Checks if a drive can be stopped.
799 * Returns: %TRUE if the @drive can be stopped, %FALSE otherwise.
804 g_drive_can_stop (GDrive *drive)
808 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
810 iface = G_DRIVE_GET_IFACE (drive);
812 if (iface->can_stop == NULL)
815 return (* iface->can_stop) (drive);
821 * @flags: flags affecting the unmount if required for stopping.
822 * @mount_operation: (allow-none): a #GMountOperation or %NULL to avoid
824 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
825 * @callback: (allow-none): a #GAsyncReadyCallback, or %NULL.
826 * @user_data: user data to pass to @callback
828 * Asynchronously stops a drive.
830 * When the operation is finished, @callback will be called.
831 * You can then call g_drive_stop_finish() to obtain the
832 * result of the operation.
837 g_drive_stop (GDrive *drive,
838 GMountUnmountFlags flags,
839 GMountOperation *mount_operation,
840 GCancellable *cancellable,
841 GAsyncReadyCallback callback,
846 g_return_if_fail (G_IS_DRIVE (drive));
848 iface = G_DRIVE_GET_IFACE (drive);
850 if (iface->stop == NULL)
852 g_task_report_new_error (drive, callback, user_data,
854 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
855 _("drive doesn't implement stop"));
859 (* iface->stop) (drive, flags, mount_operation, cancellable, callback, user_data);
863 * g_drive_stop_finish:
865 * @result: a #GAsyncResult.
866 * @error: a #GError, or %NULL
868 * Finishes stopping a drive.
870 * Returns: %TRUE if the drive has been stopped successfully,
876 g_drive_stop_finish (GDrive *drive,
877 GAsyncResult *result,
882 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
883 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
885 if (g_async_result_legacy_propagate_error (result, error))
887 else if (g_async_result_is_tagged (result, g_drive_start))
888 return g_task_propagate_boolean (G_TASK (result), error);
890 iface = G_DRIVE_GET_IFACE (drive);
892 return (* iface->stop_finish) (drive, result, error);
896 * g_drive_get_sort_key:
899 * Gets the sort key for @drive, if any.
901 * Returns: Sorting key for @drive or %NULL if no such key is available.
906 g_drive_get_sort_key (GDrive *drive)
908 const gchar *ret = NULL;
911 g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
913 iface = G_DRIVE_GET_IFACE (drive);
914 if (iface->get_sort_key != NULL)
915 ret = iface->get_sort_key (drive);