aboutsummaryrefslogtreecommitdiffstats
path: root/em-format/e-mail-part-list.c
blob: 0ad8a9198f5216421789c686b82b4b1d3d59400f (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
/*
 * e-mail-part-list.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-list.h"

G_DEFINE_TYPE (EMailPartList, e_mail_part_list, G_TYPE_OBJECT)


static CamelObjectBag *registry = NULL;
G_LOCK_DEFINE_STATIC (registry);

static void
unref_mail_part (gpointer user_data)
{
    if (user_data)
        e_mail_part_unref (user_data);
}

static void
e_mail_part_list_finalize (GObject *object)
{
    EMailPartList *part_list = E_MAIL_PART_LIST (object);

    g_clear_object (&part_list->folder);
    g_clear_object (&part_list->message);

    if (part_list->list) {
        g_slist_free_full (part_list->list, unref_mail_part);
        part_list->list = NULL;
    }

    if (part_list->message_uid) {
        g_free (part_list->message_uid);
        part_list->message_uid = NULL;
    }

    G_OBJECT_CLASS (e_mail_part_list_parent_class)->finalize (object);
}

static void
e_mail_part_list_class_init (EMailPartListClass *klass)
{
    GObjectClass *object_class;

    object_class = G_OBJECT_CLASS (klass);
    object_class->finalize = e_mail_part_list_finalize;
}

static void
e_mail_part_list_init (EMailPartList *part_list)
{

}

EMailPartList *
e_mail_part_list_new ()
{
    return g_object_new (E_TYPE_MAIL_PART_LIST, NULL);
}

EMailPart *
e_mail_part_list_find_part (EMailPartList *part_list,
                            const gchar *id)
{
    GSList *iter;
    gboolean by_cid;

    g_return_val_if_fail (E_IS_MAIL_PART_LIST (part_list), NULL);
    g_return_val_if_fail (id && *id, NULL);

    by_cid = (g_str_has_prefix (id, "cid:") || g_str_has_prefix (id, "CID:"));

    for (iter = part_list->list; iter; iter = iter->next) {

        EMailPart *part = iter->data;
        if (!part)
            continue;

        if ((by_cid && (g_strcmp0 (part->cid, id) == 0)) ||
            (!by_cid && (g_strcmp0 (part->id, id) == 0)))
            return part;
    }

    return NULL;
}

/**
 * e_mail_part_list_get_iter:
 * @part_list: a #GSList of #EMailPart
 * @id: id of #EMailPart to lookup
 *
 * Returns iter of an #EMailPart within the @part_list.
 *
 * Return Value: a #GSList sublist. The list is owned by #EMailPartList and
 * must not be freed or altered.
 */
GSList *
e_mail_part_list_get_iter (GSList *list,
                           const gchar *id)
{
    GSList *iter;

    g_return_val_if_fail (list != NULL, NULL);
    g_return_val_if_fail (id && *id, NULL);

    for (iter = list; iter; iter = iter->next) {

        EMailPart *part = iter->data;
        if (!part)
            continue;

        if (g_strcmp0 (part->id, id) == 0)
            return iter;
    }

    return NULL;
}

/**
 * e_mail_part_list_get_registry:
 *
 * Returns a #CamelObjectBag where parsed #EMailPartLists can be stored.
 */
CamelObjectBag *
e_mail_part_list_get_registry (void)
{
    G_LOCK (registry);
    if (registry == NULL) {
        registry = camel_object_bag_new (
                g_str_hash, g_str_equal,
                (CamelCopyFunc) g_strdup, g_free);
    }
    G_UNLOCK (registry);

    return registry;
}

static void
part_list_weak_ref_notify (gchar *mail_uri,
               EMailPartList *part_list)
{
    CamelObjectBag *reg = e_mail_part_list_get_registry ();

    camel_object_bag_remove (reg, part_list);
}

/**
 * e_mail_part_list_registry_add:
 *
 * This method should be used to add a new @part_list to the
 * #CamelObjectBag registry. It will automatically handle removing
 * the @part_list from the bag when it's destroyed.
 *
 * The @registry don't take any reference to the @part_list.
 */
void
e_mail_part_list_registry_add (CamelObjectBag *registry,
                   const gchar *mail_uri,
                   EMailPartList *part_list)
{
    camel_object_bag_add (registry, mail_uri, part_list);

    g_object_weak_ref (
        G_OBJECT (part_list),
        (GWeakNotify) part_list_weak_ref_notify,
        g_strdup (mail_uri));
}