aboutsummaryrefslogtreecommitdiffstats
path: root/em-format/e-mail-part-headers.c
blob: 9087e8e60d32d6e756906de797db9de3f81ce6e8 (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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
 * e-mail-part-headers.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/>
 *
 */

#include "e-mail-part-headers.h"

#include <config.h>
#include <glib/gi18n-lib.h>

#include "e-mail-part-list.h"

#define E_MAIL_PART_HEADERS_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE \
    ((obj), E_TYPE_MAIL_PART_HEADERS, EMailPartHeadersPrivate))

struct _EMailPartHeadersPrivate {
    GMutex property_lock;
    gchar **default_headers;
    GtkTreeModel *print_model;
};

enum {
    PROP_0,
    PROP_DEFAULT_HEADERS
};

G_DEFINE_TYPE (
    EMailPartHeaders,
    e_mail_part_headers,
    E_TYPE_MAIL_PART)

static const gchar *basic_headers[] = {
    N_("From"),
    N_("Reply-To"),
    N_("To"),
    N_("Cc"),
    N_("Bcc"),
    N_("Subject"),
    N_("Date"),
    N_("Newsgroups"),
    N_("Face"),
    NULL
};

static GtkTreeModel *
mail_part_headers_build_print_model (EMailPartHeaders *part)
{
    GtkListStore *list_store;
    EMailPartList *part_list;
    CamelMimeMessage *message;
    GArray *array;
    gint default_position = 0;
    guint ii, length = 0;

    /* If the part list is NULL, it means the function was called
     * too early.  The part must be added to a part list first so
     * we have access to the CamelMimeMessage. */
    part_list = e_mail_part_ref_part_list (E_MAIL_PART (part));
    g_return_val_if_fail (part_list != NULL, NULL);

    list_store = gtk_list_store_new (
        E_MAIL_PART_HEADERS_PRINT_MODEL_NUM_COLUMNS,
        G_TYPE_BOOLEAN,  /* INCLUDE */
        G_TYPE_STRING,   /* HEADER_NAME */
        G_TYPE_STRING);  /* HEADER_VALUE */

    message = e_mail_part_list_get_message (part_list);
    array = camel_medium_get_headers (CAMEL_MEDIUM (message));

    if (array != NULL)
        length = array->len;

    for (ii = 0; ii < length; ii++) {
        CamelMediumHeader *header;
        GtkTreeIter iter;
        gboolean include = FALSE;
        gint position = -1;

        header = &g_array_index (array, CamelMediumHeader, ii);

        /* EMailFormatterPrintHeaders excludes "Subject" from
         * its header table (because it puts it in an <h1> tag
         * at the top of the page), so we'll exclude it too. */
        if (g_ascii_strncasecmp (header->name, "Subject", 7) == 0)
            continue;

        /* Arrange default headers first and select them to be
         * included in the final printout.  All other headers
         * are excluded by default in the final printout. */
        if (e_mail_part_headers_is_default (part, header->name)) {
            position = default_position++;
            include = TRUE;
        }

        gtk_list_store_insert (list_store, &iter, position);

        gtk_list_store_set (
            list_store, &iter,
            E_MAIL_PART_HEADERS_PRINT_MODEL_COLUMN_INCLUDE,
            include,
            E_MAIL_PART_HEADERS_PRINT_MODEL_COLUMN_HEADER_NAME,
            header->name,
            E_MAIL_PART_HEADERS_PRINT_MODEL_COLUMN_HEADER_VALUE,
            header->value,
            -1);
    }

    if (array != NULL)
        camel_medium_free_headers (CAMEL_MEDIUM (message), array);

    g_object_unref (part_list);

    /* Stash the print model internally. */

    g_mutex_lock (&part->priv->property_lock);

    g_clear_object (&part->priv->print_model);
    part->priv->print_model = g_object_ref (list_store);

    g_mutex_unlock (&part->priv->property_lock);

    return GTK_TREE_MODEL (list_store);
}

static void
mail_part_headers_set_property (GObject *object,
                                guint property_id,
                                const GValue *value,
                                GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_DEFAULT_HEADERS:
            e_mail_part_headers_set_default_headers (
                E_MAIL_PART_HEADERS (object),
                g_value_get_boxed (value));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
mail_part_headers_get_property (GObject *object,
                                guint property_id,
                                GValue *value,
                                GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_DEFAULT_HEADERS:
            g_value_take_boxed (
                value,
                e_mail_part_headers_dup_default_headers (
                E_MAIL_PART_HEADERS (object)));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
mail_part_headers_dispose (GObject *object)
{
    EMailPartHeadersPrivate *priv;

    priv = E_MAIL_PART_HEADERS_GET_PRIVATE (object);

    g_clear_object (&priv->print_model);

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

static void
mail_part_headers_finalize (GObject *object)
{
    EMailPartHeadersPrivate *priv;

    priv = E_MAIL_PART_HEADERS_GET_PRIVATE (object);

    g_mutex_clear (&priv->property_lock);

    g_strfreev (priv->default_headers);

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

static void
mail_part_headers_constructed (GObject *object)
{
    EMailPart *part;

    part = E_MAIL_PART (object);

    /* Chain up to parent's constructed() method. */
    G_OBJECT_CLASS (e_mail_part_headers_parent_class)->
        constructed (object);

    e_mail_part_set_mime_type (part, E_MAIL_PART_HEADERS_MIME_TYPE);
}

static void
mail_part_headers_bind_dom_element (EMailPart *part,
                                    WebKitDOMElement *element)
{
    WebKitDOMDocument *document;
    WebKitDOMElement *photo;
    gchar *addr, *uri;

    document = webkit_dom_node_get_owner_document (
        WEBKIT_DOM_NODE (element));
    photo = webkit_dom_document_get_element_by_id (
        document, "__evo-contact-photo");

    /* Contact photos disabled, the <img> tag is not there. */
    if (photo == NULL)
        return;

    addr = webkit_dom_element_get_attribute (photo, "data-mailaddr");
    uri = g_strdup_printf ("mail://contact-photo?mailaddr=%s", addr);

    webkit_dom_html_image_element_set_src (
        WEBKIT_DOM_HTML_IMAGE_ELEMENT (photo), uri);

    g_free (addr);
    g_free (uri);
}

static void
e_mail_part_headers_class_init (EMailPartHeadersClass *class)
{
    GObjectClass *object_class;
    EMailPartClass *mail_part_class;

    g_type_class_add_private (class, sizeof (EMailPartHeadersPrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->set_property = mail_part_headers_set_property;
    object_class->get_property = mail_part_headers_get_property;
    object_class->dispose = mail_part_headers_dispose;
    object_class->finalize = mail_part_headers_finalize;
    object_class->constructed = mail_part_headers_constructed;

    mail_part_class = E_MAIL_PART_CLASS (class);
    mail_part_class->bind_dom_element = mail_part_headers_bind_dom_element;

    g_object_class_install_property (
        object_class,
        PROP_DEFAULT_HEADERS,
        g_param_spec_boxed (
            "default-headers",
            "Default Headers",
            "Headers to display by default",
            G_TYPE_STRV,
            G_PARAM_READWRITE |
            G_PARAM_CONSTRUCT |
            G_PARAM_STATIC_STRINGS));
}

static void
e_mail_part_headers_init (EMailPartHeaders *part)
{
    part->priv = E_MAIL_PART_HEADERS_GET_PRIVATE (part);

    g_mutex_init (&part->priv->property_lock);
}

EMailPart *
e_mail_part_headers_new (CamelMimePart *mime_part,
                         const gchar *id)
{
    g_return_val_if_fail (id != NULL, NULL);

    return g_object_new (
        E_TYPE_MAIL_PART_HEADERS,
        "id", id, "mime-part", mime_part, NULL);
}

gchar **
e_mail_part_headers_dup_default_headers (EMailPartHeaders *part)
{
    gchar **default_headers;

    g_return_val_if_fail (E_IS_MAIL_PART_HEADERS (part), NULL);

    g_mutex_lock (&part->priv->property_lock);

    default_headers = g_strdupv (part->priv->default_headers);

    g_mutex_unlock (&part->priv->property_lock);

    return default_headers;
}

void
e_mail_part_headers_set_default_headers (EMailPartHeaders *part,
                                         const gchar * const *default_headers)
{
    g_return_if_fail (E_IS_MAIL_PART_HEADERS (part));

    if (default_headers == NULL)
        default_headers = basic_headers;

    g_mutex_lock (&part->priv->property_lock);

    g_strfreev (part->priv->default_headers);
    part->priv->default_headers = g_strdupv ((gchar **) default_headers);

    g_mutex_unlock (&part->priv->property_lock);

    g_object_notify (G_OBJECT (part), "default-headers");
}

gboolean
e_mail_part_headers_is_default (EMailPartHeaders *part,
                                const gchar *header_name)
{
    gboolean is_default = FALSE;
    guint ii, length = 0;

    g_return_val_if_fail (E_IS_MAIL_PART_HEADERS (part), FALSE);
    g_return_val_if_fail (header_name != NULL, FALSE);

    g_mutex_lock (&part->priv->property_lock);

    if (part->priv->default_headers != NULL)
        length = g_strv_length (part->priv->default_headers);

    for (ii = 0; ii < length; ii++) {
        const gchar *candidate;

        /* g_strv_length() stops on the first NULL pointer,
         * so we don't have to worry about this being NULL. */
        candidate = part->priv->default_headers[ii];

        if (g_ascii_strcasecmp (header_name, candidate) == 0) {
            is_default = TRUE;
            break;
        }
    }

    g_mutex_unlock (&part->priv->property_lock);

    return is_default;
}

GtkTreeModel *
e_mail_part_headers_ref_print_model (EMailPartHeaders *part)
{
    GtkTreeModel *print_model = NULL;

    g_return_val_if_fail (E_IS_MAIL_PART_HEADERS (part), NULL);

    g_mutex_lock (&part->priv->property_lock);

    if (part->priv->print_model != NULL)
        print_model = g_object_ref (part->priv->print_model);

    g_mutex_unlock (&part->priv->property_lock);

    if (print_model == NULL) {
        /* The print model is built once on demand. */
        print_model = mail_part_headers_build_print_model (part);
    }

    return print_model;
}