1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
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.
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.
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.
20 * Author: Alexander Larsson <alexl@redhat.com>
26 #include "glocalvfs.h"
27 #include "giomodule.h"
30 G_DEFINE_TYPE (GVfs, g_vfs, G_TYPE_OBJECT);
33 g_vfs_class_init (GVfsClass *klass)
38 g_vfs_init (GVfs *vfs)
46 * Returns TRUE if construction of the @vfs was successful and its now active.
49 g_vfs_is_active (GVfs *vfs)
53 g_return_val_if_fail (G_IS_VFS (vfs), FALSE);
55 class = G_VFS_GET_CLASS (vfs);
57 return (* class->is_active) (vfs);
62 * g_vfs_get_file_for_path:
63 * @vfs: an input #GVfs.
64 * @path: a string containing a VFS path.
66 * Returns a #GFile for the given @path.
70 g_vfs_get_file_for_path (GVfs *vfs,
75 g_return_val_if_fail (G_IS_VFS (vfs), NULL);
76 g_return_val_if_fail (path != NULL, NULL);
78 class = G_VFS_GET_CLASS (vfs);
80 return (* class->get_file_for_path) (vfs, path);
84 * g_vfs_get_file_for_uri:
85 * @vfs: an input #GVfs.
86 * @uri: an input string containing a URI path.
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.
92 * Returns a #GFile for the given @uri.
96 g_vfs_get_file_for_uri (GVfs *vfs,
101 g_return_val_if_fail (G_IS_VFS (vfs), NULL);
102 g_return_val_if_fail (uri != NULL, NULL);
104 class = G_VFS_GET_CLASS (vfs);
106 return (* class->get_file_for_uri) (vfs, uri);
110 * g_vfs_get_supported_uri_schemes:
111 * @vfs: an input #GVfs.
116 const gchar * const *
117 g_vfs_get_supported_uri_schemes (GVfs *vfs)
121 g_return_val_if_fail (G_IS_VFS (vfs), NULL);
123 class = G_VFS_GET_CLASS (vfs);
125 return (* class->get_supported_uri_schemes) (vfs);
130 * @vfs: an input #GVfs.
131 * @parse_name: a string to be parsed by the VFS module.
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.
137 * Returns a #GFile for the given @parse_name.
141 g_vfs_parse_name (GVfs *vfs,
142 const char *parse_name)
146 g_return_val_if_fail (G_IS_VFS (vfs), NULL);
147 g_return_val_if_fail (parse_name != NULL, NULL);
149 class = G_VFS_GET_CLASS (vfs);
151 return (* class->parse_name) (vfs, parse_name);
154 /* Note: This compares in reverse order.
155 Higher prio -> sort first
158 compare_vfs_type (gconstpointer a,
162 GVfsClass *class_a, *class_b;
164 const char *use_this_vfs;
166 class_a = g_type_class_ref (*(GType *)a);
167 class_b = g_type_class_ref (*(GType *)b);
168 use_this_vfs = user_data;
170 if (class_a == class_b)
172 else if (use_this_vfs != NULL &&
173 strcmp (class_a->name, use_this_vfs) == 0)
175 else if (use_this_vfs != NULL &&
176 strcmp (class_b->name, use_this_vfs) == 0)
179 res = class_b->priority - class_a->priority;
181 g_type_class_unref (class_a);
182 g_type_class_unref (class_b);
189 get_default_vfs (gpointer arg)
191 volatile GType local_type;
195 const char *use_this;
197 GType (*casted_get_type)(void);
199 use_this = g_getenv ("GIO_USE_VFS");
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 ();
206 /* Ensure vfs in modules loaded */
207 g_io_modules_ensure_loaded (GIO_MODULE_DIR);
209 vfs_impls = g_type_children (G_TYPE_VFS, &n_vfs_impls);
211 g_qsort_with_data (vfs_impls, n_vfs_impls, sizeof (GType),
212 compare_vfs_type, (gpointer)use_this);
214 for (i = 0; i < n_vfs_impls; i++)
216 vfs = g_object_new (vfs_impls[i], NULL);
218 if (g_vfs_is_active (vfs))
221 g_object_unref (vfs);
233 * Returns the default #GVfs for the system.
236 g_vfs_get_default (void)
238 static GOnce once_init = G_ONCE_INIT;
240 return g_once (&once_init, get_default_vfs, NULL);
246 * Returns the local #GVfs for the system.
249 g_vfs_get_local (void)
251 static gsize vfs = 0;
253 if (g_once_init_enter (&vfs))
254 g_once_init_leave (&vfs, (gsize)g_local_vfs_new ());