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