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"
34 * @short_description: Drive management
37 * #GDrive - this represent a piece of hardware connected to the machine.
38 * It's generally only created for removable hardware or hardware with
41 * #GDrive is a container class for #GVolume objects that stem from
42 * the same piece of media. As such, #GDrive abstracts a drive with
43 * (or without) removable media and provides operations for querying
44 * whether media is available, determing whether media change is
45 * automatically detected and ejecting the media.
47 * If the #GDrive reports that media isn't automatically detected, one
48 * can poll for media; typically one should not do this periodically
49 * as a poll for media operation is potententially expensive and may
50 * spin up the drive creating noise.
52 * #GDrive supports starting and stopping drives with authentication
53 * support for the former. This can be used to support a diverse set
54 * of use cases including connecting/disconnecting iSCSI devices,
55 * powering down external disk enclosures and starting/stopping
56 * multi-disk devices such as RAID devices. Note that the actual
57 * semantics and side-effects of starting/stopping a #GDrive may vary
58 * according to implementation. To choose the correct verbs in e.g. a
59 * file manager, use g_drive_get_start_stop_type().
61 * For porting from GnomeVFS note that there is no equivalent of
62 * #GDrive in that API.
65 typedef GDriveIface GDriveInterface;
66 G_DEFINE_INTERFACE(GDrive, g_drive, G_TYPE_OBJECT)
69 g_drive_default_init (GDriveInterface *iface)
75 * Emitted when the drive's state has changed.
77 g_signal_new (I_("changed"),
80 G_STRUCT_OFFSET (GDriveIface, changed),
82 g_cclosure_marshal_VOID__VOID,
86 * GDrive::disconnected:
89 * This signal is emitted when the #GDrive have been
90 * disconnected. If the recipient is holding references to the
91 * object they should release them so the object can be
94 g_signal_new (I_("disconnected"),
97 G_STRUCT_OFFSET (GDriveIface, disconnected),
99 g_cclosure_marshal_VOID__VOID,
103 * GDrive::eject-button:
106 * Emitted when the physical eject button (if any) of a drive has
109 g_signal_new (I_("eject-button"),
112 G_STRUCT_OFFSET (GDriveIface, eject_button),
114 g_cclosure_marshal_VOID__VOID,
118 * GDrive::stop-button:
121 * Emitted when the physical stop button (if any) of a drive has
126 g_signal_new (I_("stop-button"),
129 G_STRUCT_OFFSET (GDriveIface, stop_button),
131 g_cclosure_marshal_VOID__VOID,
139 * Gets the name of @drive.
141 * Returns: a string containing @drive's name. The returned
142 * string should be freed when no longer needed.
145 g_drive_get_name (GDrive *drive)
149 g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
151 iface = G_DRIVE_GET_IFACE (drive);
153 return (* iface->get_name) (drive);
160 * Gets the icon for @drive.
162 * Returns: #GIcon for the @drive.
163 * Free the returned object with g_object_unref().
166 g_drive_get_icon (GDrive *drive)
170 g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
172 iface = G_DRIVE_GET_IFACE (drive);
174 return (* iface->get_icon) (drive);
178 * g_drive_has_volumes:
181 * Check if @drive has any mountable volumes.
183 * Returns: %TRUE if the @drive contains volumes, %FALSE otherwise.
186 g_drive_has_volumes (GDrive *drive)
190 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
192 iface = G_DRIVE_GET_IFACE (drive);
194 return (* iface->has_volumes) (drive);
198 * g_drive_get_volumes:
201 * Get a list of mountable volumes for @drive.
203 * The returned list should be freed with g_list_free(), after
204 * its elements have been unreffed with g_object_unref().
206 * Returns: (element-type GVolume) (transfer full): #GList containing any #GVolume objects on the given @drive.
209 g_drive_get_volumes (GDrive *drive)
213 g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
215 iface = G_DRIVE_GET_IFACE (drive);
217 return (* iface->get_volumes) (drive);
221 * g_drive_is_media_check_automatic:
224 * Checks if @drive is capabable of automatically detecting media changes.
226 * Returns: %TRUE if the @drive is capabable of automatically detecting
227 * media changes, %FALSE otherwise.
230 g_drive_is_media_check_automatic (GDrive *drive)
234 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
236 iface = G_DRIVE_GET_IFACE (drive);
238 return (* iface->is_media_check_automatic) (drive);
242 * g_drive_is_media_removable:
245 * Checks if the @drive supports removable media.
247 * Returns: %TRUE if @drive supports removable media, %FALSE otherwise.
250 g_drive_is_media_removable (GDrive *drive)
254 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
256 iface = G_DRIVE_GET_IFACE (drive);
258 return (* iface->is_media_removable) (drive);
265 * Checks if the @drive has media. Note that the OS may not be polling
266 * the drive for media changes; see g_drive_is_media_check_automatic()
269 * Returns: %TRUE if @drive has media, %FALSE otherwise.
272 g_drive_has_media (GDrive *drive)
276 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
278 iface = G_DRIVE_GET_IFACE (drive);
280 return (* iface->has_media) (drive);
287 * Checks if a drive can be ejected.
289 * Returns: %TRUE if the @drive can be ejected, %FALSE otherwise.
292 g_drive_can_eject (GDrive *drive)
296 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
298 iface = G_DRIVE_GET_IFACE (drive);
300 if (iface->can_eject == NULL)
303 return (* iface->can_eject) (drive);
307 * g_drive_can_poll_for_media:
310 * Checks if a drive can be polled for media changes.
312 * Returns: %TRUE if the @drive can be polled for media changes,
316 g_drive_can_poll_for_media (GDrive *drive)
320 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
322 iface = G_DRIVE_GET_IFACE (drive);
324 if (iface->poll_for_media == NULL)
327 return (* iface->can_poll_for_media) (drive);
333 * @flags: flags affecting the unmount if required for eject
334 * @cancellable: optional #GCancellable object, %NULL to ignore.
335 * @callback: a #GAsyncReadyCallback, or %NULL.
336 * @user_data: user data to pass to @callback
338 * Asynchronously ejects a drive.
340 * When the operation is finished, @callback will be called.
341 * You can then call g_drive_eject_finish() to obtain the
342 * result of the operation.
344 * Deprecated: 2.22: Use g_drive_eject_with_operation() instead.
347 g_drive_eject (GDrive *drive,
348 GMountUnmountFlags flags,
349 GCancellable *cancellable,
350 GAsyncReadyCallback callback,
355 g_return_if_fail (G_IS_DRIVE (drive));
357 iface = G_DRIVE_GET_IFACE (drive);
359 if (iface->eject == NULL)
361 g_simple_async_report_error_in_idle (G_OBJECT (drive), callback, user_data,
362 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
363 _("drive doesn't implement eject"));
368 (* iface->eject) (drive, flags, cancellable, callback, user_data);
372 * g_drive_eject_finish:
374 * @result: a #GAsyncResult.
375 * @error: a #GError, or %NULL
377 * Finishes ejecting a drive.
379 * Returns: %TRUE if the drive has been ejected successfully,
382 * Deprecated: 2.22: Use g_drive_eject_with_operation_finish() instead.
385 g_drive_eject_finish (GDrive *drive,
386 GAsyncResult *result,
391 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
392 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
394 if (G_IS_SIMPLE_ASYNC_RESULT (result))
396 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
397 if (g_simple_async_result_propagate_error (simple, error))
401 iface = G_DRIVE_GET_IFACE (drive);
403 return (* iface->eject_finish) (drive, result, error);
407 * g_drive_eject_with_operation:
409 * @flags: flags affecting the unmount if required for eject
410 * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
411 * @cancellable: optional #GCancellable object, %NULL to ignore.
412 * @callback: a #GAsyncReadyCallback, or %NULL.
413 * @user_data: user data passed to @callback.
415 * Ejects a drive. This is an asynchronous operation, and is
416 * finished by calling g_drive_eject_with_operation_finish() with the @drive
417 * and #GAsyncResult data returned in the @callback.
422 g_drive_eject_with_operation (GDrive *drive,
423 GMountUnmountFlags flags,
424 GMountOperation *mount_operation,
425 GCancellable *cancellable,
426 GAsyncReadyCallback callback,
431 g_return_if_fail (G_IS_DRIVE (drive));
433 iface = G_DRIVE_GET_IFACE (drive);
435 if (iface->eject == NULL && iface->eject_with_operation == NULL)
437 g_simple_async_report_error_in_idle (G_OBJECT (drive),
439 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
440 /* Translators: This is an error
441 * message for drive objects that
442 * don't implement any of eject or eject_with_operation. */
443 _("drive doesn't implement eject or eject_with_operation"));
447 if (iface->eject_with_operation != NULL)
448 (* iface->eject_with_operation) (drive, flags, mount_operation, cancellable, callback, user_data);
450 (* iface->eject) (drive, flags, cancellable, callback, user_data);
454 * g_drive_eject_with_operation_finish:
456 * @result: a #GAsyncResult.
457 * @error: a #GError location to store the error occuring, or %NULL to
460 * Finishes ejecting a drive. If any errors occurred during the operation,
461 * @error will be set to contain the errors and %FALSE will be returned.
463 * Returns: %TRUE if the drive was successfully ejected. %FALSE otherwise.
468 g_drive_eject_with_operation_finish (GDrive *drive,
469 GAsyncResult *result,
474 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
475 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
477 if (G_IS_SIMPLE_ASYNC_RESULT (result))
479 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
480 if (g_simple_async_result_propagate_error (simple, error))
484 iface = G_DRIVE_GET_IFACE (drive);
485 if (iface->eject_with_operation_finish != NULL)
486 return (* iface->eject_with_operation_finish) (drive, result, error);
488 return (* iface->eject_finish) (drive, result, error);
492 * g_drive_poll_for_media:
494 * @cancellable: optional #GCancellable object, %NULL to ignore.
495 * @callback: a #GAsyncReadyCallback, or %NULL.
496 * @user_data: user data to pass to @callback
498 * Asynchronously polls @drive to see if media has been inserted or removed.
500 * When the operation is finished, @callback will be called.
501 * You can then call g_drive_poll_for_media_finish() to obtain the
502 * result of the operation.
505 g_drive_poll_for_media (GDrive *drive,
506 GCancellable *cancellable,
507 GAsyncReadyCallback callback,
512 g_return_if_fail (G_IS_DRIVE (drive));
514 iface = G_DRIVE_GET_IFACE (drive);
516 if (iface->poll_for_media == NULL)
518 g_simple_async_report_error_in_idle (G_OBJECT (drive), callback, user_data,
519 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
520 _("drive doesn't implement polling for media"));
525 (* iface->poll_for_media) (drive, cancellable, callback, user_data);
529 * g_drive_poll_for_media_finish:
531 * @result: a #GAsyncResult.
532 * @error: a #GError, or %NULL
534 * Finishes an operation started with g_drive_poll_for_media() on a drive.
536 * Returns: %TRUE if the drive has been poll_for_mediaed successfully,
540 g_drive_poll_for_media_finish (GDrive *drive,
541 GAsyncResult *result,
546 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
547 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
549 if (G_IS_SIMPLE_ASYNC_RESULT (result))
551 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
552 if (g_simple_async_result_propagate_error (simple, error))
556 iface = G_DRIVE_GET_IFACE (drive);
558 return (* iface->poll_for_media_finish) (drive, result, error);
562 * g_drive_get_identifier:
564 * @kind: the kind of identifier to return
566 * Gets the identifier of the given kind for @drive.
568 * Returns: a newly allocated string containing the
569 * requested identfier, or %NULL if the #GDrive
570 * doesn't have this kind of identifier.
573 g_drive_get_identifier (GDrive *drive,
578 g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
579 g_return_val_if_fail (kind != NULL, NULL);
581 iface = G_DRIVE_GET_IFACE (drive);
583 if (iface->get_identifier == NULL)
586 return (* iface->get_identifier) (drive, kind);
590 * g_drive_enumerate_identifiers:
593 * Gets the kinds of identifiers that @drive has.
594 * Use g_drive_get_identifer() to obtain the identifiers
597 * Returns: a %NULL-terminated array of strings containing
598 * kinds of identifiers. Use g_strfreev() to free.
601 g_drive_enumerate_identifiers (GDrive *drive)
605 g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
606 iface = G_DRIVE_GET_IFACE (drive);
608 if (iface->enumerate_identifiers == NULL)
611 return (* iface->enumerate_identifiers) (drive);
615 * g_drive_get_start_stop_type:
618 * Gets a hint about how a drive can be started/stopped.
620 * Returns: A value from the #GDriveStartStopType enumeration.
625 g_drive_get_start_stop_type (GDrive *drive)
629 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
631 iface = G_DRIVE_GET_IFACE (drive);
633 if (iface->get_start_stop_type == NULL)
634 return G_DRIVE_START_STOP_TYPE_UNKNOWN;
636 return (* iface->get_start_stop_type) (drive);
644 * Checks if a drive can be started.
646 * Returns: %TRUE if the @drive can be started, %FALSE otherwise.
651 g_drive_can_start (GDrive *drive)
655 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
657 iface = G_DRIVE_GET_IFACE (drive);
659 if (iface->can_start == NULL)
662 return (* iface->can_start) (drive);
666 * g_drive_can_start_degraded:
669 * Checks if a drive can be started degraded.
671 * Returns: %TRUE if the @drive can be started degraded, %FALSE otherwise.
676 g_drive_can_start_degraded (GDrive *drive)
680 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
682 iface = G_DRIVE_GET_IFACE (drive);
684 if (iface->can_start_degraded == NULL)
687 return (* iface->can_start_degraded) (drive);
693 * @flags: flags affecting the start operation.
694 * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
695 * @cancellable: optional #GCancellable object, %NULL to ignore.
696 * @callback: a #GAsyncReadyCallback, or %NULL.
697 * @user_data: user data to pass to @callback
699 * Asynchronously starts a drive.
701 * When the operation is finished, @callback will be called.
702 * You can then call g_drive_start_finish() to obtain the
703 * result of the operation.
708 g_drive_start (GDrive *drive,
709 GDriveStartFlags flags,
710 GMountOperation *mount_operation,
711 GCancellable *cancellable,
712 GAsyncReadyCallback callback,
717 g_return_if_fail (G_IS_DRIVE (drive));
719 iface = G_DRIVE_GET_IFACE (drive);
721 if (iface->start == NULL)
723 g_simple_async_report_error_in_idle (G_OBJECT (drive), callback, user_data,
724 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
725 _("drive doesn't implement start"));
729 (* iface->start) (drive, flags, mount_operation, cancellable, callback, user_data);
733 * g_drive_start_finish:
735 * @result: a #GAsyncResult.
736 * @error: a #GError, or %NULL
738 * Finishes starting a drive.
740 * Returns: %TRUE if the drive has been started successfully,
746 g_drive_start_finish (GDrive *drive,
747 GAsyncResult *result,
752 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
753 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
755 if (G_IS_SIMPLE_ASYNC_RESULT (result))
757 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
758 if (g_simple_async_result_propagate_error (simple, error))
762 iface = G_DRIVE_GET_IFACE (drive);
764 return (* iface->start_finish) (drive, result, error);
771 * Checks if a drive can be stopped.
773 * Returns: %TRUE if the @drive can be stopped, %FALSE otherwise.
778 g_drive_can_stop (GDrive *drive)
782 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
784 iface = G_DRIVE_GET_IFACE (drive);
786 if (iface->can_stop == NULL)
789 return (* iface->can_stop) (drive);
795 * @flags: flags affecting the unmount if required for stopping.
796 * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
797 * @cancellable: optional #GCancellable object, %NULL to ignore.
798 * @callback: a #GAsyncReadyCallback, or %NULL.
799 * @user_data: user data to pass to @callback
801 * Asynchronously stops a drive.
803 * When the operation is finished, @callback will be called.
804 * You can then call g_drive_stop_finish() to obtain the
805 * result of the operation.
810 g_drive_stop (GDrive *drive,
811 GMountUnmountFlags flags,
812 GMountOperation *mount_operation,
813 GCancellable *cancellable,
814 GAsyncReadyCallback callback,
819 g_return_if_fail (G_IS_DRIVE (drive));
821 iface = G_DRIVE_GET_IFACE (drive);
823 if (iface->stop == NULL)
825 g_simple_async_report_error_in_idle (G_OBJECT (drive), callback, user_data,
826 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
827 _("drive doesn't implement stop"));
831 (* iface->stop) (drive, flags, mount_operation, cancellable, callback, user_data);
835 * g_drive_stop_finish:
837 * @result: a #GAsyncResult.
838 * @error: a #GError, or %NULL
840 * Finishes stopping a drive.
842 * Returns: %TRUE if the drive has been stopped successfully,
848 g_drive_stop_finish (GDrive *drive,
849 GAsyncResult *result,
854 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
855 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
857 if (G_IS_SIMPLE_ASYNC_RESULT (result))
859 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
860 if (g_simple_async_result_propagate_error (simple, error))
864 iface = G_DRIVE_GET_IFACE (drive);
866 return (* iface->stop_finish) (drive, result, error);