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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
/*
* e-data-capture.c
*
* This program 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; either
* version 2 of the License, or (at your option) version 3.
*
* This program 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 the program; if not, see <http://www.gnu.org/licenses/>
*
*/
/**
* SECTION: e-data-capture
* @include: e-util/e-util.h
* @short_description: Capture data from streams
*
* #EDataCapture is a #GConverter that captures data until the end of
* the input data is seen, then emits a #EDataCapture:finished signal
* with the captured data in a #GBytes instance.
*
* When used with #GConverterInputStream or #GConverterOutputStream,
* an #EDataCapture can discreetly capture the stream content for the
* purpose of caching.
**/
#include "e-data-capture.h"
#include <string.h>
#define E_DATA_CAPTURE_GET_PRIVATE(obj) \
(G_TYPE_INSTANCE_GET_PRIVATE \
((obj), E_TYPE_DATA_CAPTURE, EDataCapturePrivate))
typedef struct _SignalClosure SignalClosure;
struct _EDataCapturePrivate {
GMainContext *main_context;
GByteArray *byte_array;
GMutex byte_array_lock;
};
struct _SignalClosure {
GWeakRef data_capture;
GBytes *data;
};
enum {
PROP_0,
PROP_MAIN_CONTEXT
};
enum {
FINISHED,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL];
/* Forward Declarations */
static void e_data_capture_converter_init (GConverterIface *interface);
G_DEFINE_TYPE_WITH_CODE (
EDataCapture,
e_data_capture,
G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (
G_TYPE_CONVERTER,
e_data_capture_converter_init))
static void
signal_closure_free (SignalClosure *signal_closure)
{
g_weak_ref_set (&signal_closure->data_capture, NULL);
g_bytes_unref (signal_closure->data);
g_slice_free (SignalClosure, signal_closure);
}
static gboolean
data_capture_emit_finished_idle_cb (gpointer user_data)
{
SignalClosure *signal_closure = user_data;
EDataCapture *data_capture;
data_capture = g_weak_ref_get (&signal_closure->data_capture);
if (data_capture != NULL) {
g_signal_emit (
data_capture,
signals[FINISHED], 0,
signal_closure->data);
g_object_unref (data_capture);
}
return FALSE;
}
static void
data_capture_set_main_context (EDataCapture *data_capture,
GMainContext *main_context)
{
g_return_if_fail (data_capture->priv->main_context == NULL);
if (main_context != NULL)
g_main_context_ref (main_context);
else
main_context = g_main_context_ref_thread_default ();
data_capture->priv->main_context = main_context;
}
static void
data_capture_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
switch (property_id) {
case PROP_MAIN_CONTEXT:
data_capture_set_main_context (
E_DATA_CAPTURE (object),
g_value_get_boxed (value));
return;
}
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
static void
data_capture_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
switch (property_id) {
case PROP_MAIN_CONTEXT:
g_value_take_boxed (
value,
e_data_capture_ref_main_context (
E_DATA_CAPTURE (object)));
return;
}
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
static void
data_capture_finalize (GObject *object)
{
EDataCapturePrivate *priv;
priv = E_DATA_CAPTURE_GET_PRIVATE (object);
g_main_context_unref (priv->main_context);
g_byte_array_free (priv->byte_array, TRUE);
g_mutex_clear (&priv->byte_array_lock);
/* Chain up to parent's finalize() method. */
G_OBJECT_CLASS (e_data_capture_parent_class)->finalize (object);
}
static GConverterResult
data_capture_convert (GConverter *converter,
gconstpointer inbuf,
gsize inbuf_size,
gpointer outbuf,
gsize outbuf_size,
GConverterFlags flags,
gsize *bytes_read,
gsize *bytes_written,
GError **error)
{
EDataCapture *data_capture;
GConverterResult result;
data_capture = E_DATA_CAPTURE (converter);
/* Output buffer needs to be at least as large as the input buffer.
* The error message should never make it to the user interface so
* no need to translate. */
if (outbuf_size < inbuf_size) {
g_set_error_literal (
error, G_IO_ERROR,
G_IO_ERROR_NO_SPACE,
"EDataCapture needs more space");
return G_CONVERTER_ERROR;
}
memcpy (outbuf, inbuf, inbuf_size);
*bytes_read = *bytes_written = inbuf_size;
g_mutex_lock (&data_capture->priv->byte_array_lock);
g_byte_array_append (
data_capture->priv->byte_array, inbuf, inbuf_size);
if ((flags & G_CONVERTER_INPUT_AT_END) != 0) {
GSource *idle_source;
GMainContext *main_context;
SignalClosure *signal_closure;
signal_closure = g_slice_new0 (SignalClosure);
g_weak_ref_set (&signal_closure->data_capture, data_capture);
signal_closure->data = g_bytes_new (
data_capture->priv->byte_array->data,
data_capture->priv->byte_array->len);
main_context = e_data_capture_ref_main_context (data_capture);
idle_source = g_idle_source_new ();
g_source_set_callback (
idle_source,
data_capture_emit_finished_idle_cb,
signal_closure,
(GDestroyNotify) signal_closure_free);
g_source_set_priority (idle_source, G_PRIORITY_HIGH_IDLE);
g_source_attach (idle_source, main_context);
g_source_unref (idle_source);
g_main_context_unref (main_context);
}
g_mutex_unlock (&data_capture->priv->byte_array_lock);
if ((flags & G_CONVERTER_INPUT_AT_END) != 0)
result = G_CONVERTER_FINISHED;
else if ((flags & G_CONVERTER_FLUSH) != 0)
result = G_CONVERTER_FLUSHED;
else
result = G_CONVERTER_CONVERTED;
return result;
}
static void
data_capture_reset (GConverter *converter)
{
EDataCapture *data_capture;
data_capture = E_DATA_CAPTURE (converter);
g_mutex_lock (&data_capture->priv->byte_array_lock);
g_byte_array_set_size (data_capture->priv->byte_array, 0);
g_mutex_unlock (&data_capture->priv->byte_array_lock);
}
static void
e_data_capture_class_init (EDataCaptureClass *class)
{
GObjectClass *object_class;
g_type_class_add_private (class, sizeof (EDataCapturePrivate));
object_class = G_OBJECT_CLASS (class);
object_class->set_property = data_capture_set_property;
object_class->get_property = data_capture_get_property;
object_class->finalize = data_capture_finalize;
/**
* EDataCapture:main-context:
*
* The #GMainContext from which to emit the #EDataCapture::finished
* signal.
**/
g_object_class_install_property (
object_class,
PROP_MAIN_CONTEXT,
g_param_spec_boxed (
"main-context",
"Main Context",
"The main loop context from "
"which to emit the 'finished' signal",
G_TYPE_MAIN_CONTEXT,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
/**
* EDataCapture::finished:
* @data_capture: the #EDataCapture that received the signal
* @data: the captured data
*
* The ::finished signal is emitted when there is no more input
* data to be captured.
**/
signals[FINISHED] = g_signal_new (
"finished",
G_TYPE_FROM_CLASS (class),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (EDataCaptureClass, finished),
NULL, NULL, NULL,
G_TYPE_NONE, 1,
G_TYPE_BYTES);
}
static void
e_data_capture_converter_init (GConverterIface *interface)
{
interface->convert = data_capture_convert;
interface->reset = data_capture_reset;
}
static void
e_data_capture_init (EDataCapture *data_capture)
{
data_capture->priv = E_DATA_CAPTURE_GET_PRIVATE (data_capture);
data_capture->priv->byte_array = g_byte_array_new ();
g_mutex_init (&data_capture->priv->byte_array_lock);
}
/**
* e_data_capture_new:
* @main_context: a #GMainContext, or %NULL
*
* Creates a new #EDataCapture. If @main_context is %NULL, then the
* #EDataCapture:finished signal will be emitted from the thread-default
* #GMainContext for this thread.
*
* Returns: an #EDataCapture
**/
EDataCapture *
e_data_capture_new (GMainContext *main_context)
{
return g_object_new (
E_TYPE_DATA_CAPTURE,
"main-context", main_context, NULL);
}
/**
* e_data_capture_ref_main_context:
* @data_capture: an #EDataCapture
*
* Returns the #GMainContext from which the #EDataCapture:finished signal
* is emitted.
*
* The returned #GMainContext is referenced for thread-safety and must be
* unreferenced with g_main_context_unref() when finished with it.
*
* Returns: a #GMainContext
**/
GMainContext *
e_data_capture_ref_main_context (EDataCapture *data_capture)
{
g_return_val_if_fail (E_IS_DATA_CAPTURE (data_capture), NULL);
return g_main_context_ref (data_capture->priv->main_context);
}
|