8a7e709a72230bf5e1468b320ff59c118f8aefaf
[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 finished by calling 
251  * g_volume_unmount_finish() with the @volume and #GAsyncResults data returned in the 
252  * @callback.
253  * 
254  **/
255 void
256 g_volume_unmount (GVolume *volume,
257                   GCancellable *cancellable,
258                   GAsyncReadyCallback callback,
259                   gpointer user_data)
260 {
261   GVolumeIface *iface;
262
263   g_return_if_fail (G_IS_VOLUME (volume));
264   
265   iface = G_VOLUME_GET_IFACE (volume);
266
267   if (iface->unmount == NULL)
268     {
269       g_simple_async_report_error_in_idle (G_OBJECT (volume),
270                                            callback, user_data,
271                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
272                                            _("volume doesn't implement unmount"));
273       
274       return;
275     }
276   
277   (* iface->unmount) (volume, cancellable, callback, user_data);
278 }
279
280 /**
281  * g_volume_unmount_finish:
282  * @volume: a #GVolume.
283  * @result: a #GAsyncResult.
284  * @error: a #GError location to store the error occuring, or %NULL to 
285  * ignore.
286  * 
287  * Finishes unmounting a volume. If any errors occured during the operation, 
288  * @error will be set to contain the errors and %FALSE will be returned.
289  * 
290  * Returns: %TRUE if the volume was successfully unmounted. %FALSE otherwise.
291  **/
292 gboolean
293 g_volume_unmount_finish (GVolume              *volume,
294                          GAsyncResult         *result,
295                          GError              **error)
296 {
297   GVolumeIface *iface;
298
299   g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
300   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
301
302   if (G_IS_SIMPLE_ASYNC_RESULT (result))
303     {
304       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
305       if (g_simple_async_result_propagate_error (simple, error))
306         return FALSE;
307     }
308   
309   iface = G_VOLUME_GET_IFACE (volume);
310   return (* iface->unmount_finish) (volume, result, error);
311 }
312
313 /**
314  * g_volume_eject:
315  * @volume: a #GVolume.
316  * @cancellable: optional #GCancellable object, %NULL to ignore. 
317  * @callback: a #GAsyncReadyCallback.
318  * @user_data: user data passed to @callback.
319  * 
320  * Ejects a volume. This is an asynchronous operation, and is finished
321  * by calling g_volume_eject_finish() from the @callback with the @volume and 
322  * #GAsyncResults returned in the callback.
323  * 
324  **/
325 void
326 g_volume_eject (GVolume         *volume,
327                 GCancellable *cancellable,
328                 GAsyncReadyCallback  callback,
329                 gpointer         user_data)
330 {
331   GVolumeIface *iface;
332
333   g_return_if_fail (G_IS_VOLUME (volume));
334
335   iface = G_VOLUME_GET_IFACE (volume);
336
337   if (iface->eject == NULL)
338     {
339       g_simple_async_report_error_in_idle (G_OBJECT (volume),
340                                            callback, user_data,
341                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
342                                            _("volume doesn't implement eject"));
343       
344       return;
345     }
346   
347   (* iface->eject) (volume, cancellable, callback, user_data);
348 }
349
350 /**
351  * g_volume_eject_finish:
352  * @volume: a #GVolume.
353  * @result: a #GAsyncResult.
354  * @error: a #GError location to store the error occuring, or %NULL to 
355  * ignore.
356  * 
357  * Finishes ejecting the volume. If any errors occured during the operation, 
358  * @error will be set to contain the errors and %FALSE will be returned.
359  * 
360  * Returns: %TRUE if the volume was successfully ejected. %FALSE otherwise.
361  **/
362 gboolean
363 g_volume_eject_finish (GVolume              *volume,
364                        GAsyncResult         *result,
365                        GError              **error)
366 {
367   GVolumeIface *iface;
368
369   g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
370   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
371
372   if (G_IS_SIMPLE_ASYNC_RESULT (result))
373     {
374       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
375       if (g_simple_async_result_propagate_error (simple, error))
376         return FALSE;
377     }
378   
379   iface = G_VOLUME_GET_IFACE (volume);
380   return (* iface->eject_finish) (volume, result, error);
381 }
382
383 #define __G_VOLUME_C__
384 #include "gioaliasdef.c"