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: Virtual File System drive management
38 * #GDrive - this represent a piece of hardware connected to the machine.
39 * Its 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 * For porting from GnomeVFS note that there is no equivalent of
54 * #GDrive in that API.
57 static void g_drive_base_init (gpointer g_class);
58 static void g_drive_class_init (gpointer g_class,
62 g_drive_get_type (void)
64 static volatile gsize g_define_type_id__volatile = 0;
66 if (g_once_init_enter (&g_define_type_id__volatile))
68 const GTypeInfo drive_info =
70 sizeof (GDriveIface), /* class_size */
71 g_drive_base_init, /* base_init */
72 NULL, /* base_finalize */
74 NULL, /* class_finalize */
75 NULL, /* class_data */
80 GType g_define_type_id =
81 g_type_register_static (G_TYPE_INTERFACE, I_("GDrive"),
84 g_type_interface_add_prerequisite (g_define_type_id, G_TYPE_OBJECT);
86 g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
89 return g_define_type_id__volatile;
93 g_drive_class_init (gpointer g_class,
99 g_drive_base_init (gpointer g_class)
101 static gboolean initialized = FALSE;
109 * Emitted when the drive's state has changed.
111 g_signal_new (I_("changed"),
114 G_STRUCT_OFFSET (GDriveIface, changed),
116 g_cclosure_marshal_VOID__VOID,
120 * GDrive::disconnected:
123 * This signal is emitted when the #GDrive have been
124 * disconnected. If the recipient is holding references to the
125 * object they should release them so the object can be
128 g_signal_new (I_("disconnected"),
131 G_STRUCT_OFFSET (GDriveIface, disconnected),
133 g_cclosure_marshal_VOID__VOID,
137 * GDrive::eject-button:
140 * Emitted when the physical eject button (if any) of a drive have been pressed.
143 g_signal_new (I_("eject-button"),
146 G_STRUCT_OFFSET (GDriveIface, eject_button),
148 g_cclosure_marshal_VOID__VOID,
159 * Gets the name of @drive.
161 * Returns: a string containing @drive's name. The returned
162 * string should be freed when no longer needed.
165 g_drive_get_name (GDrive *drive)
169 g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
171 iface = G_DRIVE_GET_IFACE (drive);
173 return (* iface->get_name) (drive);
180 * Gets the icon for @drive.
182 * Returns: #GIcon for the @drive.
185 g_drive_get_icon (GDrive *drive)
189 g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
191 iface = G_DRIVE_GET_IFACE (drive);
193 return (* iface->get_icon) (drive);
197 * g_drive_has_volumes:
200 * Check if @drive has any mountable volumes.
202 * Returns: %TRUE if the @drive contains volumes, %FALSE otherwise.
205 g_drive_has_volumes (GDrive *drive)
209 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
211 iface = G_DRIVE_GET_IFACE (drive);
213 return (* iface->has_volumes) (drive);
217 * g_drive_get_volumes:
220 * Get a list of mountable volumes for @drive.
222 * The returned list should be freed with g_list_free(), after
223 * its elements have been unreffed with g_object_unref().
225 * Returns: #GList containing any #GVolume<!---->s on the given @drive.
228 g_drive_get_volumes (GDrive *drive)
232 g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
234 iface = G_DRIVE_GET_IFACE (drive);
236 return (* iface->get_volumes) (drive);
240 * g_drive_is_media_check_automatic:
243 * Checks if @drive is capabable of automatically detecting media changes.
245 * Returns: %TRUE if the @drive is capabable of automatically detecting media changes, %FALSE otherwise.
248 g_drive_is_media_check_automatic (GDrive *drive)
252 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
254 iface = G_DRIVE_GET_IFACE (drive);
256 return (* iface->is_media_check_automatic) (drive);
260 * g_drive_is_media_removable:
263 * Checks if the @drive supports removable media.
265 * Returns: %TRUE if @drive supports removable media, %FALSE otherwise.
268 g_drive_is_media_removable (GDrive *drive)
272 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
274 iface = G_DRIVE_GET_IFACE (drive);
276 return (* iface->is_media_removable) (drive);
283 * Checks if the @drive has media. Note that the OS may not be polling
284 * the drive for media changes; see g_drive_is_media_check_automatic()
287 * Returns: %TRUE if @drive has media, %FALSE otherwise.
290 g_drive_has_media (GDrive *drive)
294 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
296 iface = G_DRIVE_GET_IFACE (drive);
298 return (* iface->has_media) (drive);
303 * @drive: pointer to a #GDrive.
305 * Checks if a drive can be ejected.
307 * Returns: %TRUE if the @drive can be ejected. %FALSE otherwise.
310 g_drive_can_eject (GDrive *drive)
314 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
316 iface = G_DRIVE_GET_IFACE (drive);
318 if (iface->can_eject == NULL)
321 return (* iface->can_eject) (drive);
325 * g_drive_can_poll_for_media:
328 * Checks if a drive can be polled for media changes.
330 * Returns: %TRUE if the @drive can be polled for media changes. %FALSE otherwise.
333 g_drive_can_poll_for_media (GDrive *drive)
337 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
339 iface = G_DRIVE_GET_IFACE (drive);
341 if (iface->poll_for_media == NULL)
344 return (* iface->can_poll_for_media) (drive);
350 * @flags: flags affecting the unmount if required for eject
351 * @cancellable: optional #GCancellable object, %NULL to ignore.
352 * @callback: a #GAsyncReadyCallback, or %NULL.
353 * @user_data: a #gpointer.
359 g_drive_eject (GDrive *drive,
360 GMountUnmountFlags flags,
361 GCancellable *cancellable,
362 GAsyncReadyCallback callback,
367 g_return_if_fail (G_IS_DRIVE (drive));
369 iface = G_DRIVE_GET_IFACE (drive);
371 if (iface->eject == NULL)
373 g_simple_async_report_error_in_idle (G_OBJECT (drive), callback, user_data,
374 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
375 _("drive doesn't implement eject"));
380 (* iface->eject) (drive, flags, cancellable, callback, user_data);
384 * g_drive_eject_finish
386 * @result: a #GAsyncResult.
389 * Finishes ejecting a drive.
391 * Returns: %TRUE if the drive has been ejected successfully,
395 g_drive_eject_finish (GDrive *drive,
396 GAsyncResult *result,
401 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
402 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
404 if (G_IS_SIMPLE_ASYNC_RESULT (result))
406 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
407 if (g_simple_async_result_propagate_error (simple, error))
411 iface = G_DRIVE_GET_IFACE (drive);
413 return (* iface->eject_finish) (drive, result, error);
417 * g_drive_poll_for_media:
419 * @cancellable: optional #GCancellable object, %NULL to ignore.
420 * @callback: a #GAsyncReadyCallback, or %NULL.
421 * @user_data: a #gpointer.
423 * Polls @drive to see if media has been inserted or removed.
427 g_drive_poll_for_media (GDrive *drive,
428 GCancellable *cancellable,
429 GAsyncReadyCallback callback,
434 g_return_if_fail (G_IS_DRIVE (drive));
436 iface = G_DRIVE_GET_IFACE (drive);
438 if (iface->poll_for_media == NULL)
440 g_simple_async_report_error_in_idle (G_OBJECT (drive), callback, user_data,
441 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
442 _("drive doesn't implement polling for media"));
447 (* iface->poll_for_media) (drive, cancellable, callback, user_data);
451 * g_drive_poll_for_media_finish
453 * @result: a #GAsyncResult.
456 * Finishes poll_for_mediaing a drive.
458 * Returns: %TRUE if the drive has been poll_for_mediaed successfully,
462 g_drive_poll_for_media_finish (GDrive *drive,
463 GAsyncResult *result,
468 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
469 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
471 if (G_IS_SIMPLE_ASYNC_RESULT (result))
473 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
474 if (g_simple_async_result_propagate_error (simple, error))
478 iface = G_DRIVE_GET_IFACE (drive);
480 return (* iface->poll_for_media_finish) (drive, result, error);
484 * g_drive_get_identifier:
486 * @kind: the kind of identifier to return
488 * Gets the identifier of the given kind for @drive.
490 * Returns: a newly allocated string containing the
491 * requested identfier, or %NULL if the #GDrive
492 * doesn't have this kind of identifier
495 g_drive_get_identifier (GDrive *drive,
500 g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
501 g_return_val_if_fail (kind != NULL, NULL);
503 iface = G_DRIVE_GET_IFACE (drive);
505 if (iface->get_identifier == NULL)
508 return (* iface->get_identifier) (drive, kind);
512 * g_drive_enumerate_identifiers:
515 * Gets the kinds of identifiers that @drive has.
516 * Use g_drive_get_identifer() to obtain the identifiers
519 * Returns: a %NULL-terminated array of strings containing
520 * kinds of identifiers. Use g_strfreev() to free.
523 g_drive_enumerate_identifiers (GDrive *drive)
527 g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
528 iface = G_DRIVE_GET_IFACE (drive);
530 if (iface->enumerate_identifiers == NULL)
533 return (* iface->enumerate_identifiers) (drive);
537 #define __G_DRIVE_C__
538 #include "gioaliasdef.c"