libcaptive-gnomevfs.so separated to Gnome-VFS vs. reactos specific parts
[captive.git] / src / client / libcaptive-gnomevfs / gnome-vfs-method.c
1 /* $Id$
2  * gnome-vfs init/shutdown implementation of interface to libcaptive
3  * Copyright (C) 2002 Jan Kratochvil <project-captive@jankratochvil.net>
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; exactly version 2 of June 1991 is required
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19
20 #include "config.h"
21
22 #include "giognomevfs.h"
23 #include <libgnomevfs/gnome-vfs-method.h>
24 #include <glib/gmessages.h>
25 #include "captive/macros.h"
26 #include "gnome-vfs-module.h"
27 #include "captive/client-directory.h"
28 #include "captive/client-file.h"
29 #include "captive/client-file_info.h"
30
31
32 static GnomeVFSMethod GnomeVFSMethod_static;
33 G_LOCK_DEFINE_STATIC(GnomeVFSMethod_static);
34
35
36 /* FIXME: multi-parent */
37 G_LOCK_DEFINE_STATIC(captive_gnomevfs_uri_parent);      /* covers also 'captive_gnomevfs_uri_parent_giognomevfs' */
38 static gchar *captive_gnomevfs_uri_parent;
39 static gchar *captive_gnomevfs_uri_parent_fs_path;
40 static struct captive_gnomevfs_giognomevfs *captive_gnomevfs_uri_parent_giognomevfs;
41
42
43 static GnomeVFSResult captive_gnomevfs_uri_parent_init(GnomeVFSURI *uri)
44 {
45 gchar *uri_parent_string;
46 GnomeVFSResult errvfsresult;
47
48         g_return_val_if_fail(uri!=NULL,GNOME_VFS_ERROR_INVALID_URI);
49
50         if (!uri->parent)
51                 return GNOME_VFS_ERROR_INVALID_URI;
52         if (!uri->text) /* not needed here but we don't permit non-specific fs-image reference */
53                 return GNOME_VFS_ERROR_INVALID_URI;
54         uri_parent_string=gnome_vfs_uri_to_string(uri->parent,GNOME_VFS_URI_HIDE_NONE);
55         g_assert(uri_parent_string!=NULL);
56         /* FIXME: multi-parent */
57         G_LOCK(captive_gnomevfs_uri_parent);
58         if (captive_gnomevfs_uri_parent) {
59                 g_return_val_if_fail(!strcmp(uri_parent_string,captive_gnomevfs_uri_parent),GNOME_VFS_ERROR_TOO_MANY_OPEN_FILES);
60                 g_free(uri_parent_string);
61                 }
62         else {
63                 errvfsresult=captive_gnomevfs_giognomevfs_new(&captive_gnomevfs_uri_parent_giognomevfs,
64                                 captive_gnomevfs_uri_parent_fs_path,uri->parent);
65                 g_return_val_if_fail(errvfsresult==GNOME_VFS_OK,errvfsresult);
66                 captive_gnomevfs_uri_parent=uri_parent_string;
67                 }
68         G_UNLOCK(captive_gnomevfs_uri_parent);
69
70         return GNOME_VFS_OK;
71 }
72
73
74 static GnomeVFSResult captive_gnomevfs_open_directory(GnomeVFSMethod *method,
75                 GnomeVFSMethodHandle **method_handle,GnomeVFSURI *uri,GnomeVFSFileInfoOptions options,GnomeVFSContext *context)
76 {
77 GnomeVFSResult errvfsresult;
78 CaptiveDirectoryObject *captive_directory_object;
79
80         g_return_val_if_fail(method==&GnomeVFSMethod_static,GNOME_VFS_ERROR_BAD_PARAMETERS);
81         g_return_val_if_fail(method_handle!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
82
83         errvfsresult=captive_gnomevfs_uri_parent_init(uri);
84         g_return_val_if_fail(errvfsresult==GNOME_VFS_OK,errvfsresult);
85
86         G_LOCK(libcaptive);
87         errvfsresult=captive_directory_new_open(
88                         &captive_directory_object,      /* captive_directory_object_return */
89                         uri->text);     /* pathname */
90         G_UNLOCK(libcaptive);
91
92         *method_handle=(GnomeVFSMethodHandle *)captive_directory_object;
93         return errvfsresult;
94 }
95
96
97 static GnomeVFSResult captive_gnomevfs_close_directory(GnomeVFSMethod *method,
98                 GnomeVFSMethodHandle *method_handle,GnomeVFSContext *context)
99 {
100 CaptiveDirectoryObject *captive_directory_object;
101
102         g_return_val_if_fail(method==&GnomeVFSMethod_static,GNOME_VFS_ERROR_BAD_PARAMETERS);
103         captive_directory_object=(CaptiveDirectoryObject *)method_handle;
104         g_return_val_if_fail(CAPTIVE_DIRECTORY_IS_OBJECT(captive_directory_object),GNOME_VFS_ERROR_BAD_PARAMETERS);
105
106         G_LOCK(libcaptive);
107         g_object_unref(captive_directory_object);
108         G_UNLOCK(libcaptive);
109
110         return GNOME_VFS_OK;
111 }
112
113
114 static GnomeVFSResult captive_gnomevfs_read_directory(GnomeVFSMethod *method,
115                 GnomeVFSMethodHandle *method_handle,GnomeVFSFileInfo *file_info,GnomeVFSContext *context)
116 {
117 GnomeVFSResult errvfsresult;
118 CaptiveDirectoryObject *captive_directory_object;
119
120         g_return_val_if_fail(method==&GnomeVFSMethod_static,GNOME_VFS_ERROR_BAD_PARAMETERS);
121         captive_directory_object=(CaptiveDirectoryObject *)method_handle;
122         g_return_val_if_fail(CAPTIVE_DIRECTORY_IS_OBJECT(captive_directory_object),GNOME_VFS_ERROR_BAD_PARAMETERS);
123         g_return_val_if_fail(file_info!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
124
125         G_LOCK(libcaptive);
126         errvfsresult=captive_directory_read(captive_directory_object,file_info);
127         G_UNLOCK(libcaptive);
128
129         return errvfsresult;
130 }
131
132
133 static GnomeVFSResult captive_gnomevfs_open(GnomeVFSMethod *method,
134                 GnomeVFSMethodHandle **method_handle_return,GnomeVFSURI *uri,GnomeVFSOpenMode mode,GnomeVFSContext *context)
135 {
136 GnomeVFSResult errvfsresult;
137 CaptiveFileObject *captive_file_object;
138
139         g_return_val_if_fail(method==&GnomeVFSMethod_static,GNOME_VFS_ERROR_BAD_PARAMETERS);
140         g_return_val_if_fail(method_handle_return!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
141
142         errvfsresult=captive_gnomevfs_uri_parent_init(uri);
143         g_return_val_if_fail(errvfsresult==GNOME_VFS_OK,errvfsresult);
144
145         G_LOCK(libcaptive);
146         errvfsresult=captive_file_new_open(&captive_file_object,uri->text,mode);
147         G_UNLOCK(libcaptive);
148
149         *method_handle_return=(GnomeVFSMethodHandle *)captive_file_object;
150         return errvfsresult;
151 }
152
153
154 static GnomeVFSResult captive_gnomevfs_close(GnomeVFSMethod *method,
155                 GnomeVFSMethodHandle *method_handle,GnomeVFSContext *context)
156 {
157 CaptiveFileObject *captive_file_object;
158
159         g_return_val_if_fail(method==&GnomeVFSMethod_static,GNOME_VFS_ERROR_BAD_PARAMETERS);
160         captive_file_object=(CaptiveFileObject *)method_handle;
161         g_return_val_if_fail(CAPTIVE_FILE_IS_OBJECT(captive_file_object),GNOME_VFS_ERROR_BAD_PARAMETERS);
162
163         G_LOCK(libcaptive);
164         g_object_unref(captive_file_object);
165         G_UNLOCK(libcaptive);
166
167         return GNOME_VFS_OK;
168 }
169
170
171 static GnomeVFSResult captive_gnomevfs_read(GnomeVFSMethod *method,GnomeVFSMethodHandle *method_handle,
172                 gpointer buffer,GnomeVFSFileSize num_bytes,GnomeVFSFileSize *bytes_read_return,GnomeVFSContext *context)
173 {
174 GnomeVFSResult errvfsresult;
175 CaptiveFileObject *captive_file_object;
176
177         g_return_val_if_fail(method==&GnomeVFSMethod_static,GNOME_VFS_ERROR_BAD_PARAMETERS);
178         captive_file_object=(CaptiveFileObject *)method_handle;
179         g_return_val_if_fail(CAPTIVE_FILE_IS_OBJECT(captive_file_object),GNOME_VFS_ERROR_BAD_PARAMETERS);
180         g_return_val_if_fail(buffer!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
181         g_return_val_if_fail(bytes_read_return!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
182
183         G_LOCK(libcaptive);
184         errvfsresult=captive_file_read(captive_file_object,buffer,num_bytes,bytes_read_return);
185         G_UNLOCK(libcaptive);
186
187         return errvfsresult;
188 }
189
190
191 static GnomeVFSResult captive_gnomevfs_seek(GnomeVFSMethod *method,
192                 GnomeVFSMethodHandle *method_handle,GnomeVFSSeekPosition whence,GnomeVFSFileOffset offset,GnomeVFSContext *context)
193 {
194 GnomeVFSResult errvfsresult;
195 CaptiveFileObject *captive_file_object;
196
197         g_return_val_if_fail(method==&GnomeVFSMethod_static,GNOME_VFS_ERROR_BAD_PARAMETERS);
198         captive_file_object=(CaptiveFileObject *)method_handle;
199         g_return_val_if_fail(CAPTIVE_FILE_IS_OBJECT(captive_file_object),GNOME_VFS_ERROR_BAD_PARAMETERS);
200
201         G_LOCK(libcaptive);
202         errvfsresult=captive_file_seek(captive_file_object,whence,offset);
203         G_UNLOCK(libcaptive);
204
205         return errvfsresult;
206 }
207
208 static GnomeVFSResult captive_gnomevfs_tell(GnomeVFSMethod *method,
209                 GnomeVFSMethodHandle *method_handle,GnomeVFSFileOffset *offset_return)
210 {
211 GnomeVFSResult errvfsresult;
212 CaptiveFileObject *captive_file_object;
213
214         g_return_val_if_fail(method==&GnomeVFSMethod_static,GNOME_VFS_ERROR_BAD_PARAMETERS);
215         captive_file_object=(CaptiveFileObject *)method_handle;
216         g_return_val_if_fail(CAPTIVE_FILE_IS_OBJECT(captive_file_object),GNOME_VFS_ERROR_BAD_PARAMETERS);
217         g_return_val_if_fail(offset_return!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
218
219         G_LOCK(libcaptive);
220         errvfsresult=captive_file_tell(captive_file_object,offset_return);
221         G_UNLOCK(libcaptive);
222
223         return errvfsresult;
224 }
225
226
227 static GnomeVFSResult captive_gnomevfs_create(GnomeVFSMethod *method,
228                 GnomeVFSMethodHandle **method_handle_return,GnomeVFSURI *uri,GnomeVFSOpenMode mode,gboolean exclusive,guint perm,
229                 GnomeVFSContext *context)
230 {
231         g_return_val_if_reached(GNOME_VFS_ERROR_NOT_SUPPORTED); /* FIXME: NOT IMPLEMENTED YET */
232 }
233
234
235 static gboolean captive_gnomevfs_is_local(GnomeVFSMethod *method,const GnomeVFSURI *uri)
236 {
237         g_return_val_if_fail(method==&GnomeVFSMethod_static,GNOME_VFS_ERROR_BAD_PARAMETERS);
238         g_return_val_if_fail(uri!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
239
240         return gnome_vfs_uri_is_local(uri->parent);
241 }
242
243
244 static GnomeVFSResult captive_gnomevfs_get_file_info(GnomeVFSMethod *method,
245                 GnomeVFSURI *uri,GnomeVFSFileInfo *file_info,GnomeVFSFileInfoOptions options,GnomeVFSContext *context)
246 {
247 GnomeVFSResult errvfsresult;
248
249         g_return_val_if_fail(method==&GnomeVFSMethod_static,GNOME_VFS_ERROR_BAD_PARAMETERS);
250         g_return_val_if_fail(file_info!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
251         /* handle 'options & GNOME_VFS_FILE_INFO_GET_MIME_TYPE'? */
252
253         errvfsresult=captive_gnomevfs_uri_parent_init(uri);
254         g_return_val_if_fail(errvfsresult==GNOME_VFS_OK,errvfsresult);
255
256         G_LOCK(libcaptive);
257         errvfsresult=captive_file_info_get(uri->text,file_info);
258         G_UNLOCK(libcaptive);
259
260         return errvfsresult;
261 }
262
263
264 /**
265  * captive_gnomevfs_init:
266  *
267  * Returns: Initialized structure of #GnomeVFSMethod with static methods of libcaptive-gnomevfs.
268  */
269 GnomeVFSMethod *captive_gnomevfs_method_init(const gchar *fs_path)
270 {
271         G_LOCK(captive_gnomevfs_uri_parent);
272         captive_gnomevfs_uri_parent_fs_path=g_strdup(fs_path);
273         G_UNLOCK(captive_gnomevfs_uri_parent);
274
275         G_LOCK(GnomeVFSMethod_static);
276         CAPTIVE_MEMZERO(&GnomeVFSMethod_static);
277         GnomeVFSMethod_static.method_table_size=sizeof(GnomeVFSMethod_static);
278         GnomeVFSMethod_static.open_directory =captive_gnomevfs_open_directory;
279         GnomeVFSMethod_static.close_directory=captive_gnomevfs_close_directory;
280         GnomeVFSMethod_static.read_directory =captive_gnomevfs_read_directory;
281         GnomeVFSMethod_static.open           =captive_gnomevfs_open;    /* mandatory */
282         GnomeVFSMethod_static.close          =captive_gnomevfs_close;
283         GnomeVFSMethod_static.read           =captive_gnomevfs_read;
284         GnomeVFSMethod_static.seek           =captive_gnomevfs_seek;
285         GnomeVFSMethod_static.tell           =captive_gnomevfs_tell;
286         GnomeVFSMethod_static.create         =captive_gnomevfs_create;  /* mandatory */
287         GnomeVFSMethod_static.is_local       =captive_gnomevfs_is_local;        /* mandatory */
288         GnomeVFSMethod_static.get_file_info  =captive_gnomevfs_get_file_info;   /* mandatory */
289         G_UNLOCK(GnomeVFSMethod_static);
290
291         return &GnomeVFSMethod_static;
292 }