1 /* Copyright (c) 2009, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
31 /* modest-stream-text-to-html.c */
33 #include "modest-stream-html-to-text.h"
34 #include <tny-stream.h>
36 #include <modest-text-utils.h>
37 #include <gtkhtml/gtkhtml-stream.h>
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);
46 typedef struct _ModestStreamHtmlToTextPrivate ModestStreamHtmlToTextPrivate;
47 struct _ModestStreamHtmlToTextPrivate {
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))
56 static GObjectClass *parent_class = NULL;
59 modest_stream_html_to_text_get_type (void)
61 static GType my_type = 0;
63 static const GTypeInfo my_info = {
64 sizeof(ModestStreamHtmlToTextClass),
66 NULL, /* base finalize */
67 (GClassInitFunc) modest_stream_html_to_text_class_init,
68 NULL, /* class finalize */
69 NULL, /* class data */
70 sizeof(ModestStreamHtmlToText),
72 (GInstanceInitFunc) modest_stream_html_to_text_init,
76 static const GInterfaceInfo iface_info = {
77 (GInterfaceInitFunc) modest_stream_html_to_text_iface_init,
78 NULL, /* interface_finalize */
79 NULL /* interface_data */
82 my_type = g_type_register_static (G_TYPE_OBJECT,
83 "ModestStreamHtmlToText",
86 g_type_add_interface_static (my_type, TNY_TYPE_STREAM,
94 modest_stream_html_to_text_class_init (ModestStreamHtmlToTextClass *klass)
96 GObjectClass *gobject_class;
97 gobject_class = (GObjectClass*) klass;
99 parent_class = g_type_class_peek_parent (klass);
100 gobject_class->finalize = modest_stream_html_to_text_finalize;
102 g_type_class_add_private (gobject_class, sizeof(ModestStreamHtmlToTextPrivate));
106 modest_stream_html_to_text_init (ModestStreamHtmlToText *obj)
108 ModestStreamHtmlToTextPrivate *priv;
109 priv = MODEST_STREAM_HTML_TO_TEXT_GET_PRIVATE(obj);
117 modest_stream_html_to_text_finalize (GObject *obj)
119 ModestStreamHtmlToTextPrivate *priv;
121 priv = MODEST_STREAM_HTML_TO_TEXT_GET_PRIVATE(obj);
124 g_string_free (priv->buffer, TRUE);
128 export_to_txt_cb (const HTMLEngine * engine,
133 ModestStreamHtmlToTextPrivate *priv;
135 priv = MODEST_STREAM_HTML_TO_TEXT_GET_PRIVATE(user_data);
138 priv->buffer = g_string_new (data);
140 g_string_append (priv->buffer, data);
146 parse_input_stream (ModestStreamHtmlToText *self,
147 TnyStream *in_stream)
150 GtkHTMLStream *stream = NULL;
151 ModestStreamHtmlToTextPrivate *priv;
152 const guint BUFF_SIZE = 4096;
153 gchar buff[BUFF_SIZE];
155 priv = MODEST_STREAM_HTML_TO_TEXT_GET_PRIVATE(self);
157 buffer = g_string_new (NULL);
158 while (!tny_stream_is_eos (in_stream)) {
160 read = tny_stream_read (in_stream, buff, BUFF_SIZE);
161 buffer = g_string_append_len (buffer, buff, read);
163 tny_stream_reset (in_stream);
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);
171 return gtk_html_export (priv->html, "text/plain",
172 (GtkHTMLSaveReceiverFn) export_to_txt_cb, self);
176 modest_stream_html_to_text_new (TnyStream *in_stream)
180 obj = G_OBJECT(g_object_new(MODEST_TYPE_STREAM_HTML_TO_TEXT, NULL));
182 if (!parse_input_stream ((ModestStreamHtmlToText *) obj, in_stream)) {
183 g_warning ("%s: error parsing the input stream", __FUNCTION__);
184 g_object_unref (obj);
188 return (TnyStream *) obj;
191 /* the rest are interface functions */
193 html_to_text_read (TnyStream *self, char *buffer, size_t n)
195 ModestStreamHtmlToTextPrivate *priv;
198 priv = MODEST_STREAM_HTML_TO_TEXT_GET_PRIVATE (self);
200 for (i = 0; (i < n) && ((priv->position + i) < priv->buffer->len) ; i++)
201 buffer[i] = priv->buffer->str[priv->position + i];
209 html_to_text_write (TnyStream *self, const char *buffer, size_t n)
211 return -1; /* we cannot write */
215 html_to_text_flush (TnyStream *self)
217 /* ModestStreamHtmlToTextPrivate *priv = MODEST_STREAM_HTML_TO_TEXT_GET_PRIVATE (self); */
224 html_to_text_close (TnyStream *self)
226 ModestStreamHtmlToTextPrivate *priv;
228 priv = MODEST_STREAM_HTML_TO_TEXT_GET_PRIVATE(self);
230 tny_stream_flush (self);
237 html_to_text_is_eos (TnyStream *self)
239 ModestStreamHtmlToTextPrivate *priv;
241 priv = MODEST_STREAM_HTML_TO_TEXT_GET_PRIVATE(self);
243 return (priv->position >= (priv->buffer->len - 1));
249 html_to_text_reset (TnyStream *self)
251 ModestStreamHtmlToTextPrivate *priv;
253 priv = MODEST_STREAM_HTML_TO_TEXT_GET_PRIVATE(self);
256 return priv->position;
261 html_to_text_write_to_stream (TnyStream *self, TnyStream *output)
268 modest_stream_html_to_text_iface_init (gpointer g_iface, gpointer iface_data)
270 TnyStreamIface *klass;
272 g_return_if_fail (g_iface);
274 klass = (TnyStreamIface*) g_iface;
276 klass->read = html_to_text_read;
277 klass->write = html_to_text_write;
278 klass->flush = html_to_text_flush;
279 klass->close = html_to_text_close;
280 klass->is_eos = html_to_text_is_eos;
281 klass->reset = html_to_text_reset;
282 klass->write_to_stream = html_to_text_write_to_stream;