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;
100 * @volume: a #GVolume.
102 * Emitted when the drive's state has changed.
105 g_signal_new (I_("changed"),
108 G_STRUCT_OFFSET (GDriveIface, changed),
110 g_cclosure_marshal_VOID__VOID,
121 * Gets the name of @drive.
123 * Returns: a string containing @drive's name. The returned
124 * string should be freed when no longer needed.
127 g_drive_get_name (GDrive *drive)
131 g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
133 iface = G_DRIVE_GET_IFACE (drive);
135 return (* iface->get_name) (drive);
142 * Gets the icon for @drive.
144 * Returns: #GIcon for the @drive.
147 g_drive_get_icon (GDrive *drive)
151 g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
153 iface = G_DRIVE_GET_IFACE (drive);
155 return (* iface->get_icon) (drive);
159 * g_drive_has_volumes:
162 * Check if @drive has any mountable volumes.
164 * Returns: %TRUE if the @drive contains volumes, %FALSE otherwise.
167 g_drive_has_volumes (GDrive *drive)
171 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
173 iface = G_DRIVE_GET_IFACE (drive);
175 return (* iface->has_volumes) (drive);
179 * g_drive_get_volumes:
182 * Get a list of mountable volumes for @drive.
184 * Returns: #GList containing any #GVolume<!---->s on the given @drive.
187 g_drive_get_volumes (GDrive *drive)
191 g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
193 iface = G_DRIVE_GET_IFACE (drive);
195 return (* iface->get_volumes) (drive);
199 * g_drive_is_media_check_automatic:
202 * Checks if @drive is capabable of automatically detecting media changes.
204 * Returns: %TRUE if the @drive is capabable of automatically detecting media changes, %FALSE otherwise.
207 g_drive_is_media_check_automatic (GDrive *drive)
211 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
213 iface = G_DRIVE_GET_IFACE (drive);
215 return (* iface->is_media_check_automatic) (drive);
219 * g_drive_is_media_removable:
222 * Checks if the @drive supports removable media.
224 * Returns: %TRUE if @drive supports removable media, %FALSE otherwise.
227 g_drive_is_media_removable (GDrive *drive)
231 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
233 iface = G_DRIVE_GET_IFACE (drive);
235 return (* iface->is_media_removable) (drive);
242 * Checks if the @drive has media. Note that the OS may not be polling
243 * the drive for media changes; see g_drive_is_media_check_automatic()
246 * Returns: %TRUE if @drive has media, %FALSE otherwise.
249 g_drive_has_media (GDrive *drive)
253 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
255 iface = G_DRIVE_GET_IFACE (drive);
257 return (* iface->has_media) (drive);
262 * @drive: pointer to a #GDrive.
264 * Checks if a drive can be ejected.
266 * Returns: %TRUE if the @drive can be ejected. %FALSE otherwise.
269 g_drive_can_eject (GDrive *drive)
273 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
275 iface = G_DRIVE_GET_IFACE (drive);
277 if (iface->can_eject == NULL)
280 return (* iface->can_eject) (drive);
284 * g_drive_can_poll_for_media:
287 * Checks if a drive can be polled for media changes.
289 * Returns: %TRUE if the @drive can be polled for media changes. %FALSE otherwise.
292 g_drive_can_poll_for_media (GDrive *drive)
296 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
298 iface = G_DRIVE_GET_IFACE (drive);
300 if (iface->poll_for_media == NULL)
303 return (* iface->can_poll_for_media) (drive);
309 * @cancellable: optional #GCancellable object, %NULL to ignore.
310 * @callback: a #GAsyncReadyCallback.
311 * @user_data: a #gpointer.
317 g_drive_eject (GDrive *drive,
318 GCancellable *cancellable,
319 GAsyncReadyCallback callback,
324 g_return_if_fail (G_IS_DRIVE (drive));
326 iface = G_DRIVE_GET_IFACE (drive);
328 if (iface->eject == NULL)
330 g_simple_async_report_error_in_idle (G_OBJECT (drive), callback, user_data,
331 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
332 _("drive doesn't implement eject"));
337 (* iface->eject) (drive, cancellable, callback, user_data);
341 * g_drive_eject_finish
343 * @result: a #GAsyncResult.
346 * Finishes ejecting a drive.
348 * Returns: %TRUE if the drive has been ejected successfully,
352 g_drive_eject_finish (GDrive *drive,
353 GAsyncResult *result,
358 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
359 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
361 if (G_IS_SIMPLE_ASYNC_RESULT (result))
363 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
364 if (g_simple_async_result_propagate_error (simple, error))
368 iface = G_DRIVE_GET_IFACE (drive);
370 return (* iface->eject_finish) (drive, result, error);
374 * g_drive_poll_for_media:
376 * @cancellable: optional #GCancellable object, %NULL to ignore.
377 * @callback: a #GAsyncReadyCallback.
378 * @user_data: a #gpointer.
380 * Polls @drive to see if media has been inserted or removed.
384 g_drive_poll_for_media (GDrive *drive,
385 GCancellable *cancellable,
386 GAsyncReadyCallback callback,
391 g_return_if_fail (G_IS_DRIVE (drive));
393 iface = G_DRIVE_GET_IFACE (drive);
395 if (iface->poll_for_media == NULL)
397 g_simple_async_report_error_in_idle (G_OBJECT (drive), callback, user_data,
398 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
399 _("drive doesn't implement polling for media"));
404 (* iface->poll_for_media) (drive, cancellable, callback, user_data);
408 * g_drive_poll_for_media_finish
410 * @result: a #GAsyncResult.
413 * Finishes poll_for_mediaing a drive.
415 * Returns: %TRUE if the drive has been poll_for_mediaed successfully,
419 g_drive_poll_for_media_finish (GDrive *drive,
420 GAsyncResult *result,
425 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
426 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
428 if (G_IS_SIMPLE_ASYNC_RESULT (result))
430 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
431 if (g_simple_async_result_propagate_error (simple, error))
435 iface = G_DRIVE_GET_IFACE (drive);
437 return (* iface->poll_for_media_finish) (drive, result, error);
440 #define __G_DRIVE_C__
441 #include "gioaliasdef.c"