Documentation improvements.
[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-2008 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 "gasyncresult.h"
33 #include "gsimpleasyncresult.h"
34 #include "gioerror.h"
35 #include "glibintl.h"
36
37 #include "gioalias.h"
38
39 /**
40  * SECTION:gmount
41  * @short_description: Mount management
42  * @include: gio/gio.h
43  * @see also: GVolume, GUnixMount
44  *
45  * The #GMount interface represents user-visible mounts. Note, when 
46  * porting from GnomeVFS, #GMount is the moral equivalent of #GnomeVFSVolume.
47  *
48  * #GMount is a "mounted" filesystem that you can access. Mounted is in
49  * quotes because it's not the same as a unix mount, it might be a gvfs
50  * mount, but you can still access the files on it if you use GIO. Might or
51  * might not be related to a volume object.
52  * 
53  * Unmounting a #GMount instance is an asynchronous operation. For
54  * more information about asynchronous operations, see #GAsyncReady
55  * and #GSimpleAsyncReady. To unmount a #GMount instance, first call
56  * g_mount_unmount() with (at least) the #GMount instance and a
57  * #GAsyncReadyCallback.  The callback will be fired when the
58  * operation has resolved (either with success or failure), and a
59  * #GAsyncReady structure will be passed to the callback.  That
60  * callback should then call g_mount_unmount_finish() with the #GMount
61  * and the #GAsyncReady data to see if the operation was completed
62  * successfully.  If an @error is present when g_mount_unmount_finish() 
63  * is called, then it will be filled with any error information.
64  **/
65
66 static void g_mount_base_init (gpointer g_class);
67 static void g_mount_class_init (gpointer g_class,
68                                 gpointer class_data);
69
70 GType
71 g_mount_get_type (void)
72 {
73   static GType mount_type = 0;
74
75   if (! mount_type)
76     {
77       static const GTypeInfo mount_info =
78       {
79         sizeof (GMountIface), /* class_size */
80         g_mount_base_init,   /* base_init */
81         NULL,           /* base_finalize */
82         g_mount_class_init,
83         NULL,           /* class_finalize */
84         NULL,           /* class_data */
85         0,
86         0,              /* n_preallocs */
87         NULL
88       };
89
90       mount_type =
91         g_type_register_static (G_TYPE_INTERFACE, I_("GMount"),
92                                 &mount_info, 0);
93
94       g_type_interface_add_prerequisite (mount_type, G_TYPE_OBJECT);
95     }
96
97   return mount_type;
98 }
99
100 static void
101 g_mount_class_init (gpointer g_class,
102                     gpointer class_data)
103 {
104 }
105
106 static void
107 g_mount_base_init (gpointer g_class)
108 {
109   static gboolean initialized = FALSE;
110
111   if (! initialized)
112     {
113      /**
114       * GMount::changed:
115       * 
116       * Emitted when the mount has been changed.
117       **/
118       g_signal_new (I_("changed"),
119                     G_TYPE_MOUNT,
120                     G_SIGNAL_RUN_LAST,
121                     G_STRUCT_OFFSET (GMountIface, changed),
122                     NULL, NULL,
123                     g_cclosure_marshal_VOID__VOID,
124                     G_TYPE_NONE, 0);
125
126      /**
127       * GMount::unmounted:
128       * 
129       * This signal is emitted when the #GMount have been
130       * unmounted. If the recipient is holding references to the
131       * object they should release them so the object can be
132       * finalized.
133       **/
134       g_signal_new (I_("unmounted"),
135                     G_TYPE_MOUNT,
136                     G_SIGNAL_RUN_LAST,
137                     G_STRUCT_OFFSET (GMountIface, unmounted),
138                     NULL, NULL,
139                     g_cclosure_marshal_VOID__VOID,
140                     G_TYPE_NONE, 0);
141
142       initialized = TRUE;
143     }
144 }
145
146 /**
147  * g_mount_get_root:
148  * @mount: a #GMount.
149  * 
150  * Gets the root directory on @mount.
151  * 
152  * Returns: a #GFile. 
153  *      The returned object should be unreffed with 
154  *      g_object_unref() when no longer needed.
155  **/
156 GFile *
157 g_mount_get_root (GMount *mount)
158 {
159   GMountIface *iface;
160
161   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
162
163   iface = G_MOUNT_GET_IFACE (mount);
164
165   return (* iface->get_root) (mount);
166 }
167
168 /**
169  * g_mount_get_name:
170  * @mount: a #GMount.
171  * 
172  * Gets the name of @mount.
173  * 
174  * Returns: the name for the given @mount. 
175  *     The returned string should be freed with g_free()
176  *     when no longer needed.
177  **/
178 char *
179 g_mount_get_name (GMount *mount)
180 {
181   GMountIface *iface;
182
183   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
184
185   iface = G_MOUNT_GET_IFACE (mount);
186
187   return (* iface->get_name) (mount);
188 }
189
190 /**
191  * g_mount_get_icon:
192  * @mount: a #GMount.
193  * 
194  * Gets the icon for @mount.
195  * 
196  * Returns: a #GIcon.
197  *      The returned object should be unreffed with 
198  *      g_object_unref() when no longer needed.
199  **/
200 GIcon *
201 g_mount_get_icon (GMount *mount)
202 {
203   GMountIface *iface;
204
205   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
206
207   iface = G_MOUNT_GET_IFACE (mount);
208
209   return (* iface->get_icon) (mount);
210 }
211
212 /**
213  * g_mount_get_uuid:
214  * @mount: a #GMount.
215  * 
216  * Gets the UUID for the @mount. The reference is typically based on
217  * the file system UUID for the mount in question and should be
218  * considered an opaque string. Returns %NULL if there is no UUID
219  * available.
220  * 
221  * Returns: the UUID for @mount or %NULL if no UUID can be computed.
222  *     The returned string should be freed with g_free()
223  *     when no longer needed.
224  **/
225 char *
226 g_mount_get_uuid (GMount *mount)
227 {
228   GMountIface *iface;
229
230   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
231
232   iface = G_MOUNT_GET_IFACE (mount);
233
234   return (* iface->get_uuid) (mount);
235 }
236
237 /**
238  * g_mount_get_volume:
239  * @mount: a #GMount.
240  * 
241  * Gets the volume for the @mount.
242  * 
243  * Returns: a #GVolume or %NULL if @mount is not associated with a volume.
244  *      The returned object should be unreffed with 
245  *      g_object_unref() when no longer needed.
246  **/
247 GVolume *
248 g_mount_get_volume (GMount *mount)
249 {
250   GMountIface *iface;
251
252   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
253
254   iface = G_MOUNT_GET_IFACE (mount);
255
256   return (* iface->get_volume) (mount);
257 }
258
259 /**
260  * g_mount_get_drive:
261  * @mount: a #GMount.
262  * 
263  * Gets the drive for the @mount.
264  *
265  * This is a convenience method for getting the #GVolume and then
266  * using that object to get the #GDrive.
267  * 
268  * Returns: a #GDrive or %NULL if @mount is not associated with a volume or a drive.
269  *      The returned object should be unreffed with 
270  *      g_object_unref() when no longer needed.
271  **/
272 GDrive *
273 g_mount_get_drive (GMount *mount)
274 {
275   GMountIface *iface;
276
277   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
278
279   iface = G_MOUNT_GET_IFACE (mount);
280
281   return (* iface->get_drive) (mount);
282 }
283
284 /**
285  * g_mount_can_unmount: 
286  * @mount: a #GMount.
287  * 
288  * Checks if @mount can be mounted.
289  * 
290  * Returns: %TRUE if the @mount can be unmounted.
291  **/
292 gboolean
293 g_mount_can_unmount (GMount *mount)
294 {
295   GMountIface *iface;
296
297   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
298
299   iface = G_MOUNT_GET_IFACE (mount);
300
301   return (* iface->can_unmount) (mount);
302 }
303
304 /**
305  * g_mount_can_eject: 
306  * @mount: a #GMount.
307  * 
308  * Checks if @mount can be eject.
309  * 
310  * Returns: %TRUE if the @mount can be ejected.
311  **/
312 gboolean
313 g_mount_can_eject (GMount *mount)
314 {
315   GMountIface *iface;
316
317   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
318
319   iface = G_MOUNT_GET_IFACE (mount);
320
321   return (* iface->can_eject) (mount);
322 }
323
324 /**
325  * g_mount_unmount:
326  * @mount: a #GMount.
327  * @flags: flags affecting the operation
328  * @cancellable: optional #GCancellable object, %NULL to ignore.
329  * @callback: a #GAsyncReadyCallback, or %NULL.
330  * @user_data: user data passed to @callback.
331  * 
332  * Unmounts a mount. This is an asynchronous operation, and is 
333  * finished by calling g_mount_unmount_finish() with the @mount 
334  * and #GAsyncResult data returned in the @callback.
335  **/
336 void
337 g_mount_unmount (GMount              *mount,
338                  GMountUnmountFlags   flags,
339                  GCancellable        *cancellable,
340                  GAsyncReadyCallback  callback,
341                  gpointer             user_data)
342 {
343   GMountIface *iface;
344
345   g_return_if_fail (G_IS_MOUNT (mount));
346   
347   iface = G_MOUNT_GET_IFACE (mount);
348
349   if (iface->unmount == NULL)
350     {
351       g_simple_async_report_error_in_idle (G_OBJECT (mount),
352                                            callback, user_data,
353                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
354                                            /* Translators: This is an error
355                                             * message for mount objects that
356                                             * don't implement unmount. */
357                                            _("mount doesn't implement unmount"));
358
359       return;
360     }
361   
362   (* iface->unmount) (mount, flags, cancellable, callback, user_data);
363 }
364
365 /**
366  * g_mount_unmount_finish:
367  * @mount: a #GMount.
368  * @result: a #GAsyncResult.
369  * @error: a #GError location to store the error occuring, or %NULL to 
370  * ignore.
371  * 
372  * Finishes unmounting a mount. If any errors occurred during the operation, 
373  * @error will be set to contain the errors and %FALSE will be returned.
374  * 
375  * Returns: %TRUE if the mount was successfully unmounted. %FALSE otherwise.
376  **/
377 gboolean
378 g_mount_unmount_finish (GMount        *mount,
379                         GAsyncResult  *result,
380                         GError       **error)
381 {
382   GMountIface *iface;
383
384   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
385   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
386
387   if (G_IS_SIMPLE_ASYNC_RESULT (result))
388     {
389       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
390       if (g_simple_async_result_propagate_error (simple, error))
391         return FALSE;
392     }
393   
394   iface = G_MOUNT_GET_IFACE (mount);
395   return (* iface->unmount_finish) (mount, result, error);
396 }
397
398
399 /**
400  * g_mount_eject:
401  * @mount: a #GMount.
402  * @flags: flags affecting the unmount if required for eject
403  * @cancellable: optional #GCancellable object, %NULL to ignore.
404  * @callback: a #GAsyncReadyCallback, or %NULL.
405  * @user_data: user data passed to @callback.
406  * 
407  * Ejects a mount. This is an asynchronous operation, and is 
408  * finished by calling g_mount_eject_finish() with the @mount 
409  * and #GAsyncResult data returned in the @callback.
410  **/
411 void
412 g_mount_eject (GMount              *mount,
413                GMountUnmountFlags   flags,
414                GCancellable        *cancellable,
415                GAsyncReadyCallback  callback,
416                gpointer             user_data)
417 {
418   GMountIface *iface;
419
420   g_return_if_fail (G_IS_MOUNT (mount));
421   
422   iface = G_MOUNT_GET_IFACE (mount);
423
424   if (iface->eject == NULL)
425     {
426       g_simple_async_report_error_in_idle (G_OBJECT (mount),
427                                            callback, user_data,
428                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
429                                            /* Translators: This is an error
430                                             * message for mount objects that
431                                             * don't implement eject. */
432                                            _("mount doesn't implement eject"));
433       
434       return;
435     }
436   
437   (* iface->eject) (mount, flags, cancellable, callback, user_data);
438 }
439
440 /**
441  * g_mount_eject_finish:
442  * @mount: a #GMount.
443  * @result: a #GAsyncResult.
444  * @error: a #GError location to store the error occuring, or %NULL to 
445  * ignore.
446  * 
447  * Finishes ejecting a mount. If any errors occurred during the operation, 
448  * @error will be set to contain the errors and %FALSE will be returned.
449  * 
450  * Returns: %TRUE if the mount was successfully ejected. %FALSE otherwise.
451  **/
452 gboolean
453 g_mount_eject_finish (GMount        *mount,
454                       GAsyncResult  *result,
455                       GError       **error)
456 {
457   GMountIface *iface;
458
459   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
460   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
461
462   if (G_IS_SIMPLE_ASYNC_RESULT (result))
463     {
464       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
465       if (g_simple_async_result_propagate_error (simple, error))
466         return FALSE;
467     }
468   
469   iface = G_MOUNT_GET_IFACE (mount);
470   return (* iface->eject_finish) (mount, result, error);
471 }
472
473 /**
474  * g_mount_remount:
475  * @mount: a #GMount.
476  * @flags: flags affecting the operation
477  * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
478  * @cancellable: optional #GCancellable object, %NULL to ignore.
479  * @callback: a #GAsyncReadyCallback, or %NULL.
480  * @user_data: user data passed to @callback.
481  * 
482  * Remounts a mount. This is an asynchronous operation, and is 
483  * finished by calling g_mount_remount_finish() with the @mount 
484  * and #GAsyncResults data returned in the @callback.
485  *
486  * Remounting is useful when some setting affecting the operation
487  * of the volume has been changed, as these may need a remount to
488  * take affect. While this is semantically equivalent with unmounting
489  * and then remounting not all backends might need to actually be
490  * unmounted.
491  **/
492 void
493 g_mount_remount (GMount              *mount,
494                  GMountMountFlags     flags,
495                  GMountOperation     *mount_operation,
496                  GCancellable        *cancellable,
497                  GAsyncReadyCallback  callback,
498                  gpointer             user_data)
499 {
500   GMountIface *iface;
501
502   g_return_if_fail (G_IS_MOUNT (mount));
503   
504   iface = G_MOUNT_GET_IFACE (mount);
505
506   if (iface->remount == NULL)
507     { 
508       g_simple_async_report_error_in_idle (G_OBJECT (mount),
509                                            callback, user_data,
510                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
511                                            /* Translators: This is an error
512                                             * message for mount objects that
513                                             * don't implement remount. */
514                                            _("mount doesn't implement remount"));
515       
516       return;
517     }
518   
519   (* iface->remount) (mount, flags, mount_operation, cancellable, callback, user_data);
520 }
521
522 /**
523  * g_mount_remount_finish:
524  * @mount: a #GMount.
525  * @result: a #GAsyncResult.
526  * @error: a #GError location to store the error occuring, or %NULL to 
527  * ignore.
528  * 
529  * Finishes remounting a mount. If any errors occurred during the operation, 
530  * @error will be set to contain the errors and %FALSE will be returned.
531  * 
532  * Returns: %TRUE if the mount was successfully remounted. %FALSE otherwise.
533  **/
534 gboolean
535 g_mount_remount_finish (GMount        *mount,
536                         GAsyncResult  *result,
537                         GError       **error)
538 {
539   GMountIface *iface;
540
541   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
542   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
543
544   if (G_IS_SIMPLE_ASYNC_RESULT (result))
545     {
546       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
547       if (g_simple_async_result_propagate_error (simple, error))
548         return FALSE;
549     }
550   
551   iface = G_MOUNT_GET_IFACE (mount);
552   return (* iface->remount_finish) (mount, result, error);
553 }
554
555 /**
556  * g_mount_guess_content_type:
557  * @mount: a #GMount
558  * @force_rescan: Whether to force a rescan of the content. 
559  *     Otherwise a cached result will be used if available
560  * @cancellable: optional #GCancellable object, %NULL to ignore
561  * @callback: a #GAsyncReadyCallback
562  * @user_data: user data passed to @callback
563  * 
564  * Tries to guess the type of content stored on @mount. Returns one or
565  * more textual identifiers of well-known content types (typically
566  * prefixed with "x-content/"), e.g. x-content/image-dcf for camera 
567  * memory cards. See the <ulink url="http://www.freedesktop.org/wiki/Specifications/shared-mime-info-spec">shared-mime-info</ulink>
568  * specification for more on x-content types.
569  *
570  * This is an asynchronous operation, and is finished by calling 
571  * g_mount_guess_content_type_finish() with the @mount and #GAsyncResult 
572  * data returned in the @callback. 
573  *
574  * Since: 2.18
575  */
576 void
577 g_mount_guess_content_type (GMount              *mount,
578                             gboolean             force_rescan,
579                             GCancellable        *cancellable,
580                             GAsyncReadyCallback  callback,
581                             gpointer             user_data)
582 {
583   GMountIface *iface;
584
585   g_return_if_fail (G_IS_MOUNT (mount));
586
587   iface = G_MOUNT_GET_IFACE (mount);
588
589   if (iface->guess_content_type == NULL)
590     {
591       g_simple_async_report_error_in_idle (G_OBJECT (mount),
592                                            callback, user_data,
593                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
594                                            /* Translators: This is an error
595                                             * message for mount objects that
596                                             * don't implement content type guessing. */
597                                            _("mount doesn't implement content type guessing"));
598
599       return;
600     }
601   
602   (* iface->guess_content_type) (mount, force_rescan, cancellable, callback, user_data);
603 }
604
605 /**
606  * g_mount_guess_content_type_finish:
607  * @mount: a #GMount
608  * @result: a #GAsyncResult
609  * @error: a #GError location to store the error occuring, or %NULL to 
610  *     ignore
611  * 
612  * Finishes guessing content types of @mount. If any errors occured
613  * during the operation, @error will be set to contain the errors and
614  * %FALSE will be returned. In particular, you may get an 
615  * %G_IO_ERROR_NOT_SUPPORTED if the mount does not support content 
616  * guessing.
617  * 
618  * Returns: a %NULL-terminated array of content types or %NULL on error. 
619  *     Caller should free this array with g_strfreev() when done with it.
620  *
621  * Since: 2.18
622  **/
623 gchar **
624 g_mount_guess_content_type_finish (GMount        *mount,
625                                    GAsyncResult  *result,
626                                    GError       **error)
627 {
628   GMountIface *iface;
629
630   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
631   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
632
633   if (G_IS_SIMPLE_ASYNC_RESULT (result))
634     {
635       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
636       if (g_simple_async_result_propagate_error (simple, error))
637         return FALSE;
638     }
639   
640   iface = G_MOUNT_GET_IFACE (mount);
641   return (* iface->guess_content_type_finish) (mount, result, error);
642 }
643
644
645 #define __G_MOUNT_C__
646 #include "gioaliasdef.c"