Rework how volumes, drives and volume monitoring is done. Previosly the
[platform/upstream/glib.git] / gio / gvolume.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  *         David Zeuthen <davidz@redhat.com>
22  */
23
24 #include <config.h>
25 #include "gmount.h"
26 #include "gvolume.h"
27 #include "gsimpleasyncresult.h"
28 #include "glibintl.h"
29
30 #include "gioalias.h"
31
32 /**
33  * SECTION:volume
34  * @short_description: volume management
35  * 
36  * The #GVolume interface represents user-visible objects that can be
37  * mounted. Note, when porting from GnomeVFS, #GVolume is the moral
38  * equivalent of #GnomeVFSDrive.
39  *
40  * Mounting a #GVolume instance is an asynchronous operation. For more
41  * information about asynchronous operations, see #GAsyncReady and
42  * #GSimpleAsyncReady. To mount a #GVolume, first call
43  * g_volume_mount() with (at least) the #GVolume instane, a
44  * #GMountOperation object and a #GAsyncReadyCallback.  The callback
45  * will be fired when the operation has resolved (either with success
46  * or failure), and a #GAsyncReady structure will be passed to the
47  * callback.  That callback should then call g_volume_mount_finish()
48  * with the #GVolume instance and the #GAsyncReady data to see if the
49  * operation was completed successfully.  If an @error is present when
50  * g_volume_mount_finish() is called, then it will be filled with any
51  * error information.
52  **/
53
54 static void g_volume_base_init (gpointer g_class);
55 static void g_volume_class_init (gpointer g_class,
56                                  gpointer class_data);
57
58 GType
59 g_volume_get_type (void)
60 {
61   static GType volume_type = 0;
62
63   if (! volume_type)
64     {
65       static const GTypeInfo volume_info =
66       {
67         sizeof (GVolumeIface), /* class_size */
68         g_volume_base_init,   /* base_init */
69         NULL,           /* base_finalize */
70         g_volume_class_init,
71         NULL,           /* class_finalize */
72         NULL,           /* class_data */
73         0,
74         0,              /* n_preallocs */
75         NULL
76       };
77
78       volume_type =
79         g_type_register_static (G_TYPE_INTERFACE, I_("GVolume"),
80                                 &volume_info, 0);
81
82       g_type_interface_add_prerequisite (volume_type, G_TYPE_OBJECT);
83     }
84
85   return volume_type;
86 }
87
88 static void
89 g_volume_class_init (gpointer g_class,
90                      gpointer class_data)
91 {
92 }
93
94 static void
95 g_volume_base_init (gpointer g_class)
96 {
97   static gboolean initialized = FALSE;
98
99   if (! initialized)
100     {
101      /**
102       * GVolume::changed:
103       * 
104       * Emitted when the volume has been changed.
105       **/
106       g_signal_new (I_("changed"),
107                     G_TYPE_VOLUME,
108                     G_SIGNAL_RUN_LAST,
109                     G_STRUCT_OFFSET (GVolumeIface, changed),
110                     NULL, NULL,
111                     g_cclosure_marshal_VOID__VOID,
112                     G_TYPE_NONE, 0);
113
114       initialized = TRUE;
115     }
116 }
117
118 /**
119  * g_volume_get_name:
120  * @volume: a #GVolume.
121  * 
122  * Gets the name of @volume.
123  * 
124  * Returns: the name for the given @volume. The returned string should 
125  * be freed when no longer needed.
126  **/
127 char *
128 g_volume_get_name (GVolume *volume)
129 {
130   GVolumeIface *iface;
131
132   g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
133
134   iface = G_VOLUME_GET_IFACE (volume);
135
136   return (* iface->get_name) (volume);
137 }
138
139 /**
140  * g_volume_get_icon:
141  * @volume: a #GVolume.
142  * 
143  * Gets the icon for @volume.
144  * 
145  * Returns: a #GIcon.
146  **/
147 GIcon *
148 g_volume_get_icon (GVolume *volume)
149 {
150   GVolumeIface *iface;
151
152   g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
153
154   iface = G_VOLUME_GET_IFACE (volume);
155
156   return (* iface->get_icon) (volume);
157 }
158   
159 /**
160  * g_volume_get_drive:
161  * @volume: a #GVolume.
162  * 
163  * Gets the drive for the @volume.
164  * 
165  * Returns: a #GDrive or %NULL if @volume is not associated with a drive.
166  **/
167 GDrive *
168 g_volume_get_drive (GVolume *volume)
169 {
170   GVolumeIface *iface;
171
172   g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
173
174   iface = G_VOLUME_GET_IFACE (volume);
175
176   return (* iface->get_drive) (volume);
177 }
178
179 /**
180  * g_volume_get_mount:
181  * @volume: a #GVolume.
182  * 
183  * Gets the mount for the @volume.
184  * 
185  * Returns: a #GMount or %NULL if @volume isn't mounted.
186  **/
187 GMount *
188 g_volume_get_mount (GVolume *volume)
189 {
190   GVolumeIface *iface;
191
192   g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
193
194   iface = G_VOLUME_GET_IFACE (volume);
195
196   return (* iface->get_mount) (volume);
197 }
198
199
200 /**
201  * g_volume_can_mount:
202  * @volume: a #GVolume.
203  * 
204  * Checks if a volume can be mounted.
205  * 
206  * Returns: %TRUE if the @volume can be mounted. %FALSE otherwise.
207  **/
208 gboolean
209 g_volume_can_mount (GVolume *volume)
210 {
211   GVolumeIface *iface;
212
213   g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
214
215   iface = G_VOLUME_GET_IFACE (volume);
216
217   if (iface->can_mount == NULL)
218     return FALSE;
219
220   return (* iface->can_mount) (volume);
221 }
222
223 /**
224  * g_volume_mount:
225  * @volume: a #GVolume.
226  * @mount_operation: a #GMountOperation.
227  * @cancellable: optional #GCancellable object, %NULL to ignore.
228  * @callback: a #GAsyncReadyCallback.
229  * @user_data: a #gpointer.
230  * 
231  * Mounts a volume.
232  **/
233 void
234 g_volume_mount (GVolume    *volume,
235                 GMountOperation     *mount_operation,
236                 GCancellable        *cancellable,
237                 GAsyncReadyCallback  callback,
238                 gpointer             user_data)
239 {
240   GVolumeIface *iface;
241
242   g_return_if_fail (G_IS_VOLUME (volume));
243   g_return_if_fail (G_IS_MOUNT_OPERATION (mount_operation));
244
245   iface = G_VOLUME_GET_IFACE (volume);
246
247   if (iface->mount_fn == NULL)
248     {
249       g_simple_async_report_error_in_idle (G_OBJECT (volume), callback, user_data,
250                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
251                                            _("volume doesn't implement mount"));
252       
253       return;
254     }
255   
256   (* iface->mount_fn) (volume, mount_operation, cancellable, callback, user_data);
257 }
258
259 /**
260  * g_volume_mount_finish:
261  * @volume: pointer to a #GVolume.
262  * @result: a #GAsyncResult.
263  * @error: a #GError.
264  * 
265  * Finishes mounting a volume.
266  * 
267  * Returns: %TRUE, %FALSE if operation failed.
268  **/
269 gboolean
270 g_volume_mount_finish (GVolume  *volume,
271                        GAsyncResult      *result,
272                        GError           **error)
273 {
274   GVolumeIface *iface;
275
276   g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
277   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
278
279   if (G_IS_SIMPLE_ASYNC_RESULT (result))
280     {
281       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
282       if (g_simple_async_result_propagate_error (simple, error))
283         return FALSE;
284     }
285   
286   iface = G_VOLUME_GET_IFACE (volume);
287   return (* iface->mount_finish) (volume, result, error);
288 }
289
290 #define __G_VOLUME_C__
291 #include "gioaliasdef.c"