1 /* Copyright (c) 2007, 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-text-to-html.h"
34 #include <tny-stream.h>
36 #include <modest-text-utils.h>
38 #define HTML_PREFIX "<html><head>" \
39 "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf8\">" \
42 #define HTML_SUFFIX "</body></html>"
45 /* 'private'/'protected' functions */
46 static void modest_stream_text_to_html_class_init (ModestStreamTextToHtmlClass *klass);
47 static void modest_stream_text_to_html_init (ModestStreamTextToHtml *obj);
48 static void modest_stream_text_to_html_finalize (GObject *obj);
50 static void modest_stream_text_to_html_iface_init (gpointer g_iface, gpointer iface_data);
51 static gboolean write_line (TnyStream *self, const gchar *str, gboolean convert_to_html);
54 typedef struct _ModestStreamTextToHtmlPrivate ModestStreamTextToHtmlPrivate;
55 struct _ModestStreamTextToHtmlPrivate {
56 TnyStream *out_stream;
58 gboolean written_prefix;
63 #define MODEST_STREAM_TEXT_TO_HTML_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
64 MODEST_TYPE_STREAM_TEXT_TO_HTML, \
65 ModestStreamTextToHtmlPrivate))
67 static GObjectClass *parent_class = NULL;
70 modest_stream_text_to_html_get_type (void)
72 static GType my_type = 0;
74 static const GTypeInfo my_info = {
75 sizeof(ModestStreamTextToHtmlClass),
77 NULL, /* base finalize */
78 (GClassInitFunc) modest_stream_text_to_html_class_init,
79 NULL, /* class finalize */
80 NULL, /* class data */
81 sizeof(ModestStreamTextToHtml),
83 (GInstanceInitFunc) modest_stream_text_to_html_init,
87 static const GInterfaceInfo iface_info = {
88 (GInterfaceInitFunc) modest_stream_text_to_html_iface_init,
89 NULL, /* interface_finalize */
90 NULL /* interface_data */
93 my_type = g_type_register_static (G_TYPE_OBJECT,
94 "ModestStreamTextToHtml",
97 g_type_add_interface_static (my_type, TNY_TYPE_STREAM,
105 modest_stream_text_to_html_class_init (ModestStreamTextToHtmlClass *klass)
107 GObjectClass *gobject_class;
108 gobject_class = (GObjectClass*) klass;
110 parent_class = g_type_class_peek_parent (klass);
111 gobject_class->finalize = modest_stream_text_to_html_finalize;
113 g_type_class_add_private (gobject_class, sizeof(ModestStreamTextToHtmlPrivate));
117 modest_stream_text_to_html_init (ModestStreamTextToHtml *obj)
119 ModestStreamTextToHtmlPrivate *priv;
120 priv = MODEST_STREAM_TEXT_TO_HTML_GET_PRIVATE(obj);
122 priv->out_stream = NULL;
123 priv->written_prefix = FALSE;
124 priv->line_buffer = NULL;
125 priv->linkify_limit = 0;
126 priv->full_limit = 0;
127 priv->total_output = 0;
128 modest_text_utils_hyperlinkify_begin ();
132 modest_stream_text_to_html_finalize (GObject *obj)
134 ModestStreamTextToHtmlPrivate *priv;
136 priv = MODEST_STREAM_TEXT_TO_HTML_GET_PRIVATE(obj);
137 if (priv->out_stream)
138 g_object_unref (priv->out_stream);
139 priv->out_stream = NULL;
140 if (priv->line_buffer != NULL) {
141 g_string_free (priv->line_buffer, TRUE);
143 modest_text_utils_hyperlinkify_end ();
147 modest_stream_text_to_html_new (TnyStream *out_stream)
150 ModestStreamTextToHtmlPrivate *priv;
152 obj = G_OBJECT(g_object_new(MODEST_TYPE_STREAM_TEXT_TO_HTML, NULL));
153 priv = MODEST_STREAM_TEXT_TO_HTML_GET_PRIVATE(obj);
155 g_return_val_if_fail (out_stream, NULL);
157 priv->out_stream = g_object_ref (out_stream);
163 modest_stream_text_to_html_set_linkify_limit (ModestStreamTextToHtml *self, gssize limit)
165 ModestStreamTextToHtmlPrivate *priv = MODEST_STREAM_TEXT_TO_HTML_GET_PRIVATE (self);
166 priv->linkify_limit = limit;
170 modest_stream_text_to_html_set_full_limit (ModestStreamTextToHtml *self, gssize limit)
172 ModestStreamTextToHtmlPrivate *priv = MODEST_STREAM_TEXT_TO_HTML_GET_PRIVATE (self);
173 priv->full_limit = limit;
176 /* the rest are interface functions */
180 text_to_html_read (TnyStream *self, char *buffer, size_t n)
182 return -1; /* we cannot read */
186 write_line (TnyStream *self, const gchar *str, gboolean convert_to_html)
188 ModestStreamTextToHtmlPrivate *priv = MODEST_STREAM_TEXT_TO_HTML_GET_PRIVATE (self);
191 gssize pending_bytes;
192 gboolean hyperlinkify = TRUE;
194 /* we only leave for full limit if we're converting to html, so that we
195 preserve the prefix and suffix */
196 if (convert_to_html && (priv->full_limit > 0) &&(priv->total_output > priv->full_limit))
198 if ((priv->linkify_limit > 0) && (priv->total_output > priv->linkify_limit))
199 hyperlinkify = FALSE;
200 if (convert_to_html) {
201 html_buffer = modest_text_utils_convert_to_html_body (str, -1, hyperlinkify);
203 html_buffer = (gchar *) str;
206 pending_bytes = strlen (html_buffer);
207 priv->total_output += pending_bytes;
208 offset = html_buffer;
210 while (pending_bytes > 0) {
211 gssize written_bytes = 0;
212 written_bytes = tny_stream_write (priv->out_stream, offset, pending_bytes);
213 if (written_bytes < 0) {
215 g_free (html_buffer);
218 offset += written_bytes;
219 pending_bytes -= written_bytes;
222 g_free (html_buffer);
228 text_to_html_write (TnyStream *self, const char *buffer, size_t n)
231 ModestStreamTextToHtmlPrivate *priv = MODEST_STREAM_TEXT_TO_HTML_GET_PRIVATE (self);
234 modest_text_utils_hyperlinkify_begin ();
235 if ((!priv->written_prefix) && (n > 0)) {
236 if (!write_line (self, HTML_PREFIX, FALSE)) {
237 modest_text_utils_hyperlinkify_end ();
240 priv->written_prefix = TRUE;
245 if (priv->line_buffer == NULL)
246 priv->line_buffer = g_string_new (NULL);
248 priv->line_buffer = g_string_append_c (priv->line_buffer, c);
250 if (tny_stream_flush (self) == -1) {
251 modest_text_utils_hyperlinkify_end ();
258 modest_text_utils_hyperlinkify_end ();
264 text_to_html_flush (TnyStream *self)
266 ModestStreamTextToHtmlPrivate *priv = MODEST_STREAM_TEXT_TO_HTML_GET_PRIVATE (self);
268 if (priv->line_buffer != NULL) {
269 if (!write_line (self, priv->line_buffer->str, TRUE))
271 g_string_free (priv->line_buffer, TRUE);
272 priv->line_buffer = NULL;
279 text_to_html_close (TnyStream *self)
281 ModestStreamTextToHtmlPrivate *priv;
282 g_return_val_if_fail (self, 0);
283 priv = MODEST_STREAM_TEXT_TO_HTML_GET_PRIVATE(self);
285 tny_stream_flush (self);
286 if (!write_line (self, HTML_SUFFIX, FALSE))
289 tny_stream_close (priv->out_stream);
291 priv->out_stream = NULL;
298 text_to_html_is_eos (TnyStream *self)
306 text_to_html_reset (TnyStream *self)
313 text_to_html_write_to_stream (TnyStream *self, TnyStream *output)
320 modest_stream_text_to_html_iface_init (gpointer g_iface, gpointer iface_data)
322 TnyStreamIface *klass;
324 g_return_if_fail (g_iface);
326 klass = (TnyStreamIface*) g_iface;
328 klass->read = text_to_html_read;
329 klass->write = text_to_html_write;
330 klass->flush = text_to_html_flush;
331 klass->close = text_to_html_close;
332 klass->is_eos = text_to_html_is_eos;
333 klass->reset = text_to_html_reset;
334 klass->write_to_stream = text_to_html_write_to_stream;