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"
33 * @short_description: Virtual File System drive management
36 * #GDrive is a container class for #GVolume objects that stem from
37 * the same piece of media. As such, #GDrive abstracts a drive with
38 * (or without) removable media and provides operations for querying
39 * whether media is available, determing whether media change is
40 * automatically detected and ejecting the media.
42 * If the #GDrive reports that media isn't automatically detected, one
43 * can poll for media; typically one should not do this periodically
44 * as a poll for media operation is potententially expensive and may
45 * spin up the drive creating noise.
47 * For porting from GnomeVFS note that there is no equivalent of
48 * #GDrive in that API.
51 static void g_drive_base_init (gpointer g_class);
52 static void g_drive_class_init (gpointer g_class,
56 g_drive_get_type (void)
58 static GType drive_type = 0;
62 static const GTypeInfo drive_info =
64 sizeof (GDriveIface), /* class_size */
65 g_drive_base_init, /* base_init */
66 NULL, /* base_finalize */
68 NULL, /* class_finalize */
69 NULL, /* class_data */
76 g_type_register_static (G_TYPE_INTERFACE, I_("GDrive"),
79 g_type_interface_add_prerequisite (drive_type, G_TYPE_OBJECT);
86 g_drive_class_init (gpointer g_class,
92 g_drive_base_init (gpointer g_class)
94 static gboolean initialized = FALSE;
102 * Emitted when the drive's state has changed.
104 g_signal_new (I_("changed"),
107 G_STRUCT_OFFSET (GDriveIface, changed),
109 g_cclosure_marshal_VOID__VOID,
113 * GDrive::disconnected:
116 * This signal is emitted when the #GDrive have been
117 * disconnected. If the recipient is holding references to the
118 * object they should release them so the object can be
121 g_signal_new (I_("disconnected"),
124 G_STRUCT_OFFSET (GDriveIface, disconnected),
126 g_cclosure_marshal_VOID__VOID,
130 * GDrive::eject-button:
133 * Emitted when the physical eject button (if any) of a drive have been pressed.
136 g_signal_new (I_("eject-button"),
139 G_STRUCT_OFFSET (GDriveIface, eject_button),
141 g_cclosure_marshal_VOID__VOID,
152 * Gets the name of @drive.
154 * Returns: a string containing @drive's name. The returned
155 * string should be freed when no longer needed.
158 g_drive_get_name (GDrive *drive)
162 g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
164 iface = G_DRIVE_GET_IFACE (drive);
166 return (* iface->get_name) (drive);
173 * Gets the icon for @drive.
175 * Returns: #GIcon for the @drive.
178 g_drive_get_icon (GDrive *drive)
182 g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
184 iface = G_DRIVE_GET_IFACE (drive);
186 return (* iface->get_icon) (drive);
190 * g_drive_has_volumes:
193 * Check if @drive has any mountable volumes.
195 * Returns: %TRUE if the @drive contains volumes, %FALSE otherwise.
198 g_drive_has_volumes (GDrive *drive)
202 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
204 iface = G_DRIVE_GET_IFACE (drive);
206 return (* iface->has_volumes) (drive);
210 * g_drive_get_volumes:
213 * Get a list of mountable volumes for @drive.
215 * Returns: #GList containing any #GVolume<!---->s on the given @drive.
218 g_drive_get_volumes (GDrive *drive)
222 g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
224 iface = G_DRIVE_GET_IFACE (drive);
226 return (* iface->get_volumes) (drive);
230 * g_drive_is_media_check_automatic:
233 * Checks if @drive is capabable of automatically detecting media changes.
235 * Returns: %TRUE if the @drive is capabable of automatically detecting media changes, %FALSE otherwise.
238 g_drive_is_media_check_automatic (GDrive *drive)
242 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
244 iface = G_DRIVE_GET_IFACE (drive);
246 return (* iface->is_media_check_automatic) (drive);
250 * g_drive_is_media_removable:
253 * Checks if the @drive supports removable media.
255 * Returns: %TRUE if @drive supports removable media, %FALSE otherwise.
258 g_drive_is_media_removable (GDrive *drive)
262 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
264 iface = G_DRIVE_GET_IFACE (drive);
266 return (* iface->is_media_removable) (drive);
273 * Checks if the @drive has media. Note that the OS may not be polling
274 * the drive for media changes; see g_drive_is_media_check_automatic()
277 * Returns: %TRUE if @drive has media, %FALSE otherwise.
280 g_drive_has_media (GDrive *drive)
284 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
286 iface = G_DRIVE_GET_IFACE (drive);
288 return (* iface->has_media) (drive);
293 * @drive: pointer to a #GDrive.
295 * Checks if a drive can be ejected.
297 * Returns: %TRUE if the @drive can be ejected. %FALSE otherwise.
300 g_drive_can_eject (GDrive *drive)
304 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
306 iface = G_DRIVE_GET_IFACE (drive);
308 if (iface->can_eject == NULL)
311 return (* iface->can_eject) (drive);
315 * g_drive_can_poll_for_media:
318 * Checks if a drive can be polled for media changes.
320 * Returns: %TRUE if the @drive can be polled for media changes. %FALSE otherwise.
323 g_drive_can_poll_for_media (GDrive *drive)
327 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
329 iface = G_DRIVE_GET_IFACE (drive);
331 if (iface->poll_for_media == NULL)
334 return (* iface->can_poll_for_media) (drive);
340 * @flags: flags affecting the unmount if required for eject
341 * @cancellable: optional #GCancellable object, %NULL to ignore.
342 * @callback: a #GAsyncReadyCallback.
343 * @user_data: a #gpointer.
349 g_drive_eject (GDrive *drive,
350 GMountUnmountFlags flags,
351 GCancellable *cancellable,
352 GAsyncReadyCallback callback,
357 g_return_if_fail (G_IS_DRIVE (drive));
359 iface = G_DRIVE_GET_IFACE (drive);
361 if (iface->eject == NULL)
363 g_simple_async_report_error_in_idle (G_OBJECT (drive), callback, user_data,
364 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
365 _("drive doesn't implement eject"));
370 (* iface->eject) (drive, flags, cancellable, callback, user_data);
374 * g_drive_eject_finish
376 * @result: a #GAsyncResult.
379 * Finishes ejecting a drive.
381 * Returns: %TRUE if the drive has been ejected successfully,
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_poll_for_media:
409 * @cancellable: optional #GCancellable object, %NULL to ignore.
410 * @callback: a #GAsyncReadyCallback.
411 * @user_data: a #gpointer.
413 * Polls @drive to see if media has been inserted or removed.
417 g_drive_poll_for_media (GDrive *drive,
418 GCancellable *cancellable,
419 GAsyncReadyCallback callback,
424 g_return_if_fail (G_IS_DRIVE (drive));
426 iface = G_DRIVE_GET_IFACE (drive);
428 if (iface->poll_for_media == NULL)
430 g_simple_async_report_error_in_idle (G_OBJECT (drive), callback, user_data,
431 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
432 _("drive doesn't implement polling for media"));
437 (* iface->poll_for_media) (drive, cancellable, callback, user_data);
441 * g_drive_poll_for_media_finish
443 * @result: a #GAsyncResult.
446 * Finishes poll_for_mediaing a drive.
448 * Returns: %TRUE if the drive has been poll_for_mediaed successfully,
452 g_drive_poll_for_media_finish (GDrive *drive,
453 GAsyncResult *result,
458 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
459 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
461 if (G_IS_SIMPLE_ASYNC_RESULT (result))
463 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
464 if (g_simple_async_result_propagate_error (simple, error))
468 iface = G_DRIVE_GET_IFACE (drive);
470 return (* iface->poll_for_media_finish) (drive, result, error);
473 #define __G_DRIVE_C__
474 #include "gioaliasdef.c"