Documentation additions
[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  * The returned list should be freed with g_list_free(), after
220  * its elements have been unreffed with g_object_unref().
221  * 
222  * Returns: #GList containing any #GVolume<!---->s on the given @drive.
223  **/
224 GList *
225 g_drive_get_volumes (GDrive *drive)
226 {
227   GDriveIface *iface;
228
229   g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
230
231   iface = G_DRIVE_GET_IFACE (drive);
232
233   return (* iface->get_volumes) (drive);
234 }
235
236 /**
237  * g_drive_is_media_check_automatic:
238  * @drive: a #GDrive.
239  * 
240  * Checks if @drive is capabable of automatically detecting media changes.
241  * 
242  * Returns: %TRUE if the @drive is capabable of automatically detecting media changes, %FALSE otherwise.
243  **/
244 gboolean
245 g_drive_is_media_check_automatic (GDrive *drive)
246 {
247   GDriveIface *iface;
248
249   g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
250
251   iface = G_DRIVE_GET_IFACE (drive);
252
253   return (* iface->is_media_check_automatic) (drive);
254 }
255
256 /**
257  * g_drive_is_media_removable:
258  * @drive: a #GDrive.
259  * 
260  * Checks if the @drive supports removable media.
261  * 
262  * Returns: %TRUE if @drive supports removable media, %FALSE otherwise.
263  **/
264 gboolean
265 g_drive_is_media_removable (GDrive *drive)
266 {
267   GDriveIface *iface;
268
269   g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
270
271   iface = G_DRIVE_GET_IFACE (drive);
272
273   return (* iface->is_media_removable) (drive);
274 }
275
276 /**
277  * g_drive_has_media:
278  * @drive: a #GDrive.
279  * 
280  * Checks if the @drive has media. Note that the OS may not be polling
281  * the drive for media changes; see g_drive_is_media_check_automatic()
282  * for more details.
283  * 
284  * Returns: %TRUE if @drive has media, %FALSE otherwise.
285  **/
286 gboolean
287 g_drive_has_media (GDrive *drive)
288 {
289   GDriveIface *iface;
290
291   g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
292
293   iface = G_DRIVE_GET_IFACE (drive);
294
295   return (* iface->has_media) (drive);
296 }
297
298 /**
299  * g_drive_can_eject:
300  * @drive: pointer to a #GDrive.
301  * 
302  * Checks if a drive can be ejected.
303  * 
304  * Returns: %TRUE if the @drive can be ejected. %FALSE otherwise.
305  **/
306 gboolean
307 g_drive_can_eject (GDrive *drive)
308 {
309   GDriveIface *iface;
310
311   g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
312
313   iface = G_DRIVE_GET_IFACE (drive);
314
315   if (iface->can_eject == NULL)
316     return FALSE;
317
318   return (* iface->can_eject) (drive);
319 }
320
321 /**
322  * g_drive_can_poll_for_media:
323  * @drive: a #GDrive.
324  * 
325  * Checks if a drive can be polled for media changes.
326  * 
327  * Returns: %TRUE if the @drive can be polled for media changes. %FALSE otherwise.
328  **/
329 gboolean
330 g_drive_can_poll_for_media (GDrive *drive)
331 {
332   GDriveIface *iface;
333
334   g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
335
336   iface = G_DRIVE_GET_IFACE (drive);
337
338   if (iface->poll_for_media == NULL)
339     return FALSE;
340
341   return (* iface->can_poll_for_media) (drive);
342 }
343
344 /**
345  * g_drive_eject:
346  * @drive: a #GDrive.
347  * @flags: flags affecting the unmount if required for eject
348  * @cancellable: optional #GCancellable object, %NULL to ignore.
349  * @callback: a #GAsyncReadyCallback, or %NULL.
350  * @user_data: a #gpointer.
351  * 
352  * Ejects a drive.
353  * 
354  **/
355 void
356 g_drive_eject (GDrive              *drive,
357                GMountUnmountFlags   flags,
358                GCancellable        *cancellable,
359                GAsyncReadyCallback  callback,
360                gpointer             user_data)
361 {
362   GDriveIface *iface;
363
364   g_return_if_fail (G_IS_DRIVE (drive));
365
366   iface = G_DRIVE_GET_IFACE (drive);
367
368   if (iface->eject == NULL)
369     {
370       g_simple_async_report_error_in_idle (G_OBJECT (drive), callback, user_data,
371                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
372                                            _("drive doesn't implement eject"));
373       
374       return;
375     }
376   
377   (* iface->eject) (drive, flags, cancellable, callback, user_data);
378 }
379
380 /**
381  * g_drive_eject_finish
382  * @drive: a #GDrive.
383  * @result: a #GAsyncResult.
384  * @error: a #GError.
385  * 
386  * Finishes ejecting a drive.
387  * 
388  * Returns: %TRUE if the drive has been ejected successfully,
389  * %FALSE otherwise.
390  **/
391 gboolean
392 g_drive_eject_finish (GDrive        *drive,
393                       GAsyncResult  *result,
394                       GError       **error)
395 {
396   GDriveIface *iface;
397
398   g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
399   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
400
401   if (G_IS_SIMPLE_ASYNC_RESULT (result))
402     {
403       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
404       if (g_simple_async_result_propagate_error (simple, error))
405         return FALSE;
406     }
407   
408   iface = G_DRIVE_GET_IFACE (drive);
409   
410   return (* iface->eject_finish) (drive, result, error);
411 }
412
413 /**
414  * g_drive_poll_for_media:
415  * @drive: a #GDrive.
416  * @cancellable: optional #GCancellable object, %NULL to ignore.
417  * @callback: a #GAsyncReadyCallback, or %NULL.
418  * @user_data: a #gpointer.
419  * 
420  * Polls @drive to see if media has been inserted or removed.
421  * 
422  **/
423 void
424 g_drive_poll_for_media (GDrive              *drive,
425                         GCancellable        *cancellable,
426                         GAsyncReadyCallback  callback,
427                         gpointer             user_data)
428 {
429   GDriveIface *iface;
430
431   g_return_if_fail (G_IS_DRIVE (drive));
432
433   iface = G_DRIVE_GET_IFACE (drive);
434
435   if (iface->poll_for_media == NULL)
436     {
437       g_simple_async_report_error_in_idle (G_OBJECT (drive), callback, user_data,
438                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
439                                            _("drive doesn't implement polling for media"));
440       
441       return;
442     }
443   
444   (* iface->poll_for_media) (drive, cancellable, callback, user_data);
445 }
446
447 /**
448  * g_drive_poll_for_media_finish
449  * @drive: a #GDrive.
450  * @result: a #GAsyncResult.
451  * @error: a #GError.
452  * 
453  * Finishes poll_for_mediaing a drive.
454  * 
455  * Returns: %TRUE if the drive has been poll_for_mediaed successfully,
456  * %FALSE otherwise.
457  **/
458 gboolean
459 g_drive_poll_for_media_finish (GDrive        *drive,
460                                GAsyncResult  *result,
461                                GError       **error)
462 {
463   GDriveIface *iface;
464
465   g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
466   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
467
468   if (G_IS_SIMPLE_ASYNC_RESULT (result))
469     {
470       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
471       if (g_simple_async_result_propagate_error (simple, error))
472         return FALSE;
473     }
474   
475   iface = G_DRIVE_GET_IFACE (drive);
476   
477   return (* iface->poll_for_media_finish) (drive, result, error);
478 }
479
480 /**
481  * g_drive_get_identifier:
482  * @drive: a #GDrive
483  * @kind: the kind of identifier to return
484  *
485  * Gets the identifier of the given kind for @drive.
486  *
487  * Returns: a newly allocated string containing the
488  *   requested identfier, or %NULL if the #GDrive
489  *   doesn't have this kind of identifier
490  */
491 char *
492 g_drive_get_identifier (GDrive     *drive,
493                         const char *kind)
494 {
495   GDriveIface *iface;
496
497   g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
498   g_return_val_if_fail (kind != NULL, NULL);
499
500   iface = G_DRIVE_GET_IFACE (drive);
501
502   if (iface->get_identifier == NULL)
503     return NULL;
504   
505   return (* iface->get_identifier) (drive, kind);
506 }
507
508 /**
509  * g_drive_enumerate_identifiers:
510  * @drive: a #GDrive
511  *
512  * Gets the kinds of identifiers that @drive has. 
513  * Use g_drive_get_identifer() to obtain the identifiers
514  * themselves.
515  *
516  * Returns: a %NULL-terminated array of strings containing
517  *   kinds of identifiers. Use g_strfreev() to free.
518  */
519 char **
520 g_drive_enumerate_identifiers (GDrive *drive)
521 {
522   GDriveIface *iface;
523
524   g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
525   iface = G_DRIVE_GET_IFACE (drive);
526
527   if (iface->enumerate_identifiers == NULL)
528     return NULL;
529   
530   return (* iface->enumerate_identifiers) (drive);
531 }
532
533
534 #define __G_DRIVE_C__
535 #include "gioaliasdef.c"