xdgmime: plug a small leak
[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 "gsimpleasyncresult.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  * #GSimpleAsyncResult. To mount a #GVolume, first call
46  * g_volume_mount() with (at least) the #GVolume instance, optionally
47  * a #GMountOperation object 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_simple_async_report_error_in_idle (G_OBJECT (volume), callback, user_data,
363                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
364                                            _("volume doesn't implement mount"));
365       
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   
401   iface = G_VOLUME_GET_IFACE (volume);
402   return (* iface->mount_finish) (volume, result, error);
403 }
404
405 /**
406  * g_volume_eject:
407  * @volume: a #GVolume.
408  * @flags: flags affecting the unmount if required for eject
409  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
410  * @callback: (allow-none): a #GAsyncReadyCallback, or %NULL.
411  * @user_data: user data that gets passed to @callback
412  * 
413  * Ejects a volume. This is an asynchronous operation, and is
414  * finished by calling g_volume_eject_finish() with the @volume
415  * and #GAsyncResult returned in the @callback.
416  *
417  * Deprecated: 2.22: Use g_volume_eject_with_operation() instead.
418  **/
419 void
420 g_volume_eject (GVolume             *volume,
421                 GMountUnmountFlags   flags,
422                 GCancellable        *cancellable,
423                 GAsyncReadyCallback  callback,
424                 gpointer             user_data)
425 {
426   GVolumeIface *iface;
427
428   g_return_if_fail (G_IS_VOLUME (volume));
429
430   iface = G_VOLUME_GET_IFACE (volume);
431
432   if (iface->eject == NULL)
433     {
434       g_simple_async_report_error_in_idle (G_OBJECT (volume), callback, user_data,
435                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
436                                            _("volume doesn't implement eject"));
437       
438       return;
439     }
440   
441   (* iface->eject) (volume, flags, cancellable, callback, user_data);
442 }
443
444 /**
445  * g_volume_eject_finish:
446  * @volume: pointer to a #GVolume.
447  * @result: a #GAsyncResult.
448  * @error: a #GError location to store an error, or %NULL to ignore
449  * 
450  * Finishes ejecting a volume. If any errors occurred during the operation,
451  * @error will be set to contain the errors and %FALSE will be returned.
452  * 
453  * Returns: %TRUE, %FALSE if operation failed.
454  *
455  * Deprecated: 2.22: Use g_volume_eject_with_operation_finish() instead.
456  **/
457 gboolean
458 g_volume_eject_finish (GVolume       *volume,
459                        GAsyncResult  *result,
460                        GError       **error)
461 {
462   GVolumeIface *iface;
463
464   g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
465   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
466
467   if (g_async_result_legacy_propagate_error (result, error))
468     return FALSE;
469   
470   iface = G_VOLUME_GET_IFACE (volume);
471   return (* iface->eject_finish) (volume, result, error);
472 }
473
474 /**
475  * g_volume_eject_with_operation:
476  * @volume: a #GVolume.
477  * @flags: flags affecting the unmount if required for eject
478  * @mount_operation: (allow-none): a #GMountOperation or %NULL to
479  *     avoid user interaction.
480  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
481  * @callback: (allow-none): a #GAsyncReadyCallback, or %NULL.
482  * @user_data: user data passed to @callback.
483  *
484  * Ejects a volume. This is an asynchronous operation, and is
485  * finished by calling g_volume_eject_with_operation_finish() with the @volume
486  * and #GAsyncResult data returned in the @callback.
487  *
488  * Since: 2.22
489  **/
490 void
491 g_volume_eject_with_operation (GVolume              *volume,
492                                GMountUnmountFlags   flags,
493                                GMountOperation     *mount_operation,
494                                GCancellable        *cancellable,
495                                GAsyncReadyCallback  callback,
496                                gpointer             user_data)
497 {
498   GVolumeIface *iface;
499
500   g_return_if_fail (G_IS_VOLUME (volume));
501
502   iface = G_VOLUME_GET_IFACE (volume);
503
504   if (iface->eject == NULL && iface->eject_with_operation == NULL)
505     {
506       g_simple_async_report_error_in_idle (G_OBJECT (volume),
507                                            callback, user_data,
508                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
509                                            /* Translators: This is an error
510                                             * message for volume objects that
511                                             * don't implement any of eject or eject_with_operation. */
512                                            _("volume doesn't implement eject or eject_with_operation"));
513       return;
514     }
515
516   if (iface->eject_with_operation != NULL)
517     (* iface->eject_with_operation) (volume, flags, mount_operation, cancellable, callback, user_data);
518   else
519     (* iface->eject) (volume, flags, cancellable, callback, user_data);
520 }
521
522 /**
523  * g_volume_eject_with_operation_finish:
524  * @volume: a #GVolume.
525  * @result: a #GAsyncResult.
526  * @error: a #GError location to store the error occurring, or %NULL to
527  *     ignore.
528  *
529  * Finishes ejecting a volume. If any errors occurred during the operation,
530  * @error will be set to contain the errors and %FALSE will be returned.
531  *
532  * Returns: %TRUE if the volume was successfully ejected. %FALSE otherwise.
533  *
534  * Since: 2.22
535  **/
536 gboolean
537 g_volume_eject_with_operation_finish (GVolume        *volume,
538                                       GAsyncResult  *result,
539                                       GError       **error)
540 {
541   GVolumeIface *iface;
542
543   g_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
544   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
545
546   if (g_async_result_legacy_propagate_error (result, error))
547     return FALSE;
548
549   iface = G_VOLUME_GET_IFACE (volume);
550   if (iface->eject_with_operation_finish != NULL)
551     return (* iface->eject_with_operation_finish) (volume, result, error);
552   else
553     return (* iface->eject_finish) (volume, result, error);
554 }
555
556 /**
557  * g_volume_get_identifier:
558  * @volume: a #GVolume
559  * @kind: the kind of identifier to return
560  *
561  * Gets the identifier of the given kind for @volume. 
562  * See the <link linkend="volume-identifier">introduction</link>
563  * for more information about volume identifiers.
564  *
565  * Returns: a newly allocated string containing the
566  *   requested identfier, or %NULL if the #GVolume
567  *   doesn't have this kind of identifier
568  */
569 char *
570 g_volume_get_identifier (GVolume    *volume,
571                          const char *kind)
572 {
573   GVolumeIface *iface;
574
575   g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
576   g_return_val_if_fail (kind != NULL, NULL);
577
578   iface = G_VOLUME_GET_IFACE (volume);
579
580   if (iface->get_identifier == NULL)
581     return NULL;
582   
583   return (* iface->get_identifier) (volume, kind);
584 }
585
586 /**
587  * g_volume_enumerate_identifiers:
588  * @volume: a #GVolume
589  * 
590  * Gets the kinds of <link linkend="volume-identifier">identifiers</link>
591  * that @volume has. Use g_volume_get_identifier() to obtain
592  * the identifiers themselves.
593  *
594  * Returns: (array zero-terminated=1) (transfer full): a %NULL-terminated array
595  *   of strings containing kinds of identifiers. Use g_strfreev() to free.
596  */
597 char **
598 g_volume_enumerate_identifiers (GVolume *volume)
599 {
600   GVolumeIface *iface;
601
602   g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
603   iface = G_VOLUME_GET_IFACE (volume);
604
605   if (iface->enumerate_identifiers == NULL)
606     return NULL;
607   
608   return (* iface->enumerate_identifiers) (volume);
609 }
610
611 /**
612  * g_volume_get_activation_root:
613  * @volume: a #GVolume
614  *
615  * Gets the activation root for a #GVolume if it is known ahead of
616  * mount time. Returns %NULL otherwise. If not %NULL and if @volume
617  * is mounted, then the result of g_mount_get_root() on the
618  * #GMount object obtained from g_volume_get_mount() will always
619  * either be equal or a prefix of what this function returns. In
620  * other words, in code
621  *
622  * <programlisting>
623  *   GMount *mount;
624  *   GFile *mount_root
625  *   GFile *volume_activation_root;
626  *
627  *   mount = g_volume_get_mount (volume); /&ast; mounted, so never NULL &ast;/
628  *   mount_root = g_mount_get_root (mount);
629  *   volume_activation_root = g_volume_get_activation_root(volume); /&ast; assume not NULL &ast;/
630  * </programlisting>
631  *
632  * then the expression
633  *
634  * <programlisting>
635  *   (g_file_has_prefix (volume_activation_root, mount_root) ||
636       g_file_equal (volume_activation_root, mount_root))
637  * </programlisting>
638  *
639  * will always be %TRUE.
640  *
641  * Activation roots are typically used in #GVolumeMonitor
642  * implementations to find the underlying mount to shadow, see
643  * g_mount_is_shadowed() for more details.
644  *
645  * Returns: (transfer full): the activation root of @volume or %NULL. Use
646  * g_object_unref() to free.
647  *
648  * Since: 2.18
649  **/
650 GFile *
651 g_volume_get_activation_root (GVolume *volume)
652 {
653   GVolumeIface *iface;
654
655   g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
656   iface = G_VOLUME_GET_IFACE (volume);
657
658   if (iface->get_activation_root == NULL)
659     return NULL;
660
661   return (* iface->get_activation_root) (volume);
662 }
663
664 /**
665  * g_volume_get_sort_key:
666  * @volume: A #GVolume.
667  *
668  * Gets the sort key for @volume, if any.
669  *
670  * Returns: Sorting key for @volume or %NULL if no such key is available.
671  *
672  * Since: 2.32
673  */
674 const gchar *
675 g_volume_get_sort_key (GVolume  *volume)
676 {
677   const gchar *ret = NULL;
678   GVolumeIface *iface;
679
680   g_return_val_if_fail (G_IS_VOLUME (volume), NULL);
681
682   iface = G_VOLUME_GET_IFACE (volume);
683   if (iface->get_sort_key != NULL)
684     ret = iface->get_sort_key (volume);
685
686   return ret;
687 }