Add GMountUnmountFlags to all unmount and eject calls. Add
[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      /**
119       * GMount::unmounted:
120       * 
121       * This signal is emitted when the #GMount have been
122       * unmounted. If the recipient is holding references to the
123       * object they should release them so the object can be
124       * finalized.
125       **/
126       g_signal_new (I_("unmounted"),
127                     G_TYPE_MOUNT,
128                     G_SIGNAL_RUN_LAST,
129                     G_STRUCT_OFFSET (GMountIface, unmounted),
130                     NULL, NULL,
131                     g_cclosure_marshal_VOID__VOID,
132                     G_TYPE_NONE, 0);
133
134       initialized = TRUE;
135     }
136 }
137
138 /**
139  * g_mount_get_root:
140  * @mount: a #GMount.
141  * 
142  * Gets the root directory on @mount.
143  * 
144  * Returns: a #GFile.
145  **/
146 GFile *
147 g_mount_get_root (GMount *mount)
148 {
149   GMountIface *iface;
150
151   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
152
153   iface = G_MOUNT_GET_IFACE (mount);
154
155   return (* iface->get_root) (mount);
156 }
157
158 /**
159  * g_mount_get_name:
160  * @mount: a #GMount.
161  * 
162  * Gets the name of @mount.
163  * 
164  * Returns: the name for the given @mount. The returned string should 
165  * be freed when no longer needed.
166  **/
167 char *
168 g_mount_get_name (GMount *mount)
169 {
170   GMountIface *iface;
171
172   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
173
174   iface = G_MOUNT_GET_IFACE (mount);
175
176   return (* iface->get_name) (mount);
177 }
178
179 /**
180  * g_mount_get_icon:
181  * @mount: a #GMount.
182  * 
183  * Gets the icon for @mount.
184  * 
185  * Returns: a #GIcon.
186  **/
187 GIcon *
188 g_mount_get_icon (GMount *mount)
189 {
190   GMountIface *iface;
191
192   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
193
194   iface = G_MOUNT_GET_IFACE (mount);
195
196   return (* iface->get_icon) (mount);
197 }
198
199 /**
200  * g_mount_get_uuid:
201  * @mount: a #GMount.
202  * 
203  * Gets the UUID for the @mount. The reference is typically based on
204  * the file system UUID for the mount in question and should be
205  * considered an opaque string. Returns %NULL if there is no UUID
206  * available.
207  * 
208  * Returns: the UUID for @mount or %NULL if no UUID can be computed.
209  **/
210 char *
211 g_mount_get_uuid (GMount *mount)
212 {
213   GMountIface *iface;
214
215   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
216
217   iface = G_MOUNT_GET_IFACE (mount);
218
219   return (* iface->get_uuid) (mount);
220 }
221
222 /**
223  * g_mount_get_volume:
224  * @mount: a #GMount.
225  * 
226  * Gets the volume for the @mount.
227  * 
228  * Returns: a #GVolume or %NULL if @mount is not associated with a volume.
229  **/
230 GVolume *
231 g_mount_get_volume (GMount *mount)
232 {
233   GMountIface *iface;
234
235   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
236
237   iface = G_MOUNT_GET_IFACE (mount);
238
239   return (* iface->get_volume) (mount);
240 }
241
242 /**
243  * g_mount_get_drive:
244  * @mount: a #GMount.
245  * 
246  * Gets the drive for the @mount.
247  *
248  * This is a convenience method for getting the #GVolume and then
249  * using that object to get the #GDrive.
250  * 
251  * Returns: a #GDrive or %NULL if @mount is not associated with a volume or a drive.
252  **/
253 GDrive *
254 g_mount_get_drive (GMount *mount)
255 {
256   GMountIface *iface;
257
258   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
259
260   iface = G_MOUNT_GET_IFACE (mount);
261
262   return (* iface->get_drive) (mount);
263 }
264
265 /**
266  * g_mount_can_unmount: 
267  * @mount: a #GMount.
268  * 
269  * Checks if @mount can be mounted.
270  * 
271  * Returns: %TRUE if the @mount can be unmounted.
272  **/
273 gboolean
274 g_mount_can_unmount (GMount *mount)
275 {
276   GMountIface *iface;
277
278   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
279
280   iface = G_MOUNT_GET_IFACE (mount);
281
282   return (* iface->can_unmount) (mount);
283 }
284
285 /**
286  * g_mount_can_eject: 
287  * @mount: a #GMount.
288  * 
289  * Checks if @mount can be eject.
290  * 
291  * Returns: %TRUE if the @mount can be ejected.
292  **/
293 gboolean
294 g_mount_can_eject (GMount *mount)
295 {
296   GMountIface *iface;
297
298   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
299
300   iface = G_MOUNT_GET_IFACE (mount);
301
302   return (* iface->can_eject) (mount);
303 }
304
305 /**
306  * g_mount_unmount:
307  * @mount: a #GMount.
308  * @flags: flags affecting the operation
309  * @cancellable: optional #GCancellable object, %NULL to ignore.
310  * @callback: a #GAsyncReadyCallback.
311  * @user_data: user data passed to @callback.
312  * 
313  * Unmounts a mount. This is an asynchronous operation, and is 
314  * finished by calling g_mount_unmount_finish() with the @mount 
315  * and #GAsyncResults data returned in the @callback.
316  **/
317 void
318 g_mount_unmount (GMount *mount,
319                  GMountUnmountFlags flags,
320                  GCancellable *cancellable,
321                  GAsyncReadyCallback callback,
322                  gpointer user_data)
323 {
324   GMountIface *iface;
325
326   g_return_if_fail (G_IS_MOUNT (mount));
327   
328   iface = G_MOUNT_GET_IFACE (mount);
329
330   if (iface->unmount == NULL)
331     {
332       g_simple_async_report_error_in_idle (G_OBJECT (mount),
333                                            callback, user_data,
334                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
335                                            _("mount doesn't implement unmount"));
336       
337       return;
338     }
339   
340   (* iface->unmount) (mount, flags, cancellable, callback, user_data);
341 }
342
343 /**
344  * g_mount_unmount_finish:
345  * @mount: a #GMount.
346  * @result: a #GAsyncResult.
347  * @error: a #GError location to store the error occuring, or %NULL to 
348  * ignore.
349  * 
350  * Finishes unmounting a mount. If any errors occured during the operation, 
351  * @error will be set to contain the errors and %FALSE will be returned.
352  * 
353  * Returns: %TRUE if the mount was successfully unmounted. %FALSE otherwise.
354  **/
355 gboolean
356 g_mount_unmount_finish (GMount       *mount,
357                         GAsyncResult  *result,
358                         GError       **error)
359 {
360   GMountIface *iface;
361
362   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
363   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
364
365   if (G_IS_SIMPLE_ASYNC_RESULT (result))
366     {
367       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
368       if (g_simple_async_result_propagate_error (simple, error))
369         return FALSE;
370     }
371   
372   iface = G_MOUNT_GET_IFACE (mount);
373   return (* iface->unmount_finish) (mount, result, error);
374 }
375
376
377 /**
378  * g_mount_eject:
379  * @mount: a #GMount.
380  * @flags: flags affecting the unmount if required for eject
381  * @cancellable: optional #GCancellable object, %NULL to ignore.
382  * @callback: a #GAsyncReadyCallback.
383  * @user_data: user data passed to @callback.
384  * 
385  * Ejects a mount. This is an asynchronous operation, and is 
386  * finished by calling g_mount_eject_finish() with the @mount 
387  * and #GAsyncResults data returned in the @callback.
388  **/
389 void
390 g_mount_eject (GMount *mount,
391                GMountUnmountFlags flags,
392                GCancellable *cancellable,
393                GAsyncReadyCallback callback,
394                gpointer user_data)
395 {
396   GMountIface *iface;
397
398   g_return_if_fail (G_IS_MOUNT (mount));
399   
400   iface = G_MOUNT_GET_IFACE (mount);
401
402   if (iface->eject == NULL)
403     {
404       g_simple_async_report_error_in_idle (G_OBJECT (mount),
405                                            callback, user_data,
406                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
407                                            _("mount doesn't implement eject"));
408       
409       return;
410     }
411   
412   (* iface->eject) (mount, flags, cancellable, callback, user_data);
413 }
414
415 /**
416  * g_mount_eject_finish:
417  * @mount: a #GMount.
418  * @result: a #GAsyncResult.
419  * @error: a #GError location to store the error occuring, or %NULL to 
420  * ignore.
421  * 
422  * Finishes ejecting a mount. If any errors occured during the operation, 
423  * @error will be set to contain the errors and %FALSE will be returned.
424  * 
425  * Returns: %TRUE if the mount was successfully ejected. %FALSE otherwise.
426  **/
427 gboolean
428 g_mount_eject_finish (GMount       *mount,
429                       GAsyncResult  *result,
430                       GError       **error)
431 {
432   GMountIface *iface;
433
434   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
435   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
436
437   if (G_IS_SIMPLE_ASYNC_RESULT (result))
438     {
439       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
440       if (g_simple_async_result_propagate_error (simple, error))
441         return FALSE;
442     }
443   
444   iface = G_MOUNT_GET_IFACE (mount);
445   return (* iface->eject_finish) (mount, result, error);
446 }
447
448 /**
449  * g_mount_remount:
450  * @mount: a #GMount.
451  * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
452  * @cancellable: optional #GCancellable object, %NULL to ignore.
453  * @callback: a #GAsyncReadyCallback.
454  * @user_data: user data passed to @callback.
455  * 
456  * Remounts a mount. This is an asynchronous operation, and is 
457  * finished by calling g_mount_unmount_finish() with the @mount 
458  * and #GAsyncResults data returned in the @callback.
459  *
460  * Remounting is useful when some setting affecting the operation
461  * of the volume has been changed, as these may need a remount to
462  * take affect. While this is semantically equivalent with unmounting
463  * and then remounting not all backends might need to actually be
464  * unmounted.
465  **/
466 void
467 g_mount_remount (GMount *mount,
468                  GMountOperation *mount_operation,
469                  GCancellable *cancellable,
470                  GAsyncReadyCallback callback,
471                  gpointer user_data)
472 {
473   GMountIface *iface;
474
475   g_return_if_fail (G_IS_MOUNT (mount));
476   
477   iface = G_MOUNT_GET_IFACE (mount);
478
479   if (iface->remount == NULL)
480     {
481       g_simple_async_report_error_in_idle (G_OBJECT (mount),
482                                            callback, user_data,
483                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
484                                            _("mount doesn't implement remount"));
485       
486       return;
487     }
488   
489   (* iface->remount) (mount, mount_operation, cancellable, callback, user_data);
490 }
491
492 /**
493  * g_mount_remount_finish:
494  * @mount: a #GMount.
495  * @result: a #GAsyncResult.
496  * @error: a #GError location to store the error occuring, or %NULL to 
497  * ignore.
498  * 
499  * Finishes remounting a mount. If any errors occured during the operation, 
500  * @error will be set to contain the errors and %FALSE will be returned.
501  * 
502  * Returns: %TRUE if the mount was successfully remounted. %FALSE otherwise.
503  **/
504 gboolean
505 g_mount_remount_finish (GMount       *mount,
506                         GAsyncResult  *result,
507                         GError       **error)
508 {
509   GMountIface *iface;
510
511   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
512   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
513
514   if (G_IS_SIMPLE_ASYNC_RESULT (result))
515     {
516       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
517       if (g_simple_async_result_propagate_error (simple, error))
518         return FALSE;
519     }
520   
521   iface = G_MOUNT_GET_IFACE (mount);
522   return (* iface->remount_finish) (mount, result, error);
523 }
524
525
526 #define __G_MOUNT_C__
527 #include "gioaliasdef.c"