For async functions that have no non-async version, document that the
[platform/upstream/glib.git] / gio / gdrive.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  *         David Zeuthen <davidz@redhat.com>
22  */
23
24 #include <config.h>
25 #include "gdrive.h"
26 #include "gsimpleasyncresult.h"
27 #include "glibintl.h"
28
29 #include "gioalias.h"
30
31 /**
32  * SECTION:gdrive
33  * @short_description: Virtual File System drive management
34  * @include: gio.h
35  * 
36  * #GDrive - this represent a piece of hardware connected to the machine.
37  * Its generally only created for removable hardware or hardware with
38  * removable media. 
39  *
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.
45  *
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.
50  *
51  * For porting from GnomeVFS note that there is no equivalent of
52  * #GDrive in that API.
53  **/
54
55 static void g_drive_base_init (gpointer g_class);
56 static void g_drive_class_init (gpointer g_class,
57                                  gpointer class_data);
58
59 GType
60 g_drive_get_type (void)
61 {
62   static GType drive_type = 0;
63
64   if (! drive_type)
65     {
66       static const GTypeInfo drive_info =
67       {
68         sizeof (GDriveIface), /* class_size */
69         g_drive_base_init,   /* base_init */
70         NULL,           /* base_finalize */
71         g_drive_class_init,
72         NULL,           /* class_finalize */
73         NULL,           /* class_data */
74         0,
75         0,              /* n_preallocs */
76         NULL
77       };
78
79       drive_type =
80         g_type_register_static (G_TYPE_INTERFACE, I_("GDrive"),
81                                 &drive_info, 0);
82
83       g_type_interface_add_prerequisite (drive_type, G_TYPE_OBJECT);
84     }
85
86   return drive_type;
87 }
88
89 static void
90 g_drive_class_init (gpointer g_class,
91                     gpointer class_data)
92 {
93 }
94
95 static void
96 g_drive_base_init (gpointer g_class)
97 {
98   static gboolean initialized = FALSE;
99
100   if (! initialized)
101     {
102       /**
103       * GDrive::changed:
104       * @drive: a #GDrive.
105       * 
106       * Emitted when the drive's state has changed.
107       **/
108       g_signal_new (I_("changed"),
109                     G_TYPE_DRIVE,
110                     G_SIGNAL_RUN_LAST,
111                     G_STRUCT_OFFSET (GDriveIface, changed),
112                     NULL, NULL,
113                     g_cclosure_marshal_VOID__VOID,
114                     G_TYPE_NONE, 0);
115
116       /**
117       * GDrive::disconnected:
118       * @drive: a #GDrive.
119       * 
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
123       * finalized.
124       **/
125       g_signal_new (I_("disconnected"),
126                     G_TYPE_DRIVE,
127                     G_SIGNAL_RUN_LAST,
128                     G_STRUCT_OFFSET (GDriveIface, disconnected),
129                     NULL, NULL,
130                     g_cclosure_marshal_VOID__VOID,
131                     G_TYPE_NONE, 0);
132
133       /**
134       * GDrive::eject-button:
135       * @drive: a #GDrive.
136       * 
137       * Emitted when the physical eject button (if any) of a drive have been pressed.
138       * 
139       **/
140       g_signal_new (I_("eject-button"),
141                     G_TYPE_DRIVE,
142                     G_SIGNAL_RUN_LAST,
143                     G_STRUCT_OFFSET (GDriveIface, eject_button),
144                     NULL, NULL,
145                     g_cclosure_marshal_VOID__VOID,
146                     G_TYPE_NONE, 0);
147
148       initialized = TRUE;
149     }
150 }
151
152 /**
153  * g_drive_get_name:
154  * @drive: a #GDrive.
155  * 
156  * Gets the name of @drive.
157  *
158  * Returns: a string containing @drive's name. The returned 
159  *     string should be freed when no longer needed.
160  **/
161 char *
162 g_drive_get_name (GDrive *drive)
163 {
164   GDriveIface *iface;
165
166   g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
167
168   iface = G_DRIVE_GET_IFACE (drive);
169
170   return (* iface->get_name) (drive);
171 }
172
173 /**
174  * g_drive_get_icon:
175  * @drive: a #GDrive.
176  * 
177  * Gets the icon for @drive.
178  * 
179  * Returns: #GIcon for the @drive.
180  **/
181 GIcon *
182 g_drive_get_icon (GDrive *drive)
183 {
184   GDriveIface *iface;
185   
186   g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
187
188   iface = G_DRIVE_GET_IFACE (drive);
189
190   return (* iface->get_icon) (drive);
191 }
192
193 /**
194  * g_drive_has_volumes:
195  * @drive: a #GDrive.
196  * 
197  * Check if @drive has any mountable volumes.
198  * 
199  * Returns: %TRUE if the @drive contains volumes, %FALSE otherwise.
200  **/
201 gboolean
202 g_drive_has_volumes (GDrive *drive)
203 {
204   GDriveIface *iface;
205
206   g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
207
208   iface = G_DRIVE_GET_IFACE (drive);
209
210   return (* iface->has_volumes) (drive);
211 }
212
213 /**
214  * g_drive_get_volumes:
215  * @drive: a #GDrive.
216  * 
217  * Get a list of mountable volumes for @drive.
218  * 
219  * Returns: #GList containing any #GVolume<!---->s on the given @drive.
220  **/
221 GList *
222 g_drive_get_volumes (GDrive *drive)
223 {
224   GDriveIface *iface;
225
226   g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
227
228   iface = G_DRIVE_GET_IFACE (drive);
229
230   return (* iface->get_volumes) (drive);
231 }
232
233 /**
234  * g_drive_is_media_check_automatic:
235  * @drive: a #GDrive.
236  * 
237  * Checks if @drive is capabable of automatically detecting media changes.
238  * 
239  * Returns: %TRUE if the @drive is capabable of automatically detecting media changes, %FALSE otherwise.
240  **/
241 gboolean
242 g_drive_is_media_check_automatic (GDrive *drive)
243 {
244   GDriveIface *iface;
245
246   g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
247
248   iface = G_DRIVE_GET_IFACE (drive);
249
250   return (* iface->is_media_check_automatic) (drive);
251 }
252
253 /**
254  * g_drive_is_media_removable:
255  * @drive: a #GDrive.
256  * 
257  * Checks if the @drive supports removable media.
258  * 
259  * Returns: %TRUE if @drive supports removable media, %FALSE otherwise.
260  **/
261 gboolean
262 g_drive_is_media_removable (GDrive *drive)
263 {
264   GDriveIface *iface;
265
266   g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
267
268   iface = G_DRIVE_GET_IFACE (drive);
269
270   return (* iface->is_media_removable) (drive);
271 }
272
273 /**
274  * g_drive_has_media:
275  * @drive: a #GDrive.
276  * 
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()
279  * for more details.
280  * 
281  * Returns: %TRUE if @drive has media, %FALSE otherwise.
282  **/
283 gboolean
284 g_drive_has_media (GDrive *drive)
285 {
286   GDriveIface *iface;
287
288   g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
289
290   iface = G_DRIVE_GET_IFACE (drive);
291
292   return (* iface->has_media) (drive);
293 }
294
295 /**
296  * g_drive_can_eject:
297  * @drive: pointer to a #GDrive.
298  * 
299  * Checks if a drive can be ejected.
300  * 
301  * Returns: %TRUE if the @drive can be ejected. %FALSE otherwise.
302  **/
303 gboolean
304 g_drive_can_eject (GDrive *drive)
305 {
306   GDriveIface *iface;
307
308   g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
309
310   iface = G_DRIVE_GET_IFACE (drive);
311
312   if (iface->can_eject == NULL)
313     return FALSE;
314
315   return (* iface->can_eject) (drive);
316 }
317
318 /**
319  * g_drive_can_poll_for_media:
320  * @drive: a #GDrive.
321  * 
322  * Checks if a drive can be polled for media changes.
323  * 
324  * Returns: %TRUE if the @drive can be polled for media changes. %FALSE otherwise.
325  **/
326 gboolean
327 g_drive_can_poll_for_media (GDrive *drive)
328 {
329   GDriveIface *iface;
330
331   g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
332
333   iface = G_DRIVE_GET_IFACE (drive);
334
335   if (iface->poll_for_media == NULL)
336     return FALSE;
337
338   return (* iface->can_poll_for_media) (drive);
339 }
340
341 /**
342  * g_drive_eject:
343  * @drive: a #GDrive.
344  * @flags: flags affecting the unmount if required for eject
345  * @cancellable: optional #GCancellable object, %NULL to ignore.
346  * @callback: a #GAsyncReadyCallback, or %NULL.
347  * @user_data: a #gpointer.
348  * 
349  * Ejects a drive.
350  * 
351  **/
352 void
353 g_drive_eject (GDrive              *drive,
354                GMountUnmountFlags   flags,
355                GCancellable        *cancellable,
356                GAsyncReadyCallback  callback,
357                gpointer             user_data)
358 {
359   GDriveIface *iface;
360
361   g_return_if_fail (G_IS_DRIVE (drive));
362
363   iface = G_DRIVE_GET_IFACE (drive);
364
365   if (iface->eject == NULL)
366     {
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"));
370       
371       return;
372     }
373   
374   (* iface->eject) (drive, flags, cancellable, callback, user_data);
375 }
376
377 /**
378  * g_drive_eject_finish
379  * @drive: a #GDrive.
380  * @result: a #GAsyncResult.
381  * @error: a #GError.
382  * 
383  * Finishes ejecting a drive.
384  * 
385  * Returns: %TRUE if the drive has been ejected successfully,
386  * %FALSE otherwise.
387  **/
388 gboolean
389 g_drive_eject_finish (GDrive        *drive,
390                       GAsyncResult  *result,
391                       GError       **error)
392 {
393   GDriveIface *iface;
394
395   g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
396   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
397
398   if (G_IS_SIMPLE_ASYNC_RESULT (result))
399     {
400       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
401       if (g_simple_async_result_propagate_error (simple, error))
402         return FALSE;
403     }
404   
405   iface = G_DRIVE_GET_IFACE (drive);
406   
407   return (* iface->eject_finish) (drive, result, error);
408 }
409
410 /**
411  * g_drive_poll_for_media:
412  * @drive: a #GDrive.
413  * @cancellable: optional #GCancellable object, %NULL to ignore.
414  * @callback: a #GAsyncReadyCallback, or %NULL.
415  * @user_data: a #gpointer.
416  * 
417  * Polls @drive to see if media has been inserted or removed.
418  * 
419  **/
420 void
421 g_drive_poll_for_media (GDrive              *drive,
422                         GCancellable        *cancellable,
423                         GAsyncReadyCallback  callback,
424                         gpointer             user_data)
425 {
426   GDriveIface *iface;
427
428   g_return_if_fail (G_IS_DRIVE (drive));
429
430   iface = G_DRIVE_GET_IFACE (drive);
431
432   if (iface->poll_for_media == NULL)
433     {
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"));
437       
438       return;
439     }
440   
441   (* iface->poll_for_media) (drive, cancellable, callback, user_data);
442 }
443
444 /**
445  * g_drive_poll_for_media_finish
446  * @drive: a #GDrive.
447  * @result: a #GAsyncResult.
448  * @error: a #GError.
449  * 
450  * Finishes poll_for_mediaing a drive.
451  * 
452  * Returns: %TRUE if the drive has been poll_for_mediaed successfully,
453  * %FALSE otherwise.
454  **/
455 gboolean
456 g_drive_poll_for_media_finish (GDrive        *drive,
457                                GAsyncResult  *result,
458                                GError       **error)
459 {
460   GDriveIface *iface;
461
462   g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
463   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
464
465   if (G_IS_SIMPLE_ASYNC_RESULT (result))
466     {
467       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
468       if (g_simple_async_result_propagate_error (simple, error))
469         return FALSE;
470     }
471   
472   iface = G_DRIVE_GET_IFACE (drive);
473   
474   return (* iface->poll_for_media_finish) (drive, result, error);
475 }
476
477 #define __G_DRIVE_C__
478 #include "gioaliasdef.c"