File: camel-mime-filter-html.c

package info (click to toggle)
evolution-data-server 3.22.7-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 62,116 kB
  • ctags: 41,630
  • sloc: ansic: 302,318; makefile: 4,890; sh: 4,182; cpp: 472; xml: 462; perl: 368; python: 71
file content (210 lines) | stat: -rw-r--r-- 5,744 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 * This library is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors: Michael Zucchi <notzed@ximian.com>
 */

#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>

#include "camel-html-parser.h"
#include "camel-mime-filter-html.h"

#define d(x)

#define CAMEL_MIME_FILTER_HTML_GET_PRIVATE(obj) \
	(G_TYPE_INSTANCE_GET_PRIVATE \
	((obj), CAMEL_TYPE_MIME_FILTER_HTML, CamelMimeFilterHTMLPrivate))

struct _CamelMimeFilterHTMLPrivate {
	CamelHTMLParser *ctxt;
};

G_DEFINE_TYPE (CamelMimeFilterHTML, camel_mime_filter_html, CAMEL_TYPE_MIME_FILTER)

/* ********************************************************************** */

#if 0

/* well we odnt use this stuff yet */

static struct {
	gchar *element;
	gchar *remap;
} map_start[] = {
	{ "p", "\n\n" },
	{ "br", "\n" },
	{ "h1", "\n" }, { "h2", "\n" }, { "h3", "\n" }, { "h4", "\n" }, { "h5", "\n" }, { "h6", "\n" },
};

static struct {
	gchar *element;
	gchar *remap;
} map_end[] = {
	{ "h1", "\n" }, { "h2", "\n" }, { "h3", "\n" }, { "h4", "\n" }, { "h5", "\n" }, { "h6", "\n" },
};
#endif

/* ********************************************************************** */

static void
mime_filter_html_run (CamelMimeFilter *mime_filter,
                      const gchar *in,
                      gsize inlen,
                      gsize prespace,
                      gchar **out,
                      gsize *outlenptr,
                      gsize *outprespace,
                      gint last)
{
	CamelMimeFilterHTMLPrivate *priv;
	CamelHTMLParserState state;
	gchar *outp;

	priv = CAMEL_MIME_FILTER_HTML_GET_PRIVATE (mime_filter);

	d (printf ("converting html:\n%.*s\n", (gint) inlen, in));

	/* We should generally shrink the data, but this'll do */
	camel_mime_filter_set_size (mime_filter, inlen * 2 + 256, FALSE);
	outp = mime_filter->outbuf;

	camel_html_parser_set_data (priv->ctxt, in, inlen, last);
	do {
		const gchar *data;
		gint len;

		state = camel_html_parser_step (priv->ctxt, &data, &len);

		switch (state) {
		case CAMEL_HTML_PARSER_DATA:
		case CAMEL_HTML_PARSER_ENT:
			memcpy (outp, data, len);
			outp += len;
			break;
		case CAMEL_HTML_PARSER_ELEMENT:
			/* FIXME: do some whitespace processing here */
			break;
		default:
			/* ignore everything else */
			break;
		}
	} while (state != CAMEL_HTML_PARSER_EOF && state != CAMEL_HTML_PARSER_EOD);

	*out = mime_filter->outbuf;
	*outlenptr = outp - mime_filter->outbuf;
	*outprespace = mime_filter->outbuf - mime_filter->outreal;

	d (printf ("converted html end:\n%.*s\n", (gint) * outlenptr, *out));
}

static void
mime_filter_html_dispose (GObject *object)
{
	CamelMimeFilterHTMLPrivate *priv;

	priv = CAMEL_MIME_FILTER_HTML_GET_PRIVATE (object);

	if (priv->ctxt != NULL) {
		g_object_unref (priv->ctxt);
		priv->ctxt = NULL;
	}

	/* Chain up to parent's dispose() method. */
	G_OBJECT_CLASS (camel_mime_filter_html_parent_class)->dispose (object);
}

static void
mime_filter_html_filter (CamelMimeFilter *mime_filter,
                         const gchar *in,
                         gsize len,
                         gsize prespace,
                         gchar **out,
                         gsize *outlenptr,
                         gsize *outprespace)
{
	mime_filter_html_run (
		mime_filter, in, len, prespace,
		out, outlenptr, outprespace, FALSE);
}

static void
mime_filter_html_complete (CamelMimeFilter *mime_filter,
                           const gchar *in,
                           gsize len,
                           gsize prespace,
                           gchar **out,
                           gsize *outlenptr,
                           gsize *outprespace)
{
	mime_filter_html_run (
		mime_filter, in, len, prespace,
		out, outlenptr, outprespace, TRUE);
}

static void
mime_filter_html_reset (CamelMimeFilter *mime_filter)
{
	CamelMimeFilterHTMLPrivate *priv;

	priv = CAMEL_MIME_FILTER_HTML_GET_PRIVATE (mime_filter);

	g_object_unref (priv->ctxt);
	priv->ctxt = camel_html_parser_new ();
}

static void
camel_mime_filter_html_class_init (CamelMimeFilterHTMLClass *class)
{
	GObjectClass *object_class;
	CamelMimeFilterClass *mime_filter_class;

	g_type_class_add_private (class, sizeof (CamelMimeFilterHTMLPrivate));

	object_class = G_OBJECT_CLASS (class);
	object_class->dispose = mime_filter_html_dispose;

	mime_filter_class = CAMEL_MIME_FILTER_CLASS (class);
	mime_filter_class->filter = mime_filter_html_filter;
	mime_filter_class->complete = mime_filter_html_complete;
	mime_filter_class->reset = mime_filter_html_reset;
}

static void
camel_mime_filter_html_init (CamelMimeFilterHTML *mime_filter)
{
	mime_filter->priv = CAMEL_MIME_FILTER_HTML_GET_PRIVATE (mime_filter);
	mime_filter->priv->ctxt = camel_html_parser_new ();
}

/**
 * camel_mime_filter_html_new:
 *
 * Create a new #CamelMimeFilterHTML object.
 *
 * Returns: a new #CamelMimeFilterHTML object
 **/
CamelMimeFilter *
camel_mime_filter_html_new (void)
{
	return g_object_new (CAMEL_TYPE_MIME_FILTER_HTML, NULL);
}