gio/ docs/reference/gio Merged gio-standalone into glib.
[platform/upstream/glib.git] / gio / gvfs.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  */
22
23 #include <config.h>
24 #include <string.h>
25 #include "gvfs.h"
26 #include "glocalvfs.h"
27 #include "giomodule.h"
28 #include "glibintl.h"
29
30 G_DEFINE_TYPE (GVfs, g_vfs, G_TYPE_OBJECT);
31
32 static void
33 g_vfs_class_init (GVfsClass *klass)
34 {
35 }
36
37 static void
38 g_vfs_init (GVfs *vfs)
39 {
40 }
41
42 /**
43  * g_vfs_is_active:
44  * @vfs: an  #GVfs.
45  * 
46  * Returns TRUE if construction of the @vfs was successful and its now active.
47  **/
48 gboolean
49 g_vfs_is_active (GVfs *vfs)
50 {
51   GVfsClass *class;
52
53   g_return_val_if_fail (G_IS_VFS (vfs), FALSE);
54
55   class = G_VFS_GET_CLASS (vfs);
56
57   return (* class->is_active) (vfs);
58 }
59
60
61 /**
62  * g_vfs_get_file_for_path:
63  * @vfs: an input #GVfs.
64  * @path: a string containing a VFS path.
65  * 
66  * Returns a #GFile for the given @path.
67  * 
68  **/
69 GFile *
70 g_vfs_get_file_for_path (GVfs *vfs,
71                          const char *path)
72 {
73   GVfsClass *class;
74   
75   g_return_val_if_fail (G_IS_VFS (vfs), NULL);
76   g_return_val_if_fail (path != NULL, NULL);
77
78   class = G_VFS_GET_CLASS (vfs);
79
80   return (* class->get_file_for_path) (vfs, path);
81 }
82
83 /**
84  * g_vfs_get_file_for_uri:
85  * @vfs: an input #GVfs.
86  * @uri: an input string containing a URI path.
87  *
88  * This operation never fails, but the returned object
89  * might not support any I/O operation if the uri
90  * is malformed or if the uri type is not supported.
91  * 
92  * Returns a #GFile for the given @uri. 
93  * 
94  **/
95 GFile *
96 g_vfs_get_file_for_uri (GVfs *vfs,
97                         const char *uri)
98 {
99   GVfsClass *class;
100   
101   g_return_val_if_fail (G_IS_VFS (vfs), NULL);
102   g_return_val_if_fail (uri != NULL, NULL);
103
104   class = G_VFS_GET_CLASS (vfs);
105
106   return (* class->get_file_for_uri) (vfs, uri);
107 }
108
109 /**
110  * g_vfs_get_supported_uri_schemes:
111  * @vfs: an input #GVfs.
112  * 
113  * Returns: 
114  * 
115  **/
116 const gchar * const *
117 g_vfs_get_supported_uri_schemes (GVfs *vfs)
118 {
119   GVfsClass *class;
120   
121   g_return_val_if_fail (G_IS_VFS (vfs), NULL);
122
123   class = G_VFS_GET_CLASS (vfs);
124
125   return (* class->get_supported_uri_schemes) (vfs);
126 }
127
128 /**
129  * g_vfs_parse_name:
130  * @vfs: an input #GVfs.
131  * @parse_name: a string to be parsed by the VFS module.
132  * 
133  * This operation never fails, but the returned object might 
134  * not support any I/O operations if the @parse_name cannot 
135  * be parsed by the #GVfs module.
136  * 
137  * Returns a #GFile for the given @parse_name.
138  * 
139  **/
140 GFile *
141 g_vfs_parse_name (GVfs *vfs,
142                   const char *parse_name)
143 {
144   GVfsClass *class;
145   
146   g_return_val_if_fail (G_IS_VFS (vfs), NULL);
147   g_return_val_if_fail (parse_name != NULL, NULL);
148
149   class = G_VFS_GET_CLASS (vfs);
150
151   return (* class->parse_name) (vfs, parse_name);
152 }
153
154 /* Note: This compares in reverse order.
155    Higher prio -> sort first
156  */
157 static gint
158 compare_vfs_type (gconstpointer  a,
159                   gconstpointer  b,
160                   gpointer       user_data)
161 {
162   GVfsClass *class_a, *class_b;
163   gint res;
164   const char *use_this_vfs;
165   
166   class_a = g_type_class_ref (*(GType *)a);
167   class_b = g_type_class_ref (*(GType *)b);
168   use_this_vfs = user_data;
169
170   if (class_a == class_b)
171     res = 0;
172   else if (use_this_vfs != NULL &&
173            strcmp (class_a->name, use_this_vfs) == 0)
174     res = -1;
175   else if (use_this_vfs != NULL &&
176            strcmp (class_b->name, use_this_vfs) == 0)
177     res = 1;
178   else 
179     res = class_b->priority - class_a->priority;
180   
181   g_type_class_unref (class_a);
182   g_type_class_unref (class_b);
183   
184   return res;
185 }
186
187
188 static gpointer
189 get_default_vfs (gpointer arg)
190 {
191   volatile GType local_type;
192   GType *vfs_impls;
193   int i;
194   guint n_vfs_impls;
195   const char *use_this;
196   GVfs *vfs;
197   GType (*casted_get_type)(void);
198
199   use_this = g_getenv ("GIO_USE_VFS");
200   
201   /* Ensure GLocalVfs type is available
202      the cast is required to avoid any G_GNUC_CONST optimizations */
203   casted_get_type = g_local_vfs_get_type;
204   local_type = casted_get_type ();
205   
206   /* Ensure vfs in modules loaded */
207   g_io_modules_ensure_loaded (GIO_MODULE_DIR);
208
209   vfs_impls = g_type_children (G_TYPE_VFS, &n_vfs_impls);
210
211   g_qsort_with_data (vfs_impls, n_vfs_impls, sizeof (GType),
212                      compare_vfs_type, (gpointer)use_this);
213   
214   for (i = 0; i < n_vfs_impls; i++)
215     {
216       vfs = g_object_new (vfs_impls[i], NULL);
217
218       if (g_vfs_is_active (vfs))
219         break;
220
221       g_object_unref (vfs);
222       vfs = NULL;
223     }
224   
225   g_free (vfs_impls);
226
227   return vfs;
228 }
229
230 /**
231  * g_vfs_get_default:
232  * 
233  * Returns the default #GVfs for the system.
234  **/
235 GVfs *
236 g_vfs_get_default (void)
237 {
238   static GOnce once_init = G_ONCE_INIT;
239   
240   return g_once (&once_init, get_default_vfs, NULL);
241 }
242
243 /**
244  * g_vfs_get_local:
245  * 
246  * Returns the local #GVfs for the system.
247  **/
248 GVfs *
249 g_vfs_get_local (void)
250 {
251   static gsize vfs = 0;
252
253   if (g_once_init_enter (&vfs))
254     g_once_init_leave (&vfs, (gsize)g_local_vfs_new ());
255
256   return G_VFS (vfs);
257 }
258