aboutsummaryrefslogtreecommitdiffstats
path: root/mail/em-format-quote.c
blob: 113d854fc52cd45aba2f03671819fea2a49add8f (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  Authors: Jeffrey Stedfast <fejj@ximian.com>
 *
 *  Copyright 2003 Ximian, Inc. (www.ximian.com)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 *  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., 59 Temple Street #330, Boston, MA 02111-1307, USA.
 *
 */

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

#include <string.h>

#include <camel/camel-stream.h>
#include <camel/camel-stream-filter.h>
#include <camel/camel-mime-filter-tohtml.h>
#include <camel/camel-mime-filter-enriched.h>
#include "em-format-quote.h"

struct _EMFormatQuotePrivate {
    int dummy;
};

static void emfq_format_clone(EMFormat *, CamelMedium *, EMFormat *);
static void emfq_format_error(EMFormat *emf, CamelStream *stream, const char *txt);
static void emfq_format_message(EMFormat *, CamelStream *, CamelMedium *);
static void emfq_format_source(EMFormat *, CamelStream *, CamelMimePart *);
static void emfq_format_attachment(EMFormat *, CamelStream *, CamelMimePart *, const char *, const EMFormatHandler *);

static void emfq_builtin_init(EMFormatQuoteClass *efhc);

static EMFormatClass *emfq_parent;

static void
emfq_init(GObject *o)
{
    EMFormatQuote *emfq =(EMFormatQuote *) o;
    
    emfq->priv = g_malloc0(sizeof(*emfq->priv));
    
    /* we want to convert url's etc */
    emfq->text_html_flags = CAMEL_MIME_FILTER_TOHTML_PRE | CAMEL_MIME_FILTER_TOHTML_CONVERT_URLS
        | CAMEL_MIME_FILTER_TOHTML_CONVERT_ADDRESSES;
}

static void
emfq_finalise(GObject *o)
{
    EMFormatQuote *emfq =(EMFormatQuote *) o;

    if (emfq->stream)
        camel_object_unref(emfq->stream);
    g_free(emfq->credits);
    g_free(emfq->priv);
    
    ((GObjectClass *) emfq_parent)->finalize(o);
}

static void
emfq_base_init(EMFormatQuoteClass *emfqklass)
{
    emfq_builtin_init(emfqklass);
}

static void
emfq_class_init(GObjectClass *klass)
{
    ((EMFormatClass *) klass)->format_clone = emfq_format_clone;
    ((EMFormatClass *) klass)->format_error = emfq_format_error;
    ((EMFormatClass *) klass)->format_message = emfq_format_message;
    ((EMFormatClass *) klass)->format_source = emfq_format_source;
    ((EMFormatClass *) klass)->format_attachment = emfq_format_attachment;
    
    klass->finalize = emfq_finalise;
}

GType
em_format_quote_get_type(void)
{
    static GType type = 0;
    
    if (type == 0) {
        static const GTypeInfo info = {
            sizeof(EMFormatQuoteClass),
            (GBaseInitFunc)emfq_base_init, NULL,
            (GClassInitFunc)emfq_class_init,
            NULL, NULL,
            sizeof(EMFormatQuote), 0,
            (GInstanceInitFunc) emfq_init
        };
        
        emfq_parent = g_type_class_ref(em_format_get_type());
        type = g_type_register_static(em_format_get_type(), "EMFormatQuote", &info, 0);
    }
    
    return type;
}

EMFormatQuote *
em_format_quote_new(const char *credits, CamelStream *stream, guint32 flags)
{
    EMFormatQuote *emfq;
    
    emfq = (EMFormatQuote *)g_object_new(em_format_quote_get_type(), NULL);

    emfq->credits = g_strdup(credits);
    emfq->stream = stream;
    camel_object_ref(stream);
    emfq->flags = flags;
    
    return emfq;
}

static void
emfq_format_clone(EMFormat *emf, CamelMedium *part, EMFormat *src)
{
#define emfq ((EMFormatQuote *)emf)

    ((EMFormatClass *)emfq_parent)->format_clone(emf, part, src);

    camel_stream_reset(emfq->stream);
    em_format_format_message(emf, emfq->stream, part);
    camel_stream_flush(emfq->stream);

    g_signal_emit_by_name(emf, "complete");
#undef emfq
}

static void
emfq_format_error(EMFormat *emf, CamelStream *stream, const char *txt)
{
    /* FIXME: should we even bother writign error text for quoting? probably not... */
}

static void
emfq_format_message(EMFormat *emf, CamelStream *stream, CamelMedium *part)
{
    EMFormatQuote *emfq =(EMFormatQuote *) emf;
    
    if (emfq->credits)
        camel_stream_printf(stream, "%s", emfq->credits);


    if (emfq->flags & EM_FORMAT_QUOTE_CITE)
        camel_stream_printf(stream, "<!--+GtkHTML:<DATA class=\"ClueFlow\" key=\"orig\" value=\"1\">-->\n"
                    "<blockquote type=cite>\n"
                    "<font color=\"#%06x\">\n",
                    emfq->citation_colour & 0xffffff);

    if (emfq->flags & EM_FORMAT_QUOTE_HEADERS) {
        camel_stream_printf(stream, "<b>To: </b> Header goes here<br>");
    }

    em_format_part(emf, stream, (CamelMimePart *)part);

    if (emfq->flags & EM_FORMAT_QUOTE_CITE)
        camel_stream_write_string(stream, "</blockquote></font><!--+GtkHTML:<DATA class=\"ClueFlow\" clear=\"orig\">-->");
}

static void
emfq_format_source(EMFormat *emf, CamelStream *stream, CamelMimePart *part)
{
    CamelStreamFilter *filtered_stream;
    CamelMimeFilter *html_filter;
    CamelDataWrapper *dw = (CamelDataWrapper *)part;

    filtered_stream = camel_stream_filter_new_with_stream ((CamelStream *) stream);
    html_filter = camel_mime_filter_tohtml_new (CAMEL_MIME_FILTER_TOHTML_CONVERT_NL
                            | CAMEL_MIME_FILTER_TOHTML_CONVERT_SPACES
                            | CAMEL_MIME_FILTER_TOHTML_ESCAPE_8BIT, 0);
    camel_stream_filter_add(filtered_stream, html_filter);
    camel_object_unref(html_filter);
    
    em_format_format_text(emf, (CamelStream *)filtered_stream, dw);
    camel_object_unref(filtered_stream);
}

static void
emfq_format_attachment(EMFormat *emf, CamelStream *stream, CamelMimePart *part, const char *mime_type, const EMFormatHandler *handle)
{
    ;
}

#include <camel/camel-medium.h>
#include <camel/camel-mime-part.h>
#include <camel/camel-multipart.h>
#include <camel/camel-url.h>

static void
emfq_text_plain(EMFormatQuote *emfq, CamelStream *stream, CamelMimePart *part, EMFormatHandler *info)
{
    CamelStreamFilter *filtered_stream;
    CamelMimeFilter *html_filter;
    CamelContentType *type;
    const char *format;
    guint32 rgb = 0x737373, flags;

    flags = emfq->text_html_flags;
    
    /* Check for RFC 2646 flowed text. */
    type = camel_mime_part_get_content_type(part);
    if (header_content_type_is(type, "text", "plain")
        && (format = header_content_type_param(type, "format"))
        && !g_ascii_strcasecmp(format, "flowed"))
        flags |= CAMEL_MIME_FILTER_TOHTML_FORMAT_FLOWED;
    
    html_filter = camel_mime_filter_tohtml_new(flags, rgb);
    filtered_stream = camel_stream_filter_new_with_stream(stream);
    camel_stream_filter_add(filtered_stream, html_filter);
    camel_object_unref(html_filter);
    
    em_format_format_text((EMFormat *)emfq, (CamelStream *)filtered_stream, camel_medium_get_content_object((CamelMedium *)part));
    camel_stream_flush((CamelStream *)filtered_stream);
    camel_object_unref(filtered_stream);
}

static void
emfq_text_enriched(EMFormatQuote *emfq, CamelStream *stream, CamelMimePart *part, EMFormatHandler *info)
{
    CamelStreamFilter *filtered_stream;
    CamelMimeFilter *enriched;
    CamelDataWrapper *dw;
    guint32 flags = 0;
    
    dw = camel_medium_get_content_object((CamelMedium *)part);
    
    if (!strcmp(info->mime_type, "text/richtext")) {
        flags = CAMEL_MIME_FILTER_ENRICHED_IS_RICHTEXT;
        camel_stream_write_string( stream, "\n<!-- text/richtext -->\n");
    } else {
        camel_stream_write_string( stream, "\n<!-- text/enriched -->\n");
    }
    
    enriched = camel_mime_filter_enriched_new(flags);
    filtered_stream = camel_stream_filter_new_with_stream (stream);
    camel_stream_filter_add(filtered_stream, enriched);
    camel_object_unref(enriched);

    camel_stream_write_string(stream, "<br><hr><br>");
    em_format_format_text((EMFormat *)emfq, (CamelStream *)filtered_stream, dw);
    camel_object_unref(filtered_stream);
}

static void
emfq_text_html(EMFormat *emf, CamelStream *stream, CamelMimePart *part, EMFormatHandler *info)
{
    camel_stream_write_string(stream, "\n<!-- text/html -->\n");
    em_format_format_text(emf, stream, camel_medium_get_content_object((CamelMedium *)part));
}

static const char *type_remove_table[] = {
    "message/external-body",
    "multipart/appledouble",
};

static EMFormatHandler type_builtin_table[] = {
    { "text/plain",(EMFormatFunc)emfq_text_plain },
    { "text/enriched",(EMFormatFunc)emfq_text_enriched },
    { "text/richtext",(EMFormatFunc)emfq_text_enriched },
    { "text/html",(EMFormatFunc)emfq_text_html },
/*  { "multipart/related",(EMFormatFunc)emfq_multipart_related },*/
};

static void
emfq_builtin_init(EMFormatQuoteClass *efhc)
{
    int i;
    
    for (i = 0; i < sizeof(type_remove_table) / sizeof(type_remove_table[0]); i++)
        em_format_class_remove_handler((EMFormatClass *) efhc, type_remove_table[i]);

    for (i=0;i<sizeof(type_builtin_table)/sizeof(type_builtin_table[0]);i++)
        em_format_class_add_handler((EMFormatClass *)efhc, &type_builtin_table[i]);
}