1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
---|
2 | /** |
---|
3 | * bonobo-stream.c: Stream manipulation, abstract class |
---|
4 | * |
---|
5 | * Author: |
---|
6 | * Miguel de Icaza (miguel@gnu.org). |
---|
7 | * |
---|
8 | * Copyright 1999, 2000 Helix Code, Inc. |
---|
9 | */ |
---|
10 | #include <config.h> |
---|
11 | |
---|
12 | #include <bonobo/bonobo-stream.h> |
---|
13 | #include <bonobo/bonobo-exception.h> |
---|
14 | #include <bonobo/bonobo-storage-plugin.h> |
---|
15 | |
---|
16 | static BonoboObjectClass *bonobo_stream_parent_class; |
---|
17 | |
---|
18 | POA_Bonobo_Stream__vepv bonobo_stream_vepv; |
---|
19 | |
---|
20 | #define CLASS(o) BONOBO_STREAM_CLASS(GTK_OBJECT(o)->klass) |
---|
21 | |
---|
22 | static inline BonoboStream * |
---|
23 | bonobo_stream_from_servant (PortableServer_Servant servant) |
---|
24 | { |
---|
25 | return BONOBO_STREAM (bonobo_object_from_servant (servant)); |
---|
26 | } |
---|
27 | |
---|
28 | static Bonobo_StorageInfo* |
---|
29 | impl_Bonobo_Stream_getInfo (PortableServer_Servant servant, |
---|
30 | const Bonobo_StorageInfoFields mask, |
---|
31 | CORBA_Environment *ev) |
---|
32 | { |
---|
33 | BonoboStream *stream = bonobo_stream_from_servant (servant); |
---|
34 | |
---|
35 | return CLASS (stream)->get_info (stream, mask, ev); |
---|
36 | } |
---|
37 | |
---|
38 | static void |
---|
39 | impl_Bonobo_Stream_setInfo (PortableServer_Servant servant, |
---|
40 | const Bonobo_StorageInfo *info, |
---|
41 | const Bonobo_StorageInfoFields mask, |
---|
42 | CORBA_Environment *ev) |
---|
43 | { |
---|
44 | BonoboStream *stream = bonobo_stream_from_servant (servant); |
---|
45 | |
---|
46 | CLASS (stream)->set_info (stream, info, mask, ev); |
---|
47 | } |
---|
48 | |
---|
49 | static void |
---|
50 | impl_Bonobo_Stream_read (PortableServer_Servant servant, |
---|
51 | CORBA_long count, |
---|
52 | Bonobo_Stream_iobuf **buffer, |
---|
53 | CORBA_Environment *ev) |
---|
54 | { |
---|
55 | BonoboStream *stream = bonobo_stream_from_servant (servant); |
---|
56 | |
---|
57 | CLASS (stream)->read (stream, count, buffer, ev); |
---|
58 | } |
---|
59 | |
---|
60 | static void |
---|
61 | impl_Bonobo_Stream_write (PortableServer_Servant servant, |
---|
62 | const Bonobo_Stream_iobuf *buffer, |
---|
63 | CORBA_Environment *ev) |
---|
64 | { |
---|
65 | BonoboStream *stream = bonobo_stream_from_servant (servant); |
---|
66 | |
---|
67 | CLASS (stream)->write (stream, buffer, ev); |
---|
68 | } |
---|
69 | |
---|
70 | static CORBA_long |
---|
71 | impl_Bonobo_Stream_seek (PortableServer_Servant servant, |
---|
72 | CORBA_long offset, |
---|
73 | Bonobo_Stream_SeekType whence, |
---|
74 | CORBA_Environment *ev) |
---|
75 | { |
---|
76 | BonoboStream *stream = bonobo_stream_from_servant (servant); |
---|
77 | |
---|
78 | return CLASS (stream)->seek (stream, offset, whence, ev); |
---|
79 | } |
---|
80 | |
---|
81 | static void |
---|
82 | impl_Bonobo_Stream_truncate (PortableServer_Servant servant, |
---|
83 | CORBA_long length, |
---|
84 | CORBA_Environment *ev) |
---|
85 | { |
---|
86 | BonoboStream *stream = bonobo_stream_from_servant (servant); |
---|
87 | |
---|
88 | CLASS (stream)->truncate (stream, length, ev); |
---|
89 | } |
---|
90 | |
---|
91 | static void |
---|
92 | impl_Bonobo_Stream_copyTo (PortableServer_Servant servant, |
---|
93 | const CORBA_char *dest, |
---|
94 | CORBA_long bytes, |
---|
95 | CORBA_long *read, |
---|
96 | CORBA_long *written, |
---|
97 | CORBA_Environment *ev) |
---|
98 | { |
---|
99 | BonoboStream *stream = bonobo_stream_from_servant (servant); |
---|
100 | |
---|
101 | CLASS (stream)->copy_to (stream, dest, bytes, read, written, ev); |
---|
102 | } |
---|
103 | |
---|
104 | static void |
---|
105 | impl_Bonobo_Stream_commit (PortableServer_Servant servant, |
---|
106 | CORBA_Environment * ev) |
---|
107 | { |
---|
108 | BonoboStream *stream = bonobo_stream_from_servant (servant); |
---|
109 | |
---|
110 | CLASS (stream)->commit (stream, ev); |
---|
111 | } |
---|
112 | |
---|
113 | static void |
---|
114 | impl_Bonobo_Stream_revert (PortableServer_Servant servant, |
---|
115 | CORBA_Environment * ev) |
---|
116 | { |
---|
117 | BonoboStream *stream = bonobo_stream_from_servant (servant); |
---|
118 | |
---|
119 | CLASS (stream)->revert (stream, ev); |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | * bonobo_stream_get_epv: |
---|
124 | */ |
---|
125 | POA_Bonobo_Stream__epv * |
---|
126 | bonobo_stream_get_epv (void) |
---|
127 | { |
---|
128 | POA_Bonobo_Stream__epv *epv; |
---|
129 | |
---|
130 | epv = g_new0 (POA_Bonobo_Stream__epv, 1); |
---|
131 | |
---|
132 | epv->getInfo = impl_Bonobo_Stream_getInfo; |
---|
133 | epv->setInfo = impl_Bonobo_Stream_setInfo; |
---|
134 | epv->read = impl_Bonobo_Stream_read; |
---|
135 | epv->write = impl_Bonobo_Stream_write; |
---|
136 | epv->seek = impl_Bonobo_Stream_seek; |
---|
137 | epv->truncate = impl_Bonobo_Stream_truncate; |
---|
138 | epv->copyTo = impl_Bonobo_Stream_copyTo; |
---|
139 | epv->commit = impl_Bonobo_Stream_commit; |
---|
140 | epv->revert = impl_Bonobo_Stream_revert; |
---|
141 | |
---|
142 | return epv; |
---|
143 | } |
---|
144 | |
---|
145 | static void |
---|
146 | init_stream_corba_class (void) |
---|
147 | { |
---|
148 | /* The VEPV */ |
---|
149 | bonobo_stream_vepv.Bonobo_Unknown_epv = bonobo_object_get_epv (); |
---|
150 | bonobo_stream_vepv.Bonobo_Stream_epv = bonobo_stream_get_epv (); |
---|
151 | } |
---|
152 | |
---|
153 | static void |
---|
154 | bonobo_stream_class_init (BonoboStreamClass *klass) |
---|
155 | { |
---|
156 | bonobo_stream_parent_class = gtk_type_class (bonobo_object_get_type ()); |
---|
157 | |
---|
158 | init_stream_corba_class (); |
---|
159 | } |
---|
160 | |
---|
161 | /** |
---|
162 | * bonobo_stream_get_type: |
---|
163 | * |
---|
164 | * Returns: the GtkType for a BonoboStream. |
---|
165 | */ |
---|
166 | GtkType |
---|
167 | bonobo_stream_get_type (void) |
---|
168 | { |
---|
169 | static GtkType type = 0; |
---|
170 | |
---|
171 | if (!type){ |
---|
172 | GtkTypeInfo info = { |
---|
173 | "BonoboStream", |
---|
174 | sizeof (BonoboStream), |
---|
175 | sizeof (BonoboStreamClass), |
---|
176 | (GtkClassInitFunc) bonobo_stream_class_init, |
---|
177 | (GtkObjectInitFunc) NULL, |
---|
178 | NULL, /* reserved 1 */ |
---|
179 | NULL, /* reserved 2 */ |
---|
180 | (GtkClassInitFunc) NULL |
---|
181 | }; |
---|
182 | |
---|
183 | type = gtk_type_unique (bonobo_object_get_type (), &info); |
---|
184 | } |
---|
185 | |
---|
186 | return type; |
---|
187 | } |
---|
188 | |
---|
189 | /** |
---|
190 | * bonobo_stream_open: |
---|
191 | * @driver: driver to use for opening. |
---|
192 | * @path: path where the base file resides |
---|
193 | * @flags: Bonobo Storage OpenMode |
---|
194 | * @mode: Unix open(2) mode |
---|
195 | * |
---|
196 | * Opens or creates the file named at @path with the stream driver @driver. |
---|
197 | * |
---|
198 | * @driver is one of: "fs" or "vfs" for now. |
---|
199 | * |
---|
200 | * Returns: a created BonoboStream object. |
---|
201 | */ |
---|
202 | BonoboStream * |
---|
203 | bonobo_stream_open_full (const char *driver, const char *path, gint flags, |
---|
204 | gint mode, CORBA_Environment *opt_ev) |
---|
205 | { |
---|
206 | BonoboStream *stream = NULL; |
---|
207 | StoragePlugin *p; |
---|
208 | CORBA_Environment ev, *my_ev; |
---|
209 | |
---|
210 | if (!opt_ev) { |
---|
211 | CORBA_exception_init (&ev); |
---|
212 | my_ev = &ev; |
---|
213 | } else |
---|
214 | my_ev = opt_ev; |
---|
215 | |
---|
216 | if (!driver || !path) |
---|
217 | CORBA_exception_set (my_ev, CORBA_USER_EXCEPTION, |
---|
218 | ex_Bonobo_Storage_IOError, NULL); |
---|
219 | else if (!(p = bonobo_storage_plugin_find (driver)) || |
---|
220 | !p->stream_open) |
---|
221 | CORBA_exception_set (my_ev, CORBA_USER_EXCEPTION, |
---|
222 | ex_Bonobo_Storage_NotSupported, NULL); |
---|
223 | else |
---|
224 | stream = p->stream_open (path, flags, mode, my_ev); |
---|
225 | |
---|
226 | if (!opt_ev) { |
---|
227 | if (BONOBO_EX (my_ev)) |
---|
228 | g_warning ("bonobo_stream_open failed '%s'", |
---|
229 | bonobo_exception_get_text (my_ev)); |
---|
230 | CORBA_exception_free (&ev); |
---|
231 | } |
---|
232 | |
---|
233 | return stream; |
---|
234 | } |
---|
235 | |
---|
236 | BonoboStream * |
---|
237 | bonobo_stream_open (const char *driver, const char *path, |
---|
238 | gint flags, gint mode) |
---|
239 | { |
---|
240 | return bonobo_stream_open_full (driver, path, flags, mode, NULL); |
---|
241 | } |
---|
242 | |
---|
243 | /** |
---|
244 | * bonobo_stream_corba_object_create: |
---|
245 | * @object: the GtkObject that will wrap the CORBA object |
---|
246 | * |
---|
247 | * Creates and activates the CORBA object that is wrapped by the |
---|
248 | * @object BonoboObject. |
---|
249 | * |
---|
250 | * Returns: An activated object reference to the created object |
---|
251 | * or %CORBA_OBJECT_NIL in case of failure. |
---|
252 | */ |
---|
253 | Bonobo_Stream |
---|
254 | bonobo_stream_corba_object_create (BonoboObject *object) |
---|
255 | { |
---|
256 | POA_Bonobo_Stream *servant; |
---|
257 | CORBA_Environment ev; |
---|
258 | |
---|
259 | servant = (POA_Bonobo_Stream *) g_new0 (BonoboObjectServant, 1); |
---|
260 | servant->vepv = &bonobo_stream_vepv; |
---|
261 | |
---|
262 | CORBA_exception_init (&ev); |
---|
263 | |
---|
264 | POA_Bonobo_Stream__init ((PortableServer_Servant) servant, &ev); |
---|
265 | if (BONOBO_EX (&ev)){ |
---|
266 | g_free (servant); |
---|
267 | CORBA_exception_free (&ev); |
---|
268 | return CORBA_OBJECT_NIL; |
---|
269 | } |
---|
270 | |
---|
271 | CORBA_exception_free (&ev); |
---|
272 | |
---|
273 | return bonobo_object_activate_servant (object, servant); |
---|
274 | } |
---|
275 | |
---|
276 | /** |
---|
277 | * bonobo_internal_get_major_mime_type: |
---|
278 | * |
---|
279 | * This is not the function you are looking for. In fact, you've never |
---|
280 | * even heard of this function. |
---|
281 | * But when you wake up, you will remember to g_free the result. |
---|
282 | */ |
---|
283 | gchar * |
---|
284 | bonobo_internal_get_major_mime_type (const char *mime_type) |
---|
285 | { |
---|
286 | char *major_end; |
---|
287 | char *major; |
---|
288 | int major_length; |
---|
289 | |
---|
290 | major_end = strchr (mime_type, '/'); |
---|
291 | if (!major_end) |
---|
292 | return g_strdup (mime_type); |
---|
293 | |
---|
294 | major_length = major_end - mime_type; |
---|
295 | major = g_strndup (mime_type, major_length); |
---|
296 | |
---|
297 | return major; |
---|
298 | } |
---|