aboutsummaryrefslogtreecommitdiffstats
path: root/mail/em-sync-stream.c
blob: 1224e468bd6b327641c9fd074376d5f9ec105288 (plain) (blame)
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  Authors: Jeffrey Stedfast <fejj@ximian.com>
 *       Michael Zucchi <notzed@ximian.com>
 *
 *  Copyright 2001 Ximian, Inc. (www.ximian.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>
#include <stdio.h>
#include <camel/camel-stream.h>
#include <camel/camel-object.h>
#include <gtk/gtkmain.h>
#include "em-sync-stream.h"

#include "mail-mt.h"

#define LOG_STREAM

#define d(x)

#define EMSS_CLASS(x) ((EMSyncStreamClass *)(((CamelObject *)(x))->klass))

struct _EMSyncStreamPrivate {
    /* FIXME: use a single data port/gui channel for all instances */
    /* TODO: possibly just use one of the mail-mt ports ... */
    struct _EMsgPort *data_port, *reply_port;
    struct _GIOChannel *gui_channel;
    guint gui_watch;

    char *buf_data;
    int buf_used;
    int buf_size;

#ifdef LOG_STREAM
    FILE *logfd;
#endif
};

#ifdef LOG_STREAM
static int dolog;
#endif

/* Should probably expose messages to outside world ... so subclasses can extend */
enum _write_msg_t {
    EMSS_WRITE,
    EMSS_FLUSH,
    EMSS_CLOSE,
};

struct _write_msg {
    EMsg msg;

    enum _write_msg_t op;

    const char *data;
    size_t n;
};

static void em_sync_stream_class_init (EMSyncStreamClass *klass);
static void em_sync_stream_init (CamelObject *object);
static void em_sync_stream_finalize (CamelObject *object);

static ssize_t stream_write(CamelStream *stream, const char *buffer, size_t n);
static int stream_close(CamelStream *stream);
static int stream_flush(CamelStream *stream);

static CamelStreamClass *parent_class = NULL;

CamelType
em_sync_stream_get_type (void)
{
    static CamelType type = CAMEL_INVALID_TYPE;

    if (type == CAMEL_INVALID_TYPE) {
#ifdef LOG_STREAM
        dolog = getenv("EVOLUTION_MAIL_LOG_HTML") != NULL;
#endif
        type = camel_type_register (CAMEL_STREAM_TYPE,
                        "EMSyncStream",
                        sizeof (EMSyncStream),
                        sizeof (EMSyncStreamClass),
                        (CamelObjectClassInitFunc) em_sync_stream_class_init,
                        NULL,
                        (CamelObjectInitFunc) em_sync_stream_init,
                        (CamelObjectFinalizeFunc) em_sync_stream_finalize);
    }

    return type;
}

static void
em_sync_stream_class_init (EMSyncStreamClass *klass)
{
    CamelStreamClass *stream_class = CAMEL_STREAM_CLASS (klass);

    parent_class = (CamelStreamClass *) CAMEL_STREAM_TYPE;

    /* virtual method overload */
    stream_class->write = stream_write;
    stream_class->flush = stream_flush;
    stream_class->close = stream_close;
}

static gboolean
emcs_gui_received(GIOChannel *source, GIOCondition cond, void *data)
{
    EMSyncStream *emss = data;
    struct _EMSyncStreamPrivate *p = emss->priv;
    struct _write_msg *msg;

    d(printf("%p: gui sync op job waiting\n", emss));

    msg = (struct _write_msg *)e_msgport_get(p->data_port);
    /* Should never happen ... */
    if (msg == NULL)
        return TRUE;

    d(printf("%p: running sync op %d\n", emss, msg->op));

    /* force out any pending data before doing anything else */
    if (p->buf_used > 0) {
        EMSS_CLASS(emss)->sync_write((CamelStream *)emss, p->buf_data, p->buf_used);
#ifdef LOG_STREAM
        if (p->logfd)
            fwrite(p->buf_data, 1, p->buf_used, p->logfd);
#endif
        p->buf_used = 0;
    }

    /* FIXME: need to handle return values */

    switch (msg->op) {
    case EMSS_WRITE:
        EMSS_CLASS(emss)->sync_write((CamelStream *)emss, msg->data, msg->n);
#ifdef LOG_STREAM
        if (p->logfd)
            fwrite(msg->data, 1, msg->n, p->logfd);
#endif
        break;
    case EMSS_FLUSH:
        EMSS_CLASS(emss)->sync_flush((CamelStream *)emss);
        break;
    case EMSS_CLOSE:
        EMSS_CLASS(emss)->sync_close((CamelStream *)emss);
#ifdef LOG_STREAM
        if (p->logfd) {
            fclose(p->logfd);
            p->logfd = NULL;
        }
#endif
        break;
    }

    e_msgport_reply((EMsg *)msg);
    d(printf("%p: gui sync op jobs done\n", emss));

    return TRUE;
}

static void
em_sync_stream_init (CamelObject *object)
{
    EMSyncStream *emss = (EMSyncStream *)object;
    struct _EMSyncStreamPrivate *p;

    p = emss->priv = g_malloc0(sizeof(*p));

    p->data_port = e_msgport_new();
    p->reply_port = e_msgport_new();

#ifndef G_OS_WIN32
    p->gui_channel = g_io_channel_unix_new(e_msgport_fd(p->data_port));
#else
    p->gui_channel = g_io_channel_win32_new_socket(e_msgport_fd(p->data_port));
#endif
    p->gui_watch = g_io_add_watch(p->gui_channel, G_IO_IN, emcs_gui_received, emss);

#ifdef LOG_STREAM
    if (dolog) {
        char name[32];
        static int count;

        sprintf(name, "sync-stream.%d.html", count++);
        printf("Saving raw data stream to '%s'\n", name);
        p->logfd = fopen(name, "w");
    }
#endif

    d(printf("%p: new emss\n", emss));
}

static void
sync_op(EMSyncStream *emss, enum _write_msg_t op, const char *data, size_t n)
{
    struct _EMSyncStreamPrivate *p = emss->priv;
    struct _write_msg msg;
    EMsg *reply_msg;

    d(printf("%p: launching sync op %d\n", emss, op));

    /* we do everything synchronous, we should never have any locks, and
       this prevents overflow from banked up data */

    msg.msg.reply_port = p->reply_port;
    msg.op = op;
    msg.data = data;
    msg.n = n;

    e_msgport_put(p->data_port, &msg.msg);
    reply_msg = e_msgport_wait(p->reply_port);
    g_return_if_fail (reply_msg == &msg.msg);

    d(printf("%p: returned sync op %d\n", emss, op));
}

static void
em_sync_stream_finalize (CamelObject *object)
{
    EMSyncStream *emss = (EMSyncStream *)object;
    struct _EMSyncStreamPrivate *p = emss->priv;

    /* TODO: is this stuff safe to do in another thread? */
    g_source_remove(p->gui_watch);
    g_io_channel_unref(p->gui_channel);

    e_msgport_destroy(p->data_port);
    e_msgport_destroy(p->reply_port);

    p->data_port = NULL;
    p->reply_port = NULL;

    g_free(p->buf_data);

#ifdef LOG_STREAM
    if (p->logfd)
        fclose(p->logfd);
#endif

    g_free(p);
}

static ssize_t
stream_write (CamelStream *stream, const char *buffer, size_t n)
{
    EMSyncStream *emss = EM_SYNC_STREAM (stream);
    struct _EMSyncStreamPrivate *p = emss->priv;

    if (emss->cancel)
        return -1;

    if (pthread_equal(pthread_self(), mail_gui_thread)) {
        EMSS_CLASS(emss)->sync_write(stream, buffer, n);
#ifdef LOG_STREAM
        if (p->logfd)
            fwrite(buffer, 1, n, p->logfd);
#endif
    } else if (p->buf_size > 0) {
        size_t left = p->buf_size-p->buf_used;

        if (n >= left) {
            sync_op(emss, EMSS_WRITE, buffer, n);
        } else {
            memcpy(p->buf_data + p->buf_used, buffer, n);
            p->buf_used += n;
        }
    } else {
        sync_op(emss, EMSS_WRITE, buffer, n);
    }

    return (ssize_t) n;
}

static int
stream_flush(CamelStream *stream)
{
    EMSyncStream *emss = (EMSyncStream *)stream;

    if (emss->cancel)
        return -1;

    if (pthread_equal(pthread_self(), mail_gui_thread))
        return ((EMSyncStreamClass *)(((CamelObject *)emss)->klass))->sync_flush(stream);
    else
        sync_op(emss, EMSS_FLUSH, NULL, 0);

    return 0;
}

static int
stream_close(CamelStream *stream)
{
    EMSyncStream *emss = (EMSyncStream *)stream;

    if (emss->cancel)
        return -1;

    d(printf("%p: closing stream\n", stream));

    if (pthread_equal(pthread_self(), mail_gui_thread)) {
#ifdef LOG_STREAM
        if (emss->priv->logfd) {
            fclose(emss->priv->logfd);
            emss->priv->logfd = NULL;
        }
#endif
        return ((EMSyncStreamClass *)(((CamelObject *)emss)->klass))->sync_close(stream);
    } else
        sync_op(emss, EMSS_CLOSE, NULL, 0);

    return 0;
}

void
em_sync_stream_set_buffer_size(EMSyncStream *emss, size_t size)
{
    struct _EMSyncStreamPrivate *p = emss->priv;

    g_free(p->buf_data);
    p->buf_data = g_malloc(size);
    p->buf_size = size;
    p->buf_used = 0;
}