aboutsummaryrefslogtreecommitdiffstats
path: root/em-format/e-mail-part.c
blob: c7b07452eb82a55e6dd8855a9360bd254f6ea400 (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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/*
 * e-mail-part.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/>
 *
 */

/**
 * EMailPart:
 *
 * The #EMailPart is a wrapper around #CamelMimePart which holds additional
 * information about the mime part, like it's ID, encryption type etc.
 *
 * Each #EMailPart must have a unique ID. The ID is a dot-separated
 * hierarchical description of the location of the part within the email
 * message.
 */

#include "e-mail-part.h"

#include <string.h>

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

#define E_MAIL_PART_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE \
    ((obj), E_TYPE_MAIL_PART, EMailPartPrivate))

struct _EMailPartPrivate {
    GWeakRef part_list;
    CamelMimePart *mime_part;

    gchar *id;
    gchar *cid;
    gchar *mime_type;

    gboolean is_attachment;
};

enum {
    PROP_0,
    PROP_CID,
    PROP_ID,
    PROP_IS_ATTACHMENT,
    PROP_MIME_PART,
    PROP_MIME_TYPE,
    PROP_PART_LIST
};

G_DEFINE_TYPE_WITH_CODE (
    EMailPart,
    e_mail_part,
    G_TYPE_OBJECT,
    G_IMPLEMENT_INTERFACE (
        E_TYPE_EXTENSIBLE, NULL))

static void
mail_part_validity_pair_free (gpointer ptr)
{
    EMailPartValidityPair *pair = ptr;

    if (!pair)
        return;

    camel_cipher_validity_free (pair->validity);
    g_free (pair);
}

static void
mail_part_set_id (EMailPart *part,
                  const gchar *id)
{
    g_return_if_fail (part->priv->id == NULL);

    part->priv->id = g_strdup (id);
}

static void
mail_part_set_mime_part (EMailPart *part,
                         CamelMimePart *mime_part)
{
    g_return_if_fail (part->priv->mime_part == NULL);

    /* The CamelMimePart is optional. */
    if (mime_part != NULL)
        part->priv->mime_part = g_object_ref (mime_part);
}

static void
mail_part_set_property (GObject *object,
                        guint property_id,
                        const GValue *value,
                        GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_CID:
            e_mail_part_set_cid (
                E_MAIL_PART (object),
                g_value_get_string (value));
            return;

        case PROP_ID:
            mail_part_set_id (
                E_MAIL_PART (object),
                g_value_get_string (value));
            return;

        case PROP_IS_ATTACHMENT:
            e_mail_part_set_is_attachment (
                E_MAIL_PART (object),
                g_value_get_boolean (value));
            return;

        case PROP_MIME_PART:
            mail_part_set_mime_part (
                E_MAIL_PART (object),
                g_value_get_object (value));
            return;

        case PROP_MIME_TYPE:
            e_mail_part_set_mime_type (
                E_MAIL_PART (object),
                g_value_get_string (value));
            return;

        case PROP_PART_LIST:
            e_mail_part_set_part_list (
                E_MAIL_PART (object),
                g_value_get_object (value));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
mail_part_get_property (GObject *object,
                        guint property_id,
                        GValue *value,
                        GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_CID:
            g_value_set_string (
                value,
                e_mail_part_get_cid (
                E_MAIL_PART (object)));
            return;

        case PROP_ID:
            g_value_set_string (
                value,
                e_mail_part_get_id (
                E_MAIL_PART (object)));
            return;

        case PROP_IS_ATTACHMENT:
            g_value_set_boolean (
                value,
                e_mail_part_get_is_attachment (
                E_MAIL_PART (object)));
            return;

        case PROP_MIME_PART:
            g_value_take_object (
                value,
                e_mail_part_ref_mime_part (
                E_MAIL_PART (object)));
            return;

        case PROP_MIME_TYPE:
            g_value_set_string (
                value,
                e_mail_part_get_mime_type (
                E_MAIL_PART (object)));
            return;

        case PROP_PART_LIST:
            g_value_take_object (
                value,
                e_mail_part_ref_part_list (
                E_MAIL_PART (object)));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
mail_part_dispose (GObject *object)
{
    EMailPartPrivate *priv;

    priv = E_MAIL_PART_GET_PRIVATE (object);

    g_weak_ref_set (&priv->part_list, NULL);

    g_clear_object (&priv->mime_part);

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

static void
mail_part_finalize (GObject *object)
{
    EMailPart *part = E_MAIL_PART (object);
    EMailPartValidityPair *pair;

    g_free (part->priv->id);
    g_free (part->priv->cid);
    g_free (part->priv->mime_type);

    while ((pair = g_queue_pop_head (&part->validities)) != NULL)
        mail_part_validity_pair_free (pair);

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

static void
mail_part_constructed (GObject *object)
{
    /* Chain up to parent's constructed() method. */
    G_OBJECT_CLASS (e_mail_part_parent_class)->constructed (object);

    e_extensible_load_extensions (E_EXTENSIBLE (object));
}

static void
e_mail_part_class_init (EMailPartClass *class)
{
    GObjectClass *object_class;

    g_type_class_add_private (class, sizeof (EMailPartPrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->set_property = mail_part_set_property;
    object_class->get_property = mail_part_get_property;
    object_class->dispose = mail_part_dispose;
    object_class->finalize = mail_part_finalize;
    object_class->constructed = mail_part_constructed;

    g_object_class_install_property (
        object_class,
        PROP_CID,
        g_param_spec_string (
            "cid",
            "Content ID",
            "The MIME Content-ID",
            NULL,
            G_PARAM_READWRITE |
            G_PARAM_STATIC_STRINGS));

    g_object_class_install_property (
        object_class,
        PROP_ID,
        g_param_spec_string (
            "id",
            "Part ID",
            "The part ID",
            NULL,
            G_PARAM_READWRITE |
            G_PARAM_CONSTRUCT_ONLY |
            G_PARAM_STATIC_STRINGS));

    g_object_class_install_property (
        object_class,
        PROP_IS_ATTACHMENT,
        g_param_spec_boolean (
            "is-attachment",
            "Is Attachment",
            "Format the part as an attachment",
            FALSE,
            G_PARAM_READWRITE |
            G_PARAM_CONSTRUCT |
            G_PARAM_STATIC_STRINGS));

    g_object_class_install_property (
        object_class,
        PROP_MIME_PART,
        g_param_spec_object (
            "mime-part",
            "MIME Part",
            "The MIME part",
            CAMEL_TYPE_MIME_PART,
            G_PARAM_READWRITE |
            G_PARAM_CONSTRUCT_ONLY |
            G_PARAM_STATIC_STRINGS));

    g_object_class_install_property (
        object_class,
        PROP_MIME_TYPE,
        g_param_spec_string (
            "mime-type",
            "MIME Type",
            "The MIME type",
            NULL,
            G_PARAM_READWRITE |
            G_PARAM_STATIC_STRINGS));

    g_object_class_install_property (
        object_class,
        PROP_PART_LIST,
        g_param_spec_object (
            "part-list",
            "Part List",
            "The part list that owns the part",
            E_TYPE_MAIL_PART_LIST,
            G_PARAM_READWRITE |
            G_PARAM_STATIC_STRINGS));
}

static void
e_mail_part_init (EMailPart *part)
{
    part->priv = E_MAIL_PART_GET_PRIVATE (part);
}

/**
 * e_mail_part_new:
 * @mime_part: (allow-none) a #CamelMimePart or %NULL
 * @id: part ID
 *
 * Creates a new #EMailPart for the given @mime_part.
 *
 * Return value: a new #EMailPart
 */
EMailPart *
e_mail_part_new (CamelMimePart *mime_part,
                 const gchar *id)
{
    g_return_val_if_fail (id != NULL, NULL);

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

const gchar *
e_mail_part_get_id (EMailPart *part)
{
    g_return_val_if_fail (E_IS_MAIL_PART (part), NULL);

    return part->priv->id;
}

const gchar *
e_mail_part_get_cid (EMailPart *part)
{
    g_return_val_if_fail (E_IS_MAIL_PART (part), NULL);

    return part->priv->cid;
}

void
e_mail_part_set_cid (EMailPart *part,
                     const gchar *cid)
{
    g_return_if_fail (E_IS_MAIL_PART (part));

    g_free (part->priv->cid);
    part->priv->cid = g_strdup (cid);

    g_object_notify (G_OBJECT (part), "cid");
}

gboolean
e_mail_part_id_has_prefix (EMailPart *part,
                           const gchar *prefix)
{
    g_return_val_if_fail (E_IS_MAIL_PART (part), FALSE);
    g_return_val_if_fail (prefix != NULL, FALSE);

    return g_str_has_prefix (part->priv->id, prefix);
}

gboolean
e_mail_part_id_has_suffix (EMailPart *part,
                           const gchar *suffix)
{
    g_return_val_if_fail (E_IS_MAIL_PART (part), FALSE);
    g_return_val_if_fail (suffix != NULL, FALSE);

    return g_str_has_suffix (part->priv->id, suffix);
}

gboolean
e_mail_part_id_has_substr (EMailPart *part,
                           const gchar *substr)
{
    g_return_val_if_fail (E_IS_MAIL_PART (part), FALSE);
    g_return_val_if_fail (substr != NULL, FALSE);

    return (strstr (part->priv->id, substr) != NULL);
}

CamelMimePart *
e_mail_part_ref_mime_part (EMailPart *part)
{
    CamelMimePart *mime_part = NULL;

    g_return_val_if_fail (E_IS_MAIL_PART (part), NULL);

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

    return mime_part;
}

const gchar *
e_mail_part_get_mime_type (EMailPart *part)
{
    g_return_val_if_fail (E_IS_MAIL_PART (part), NULL);

    return part->priv->mime_type;
}

void
e_mail_part_set_mime_type (EMailPart *part,
                           const gchar *mime_type)
{
    g_return_if_fail (E_IS_MAIL_PART (part));

    if (g_strcmp0 (mime_type, part->priv->mime_type) == 0)
        return;

    g_free (part->priv->mime_type);
    part->priv->mime_type = g_strdup (mime_type);

    g_object_notify (G_OBJECT (part), "mime-type");
}

EMailPartList *
e_mail_part_ref_part_list (EMailPart *part)
{
    g_return_val_if_fail (E_IS_MAIL_PART (part), NULL);

    return g_weak_ref_get (&part->priv->part_list);
}

void
e_mail_part_set_part_list (EMailPart *part,
                           EMailPartList *part_list)
{
    g_return_if_fail (E_IS_MAIL_PART (part));

    if (part_list != NULL)
        g_return_if_fail (E_IS_MAIL_PART_LIST (part_list));

    g_weak_ref_set (&part->priv->part_list, part_list);

    g_object_notify (G_OBJECT (part), "part-list");
}

gboolean
e_mail_part_get_is_attachment (EMailPart *part)
{
    g_return_val_if_fail (E_IS_MAIL_PART (part), FALSE);

    return part->priv->is_attachment;
}

void
e_mail_part_set_is_attachment (EMailPart *part,
                               gboolean is_attachment)
{
    g_return_if_fail (E_IS_MAIL_PART (part));

    if (is_attachment == part->priv->is_attachment)
        return;

    part->priv->is_attachment = is_attachment;

    g_object_notify (G_OBJECT (part), "is-attachment");
}

void
e_mail_part_bind_dom_element (EMailPart *part,
                              WebKitDOMElement *element)
{
    EMailPartClass *class;

    g_return_if_fail (E_IS_MAIL_PART (part));
    g_return_if_fail (WEBKIT_DOM_IS_ELEMENT (element));

    class = E_MAIL_PART_GET_CLASS (part);

    if (class->bind_dom_element != NULL)
        class->bind_dom_element (part, element);
}

static EMailPartValidityPair *
mail_part_find_validity_pair (EMailPart *part,
                              EMailPartValidityFlags validity_type)
{
    GList *head, *link;

    head = g_queue_peek_head_link (&part->validities);

    for (link = head; link != NULL; link = g_list_next (link)) {
        EMailPartValidityPair *pair = link->data;

        if (pair == NULL)
            continue;

        if ((pair->validity_type & validity_type) == validity_type)
            return pair;
    }

    return NULL;
}

/**
 * e_mail_part_update_validity:
 * @part: An #EMailPart
 * @validity_type: E_MAIL_PART_VALIDITY_* flags
 * @validity: a #CamelCipherValidity
 *
 * Updates validity of the @part. When the part already has some validity
 * set, the new @validity and @validity_type are just appended, preserving
 * the original validity. Validities of the same type (PGP or S/MIME) are
 * merged together.
 */
void
e_mail_part_update_validity (EMailPart *part,
                             CamelCipherValidity *validity,
                             EMailPartValidityFlags validity_type)
{
    EMailPartValidityPair *pair;
    EMailPartValidityFlags mask;

    g_return_if_fail (E_IS_MAIL_PART (part));

    mask = E_MAIL_PART_VALIDITY_PGP | E_MAIL_PART_VALIDITY_SMIME;

    pair = mail_part_find_validity_pair (part, validity_type & mask);
    if (pair != NULL) {
        pair->validity_type |= validity_type;
        camel_cipher_validity_envelope (pair->validity, validity);
    } else {
        pair = g_new0 (EMailPartValidityPair, 1);
        pair->validity_type = validity_type;
        pair->validity = camel_cipher_validity_clone (validity);

        g_queue_push_tail (&part->validities, pair);
    }
}

/**
 * e_mail_part_get_validity:
 * @part: An #EMailPart
 * @validity_type: E_MAIL_PART_VALIDITY_* flags
 *
 * Returns, validity of @part contains any validity with the same bits
 * as @validity_type set. It should contain all bits of it.
 *
 * Returns: a #CamelCipherValidity of the given type, %NULL if not found
 *
 * Since: 3.8
 */
CamelCipherValidity *
e_mail_part_get_validity (EMailPart *part,
                          EMailPartValidityFlags validity_type)
{
    EMailPartValidityPair *pair;

    g_return_val_if_fail (E_IS_MAIL_PART (part), NULL);

    pair = mail_part_find_validity_pair (part, validity_type);

    return (pair != NULL) ? pair->validity : NULL;
}

gboolean
e_mail_part_has_validity (EMailPart *part)
{
    g_return_val_if_fail (E_IS_MAIL_PART (part), FALSE);

    return !g_queue_is_empty (&part->validities);
}

EMailPartValidityFlags
e_mail_part_get_validity_flags (EMailPart *part)
{
    EMailPartValidityFlags flags = 0;
    GList *head, *link;

    g_return_val_if_fail (E_IS_MAIL_PART (part), 0);

    head = g_queue_peek_head_link (&part->validities);

    for (link = head; link != NULL; link = g_list_next (link)) {
        EMailPartValidityPair *pair = link->data;

        if (pair != NULL)
            flags |= pair->validity_type;
    }

    return flags;
}