* Fixes NB#91689. fixes a wrong check for ASCII
[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         GtkHTML *html;
56 };
57 #define MODEST_TNY_STREAM_GTKHTML_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
58                                                        MODEST_TYPE_TNY_STREAM_GTKHTML, \
59                                                        ModestTnyStreamGtkhtmlPrivate))
60 /* globals */
61 static GObjectClass *parent_class = NULL;
62
63 /* uncomment the following if you have defined any signals */
64 /* static guint signals[LAST_SIGNAL] = {0}; */
65
66 GType
67 modest_tny_stream_gtkhtml_get_type (void)
68 {
69         static GType my_type = 0;
70         if (!my_type) {
71                 static const GTypeInfo my_info = {
72                         sizeof(ModestTnyStreamGtkhtmlClass),
73                         NULL,           /* base init */
74                         NULL,           /* base finalize */
75                         (GClassInitFunc) modest_tny_stream_gtkhtml_class_init,
76                         NULL,           /* class finalize */
77                         NULL,           /* class data */
78                         sizeof(ModestTnyStreamGtkhtml),
79                         1,              /* n_preallocs */
80                         (GInstanceInitFunc) modest_tny_stream_gtkhtml_init,
81                         NULL
82                 };
83
84                 static const GInterfaceInfo iface_info = {
85                         (GInterfaceInitFunc) modest_tny_stream_gtkhml_iface_init,
86                         NULL,         /* interface_finalize */
87                         NULL          /* interface_data */
88                 };
89
90                 my_type = g_type_register_static (G_TYPE_OBJECT,
91                                                   "ModestTnyStreamGtkhtml",
92                                                   &my_info, 0);
93
94                 g_type_add_interface_static (my_type, TNY_TYPE_STREAM,
95                                              &iface_info);
96
97         }
98         return my_type;
99 }
100
101 static void
102 modest_tny_stream_gtkhtml_class_init (ModestTnyStreamGtkhtmlClass *klass)
103 {
104         GObjectClass *gobject_class;
105         gobject_class = (GObjectClass*) klass;
106
107         parent_class            = g_type_class_peek_parent (klass);
108         gobject_class->finalize = modest_tny_stream_gtkhtml_finalize;
109
110         g_type_class_add_private (gobject_class, sizeof(ModestTnyStreamGtkhtmlPrivate));
111 }
112
113 static void
114 modest_tny_stream_gtkhtml_init (ModestTnyStreamGtkhtml *obj)
115 {
116         ModestTnyStreamGtkhtmlPrivate *priv;
117         priv = MODEST_TNY_STREAM_GTKHTML_GET_PRIVATE(obj);
118
119         priv->stream  = NULL;
120         priv->html = NULL;
121 }
122
123 static void
124 modest_tny_stream_gtkhtml_finalize (GObject *obj)
125 {
126         ModestTnyStreamGtkhtmlPrivate *priv;
127
128         priv = MODEST_TNY_STREAM_GTKHTML_GET_PRIVATE(obj);
129         priv->stream = NULL;
130         if (priv->html) {
131                 g_object_unref (priv->html);
132                 priv->html = NULL;
133         }
134 }
135
136 GObject*
137 modest_tny_stream_gtkhtml_new (GtkHTMLStream *stream, GtkHTML *html)
138 {
139         GObject *obj;
140         ModestTnyStreamGtkhtmlPrivate *priv;
141         
142         obj  = G_OBJECT(g_object_new(MODEST_TYPE_TNY_STREAM_GTKHTML, NULL));
143         priv = MODEST_TNY_STREAM_GTKHTML_GET_PRIVATE(obj);
144
145         g_return_val_if_fail (stream, NULL);
146         
147         priv->stream = stream;
148         priv->html = g_object_ref (html);
149
150         return obj;
151 }
152
153
154 /* the rest are interface functions */
155
156
157 static ssize_t
158 gtkhtml_read (TnyStream *self, char *buffer, size_t n)
159 {
160         return -1; /* we cannot read */
161 }
162
163
164 static ssize_t
165 gtkhtml_write (TnyStream *self, const char *buffer, size_t n)
166 {
167         ModestTnyStreamGtkhtmlPrivate *priv;
168         
169         priv = MODEST_TNY_STREAM_GTKHTML_GET_PRIVATE(self);
170
171         if (!priv->stream) {
172                 g_print ("modest: cannot write to closed stream\n");
173                 return 0;
174         }
175
176         if (n == 0 || !buffer)
177                 return 0;
178
179         if (!GTK_WIDGET_VISIBLE (priv->html))
180                 return -1;
181
182         gtk_html_stream_write (priv->stream, buffer, n);
183         return n; /* hmmm */
184 }
185
186         
187 static gint
188 gtkhtml_flush (TnyStream *self)
189 {
190         return 0;
191 }
192         
193
194 static gint
195 gtkhtml_close (TnyStream *self)
196 {
197         ModestTnyStreamGtkhtmlPrivate *priv;
198         g_return_val_if_fail (self, 0);
199         priv = MODEST_TNY_STREAM_GTKHTML_GET_PRIVATE(self);
200         
201         if (GTK_WIDGET_VISIBLE (priv->html)) {
202                 gtk_html_stream_close   (priv->stream, GTK_HTML_STREAM_OK);
203         }
204         priv->stream = NULL;
205         if (priv->html) {
206                 g_object_unref (priv->html);
207                 priv->html = NULL;
208         }
209
210         return 0;
211 }
212
213
214 static gboolean
215 gtkhtml_is_eos (TnyStream *self)
216 {
217         return TRUE;
218 }
219
220
221         
222 static gint
223 gtkhtml_reset (TnyStream *self)
224 {
225         return 0;
226 }
227
228         
229 static ssize_t
230 gtkhtml_write_to_stream (TnyStream *self, TnyStream *output)
231 {
232         return 0;
233 }
234
235
236 static void
237 modest_tny_stream_gtkhml_iface_init (gpointer g_iface, gpointer iface_data)
238 {
239         TnyStreamIface *klass;
240         
241         g_return_if_fail (g_iface);
242
243         klass = (TnyStreamIface*) g_iface;
244         
245         klass->read            = gtkhtml_read;
246         klass->write           = gtkhtml_write;
247         klass->flush           = gtkhtml_flush;
248         klass->close           = gtkhtml_close;
249         klass->is_eos          = gtkhtml_is_eos;
250         klass->reset           = gtkhtml_reset;
251         klass->write_to_stream = gtkhtml_write_to_stream;
252 }