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