Fixed a crash in HTML -> text conversor when the body is empty
[modest] / src / modest-stream-html-to-text.c
1 /* Copyright (c) 2009, 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-stream-text-to-html.c */
32
33 #include "modest-stream-html-to-text.h"
34 #include <tny-stream.h>
35 #include <string.h>
36 #include <modest-text-utils.h>
37 #include <gtkhtml/gtkhtml-stream.h>
38
39
40 /* 'private'/'protected' functions */
41 static void  modest_stream_html_to_text_class_init   (ModestStreamHtmlToTextClass *klass);
42 static void  modest_stream_html_to_text_init         (ModestStreamHtmlToText *obj);
43 static void  modest_stream_html_to_text_finalize     (GObject *obj);
44 static void  modest_stream_html_to_text_iface_init   (gpointer g_iface, gpointer iface_data);
45
46 typedef struct _ModestStreamHtmlToTextPrivate ModestStreamHtmlToTextPrivate;
47 struct _ModestStreamHtmlToTextPrivate {
48         GString *buffer;
49         gint position;
50         GtkHTML *html;
51 };
52 #define MODEST_STREAM_HTML_TO_TEXT_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
53                                                        MODEST_TYPE_STREAM_HTML_TO_TEXT, \
54                                                        ModestStreamHtmlToTextPrivate))
55 /* globals */
56 static GObjectClass *parent_class = NULL;
57
58 GType
59 modest_stream_html_to_text_get_type (void)
60 {
61         static GType my_type = 0;
62         if (!my_type) {
63                 static const GTypeInfo my_info = {
64                         sizeof(ModestStreamHtmlToTextClass),
65                         NULL,           /* base init */
66                         NULL,           /* base finalize */
67                         (GClassInitFunc) modest_stream_html_to_text_class_init,
68                         NULL,           /* class finalize */
69                         NULL,           /* class data */
70                         sizeof(ModestStreamHtmlToText),
71                         1,              /* n_preallocs */
72                         (GInstanceInitFunc) modest_stream_html_to_text_init,
73                         NULL
74                 };
75
76                 static const GInterfaceInfo iface_info = {
77                         (GInterfaceInitFunc) modest_stream_html_to_text_iface_init,
78                         NULL,         /* interface_finalize */
79                         NULL          /* interface_data */
80                 };
81
82                 my_type = g_type_register_static (G_TYPE_OBJECT,
83                                                   "ModestStreamHtmlToText",
84                                                   &my_info, 0);
85
86                 g_type_add_interface_static (my_type, TNY_TYPE_STREAM,
87                                              &iface_info);
88
89         }
90         return my_type;
91 }
92
93 static void
94 modest_stream_html_to_text_class_init (ModestStreamHtmlToTextClass *klass)
95 {
96         GObjectClass *gobject_class;
97         gobject_class = (GObjectClass*) klass;
98
99         parent_class            = g_type_class_peek_parent (klass);
100         gobject_class->finalize = modest_stream_html_to_text_finalize;
101
102         g_type_class_add_private (gobject_class, sizeof(ModestStreamHtmlToTextPrivate));
103 }
104
105 static void
106 modest_stream_html_to_text_init (ModestStreamHtmlToText *obj)
107 {
108         ModestStreamHtmlToTextPrivate *priv;
109         priv = MODEST_STREAM_HTML_TO_TEXT_GET_PRIVATE(obj);
110
111         priv->position = 0;
112         priv->buffer = NULL;
113         priv->html = NULL;
114 }
115
116 static void
117 modest_stream_html_to_text_finalize (GObject *obj)
118 {
119         ModestStreamHtmlToTextPrivate *priv;
120
121         priv = MODEST_STREAM_HTML_TO_TEXT_GET_PRIVATE(obj);
122
123         if (priv->buffer)
124                 g_string_free (priv->buffer, TRUE);
125 }
126
127 static gboolean
128 export_to_txt_cb (const HTMLEngine * engine,
129                   const char *data,
130                   unsigned int len,
131                   void *user_data)
132 {
133         ModestStreamHtmlToTextPrivate *priv;
134
135         priv = MODEST_STREAM_HTML_TO_TEXT_GET_PRIVATE(user_data);
136
137         if (!priv->buffer)
138                 priv->buffer = g_string_new (data);
139         else
140                 g_string_append (priv->buffer, data);
141
142         return TRUE;
143 }
144
145 static gboolean
146 parse_input_stream (ModestStreamHtmlToText *self,
147                     TnyStream *in_stream)
148 {
149         GString *buffer;
150         GtkHTMLStream *stream = NULL;
151         ModestStreamHtmlToTextPrivate *priv;
152         const guint BUFF_SIZE = 4096;
153         gchar buff[BUFF_SIZE];
154
155         priv = MODEST_STREAM_HTML_TO_TEXT_GET_PRIVATE(self);
156
157         buffer = g_string_new (NULL);
158         while (!tny_stream_is_eos (in_stream)) {
159                 gint read;
160                 read = tny_stream_read (in_stream, buff, BUFF_SIZE);
161                 buffer = g_string_append_len (buffer, buff, read);
162         }
163         tny_stream_reset (in_stream);
164
165         priv->html = g_object_new (GTK_TYPE_HTML, "visible", FALSE, NULL);
166         gtk_html_set_default_engine (priv->html, TRUE);
167         stream = gtk_html_begin_full(priv->html, NULL, "text/html", 0);
168         gtk_html_write(priv->html, stream, buffer->str, buffer->len);
169         gtk_html_end(priv->html, stream, 0);
170
171         return gtk_html_export (priv->html, "text/plain",
172                                 (GtkHTMLSaveReceiverFn) export_to_txt_cb, self);
173 }
174
175 TnyStream *
176 modest_stream_html_to_text_new (TnyStream *in_stream)
177 {
178         GObject *obj;
179
180         obj  = G_OBJECT(g_object_new(MODEST_TYPE_STREAM_HTML_TO_TEXT, NULL));
181
182         if (!parse_input_stream ((ModestStreamHtmlToText *) obj, in_stream)) {
183                 g_warning ("%s: error parsing the input stream", __FUNCTION__);
184                 g_object_unref (obj);
185                 obj = NULL;
186         }
187
188         return (TnyStream *) obj;
189 }
190
191 /* the rest are interface functions */
192 static ssize_t
193 html_to_text_read (TnyStream *self, char *buffer, size_t n)
194 {
195         ModestStreamHtmlToTextPrivate *priv;
196         gint i = 0;
197
198         priv = MODEST_STREAM_HTML_TO_TEXT_GET_PRIVATE (self);
199
200         if (priv->buffer) {
201                 for (i = 0; (i < n) && ((priv->position + i) < priv->buffer->len) ; i++)
202                         buffer[i] = priv->buffer->str[priv->position + i];
203         }
204
205         priv->position += i;
206
207         return i;
208 }
209
210 static ssize_t
211 html_to_text_write (TnyStream *self, const char *buffer, size_t n)
212 {
213         return -1;  /* we cannot write */
214 }
215
216 static gint
217 html_to_text_flush (TnyStream *self)
218 {
219         /* ModestStreamHtmlToTextPrivate *priv = MODEST_STREAM_HTML_TO_TEXT_GET_PRIVATE (self); */
220
221         return 0;
222 }
223
224
225 static gint
226 html_to_text_close (TnyStream *self)
227 {
228         ModestStreamHtmlToTextPrivate *priv;
229
230         priv = MODEST_STREAM_HTML_TO_TEXT_GET_PRIVATE(self);
231
232         tny_stream_flush (self);
233
234         return 0;
235 }
236
237
238 static gboolean
239 html_to_text_is_eos (TnyStream *self)
240 {
241         ModestStreamHtmlToTextPrivate *priv;
242
243         priv = MODEST_STREAM_HTML_TO_TEXT_GET_PRIVATE(self);
244
245         /* This could happen if the body is empty */
246         if (!priv->buffer)
247                 return TRUE;
248         else
249                 return (priv->position >= (priv->buffer->len - 1));
250 }
251
252
253
254 static gint
255 html_to_text_reset (TnyStream *self)
256 {
257         ModestStreamHtmlToTextPrivate *priv;
258
259         priv = MODEST_STREAM_HTML_TO_TEXT_GET_PRIVATE(self);
260         priv->position = 0;
261
262         return priv->position;
263 }
264
265
266 static ssize_t
267 html_to_text_write_to_stream (TnyStream *self, TnyStream *output)
268 {
269         return 0;
270 }
271
272
273 static void
274 modest_stream_html_to_text_iface_init (gpointer g_iface, gpointer iface_data)
275 {
276         TnyStreamIface *klass;
277
278         g_return_if_fail (g_iface);
279
280         klass = (TnyStreamIface*) g_iface;
281
282         klass->read            = html_to_text_read;
283         klass->write           = html_to_text_write;
284         klass->flush           = html_to_text_flush;
285         klass->close           = html_to_text_close;
286         klass->is_eos          = html_to_text_is_eos;
287         klass->reset           = html_to_text_reset;
288         klass->write_to_stream = html_to_text_write_to_stream;
289 }