Fix up includes in section docs
[platform/upstream/glib.git] / gio / gmount.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2
3 /* GIO - GLib Input, Output and Streaming Library
4  * 
5  * Copyright (C) 2006-2007 Red Hat, Inc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General
18  * Public License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * Author: Alexander Larsson <alexl@redhat.com>
23  *         David Zeuthen <davidz@redhat.com>
24  */
25
26 #include <config.h>
27
28 #include <string.h>
29
30 #include "gmount.h"
31 #include "gmountprivate.h"
32 #include "gsimpleasyncresult.h"
33 #include "glibintl.h"
34
35 #include "gioalias.h"
36
37 /**
38  * SECTION:gmount
39  * @short_description: Mount management
40  * @include: gio.h
41  * 
42  * The #GMount interface represents user-visible mounts. Note, when 
43  * porting from GnomeVFS, #GMount is the moral equivalent of #GnomeVFSVolume.
44  * 
45  * Unmounting a #GMount instance is an asynchronous operation. For
46  * more information about asynchronous operations, see #GAsyncReady
47  * and #GSimpleAsyncReady. To unmount a #GMount instance, first call
48  * g_mount_unmount() with (at least) the #GMount instance and a
49  * #GAsyncReadyCallback.  The callback will be fired when the
50  * operation has resolved (either with success or failure), and a
51  * #GAsyncReady structure will be passed to the callback.  That
52  * callback should then call g_mount_unmount_finish() with the #GMount
53  * and the #GAsyncReady data to see if the operation was completed
54  * successfully.  If an @error is present when g_mount_unmount_finish() 
55  * is called, then it will be filled with any error information.
56  **/
57
58 static void g_mount_base_init (gpointer g_class);
59 static void g_mount_class_init (gpointer g_class,
60                                 gpointer class_data);
61
62 GType
63 g_mount_get_type (void)
64 {
65   static GType mount_type = 0;
66
67   if (! mount_type)
68     {
69       static const GTypeInfo mount_info =
70       {
71         sizeof (GMountIface), /* class_size */
72         g_mount_base_init,   /* base_init */
73         NULL,           /* base_finalize */
74         g_mount_class_init,
75         NULL,           /* class_finalize */
76         NULL,           /* class_data */
77         0,
78         0,              /* n_preallocs */
79         NULL
80       };
81
82       mount_type =
83         g_type_register_static (G_TYPE_INTERFACE, I_("GMount"),
84                                 &mount_info, 0);
85
86       g_type_interface_add_prerequisite (mount_type, G_TYPE_OBJECT);
87     }
88
89   return mount_type;
90 }
91
92 static void
93 g_mount_class_init (gpointer g_class,
94                     gpointer class_data)
95 {
96 }
97
98 static void
99 g_mount_base_init (gpointer g_class)
100 {
101   static gboolean initialized = FALSE;
102
103   if (! initialized)
104     {
105      /**
106       * GMount::changed:
107       * 
108       * Emitted when the mount has been changed.
109       **/
110       g_signal_new (I_("changed"),
111                     G_TYPE_MOUNT,
112                     G_SIGNAL_RUN_LAST,
113                     G_STRUCT_OFFSET (GMountIface, 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_mount_get_root:
124  * @mount: a #GMount.
125  * 
126  * Gets the root directory on @mount.
127  * 
128  * Returns: a #GFile.
129  **/
130 GFile *
131 g_mount_get_root (GMount *mount)
132 {
133   GMountIface *iface;
134
135   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
136
137   iface = G_MOUNT_GET_IFACE (mount);
138
139   return (* iface->get_root) (mount);
140 }
141
142 /**
143  * g_mount_get_name:
144  * @mount: a #GMount.
145  * 
146  * Gets the name of @mount.
147  * 
148  * Returns: the name for the given @mount. The returned string should 
149  * be freed when no longer needed.
150  **/
151 char *
152 g_mount_get_name (GMount *mount)
153 {
154   GMountIface *iface;
155
156   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
157
158   iface = G_MOUNT_GET_IFACE (mount);
159
160   return (* iface->get_name) (mount);
161 }
162
163 /**
164  * g_mount_get_icon:
165  * @mount: a #GMount.
166  * 
167  * Gets the icon for @mount.
168  * 
169  * Returns: a #GIcon.
170  **/
171 GIcon *
172 g_mount_get_icon (GMount *mount)
173 {
174   GMountIface *iface;
175
176   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
177
178   iface = G_MOUNT_GET_IFACE (mount);
179
180   return (* iface->get_icon) (mount);
181 }
182
183 /**
184  * g_mount_get_uuid:
185  * @mount: a #GMount.
186  * 
187  * Gets the UUID for the @mount. The reference is typically based on
188  * the file system UUID for the mount in question and should be
189  * considered an opaque string. Returns %NULL if there is no UUID
190  * available.
191  * 
192  * Returns: the UUID for @mount or %NULL if no UUID can be computed.
193  **/
194 char *
195 g_mount_get_uuid (GMount *mount)
196 {
197   GMountIface *iface;
198
199   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
200
201   iface = G_MOUNT_GET_IFACE (mount);
202
203   return (* iface->get_uuid) (mount);
204 }
205
206 /**
207  * g_mount_get_volume:
208  * @mount: a #GMount.
209  * 
210  * Gets the volume for the @mount.
211  * 
212  * Returns: a #GVolume or %NULL if @mount is not associated with a volume.
213  **/
214 GVolume *
215 g_mount_get_volume (GMount *mount)
216 {
217   GMountIface *iface;
218
219   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
220
221   iface = G_MOUNT_GET_IFACE (mount);
222
223   return (* iface->get_volume) (mount);
224 }
225
226 /**
227  * g_mount_get_drive:
228  * @mount: a #GMount.
229  * 
230  * Gets the drive for the @mount.
231  *
232  * This is a convenience method for getting the #GVolume and then
233  * using that object to get the #GDrive.
234  * 
235  * Returns: a #GDrive or %NULL if @mount is not associated with a volume or a drive.
236  **/
237 GDrive *
238 g_mount_get_drive (GMount *mount)
239 {
240   GMountIface *iface;
241
242   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
243
244   iface = G_MOUNT_GET_IFACE (mount);
245
246   return (* iface->get_drive) (mount);
247 }
248
249 /**
250  * g_mount_can_unmount: 
251  * @mount: a #GMount.
252  * 
253  * Checks if @mount can be mounted.
254  * 
255  * Returns: %TRUE if the @mount can be unmounted.
256  **/
257 gboolean
258 g_mount_can_unmount (GMount *mount)
259 {
260   GMountIface *iface;
261
262   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
263
264   iface = G_MOUNT_GET_IFACE (mount);
265
266   return (* iface->can_unmount) (mount);
267 }
268
269 /**
270  * g_mount_can_eject: 
271  * @mount: a #GMount.
272  * 
273  * Checks if @mount can be eject.
274  * 
275  * Returns: %TRUE if the @mount can be ejected.
276  **/
277 gboolean
278 g_mount_can_eject (GMount *mount)
279 {
280   GMountIface *iface;
281
282   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
283
284   iface = G_MOUNT_GET_IFACE (mount);
285
286   return (* iface->can_eject) (mount);
287 }
288
289 /**
290  * g_mount_unmount:
291  * @mount: a #GMount.
292  * @cancellable: optional #GCancellable object, %NULL to ignore.
293  * @callback: a #GAsyncReadyCallback.
294  * @user_data: user data passed to @callback.
295  * 
296  * Unmounts a mount. This is an asynchronous operation, and is 
297  * finished by calling g_mount_unmount_finish() with the @mount 
298  * and #GAsyncResults data returned in the @callback.
299  **/
300 void
301 g_mount_unmount (GMount             *mount,
302                  GCancellable        *cancellable,
303                  GAsyncReadyCallback  callback,
304                  gpointer             user_data)
305 {
306   GMountIface *iface;
307
308   g_return_if_fail (G_IS_MOUNT (mount));
309   
310   iface = G_MOUNT_GET_IFACE (mount);
311
312   if (iface->unmount == NULL)
313     {
314       g_simple_async_report_error_in_idle (G_OBJECT (mount),
315                                            callback, user_data,
316                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
317                                            _("mount doesn't implement unmount"));
318       
319       return;
320     }
321   
322   (* iface->unmount) (mount, cancellable, callback, user_data);
323 }
324
325 /**
326  * g_mount_unmount_finish:
327  * @mount: a #GMount.
328  * @result: a #GAsyncResult.
329  * @error: a #GError location to store the error occuring, or %NULL to 
330  * ignore.
331  * 
332  * Finishes unmounting a mount. If any errors occured during the operation, 
333  * @error will be set to contain the errors and %FALSE will be returned.
334  * 
335  * Returns: %TRUE if the mount was successfully unmounted. %FALSE otherwise.
336  **/
337 gboolean
338 g_mount_unmount_finish (GMount       *mount,
339                         GAsyncResult  *result,
340                         GError       **error)
341 {
342   GMountIface *iface;
343
344   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
345   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
346
347   if (G_IS_SIMPLE_ASYNC_RESULT (result))
348     {
349       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
350       if (g_simple_async_result_propagate_error (simple, error))
351         return FALSE;
352     }
353   
354   iface = G_MOUNT_GET_IFACE (mount);
355   return (* iface->unmount_finish) (mount, result, error);
356 }
357
358
359 /**
360  * g_mount_eject:
361  * @mount: a #GMount.
362  * @cancellable: optional #GCancellable object, %NULL to ignore.
363  * @callback: a #GAsyncReadyCallback.
364  * @user_data: user data passed to @callback.
365  * 
366  * Ejects a mount. This is an asynchronous operation, and is 
367  * finished by calling g_mount_eject_finish() with the @mount 
368  * and #GAsyncResults data returned in the @callback.
369  **/
370 void
371 g_mount_eject (GMount             *mount,
372                GCancellable        *cancellable,
373                GAsyncReadyCallback  callback,
374                gpointer             user_data)
375 {
376   GMountIface *iface;
377
378   g_return_if_fail (G_IS_MOUNT (mount));
379   
380   iface = G_MOUNT_GET_IFACE (mount);
381
382   if (iface->eject == NULL)
383     {
384       g_simple_async_report_error_in_idle (G_OBJECT (mount),
385                                            callback, user_data,
386                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
387                                            _("mount doesn't implement eject"));
388       
389       return;
390     }
391   
392   (* iface->eject) (mount, cancellable, callback, user_data);
393 }
394
395 /**
396  * g_mount_eject_finish:
397  * @mount: a #GMount.
398  * @result: a #GAsyncResult.
399  * @error: a #GError location to store the error occuring, or %NULL to 
400  * ignore.
401  * 
402  * Finishes ejecting a mount. If any errors occured during the operation, 
403  * @error will be set to contain the errors and %FALSE will be returned.
404  * 
405  * Returns: %TRUE if the mount was successfully ejected. %FALSE otherwise.
406  **/
407 gboolean
408 g_mount_eject_finish (GMount       *mount,
409                       GAsyncResult  *result,
410                       GError       **error)
411 {
412   GMountIface *iface;
413
414   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
415   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
416
417   if (G_IS_SIMPLE_ASYNC_RESULT (result))
418     {
419       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
420       if (g_simple_async_result_propagate_error (simple, error))
421         return FALSE;
422     }
423   
424   iface = G_MOUNT_GET_IFACE (mount);
425   return (* iface->eject_finish) (mount, result, error);
426 }
427
428 #define __G_MOUNT_C__
429 #include "gioaliasdef.c"