Moved all relevant typedefs into these files.
[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 "gasyncresult.h"
28 #include "gioerror.h"
29 #include "glibintl.h"
30
31 #include "gioalias.h"
32
33 /**
34  * SECTION:gdrive
35  * @short_description: Virtual File System drive management
36  * @include: gio/gio.h
37  * 
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. 
41  *
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.
47  *
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.
52  *
53  * For porting from GnomeVFS note that there is no equivalent of
54  * #GDrive in that API.
55  **/
56
57 static void g_drive_base_init (gpointer g_class);
58 static void g_drive_class_init (gpointer g_class,
59                                  gpointer class_data);
60
61 GType
62 g_drive_get_type (void)
63 {
64   static GType drive_type = 0;
65
66   if (! drive_type)
67     {
68       static const GTypeInfo drive_info =
69       {
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 */
76         0,
77         0,              /* n_preallocs */
78         NULL
79       };
80
81       drive_type =
82         g_type_register_static (G_TYPE_INTERFACE, I_("GDrive"),
83                                 &drive_info, 0);
84
85       g_type_interface_add_prerequisite (drive_type, G_TYPE_OBJECT);
86     }
87
88   return drive_type;
89 }
90
91 static void
92 g_drive_class_init (gpointer g_class,
93                     gpointer class_data)
94 {
95 }
96
97 static void
98 g_drive_base_init (gpointer g_class)
99 {
100   static gboolean initialized = FALSE;
101
102   if (! initialized)
103     {
104       /**
105       * GDrive::changed:
106       * @drive: a #GDrive.
107       * 
108       * Emitted when the drive's state has changed.
109       **/
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);
117
118       /**
119       * GDrive::disconnected:
120       * @drive: a #GDrive.
121       * 
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.
126       **/
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);
134
135       /**
136       * GDrive::eject-button:
137       * @drive: a #GDrive.
138       * 
139       * Emitted when the physical eject button (if any) of a drive have been pressed.
140       * 
141       **/
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);
149
150       initialized = TRUE;
151     }
152 }
153
154 /**
155  * g_drive_get_name:
156  * @drive: a #GDrive.
157  * 
158  * Gets the name of @drive.
159  *
160  * Returns: a string containing @drive's name. The returned 
161  *     string should be freed when no longer needed.
162  **/
163 char *
164 g_drive_get_name (GDrive *drive)
165 {
166   GDriveIface *iface;
167
168   g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
169
170   iface = G_DRIVE_GET_IFACE (drive);
171
172   return (* iface->get_name) (drive);
173 }
174
175 /**
176  * g_drive_get_icon:
177  * @drive: a #GDrive.
178  * 
179  * Gets the icon for @drive.
180  * 
181  * Returns: #GIcon for the @drive.
182  **/
183 GIcon *
184 g_drive_get_icon (GDrive *drive)
185 {
186   GDriveIface *iface;
187   
188   g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
189
190   iface = G_DRIVE_GET_IFACE (drive);
191
192   return (* iface->get_icon) (drive);
193 }
194
195 /**
196  * g_drive_has_volumes:
197  * @drive: a #GDrive.
198  * 
199  * Check if @drive has any mountable volumes.
200  * 
201  * Returns: %TRUE if the @drive contains volumes, %FALSE otherwise.
202  **/
203 gboolean
204 g_drive_has_volumes (GDrive *drive)
205 {
206   GDriveIface *iface;
207
208   g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
209
210   iface = G_DRIVE_GET_IFACE (drive);
211
212   return (* iface->has_volumes) (drive);
213 }
214
215 /**
216  * g_drive_get_volumes:
217  * @drive: a #GDrive.
218  * 
219  * Get a list of mountable volumes for @drive.
220  *
221  * The returned list should be freed with g_list_free(), after
222  * its elements have been unreffed with g_object_unref().
223  * 
224  * Returns: #GList containing any #GVolume<!---->s on the given @drive.
225  **/
226 GList *
227 g_drive_get_volumes (GDrive *drive)
228 {
229   GDriveIface *iface;
230
231   g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
232
233   iface = G_DRIVE_GET_IFACE (drive);
234
235   return (* iface->get_volumes) (drive);
236 }
237
238 /**
239  * g_drive_is_media_check_automatic:
240  * @drive: a #GDrive.
241  * 
242  * Checks if @drive is capabable of automatically detecting media changes.
243  * 
244  * Returns: %TRUE if the @drive is capabable of automatically detecting media changes, %FALSE otherwise.
245  **/
246 gboolean
247 g_drive_is_media_check_automatic (GDrive *drive)
248 {
249   GDriveIface *iface;
250
251   g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
252
253   iface = G_DRIVE_GET_IFACE (drive);
254
255   return (* iface->is_media_check_automatic) (drive);
256 }
257
258 /**
259  * g_drive_is_media_removable:
260  * @drive: a #GDrive.
261  * 
262  * Checks if the @drive supports removable media.
263  * 
264  * Returns: %TRUE if @drive supports removable media, %FALSE otherwise.
265  **/
266 gboolean
267 g_drive_is_media_removable (GDrive *drive)
268 {
269   GDriveIface *iface;
270
271   g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
272
273   iface = G_DRIVE_GET_IFACE (drive);
274
275   return (* iface->is_media_removable) (drive);
276 }
277
278 /**
279  * g_drive_has_media:
280  * @drive: a #GDrive.
281  * 
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.
285  * 
286  * Returns: %TRUE if @drive has media, %FALSE otherwise.
287  **/
288 gboolean
289 g_drive_has_media (GDrive *drive)
290 {
291   GDriveIface *iface;
292
293   g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
294
295   iface = G_DRIVE_GET_IFACE (drive);
296
297   return (* iface->has_media) (drive);
298 }
299
300 /**
301  * g_drive_can_eject:
302  * @drive: pointer to a #GDrive.
303  * 
304  * Checks if a drive can be ejected.
305  * 
306  * Returns: %TRUE if the @drive can be ejected. %FALSE otherwise.
307  **/
308 gboolean
309 g_drive_can_eject (GDrive *drive)
310 {
311   GDriveIface *iface;
312
313   g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
314
315   iface = G_DRIVE_GET_IFACE (drive);
316
317   if (iface->can_eject == NULL)
318     return FALSE;
319
320   return (* iface->can_eject) (drive);
321 }
322
323 /**
324  * g_drive_can_poll_for_media:
325  * @drive: a #GDrive.
326  * 
327  * Checks if a drive can be polled for media changes.
328  * 
329  * Returns: %TRUE if the @drive can be polled for media changes. %FALSE otherwise.
330  **/
331 gboolean
332 g_drive_can_poll_for_media (GDrive *drive)
333 {
334   GDriveIface *iface;
335
336   g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
337
338   iface = G_DRIVE_GET_IFACE (drive);
339
340   if (iface->poll_for_media == NULL)
341     return FALSE;
342
343   return (* iface->can_poll_for_media) (drive);
344 }
345
346 /**
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.
353  * 
354  * Ejects a drive.
355  * 
356  **/
357 void
358 g_drive_eject (GDrive              *drive,
359                GMountUnmountFlags   flags,
360                GCancellable        *cancellable,
361                GAsyncReadyCallback  callback,
362                gpointer             user_data)
363 {
364   GDriveIface *iface;
365
366   g_return_if_fail (G_IS_DRIVE (drive));
367
368   iface = G_DRIVE_GET_IFACE (drive);
369
370   if (iface->eject == NULL)
371     {
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"));
375       
376       return;
377     }
378   
379   (* iface->eject) (drive, flags, cancellable, callback, user_data);
380 }
381
382 /**
383  * g_drive_eject_finish
384  * @drive: a #GDrive.
385  * @result: a #GAsyncResult.
386  * @error: a #GError.
387  * 
388  * Finishes ejecting a drive.
389  * 
390  * Returns: %TRUE if the drive has been ejected successfully,
391  * %FALSE otherwise.
392  **/
393 gboolean
394 g_drive_eject_finish (GDrive        *drive,
395                       GAsyncResult  *result,
396                       GError       **error)
397 {
398   GDriveIface *iface;
399
400   g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
401   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
402
403   if (G_IS_SIMPLE_ASYNC_RESULT (result))
404     {
405       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
406       if (g_simple_async_result_propagate_error (simple, error))
407         return FALSE;
408     }
409   
410   iface = G_DRIVE_GET_IFACE (drive);
411   
412   return (* iface->eject_finish) (drive, result, error);
413 }
414
415 /**
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.
421  * 
422  * Polls @drive to see if media has been inserted or removed.
423  * 
424  **/
425 void
426 g_drive_poll_for_media (GDrive              *drive,
427                         GCancellable        *cancellable,
428                         GAsyncReadyCallback  callback,
429                         gpointer             user_data)
430 {
431   GDriveIface *iface;
432
433   g_return_if_fail (G_IS_DRIVE (drive));
434
435   iface = G_DRIVE_GET_IFACE (drive);
436
437   if (iface->poll_for_media == NULL)
438     {
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"));
442       
443       return;
444     }
445   
446   (* iface->poll_for_media) (drive, cancellable, callback, user_data);
447 }
448
449 /**
450  * g_drive_poll_for_media_finish
451  * @drive: a #GDrive.
452  * @result: a #GAsyncResult.
453  * @error: a #GError.
454  * 
455  * Finishes poll_for_mediaing a drive.
456  * 
457  * Returns: %TRUE if the drive has been poll_for_mediaed successfully,
458  * %FALSE otherwise.
459  **/
460 gboolean
461 g_drive_poll_for_media_finish (GDrive        *drive,
462                                GAsyncResult  *result,
463                                GError       **error)
464 {
465   GDriveIface *iface;
466
467   g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
468   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
469
470   if (G_IS_SIMPLE_ASYNC_RESULT (result))
471     {
472       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
473       if (g_simple_async_result_propagate_error (simple, error))
474         return FALSE;
475     }
476   
477   iface = G_DRIVE_GET_IFACE (drive);
478   
479   return (* iface->poll_for_media_finish) (drive, result, error);
480 }
481
482 /**
483  * g_drive_get_identifier:
484  * @drive: a #GDrive
485  * @kind: the kind of identifier to return
486  *
487  * Gets the identifier of the given kind for @drive.
488  *
489  * Returns: a newly allocated string containing the
490  *   requested identfier, or %NULL if the #GDrive
491  *   doesn't have this kind of identifier
492  */
493 char *
494 g_drive_get_identifier (GDrive     *drive,
495                         const char *kind)
496 {
497   GDriveIface *iface;
498
499   g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
500   g_return_val_if_fail (kind != NULL, NULL);
501
502   iface = G_DRIVE_GET_IFACE (drive);
503
504   if (iface->get_identifier == NULL)
505     return NULL;
506   
507   return (* iface->get_identifier) (drive, kind);
508 }
509
510 /**
511  * g_drive_enumerate_identifiers:
512  * @drive: a #GDrive
513  *
514  * Gets the kinds of identifiers that @drive has. 
515  * Use g_drive_get_identifer() to obtain the identifiers
516  * themselves.
517  *
518  * Returns: a %NULL-terminated array of strings containing
519  *   kinds of identifiers. Use g_strfreev() to free.
520  */
521 char **
522 g_drive_enumerate_identifiers (GDrive *drive)
523 {
524   GDriveIface *iface;
525
526   g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
527   iface = G_DRIVE_GET_IFACE (drive);
528
529   if (iface->enumerate_identifiers == NULL)
530     return NULL;
531   
532   return (* iface->enumerate_identifiers) (drive);
533 }
534
535
536 #define __G_DRIVE_C__
537 #include "gioaliasdef.c"