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 has
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.
183 * Free the returned object with g_object_unref().
186 g_drive_get_icon (GDrive
*drive
)
190 g_return_val_if_fail (G_IS_DRIVE (drive
), NULL
);
192 iface
= G_DRIVE_GET_IFACE (drive
);
194 return (* iface
->get_icon
) (drive
);
198 * g_drive_has_volumes:
201 * Check if @drive has any mountable volumes.
203 * Returns: %TRUE if the @drive contains volumes, %FALSE otherwise.
206 g_drive_has_volumes (GDrive
*drive
)
210 g_return_val_if_fail (G_IS_DRIVE (drive
), FALSE
);
212 iface
= G_DRIVE_GET_IFACE (drive
);
214 return (* iface
->has_volumes
) (drive
);
218 * g_drive_get_volumes:
221 * Get a list of mountable volumes for @drive.
223 * The returned list should be freed with g_list_free(), after
224 * its elements have been unreffed with g_object_unref().
226 * Returns: #GList containing any #GVolume objects on the given @drive.
229 g_drive_get_volumes (GDrive
*drive
)
233 g_return_val_if_fail (G_IS_DRIVE (drive
), NULL
);
235 iface
= G_DRIVE_GET_IFACE (drive
);
237 return (* iface
->get_volumes
) (drive
);
241 * g_drive_is_media_check_automatic:
244 * Checks if @drive is capabable of automatically detecting media changes.
246 * Returns: %TRUE if the @drive is capabable of automatically detecting
247 * media changes, %FALSE otherwise.
250 g_drive_is_media_check_automatic (GDrive
*drive
)
254 g_return_val_if_fail (G_IS_DRIVE (drive
), FALSE
);
256 iface
= G_DRIVE_GET_IFACE (drive
);
258 return (* iface
->is_media_check_automatic
) (drive
);
262 * g_drive_is_media_removable:
265 * Checks if the @drive supports removable media.
267 * Returns: %TRUE if @drive supports removable media, %FALSE otherwise.
270 g_drive_is_media_removable (GDrive
*drive
)
274 g_return_val_if_fail (G_IS_DRIVE (drive
), FALSE
);
276 iface
= G_DRIVE_GET_IFACE (drive
);
278 return (* iface
->is_media_removable
) (drive
);
285 * Checks if the @drive has media. Note that the OS may not be polling
286 * the drive for media changes; see g_drive_is_media_check_automatic()
289 * Returns: %TRUE if @drive has media, %FALSE otherwise.
292 g_drive_has_media (GDrive
*drive
)
296 g_return_val_if_fail (G_IS_DRIVE (drive
), FALSE
);
298 iface
= G_DRIVE_GET_IFACE (drive
);
300 return (* iface
->has_media
) (drive
);
307 * Checks if a drive can be ejected.
309 * Returns: %TRUE if the @drive can be ejected, %FALSE otherwise.
312 g_drive_can_eject (GDrive
*drive
)
316 g_return_val_if_fail (G_IS_DRIVE (drive
), FALSE
);
318 iface
= G_DRIVE_GET_IFACE (drive
);
320 if (iface
->can_eject
== NULL
)
323 return (* iface
->can_eject
) (drive
);
327 * g_drive_can_poll_for_media:
330 * Checks if a drive can be polled for media changes.
332 * Returns: %TRUE if the @drive can be polled for media changes,
336 g_drive_can_poll_for_media (GDrive
*drive
)
340 g_return_val_if_fail (G_IS_DRIVE (drive
), FALSE
);
342 iface
= G_DRIVE_GET_IFACE (drive
);
344 if (iface
->poll_for_media
== NULL
)
347 return (* iface
->can_poll_for_media
) (drive
);
353 * @flags: flags affecting the unmount if required for eject
354 * @cancellable: optional #GCancellable object, %NULL to ignore.
355 * @callback: a #GAsyncReadyCallback, or %NULL.
356 * @user_data: user data to pass to @callback
358 * Asynchronously ejects a drive.
360 * When the operation is finished, @callback will be called.
361 * You can then call g_drive_eject_finish() to obtain the
362 * result of the operation.
365 g_drive_eject (GDrive
*drive
,
366 GMountUnmountFlags flags
,
367 GCancellable
*cancellable
,
368 GAsyncReadyCallback callback
,
373 g_return_if_fail (G_IS_DRIVE (drive
));
375 iface
= G_DRIVE_GET_IFACE (drive
);
377 if (iface
->eject
== NULL
)
379 g_simple_async_report_error_in_idle (G_OBJECT (drive
), callback
, user_data
,
380 G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
381 _("drive doesn't implement eject"));
386 (* iface
->eject
) (drive
, flags
, cancellable
, callback
, user_data
);
390 * g_drive_eject_finish:
392 * @result: a #GAsyncResult.
393 * @error: a #GError, or %NULL
395 * Finishes ejecting a drive.
397 * Returns: %TRUE if the drive has been ejected successfully,
401 g_drive_eject_finish (GDrive
*drive
,
402 GAsyncResult
*result
,
407 g_return_val_if_fail (G_IS_DRIVE (drive
), FALSE
);
408 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), FALSE
);
410 if (G_IS_SIMPLE_ASYNC_RESULT (result
))
412 GSimpleAsyncResult
*simple
= G_SIMPLE_ASYNC_RESULT (result
);
413 if (g_simple_async_result_propagate_error (simple
, error
))
417 iface
= G_DRIVE_GET_IFACE (drive
);
419 return (* iface
->eject_finish
) (drive
, result
, error
);
423 * g_drive_poll_for_media:
425 * @cancellable: optional #GCancellable object, %NULL to ignore.
426 * @callback: a #GAsyncReadyCallback, or %NULL.
427 * @user_data: user data to pass to @callback
429 * Asynchronously polls @drive to see if media has been inserted or removed.
431 * When the operation is finished, @callback will be called.
432 * You can then call g_drive_poll_for_media_finish() to obtain the
433 * result of the operation.
436 g_drive_poll_for_media (GDrive
*drive
,
437 GCancellable
*cancellable
,
438 GAsyncReadyCallback callback
,
443 g_return_if_fail (G_IS_DRIVE (drive
));
445 iface
= G_DRIVE_GET_IFACE (drive
);
447 if (iface
->poll_for_media
== NULL
)
449 g_simple_async_report_error_in_idle (G_OBJECT (drive
), callback
, user_data
,
450 G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
451 _("drive doesn't implement polling for media"));
456 (* iface
->poll_for_media
) (drive
, cancellable
, callback
, user_data
);
460 * g_drive_poll_for_media_finish:
462 * @result: a #GAsyncResult.
463 * @error: a #GError, or %NULL
465 * Finishes an operation started with g_drive_poll_for_media() on a drive.
467 * Returns: %TRUE if the drive has been poll_for_mediaed successfully,
471 g_drive_poll_for_media_finish (GDrive
*drive
,
472 GAsyncResult
*result
,
477 g_return_val_if_fail (G_IS_DRIVE (drive
), FALSE
);
478 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), FALSE
);
480 if (G_IS_SIMPLE_ASYNC_RESULT (result
))
482 GSimpleAsyncResult
*simple
= G_SIMPLE_ASYNC_RESULT (result
);
483 if (g_simple_async_result_propagate_error (simple
, error
))
487 iface
= G_DRIVE_GET_IFACE (drive
);
489 return (* iface
->poll_for_media_finish
) (drive
, result
, error
);
493 * g_drive_get_identifier:
495 * @kind: the kind of identifier to return
497 * Gets the identifier of the given kind for @drive.
499 * Returns: a newly allocated string containing the
500 * requested identfier, or %NULL if the #GDrive
501 * doesn't have this kind of identifier.
504 g_drive_get_identifier (GDrive
*drive
,
509 g_return_val_if_fail (G_IS_DRIVE (drive
), NULL
);
510 g_return_val_if_fail (kind
!= NULL
, NULL
);
512 iface
= G_DRIVE_GET_IFACE (drive
);
514 if (iface
->get_identifier
== NULL
)
517 return (* iface
->get_identifier
) (drive
, kind
);
521 * g_drive_enumerate_identifiers:
524 * Gets the kinds of identifiers that @drive has.
525 * Use g_drive_get_identifer() to obtain the identifiers
528 * Returns: a %NULL-terminated array of strings containing
529 * kinds of identifiers. Use g_strfreev() to free.
532 g_drive_enumerate_identifiers (GDrive
*drive
)
536 g_return_val_if_fail (G_IS_DRIVE (drive
), NULL
);
537 iface
= G_DRIVE_GET_IFACE (drive
);
539 if (iface
->enumerate_identifiers
== NULL
)
542 return (* iface
->enumerate_identifiers
) (drive
);
546 #define __G_DRIVE_C__
547 #include "gioaliasdef.c"