summaryrefslogtreecommitdiff
path: root/camel/camel-filter-output-stream.c
blob: 2496fbae30bbd7e92fcc830c1ec0a66ad5b31d86 (plain)
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
 * camel-filter-output-stream.c
 *
 * 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/>.
 *
 */

/**
 * SECTION: camel-filter-output-stream
 * @short_description: Filtered output stream
 * @include: camel/camel.h
 * @see_also: #GOutputStream, #CamelMimeFilter
 *
 * #CamelFilterOutputStream is similar to #GConverterOutputStream, except it
 * operates on a #CamelMimeFilter instead of a #GConverter.
 *
 * This class is meant to be a temporary solution until all of Camel's MIME
 * filters are ported to the #GConverter interface.
 **/

#include "camel-filter-output-stream.h"

#include <string.h>

#define CAMEL_FILTER_OUTPUT_STREAM_GET_PRIVATE(obj) \
	(G_TYPE_INSTANCE_GET_PRIVATE \
	((obj), CAMEL_TYPE_FILTER_OUTPUT_STREAM, CamelFilterOutputStreamPrivate))

#define READ_PAD (128)		/* bytes padded before buffer */
#define READ_SIZE (4096)

struct _CamelFilterOutputStreamPrivate {
	CamelMimeFilter *filter;
};

enum {
	PROP_0,
	PROP_FILTER
};

G_DEFINE_TYPE (
	CamelFilterOutputStream,
	camel_filter_output_stream,
	G_TYPE_FILTER_OUTPUT_STREAM)

static void
filter_output_stream_set_filter (CamelFilterOutputStream *filter_stream,
                                 CamelMimeFilter *filter)
{
	g_return_if_fail (CAMEL_IS_MIME_FILTER (filter));
	g_return_if_fail (filter_stream->priv->filter == NULL);

	filter_stream->priv->filter = g_object_ref (filter);
}

static void
filter_output_stream_set_property (GObject *object,
                                   guint property_id,
                                   const GValue *value,
                                   GParamSpec *pspec)
{
	switch (property_id) {
		case PROP_FILTER:
			filter_output_stream_set_filter (
				CAMEL_FILTER_OUTPUT_STREAM (object),
				g_value_get_object (value));
			return;
	}

	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
filter_output_stream_get_property (GObject *object,
                                   guint property_id,
                                   GValue *value,
                                   GParamSpec *pspec)
{
	switch (property_id) {
		case PROP_FILTER:
			g_value_set_object (
				value,
				camel_filter_output_stream_get_filter (
				CAMEL_FILTER_OUTPUT_STREAM (object)));
			return;
	}

	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
filter_output_stream_dispose (GObject *object)
{
	CamelFilterOutputStreamPrivate *priv;

	priv = CAMEL_FILTER_OUTPUT_STREAM_GET_PRIVATE (object);

	/* XXX GOutputStream calls flush() one last time during
	 *     dispose(), so chain up before clearing our filter. */
	G_OBJECT_CLASS (camel_filter_output_stream_parent_class)->
		dispose (object);

	g_clear_object (&priv->filter);
}

static gssize
filter_output_stream_write (GOutputStream *stream,
                            gconstpointer buffer,
                            gsize count,
                            GCancellable *cancellable,
                            GError **error)
{
	CamelMimeFilter *filter;
	GOutputStream *base_stream;
	gchar real_buffer[READ_SIZE + READ_PAD];
	const gchar *input_buffer = buffer;
	gsize bytes_left = count;

	filter = camel_filter_output_stream_get_filter (
		CAMEL_FILTER_OUTPUT_STREAM (stream));
	base_stream = g_filter_output_stream_get_base_stream (
		G_FILTER_OUTPUT_STREAM (stream));

	while (bytes_left > 0) {
		gsize length;
		gsize presize;
		gchar *bufptr;
		gboolean success;

		bufptr = real_buffer + READ_PAD;
		length = MIN (READ_SIZE, bytes_left);
		memcpy (bufptr, input_buffer, length);
		input_buffer += length;
		bytes_left -= length;

		presize = READ_PAD;

		camel_mime_filter_filter (
			filter, bufptr, length, presize,
			&bufptr, &length, &presize);

		/* XXX The bytes_written argument can be NULL,
		 *     even though the API docs don't say so. */
		success = g_output_stream_write_all (
			base_stream, bufptr, length,
			NULL, cancellable, error);
		if (!success)
			return -1;
	}

	return count;
}

static gboolean
filter_output_stream_flush (GOutputStream *stream,
                            GCancellable *cancellable,
                            GError **error)
{
	CamelMimeFilter *filter;
	GOutputStream *base_stream;
	gchar *bufptr = (gchar *) "";
	gsize length = 0;
	gsize presize = 0;
	gboolean success = TRUE;

	filter = camel_filter_output_stream_get_filter (
		CAMEL_FILTER_OUTPUT_STREAM (stream));
	base_stream = g_filter_output_stream_get_base_stream (
		G_FILTER_OUTPUT_STREAM (stream));

	camel_mime_filter_complete (
		filter, bufptr, length, presize,
		&bufptr, &length, &presize);

	if (length > 0) {
		/* XXX The bytes_written argument can be NULL,
		 *     even though the API docs don't say so. */
		success = g_output_stream_write_all (
			base_stream, bufptr, length,
			NULL, cancellable, error);
	}

	if (success) {
		success = g_output_stream_flush (
			base_stream, cancellable, error);
	}

	return success;
}

static void
camel_filter_output_stream_class_init (CamelFilterOutputStreamClass *class)
{
	GObjectClass *object_class;
	GOutputStreamClass *stream_class;

	g_type_class_add_private (
		class, sizeof (CamelFilterOutputStreamPrivate));

	object_class = G_OBJECT_CLASS (class);
	object_class->set_property = filter_output_stream_set_property;
	object_class->get_property = filter_output_stream_get_property;
	object_class->dispose = filter_output_stream_dispose;

	stream_class = G_OUTPUT_STREAM_CLASS (class);
	stream_class->write_fn = filter_output_stream_write;
	stream_class->flush = filter_output_stream_flush;

	g_object_class_install_property (
		object_class,
		PROP_FILTER,
		g_param_spec_object (
			"filter",
			"Filter",
			"The MIME filter object",
			CAMEL_TYPE_MIME_FILTER,
			G_PARAM_READWRITE |
			G_PARAM_CONSTRUCT_ONLY |
			G_PARAM_STATIC_STRINGS));
}

static void
camel_filter_output_stream_init (CamelFilterOutputStream *filter_stream)
{
	filter_stream->priv =
		CAMEL_FILTER_OUTPUT_STREAM_GET_PRIVATE (filter_stream);
}

/**
 * camel_filter_output_stream_new:
 * @base_stream: a #GOutputStream
 * @filter: a #CamelMimeFilter
 *
 * Creates a new filtered output stream for the @base_stream.
 *
 * Returns: a new #GOutputStream
 *
 * Since: 3.12
 **/
GOutputStream *
camel_filter_output_stream_new (GOutputStream *base_stream,
                                CamelMimeFilter *filter)
{
	g_return_val_if_fail (G_IS_OUTPUT_STREAM (base_stream), NULL);
	g_return_val_if_fail (CAMEL_IS_MIME_FILTER (filter), NULL);

	return g_object_new (
		CAMEL_TYPE_FILTER_OUTPUT_STREAM,
		"base-stream", base_stream,
		"filter", filter, NULL);
}

/**
 * camel_filter_output_stream_get_filter:
 * @filter_stream: a #CamelFilterOutputStream
 *
 * Gets the #CamelMimeFilter that is used by @filter_stream.
 *
 * Returns: a #CamelMimeFilter
 *
 * Since: 3.12
 **/
CamelMimeFilter *
camel_filter_output_stream_get_filter (CamelFilterOutputStream *filter_stream)
{
	g_return_val_if_fail (
		CAMEL_IS_FILTER_OUTPUT_STREAM (filter_stream), NULL);

	return filter_stream->priv->filter;
}