aboutsummaryrefslogtreecommitdiffstats
path: root/em-format/e-mail-extension-registry.c
blob: 3d5e7baa7c497ff05a324c66338d2ebe9d25045f (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
/*
 * e-mail-extension-registry.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-extension-registry.h"

#include <string.h>

#include <libebackend/libebackend.h>

#include "e-mail-formatter-extension.h"
#include "e-mail-parser-extension.h"

#define E_MAIL_EXTENSION_REGISTRY_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE \
    ((obj), E_TYPE_MAIL_EXTENSION_REGISTRY, EMailExtensionRegistryPrivate))

struct _EMailExtensionRegistryPrivate {
    GHashTable *table;
};

G_DEFINE_ABSTRACT_TYPE (
    EMailExtensionRegistry,
    e_mail_extension_registry,
    G_TYPE_OBJECT)

/**
 * EMailExtensionRegistry:
 *
 * The #EMailExtensionRegistry is an abstract class representing a registry
 * for #EMailExtension<!-//>s.
 *
 * #EMailParser and #EMailFormatter both have internally a registry object
 * based on the #EMailExtensionRegistry.
 *
 * One extension can registry itself for more mime-types.
 */

static void
destroy_queue (GQueue *queue)
{
    g_queue_free_full (queue, g_object_unref);
}

static void
mail_extension_registry_add_extension (EMailExtensionRegistry *registry,
                                       const gchar **mime_types,
                                       GType extension_type,
                                       GCompareDataFunc compare_func)
{
    GObject *extension;
    gint ii;

    if (mime_types == NULL) {
        g_critical (
            "%s does not define any MIME types",
            g_type_name (extension_type));
        return;
    }

    extension = g_object_new (extension_type, NULL);

    for (ii = 0; mime_types[ii] != NULL; ii++) {
        GQueue *queue;

        queue = g_hash_table_lookup (
            registry->priv->table, mime_types[ii]);
        if (queue == NULL) {
            queue = g_queue_new ();
            g_hash_table_insert (
                registry->priv->table,
                (gpointer) mime_types[ii],
                queue);
        }

        g_queue_insert_sorted (
            queue, g_object_ref (extension),
            compare_func, NULL);

        if (camel_debug ("emformat:registry")) {
            printf (
                "Added extension '%s' for type '%s'\n",
                g_type_name (extension_type),
                mime_types[ii]);
        }
    }

    g_object_unref (extension);
}

static void
mail_extension_registry_finalize (GObject *object)
{
    EMailExtensionRegistryPrivate *priv;

    priv = E_MAIL_EXTENSION_REGISTRY_GET_PRIVATE (object);

    g_hash_table_destroy (priv->table);

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

void
e_mail_extension_registry_class_init (EMailExtensionRegistryClass *class)
{
    GObjectClass *object_class;

    g_type_class_add_private (
        class, sizeof (EMailExtensionRegistryPrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->finalize = mail_extension_registry_finalize;
}

void
e_mail_extension_registry_init (EMailExtensionRegistry *registry)
{
    registry->priv = E_MAIL_EXTENSION_REGISTRY_GET_PRIVATE (registry);

    registry->priv->table = g_hash_table_new_full (
        (GHashFunc) g_str_hash,
        (GEqualFunc) g_str_equal,
        (GDestroyNotify) NULL,
        (GDestroyNotify) destroy_queue);
}

/**
 * e_mail_extension_registry_get_for_mime_type:
 * @regstry: An #EMailExtensionRegistry
 * @mime_type: A string with mime-type to look up
 *
 * Tries to lookup list of #EMailExtension<!-//>s that has registryed themselves
 * as handlers for the @mime_type.
 *
 * Return value: Returns #GQueue of #EMailExtension<!-//>s or %NULL when there
 * are no extension registryed for given @mime_type.
 */
GQueue *
e_mail_extension_registry_get_for_mime_type (EMailExtensionRegistry *registry,
                                             const gchar *mime_type)
{
    g_return_val_if_fail (E_IS_MAIL_EXTENSION_REGISTRY (registry), NULL);
    g_return_val_if_fail (mime_type && *mime_type, NULL);

    return g_hash_table_lookup (registry->priv->table, mime_type);
}

/**
 * e_mail_extension_registry_get_fallback:
 * @registry: An #EMailExtensionRegistry
 * @mime_type: A string with mime-type whose fallback to look up
 *
 * Tries to lookup fallback parsers for given mime type. For instance, for
 * multipart/alternative, it will try to lookup multipart/ * parser.
 *
 * Return Value: Returns #QGueue of #EMailExtension<!-//>>s or %NULL when there
 * are no extensions registryed for the fallback type.
 */
GQueue *
e_mail_extension_registry_get_fallback (EMailExtensionRegistry *registry,
                                        const gchar *mime_type)
{
    gchar *s, *type;
    gsize len;
    GQueue *parsers;

    g_return_val_if_fail (E_IS_MAIL_EXTENSION_REGISTRY (registry), NULL);
    g_return_val_if_fail (mime_type && *mime_type, NULL);

    s = strchr (mime_type, '/');
    if (!s)
        return NULL;

    len = s - mime_type;

    s = g_alloca (len);
    strncpy (s, mime_type, len);
    type = g_ascii_strdown (s, len);
    s = g_strdup_printf ("%s/*", type);

    parsers = g_hash_table_lookup (registry->priv->table, s);

    g_free (type);
    g_free (s);

    return parsers;
}

/******************************************************************************/

G_DEFINE_TYPE_WITH_CODE (
    EMailParserExtensionRegistry,
    e_mail_parser_extension_registry,
    E_TYPE_MAIL_EXTENSION_REGISTRY,
    G_IMPLEMENT_INTERFACE (E_TYPE_EXTENSIBLE, NULL))

static void
e_mail_parser_extension_registry_class_init (EMailParserExtensionRegistryClass *class)
{
}

static void
e_mail_parser_extension_registry_init (EMailParserExtensionRegistry *registry)
{
}

static gint
mail_parser_extension_registry_compare (gconstpointer extension1,
                                        gconstpointer extension2,
                                        gpointer user_data)
{
    EMailParserExtensionClass *class1;
    EMailParserExtensionClass *class2;

    class1 = E_MAIL_PARSER_EXTENSION_GET_CLASS (extension1);
    class2 = E_MAIL_PARSER_EXTENSION_GET_CLASS (extension2);

    if (class1->priority == class2->priority)
        return 0;

    return (class1->priority < class2->priority) ? -1 : 1;
}

void
e_mail_parser_extension_registry_load (EMailParserExtensionRegistry *registry)
{
    GType *children;
    GType base_extension_type;
    guint ii, n_children;

    g_return_if_fail (E_IS_MAIL_PARSER_EXTENSION_REGISTRY (registry));

    base_extension_type = E_TYPE_MAIL_PARSER_EXTENSION;
    children = g_type_children (base_extension_type, &n_children);

    for (ii = 0; ii < n_children; ii++) {
        EMailParserExtensionClass *class;

        if (G_TYPE_IS_ABSTRACT (children[ii]))
            continue;

        class = g_type_class_ref (children[ii]);

        mail_extension_registry_add_extension (
            E_MAIL_EXTENSION_REGISTRY (registry),
            class->mime_types, children[ii],
            mail_parser_extension_registry_compare);

        g_type_class_unref (class);
    }

    g_free (children);
}

/******************************************************************************/

G_DEFINE_TYPE_WITH_CODE (
    EMailFormatterExtensionRegistry,
    e_mail_formatter_extension_registry,
    E_TYPE_MAIL_EXTENSION_REGISTRY,
    G_IMPLEMENT_INTERFACE (E_TYPE_EXTENSIBLE, NULL))

static void
e_mail_formatter_extension_registry_class_init (EMailFormatterExtensionRegistryClass *class)
{
}

static void
e_mail_formatter_extension_registry_init (EMailFormatterExtensionRegistry *registry)
{
}

static gint
mail_formatter_extension_registry_compare (gconstpointer extension1,
                                           gconstpointer extension2,
                                           gpointer user_data)
{
    EMailFormatterExtensionClass *class1;
    EMailFormatterExtensionClass *class2;

    class1 = E_MAIL_FORMATTER_EXTENSION_GET_CLASS (extension1);
    class2 = E_MAIL_FORMATTER_EXTENSION_GET_CLASS (extension2);

    if (class1->priority == class2->priority)
        return 0;

    return (class1->priority < class2->priority) ? -1 : 1;
}

void
e_mail_formatter_extension_registry_load (EMailFormatterExtensionRegistry *registry,
                                          GType base_extension_type)
{
    GType *children;
    guint ii, n_children;

    g_return_if_fail (E_IS_MAIL_FORMATTER_EXTENSION_REGISTRY (registry));

    children = g_type_children (base_extension_type, &n_children);

    for (ii = 0; ii < n_children; ii++) {
        EMailFormatterExtensionClass *class;

        if (G_TYPE_IS_ABSTRACT (children[ii]))
            continue;

        class = g_type_class_ref (children[ii]);

        mail_extension_registry_add_extension (
            E_MAIL_EXTENSION_REGISTRY (registry),
            class->mime_types, children[ii],
            mail_formatter_extension_registry_compare);

        g_type_class_unref (class);
    }

    g_free (children);
}