Fix up includes in section docs
[platform/upstream/glib.git] / gio / gvolume.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 "gmount.h"
26 #include "gvolume.h"
27 #include "gsimpleasyncresult.h"
28 #include "glibintl.h"
29
30 #include "gioalias.h"
31
32 /**
33  * SECTION:gvolume
34  * @short_description: Volume management
35  * @include: gio.h
36  * 
37  * The #GVolume interface represents user-visible objects that can be
38  * mounted. Note, when porting from GnomeVFS, #GVolume is the moral
39  * equivalent of #GnomeVFSDrive.
40  *
41  * Mounting a #GVolume instance is an asynchronous operation. For more
42  * information about asynchronous operations, see #GAsyncReady and
43  * #GSimpleAsyncReady. To mount a #GVolume, first call
44  * g_volume_mount() with (at least) the #GVolume instane, a
45  * #GMountOperation object and a #GAsyncReadyCallback.  The callback
46  * will be fired when the operation has resolved (either with success
47  * or failure), and a #GAsyncReady structure will be passed to the
48  * callback.  That callback should then call g_volume_mount_finish()
49  * with the #GVolume instance and the #GAsyncReady data to see if the
50  * operation was completed successfully.  If an @error is present when
51  * g_volume_mount_finish() is called, then it will be filled with any
52  * error information.
53  **/
54
55 static void g_volume_base_init (gpointer g_class);
56 static void g_volume_class_init (gpointer g_class,
57                                  gpointer class_data);
58
59 GType
60 g_volume_get_type (void)
61 {
62   static GType volume_type = 0;
63
64   if (! volume_type)
65     {
66       static const GTypeInfo volume_info =
67       {
68         sizeof (GVolumeIface), /* class_size */
69         g_volume_base_init,   /* base_init */
70         NULL,           /* base_finalize */
71         g_volume_class_init,
72         NULL,           /* class_finalize */
73         NULL,           /* class_data */
74         0,
75         0,              /* n_preallocs */
76         NULL
77       };
78
79       volume_type =
80         g_type_register_static (G_TYPE_INTERFACE, I_("GVolume"),
81                                 &volume_info, 0);
82
83       g_type_interface_add_prerequisite (volume_type, G_TYPE_OBJECT);
84     }
85
86   return volume_type;
87 }
88
89 static void
90 g_volume_class_init (gpointer g_class,
91                      gpointer class_data)
92 {
93 }
94
95 static void
96 g_volume_base_init (gpointer g_class)
97 {
98   static gboolean initialized = FALSE;
99
100   if (! initialized)
101     {
102      /**
103       * GVolume::changed:
104       * 
105       * Emitted when the volume has been changed.
106       **/
107       g_signal_new (I_("changed"),
108                     G_TYPE_VOLUME,
109                     G_SIGNAL_RUN_LAST,
110                     G_STRUCT_OFFSET (GVolumeIface, changed),
111                     NULL, NULL,
112                     g_cclosure_marshal_VOID__VOID,
113                     G_TYPE_NONE, 0);
114
115       initialized = TRUE;
116     }
117 }
118
119 /**
120  * g_volume_get_name:
121  * @volume: a #GVolume.
122  * 
123  * Gets the name of @volume.
124  * 
125  * Returns: the name for the given @volume. The returned string should 
126  * be freed when no longer needed.
127  **/
128 char *
129 g_volume_get_name (GVolume *volume)
130 {
131   GVolumeIface *iface;
132
133   g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
134
135   iface = G_VOLUME_GET_IFACE (volume);
136
137   return (* iface->get_name) (volume);
138 }
139
140 /**
141  * g_volume_get_icon:
142  * @volume: a #GVolume.
143  * 
144  * Gets the icon for @volume.
145  * 
146  * Returns: a #GIcon.
147  **/
148 GIcon *
149 g_volume_get_icon (GVolume *volume)
150 {
151   GVolumeIface *iface;
152
153   g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
154
155   iface = G_VOLUME_GET_IFACE (volume);
156
157   return (* iface->get_icon) (volume);
158 }
159
160 /**
161  * g_volume_get_uuid:
162  * @volume: a #GVolume.
163  * 
164  * Gets the UUID for the @volume. The reference is typically based on
165  * the file system UUID for the volume in question and should be
166  * considered an opaque string. Returns %NULL if there is no UUID
167  * available.
168  * 
169  * Returns: the UUID for @volume or %NULL if no UUID can be computed.
170  **/
171 char *
172 g_volume_get_uuid (GVolume *volume)
173 {
174   GVolumeIface *iface;
175
176   g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
177
178   iface = G_VOLUME_GET_IFACE (volume);
179
180   return (* iface->get_uuid) (volume);
181 }
182   
183 /**
184  * g_volume_get_drive:
185  * @volume: a #GVolume.
186  * 
187  * Gets the drive for the @volume.
188  * 
189  * Returns: a #GDrive or %NULL if @volume is not associated with a drive.
190  **/
191 GDrive *
192 g_volume_get_drive (GVolume *volume)
193 {
194   GVolumeIface *iface;
195
196   g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
197
198   iface = G_VOLUME_GET_IFACE (volume);
199
200   return (* iface->get_drive) (volume);
201 }
202
203 /**
204  * g_volume_get_mount:
205  * @volume: a #GVolume.
206  * 
207  * Gets the mount for the @volume.
208  * 
209  * Returns: a #GMount or %NULL if @volume isn't mounted.
210  **/
211 GMount *
212 g_volume_get_mount (GVolume *volume)
213 {
214   GVolumeIface *iface;
215
216   g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
217
218   iface = G_VOLUME_GET_IFACE (volume);
219
220   return (* iface->get_mount) (volume);
221 }
222
223
224 /**
225  * g_volume_can_mount:
226  * @volume: a #GVolume.
227  * 
228  * Checks if a volume can be mounted.
229  * 
230  * Returns: %TRUE if the @volume can be mounted. %FALSE otherwise.
231  **/
232 gboolean
233 g_volume_can_mount (GVolume *volume)
234 {
235   GVolumeIface *iface;
236
237   g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
238
239   iface = G_VOLUME_GET_IFACE (volume);
240
241   if (iface->can_mount == NULL)
242     return FALSE;
243
244   return (* iface->can_mount) (volume);
245 }
246
247 /**
248  * g_volume_can_eject:
249  * @volume: a #GVolume.
250  * 
251  * Checks if a volume can be ejected.
252  * 
253  * Returns: %TRUE if the @volume can be ejected. %FALSE otherwise.
254  **/
255 gboolean
256 g_volume_can_eject (GVolume *volume)
257 {
258   GVolumeIface *iface;
259
260   g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
261
262   iface = G_VOLUME_GET_IFACE (volume);
263
264   if (iface->can_eject == NULL)
265     return FALSE;
266
267   return (* iface->can_eject) (volume);
268 }
269
270 /**
271  * g_volume_mount:
272  * @volume: a #GVolume.
273  * @mount_operation: a #GMountOperation.
274  * @cancellable: optional #GCancellable object, %NULL to ignore.
275  * @callback: a #GAsyncReadyCallback.
276  * @user_data: a #gpointer.
277  * 
278  * Mounts a volume.
279  **/
280 void
281 g_volume_mount (GVolume    *volume,
282                 GMountOperation     *mount_operation,
283                 GCancellable        *cancellable,
284                 GAsyncReadyCallback  callback,
285                 gpointer             user_data)
286 {
287   GVolumeIface *iface;
288
289   g_return_if_fail (G_IS_VOLUME (volume));
290   g_return_if_fail (G_IS_MOUNT_OPERATION (mount_operation));
291
292   iface = G_VOLUME_GET_IFACE (volume);
293
294   if (iface->mount_fn == NULL)
295     {
296       g_simple_async_report_error_in_idle (G_OBJECT (volume), callback, user_data,
297                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
298                                            _("volume doesn't implement mount"));
299       
300       return;
301     }
302   
303   (* iface->mount_fn) (volume, mount_operation, cancellable, callback, user_data);
304 }
305
306 /**
307  * g_volume_mount_finish:
308  * @volume: pointer to a #GVolume.
309  * @result: a #GAsyncResult.
310  * @error: a #GError.
311  * 
312  * Finishes mounting a volume.
313  * 
314  * Returns: %TRUE, %FALSE if operation failed.
315  **/
316 gboolean
317 g_volume_mount_finish (GVolume  *volume,
318                        GAsyncResult      *result,
319                        GError           **error)
320 {
321   GVolumeIface *iface;
322
323   g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
324   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
325
326   if (G_IS_SIMPLE_ASYNC_RESULT (result))
327     {
328       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
329       if (g_simple_async_result_propagate_error (simple, error))
330         return FALSE;
331     }
332   
333   iface = G_VOLUME_GET_IFACE (volume);
334   return (* iface->mount_finish) (volume, result, error);
335 }
336
337 /**
338  * g_volume_eject:
339  * @volume: a #GVolume.
340  * @cancellable: optional #GCancellable object, %NULL to ignore.
341  * @callback: a #GAsyncReadyCallback.
342  * @user_data: a #gpointer.
343  * 
344  * Ejects a volume.
345  **/
346 void
347 g_volume_eject (GVolume    *volume,
348                 GCancellable        *cancellable,
349                 GAsyncReadyCallback  callback,
350                 gpointer             user_data)
351 {
352   GVolumeIface *iface;
353
354   g_return_if_fail (G_IS_VOLUME (volume));
355
356   iface = G_VOLUME_GET_IFACE (volume);
357
358   if (iface->eject == NULL)
359     {
360       g_simple_async_report_error_in_idle (G_OBJECT (volume), callback, user_data,
361                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
362                                            _("volume doesn't implement eject"));
363       
364       return;
365     }
366   
367   (* iface->eject) (volume, cancellable, callback, user_data);
368 }
369
370 /**
371  * g_volume_eject_finish:
372  * @volume: pointer to a #GVolume.
373  * @result: a #GAsyncResult.
374  * @error: a #GError.
375  * 
376  * Finishes ejecting a volume.
377  * 
378  * Returns: %TRUE, %FALSE if operation failed.
379  **/
380 gboolean
381 g_volume_eject_finish (GVolume  *volume,
382                        GAsyncResult      *result,
383                        GError           **error)
384 {
385   GVolumeIface *iface;
386
387   g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
388   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
389
390   if (G_IS_SIMPLE_ASYNC_RESULT (result))
391     {
392       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
393       if (g_simple_async_result_propagate_error (simple, error))
394         return FALSE;
395     }
396   
397   iface = G_VOLUME_GET_IFACE (volume);
398   return (* iface->eject_finish) (volume, result, error);
399 }
400
401 #define __G_VOLUME_C__
402 #include "gioaliasdef.c"