Bumps documentation to 93% symbol coverage, touching most
[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  */
22
23 #include <config.h>
24 #include "gdrive.h"
25 #include "gsimpleasyncresult.h"
26 #include "glibintl.h"
27
28 /**
29  * SECTION:gdrive
30  * @short_description: Virtual File System drive management.
31  * @include: gio/gdrive.h
32  * 
33  * #GDrive manages drive operations from GVFS, including volume mounting
34  * and ejecting, and getting the drive's name and icon. 
35  * 
36  **/
37
38 static void g_drive_base_init (gpointer g_class);
39 static void g_drive_class_init (gpointer g_class,
40                                  gpointer class_data);
41
42 GType
43 g_drive_get_type (void)
44 {
45   static GType drive_type = 0;
46
47   if (! drive_type)
48     {
49       static const GTypeInfo drive_info =
50       {
51         sizeof (GDriveIface), /* class_size */
52         g_drive_base_init,   /* base_init */
53         NULL,           /* base_finalize */
54         g_drive_class_init,
55         NULL,           /* class_finalize */
56         NULL,           /* class_data */
57         0,
58         0,              /* n_preallocs */
59         NULL
60       };
61
62       drive_type =
63         g_type_register_static (G_TYPE_INTERFACE, I_("GDrive"),
64                                 &drive_info, 0);
65
66       g_type_interface_add_prerequisite (drive_type, G_TYPE_OBJECT);
67     }
68
69   return drive_type;
70 }
71
72 static void
73 g_drive_class_init (gpointer g_class,
74                    gpointer class_data)
75 {
76 }
77
78 static void
79 g_drive_base_init (gpointer g_class)
80 {
81   static gboolean initialized = FALSE;
82
83   if (! initialized)
84     {
85       /**
86       * GDrive::changed:
87       * @volume: a #GVolume.
88       * 
89       * Emitted when the drive's state has changed.
90       * 
91       **/
92       g_signal_new (I_("changed"),
93                     G_TYPE_DRIVE,
94                     G_SIGNAL_RUN_LAST,
95                     G_STRUCT_OFFSET (GDriveIface, changed),
96                     NULL, NULL,
97                     g_cclosure_marshal_VOID__VOID,
98                     G_TYPE_NONE, 0);
99
100       initialized = TRUE;
101     }
102 }
103
104 /**
105  * g_drive_get_name:
106  * @drive: a #GDrive.
107  * 
108  * Returns: string containing @drive's name.
109  *
110  * The returned string should be freed when no longer needed.
111  **/
112 char *
113 g_drive_get_name (GDrive *drive)
114 {
115   GDriveIface *iface;
116
117   g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
118
119   iface = G_DRIVE_GET_IFACE (drive);
120
121   return (* iface->get_name) (drive);
122 }
123
124 /**
125  * g_drive_get_icon:
126  * @drive: a #GDrive.
127  * 
128  * Gets the icon for @drive.
129  * 
130  * Returns: #GIcon for the @drive.
131  **/
132 GIcon *
133 g_drive_get_icon (GDrive *drive)
134 {
135   GDriveIface *iface;
136   
137   g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
138
139   iface = G_DRIVE_GET_IFACE (drive);
140
141   return (* iface->get_icon) (drive);
142 }
143
144 /**
145  * g_drive_has_volumes:
146  * @drive: a #GDrive.
147  * 
148  * Checks if a drive has any volumes.
149  * 
150  * Returns: %TRUE if @drive contains volumes, %FALSE otherwise.
151  **/
152 gboolean
153 g_drive_has_volumes (GDrive *drive)
154 {
155   GDriveIface *iface;
156
157   g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
158
159   iface = G_DRIVE_GET_IFACE (drive);
160
161   return (* iface->has_volumes) (drive);
162 }
163
164 /**
165  * g_drive_get_volumes:
166  * @drive: a #GDrive.
167  * 
168  * Gets a list of volumes for a drive.
169  * 
170  * Returns: #GList containing any #GVolume<!---->s on the given @drive.
171  * NOTE: Fact-check this.
172  **/
173 GList *
174 g_drive_get_volumes (GDrive *drive)
175 {
176   GDriveIface *iface;
177
178   g_return_val_if_fail (G_IS_DRIVE (drive), NULL);
179
180   iface = G_DRIVE_GET_IFACE (drive);
181
182   return (* iface->get_volumes) (drive);
183 }
184
185 /**
186  * g_drive_is_automounted:
187  * @drive: a #GDrive.
188  * 
189  * Checks if a drive was automatically mounted, e.g. by HAL.
190  * 
191  * Returns: %TRUE if the drive was automounted. %FALSE otherwise.
192  **/
193 gboolean
194 g_drive_is_automounted (GDrive *drive)
195 {
196   GDriveIface *iface;
197
198   g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
199
200   iface = G_DRIVE_GET_IFACE (drive);
201
202   return (* iface->is_automounted) (drive);
203 }
204
205 /**
206  * g_drive_can_mount:
207  * @drive: a #GDrive.
208  * 
209  * Checks if a drive can be mounted.
210  * 
211  * Returns: %TRUE if the @drive can be mounted. %FALSE otherwise.
212  **/
213 gboolean
214 g_drive_can_mount (GDrive *drive)
215 {
216   GDriveIface *iface;
217
218   g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
219
220   iface = G_DRIVE_GET_IFACE (drive);
221
222   if (iface->can_mount == NULL)
223     return FALSE;
224
225   return (* iface->can_mount) (drive);
226 }
227
228 /**
229  * g_drive_can_eject:
230  * @drive: pointer to a #GDrive.
231  * 
232  * Checks if a drive can be ejected.
233  * 
234  * Returns: %TRUE if the @drive can be ejected. %FALSE otherwise.
235  **/
236 gboolean
237 g_drive_can_eject (GDrive *drive)
238 {
239   GDriveIface *iface;
240
241   g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
242
243   iface = G_DRIVE_GET_IFACE (drive);
244
245   if (iface->can_eject == NULL)
246     return FALSE;
247
248   return (* iface->can_eject) (drive);
249 }
250
251 /**
252  * g_drive_mount:
253  * @drive: a #GDrive.
254  * @mount_operation: a #GMountOperation.
255  * @cancellable: optional #GCancellable object, %NULL to ignore.
256  * @callback: a #GAsyncReadyCallback.
257  * @user_data: a #gpointer.
258  * 
259  * Mounts a drive.
260  * 
261  **/
262 void
263 g_drive_mount (GDrive         *drive,
264                GMountOperation *mount_operation,
265                GCancellable *cancellable,
266                GAsyncReadyCallback callback,
267                gpointer         user_data)
268 {
269   GDriveIface *iface;
270
271   g_return_if_fail (G_IS_DRIVE (drive));
272   g_return_if_fail (G_IS_MOUNT_OPERATION (mount_operation));
273
274   iface = G_DRIVE_GET_IFACE (drive);
275
276   if (iface->mount == NULL)
277     {
278       g_simple_async_report_error_in_idle (G_OBJECT (drive), callback, user_data,
279                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
280                                            _("drive doesn't implement mount"));
281       
282       return;
283     }
284   
285   (* iface->mount) (drive, mount_operation, cancellable, callback, user_data);
286 }
287
288 /**
289  * g_drive_mount_finish:
290  * @drive: pointer to a #GDrive.
291  * @result: a #GAsyncResult.
292  * @error: a #GError.
293  * 
294  * Finishes mounting a drive.
295  * 
296  * Returns: %TRUE, %FALSE if operation failed.
297  **/
298 gboolean
299 g_drive_mount_finish (GDrive               *drive,
300                       GAsyncResult         *result,
301                       GError              **error)
302 {
303   GDriveIface *iface;
304
305   g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
306   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
307
308   if (G_IS_SIMPLE_ASYNC_RESULT (result))
309     {
310       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
311       if (g_simple_async_result_propagate_error (simple, error))
312         return FALSE;
313     }
314   
315   iface = G_DRIVE_GET_IFACE (drive);
316   return (* iface->mount_finish) (drive, result, error);
317 }
318
319 /**
320  * g_drive_eject:
321  * @drive: a #GDrive.
322  * @cancellable: optional #GCancellable object, %NULL to ignore.
323  * @callback: a #GAsyncReadyCallback.
324  * @user_data: a #gpointer.
325  * 
326  * Ejects a drive.
327  * 
328  **/
329 void
330 g_drive_eject (GDrive         *drive,
331                GCancellable *cancellable,
332                GAsyncReadyCallback  callback,
333                gpointer         user_data)
334 {
335   GDriveIface *iface;
336
337   g_return_if_fail (G_IS_DRIVE (drive));
338
339   iface = G_DRIVE_GET_IFACE (drive);
340
341   if (iface->eject == NULL)
342     {
343       g_simple_async_report_error_in_idle (G_OBJECT (drive), callback, user_data,
344                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
345                                            _("drive doesn't implement eject"));
346       
347       return;
348     }
349   
350   (* iface->eject) (drive, cancellable, callback, user_data);
351 }
352
353 /**
354  * g_drive_eject_finish
355  * @drive: a #GDrive.
356  * @result: a #GAsyncResult.
357  * @error: a #GError.
358  * 
359  * Finishes ejecting a drive.
360  * 
361  * Returns: %TRUE if the drive has been ejected successfully,
362  * %FALSE otherwise.
363  **/
364 gboolean
365 g_drive_eject_finish (GDrive               *drive,
366                       GAsyncResult         *result,
367                       GError              **error)
368 {
369   GDriveIface *iface;
370
371   g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
372   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
373
374   if (G_IS_SIMPLE_ASYNC_RESULT (result))
375     {
376       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
377       if (g_simple_async_result_propagate_error (simple, error))
378         return FALSE;
379     }
380   
381   iface = G_DRIVE_GET_IFACE (drive);
382   
383   return (* iface->mount_finish) (drive, result, error);
384 }