cleanup
[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, see <http://www.gnu.org/licenses/>.
17  *
18  * Author: Alexander Larsson <alexl@redhat.com>
19  */
20
21 #include "config.h"
22 #include <string.h>
23 #include "gvfs.h"
24 #include "glib-private.h"
25 #include "glocalvfs.h"
26 #include "gresourcefile.h"
27 #include "giomodule-priv.h"
28 #include "glibintl.h"
29
30
31 /**
32  * SECTION:gvfs
33  * @short_description: Virtual File System
34  * @include: gio/gio.h
35  *
36  * Entry point for using GIO functionality.
37  *
38  */
39
40 G_DEFINE_TYPE (GVfs, g_vfs, G_TYPE_OBJECT);
41
42 static void
43 g_vfs_class_init (GVfsClass *klass)
44 {
45 }
46
47 static void
48 g_vfs_init (GVfs *vfs)
49 {
50 }
51
52 /**
53  * g_vfs_is_active:
54  * @vfs: a #GVfs.
55  *
56  * Checks if the VFS is active.
57  *
58  * Returns: %TRUE if construction of the @vfs was successful
59  *     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: (transfer full): a #GFile.
82  *     Free the returned object with g_object_unref().
83  */
84 GFile *
85 g_vfs_get_file_for_path (GVfs       *vfs,
86                          const char *path)
87 {
88   GVfsClass *class;
89  
90   g_return_val_if_fail (G_IS_VFS (vfs), NULL);
91   g_return_val_if_fail (path != NULL, NULL);
92
93   class = G_VFS_GET_CLASS (vfs);
94
95   return (* class->get_file_for_path) (vfs, path);
96 }
97
98 /**
99  * g_vfs_get_file_for_uri:
100  * @vfs: a#GVfs.
101  * @uri: a string containing a URI
102  *
103  * Gets a #GFile for @uri.
104  *
105  * This operation never fails, but the returned object
106  * might not support any I/O operation if the URI
107  * is malformed or if the URI scheme is not supported.
108  *
109  * Returns: (transfer full): a #GFile.
110  *     Free the returned object with g_object_unref().
111  */
112 GFile *
113 g_vfs_get_file_for_uri (GVfs       *vfs,
114                         const char *uri)
115 {
116   GVfsClass *class;
117  
118   g_return_val_if_fail (G_IS_VFS (vfs), NULL);
119   g_return_val_if_fail (uri != NULL, NULL);
120
121   class = G_VFS_GET_CLASS (vfs);
122
123   /* This is an unfortunate placement, but we really
124    * need to check this before chaining to the vfs,
125    * because we want to support resource uris for
126    * all vfs:es, even those that predate resources.
127    */
128   if (g_str_has_prefix (uri, "resource:"))
129     return _g_resource_file_new (uri);
130
131   return (* class->get_file_for_uri) (vfs, uri);
132 }
133
134 /**
135  * g_vfs_get_supported_uri_schemes:
136  * @vfs: a #GVfs.
137  *
138  * Gets a list of URI schemes supported by @vfs.
139  *
140  * Returns: (transfer none): a %NULL-terminated array of strings.
141  *     The returned array belongs to GIO and must
142  *     not be freed or modified.
143  */
144 const gchar * const *
145 g_vfs_get_supported_uri_schemes (GVfs *vfs)
146 {
147   GVfsClass *class;
148
149   g_return_val_if_fail (G_IS_VFS (vfs), NULL);
150
151   class = G_VFS_GET_CLASS (vfs);
152
153   return (* class->get_supported_uri_schemes) (vfs);
154 }
155
156 /**
157  * g_vfs_parse_name:
158  * @vfs: a #GVfs.
159  * @parse_name: a string to be parsed by the VFS module.
160  *
161  * This operation never fails, but the returned object might
162  * not support any I/O operations if the @parse_name cannot
163  * be parsed by the #GVfs module.
164  *
165  * Returns: (transfer full): a #GFile for the given @parse_name.
166  *     Free the returned object with g_object_unref().
167  */
168 GFile *
169 g_vfs_parse_name (GVfs       *vfs,
170                   const char *parse_name)
171 {
172   GVfsClass *class;
173
174   g_return_val_if_fail (G_IS_VFS (vfs), NULL);
175   g_return_val_if_fail (parse_name != NULL, NULL);
176
177   class = G_VFS_GET_CLASS (vfs);
178
179   if (g_str_has_prefix (parse_name, "resource:"))
180     return _g_resource_file_new (parse_name);
181
182   return (* class->parse_name) (vfs, parse_name);
183 }
184
185 /**
186  * g_vfs_get_default:
187  *
188  * Gets the default #GVfs for the system.
189  *
190  * Returns: (transfer none): a #GVfs.
191  */
192 GVfs *
193 g_vfs_get_default (void)
194 {
195   if (GLIB_PRIVATE_CALL (g_check_setuid) ())
196     return g_vfs_get_local ();
197   return _g_io_module_get_default (G_VFS_EXTENSION_POINT_NAME,
198                                    "GIO_USE_VFS",
199                                    (GIOModuleVerifyFunc)g_vfs_is_active);
200 }
201
202 /**
203  * g_vfs_get_local:
204  *
205  * Gets the local #GVfs for the system.
206  *
207  * Returns: (transfer none): a #GVfs.
208  */
209 GVfs *
210 g_vfs_get_local (void)
211 {
212   static gsize vfs = 0;
213
214   if (g_once_init_enter (&vfs))
215     g_once_init_leave (&vfs, (gsize)_g_local_vfs_new ());
216
217   return G_VFS (vfs);
218 }