1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * gnome-storage-vfs.c: Gnome VFS based storage implementation
6 * Michael Meeks <michael@helixcode.com>
9 #include "bonobo-storage-vfs.h"
11 #include "bonobo-stream-vfs.h"
12 #include <bonobo/bonobo-storage.h>
13 #include <libgnomevfs/gnome-vfs-directory.h>
14 #include <libgnomevfs/gnome-vfs-ops.h>
19 static BonoboObjectClass *bonobo_storage_vfs_parent_class;
22 concat_dir_and_file (const char *dir, const char *file)
24 g_return_val_if_fail (dir != NULL, NULL);
25 g_return_val_if_fail (file != NULL, NULL);
27 /* If the directory name doesn't have a / on the end, we need
28 to add one so we get a proper path to the file */
29 if (dir[0] != '\0' && dir [strlen(dir) - 1] != '/')
30 return g_strconcat (dir, "/", file, NULL);
32 return g_strconcat (dir, file, NULL);
35 static Bonobo_StorageInfo*
36 vfs_get_info (PortableServer_Servant storage,
37 const CORBA_char *path,
38 const Bonobo_StorageInfoFields mask,
39 CORBA_Environment *ev)
41 g_warning ("FIXME: get_info not yet implemented");
42 CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
43 ex_Bonobo_Storage_NotSupported,
46 return CORBA_OBJECT_NIL;
50 vfs_set_info (PortableServer_Servant storage,
51 const CORBA_char *path,
52 const Bonobo_StorageInfo *info,
53 const Bonobo_StorageInfoFields mask,
54 CORBA_Environment *ev)
56 g_warning ("FIXME: set_info not yet implemented");
57 CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
58 ex_Bonobo_Storage_NotSupported,
63 vfs_open_stream (PortableServer_Servant storage,
64 const CORBA_char *path,
65 Bonobo_Storage_OpenMode mode,
66 CORBA_Environment *ev)
68 BonoboStorageVfs *storage_vfs = BONOBO_STORAGE_VFS (
69 bonobo_object (storage));
70 BonoboStreamVfs *stream;
73 full = concat_dir_and_file (storage_vfs->path, path);
74 stream = bonobo_stream_vfs_open (full, mode, ev);
77 return CORBA_Object_duplicate (BONOBO_OBJREF (stream), NULL);
79 return CORBA_OBJECT_NIL;
83 * Creates the Gtk object and the corba server bound to it
85 static BonoboStorageVfs *
86 do_bonobo_storage_vfs_create (const char *path)
88 BonoboStorageVfs *storage_vfs;
90 storage_vfs = g_object_new (bonobo_storage_vfs_get_type (), NULL);
91 storage_vfs->path = g_strdup (path);
97 vfs_rename (PortableServer_Servant storage,
98 const CORBA_char *path,
99 const CORBA_char *new_path,
100 CORBA_Environment *ev)
102 g_warning ("Not yet implemented");
103 CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
104 ex_Bonobo_Stream_IOError, NULL);
108 vfs_commit (PortableServer_Servant storage,
109 CORBA_Environment *ev)
111 CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
112 ex_Bonobo_Stream_NotSupported, NULL);
116 vfs_revert (PortableServer_Servant storage,
117 CORBA_Environment *ev)
119 CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
120 ex_Bonobo_Stream_NotSupported, NULL);
123 static Bonobo_Storage_DirectoryList *
124 vfs_list_contents (PortableServer_Servant storage,
125 const CORBA_char *path,
126 Bonobo_StorageInfoFields mask,
127 CORBA_Environment *ev)
129 BonoboStorageVfs *storage_vfs;
130 Bonobo_Storage_DirectoryList *list = NULL;
131 GnomeVFSResult result;
132 GList *dir_list, *info;
136 storage_vfs = BONOBO_STORAGE_VFS (storage);
138 uri = concat_dir_and_file (storage_vfs->path, path);
140 result = gnome_vfs_directory_list_load (
142 (mask & Bonobo_FIELD_CONTENT_TYPE) ?
143 GNOME_VFS_FILE_INFO_GET_MIME_TYPE :
144 GNOME_VFS_FILE_INFO_DEFAULT);
146 if (result != GNOME_VFS_OK) {
147 CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
148 ex_Bonobo_Storage_NotFound, NULL);
153 len = g_list_length (dir_list);
154 list = Bonobo_Storage_DirectoryList__alloc ();
155 list->_buffer = CORBA_sequence_Bonobo_StorageInfo_allocbuf (len);
157 CORBA_sequence_set_release (list, TRUE);
160 for (info = dir_list; info; info = info->next) {
161 bonobo_stream_vfs_storageinfo_from_file_info (
162 &list->_buffer [i++], info->data);
163 gnome_vfs_file_info_unref (info->data);
166 g_list_free (dir_list);
173 * bonobo_storage_vfs_open:
174 * @path: path to existing directory that represents the storage
177 * Returns a BonoboStorage object that represents the storage at @path
180 bonobo_storage_vfs_open (const char *path,
181 Bonobo_Storage_OpenMode mode,
182 CORBA_Environment *ev)
184 GnomeVFSResult result;
185 GnomeVFSFileInfo *info;
186 gboolean create = FALSE;
188 g_return_val_if_fail (path != NULL, NULL);
190 info = gnome_vfs_file_info_new ();
191 result = gnome_vfs_get_file_info (
192 path, info, GNOME_VFS_FILE_INFO_DEFAULT);
194 if (result == GNOME_VFS_ERROR_NOT_FOUND &&
195 (mode & Bonobo_Storage_CREATE))
198 else if (mode & Bonobo_Storage_READ) {
199 if (result != GNOME_VFS_OK) {
200 CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
201 ex_Bonobo_Stream_NoPermission, NULL);
205 if ((info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_TYPE) &&
206 (info->type != GNOME_VFS_FILE_TYPE_DIRECTORY)) {
207 CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
208 ex_Bonobo_Stream_IOError, NULL);
212 } else if (mode & (Bonobo_Storage_WRITE)) {
213 if (result == GNOME_VFS_ERROR_NOT_FOUND)
216 if ((info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_TYPE) &&
217 (info->type != GNOME_VFS_FILE_TYPE_DIRECTORY)) {
218 CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
219 ex_Bonobo_Stream_IOError, NULL);
223 gnome_vfs_file_info_unref (info);
226 result = gnome_vfs_make_directory (
227 path, GNOME_VFS_PERM_USER_ALL |
228 GNOME_VFS_PERM_GROUP_ALL);
230 if (result != GNOME_VFS_OK) {
231 CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
232 ex_Bonobo_Stream_NoPermission, NULL);
237 return do_bonobo_storage_vfs_create (path);
240 static Bonobo_Storage
241 vfs_open_storage (PortableServer_Servant storage,
242 const CORBA_char *path,
243 Bonobo_Storage_OpenMode mode,
244 CORBA_Environment *ev)
246 BonoboStorageVfs *storage_vfs = BONOBO_STORAGE_VFS (
247 bonobo_object (storage));
248 BonoboStorageVfs *new_storage;
249 GnomeVFSResult result;
252 full = concat_dir_and_file (storage_vfs->path, path);
254 result = gnome_vfs_make_directory (full, GNOME_VFS_PERM_USER_ALL);
255 if (result == GNOME_VFS_OK ||
256 result == GNOME_VFS_ERROR_FILE_EXISTS)
257 new_storage = do_bonobo_storage_vfs_create (full);
260 CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
261 ex_Bonobo_Storage_NoPermission, NULL);
267 return CORBA_Object_duplicate (BONOBO_OBJREF (new_storage), NULL);
269 return CORBA_OBJECT_NIL;
273 vfs_erase (PortableServer_Servant storage,
274 const CORBA_char *path,
275 CORBA_Environment *ev)
277 BonoboStorageVfs *storage_vfs = BONOBO_STORAGE_VFS (
278 bonobo_object (storage));
279 GnomeVFSResult result;
282 full = concat_dir_and_file (storage_vfs->path, path);
284 result = gnome_vfs_unlink (full);
287 if (result != GNOME_VFS_OK)
288 CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
289 ex_Bonobo_Storage_NoPermission,
294 vfs_copy_to (PortableServer_Servant storage,
296 CORBA_Environment *ev)
298 BonoboStorageVfs *storage_vfs = BONOBO_STORAGE_VFS (
299 bonobo_object (storage));
301 bonobo_storage_copy_to (
302 BONOBO_OBJREF (storage_vfs), dest, ev);
306 bonobo_storage_vfs_finalize (GObject *object)
308 BonoboStorageVfs *storage_vfs = BONOBO_STORAGE_VFS (object);
310 g_free (storage_vfs->path);
311 storage_vfs->path = NULL;
313 G_OBJECT_CLASS (bonobo_storage_vfs_parent_class)->finalize (object);
317 bonobo_storage_vfs_class_init (BonoboStorageVfsClass *klass)
319 GObjectClass *object_class = (GObjectClass *) klass;
320 POA_Bonobo_Storage__epv *epv = &klass->epv;
322 bonobo_storage_vfs_parent_class =
323 g_type_class_peek_parent (klass);
325 epv->getInfo = vfs_get_info;
326 epv->setInfo = vfs_set_info;
327 epv->openStream = vfs_open_stream;
328 epv->openStorage = vfs_open_storage;
329 epv->copyTo = vfs_copy_to;
330 epv->rename = vfs_rename;
331 epv->commit = vfs_commit;
332 epv->revert = vfs_revert;
333 epv->listContents = vfs_list_contents;
334 epv->erase = vfs_erase;
336 object_class->finalize = bonobo_storage_vfs_finalize;
340 bonobo_storage_vfs_init (GObject *object)
345 BONOBO_TYPE_FUNC_FULL (BonoboStorageVfs,
347 bonobo_object_get_type (),