build: Add an --enable-code-coverage configure option to enable gcov support
[platform/upstream/evolution-data-server.git] / camel / camel-mime-filter-html.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  *  Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
4  *
5  *  Authors: Michael Zucchi <notzed@ximian.com>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of version 2 of the GNU Lesser General Public
9  * License as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 #include <fcntl.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30
31 #include "camel-html-parser.h"
32 #include "camel-mime-filter-html.h"
33
34 #define d(x)
35
36 #define CAMEL_MIME_FILTER_HTML_GET_PRIVATE(obj) \
37         (G_TYPE_INSTANCE_GET_PRIVATE \
38         ((obj), CAMEL_TYPE_MIME_FILTER_HTML, CamelMimeFilterHTMLPrivate))
39
40 struct _CamelMimeFilterHTMLPrivate {
41         CamelHTMLParser *ctxt;
42 };
43
44 G_DEFINE_TYPE (CamelMimeFilterHTML, camel_mime_filter_html, CAMEL_TYPE_MIME_FILTER)
45
46 /* ********************************************************************** */
47
48 #if 0
49
50 /* well we odnt use this stuff yet */
51
52 static struct {
53         gchar *element;
54         gchar *remap;
55 } map_start[] = {
56         { "p", "\n\n" },
57         { "br", "\n" },
58         { "h1", "\n" }, { "h2", "\n" }, { "h3", "\n" }, { "h4", "\n" }, { "h5", "\n" }, { "h6", "\n" },
59 };
60
61 static struct {
62         gchar *element;
63         gchar *remap;
64 } map_end[] = {
65         { "h1", "\n" }, { "h2", "\n" }, { "h3", "\n" }, { "h4", "\n" }, { "h5", "\n" }, { "h6", "\n" },
66 };
67 #endif
68
69 /* ********************************************************************** */
70
71 static void
72 mime_filter_html_run (CamelMimeFilter *mime_filter,
73                       const gchar *in,
74                       gsize inlen,
75                       gsize prespace,
76                       gchar **out,
77                       gsize *outlenptr,
78                       gsize *outprespace,
79                       gint last)
80 {
81         CamelMimeFilterHTMLPrivate *priv;
82         camel_html_parser_t state;
83         gchar *outp;
84
85         priv = CAMEL_MIME_FILTER_HTML_GET_PRIVATE (mime_filter);
86
87         d(printf("converting html:\n%.*s\n", (gint)inlen, in));
88
89         /* We should generally shrink the data, but this'll do */
90         camel_mime_filter_set_size (mime_filter, inlen * 2 + 256, FALSE);
91         outp = mime_filter->outbuf;
92
93         camel_html_parser_set_data (priv->ctxt, in, inlen, last);
94         do {
95                 const gchar *data;
96                 gint len;
97
98                 state = camel_html_parser_step (priv->ctxt, &data, &len);
99
100                 switch (state) {
101                 case CAMEL_HTML_PARSER_DATA:
102                 case CAMEL_HTML_PARSER_ENT:
103                         memcpy (outp, data, len);
104                         outp += len;
105                         break;
106                 case CAMEL_HTML_PARSER_ELEMENT:
107                         /* FIXME: do some whitespace processing here */
108                         break;
109                 default:
110                         /* ignore everything else */
111                         break;
112                 }
113         } while (state != CAMEL_HTML_PARSER_EOF && state != CAMEL_HTML_PARSER_EOD);
114
115         *out = mime_filter->outbuf;
116         *outlenptr = outp - mime_filter->outbuf;
117         *outprespace = mime_filter->outbuf - mime_filter->outreal;
118
119         d(printf("converted html end:\n%.*s\n", (gint)*outlenptr, *out));
120 }
121
122 static void
123 mime_filter_html_dispose (GObject *object)
124 {
125         CamelMimeFilterHTMLPrivate *priv;
126
127         priv = CAMEL_MIME_FILTER_HTML_GET_PRIVATE (object);
128
129         if (priv->ctxt != NULL) {
130                 g_object_unref (priv->ctxt);
131                 priv->ctxt = NULL;
132         }
133
134         /* Chain up to parent's dispose() method. */
135         G_OBJECT_CLASS (camel_mime_filter_html_parent_class)->dispose (object);
136 }
137
138 static void
139 mime_filter_html_filter (CamelMimeFilter *mime_filter,
140                          const gchar *in,
141                          gsize len,
142                          gsize prespace,
143                          gchar **out,
144                          gsize *outlenptr,
145                          gsize *outprespace)
146 {
147         mime_filter_html_run (
148                 mime_filter, in, len, prespace,
149                 out, outlenptr, outprespace, FALSE);
150 }
151
152 static void
153 mime_filter_html_complete (CamelMimeFilter *mime_filter,
154                            const gchar *in,
155                            gsize len,
156                            gsize prespace,
157                            gchar **out,
158                            gsize *outlenptr,
159                            gsize *outprespace)
160 {
161         mime_filter_html_run (
162                 mime_filter, in, len, prespace,
163                 out, outlenptr, outprespace, TRUE);
164 }
165
166 static void
167 mime_filter_html_reset (CamelMimeFilter *mime_filter)
168 {
169         CamelMimeFilterHTMLPrivate *priv;
170
171         priv = CAMEL_MIME_FILTER_HTML_GET_PRIVATE (mime_filter);
172
173         g_object_unref (priv->ctxt);
174         priv->ctxt = camel_html_parser_new ();
175 }
176
177 static void
178 camel_mime_filter_html_class_init (CamelMimeFilterHTMLClass *class)
179 {
180         GObjectClass *object_class;
181         CamelMimeFilterClass *mime_filter_class;
182
183         g_type_class_add_private (class, sizeof (CamelMimeFilterHTMLPrivate));
184
185         object_class = G_OBJECT_CLASS (class);
186         object_class->dispose = mime_filter_html_dispose;
187
188         mime_filter_class = CAMEL_MIME_FILTER_CLASS (class);
189         mime_filter_class->filter = mime_filter_html_filter;
190         mime_filter_class->complete = mime_filter_html_complete;
191         mime_filter_class->reset = mime_filter_html_reset;
192 }
193
194 static void
195 camel_mime_filter_html_init (CamelMimeFilterHTML *mime_filter)
196 {
197         mime_filter->priv = CAMEL_MIME_FILTER_HTML_GET_PRIVATE (mime_filter);
198         mime_filter->priv->ctxt = camel_html_parser_new ();
199 }
200
201 /**
202  * camel_mime_filter_html_new:
203  *
204  * Create a new #CamelMimeFilterHTML object.
205  *
206  * Returns: a new #CamelMimeFilterHTML object
207  **/
208 CamelMimeFilter *
209 camel_mime_filter_html_new (void)
210 {
211         return g_object_new (CAMEL_TYPE_MIME_FILTER_HTML, NULL);
212 }