Fix a segfault
[glib.git] / gio / gdrive.c
blob9d912042e6784e02b95771474acb6603a8a91bbf
1 /* GIO - GLib Input, Output and Streaming Library
2 *
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>
24 #include "config.h"
25 #include "gdrive.h"
26 #include "gsimpleasyncresult.h"
27 #include "gasyncresult.h"
28 #include "gioerror.h"
29 #include "glibintl.h"
31 #include "gioalias.h"
33 /**
34 * SECTION:gdrive
35 * @short_description: Virtual File System drive management
36 * @include: gio/gio.h
38 * #GDrive - this represent a piece of hardware connected to the machine.
39 * Its generally only created for removable hardware or hardware with
40 * removable media.
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.
55 **/
57 static void g_drive_base_init (gpointer g_class);
58 static void g_drive_class_init (gpointer g_class,
59 gpointer class_data);
61 GType
62 g_drive_get_type (void)
64 static GType drive_type = 0;
66 if (! drive_type)
68 static const GTypeInfo drive_info =
70 sizeof (GDriveIface), /* class_size */
71 g_drive_base_init, /* base_init */
72 NULL, /* base_finalize */
73 g_drive_class_init,
74 NULL, /* class_finalize */
75 NULL, /* class_data */
77 0, /* n_preallocs */
78 NULL
81 drive_type =
82 g_type_register_static (G_TYPE_INTERFACE, I_("GDrive"),
83 &drive_info, 0);
85 g_type_interface_add_prerequisite (drive_type, G_TYPE_OBJECT);
88 return drive_type;
91 static void
92 g_drive_class_init (gpointer g_class,
93 gpointer class_data)
97 static void
98 g_drive_base_init (gpointer g_class)
100 static gboolean initialized = FALSE;
102 if (! initialized)
105 * GDrive::changed:
106 * @drive: a #GDrive.
108 * Emitted when the drive's state has changed.
110 g_signal_new (I_("changed"),
111 G_TYPE_DRIVE,
112 G_SIGNAL_RUN_LAST,
113 G_STRUCT_OFFSET (GDriveIface, changed),
114 NULL, NULL,
115 g_cclosure_marshal_VOID__VOID,
116 G_TYPE_NONE, 0);
119 * GDrive::disconnected:
120 * @drive: a #GDrive.
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
125 * finalized.
127 g_signal_new (I_("disconnected"),
128 G_TYPE_DRIVE,
129 G_SIGNAL_RUN_LAST,
130 G_STRUCT_OFFSET (GDriveIface, disconnected),
131 NULL, NULL,
132 g_cclosure_marshal_VOID__VOID,
133 G_TYPE_NONE, 0);
136 * GDrive::eject-button:
137 * @drive: a #GDrive.
139 * Emitted when the physical eject button (if any) of a drive have been pressed.
142 g_signal_new (I_("eject-button"),
143 G_TYPE_DRIVE,
144 G_SIGNAL_RUN_LAST,
145 G_STRUCT_OFFSET (GDriveIface, eject_button),
146 NULL, NULL,
147 g_cclosure_marshal_VOID__VOID,
148 G_TYPE_NONE, 0);
150 initialized = TRUE;
155 * g_drive_get_name:
156 * @drive: a #GDrive.
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.
163 char *
164 g_drive_get_name (GDrive *drive)
166 GDriveIface *iface;
168 g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
170 iface = G_DRIVE_GET_IFACE (drive);
172 return (* iface->get_name) (drive);
176 * g_drive_get_icon:
177 * @drive: a #GDrive.
179 * Gets the icon for @drive.
181 * Returns: #GIcon for the @drive.
183 GIcon *
184 g_drive_get_icon (GDrive *drive)
186 GDriveIface *iface;
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:
197 * @drive: a #GDrive.
199 * Check if @drive has any mountable volumes.
201 * Returns: %TRUE if the @drive contains volumes, %FALSE otherwise.
203 gboolean
204 g_drive_has_volumes (GDrive *drive)
206 GDriveIface *iface;
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:
217 * @drive: a #GDrive.
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.
226 GList *
227 g_drive_get_volumes (GDrive *drive)
229 GDriveIface *iface;
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:
240 * @drive: a #GDrive.
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.
246 gboolean
247 g_drive_is_media_check_automatic (GDrive *drive)
249 GDriveIface *iface;
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:
260 * @drive: a #GDrive.
262 * Checks if the @drive supports removable media.
264 * Returns: %TRUE if @drive supports removable media, %FALSE otherwise.
266 gboolean
267 g_drive_is_media_removable (GDrive *drive)
269 GDriveIface *iface;
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);
279 * g_drive_has_media:
280 * @drive: a #GDrive.
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()
284 * for more details.
286 * Returns: %TRUE if @drive has media, %FALSE otherwise.
288 gboolean
289 g_drive_has_media (GDrive *drive)
291 GDriveIface *iface;
293 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
295 iface = G_DRIVE_GET_IFACE (drive);
297 return (* iface->has_media) (drive);
301 * g_drive_can_eject:
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.
308 gboolean
309 g_drive_can_eject (GDrive *drive)
311 GDriveIface *iface;
313 g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
315 iface = G_DRIVE_GET_IFACE (drive);
317 if (iface->can_eject == NULL)
318 return FALSE;
320 return (* iface->can_eject) (drive);
324 * g_drive_can_poll_for_media:
325 * @drive: a #GDrive.
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.
331 gboolean
332 g_drive_can_poll_for_media (GDrive *drive)
334 GDriveIface *iface;
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)
341 return FALSE;
343 return (* iface->can_poll_for_media) (drive);
347 * g_drive_eject:
348 * @drive: a #GDrive.
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.
354 * Ejects a drive.
357 void
358 g_drive_eject (GDrive *drive,
359 GMountUnmountFlags flags,
360 GCancellable *cancellable,
361 GAsyncReadyCallback callback,
362 gpointer user_data)
364 GDriveIface *iface;
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"));
376 return;
379 (* iface->eject) (drive, flags, cancellable, callback, user_data);
383 * g_drive_eject_finish
384 * @drive: a #GDrive.
385 * @result: a #GAsyncResult.
386 * @error: a #GError.
388 * Finishes ejecting a drive.
390 * Returns: %TRUE if the drive has been ejected successfully,
391 * %FALSE otherwise.
393 gboolean
394 g_drive_eject_finish (GDrive *drive,
395 GAsyncResult *result,
396 GError **error)
398 GDriveIface *iface;
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))
407 return FALSE;
410 iface = G_DRIVE_GET_IFACE (drive);
412 return (* iface->eject_finish) (drive, result, error);
416 * g_drive_poll_for_media:
417 * @drive: a #GDrive.
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.
425 void
426 g_drive_poll_for_media (GDrive *drive,
427 GCancellable *cancellable,
428 GAsyncReadyCallback callback,
429 gpointer user_data)
431 GDriveIface *iface;
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"));
443 return;
446 (* iface->poll_for_media) (drive, cancellable, callback, user_data);
450 * g_drive_poll_for_media_finish
451 * @drive: a #GDrive.
452 * @result: a #GAsyncResult.
453 * @error: a #GError.
455 * Finishes poll_for_mediaing a drive.
457 * Returns: %TRUE if the drive has been poll_for_mediaed successfully,
458 * %FALSE otherwise.
460 gboolean
461 g_drive_poll_for_media_finish (GDrive *drive,
462 GAsyncResult *result,
463 GError **error)
465 GDriveIface *iface;
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))
474 return FALSE;
477 iface = G_DRIVE_GET_IFACE (drive);
479 return (* iface->poll_for_media_finish) (drive, result, error);
483 * g_drive_get_identifier:
484 * @drive: a #GDrive
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
493 char *
494 g_drive_get_identifier (GDrive *drive,
495 const char *kind)
497 GDriveIface *iface;
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)
505 return NULL;
507 return (* iface->get_identifier) (drive, kind);
511 * g_drive_enumerate_identifiers:
512 * @drive: a #GDrive
514 * Gets the kinds of identifiers that @drive has.
515 * Use g_drive_get_identifer() to obtain the identifiers
516 * themselves.
518 * Returns: a %NULL-terminated array of strings containing
519 * kinds of identifiers. Use g_strfreev() to free.
521 char **
522 g_drive_enumerate_identifiers (GDrive *drive)
524 GDriveIface *iface;
526 g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
527 iface = G_DRIVE_GET_IFACE (drive);
529 if (iface->enumerate_identifiers == NULL)
530 return NULL;
532 return (* iface->enumerate_identifiers) (drive);
536 #define __G_DRIVE_C__
537 #include "gioaliasdef.c"