Provide eject() on both GMount and GVolume and utility functions to guess
[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  * 
41  * The #GMount interface represents user-visible mounts. Note, when
42  * porting from GnomeVFS, #GMount is the moral equivalent of
43  * #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
55  * g_mount_unmount_finish() is called, then it will be filled with any
56  * error information.
57  **/
58
59 static void g_mount_base_init (gpointer g_class);
60 static void g_mount_class_init (gpointer g_class,
61                                 gpointer class_data);
62
63 GType
64 g_mount_get_type (void)
65 {
66   static GType mount_type = 0;
67
68   if (! mount_type)
69     {
70       static const GTypeInfo mount_info =
71       {
72         sizeof (GMountIface), /* class_size */
73         g_mount_base_init,   /* base_init */
74         NULL,           /* base_finalize */
75         g_mount_class_init,
76         NULL,           /* class_finalize */
77         NULL,           /* class_data */
78         0,
79         0,              /* n_preallocs */
80         NULL
81       };
82
83       mount_type =
84         g_type_register_static (G_TYPE_INTERFACE, I_("GMount"),
85                                 &mount_info, 0);
86
87       g_type_interface_add_prerequisite (mount_type, G_TYPE_OBJECT);
88     }
89
90   return mount_type;
91 }
92
93 static void
94 g_mount_class_init (gpointer g_class,
95                     gpointer class_data)
96 {
97 }
98
99 static void
100 g_mount_base_init (gpointer g_class)
101 {
102   static gboolean initialized = FALSE;
103
104   if (! initialized)
105     {
106      /**
107       * GMount::changed:
108       * 
109       * Emitted when the mount has been changed.
110       **/
111       g_signal_new (I_("changed"),
112                     G_TYPE_MOUNT,
113                     G_SIGNAL_RUN_LAST,
114                     G_STRUCT_OFFSET (GMountIface, changed),
115                     NULL, NULL,
116                     g_cclosure_marshal_VOID__VOID,
117                     G_TYPE_NONE, 0);
118
119       initialized = TRUE;
120     }
121 }
122
123 /**
124  * g_mount_get_root:
125  * @mount: a #GMount.
126  * 
127  * Gets the root directory on @mount.
128  * 
129  * Returns: a #GFile.
130  **/
131 GFile *
132 g_mount_get_root (GMount *mount)
133 {
134   GMountIface *iface;
135
136   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
137
138   iface = G_MOUNT_GET_IFACE (mount);
139
140   return (* iface->get_root) (mount);
141 }
142
143 /**
144  * g_mount_get_name:
145  * @mount: a #GMount.
146  * 
147  * Gets the name of @mount.
148  * 
149  * Returns: the name for the given @mount. The returned string should 
150  * be freed when no longer needed.
151  **/
152 char *
153 g_mount_get_name (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_name) (mount);
162 }
163
164 /**
165  * g_mount_get_icon:
166  * @mount: a #GMount.
167  * 
168  * Gets the icon for @mount.
169  * 
170  * Returns: a #GIcon.
171  **/
172 GIcon *
173 g_mount_get_icon (GMount *mount)
174 {
175   GMountIface *iface;
176
177   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
178
179   iface = G_MOUNT_GET_IFACE (mount);
180
181   return (* iface->get_icon) (mount);
182 }
183
184 /**
185  * g_mount_get_uuid:
186  * @mount: a #GMount.
187  * 
188  * Gets the UUID for the @mount. The reference is typically based on
189  * the file system UUID for the mount in question and should be
190  * considered an opaque string. Returns %NULL if there is no UUID
191  * available.
192  * 
193  * Returns: the UUID for @mount or %NULL if no UUID can be computed.
194  **/
195 char *
196 g_mount_get_uuid (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_uuid) (mount);
205 }
206
207 /**
208  * g_mount_get_volume:
209  * @mount: a #GMount.
210  * 
211  * Gets the volume for the @mount.
212  * 
213  * Returns: a #GVolume or %NULL if @mount is not associated with a volume.
214  **/
215 GVolume *
216 g_mount_get_volume (GMount *mount)
217 {
218   GMountIface *iface;
219
220   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
221
222   iface = G_MOUNT_GET_IFACE (mount);
223
224   return (* iface->get_volume) (mount);
225 }
226
227 /**
228  * g_mount_get_drive:
229  * @mount: a #GMount.
230  * 
231  * Gets the drive for the @mount.
232  *
233  * This is a convenience method for getting the #GVolume and then
234  * using that object to get the #GDrive.
235  * 
236  * Returns: a #GDrive or %NULL if @mount is not associated with a volume or a drive.
237  **/
238 GDrive *
239 g_mount_get_drive (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_drive) (mount);
248 }
249
250 /**
251  * g_mount_can_unmount: 
252  * @mount: a #GMount.
253  * 
254  * Checks if @mount can be mounted.
255  * 
256  * Returns: %TRUE if the @mount can be unmounted.
257  **/
258 gboolean
259 g_mount_can_unmount (GMount *mount)
260 {
261   GMountIface *iface;
262
263   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
264
265   iface = G_MOUNT_GET_IFACE (mount);
266
267   return (* iface->can_unmount) (mount);
268 }
269
270 /**
271  * g_mount_can_eject: 
272  * @mount: a #GMount.
273  * 
274  * Checks if @mount can be eject.
275  * 
276  * Returns: %TRUE if the @mount can be ejected.
277  **/
278 gboolean
279 g_mount_can_eject (GMount *mount)
280 {
281   GMountIface *iface;
282
283   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
284
285   iface = G_MOUNT_GET_IFACE (mount);
286
287   return (* iface->can_eject) (mount);
288 }
289
290 /**
291  * g_mount_unmount:
292  * @mount: a #GMount.
293  * @cancellable: optional #GCancellable object, %NULL to ignore.
294  * @callback: a #GAsyncReadyCallback.
295  * @user_data: user data passed to @callback.
296  * 
297  * Unmounts a mount. This is an asynchronous operation, and is 
298  * finished by calling g_mount_unmount_finish() with the @mount 
299  * and #GAsyncResults data returned in the @callback.
300  **/
301 void
302 g_mount_unmount (GMount             *mount,
303                  GCancellable        *cancellable,
304                  GAsyncReadyCallback  callback,
305                  gpointer             user_data)
306 {
307   GMountIface *iface;
308
309   g_return_if_fail (G_IS_MOUNT (mount));
310   
311   iface = G_MOUNT_GET_IFACE (mount);
312
313   if (iface->unmount == NULL)
314     {
315       g_simple_async_report_error_in_idle (G_OBJECT (mount),
316                                            callback, user_data,
317                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
318                                            _("mount doesn't implement unmount"));
319       
320       return;
321     }
322   
323   (* iface->unmount) (mount, cancellable, callback, user_data);
324 }
325
326 /**
327  * g_mount_unmount_finish:
328  * @mount: a #GMount.
329  * @result: a #GAsyncResult.
330  * @error: a #GError location to store the error occuring, or %NULL to 
331  * ignore.
332  * 
333  * Finishes unmounting a mount. If any errors occured during the operation, 
334  * @error will be set to contain the errors and %FALSE will be returned.
335  * 
336  * Returns: %TRUE if the mount was successfully unmounted. %FALSE otherwise.
337  **/
338 gboolean
339 g_mount_unmount_finish (GMount       *mount,
340                         GAsyncResult  *result,
341                         GError       **error)
342 {
343   GMountIface *iface;
344
345   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
346   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
347
348   if (G_IS_SIMPLE_ASYNC_RESULT (result))
349     {
350       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
351       if (g_simple_async_result_propagate_error (simple, error))
352         return FALSE;
353     }
354   
355   iface = G_MOUNT_GET_IFACE (mount);
356   return (* iface->unmount_finish) (mount, result, error);
357 }
358
359
360 /**
361  * g_mount_eject:
362  * @mount: a #GMount.
363  * @cancellable: optional #GCancellable object, %NULL to ignore.
364  * @callback: a #GAsyncReadyCallback.
365  * @user_data: user data passed to @callback.
366  * 
367  * Ejects a mount. This is an asynchronous operation, and is 
368  * finished by calling g_mount_eject_finish() with the @mount 
369  * and #GAsyncResults data returned in the @callback.
370  **/
371 void
372 g_mount_eject (GMount             *mount,
373                GCancellable        *cancellable,
374                GAsyncReadyCallback  callback,
375                gpointer             user_data)
376 {
377   GMountIface *iface;
378
379   g_return_if_fail (G_IS_MOUNT (mount));
380   
381   iface = G_MOUNT_GET_IFACE (mount);
382
383   if (iface->eject == NULL)
384     {
385       g_simple_async_report_error_in_idle (G_OBJECT (mount),
386                                            callback, user_data,
387                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
388                                            _("mount doesn't implement eject"));
389       
390       return;
391     }
392   
393   (* iface->eject) (mount, cancellable, callback, user_data);
394 }
395
396 /**
397  * g_mount_eject_finish:
398  * @mount: a #GMount.
399  * @result: a #GAsyncResult.
400  * @error: a #GError location to store the error occuring, or %NULL to 
401  * ignore.
402  * 
403  * Finishes ejecting a mount. If any errors occured during the operation, 
404  * @error will be set to contain the errors and %FALSE will be returned.
405  * 
406  * Returns: %TRUE if the mount was successfully ejected. %FALSE otherwise.
407  **/
408 gboolean
409 g_mount_eject_finish (GMount       *mount,
410                       GAsyncResult  *result,
411                       GError       **error)
412 {
413   GMountIface *iface;
414
415   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
416   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
417
418   if (G_IS_SIMPLE_ASYNC_RESULT (result))
419     {
420       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
421       if (g_simple_async_result_propagate_error (simple, error))
422         return FALSE;
423     }
424   
425   iface = G_MOUNT_GET_IFACE (mount);
426   return (* iface->eject_finish) (mount, result, error);
427 }
428
429 #define __G_MOUNT_C__
430 #include "gioaliasdef.c"