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