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