"Initial commit to Gerrit"
[profile/ivi/libgsf.git] / gsf-gnome / gsf-output-bonobo.c
1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * gsf-output-bonobo.c: bonobo based output
4  *
5  * Copyright (C) 2002-2003 Dom Lachowicz (cinamod@hotmail.com)
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of version 2.1 of the GNU Lesser General Public
9  * License as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
19  * USA
20  */
21
22 #include <gsf-config.h>
23 #include <gsf-gnome/gsf-output-bonobo.h>
24 #include <bonobo/bonobo-exception.h>
25 #include <bonobo/bonobo-persist-stream.h>
26 #include <gsf/gsf-output-impl.h>
27 #include <gsf/gsf-impl-utils.h>
28
29 struct _GsfOutputBonobo {
30         GsfOutput output;
31         Bonobo_Stream stream ;
32 };
33
34 typedef struct {
35         GsfOutputClass output_class;
36 } GsfOutputBonoboClass;
37
38 /**
39  * gsf_output_bonobo_new :
40  * @stream : non-%NULL bonobo stream
41  * @err    : optionally %NULL.
42  *
43  * Returns: a new file or %NULL.
44  **/
45 GsfOutput *
46 gsf_output_bonobo_new (Bonobo_Stream const stream, G_GNUC_UNUSED GError **err)
47 {
48         GsfOutputBonobo *res;
49
50         res = g_object_new (GSF_OUTPUT_BONOBO_TYPE, NULL);
51         if (G_UNLIKELY (NULL == res)) return NULL;
52
53         res->stream = stream;
54
55         return GSF_OUTPUT (res);
56 }
57
58 static gboolean
59 gsf_output_bonobo_close (GsfOutput *output)
60 {
61         GsfOutputBonobo *bonobo = GSF_OUTPUT_BONOBO (output);
62         gboolean res = FALSE;
63
64         if (bonobo->stream != NULL) {
65                 bonobo->stream = NULL;
66                 res = TRUE;
67         }
68
69         return res;
70 }
71
72 static gboolean
73 gsf_output_bonobo_seek (GsfOutput *output, gsf_off_t offset,
74                         GSeekType whence)
75 {
76         GsfOutputBonobo const *bonobo = GSF_OUTPUT_BONOBO (output);
77         Bonobo_Stream_SeekType bwhence = 0; /* make compiler shut up */
78         CORBA_long        pos;
79         CORBA_Environment ev;
80
81         g_return_val_if_fail (bonobo->stream != CORBA_OBJECT_NIL, 
82                 gsf_output_set_error (output, 0, "missing stream"));
83
84         switch (whence) {
85         case G_SEEK_SET : bwhence = Bonobo_Stream_SeekSet; break;
86         case G_SEEK_CUR : bwhence = Bonobo_Stream_SeekCur; break;
87         case G_SEEK_END : bwhence = Bonobo_Stream_SeekEnd; break;
88         default:
89                 break; /*checked in GsfOutput wrapper */
90         }
91
92         CORBA_exception_init (&ev);
93         pos = Bonobo_Stream_seek
94                 (bonobo->stream, offset, bwhence, &ev);
95         if (BONOBO_EX (&ev)) {
96                 gsf_output_set_error (output, 0,
97                         "%s", bonobo_exception_get_text (&ev));
98                 CORBA_exception_free (&ev);
99                 return FALSE;
100         }
101         return TRUE;
102 }
103
104 static gboolean
105 gsf_output_bonobo_write (GsfOutput *output,
106                          size_t num_bytes,
107                          guint8 const *buffer)
108 {
109         GsfOutputBonobo *bonobo = GSF_OUTPUT_BONOBO (output);
110         Bonobo_Stream_iobuf *bsobuf;
111         CORBA_Environment ev;
112
113         g_return_val_if_fail (bonobo != NULL, FALSE);
114         g_return_val_if_fail (bonobo->stream != NULL, FALSE);
115
116         bsobuf = Bonobo_Stream_iobuf__alloc ();
117         bsobuf->_buffer = (CORBA_octet*)buffer;
118         bsobuf->_length = num_bytes;
119
120         CORBA_exception_init (&ev);
121         Bonobo_Stream_write (bonobo->stream, bsobuf, &ev);
122         if (BONOBO_EX (&ev)) {
123                 g_warning ("%s", bonobo_exception_get_text (&ev));
124                 CORBA_exception_free (&ev);
125                 return FALSE;
126         }
127
128         return TRUE;
129 }
130
131 static void
132 gsf_output_bonobo_finalize (GObject *obj)
133 {
134         GObjectClass *parent_class;
135         GsfOutput *output = (GsfOutput *)obj;
136
137         gsf_output_bonobo_close (output);
138
139         parent_class = g_type_class_peek (GSF_OUTPUT_TYPE);
140         if (parent_class && parent_class->finalize)
141                 parent_class->finalize (obj);
142 }
143
144 static void
145 gsf_output_bonobo_init (GObject *obj)
146 {
147         GsfOutputBonobo *stream = GSF_OUTPUT_BONOBO (obj);
148
149         stream->stream = NULL;
150 }
151
152 static void
153 gsf_output_bonobo_class_init (GObjectClass *gobject_class)
154 {
155         GsfOutputClass *output_class = GSF_OUTPUT_CLASS (gobject_class);
156
157         gobject_class->finalize = gsf_output_bonobo_finalize;
158         output_class->Close     = gsf_output_bonobo_close;
159         output_class->Seek      = gsf_output_bonobo_seek;
160         output_class->Write     = gsf_output_bonobo_write;
161 }
162
163 GSF_CLASS (GsfOutputBonobo, gsf_output_bonobo,
164            gsf_output_bonobo_class_init, gsf_output_bonobo_init, GSF_OUTPUT_TYPE)