Rename all struct members named: read, write, close, truncate, or mount to
[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: pointer to a #GDrive.
293  * @result: a #GAsyncResult.
294  * @error: a #GError.
295  * 
296  * Finishes mounting a drive.
297  * 
298  * Returns: %TRUE, %FALSE if operation failed.
299  **/
300 gboolean
301 g_drive_mount_finish (GDrive        *drive,
302                       GAsyncResult  *result,
303                       GError       **error)
304 {
305   GDriveIface *iface;
306
307   g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
308   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
309
310   if (G_IS_SIMPLE_ASYNC_RESULT (result))
311     {
312       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
313       if (g_simple_async_result_propagate_error (simple, error))
314         return FALSE;
315     }
316   
317   iface = G_DRIVE_GET_IFACE (drive);
318   return (* iface->mount_finish) (drive, result, error);
319 }
320
321 /**
322  * g_drive_eject:
323  * @drive: a #GDrive.
324  * @cancellable: optional #GCancellable object, %NULL to ignore.
325  * @callback: a #GAsyncReadyCallback.
326  * @user_data: a #gpointer.
327  * 
328  * Ejects a drive.
329  * 
330  **/
331 void
332 g_drive_eject (GDrive              *drive,
333                GCancellable        *cancellable,
334                GAsyncReadyCallback  callback,
335                gpointer             user_data)
336 {
337   GDriveIface *iface;
338
339   g_return_if_fail (G_IS_DRIVE (drive));
340
341   iface = G_DRIVE_GET_IFACE (drive);
342
343   if (iface->eject == NULL)
344     {
345       g_simple_async_report_error_in_idle (G_OBJECT (drive), callback, user_data,
346                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
347                                            _("drive doesn't implement eject"));
348       
349       return;
350     }
351   
352   (* iface->eject) (drive, cancellable, callback, user_data);
353 }
354
355 /**
356  * g_drive_eject_finish
357  * @drive: a #GDrive.
358  * @result: a #GAsyncResult.
359  * @error: a #GError.
360  * 
361  * Finishes ejecting a drive.
362  * 
363  * Returns: %TRUE if the drive has been ejected successfully,
364  * %FALSE otherwise.
365  **/
366 gboolean
367 g_drive_eject_finish (GDrive        *drive,
368                       GAsyncResult  *result,
369                       GError       **error)
370 {
371   GDriveIface *iface;
372
373   g_return_val_if_fail (G_IS_DRIVE (drive), FALSE);
374   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
375
376   if (G_IS_SIMPLE_ASYNC_RESULT (result))
377     {
378       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
379       if (g_simple_async_result_propagate_error (simple, error))
380         return FALSE;
381     }
382   
383   iface = G_DRIVE_GET_IFACE (drive);
384   
385   return (* iface->mount_finish) (drive, result, error);
386 }
387
388 #define __G_DRIVE_C__
389 #include "gioaliasdef.c"