documentation: Mention g_mount_remount_finish() instead of
[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  * @see also: GVolume, GUnixMount
42  *
43  * The #GMount interface represents user-visible mounts. Note, when 
44  * porting from GnomeVFS, #GMount is the moral equivalent of #GnomeVFSVolume.
45  *
46  * #GMount is a "mounted" filesystem that you can access. Mounted is in
47  * quotes because it's not the same as a unix mount, it might be a gvfs
48  * mount, but you can still access the files on it if you use GIO. Might or
49  * might not be related to a volume object.
50  * 
51  * Unmounting a #GMount instance is an asynchronous operation. For
52  * more information about asynchronous operations, see #GAsyncReady
53  * and #GSimpleAsyncReady. To unmount a #GMount instance, first call
54  * g_mount_unmount() with (at least) the #GMount instance and a
55  * #GAsyncReadyCallback.  The callback will be fired when the
56  * operation has resolved (either with success or failure), and a
57  * #GAsyncReady structure will be passed to the callback.  That
58  * callback should then call g_mount_unmount_finish() with the #GMount
59  * and the #GAsyncReady data to see if the operation was completed
60  * successfully.  If an @error is present when g_mount_unmount_finish() 
61  * is called, then it will be filled with any error information.
62  **/
63
64 static void g_mount_base_init (gpointer g_class);
65 static void g_mount_class_init (gpointer g_class,
66                                 gpointer class_data);
67
68 GType
69 g_mount_get_type (void)
70 {
71   static GType mount_type = 0;
72
73   if (! mount_type)
74     {
75       static const GTypeInfo mount_info =
76       {
77         sizeof (GMountIface), /* class_size */
78         g_mount_base_init,   /* base_init */
79         NULL,           /* base_finalize */
80         g_mount_class_init,
81         NULL,           /* class_finalize */
82         NULL,           /* class_data */
83         0,
84         0,              /* n_preallocs */
85         NULL
86       };
87
88       mount_type =
89         g_type_register_static (G_TYPE_INTERFACE, I_("GMount"),
90                                 &mount_info, 0);
91
92       g_type_interface_add_prerequisite (mount_type, G_TYPE_OBJECT);
93     }
94
95   return mount_type;
96 }
97
98 static void
99 g_mount_class_init (gpointer g_class,
100                     gpointer class_data)
101 {
102 }
103
104 static void
105 g_mount_base_init (gpointer g_class)
106 {
107   static gboolean initialized = FALSE;
108
109   if (! initialized)
110     {
111      /**
112       * GMount::changed:
113       * 
114       * Emitted when the mount has been changed.
115       **/
116       g_signal_new (I_("changed"),
117                     G_TYPE_MOUNT,
118                     G_SIGNAL_RUN_LAST,
119                     G_STRUCT_OFFSET (GMountIface, changed),
120                     NULL, NULL,
121                     g_cclosure_marshal_VOID__VOID,
122                     G_TYPE_NONE, 0);
123
124      /**
125       * GMount::unmounted:
126       * 
127       * This signal is emitted when the #GMount have been
128       * unmounted. If the recipient is holding references to the
129       * object they should release them so the object can be
130       * finalized.
131       **/
132       g_signal_new (I_("unmounted"),
133                     G_TYPE_MOUNT,
134                     G_SIGNAL_RUN_LAST,
135                     G_STRUCT_OFFSET (GMountIface, unmounted),
136                     NULL, NULL,
137                     g_cclosure_marshal_VOID__VOID,
138                     G_TYPE_NONE, 0);
139
140       initialized = TRUE;
141     }
142 }
143
144 /**
145  * g_mount_get_root:
146  * @mount: a #GMount.
147  * 
148  * Gets the root directory on @mount.
149  * 
150  * Returns: a #GFile.
151  **/
152 GFile *
153 g_mount_get_root (GMount *mount)
154 {
155   GMountIface *iface;
156
157   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
158
159   iface = G_MOUNT_GET_IFACE (mount);
160
161   return (* iface->get_root) (mount);
162 }
163
164 /**
165  * g_mount_get_name:
166  * @mount: a #GMount.
167  * 
168  * Gets the name of @mount.
169  * 
170  * Returns: the name for the given @mount. The returned string should 
171  * be freed when no longer needed.
172  **/
173 char *
174 g_mount_get_name (GMount *mount)
175 {
176   GMountIface *iface;
177
178   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
179
180   iface = G_MOUNT_GET_IFACE (mount);
181
182   return (* iface->get_name) (mount);
183 }
184
185 /**
186  * g_mount_get_icon:
187  * @mount: a #GMount.
188  * 
189  * Gets the icon for @mount.
190  * 
191  * Returns: a #GIcon.
192  **/
193 GIcon *
194 g_mount_get_icon (GMount *mount)
195 {
196   GMountIface *iface;
197
198   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
199
200   iface = G_MOUNT_GET_IFACE (mount);
201
202   return (* iface->get_icon) (mount);
203 }
204
205 /**
206  * g_mount_get_uuid:
207  * @mount: a #GMount.
208  * 
209  * Gets the UUID for the @mount. The reference is typically based on
210  * the file system UUID for the mount in question and should be
211  * considered an opaque string. Returns %NULL if there is no UUID
212  * available.
213  * 
214  * Returns: the UUID for @mount or %NULL if no UUID can be computed.
215  **/
216 char *
217 g_mount_get_uuid (GMount *mount)
218 {
219   GMountIface *iface;
220
221   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
222
223   iface = G_MOUNT_GET_IFACE (mount);
224
225   return (* iface->get_uuid) (mount);
226 }
227
228 /**
229  * g_mount_get_volume:
230  * @mount: a #GMount.
231  * 
232  * Gets the volume for the @mount.
233  * 
234  * Returns: a #GVolume or %NULL if @mount is not associated with a volume.
235  **/
236 GVolume *
237 g_mount_get_volume (GMount *mount)
238 {
239   GMountIface *iface;
240
241   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
242
243   iface = G_MOUNT_GET_IFACE (mount);
244
245   return (* iface->get_volume) (mount);
246 }
247
248 /**
249  * g_mount_get_drive:
250  * @mount: a #GMount.
251  * 
252  * Gets the drive for the @mount.
253  *
254  * This is a convenience method for getting the #GVolume and then
255  * using that object to get the #GDrive.
256  * 
257  * Returns: a #GDrive or %NULL if @mount is not associated with a volume or a drive.
258  **/
259 GDrive *
260 g_mount_get_drive (GMount *mount)
261 {
262   GMountIface *iface;
263
264   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
265
266   iface = G_MOUNT_GET_IFACE (mount);
267
268   return (* iface->get_drive) (mount);
269 }
270
271 /**
272  * g_mount_can_unmount: 
273  * @mount: a #GMount.
274  * 
275  * Checks if @mount can be mounted.
276  * 
277  * Returns: %TRUE if the @mount can be unmounted.
278  **/
279 gboolean
280 g_mount_can_unmount (GMount *mount)
281 {
282   GMountIface *iface;
283
284   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
285
286   iface = G_MOUNT_GET_IFACE (mount);
287
288   return (* iface->can_unmount) (mount);
289 }
290
291 /**
292  * g_mount_can_eject: 
293  * @mount: a #GMount.
294  * 
295  * Checks if @mount can be eject.
296  * 
297  * Returns: %TRUE if the @mount can be ejected.
298  **/
299 gboolean
300 g_mount_can_eject (GMount *mount)
301 {
302   GMountIface *iface;
303
304   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
305
306   iface = G_MOUNT_GET_IFACE (mount);
307
308   return (* iface->can_eject) (mount);
309 }
310
311 /**
312  * g_mount_unmount:
313  * @mount: a #GMount.
314  * @flags: flags affecting the operation
315  * @cancellable: optional #GCancellable object, %NULL to ignore.
316  * @callback: a #GAsyncReadyCallback, or %NULL.
317  * @user_data: user data passed to @callback.
318  * 
319  * Unmounts a mount. This is an asynchronous operation, and is 
320  * finished by calling g_mount_unmount_finish() with the @mount 
321  * and #GAsyncResults data returned in the @callback.
322  **/
323 void
324 g_mount_unmount (GMount *mount,
325                  GMountUnmountFlags flags,
326                  GCancellable *cancellable,
327                  GAsyncReadyCallback callback,
328                  gpointer user_data)
329 {
330   GMountIface *iface;
331
332   g_return_if_fail (G_IS_MOUNT (mount));
333   
334   iface = G_MOUNT_GET_IFACE (mount);
335
336   if (iface->unmount == NULL)
337     {
338       g_simple_async_report_error_in_idle (G_OBJECT (mount),
339                                            callback, user_data,
340                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
341                                            _("mount doesn't implement unmount"));
342       
343       return;
344     }
345   
346   (* iface->unmount) (mount, flags, cancellable, callback, user_data);
347 }
348
349 /**
350  * g_mount_unmount_finish:
351  * @mount: a #GMount.
352  * @result: a #GAsyncResult.
353  * @error: a #GError location to store the error occuring, or %NULL to 
354  * ignore.
355  * 
356  * Finishes unmounting a mount. If any errors occurred during the operation, 
357  * @error will be set to contain the errors and %FALSE will be returned.
358  * 
359  * Returns: %TRUE if the mount was successfully unmounted. %FALSE otherwise.
360  **/
361 gboolean
362 g_mount_unmount_finish (GMount       *mount,
363                         GAsyncResult  *result,
364                         GError       **error)
365 {
366   GMountIface *iface;
367
368   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
369   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
370
371   if (G_IS_SIMPLE_ASYNC_RESULT (result))
372     {
373       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
374       if (g_simple_async_result_propagate_error (simple, error))
375         return FALSE;
376     }
377   
378   iface = G_MOUNT_GET_IFACE (mount);
379   return (* iface->unmount_finish) (mount, result, error);
380 }
381
382
383 /**
384  * g_mount_eject:
385  * @mount: a #GMount.
386  * @flags: flags affecting the unmount if required for eject
387  * @cancellable: optional #GCancellable object, %NULL to ignore.
388  * @callback: a #GAsyncReadyCallback, or %NULL.
389  * @user_data: user data passed to @callback.
390  * 
391  * Ejects a mount. This is an asynchronous operation, and is 
392  * finished by calling g_mount_eject_finish() with the @mount 
393  * and #GAsyncResults data returned in the @callback.
394  **/
395 void
396 g_mount_eject (GMount *mount,
397                GMountUnmountFlags flags,
398                GCancellable *cancellable,
399                GAsyncReadyCallback callback,
400                gpointer user_data)
401 {
402   GMountIface *iface;
403
404   g_return_if_fail (G_IS_MOUNT (mount));
405   
406   iface = G_MOUNT_GET_IFACE (mount);
407
408   if (iface->eject == NULL)
409     {
410       g_simple_async_report_error_in_idle (G_OBJECT (mount),
411                                            callback, user_data,
412                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
413                                            _("mount doesn't implement eject"));
414       
415       return;
416     }
417   
418   (* iface->eject) (mount, flags, cancellable, callback, user_data);
419 }
420
421 /**
422  * g_mount_eject_finish:
423  * @mount: a #GMount.
424  * @result: a #GAsyncResult.
425  * @error: a #GError location to store the error occuring, or %NULL to 
426  * ignore.
427  * 
428  * Finishes ejecting a mount. If any errors occurred during the operation, 
429  * @error will be set to contain the errors and %FALSE will be returned.
430  * 
431  * Returns: %TRUE if the mount was successfully ejected. %FALSE otherwise.
432  **/
433 gboolean
434 g_mount_eject_finish (GMount       *mount,
435                       GAsyncResult  *result,
436                       GError       **error)
437 {
438   GMountIface *iface;
439
440   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
441   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
442
443   if (G_IS_SIMPLE_ASYNC_RESULT (result))
444     {
445       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
446       if (g_simple_async_result_propagate_error (simple, error))
447         return FALSE;
448     }
449   
450   iface = G_MOUNT_GET_IFACE (mount);
451   return (* iface->eject_finish) (mount, result, error);
452 }
453
454 /**
455  * g_mount_remount:
456  * @mount: a #GMount.
457  * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
458  * @cancellable: optional #GCancellable object, %NULL to ignore.
459  * @callback: a #GAsyncReadyCallback, or %NULL.
460  * @user_data: user data passed to @callback.
461  * 
462  * Remounts a mount. This is an asynchronous operation, and is 
463  * finished by calling g_mount_remount_finish() with the @mount 
464  * and #GAsyncResults data returned in the @callback.
465  *
466  * Remounting is useful when some setting affecting the operation
467  * of the volume has been changed, as these may need a remount to
468  * take affect. While this is semantically equivalent with unmounting
469  * and then remounting not all backends might need to actually be
470  * unmounted.
471  **/
472 void
473 g_mount_remount (GMount *mount,
474                  GMountOperation *mount_operation,
475                  GCancellable *cancellable,
476                  GAsyncReadyCallback callback,
477                  gpointer user_data)
478 {
479   GMountIface *iface;
480
481   g_return_if_fail (G_IS_MOUNT (mount));
482   
483   iface = G_MOUNT_GET_IFACE (mount);
484
485   if (iface->remount == NULL)
486     {
487       g_simple_async_report_error_in_idle (G_OBJECT (mount),
488                                            callback, user_data,
489                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
490                                            _("mount doesn't implement remount"));
491       
492       return;
493     }
494   
495   (* iface->remount) (mount, mount_operation, cancellable, callback, user_data);
496 }
497
498 /**
499  * g_mount_remount_finish:
500  * @mount: a #GMount.
501  * @result: a #GAsyncResult.
502  * @error: a #GError location to store the error occuring, or %NULL to 
503  * ignore.
504  * 
505  * Finishes remounting a mount. If any errors occurred during the operation, 
506  * @error will be set to contain the errors and %FALSE will be returned.
507  * 
508  * Returns: %TRUE if the mount was successfully remounted. %FALSE otherwise.
509  **/
510 gboolean
511 g_mount_remount_finish (GMount       *mount,
512                         GAsyncResult  *result,
513                         GError       **error)
514 {
515   GMountIface *iface;
516
517   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
518   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
519
520   if (G_IS_SIMPLE_ASYNC_RESULT (result))
521     {
522       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
523       if (g_simple_async_result_propagate_error (simple, error))
524         return FALSE;
525     }
526   
527   iface = G_MOUNT_GET_IFACE (mount);
528   return (* iface->remount_finish) (mount, result, error);
529 }
530
531
532 #define __G_MOUNT_C__
533 #include "gioaliasdef.c"