aboutsummaryrefslogtreecommitdiffstats
path: root/em-format/e-mail-part.c
blob: c0a57271b435291e445137252bab1afecf3482d2 (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
/*
 * 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/>
 *
 */

#include <camel/camel.h>

#include "e-mail-part.h"

/**
 * EMailPart:
 *
 * The #EMailPart is a wrapper around #CamelMimePart which holds additional
 * information about the mime part, like it's ID, encryption type etc.
 *
 * #EMailPart is not GObject-based, but has a simple reference counting.
 *
 * 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.
 */

struct _EMailPartPrivate {
    guint ref_cnt;
    gsize instance_size;
    GFreeFunc free_func;
};

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_free (EMailPart *part)
{
    if (!part)
        return;

    if (part->part) {
        g_object_unref (part->part);
        part->part = NULL;
    }

    if (part->cid) {
        g_free (part->cid);
        part->cid = NULL;
    }

    if (part->mime_type) {
        g_free (part->mime_type);
        part->mime_type = NULL;
    }

    if (part->validities) {
        g_slist_free_full (part->validities, mail_part_validity_pair_free);
        part->validities = NULL;
    }

    if (part->priv->free_func) {
        part->priv->free_func (part);
        part->priv->free_func = NULL;
    }

    if (part->id) {
        g_free (part->id);
        part->id = NULL;
    }

    g_free (part->priv);
    part->priv = NULL;

    g_free (part);
}

/**
 * e_mail_part_new:
 * @part: (allow-none) a #CamelMimePart or %NULL
 * @id: part ID
 *
 * Creates a new #EMailPart for given mime part.
 *
 * Return value: a new #EMailPart
 */
EMailPart *
e_mail_part_new (CamelMimePart *part,
                 const gchar *id)
{
    return e_mail_part_subclass_new (part, id, sizeof (EMailPart), NULL);
}

/**
 * e_mail_part_new:
 * @part: (allow-none) a #CamelMimePart or %NULL
 * @id: part ID
 * @size: Size of the EMailPart subclass
 *
 * Allocates a @size bytes representing an #EMailPart subclass.
 *
 * Return value: a new #EMailPart-based object
 */
EMailPart *
e_mail_part_subclass_new (CamelMimePart *part,
                          const gchar *id,
                          gsize size,
                          GFreeFunc free_func)
{
    EMailPart *mail_part;

    g_return_val_if_fail (size >= sizeof (EMailPart), NULL);

    mail_part = g_malloc0 (size);
    mail_part->priv = g_new0 (EMailPartPrivate, 1);

    mail_part->priv->ref_cnt = 1;
    mail_part->priv->free_func = free_func;
    mail_part->priv->instance_size = size;

    if (part) {
        mail_part->part = g_object_ref (part);
    }

    if (id) {
        mail_part->id = g_strdup (id);
    }

    return mail_part;
}

EMailPart *
e_mail_part_ref (EMailPart *part)
{
    g_return_val_if_fail (part != NULL, NULL);
    g_return_val_if_fail (part->priv != NULL, NULL);

    g_atomic_int_inc (&part->priv->ref_cnt);

    return part;
}

void
e_mail_part_unref (EMailPart *part)
{
    g_return_if_fail (part != NULL);
    g_return_if_fail (part->priv != NULL);

    if (g_atomic_int_dec_and_test (&part->priv->ref_cnt)) {
        mail_part_free (part);
    }
}

gsize
e_mail_part_get_instance_size (EMailPart *part)
{
    g_return_val_if_fail (part != NULL, 0);

    return part->priv->instance_size;
}

static EMailPartValidityPair *
mail_part_find_validity_pair (EMailPart *part,
                  guint32 validity_type)
{
    GSList *lst;

    for (lst = part->validities; lst; lst = lst->next) {
        EMailPartValidityPair *pair = lst->data;

        if (pair && (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,
                             guint32 validity_type)
{
    EMailPartValidityPair *pair;

    g_return_if_fail (part != NULL);

    pair = mail_part_find_validity_pair (part, validity_type & (E_MAIL_PART_VALIDITY_PGP | E_MAIL_PART_VALIDITY_SMIME));
    if (pair) {
        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);

        part->validities = g_slist_append (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,
              guint32 validity_type)
{
    EMailPartValidityPair *pair;

    g_return_val_if_fail (part != NULL, NULL);

    pair = mail_part_find_validity_pair (part, validity_type);

    return pair ? pair->validity : NULL;
}