Add new error codes for when compilation fails and make compilation error
[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  */
22
23 #include <config.h>
24 #include "gvolume.h"
25 #include "gvolumeprivate.h"
26 #include "gsimpleasyncresult.h"
27 #include "glibintl.h"
28
29 #include "gioalias.h"
30
31 /**
32  * SECTION:gvolume
33  * @short_description: mounted volume management
34  * 
35  * Class for managing mounted volumes.
36  * 
37  * Unmounting volumes is an asynchronous operation. For more information about
38  * asynchronous operations, see #GAsyncReady and #GSimpleAsyncReady. To unmount a volume, 
39  * first call g_volume_unmount() with (at least) the volume and a #GAsyncReadyCallback.
40  * The callback will be fired when the operation has resolved (either with success or failure), 
41  * and a #GAsyncReady structure will be passed to the callback. 
42  * That callback should then call g_volume_unmount_finish() with
43  * the volume and the #GAsyncReady data to see if the operation was completed successfully.
44  * If an @error is present when g_volume_unmount_finish() is called, then it will
45  * be filled with any error information.
46  * 
47  * Ejecting volumes is also an asynchronous operation. 
48  * To eject a volume, call g_volume_eject() with (at least) the volume to eject 
49  * and a #GAsyncReadyCallback. The callback will be fired when the eject operation
50  * has resolved (either with success or failure), and a #GAsyncReady structure will
51  * be passed to the callback. That callback should then call g_volume_eject_finish()
52  * with the volume and the #GAsyncReady data to determine if the operation was completed
53  * successfully. If an @error is present when g_volume_eject_finish() is called, then
54  * it will be filled with any error information. 
55  *
56  **/
57
58 static void g_volume_base_init (gpointer g_class);
59 static void g_volume_class_init (gpointer g_class,
60                                  gpointer class_data);
61
62 GType
63 g_volume_get_type (void)
64 {
65   static GType volume_type = 0;
66
67   if (! volume_type)
68     {
69       static const GTypeInfo volume_info =
70       {
71         sizeof (GVolumeIface), /* class_size */
72         g_volume_base_init,   /* base_init */
73         NULL,           /* base_finalize */
74         g_volume_class_init,
75         NULL,           /* class_finalize */
76         NULL,           /* class_data */
77         0,
78         0,              /* n_preallocs */
79         NULL
80       };
81
82       volume_type =
83         g_type_register_static (G_TYPE_INTERFACE, I_("GVolume"),
84                                 &volume_info, 0);
85
86       g_type_interface_add_prerequisite (volume_type, G_TYPE_OBJECT);
87     }
88
89   return volume_type;
90 }
91
92 static void
93 g_volume_class_init (gpointer g_class,
94                    gpointer class_data)
95 {
96 }
97
98 static void
99 g_volume_base_init (gpointer g_class)
100 {
101   static gboolean initialized = FALSE;
102
103   if (! initialized)
104     {
105      /**
106       * GVolume::changed:
107       * 
108       * Emitted when the volume has been changed.
109       **/
110       g_signal_new (I_("changed"),
111                     G_TYPE_VOLUME,
112                     G_SIGNAL_RUN_LAST,
113                     G_STRUCT_OFFSET (GVolumeIface, changed),
114                     NULL, NULL,
115                     g_cclosure_marshal_VOID__VOID,
116                     G_TYPE_NONE, 0);
117
118       initialized = TRUE;
119     }
120 }
121
122 /**
123  * g_volume_get_root:
124  * @volume: a #GVolume.
125  * 
126  * Gets the root directory on @volume.
127  * 
128  * Returns: a #GFile.
129  **/
130 GFile *
131 g_volume_get_root (GVolume *volume)
132 {
133   GVolumeIface *iface;
134
135   g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
136
137   iface = G_VOLUME_GET_IFACE (volume);
138
139   return (* iface->get_root) (volume);
140 }
141
142 /**
143  * g_volume_get_name:
144  * @volume: a #GVolume.
145  * 
146  * Gets the name of @volume.
147  * 
148  * Returns: the name for the given @volume. The returned string should 
149  * be freed when no longer needed.
150  **/
151 char *
152 g_volume_get_name (GVolume *volume)
153 {
154   GVolumeIface *iface;
155
156   g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
157
158   iface = G_VOLUME_GET_IFACE (volume);
159
160   return (* iface->get_name) (volume);
161 }
162
163 /**
164  * g_volume_get_icon:
165  * @volume: a #GVolume.
166  * 
167  * Gets the icon for @volume.
168  * 
169  * Returns: a #GIcon.
170  **/
171 GIcon *
172 g_volume_get_icon (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_icon) (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.
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_can_unmount: 
205  * @volume: a #GVolume.
206  * 
207  * Checks if @volume can be mounted.
208  * 
209  * Returns: %TRUE if the @volume can be unmounted.
210  **/
211 gboolean
212 g_volume_can_unmount (GVolume *volume)
213 {
214   GVolumeIface *iface;
215
216   g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
217
218   iface = G_VOLUME_GET_IFACE (volume);
219
220   return (* iface->can_unmount) (volume);
221 }
222
223 /**
224  * g_volume_can_eject:
225  * @volume: a #GVolume.
226  * 
227  * Checks if @volume can be ejected.
228  * 
229  * Returns: %TRUE if the @volume can be ejected.
230  **/
231 gboolean
232 g_volume_can_eject (GVolume *volume)
233 {
234   GVolumeIface *iface;
235
236   g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
237
238   iface = G_VOLUME_GET_IFACE (volume);
239
240   return (* iface->can_eject) (volume);
241 }
242
243 /**
244  * g_volume_unmount:
245  * @volume: a #GVolume.
246  * @cancellable: optional #GCancellable object, %NULL to ignore.
247  * @callback: a #GAsyncReadyCallback.
248  * @user_data: user data passed to @callback.
249  * 
250  * Unmounts a volume. This is an asynchronous operation, and is 
251  * finished by calling g_volume_unmount_finish() with the @volume 
252  * and #GAsyncResults data returned in the @callback.
253  **/
254 void
255 g_volume_unmount (GVolume             *volume,
256                   GCancellable        *cancellable,
257                   GAsyncReadyCallback  callback,
258                   gpointer             user_data)
259 {
260   GVolumeIface *iface;
261
262   g_return_if_fail (G_IS_VOLUME (volume));
263   
264   iface = G_VOLUME_GET_IFACE (volume);
265
266   if (iface->unmount == NULL)
267     {
268       g_simple_async_report_error_in_idle (G_OBJECT (volume),
269                                            callback, user_data,
270                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
271                                            _("volume doesn't implement unmount"));
272       
273       return;
274     }
275   
276   (* iface->unmount) (volume, cancellable, callback, user_data);
277 }
278
279 /**
280  * g_volume_unmount_finish:
281  * @volume: a #GVolume.
282  * @result: a #GAsyncResult.
283  * @error: a #GError location to store the error occuring, or %NULL to 
284  * ignore.
285  * 
286  * Finishes unmounting a volume. If any errors occured during the operation, 
287  * @error will be set to contain the errors and %FALSE will be returned.
288  * 
289  * Returns: %TRUE if the volume was successfully unmounted. %FALSE otherwise.
290  **/
291 gboolean
292 g_volume_unmount_finish (GVolume       *volume,
293                          GAsyncResult  *result,
294                          GError       **error)
295 {
296   GVolumeIface *iface;
297
298   g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
299   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
300
301   if (G_IS_SIMPLE_ASYNC_RESULT (result))
302     {
303       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
304       if (g_simple_async_result_propagate_error (simple, error))
305         return FALSE;
306     }
307   
308   iface = G_VOLUME_GET_IFACE (volume);
309   return (* iface->unmount_finish) (volume, result, error);
310 }
311
312 /**
313  * g_volume_eject:
314  * @volume: a #GVolume.
315  * @cancellable: optional #GCancellable object, %NULL to ignore. 
316  * @callback: a #GAsyncReadyCallback.
317  * @user_data: user data passed to @callback.
318  * 
319  * Ejects a volume. This is an asynchronous operation, and is 
320  * finished by calling g_volume_eject_finish() from the @callback 
321  * with the @volume and #GAsyncResults returned in the callback.
322  **/
323 void
324 g_volume_eject (GVolume             *volume,
325                 GCancellable        *cancellable,
326                 GAsyncReadyCallback  callback,
327                 gpointer             user_data)
328 {
329   GVolumeIface *iface;
330
331   g_return_if_fail (G_IS_VOLUME (volume));
332
333   iface = G_VOLUME_GET_IFACE (volume);
334
335   if (iface->eject == NULL)
336     {
337       g_simple_async_report_error_in_idle (G_OBJECT (volume),
338                                            callback, user_data,
339                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
340                                            _("volume doesn't implement eject"));
341       
342       return;
343     }
344   
345   (* iface->eject) (volume, cancellable, callback, user_data);
346 }
347
348 /**
349  * g_volume_eject_finish:
350  * @volume: a #GVolume.
351  * @result: a #GAsyncResult.
352  * @error: a #GError location to store the error occuring, or %NULL to 
353  * ignore.
354  * 
355  * Finishes ejecting the volume. If any errors occured during the operation, 
356  * @error will be set to contain the errors and %FALSE will be returned.
357  * 
358  * Returns: %TRUE if the volume was successfully ejected. %FALSE otherwise.
359  **/
360 gboolean
361 g_volume_eject_finish (GVolume       *volume,
362                        GAsyncResult  *result,
363                        GError       **error)
364 {
365   GVolumeIface *iface;
366
367   g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
368   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
369
370   if (G_IS_SIMPLE_ASYNC_RESULT (result))
371     {
372       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
373       if (g_simple_async_result_propagate_error (simple, error))
374         return FALSE;
375     }
376   
377   iface = G_VOLUME_GET_IFACE (volume);
378   return (* iface->eject_finish) (volume, result, error);
379 }
380
381 #define __G_VOLUME_C__
382 #include "gioaliasdef.c"