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 GType drive_type
= 0;
68 static 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 */
82 g_type_register_static (G_TYPE_INTERFACE
, I_("GDrive"),
85 g_type_interface_add_prerequisite (drive_type
, G_TYPE_OBJECT
);
92 g_drive_class_init (gpointer g_class
,
98 g_drive_base_init (gpointer g_class
)
100 static gboolean initialized
= FALSE
;
108 * Emitted when the drive's state has changed.
110 g_signal_new (I_("changed"),
113 G_STRUCT_OFFSET (GDriveIface
, changed
),
115 g_cclosure_marshal_VOID__VOID
,
119 * GDrive::disconnected:
122 * This signal is emitted when the #GDrive have been
123 * disconnected. If the recipient is holding references to the
124 * object they should release them so the object can be
127 g_signal_new (I_("disconnected"),
130 G_STRUCT_OFFSET (GDriveIface
, disconnected
),
132 g_cclosure_marshal_VOID__VOID
,
136 * GDrive::eject-button:
139 * Emitted when the physical eject button (if any) of a drive have been pressed.
142 g_signal_new (I_("eject-button"),
145 G_STRUCT_OFFSET (GDriveIface
, eject_button
),
147 g_cclosure_marshal_VOID__VOID
,
158 * Gets the name of @drive.
160 * Returns: a string containing @drive's name. The returned
161 * string should be freed when no longer needed.
164 g_drive_get_name (GDrive
*drive
)
168 g_return_val_if_fail (G_IS_DRIVE (drive
), NULL
);
170 iface
= G_DRIVE_GET_IFACE (drive
);
172 return (* iface
->get_name
) (drive
);
179 * Gets the icon for @drive.
181 * Returns: #GIcon for the @drive.
184 g_drive_get_icon (GDrive
*drive
)
188 g_return_val_if_fail (G_IS_DRIVE (drive
), NULL
);
190 iface
= G_DRIVE_GET_IFACE (drive
);
192 return (* iface
->get_icon
) (drive
);
196 * g_drive_has_volumes:
199 * Check if @drive has any mountable volumes.
201 * Returns: %TRUE if the @drive contains volumes, %FALSE otherwise.
204 g_drive_has_volumes (GDrive
*drive
)
208 g_return_val_if_fail (G_IS_DRIVE (drive
), FALSE
);
210 iface
= G_DRIVE_GET_IFACE (drive
);
212 return (* iface
->has_volumes
) (drive
);
216 * g_drive_get_volumes:
219 * Get a list of mountable volumes for @drive.
221 * The returned list should be freed with g_list_free(), after
222 * its elements have been unreffed with g_object_unref().
224 * Returns: #GList containing any #GVolume<!---->s on the given @drive.
227 g_drive_get_volumes (GDrive
*drive
)
231 g_return_val_if_fail (G_IS_DRIVE (drive
), NULL
);
233 iface
= G_DRIVE_GET_IFACE (drive
);
235 return (* iface
->get_volumes
) (drive
);
239 * g_drive_is_media_check_automatic:
242 * Checks if @drive is capabable of automatically detecting media changes.
244 * Returns: %TRUE if the @drive is capabable of automatically detecting media changes, %FALSE otherwise.
247 g_drive_is_media_check_automatic (GDrive
*drive
)
251 g_return_val_if_fail (G_IS_DRIVE (drive
), FALSE
);
253 iface
= G_DRIVE_GET_IFACE (drive
);
255 return (* iface
->is_media_check_automatic
) (drive
);
259 * g_drive_is_media_removable:
262 * Checks if the @drive supports removable media.
264 * Returns: %TRUE if @drive supports removable media, %FALSE otherwise.
267 g_drive_is_media_removable (GDrive
*drive
)
271 g_return_val_if_fail (G_IS_DRIVE (drive
), FALSE
);
273 iface
= G_DRIVE_GET_IFACE (drive
);
275 return (* iface
->is_media_removable
) (drive
);
282 * Checks if the @drive has media. Note that the OS may not be polling
283 * the drive for media changes; see g_drive_is_media_check_automatic()
286 * Returns: %TRUE if @drive has media, %FALSE otherwise.
289 g_drive_has_media (GDrive
*drive
)
293 g_return_val_if_fail (G_IS_DRIVE (drive
), FALSE
);
295 iface
= G_DRIVE_GET_IFACE (drive
);
297 return (* iface
->has_media
) (drive
);
302 * @drive: pointer to a #GDrive.
304 * Checks if a drive can be ejected.
306 * Returns: %TRUE if the @drive can be ejected. %FALSE otherwise.
309 g_drive_can_eject (GDrive
*drive
)
313 g_return_val_if_fail (G_IS_DRIVE (drive
), FALSE
);
315 iface
= G_DRIVE_GET_IFACE (drive
);
317 if (iface
->can_eject
== NULL
)
320 return (* iface
->can_eject
) (drive
);
324 * g_drive_can_poll_for_media:
327 * Checks if a drive can be polled for media changes.
329 * Returns: %TRUE if the @drive can be polled for media changes. %FALSE otherwise.
332 g_drive_can_poll_for_media (GDrive
*drive
)
336 g_return_val_if_fail (G_IS_DRIVE (drive
), FALSE
);
338 iface
= G_DRIVE_GET_IFACE (drive
);
340 if (iface
->poll_for_media
== NULL
)
343 return (* iface
->can_poll_for_media
) (drive
);
349 * @flags: flags affecting the unmount if required for eject
350 * @cancellable: optional #GCancellable object, %NULL to ignore.
351 * @callback: a #GAsyncReadyCallback, or %NULL.
352 * @user_data: a #gpointer.
358 g_drive_eject (GDrive
*drive
,
359 GMountUnmountFlags flags
,
360 GCancellable
*cancellable
,
361 GAsyncReadyCallback callback
,
366 g_return_if_fail (G_IS_DRIVE (drive
));
368 iface
= G_DRIVE_GET_IFACE (drive
);
370 if (iface
->eject
== NULL
)
372 g_simple_async_report_error_in_idle (G_OBJECT (drive
), callback
, user_data
,
373 G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
374 _("drive doesn't implement eject"));
379 (* iface
->eject
) (drive
, flags
, cancellable
, callback
, user_data
);
383 * g_drive_eject_finish
385 * @result: a #GAsyncResult.
388 * Finishes ejecting a drive.
390 * Returns: %TRUE if the drive has been ejected successfully,
394 g_drive_eject_finish (GDrive
*drive
,
395 GAsyncResult
*result
,
400 g_return_val_if_fail (G_IS_DRIVE (drive
), FALSE
);
401 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), FALSE
);
403 if (G_IS_SIMPLE_ASYNC_RESULT (result
))
405 GSimpleAsyncResult
*simple
= G_SIMPLE_ASYNC_RESULT (result
);
406 if (g_simple_async_result_propagate_error (simple
, error
))
410 iface
= G_DRIVE_GET_IFACE (drive
);
412 return (* iface
->eject_finish
) (drive
, result
, error
);
416 * g_drive_poll_for_media:
418 * @cancellable: optional #GCancellable object, %NULL to ignore.
419 * @callback: a #GAsyncReadyCallback, or %NULL.
420 * @user_data: a #gpointer.
422 * Polls @drive to see if media has been inserted or removed.
426 g_drive_poll_for_media (GDrive
*drive
,
427 GCancellable
*cancellable
,
428 GAsyncReadyCallback callback
,
433 g_return_if_fail (G_IS_DRIVE (drive
));
435 iface
= G_DRIVE_GET_IFACE (drive
);
437 if (iface
->poll_for_media
== NULL
)
439 g_simple_async_report_error_in_idle (G_OBJECT (drive
), callback
, user_data
,
440 G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
441 _("drive doesn't implement polling for media"));
446 (* iface
->poll_for_media
) (drive
, cancellable
, callback
, user_data
);
450 * g_drive_poll_for_media_finish
452 * @result: a #GAsyncResult.
455 * Finishes poll_for_mediaing a drive.
457 * Returns: %TRUE if the drive has been poll_for_mediaed successfully,
461 g_drive_poll_for_media_finish (GDrive
*drive
,
462 GAsyncResult
*result
,
467 g_return_val_if_fail (G_IS_DRIVE (drive
), FALSE
);
468 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), FALSE
);
470 if (G_IS_SIMPLE_ASYNC_RESULT (result
))
472 GSimpleAsyncResult
*simple
= G_SIMPLE_ASYNC_RESULT (result
);
473 if (g_simple_async_result_propagate_error (simple
, error
))
477 iface
= G_DRIVE_GET_IFACE (drive
);
479 return (* iface
->poll_for_media_finish
) (drive
, result
, error
);
483 * g_drive_get_identifier:
485 * @kind: the kind of identifier to return
487 * Gets the identifier of the given kind for @drive.
489 * Returns: a newly allocated string containing the
490 * requested identfier, or %NULL if the #GDrive
491 * doesn't have this kind of identifier
494 g_drive_get_identifier (GDrive
*drive
,
499 g_return_val_if_fail (G_IS_DRIVE (drive
), NULL
);
500 g_return_val_if_fail (kind
!= NULL
, NULL
);
502 iface
= G_DRIVE_GET_IFACE (drive
);
504 if (iface
->get_identifier
== NULL
)
507 return (* iface
->get_identifier
) (drive
, kind
);
511 * g_drive_enumerate_identifiers:
514 * Gets the kinds of identifiers that @drive has.
515 * Use g_drive_get_identifer() to obtain the identifiers
518 * Returns: a %NULL-terminated array of strings containing
519 * kinds of identifiers. Use g_strfreev() to free.
522 g_drive_enumerate_identifiers (GDrive
*drive
)
526 g_return_val_if_fail (G_IS_DRIVE (drive
), NULL
);
527 iface
= G_DRIVE_GET_IFACE (drive
);
529 if (iface
->enumerate_identifiers
== NULL
)
532 return (* iface
->enumerate_identifiers
) (drive
);
536 #define __G_DRIVE_C__
537 #include "gioaliasdef.c"