aboutsummaryrefslogtreecommitdiffstats
path: root/mail/em-format-hook.c
blob: 7ceeb20e4d1ae7d535c279e1b332246e3f83445b (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
/*
 * 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/>
 *
 *
 * Authors:
 *      Michael Zucchi <notzed@ximian.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>
#include <stdlib.h>

#include "em-format-hook.h"

#include <glib/gi18n.h>

/* class name -> class map for EMFormat and subclasses */
static GHashTable *emfh_types;

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

/* Mail formatter handler plugin */

/*
 * <hook class="org.gnome.evolution.mail.format:1.0">
 * <group id="EMFormatHTML">
 *   <item flags="inline,inline_disposition"
 *         mime_type="text/vcard"
 *         format="format_vcard"/>
 * </group>
 * </hook>
 */

#define emfh ((EMFormatHook *)eph)

#define d(x)

static const EPluginHookTargetKey emfh_flag_map[] = {
    { "inline", EM_FORMAT_HANDLER_INLINE },
    { "inline_disposition", EM_FORMAT_HANDLER_INLINE_DISPOSITION },
    { NULL }
};

G_DEFINE_TYPE (EMFormatHook, em_format_hook, E_TYPE_PLUGIN_HOOK)

static void
emfh_parse_part (EMFormat *emf,
                 CamelMimePart *part,
                 GString *part_id,
                 EMFormatParserInfo *info,
                 GCancellable *cancellable)
{
    struct _EMFormatHookItem *item = (EMFormatHookItem *) info->handler;

    if (item->hook->hook.plugin->enabled) {
        EMFormatHookTarget target = {
            emf, part, part_id, info
        };

        e_plugin_invoke (item->hook->hook.plugin, item->format, &target);
    } else if (info->handler->old) {
        info->handler->old->parse_func (
            emf, part, part_id, info, cancellable);
    }
}

static void
emfh_free_item (struct _EMFormatHookItem *item)
{
    /* FIXME: remove from formatter class */

    g_free (item->handler.mime_type);
    g_free (item->format);
    g_free (item);
}

static void
emfh_free_group (struct _EMFormatHookGroup *group)
{
    g_slist_foreach (group->items, (GFunc) emfh_free_item, NULL);
    g_slist_free (group->items);

    g_free (group->id);
    g_free (group);
}

static struct _EMFormatHookItem *
emfh_construct_item (EPluginHook *eph,
                     EMFormatHookGroup *group,
                     xmlNodePtr root)
{
    struct _EMFormatHookItem *item;

    d(printf("  loading group item\n"));
    item = g_malloc0 (sizeof (*item));

    item->handler.mime_type = e_plugin_xml_prop(root, "mime_type");
    item->handler.flags = e_plugin_hook_mask(root, emfh_flag_map, "flags");
    item->format = e_plugin_xml_prop(root, "format");

    item->handler.parse_func = emfh_parse_part;
    item->hook = emfh;

    if (item->handler.mime_type == NULL || item->format == NULL)
        goto error;

    d(printf("   type='%s' format='%s'\n", item->handler.mime_type, item->format));

    return item;
error:
    d(printf("error!\n"));
    emfh_free_item (item);
    return NULL;
}

static struct _EMFormatHookGroup *
emfh_construct_group (EPluginHook *eph,
                      xmlNodePtr root)
{
    struct _EMFormatHookGroup *group;
    xmlNodePtr node;

    d(printf(" loading group\n"));
    group = g_malloc0 (sizeof (*group));

    group->id = e_plugin_xml_prop(root, "id");
    if (group->id == NULL)
        goto error;

    node = root->children;
    while (node) {
        if (0 == strcmp((gchar *)node->name, "item")) {
            struct _EMFormatHookItem *item;

            item = emfh_construct_item (eph, group, node);
            if (item)
                group->items = g_slist_append (group->items, item);
        }
        node = node->next;
    }

    return group;
error:
    emfh_free_group (group);
    return NULL;
}

static gint
emfh_construct (EPluginHook *eph,
                EPlugin *ep,
                xmlNodePtr root)
{
    xmlNodePtr node;

    d(printf("loading format hook\n"));

    if (((EPluginHookClass *) em_format_hook_parent_class)->
        construct (eph, ep, root) == -1)
        return -1;

    node = root->children;
    while (node) {
        if (strcmp((gchar *)node->name, "group") == 0) {
            struct _EMFormatHookGroup *group;

            group = emfh_construct_group (eph, node);
            if (group) {
                EMFormatClass *class;

                if (emfh_types
                    && (class = g_hash_table_lookup (emfh_types, group->id))) {
                    GSList *l = group->items;

                    for (; l; l = g_slist_next (l)) {
                        EMFormatHookItem *item = l->data;
                        /* TODO: only add handlers if enabled? */
                        /* Well, disabling is handled by the callback,
                         * if we leave as is, then we can enable the
                         * plugin after startup and it will start
                         * working automagically */
                        em_format_class_add_handler (class, &item->handler);
                    }
                }
                /* We don't actually need to keep this
                 * around once its set on the class. */
                emfh->groups = g_slist_append (emfh->groups, group);
            }
        }
        node = node->next;
    }

    eph->plugin = ep;

    /* Load the plugin as it does a few thing in the formatter thread. */
    ((EPluginClass *) G_OBJECT_GET_CLASS (ep))->enable (ep, 1);
    return 0;
}

static void
emfh_enable (EPluginHook *eph,
             gint state)
{
    GSList *g, *l;
    EMFormatClass *class;

    g = emfh->groups;
    if (emfh_types == NULL)
        return;

    for (; g; g = g_slist_next (g)) {
        struct _EMFormatHookGroup *group = g->data;

        class = g_hash_table_lookup (emfh_types, group->id);
        for (l = group->items; l; l = g_slist_next (l)) {
            EMFormatHookItem *item = l->data;

            if (state)
                em_format_class_add_handler (class, &item->handler);
            else
                em_format_class_remove_handler (class, &item->handler);
        }
    }
}

static void
format_hook_finalize (GObject *object)
{
    EPluginHook *eph = (EPluginHook *) object;

    g_slist_foreach (emfh->groups, (GFunc) emfh_free_group, NULL);
    g_slist_free (emfh->groups);

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

static void
em_format_hook_class_init (EMFormatHookClass *class)
{
    GObjectClass *object_class;
    EPluginHookClass *hook_class;

    object_class = G_OBJECT_CLASS (class);
    object_class->finalize = format_hook_finalize;

    hook_class = E_PLUGIN_HOOK_CLASS (class);
    hook_class->construct = emfh_construct;
    hook_class->enable = emfh_enable;
    hook_class->id = "org.gnome.evolution.mail.format:1.0";
}

static void
em_format_hook_init (EMFormatHook *hook)
{
}

void
em_format_hook_register_type (GType type)
{
    EMFormatClass *class;

    if (emfh_types == NULL)
        emfh_types = g_hash_table_new (g_str_hash, g_str_equal);

    d(printf("registering formatter type '%s'\n", g_type_name(type)));

    class = g_type_class_ref (type);
    g_hash_table_insert (emfh_types, (gpointer) g_type_name (type), class);
}