Make GSettingsSchemaKey public
[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 "gthemedicon.h"
28 #include "gasyncresult.h"
29 #include "gtask.h"
30 #include "gioerror.h"
31 #include "glibintl.h"
32
33
34 /**
35  * SECTION:gvolume
36  * @short_description: Volume management
37  * @include: gio/gio.h
38  * 
39  * The #GVolume interface represents user-visible objects that can be
40  * mounted. Note, when porting from GnomeVFS, #GVolume is the moral
41  * equivalent of #GnomeVFSDrive.
42  *
43  * Mounting a #GVolume instance is an asynchronous operation. For more
44  * information about asynchronous operations, see #GAsyncResult and
45  * #GTask. To mount a #GVolume, first call g_volume_mount() with (at
46  * least) the #GVolume instance, optionally a #GMountOperation object
47  * and a #GAsyncReadyCallback.
48  *
49  * Typically, one will only want to pass %NULL for the
50  * #GMountOperation if automounting all volumes when a desktop session
51  * starts since it's not desirable to put up a lot of dialogs asking
52  * for credentials.
53  *
54  * The callback will be fired when the operation has resolved (either
55  * with success or failure), and a #GAsyncReady structure will be
56  * passed to the callback.  That callback should then call
57  * g_volume_mount_finish() with the #GVolume instance and the
58  * #GAsyncReady data to see if the operation was completed
59  * successfully.  If an @error is present when g_volume_mount_finish()
60  * is called, then it will be filled with any error information.
61  *
62  * <para id="volume-identifier">
63  * It is sometimes necessary to directly access the underlying
64  * operating system object behind a volume (e.g. for passing a volume
65  * to an application via the commandline). For this purpose, GIO
66  * allows to obtain an 'identifier' for the volume. There can be
67  * different kinds of identifiers, such as Hal UDIs, filesystem labels,
68  * traditional Unix devices (e.g. <filename>/dev/sda2</filename>),
69  * uuids. GIO uses predefind strings as names for the different kinds
70  * of identifiers: #G_VOLUME_IDENTIFIER_KIND_HAL_UDI,
71  * #G_VOLUME_IDENTIFIER_KIND_LABEL, etc. Use g_volume_get_identifier()
72  * to obtain an identifier for a volume.
73  * </para>
74  *
75  * Note that #G_VOLUME_IDENTIFIER_KIND_HAL_UDI will only be available
76  * when the gvfs hal volume monitor is in use. Other volume monitors
77  * will generally be able to provide the #G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE
78  * identifier, which can be used to obtain a hal device by means of
79  * libhal_manager_find_device_string_match().
80  */
81
82 typedef GVolumeIface GVolumeInterface;
83 G_DEFINE_INTERFACE(GVolume, g_volume, G_TYPE_OBJECT)
84
85 static void
86 g_volume_default_init (GVolumeInterface *iface)
87 {
88   /**
89    * GVolume::changed:
90    * 
91    * Emitted when the volume has been changed.
92    **/
93   g_signal_new (I_("changed"),
94                 G_TYPE_VOLUME,
95                 G_SIGNAL_RUN_LAST,
96                 G_STRUCT_OFFSET (GVolumeIface, changed),
97                 NULL, NULL,
98                 g_cclosure_marshal_VOID__VOID,
99                 G_TYPE_NONE, 0);
100
101   /**
102    * GVolume::removed:
103    * 
104    * This signal is emitted when the #GVolume have been removed. If
105    * the recipient is holding references to the object they should
106    * release them so the object can be finalized.
107    **/
108   g_signal_new (I_("removed"),
109                 G_TYPE_VOLUME,
110                 G_SIGNAL_RUN_LAST,
111                 G_STRUCT_OFFSET (GVolumeIface, removed),
112                 NULL, NULL,
113                 g_cclosure_marshal_VOID__VOID,
114                 G_TYPE_NONE, 0);
115 }
116
117 /**
118  * g_volume_get_name:
119  * @volume: a #GVolume.
120  * 
121  * Gets the name of @volume.
122  * 
123  * Returns: the name for the given @volume. The returned string should 
124  * be freed with g_free() when no longer needed.
125  **/
126 char *
127 g_volume_get_name (GVolume *volume)
128 {
129   GVolumeIface *iface;
130
131   g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
132
133   iface = G_VOLUME_GET_IFACE (volume);
134
135   return (* iface->get_name) (volume);
136 }
137
138 /**
139  * g_volume_get_icon:
140  * @volume: a #GVolume.
141  * 
142  * Gets the icon for @volume.
143  * 
144  * Returns: (transfer full): a #GIcon.
145  *     The returned object should be unreffed with g_object_unref()
146  *     when no longer needed.
147  **/
148 GIcon *
149 g_volume_get_icon (GVolume *volume)
150 {
151   GVolumeIface *iface;
152
153   g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
154
155   iface = G_VOLUME_GET_IFACE (volume);
156
157   return (* iface->get_icon) (volume);
158 }
159
160 /**
161  * g_volume_get_symbolic_icon:
162  * @volume: a #GVolume.
163  * 
164  * Gets the symbolic icon for @volume.
165  * 
166  * Returns: (transfer full): a #GIcon.
167  *     The returned object should be unreffed with g_object_unref()
168  *     when no longer needed.
169  *
170  * Since: 2.34
171  **/
172 GIcon *
173 g_volume_get_symbolic_icon (GVolume *volume)
174 {
175   GVolumeIface *iface;
176   GIcon *ret;
177
178   g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
179
180   iface = G_VOLUME_GET_IFACE (volume);
181
182   if (iface->get_symbolic_icon != NULL)
183     ret = iface->get_symbolic_icon (volume);
184   else
185     ret = g_themed_icon_new_with_default_fallbacks ("folder-remote-symbolic");
186
187   return ret;
188
189 }
190
191 /**
192  * g_volume_get_uuid:
193  * @volume: a #GVolume.
194  * 
195  * Gets the UUID for the @volume. The reference is typically based on
196  * the file system UUID for the volume in question and should be
197  * considered an opaque string. Returns %NULL if there is no UUID
198  * available.
199  * 
200  * Returns: the UUID for @volume or %NULL if no UUID can be computed.
201  *     The returned string should be freed with g_free() 
202  *     when no longer needed.
203  **/
204 char *
205 g_volume_get_uuid (GVolume *volume)
206 {
207   GVolumeIface *iface;
208
209   g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
210
211   iface = G_VOLUME_GET_IFACE (volume);
212
213   return (* iface->get_uuid) (volume);
214 }
215   
216 /**
217  * g_volume_get_drive:
218  * @volume: a #GVolume.
219  * 
220  * Gets the drive for the @volume.
221  * 
222  * Returns: (transfer full): a #GDrive or %NULL if @volume is not associated with a drive.
223  *     The returned object should be unreffed with g_object_unref()
224  *     when no longer needed.
225  **/
226 GDrive *
227 g_volume_get_drive (GVolume *volume)
228 {
229   GVolumeIface *iface;
230
231   g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
232
233   iface = G_VOLUME_GET_IFACE (volume);
234
235   return (* iface->get_drive) (volume);
236 }
237
238 /**
239  * g_volume_get_mount:
240  * @volume: a #GVolume.
241  * 
242  * Gets the mount for the @volume.
243  * 
244  * Returns: (transfer full): a #GMount or %NULL if @volume isn't mounted.
245  *     The returned object should be unreffed with g_object_unref()
246  *     when no longer needed.
247  **/
248 GMount *
249 g_volume_get_mount (GVolume *volume)
250 {
251   GVolumeIface *iface;
252
253   g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
254
255   iface = G_VOLUME_GET_IFACE (volume);
256
257   return (* iface->get_mount) (volume);
258 }
259
260
261 /**
262  * g_volume_can_mount:
263  * @volume: a #GVolume.
264  * 
265  * Checks if a volume can be mounted.
266  * 
267  * Returns: %TRUE if the @volume can be mounted. %FALSE otherwise.
268  **/
269 gboolean
270 g_volume_can_mount (GVolume *volume)
271 {
272   GVolumeIface *iface;
273
274   g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
275
276   iface = G_VOLUME_GET_IFACE (volume);
277
278   if (iface->can_mount == NULL)
279     return FALSE;
280
281   return (* iface->can_mount) (volume);
282 }
283
284 /**
285  * g_volume_can_eject:
286  * @volume: a #GVolume.
287  * 
288  * Checks if a volume can be ejected.
289  * 
290  * Returns: %TRUE if the @volume can be ejected. %FALSE otherwise.
291  **/
292 gboolean
293 g_volume_can_eject (GVolume *volume)
294 {
295   GVolumeIface *iface;
296
297   g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
298
299   iface = G_VOLUME_GET_IFACE (volume);
300
301   if (iface->can_eject == NULL)
302     return FALSE;
303
304   return (* iface->can_eject) (volume);
305 }
306
307 /**
308  * g_volume_should_automount:
309  * @volume: a #GVolume
310  *
311  * Returns whether the volume should be automatically mounted.
312  * 
313  * Returns: %TRUE if the volume should be automatically mounted.
314  */
315 gboolean
316 g_volume_should_automount (GVolume *volume)
317 {
318   GVolumeIface *iface;
319
320   g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
321
322   iface = G_VOLUME_GET_IFACE (volume);
323
324   if (iface->should_automount == NULL)
325     return FALSE;
326
327   return (* iface->should_automount) (volume);
328 }
329
330
331 /**
332  * g_volume_mount:
333  * @volume: a #GVolume.
334  * @flags: flags affecting the operation
335  * @mount_operation: (allow-none): a #GMountOperation or %NULL to avoid user interaction.
336  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
337  * @callback: (allow-none): a #GAsyncReadyCallback, or %NULL.
338  * @user_data: user data that gets passed to @callback
339  * 
340  * Mounts a volume. This is an asynchronous operation, and is
341  * finished by calling g_volume_mount_finish() with the @volume
342  * and #GAsyncResult returned in the @callback.
343  *
344  * Virtual: mount_fn
345  **/
346 void
347 g_volume_mount (GVolume             *volume,
348                 GMountMountFlags     flags,
349                 GMountOperation     *mount_operation,
350                 GCancellable        *cancellable,
351                 GAsyncReadyCallback  callback,
352                 gpointer             user_data)
353 {
354   GVolumeIface *iface;
355
356   g_return_if_fail (G_IS_VOLUME (volume));
357
358   iface = G_VOLUME_GET_IFACE (volume);
359
360   if (iface->mount_fn == NULL)
361     {
362       g_task_report_new_error (volume, callback, user_data,
363                                g_volume_mount,
364                                G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
365                                _("volume doesn't implement mount"));
366       return;
367     }
368   
369   (* iface->mount_fn) (volume, flags, mount_operation, cancellable, callback, user_data);
370 }
371
372 /**
373  * g_volume_mount_finish:
374  * @volume: a #GVolume
375  * @result: a #GAsyncResult
376  * @error: a #GError location to store an error, or %NULL to ignore
377  * 
378  * Finishes mounting a volume. If any errors occurred during the operation,
379  * @error will be set to contain the errors and %FALSE will be returned.
380  *
381  * If the mount operation succeeded, g_volume_get_mount() on @volume
382  * is guaranteed to return the mount right after calling this
383  * function; there's no need to listen for the 'mount-added' signal on
384  * #GVolumeMonitor.
385  * 
386  * Returns: %TRUE, %FALSE if operation failed.
387  **/
388 gboolean
389 g_volume_mount_finish (GVolume       *volume,
390                        GAsyncResult  *result,
391                        GError       **error)
392 {
393   GVolumeIface *iface;
394
395   g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
396   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
397
398   if (g_async_result_legacy_propagate_error (result, error))
399     return FALSE;
400   else if (g_async_result_is_tagged (result, g_volume_mount))
401     return g_task_propagate_boolean (G_TASK (result), error);
402   
403   iface = G_VOLUME_GET_IFACE (volume);
404   return (* iface->mount_finish) (volume, result, error);
405 }
406
407 /**
408  * g_volume_eject:
409  * @volume: a #GVolume.
410  * @flags: flags affecting the unmount if required for eject
411  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
412  * @callback: (allow-none): a #GAsyncReadyCallback, or %NULL.
413  * @user_data: user data that gets passed to @callback
414  * 
415  * Ejects a volume. This is an asynchronous operation, and is
416  * finished by calling g_volume_eject_finish() with the @volume
417  * and #GAsyncResult returned in the @callback.
418  *
419  * Deprecated: 2.22: Use g_volume_eject_with_operation() instead.
420  **/
421 void
422 g_volume_eject (GVolume             *volume,
423                 GMountUnmountFlags   flags,
424                 GCancellable        *cancellable,
425                 GAsyncReadyCallback  callback,
426                 gpointer             user_data)
427 {
428   GVolumeIface *iface;
429
430   g_return_if_fail (G_IS_VOLUME (volume));
431
432   iface = G_VOLUME_GET_IFACE (volume);
433
434   if (iface->eject == NULL)
435     {
436       g_task_report_new_error (volume, callback, user_data,
437                                g_volume_eject_with_operation,
438                                G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
439                                _("volume doesn't implement eject"));
440       return;
441     }
442   
443   (* iface->eject) (volume, flags, cancellable, callback, user_data);
444 }
445
446 /**
447  * g_volume_eject_finish:
448  * @volume: pointer to a #GVolume.
449  * @result: a #GAsyncResult.
450  * @error: a #GError location to store an error, or %NULL to ignore
451  * 
452  * Finishes ejecting a volume. If any errors occurred during the operation,
453  * @error will be set to contain the errors and %FALSE will be returned.
454  * 
455  * Returns: %TRUE, %FALSE if operation failed.
456  *
457  * Deprecated: 2.22: Use g_volume_eject_with_operation_finish() instead.
458  **/
459 gboolean
460 g_volume_eject_finish (GVolume       *volume,
461                        GAsyncResult  *result,
462                        GError       **error)
463 {
464   GVolumeIface *iface;
465
466   g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
467   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
468
469   if (g_async_result_legacy_propagate_error (result, error))
470     return FALSE;
471   if (g_async_result_is_tagged (result, g_volume_eject_with_operation))
472     return g_task_propagate_boolean (G_TASK (result), error);
473   
474   iface = G_VOLUME_GET_IFACE (volume);
475   return (* iface->eject_finish) (volume, result, error);
476 }
477
478 /**
479  * g_volume_eject_with_operation:
480  * @volume: a #GVolume.
481  * @flags: flags affecting the unmount if required for eject
482  * @mount_operation: (allow-none): a #GMountOperation or %NULL to
483  *     avoid user interaction.
484  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
485  * @callback: (allow-none): a #GAsyncReadyCallback, or %NULL.
486  * @user_data: user data passed to @callback.
487  *
488  * Ejects a volume. This is an asynchronous operation, and is
489  * finished by calling g_volume_eject_with_operation_finish() with the @volume
490  * and #GAsyncResult data returned in the @callback.
491  *
492  * Since: 2.22
493  **/
494 void
495 g_volume_eject_with_operation (GVolume              *volume,
496                                GMountUnmountFlags   flags,
497                                GMountOperation     *mount_operation,
498                                GCancellable        *cancellable,
499                                GAsyncReadyCallback  callback,
500                                gpointer             user_data)
501 {
502   GVolumeIface *iface;
503
504   g_return_if_fail (G_IS_VOLUME (volume));
505
506   iface = G_VOLUME_GET_IFACE (volume);
507
508   if (iface->eject == NULL && iface->eject_with_operation == NULL)
509     {
510       g_task_report_new_error (volume, callback, user_data,
511                                g_volume_eject_with_operation,
512                                G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
513                                /* Translators: This is an error
514                                 * message for volume objects that
515                                 * don't implement any of eject or eject_with_operation. */
516                                _("volume doesn't implement eject or eject_with_operation"));
517       return;
518     }
519
520   if (iface->eject_with_operation != NULL)
521     (* iface->eject_with_operation) (volume, flags, mount_operation, cancellable, callback, user_data);
522   else
523     (* iface->eject) (volume, flags, cancellable, callback, user_data);
524 }
525
526 /**
527  * g_volume_eject_with_operation_finish:
528  * @volume: a #GVolume.
529  * @result: a #GAsyncResult.
530  * @error: a #GError location to store the error occurring, or %NULL to
531  *     ignore.
532  *
533  * Finishes ejecting a volume. If any errors occurred during the operation,
534  * @error will be set to contain the errors and %FALSE will be returned.
535  *
536  * Returns: %TRUE if the volume was successfully ejected. %FALSE otherwise.
537  *
538  * Since: 2.22
539  **/
540 gboolean
541 g_volume_eject_with_operation_finish (GVolume        *volume,
542                                       GAsyncResult  *result,
543                                       GError       **error)
544 {
545   GVolumeIface *iface;
546
547   g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
548   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
549
550   if (g_async_result_legacy_propagate_error (result, error))
551     return FALSE;
552   else if (g_async_result_is_tagged (result, g_volume_eject_with_operation))
553     return g_task_propagate_boolean (G_TASK (result), error);
554
555   iface = G_VOLUME_GET_IFACE (volume);
556   if (iface->eject_with_operation_finish != NULL)
557     return (* iface->eject_with_operation_finish) (volume, result, error);
558   else
559     return (* iface->eject_finish) (volume, result, error);
560 }
561
562 /**
563  * g_volume_get_identifier:
564  * @volume: a #GVolume
565  * @kind: the kind of identifier to return
566  *
567  * Gets the identifier of the given kind for @volume. 
568  * See the <link linkend="volume-identifier">introduction</link>
569  * for more information about volume identifiers.
570  *
571  * Returns: a newly allocated string containing the
572  *   requested identfier, or %NULL if the #GVolume
573  *   doesn't have this kind of identifier
574  */
575 char *
576 g_volume_get_identifier (GVolume    *volume,
577                          const char *kind)
578 {
579   GVolumeIface *iface;
580
581   g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
582   g_return_val_if_fail (kind != NULL, NULL);
583
584   iface = G_VOLUME_GET_IFACE (volume);
585
586   if (iface->get_identifier == NULL)
587     return NULL;
588   
589   return (* iface->get_identifier) (volume, kind);
590 }
591
592 /**
593  * g_volume_enumerate_identifiers:
594  * @volume: a #GVolume
595  * 
596  * Gets the kinds of <link linkend="volume-identifier">identifiers</link>
597  * that @volume has. Use g_volume_get_identifier() to obtain
598  * the identifiers themselves.
599  *
600  * Returns: (array zero-terminated=1) (transfer full): a %NULL-terminated array
601  *   of strings containing kinds of identifiers. Use g_strfreev() to free.
602  */
603 char **
604 g_volume_enumerate_identifiers (GVolume *volume)
605 {
606   GVolumeIface *iface;
607
608   g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
609   iface = G_VOLUME_GET_IFACE (volume);
610
611   if (iface->enumerate_identifiers == NULL)
612     return NULL;
613   
614   return (* iface->enumerate_identifiers) (volume);
615 }
616
617 /**
618  * g_volume_get_activation_root:
619  * @volume: a #GVolume
620  *
621  * Gets the activation root for a #GVolume if it is known ahead of
622  * mount time. Returns %NULL otherwise. If not %NULL and if @volume
623  * is mounted, then the result of g_mount_get_root() on the
624  * #GMount object obtained from g_volume_get_mount() will always
625  * either be equal or a prefix of what this function returns. In
626  * other words, in code
627  *
628  * <programlisting>
629  *   GMount *mount;
630  *   GFile *mount_root
631  *   GFile *volume_activation_root;
632  *
633  *   mount = g_volume_get_mount (volume); /&ast; mounted, so never NULL &ast;/
634  *   mount_root = g_mount_get_root (mount);
635  *   volume_activation_root = g_volume_get_activation_root(volume); /&ast; assume not NULL &ast;/
636  * </programlisting>
637  *
638  * then the expression
639  *
640  * <programlisting>
641  *   (g_file_has_prefix (volume_activation_root, mount_root) ||
642       g_file_equal (volume_activation_root, mount_root))
643  * </programlisting>
644  *
645  * will always be %TRUE.
646  *
647  * Activation roots are typically used in #GVolumeMonitor
648  * implementations to find the underlying mount to shadow, see
649  * g_mount_is_shadowed() for more details.
650  *
651  * Returns: (transfer full): the activation root of @volume or %NULL. Use
652  * g_object_unref() to free.
653  *
654  * Since: 2.18
655  **/
656 GFile *
657 g_volume_get_activation_root (GVolume *volume)
658 {
659   GVolumeIface *iface;
660
661   g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
662   iface = G_VOLUME_GET_IFACE (volume);
663
664   if (iface->get_activation_root == NULL)
665     return NULL;
666
667   return (* iface->get_activation_root) (volume);
668 }
669
670 /**
671  * g_volume_get_sort_key:
672  * @volume: A #GVolume.
673  *
674  * Gets the sort key for @volume, if any.
675  *
676  * Returns: Sorting key for @volume or %NULL if no such key is available.
677  *
678  * Since: 2.32
679  */
680 const gchar *
681 g_volume_get_sort_key (GVolume  *volume)
682 {
683   const gchar *ret = NULL;
684   GVolumeIface *iface;
685
686   g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
687
688   iface = G_VOLUME_GET_IFACE (volume);
689   if (iface->get_sort_key != NULL)
690     ret = iface->get_sort_key (volume);
691
692   return ret;
693 }