* Removed a duplicated save_state call
[modest] / src / widgets / modest-tny-stream-gtkhtml.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30
31 /* modest-tny-stream-gtkhtml.c */
32
33 #include "modest-tny-stream-gtkhtml.h"
34 #include <tny-stream.h>
35 #include <gtkhtml/gtkhtml-stream.h>
36 #include <gtkhtml/gtkhtml-search.h>
37
38 /* 'private'/'protected' functions */
39 static void  modest_tny_stream_gtkhtml_class_init   (ModestTnyStreamGtkhtmlClass *klass);
40 static void  modest_tny_stream_gtkhtml_init         (ModestTnyStreamGtkhtml *obj);
41 static void  modest_tny_stream_gtkhtml_finalize     (GObject *obj);
42
43 static void  modest_tny_stream_gtkhml_iface_init (gpointer g_iface, gpointer iface_data);
44
45 /* list my signals */
46 enum {
47         /* MY_SIGNAL_1, */
48         /* MY_SIGNAL_2, */
49         LAST_SIGNAL
50 };
51
52 typedef struct _ModestTnyStreamGtkhtmlPrivate ModestTnyStreamGtkhtmlPrivate;
53 struct _ModestTnyStreamGtkhtmlPrivate {
54         GtkHTMLStream *stream;
55 };
56 #define MODEST_TNY_STREAM_GTKHTML_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
57                                                        MODEST_TYPE_TNY_STREAM_GTKHTML, \
58                                                        ModestTnyStreamGtkhtmlPrivate))
59 /* globals */
60 static GObjectClass *parent_class = NULL;
61
62 /* uncomment the following if you have defined any signals */
63 /* static guint signals[LAST_SIGNAL] = {0}; */
64
65 GType
66 modest_tny_stream_gtkhtml_get_type (void)
67 {
68         static GType my_type = 0;
69         if (!my_type) {
70                 static const GTypeInfo my_info = {
71                         sizeof(ModestTnyStreamGtkhtmlClass),
72                         NULL,           /* base init */
73                         NULL,           /* base finalize */
74                         (GClassInitFunc) modest_tny_stream_gtkhtml_class_init,
75                         NULL,           /* class finalize */
76                         NULL,           /* class data */
77                         sizeof(ModestTnyStreamGtkhtml),
78                         1,              /* n_preallocs */
79                         (GInstanceInitFunc) modest_tny_stream_gtkhtml_init,
80                         NULL
81                 };
82
83                 static const GInterfaceInfo iface_info = {
84                         (GInterfaceInitFunc) modest_tny_stream_gtkhml_iface_init,
85                         NULL,         /* interface_finalize */
86                         NULL          /* interface_data */
87                 };
88
89                 my_type = g_type_register_static (G_TYPE_OBJECT,
90                                                   "ModestTnyStreamGtkhtml",
91                                                   &my_info, 0);
92
93                 g_type_add_interface_static (my_type, TNY_TYPE_STREAM,
94                                              &iface_info);
95
96         }
97         return my_type;
98 }
99
100 static void
101 modest_tny_stream_gtkhtml_class_init (ModestTnyStreamGtkhtmlClass *klass)
102 {
103         GObjectClass *gobject_class;
104         gobject_class = (GObjectClass*) klass;
105
106         parent_class            = g_type_class_peek_parent (klass);
107         gobject_class->finalize = modest_tny_stream_gtkhtml_finalize;
108
109         g_type_class_add_private (gobject_class, sizeof(ModestTnyStreamGtkhtmlPrivate));
110 }
111
112 static void
113 modest_tny_stream_gtkhtml_init (ModestTnyStreamGtkhtml *obj)
114 {
115         ModestTnyStreamGtkhtmlPrivate *priv;
116         priv = MODEST_TNY_STREAM_GTKHTML_GET_PRIVATE(obj);
117
118         priv->stream  = NULL;
119 }
120
121 static void
122 modest_tny_stream_gtkhtml_finalize (GObject *obj)
123 {
124         ModestTnyStreamGtkhtmlPrivate *priv;
125
126         priv = MODEST_TNY_STREAM_GTKHTML_GET_PRIVATE(obj);
127         priv->stream = NULL;
128 }
129
130 GObject*
131 modest_tny_stream_gtkhtml_new (GtkHTMLStream *stream)
132 {
133         GObject *obj;
134         ModestTnyStreamGtkhtmlPrivate *priv;
135         
136         obj  = G_OBJECT(g_object_new(MODEST_TYPE_TNY_STREAM_GTKHTML, NULL));
137         priv = MODEST_TNY_STREAM_GTKHTML_GET_PRIVATE(obj);
138
139         g_return_val_if_fail (stream, NULL);
140         
141         priv->stream = stream;
142
143         return obj;
144 }
145
146
147 /* the rest are interface functions */
148
149
150 static ssize_t
151 gtkhtml_read (TnyStream *self, char *buffer, size_t n)
152 {
153         return -1; /* we cannot read */
154 }
155
156
157 static ssize_t
158 gtkhtml_write (TnyStream *self, const char *buffer, size_t n)
159 {
160         ModestTnyStreamGtkhtmlPrivate *priv;
161         
162         priv = MODEST_TNY_STREAM_GTKHTML_GET_PRIVATE(self);
163
164         if (!priv->stream) {
165                 g_print ("modest: cannot write to closed stream\n");
166                 return 0;
167         }
168
169         if (n == 0 || !buffer)
170                 return 0;
171                 
172         gtk_html_stream_write (priv->stream, buffer, n);
173         return n; /* hmmm */
174 }
175
176         
177 static gint
178 gtkhtml_flush (TnyStream *self)
179 {
180         return 0;
181 }
182         
183
184 static gint
185 gtkhtml_close (TnyStream *self)
186 {
187         ModestTnyStreamGtkhtmlPrivate *priv;
188         g_return_val_if_fail (self, 0);
189         priv = MODEST_TNY_STREAM_GTKHTML_GET_PRIVATE(self);
190         
191         gtk_html_stream_close   (priv->stream, GTK_HTML_STREAM_OK);
192         priv->stream = NULL;
193
194         return 0;
195 }
196
197
198 static gboolean
199 gtkhtml_is_eos (TnyStream *self)
200 {
201         return TRUE;
202 }
203
204
205         
206 static gint
207 gtkhtml_reset (TnyStream *self)
208 {
209         return 0;
210 }
211
212         
213 static ssize_t
214 gtkhtml_write_to_stream (TnyStream *self, TnyStream *output)
215 {
216         return 0;
217 }
218
219
220 static void
221 modest_tny_stream_gtkhml_iface_init (gpointer g_iface, gpointer iface_data)
222 {
223         TnyStreamIface *klass;
224         
225         g_return_if_fail (g_iface);
226
227         klass = (TnyStreamIface*) g_iface;
228         
229         klass->read_func            = gtkhtml_read;
230         klass->write_func           = gtkhtml_write;
231         klass->flush_func           = gtkhtml_flush;
232         klass->close_func           = gtkhtml_close;
233         klass->is_eos_func          = gtkhtml_is_eos;
234         klass->reset_func           = gtkhtml_reset;
235         klass->write_to_stream_func = gtkhtml_write_to_stream;
236 }