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 "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 static void g_drive_base_init (gpointer g_class);
67 static void g_drive_class_init (gpointer g_class,
71 g_drive_get_type (void)
73 static volatile gsize g_define_type_id__volatile = 0;
75 if (g_once_init_enter (&g_define_type_id__volatile))
77 const GTypeInfo drive_info =
79 sizeof (GDriveIface), /* class_size */
80 g_drive_base_init, /* base_init */
81 NULL, /* base_finalize */
83 NULL, /* class_finalize */
84 NULL, /* class_data */
89 GType g_define_type_id =
90 g_type_register_static (G_TYPE_INTERFACE, I_("GDrive"),
93 g_type_interface_add_prerequisite (g_define_type_id, G_TYPE_OBJECT);
95 g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
98 return g_define_type_id__volatile;
102 g_drive_class_init (gpointer g_class,
108 g_drive_base_init (gpointer g_class)
110 static gboolean initialized = FALSE;
118 * Emitted when the drive's state has changed.
120 g_signal_new (I_("changed"),
123 G_STRUCT_OFFSET (GDriveIface, changed),
125 g_cclosure_marshal_VOID__VOID,
129 * GDrive::disconnected:
132 * This signal is emitted when the #GDrive have been
133 * disconnected. If the recipient is holding references to the
134 * object they should release them so the object can be
137 g_signal_new (I_("disconnected"),
140 G_STRUCT_OFFSET (GDriveIface, disconnected),
142 g_cclosure_marshal_VOID__VOID,
146 * GDrive::eject-button:
149 * Emitted when the physical eject button (if any) of a drive has
152 g_signal_new (I_("eject-button"),
155 G_STRUCT_OFFSET (GDriveIface, eject_button),
157 g_cclosure_marshal_VOID__VOID,
161 * GDrive::stop-button:
164 * Emitted when the physical stop button (if any) of a drive has
169 g_signal_new (I_("stop-button"),
172 G_STRUCT_OFFSET (GDriveIface, stop_button),
174 g_cclosure_marshal_VOID__VOID,
185 * Gets the name of @drive.
187 * Returns: a string containing @drive's name. The returned
188 * string should be freed when no longer needed.
191 g_drive_get_name (GDrive *drive)
195 g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
197 iface = G_DRIVE_GET_IFACE (drive);
199 return (* iface->get_name) (drive);
206 * Gets the icon for @drive.
208 * Returns: #GIcon for the @drive.
209 * Free the returned object with g_object_unref().
212 g_drive_get_icon (GDrive *drive)
216 g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
218 iface = G_DRIVE_GET_IFACE (drive);
220 return (* iface->get_icon) (drive);
224 * g_drive_has_volumes:
227 * Check if @drive has any mountable volumes.
229 * Returns: %TRUE if the @drive contains volumes, %FALSE otherwise.
232 g_drive_has_volumes (GDrive *drive)
236 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
238 iface = G_DRIVE_GET_IFACE (drive);
240 return (* iface->has_volumes) (drive);
244 * g_drive_get_volumes:
247 * Get a list of mountable volumes for @drive.
249 * The returned list should be freed with g_list_free(), after
250 * its elements have been unreffed with g_object_unref().
252 * Returns: #GList containing any #GVolume objects on the given @drive.
255 g_drive_get_volumes (GDrive *drive)
259 g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
261 iface = G_DRIVE_GET_IFACE (drive);
263 return (* iface->get_volumes) (drive);
267 * g_drive_is_media_check_automatic:
270 * Checks if @drive is capabable of automatically detecting media changes.
272 * Returns: %TRUE if the @drive is capabable of automatically detecting
273 * media changes, %FALSE otherwise.
276 g_drive_is_media_check_automatic (GDrive *drive)
280 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
282 iface = G_DRIVE_GET_IFACE (drive);
284 return (* iface->is_media_check_automatic) (drive);
288 * g_drive_is_media_removable:
291 * Checks if the @drive supports removable media.
293 * Returns: %TRUE if @drive supports removable media, %FALSE otherwise.
296 g_drive_is_media_removable (GDrive *drive)
300 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
302 iface = G_DRIVE_GET_IFACE (drive);
304 return (* iface->is_media_removable) (drive);
311 * Checks if the @drive has media. Note that the OS may not be polling
312 * the drive for media changes; see g_drive_is_media_check_automatic()
315 * Returns: %TRUE if @drive has media, %FALSE otherwise.
318 g_drive_has_media (GDrive *drive)
322 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
324 iface = G_DRIVE_GET_IFACE (drive);
326 return (* iface->has_media) (drive);
333 * Checks if a drive can be ejected.
335 * Returns: %TRUE if the @drive can be ejected, %FALSE otherwise.
338 g_drive_can_eject (GDrive *drive)
342 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
344 iface = G_DRIVE_GET_IFACE (drive);
346 if (iface->can_eject == NULL)
349 return (* iface->can_eject) (drive);
353 * g_drive_can_poll_for_media:
356 * Checks if a drive can be polled for media changes.
358 * Returns: %TRUE if the @drive can be polled for media changes,
362 g_drive_can_poll_for_media (GDrive *drive)
366 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
368 iface = G_DRIVE_GET_IFACE (drive);
370 if (iface->poll_for_media == NULL)
373 return (* iface->can_poll_for_media) (drive);
379 * @flags: flags affecting the unmount if required for eject
380 * @cancellable: optional #GCancellable object, %NULL to ignore.
381 * @callback: a #GAsyncReadyCallback, or %NULL.
382 * @user_data: user data to pass to @callback
384 * Asynchronously ejects a drive.
386 * When the operation is finished, @callback will be called.
387 * You can then call g_drive_eject_finish() to obtain the
388 * result of the operation.
390 * Deprecated: 2.22: Use g_drive_eject_with_operation() instead.
393 g_drive_eject (GDrive *drive,
394 GMountUnmountFlags flags,
395 GCancellable *cancellable,
396 GAsyncReadyCallback callback,
401 g_return_if_fail (G_IS_DRIVE (drive));
403 iface = G_DRIVE_GET_IFACE (drive);
405 if (iface->eject == NULL)
407 g_simple_async_report_error_in_idle (G_OBJECT (drive), callback, user_data,
408 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
409 _("drive doesn't implement eject"));
414 (* iface->eject) (drive, flags, cancellable, callback, user_data);
418 * g_drive_eject_finish:
420 * @result: a #GAsyncResult.
421 * @error: a #GError, or %NULL
423 * Finishes ejecting a drive.
425 * Returns: %TRUE if the drive has been ejected successfully,
428 * Deprecated: 2.22: Use g_drive_eject_with_operation_finish() instead.
431 g_drive_eject_finish (GDrive *drive,
432 GAsyncResult *result,
437 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
438 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
440 if (G_IS_SIMPLE_ASYNC_RESULT (result))
442 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
443 if (g_simple_async_result_propagate_error (simple, error))
447 iface = G_DRIVE_GET_IFACE (drive);
449 return (* iface->eject_finish) (drive, result, error);
453 * g_drive_eject_with_operation:
455 * @flags: flags affecting the unmount if required for eject
456 * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
457 * @cancellable: optional #GCancellable object, %NULL to ignore.
458 * @callback: a #GAsyncReadyCallback, or %NULL.
459 * @user_data: user data passed to @callback.
461 * Ejects a drive. This is an asynchronous operation, and is
462 * finished by calling g_drive_eject_with_operation_finish() with the @drive
463 * and #GAsyncResult data returned in the @callback.
468 g_drive_eject_with_operation (GDrive *drive,
469 GMountUnmountFlags flags,
470 GMountOperation *mount_operation,
471 GCancellable *cancellable,
472 GAsyncReadyCallback callback,
477 g_return_if_fail (G_IS_DRIVE (drive));
479 iface = G_DRIVE_GET_IFACE (drive);
481 if (iface->eject == NULL && iface->eject_with_operation == NULL)
483 g_simple_async_report_error_in_idle (G_OBJECT (drive),
485 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
486 /* Translators: This is an error
487 * message for drive objects that
488 * don't implement any of eject or eject_with_operation. */
489 _("drive doesn't implement eject or eject_with_operation"));
493 if (iface->eject_with_operation != NULL)
494 (* iface->eject_with_operation) (drive, flags, mount_operation, cancellable, callback, user_data);
496 (* iface->eject) (drive, flags, cancellable, callback, user_data);
500 * g_drive_eject_with_operation_finish:
502 * @result: a #GAsyncResult.
503 * @error: a #GError location to store the error occuring, or %NULL to
506 * Finishes ejecting a drive. If any errors occurred during the operation,
507 * @error will be set to contain the errors and %FALSE will be returned.
509 * Returns: %TRUE if the drive was successfully ejected. %FALSE otherwise.
514 g_drive_eject_with_operation_finish (GDrive *drive,
515 GAsyncResult *result,
520 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
521 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
523 if (G_IS_SIMPLE_ASYNC_RESULT (result))
525 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
526 if (g_simple_async_result_propagate_error (simple, error))
530 iface = G_DRIVE_GET_IFACE (drive);
531 if (iface->eject_with_operation_finish != NULL)
532 return (* iface->eject_with_operation_finish) (drive, result, error);
534 return (* iface->eject_finish) (drive, result, error);
538 * g_drive_poll_for_media:
540 * @cancellable: optional #GCancellable object, %NULL to ignore.
541 * @callback: a #GAsyncReadyCallback, or %NULL.
542 * @user_data: user data to pass to @callback
544 * Asynchronously polls @drive to see if media has been inserted or removed.
546 * When the operation is finished, @callback will be called.
547 * You can then call g_drive_poll_for_media_finish() to obtain the
548 * result of the operation.
551 g_drive_poll_for_media (GDrive *drive,
552 GCancellable *cancellable,
553 GAsyncReadyCallback callback,
558 g_return_if_fail (G_IS_DRIVE (drive));
560 iface = G_DRIVE_GET_IFACE (drive);
562 if (iface->poll_for_media == NULL)
564 g_simple_async_report_error_in_idle (G_OBJECT (drive), callback, user_data,
565 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
566 _("drive doesn't implement polling for media"));
571 (* iface->poll_for_media) (drive, cancellable, callback, user_data);
575 * g_drive_poll_for_media_finish:
577 * @result: a #GAsyncResult.
578 * @error: a #GError, or %NULL
580 * Finishes an operation started with g_drive_poll_for_media() on a drive.
582 * Returns: %TRUE if the drive has been poll_for_mediaed successfully,
586 g_drive_poll_for_media_finish (GDrive *drive,
587 GAsyncResult *result,
592 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
593 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
595 if (G_IS_SIMPLE_ASYNC_RESULT (result))
597 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
598 if (g_simple_async_result_propagate_error (simple, error))
602 iface = G_DRIVE_GET_IFACE (drive);
604 return (* iface->poll_for_media_finish) (drive, result, error);
608 * g_drive_get_identifier:
610 * @kind: the kind of identifier to return
612 * Gets the identifier of the given kind for @drive.
614 * Returns: a newly allocated string containing the
615 * requested identfier, or %NULL if the #GDrive
616 * doesn't have this kind of identifier.
619 g_drive_get_identifier (GDrive *drive,
624 g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
625 g_return_val_if_fail (kind != NULL, NULL);
627 iface = G_DRIVE_GET_IFACE (drive);
629 if (iface->get_identifier == NULL)
632 return (* iface->get_identifier) (drive, kind);
636 * g_drive_enumerate_identifiers:
639 * Gets the kinds of identifiers that @drive has.
640 * Use g_drive_get_identifer() to obtain the identifiers
643 * Returns: a %NULL-terminated array of strings containing
644 * kinds of identifiers. Use g_strfreev() to free.
647 g_drive_enumerate_identifiers (GDrive *drive)
651 g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
652 iface = G_DRIVE_GET_IFACE (drive);
654 if (iface->enumerate_identifiers == NULL)
657 return (* iface->enumerate_identifiers) (drive);
661 * g_drive_get_start_stop_type:
664 * Gets a hint about how a drive can be started/stopped.
666 * Returns: A value from the #GDriveStartStopType enumeration.
671 g_drive_get_start_stop_type (GDrive *drive)
675 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
677 iface = G_DRIVE_GET_IFACE (drive);
679 if (iface->get_start_stop_type == NULL)
680 return G_DRIVE_START_STOP_TYPE_UNKNOWN;
682 return (* iface->get_start_stop_type) (drive);
690 * Checks if a drive can be started.
692 * Returns: %TRUE if the @drive can be started, %FALSE otherwise.
697 g_drive_can_start (GDrive *drive)
701 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
703 iface = G_DRIVE_GET_IFACE (drive);
705 if (iface->can_start == NULL)
708 return (* iface->can_start) (drive);
712 * g_drive_can_start_degraded:
715 * Checks if a drive can be started degraded.
717 * Returns: %TRUE if the @drive can be started degraded, %FALSE otherwise.
722 g_drive_can_start_degraded (GDrive *drive)
726 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
728 iface = G_DRIVE_GET_IFACE (drive);
730 if (iface->can_start_degraded == NULL)
733 return (* iface->can_start_degraded) (drive);
739 * @flags: flags affecting the start operation.
740 * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
741 * @cancellable: optional #GCancellable object, %NULL to ignore.
742 * @callback: a #GAsyncReadyCallback, or %NULL.
743 * @user_data: user data to pass to @callback
745 * Asynchronously starts a drive.
747 * When the operation is finished, @callback will be called.
748 * You can then call g_drive_start_finish() to obtain the
749 * result of the operation.
754 g_drive_start (GDrive *drive,
755 GDriveStartFlags flags,
756 GMountOperation *mount_operation,
757 GCancellable *cancellable,
758 GAsyncReadyCallback callback,
763 g_return_if_fail (G_IS_DRIVE (drive));
765 iface = G_DRIVE_GET_IFACE (drive);
767 if (iface->start == NULL)
769 g_simple_async_report_error_in_idle (G_OBJECT (drive), callback, user_data,
770 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
771 _("drive doesn't implement start"));
775 (* iface->start) (drive, flags, mount_operation, cancellable, callback, user_data);
779 * g_drive_start_finish:
781 * @result: a #GAsyncResult.
782 * @error: a #GError, or %NULL
784 * Finishes starting a drive.
786 * Returns: %TRUE if the drive has been started successfully,
792 g_drive_start_finish (GDrive *drive,
793 GAsyncResult *result,
798 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
799 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
801 if (G_IS_SIMPLE_ASYNC_RESULT (result))
803 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
804 if (g_simple_async_result_propagate_error (simple, error))
808 iface = G_DRIVE_GET_IFACE (drive);
810 return (* iface->start_finish) (drive, result, error);
817 * Checks if a drive can be stopped.
819 * Returns: %TRUE if the @drive can be stopped, %FALSE otherwise.
824 g_drive_can_stop (GDrive *drive)
828 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
830 iface = G_DRIVE_GET_IFACE (drive);
832 if (iface->can_stop == NULL)
835 return (* iface->can_stop) (drive);
841 * @flags: flags affecting the unmount if required for stopping.
842 * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
843 * @cancellable: optional #GCancellable object, %NULL to ignore.
844 * @callback: a #GAsyncReadyCallback, or %NULL.
845 * @user_data: user data to pass to @callback
847 * Asynchronously stops a drive.
849 * When the operation is finished, @callback will be called.
850 * You can then call g_drive_stop_finish() to obtain the
851 * result of the operation.
856 g_drive_stop (GDrive *drive,
857 GMountUnmountFlags flags,
858 GMountOperation *mount_operation,
859 GCancellable *cancellable,
860 GAsyncReadyCallback callback,
865 g_return_if_fail (G_IS_DRIVE (drive));
867 iface = G_DRIVE_GET_IFACE (drive);
869 if (iface->stop == NULL)
871 g_simple_async_report_error_in_idle (G_OBJECT (drive), callback, user_data,
872 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
873 _("drive doesn't implement stop"));
877 (* iface->stop) (drive, flags, mount_operation, cancellable, callback, user_data);
881 * g_drive_stop_finish:
883 * @result: a #GAsyncResult.
884 * @error: a #GError, or %NULL
886 * Finishes stopping a drive.
888 * Returns: %TRUE if the drive has been stopped successfully,
894 g_drive_stop_finish (GDrive *drive,
895 GAsyncResult *result,
900 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
901 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
903 if (G_IS_SIMPLE_ASYNC_RESULT (result))
905 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
906 if (g_simple_async_result_propagate_error (simple, error))
910 iface = G_DRIVE_GET_IFACE (drive);
912 return (* iface->stop_finish) (drive, result, error);
915 #define __G_DRIVE_C__
916 #include "gioaliasdef.c"