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