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 - this represent a piece of hardware connected to the machine.
37 * Its generally only created for removable hardware or hardware with
40 * #GDrive is a container class for #GVolume objects that stem from
41 * the same piece of media. As such, #GDrive abstracts a drive with
42 * (or without) removable media and provides operations for querying
43 * whether media is available, determing whether media change is
44 * automatically detected and ejecting the media.
46 * If the #GDrive reports that media isn't automatically detected, one
47 * can poll for media; typically one should not do this periodically
48 * as a poll for media operation is potententially expensive and may
49 * spin up the drive creating noise.
51 * For porting from GnomeVFS note that there is no equivalent of
52 * #GDrive in that API.
55 static void g_drive_base_init (gpointer g_class);
56 static void g_drive_class_init (gpointer g_class,
60 g_drive_get_type (void)
62 static GType drive_type = 0;
66 static const GTypeInfo drive_info =
68 sizeof (GDriveIface), /* class_size */
69 g_drive_base_init, /* base_init */
70 NULL, /* base_finalize */
72 NULL, /* class_finalize */
73 NULL, /* class_data */
80 g_type_register_static (G_TYPE_INTERFACE, I_("GDrive"),
83 g_type_interface_add_prerequisite (drive_type, G_TYPE_OBJECT);
90 g_drive_class_init (gpointer g_class,
96 g_drive_base_init (gpointer g_class)
98 static gboolean initialized = FALSE;
106 * Emitted when the drive's state has changed.
108 g_signal_new (I_("changed"),
111 G_STRUCT_OFFSET (GDriveIface, changed),
113 g_cclosure_marshal_VOID__VOID,
117 * GDrive::disconnected:
120 * This signal is emitted when the #GDrive have been
121 * disconnected. If the recipient is holding references to the
122 * object they should release them so the object can be
125 g_signal_new (I_("disconnected"),
128 G_STRUCT_OFFSET (GDriveIface, disconnected),
130 g_cclosure_marshal_VOID__VOID,
134 * GDrive::eject-button:
137 * Emitted when the physical eject button (if any) of a drive have been pressed.
140 g_signal_new (I_("eject-button"),
143 G_STRUCT_OFFSET (GDriveIface, eject_button),
145 g_cclosure_marshal_VOID__VOID,
156 * Gets the name of @drive.
158 * Returns: a string containing @drive's name. The returned
159 * string should be freed when no longer needed.
162 g_drive_get_name (GDrive *drive)
166 g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
168 iface = G_DRIVE_GET_IFACE (drive);
170 return (* iface->get_name) (drive);
177 * Gets the icon for @drive.
179 * Returns: #GIcon for the @drive.
182 g_drive_get_icon (GDrive *drive)
186 g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
188 iface = G_DRIVE_GET_IFACE (drive);
190 return (* iface->get_icon) (drive);
194 * g_drive_has_volumes:
197 * Check if @drive has any mountable volumes.
199 * Returns: %TRUE if the @drive contains volumes, %FALSE otherwise.
202 g_drive_has_volumes (GDrive *drive)
206 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
208 iface = G_DRIVE_GET_IFACE (drive);
210 return (* iface->has_volumes) (drive);
214 * g_drive_get_volumes:
217 * Get a list of mountable volumes for @drive.
219 * Returns: #GList containing any #GVolume<!---->s on the given @drive.
222 g_drive_get_volumes (GDrive *drive)
226 g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
228 iface = G_DRIVE_GET_IFACE (drive);
230 return (* iface->get_volumes) (drive);
234 * g_drive_is_media_check_automatic:
237 * Checks if @drive is capabable of automatically detecting media changes.
239 * Returns: %TRUE if the @drive is capabable of automatically detecting media changes, %FALSE otherwise.
242 g_drive_is_media_check_automatic (GDrive *drive)
246 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
248 iface = G_DRIVE_GET_IFACE (drive);
250 return (* iface->is_media_check_automatic) (drive);
254 * g_drive_is_media_removable:
257 * Checks if the @drive supports removable media.
259 * Returns: %TRUE if @drive supports removable media, %FALSE otherwise.
262 g_drive_is_media_removable (GDrive *drive)
266 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
268 iface = G_DRIVE_GET_IFACE (drive);
270 return (* iface->is_media_removable) (drive);
277 * Checks if the @drive has media. Note that the OS may not be polling
278 * the drive for media changes; see g_drive_is_media_check_automatic()
281 * Returns: %TRUE if @drive has media, %FALSE otherwise.
284 g_drive_has_media (GDrive *drive)
288 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
290 iface = G_DRIVE_GET_IFACE (drive);
292 return (* iface->has_media) (drive);
297 * @drive: pointer to a #GDrive.
299 * Checks if a drive can be ejected.
301 * Returns: %TRUE if the @drive can be ejected. %FALSE otherwise.
304 g_drive_can_eject (GDrive *drive)
308 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
310 iface = G_DRIVE_GET_IFACE (drive);
312 if (iface->can_eject == NULL)
315 return (* iface->can_eject) (drive);
319 * g_drive_can_poll_for_media:
322 * Checks if a drive can be polled for media changes.
324 * Returns: %TRUE if the @drive can be polled for media changes. %FALSE otherwise.
327 g_drive_can_poll_for_media (GDrive *drive)
331 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
333 iface = G_DRIVE_GET_IFACE (drive);
335 if (iface->poll_for_media == NULL)
338 return (* iface->can_poll_for_media) (drive);
344 * @flags: flags affecting the unmount if required for eject
345 * @cancellable: optional #GCancellable object, %NULL to ignore.
346 * @callback: a #GAsyncReadyCallback.
347 * @user_data: a #gpointer.
353 g_drive_eject (GDrive *drive,
354 GMountUnmountFlags flags,
355 GCancellable *cancellable,
356 GAsyncReadyCallback callback,
361 g_return_if_fail (G_IS_DRIVE (drive));
363 iface = G_DRIVE_GET_IFACE (drive);
365 if (iface->eject == NULL)
367 g_simple_async_report_error_in_idle (G_OBJECT (drive), callback, user_data,
368 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
369 _("drive doesn't implement eject"));
374 (* iface->eject) (drive, flags, cancellable, callback, user_data);
378 * g_drive_eject_finish
380 * @result: a #GAsyncResult.
383 * Finishes ejecting a drive.
385 * Returns: %TRUE if the drive has been ejected successfully,
389 g_drive_eject_finish (GDrive *drive,
390 GAsyncResult *result,
395 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
396 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
398 if (G_IS_SIMPLE_ASYNC_RESULT (result))
400 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
401 if (g_simple_async_result_propagate_error (simple, error))
405 iface = G_DRIVE_GET_IFACE (drive);
407 return (* iface->eject_finish) (drive, result, error);
411 * g_drive_poll_for_media:
413 * @cancellable: optional #GCancellable object, %NULL to ignore.
414 * @callback: a #GAsyncReadyCallback.
415 * @user_data: a #gpointer.
417 * Polls @drive to see if media has been inserted or removed.
421 g_drive_poll_for_media (GDrive *drive,
422 GCancellable *cancellable,
423 GAsyncReadyCallback callback,
428 g_return_if_fail (G_IS_DRIVE (drive));
430 iface = G_DRIVE_GET_IFACE (drive);
432 if (iface->poll_for_media == NULL)
434 g_simple_async_report_error_in_idle (G_OBJECT (drive), callback, user_data,
435 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
436 _("drive doesn't implement polling for media"));
441 (* iface->poll_for_media) (drive, cancellable, callback, user_data);
445 * g_drive_poll_for_media_finish
447 * @result: a #GAsyncResult.
450 * Finishes poll_for_mediaing a drive.
452 * Returns: %TRUE if the drive has been poll_for_mediaed successfully,
456 g_drive_poll_for_media_finish (GDrive *drive,
457 GAsyncResult *result,
462 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
463 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
465 if (G_IS_SIMPLE_ASYNC_RESULT (result))
467 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
468 if (g_simple_async_result_propagate_error (simple, error))
472 iface = G_DRIVE_GET_IFACE (drive);
474 return (* iface->poll_for_media_finish) (drive, result, error);
477 #define __G_DRIVE_C__
478 #include "gioaliasdef.c"