Add g_mount_guess_content_type
[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  **/
154 GFile *
155 g_mount_get_root (GMount *mount)
156 {
157   GMountIface *iface;
158
159   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
160
161   iface = G_MOUNT_GET_IFACE (mount);
162
163   return (* iface->get_root) (mount);
164 }
165
166 /**
167  * g_mount_get_name:
168  * @mount: a #GMount.
169  * 
170  * Gets the name of @mount.
171  * 
172  * Returns: the name for the given @mount. The returned string should 
173  * be freed when no longer needed.
174  **/
175 char *
176 g_mount_get_name (GMount *mount)
177 {
178   GMountIface *iface;
179
180   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
181
182   iface = G_MOUNT_GET_IFACE (mount);
183
184   return (* iface->get_name) (mount);
185 }
186
187 /**
188  * g_mount_get_icon:
189  * @mount: a #GMount.
190  * 
191  * Gets the icon for @mount.
192  * 
193  * Returns: a #GIcon.
194  **/
195 GIcon *
196 g_mount_get_icon (GMount *mount)
197 {
198   GMountIface *iface;
199
200   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
201
202   iface = G_MOUNT_GET_IFACE (mount);
203
204   return (* iface->get_icon) (mount);
205 }
206
207 /**
208  * g_mount_get_uuid:
209  * @mount: a #GMount.
210  * 
211  * Gets the UUID for the @mount. The reference is typically based on
212  * the file system UUID for the mount in question and should be
213  * considered an opaque string. Returns %NULL if there is no UUID
214  * available.
215  * 
216  * Returns: the UUID for @mount or %NULL if no UUID can be computed.
217  **/
218 char *
219 g_mount_get_uuid (GMount *mount)
220 {
221   GMountIface *iface;
222
223   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
224
225   iface = G_MOUNT_GET_IFACE (mount);
226
227   return (* iface->get_uuid) (mount);
228 }
229
230 /**
231  * g_mount_get_volume:
232  * @mount: a #GMount.
233  * 
234  * Gets the volume for the @mount.
235  * 
236  * Returns: a #GVolume or %NULL if @mount is not associated with a volume.
237  **/
238 GVolume *
239 g_mount_get_volume (GMount *mount)
240 {
241   GMountIface *iface;
242
243   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
244
245   iface = G_MOUNT_GET_IFACE (mount);
246
247   return (* iface->get_volume) (mount);
248 }
249
250 /**
251  * g_mount_get_drive:
252  * @mount: a #GMount.
253  * 
254  * Gets the drive for the @mount.
255  *
256  * This is a convenience method for getting the #GVolume and then
257  * using that object to get the #GDrive.
258  * 
259  * Returns: a #GDrive or %NULL if @mount is not associated with a volume or a drive.
260  **/
261 GDrive *
262 g_mount_get_drive (GMount *mount)
263 {
264   GMountIface *iface;
265
266   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
267
268   iface = G_MOUNT_GET_IFACE (mount);
269
270   return (* iface->get_drive) (mount);
271 }
272
273 /**
274  * g_mount_can_unmount: 
275  * @mount: a #GMount.
276  * 
277  * Checks if @mount can be mounted.
278  * 
279  * Returns: %TRUE if the @mount can be unmounted.
280  **/
281 gboolean
282 g_mount_can_unmount (GMount *mount)
283 {
284   GMountIface *iface;
285
286   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
287
288   iface = G_MOUNT_GET_IFACE (mount);
289
290   return (* iface->can_unmount) (mount);
291 }
292
293 /**
294  * g_mount_can_eject: 
295  * @mount: a #GMount.
296  * 
297  * Checks if @mount can be eject.
298  * 
299  * Returns: %TRUE if the @mount can be ejected.
300  **/
301 gboolean
302 g_mount_can_eject (GMount *mount)
303 {
304   GMountIface *iface;
305
306   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
307
308   iface = G_MOUNT_GET_IFACE (mount);
309
310   return (* iface->can_eject) (mount);
311 }
312
313 /**
314  * g_mount_unmount:
315  * @mount: a #GMount.
316  * @flags: flags affecting the operation
317  * @cancellable: optional #GCancellable object, %NULL to ignore.
318  * @callback: a #GAsyncReadyCallback, or %NULL.
319  * @user_data: user data passed to @callback.
320  * 
321  * Unmounts a mount. This is an asynchronous operation, and is 
322  * finished by calling g_mount_unmount_finish() with the @mount 
323  * and #GAsyncResults data returned in the @callback.
324  **/
325 void
326 g_mount_unmount (GMount *mount,
327                  GMountUnmountFlags flags,
328                  GCancellable *cancellable,
329                  GAsyncReadyCallback callback,
330                  gpointer user_data)
331 {
332   GMountIface *iface;
333
334   g_return_if_fail (G_IS_MOUNT (mount));
335   
336   iface = G_MOUNT_GET_IFACE (mount);
337
338   if (iface->unmount == NULL)
339     {
340       g_simple_async_report_error_in_idle (G_OBJECT (mount),
341                                            callback, user_data,
342                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
343                                            /* Translators: This is an error
344                                             * message for mount objects that
345                                             * don't implement unmount. */
346                                            _("mount doesn't implement unmount"));
347
348       return;
349     }
350   
351   (* iface->unmount) (mount, flags, cancellable, callback, user_data);
352 }
353
354 /**
355  * g_mount_unmount_finish:
356  * @mount: a #GMount.
357  * @result: a #GAsyncResult.
358  * @error: a #GError location to store the error occuring, or %NULL to 
359  * ignore.
360  * 
361  * Finishes unmounting a mount. If any errors occurred during the operation, 
362  * @error will be set to contain the errors and %FALSE will be returned.
363  * 
364  * Returns: %TRUE if the mount was successfully unmounted. %FALSE otherwise.
365  **/
366 gboolean
367 g_mount_unmount_finish (GMount       *mount,
368                         GAsyncResult  *result,
369                         GError       **error)
370 {
371   GMountIface *iface;
372
373   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
374   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
375
376   if (G_IS_SIMPLE_ASYNC_RESULT (result))
377     {
378       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
379       if (g_simple_async_result_propagate_error (simple, error))
380         return FALSE;
381     }
382   
383   iface = G_MOUNT_GET_IFACE (mount);
384   return (* iface->unmount_finish) (mount, result, error);
385 }
386
387
388 /**
389  * g_mount_eject:
390  * @mount: a #GMount.
391  * @flags: flags affecting the unmount if required for eject
392  * @cancellable: optional #GCancellable object, %NULL to ignore.
393  * @callback: a #GAsyncReadyCallback, or %NULL.
394  * @user_data: user data passed to @callback.
395  * 
396  * Ejects a mount. This is an asynchronous operation, and is 
397  * finished by calling g_mount_eject_finish() with the @mount 
398  * and #GAsyncResults data returned in the @callback.
399  **/
400 void
401 g_mount_eject (GMount *mount,
402                GMountUnmountFlags flags,
403                GCancellable *cancellable,
404                GAsyncReadyCallback callback,
405                gpointer user_data)
406 {
407   GMountIface *iface;
408
409   g_return_if_fail (G_IS_MOUNT (mount));
410   
411   iface = G_MOUNT_GET_IFACE (mount);
412
413   if (iface->eject == NULL)
414     {
415       g_simple_async_report_error_in_idle (G_OBJECT (mount),
416                                            callback, user_data,
417                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
418                                            /* Translators: This is an error
419                                             * message for mount objects that
420                                             * don't implement eject. */
421                                            _("mount doesn't implement eject"));
422       
423       return;
424     }
425   
426   (* iface->eject) (mount, flags, cancellable, callback, user_data);
427 }
428
429 /**
430  * g_mount_eject_finish:
431  * @mount: a #GMount.
432  * @result: a #GAsyncResult.
433  * @error: a #GError location to store the error occuring, or %NULL to 
434  * ignore.
435  * 
436  * Finishes ejecting a mount. If any errors occurred during the operation, 
437  * @error will be set to contain the errors and %FALSE will be returned.
438  * 
439  * Returns: %TRUE if the mount was successfully ejected. %FALSE otherwise.
440  **/
441 gboolean
442 g_mount_eject_finish (GMount       *mount,
443                       GAsyncResult  *result,
444                       GError       **error)
445 {
446   GMountIface *iface;
447
448   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
449   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
450
451   if (G_IS_SIMPLE_ASYNC_RESULT (result))
452     {
453       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
454       if (g_simple_async_result_propagate_error (simple, error))
455         return FALSE;
456     }
457   
458   iface = G_MOUNT_GET_IFACE (mount);
459   return (* iface->eject_finish) (mount, result, error);
460 }
461
462 /**
463  * g_mount_remount:
464  * @mount: a #GMount.
465  * @flags: flags affecting the operation
466  * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
467  * @cancellable: optional #GCancellable object, %NULL to ignore.
468  * @callback: a #GAsyncReadyCallback, or %NULL.
469  * @user_data: user data passed to @callback.
470  * 
471  * Remounts a mount. This is an asynchronous operation, and is 
472  * finished by calling g_mount_remount_finish() with the @mount 
473  * and #GAsyncResults data returned in the @callback.
474  *
475  * Remounting is useful when some setting affecting the operation
476  * of the volume has been changed, as these may need a remount to
477  * take affect. While this is semantically equivalent with unmounting
478  * and then remounting not all backends might need to actually be
479  * unmounted.
480  **/
481 void
482 g_mount_remount (GMount *mount,
483                  GMountMountFlags flags,
484                  GMountOperation *mount_operation,
485                  GCancellable *cancellable,
486                  GAsyncReadyCallback callback,
487                  gpointer user_data)
488 {
489   GMountIface *iface;
490
491   g_return_if_fail (G_IS_MOUNT (mount));
492   
493   iface = G_MOUNT_GET_IFACE (mount);
494
495   if (iface->remount == NULL)
496     { 
497       g_simple_async_report_error_in_idle (G_OBJECT (mount),
498                                            callback, user_data,
499                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
500                                            /* Translators: This is an error
501                                             * message for mount objects that
502                                             * don't implement remount. */
503                                            _("mount doesn't implement remount"));
504       
505       return;
506     }
507   
508   (* iface->remount) (mount, flags, mount_operation, cancellable, callback, user_data);
509 }
510
511 /**
512  * g_mount_remount_finish:
513  * @mount: a #GMount.
514  * @result: a #GAsyncResult.
515  * @error: a #GError location to store the error occuring, or %NULL to 
516  * ignore.
517  * 
518  * Finishes remounting a mount. If any errors occurred during the operation, 
519  * @error will be set to contain the errors and %FALSE will be returned.
520  * 
521  * Returns: %TRUE if the mount was successfully remounted. %FALSE otherwise.
522  **/
523 gboolean
524 g_mount_remount_finish (GMount       *mount,
525                         GAsyncResult  *result,
526                         GError       **error)
527 {
528   GMountIface *iface;
529
530   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
531   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
532
533   if (G_IS_SIMPLE_ASYNC_RESULT (result))
534     {
535       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
536       if (g_simple_async_result_propagate_error (simple, error))
537         return FALSE;
538     }
539   
540   iface = G_MOUNT_GET_IFACE (mount);
541   return (* iface->remount_finish) (mount, result, error);
542 }
543
544 /**
545  * g_mount_guess_content_type:
546  * @mount: a #GMount
547  * @force_rescan: Whether to force a rescan of the content. 
548  *     Otherwise a cached result will be used if available
549  * @cancellable: optional #GCancellable object, %NULL to ignore
550  * @callback: a #GAsyncReadyCallback
551  * @user_data: user data passed to @callback
552  * 
553  * Tries to guess the type of content stored on @mount. Returns one or
554  * more textual identifiers of well-known content types (typically
555  * prefixed with "x-content/"), e.g. x-content/image-dcf for camera 
556  * memory cards. See the <ulink url="http://www.freedesktop.org/wiki/Specifications/shared-mime-info-spec">shared-mime-info</ulink>
557  * specification for more on x-content types.
558  *
559  * This is an asynchronous operation, and is finished by calling 
560  * g_mount_guess_content_type_finish() with the @mount and #GAsyncResult 
561  * data returned in the @callback. 
562  *
563  * Since: 2.18
564  */
565 void
566 g_mount_guess_content_type (GMount              *mount,
567                             gboolean             force_rescan,
568                             GCancellable        *cancellable,
569                             GAsyncReadyCallback  callback,
570                             gpointer             user_data)
571 {
572   GMountIface *iface;
573
574   g_return_if_fail (G_IS_MOUNT (mount));
575
576   iface = G_MOUNT_GET_IFACE (mount);
577
578   if (iface->guess_content_type == NULL)
579     {
580       g_simple_async_report_error_in_idle (G_OBJECT (mount),
581                                            callback, user_data,
582                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
583                                            /* Translators: This is an error
584                                             * message for mount objects that
585                                             * don't implement content type guessing. */
586                                            _("mount doesn't implement content type guessing"));
587
588       return;
589     }
590   
591   (* iface->guess_content_type) (mount, force_rescan, cancellable, callback, user_data);
592 }
593
594 /**
595  * g_mount_guess_content_type_finish:
596  * @mount: a #GMount
597  * @result: a #GAsyncResult
598  * @error: a #GError location to store the error occuring, or %NULL to 
599  *     ignore
600  * 
601  * Finishes guessing content types of @mount. If any errors occured
602  * during the operation, @error will be set to contain the errors and
603  * %FALSE will be returned. In particular, you may get an 
604  * %G_IO_ERROR_NOT_SUPPORTED if the mount does not support content 
605  * guessing.
606  * 
607  * Returns: a %NULL-terminated array of content types or %NULL on error. 
608  *     Caller should free this array with g_strfreev() when done with it.
609  *
610  * Since: 2.18
611  **/
612 gchar **
613 g_mount_guess_content_type_finish (GMount        *mount,
614                                    GAsyncResult  *result,
615                                    GError       **error)
616 {
617   GMountIface *iface;
618
619   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
620   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
621
622   if (G_IS_SIMPLE_ASYNC_RESULT (result))
623     {
624       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
625       if (g_simple_async_result_propagate_error (simple, error))
626         return FALSE;
627     }
628   
629   iface = G_MOUNT_GET_IFACE (mount);
630   return (* iface->guess_content_type_finish) (mount, result, error);
631 }
632
633
634 #define __G_MOUNT_C__
635 #include "gioaliasdef.c"