~ubuntu-branches/ubuntu/natty/anjal/natty

« back to all changes in this revision

Viewing changes to src/module/e-mail-junk-hook.c

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Klimonda
  • Date: 2010-04-07 01:35:13 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20100407013513-4f23hmwhb00xcylg
Tags: 0.3.1+really0.0.1+git20090909.d0a14f2b-0ubuntu1
Revert anjal to 0.0.1+git20090909.d0a14f2b to get it to build in lucid
(LP: #518788)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * e-mail-junk-hook.c
3
 
 *
4
 
 * This program is free software; you can redistribute it and/or
5
 
 * modify it under the terms of the GNU Lesser General Public
6
 
 * License as published by the Free Software Foundation; either
7
 
 * version 2 of the License, or (at your option) version 3.
8
 
 *
9
 
 * This program is distributed in the hope that it will be useful,
10
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 
 * Lesser General Public License for more details.
13
 
 *
14
 
 * You should have received a copy of the GNU Lesser General Public
15
 
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
16
 
 *
17
 
 *
18
 
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
19
 
 *
20
 
 */
21
 
 
22
 
#include "e-mail-junk-hook.h"
23
 
 
24
 
#include <glib/gi18n.h>
25
 
#include <camel/camel-junk-plugin.h>
26
 
 
27
 
#include "e-util/e-alert-dialog.h"
28
 
#include "shell/e-shell.h"
29
 
 
30
 
#include "mail/em-junk.h"
31
 
#include "mail/em-utils.h"
32
 
#include "mail/mail-session.h"
33
 
 
34
 
#define E_MAIL_JUNK_HOOK_GET_PRIVATE(obj) \
35
 
        (G_TYPE_INSTANCE_GET_PRIVATE \
36
 
        ((obj), E_TYPE_MAIL_JUNK_HOOK, EMailJunkHookPrivate))
37
 
 
38
 
struct _EMailJunkHookPrivate {
39
 
        EMJunkInterface interface;
40
 
};
41
 
 
42
 
struct ErrorData {
43
 
        const gchar *error_message;
44
 
        GError *error;
45
 
};
46
 
 
47
 
static gpointer parent_class;
48
 
static GType mail_junk_hook_type;
49
 
 
50
 
static gboolean
51
 
mail_junk_hook_idle_cb (struct ErrorData *data)
52
 
{
53
 
        GtkWidget *widget;
54
 
 
55
 
        widget = e_alert_dialog_new_for_args (e_shell_get_active_window (NULL),
56
 
                data->error_message, data->error->message, NULL);
57
 
        em_utils_show_error_silent (widget);
58
 
 
59
 
        g_error_free (data->error);
60
 
        g_slice_free (struct ErrorData, data);
61
 
 
62
 
        return FALSE;
63
 
}
64
 
 
65
 
static void
66
 
mail_junk_hook_error (const gchar *error_message,
67
 
                      GError *error)
68
 
{
69
 
        struct ErrorData *data;
70
 
 
71
 
        g_return_if_fail (error != NULL);
72
 
 
73
 
        data = g_slice_new (struct ErrorData);
74
 
        data->error_message = error_message;
75
 
        data->error = error;
76
 
 
77
 
        g_idle_add ((GSourceFunc) mail_junk_hook_idle_cb, data);
78
 
}
79
 
 
80
 
static const gchar *
81
 
mail_junk_hook_get_name (CamelJunkPlugin *junk_plugin)
82
 
{
83
 
        EMJunkInterface *interface;
84
 
 
85
 
        interface = (EMJunkInterface *) junk_plugin;
86
 
 
87
 
        if (!interface->hook->plugin->enabled)
88
 
                return _("None");
89
 
 
90
 
        return interface->hook->plugin->name;
91
 
}
92
 
 
93
 
static void
94
 
mail_junk_hook_plugin_init (CamelJunkPlugin *junk_plugin)
95
 
{
96
 
        EMJunkInterface *interface;
97
 
        EPluginClass *class;
98
 
 
99
 
        interface = (EMJunkInterface *) junk_plugin;
100
 
 
101
 
        class = E_PLUGIN_GET_CLASS (interface->hook->plugin);
102
 
        g_return_if_fail (class->enable != NULL);
103
 
 
104
 
        class->enable (interface->hook->plugin, 1);
105
 
}
106
 
 
107
 
static gboolean
108
 
mail_junk_hook_check_junk (CamelJunkPlugin *junk_plugin,
109
 
                           CamelMimeMessage *mime_message)
110
 
{
111
 
        EMJunkTarget target = { mime_message, NULL };
112
 
        EMJunkInterface *interface;
113
 
        gpointer result;
114
 
 
115
 
        interface = (EMJunkInterface *) junk_plugin;
116
 
 
117
 
        if (!interface->hook->plugin->enabled)
118
 
                return FALSE;
119
 
 
120
 
        result = e_plugin_invoke (
121
 
                interface->hook->plugin,
122
 
                interface->check_junk, &target);
123
 
 
124
 
        if (target.error != NULL)
125
 
                mail_junk_hook_error ("mail:junk-check-error", target.error);
126
 
 
127
 
        return (result != NULL);
128
 
}
129
 
 
130
 
static void
131
 
mail_junk_hook_report_junk (CamelJunkPlugin *junk_plugin,
132
 
                            CamelMimeMessage *mime_message)
133
 
{
134
 
        EMJunkTarget target = { mime_message, NULL };
135
 
        EMJunkInterface *interface;
136
 
 
137
 
        interface = (EMJunkInterface *) junk_plugin;
138
 
 
139
 
        if (!interface->hook->plugin->enabled)
140
 
                return;
141
 
 
142
 
        e_plugin_invoke (
143
 
                interface->hook->plugin,
144
 
                interface->report_junk, &target);
145
 
 
146
 
        if (target.error != NULL)
147
 
                mail_junk_hook_error ("mail:junk-report-error", target.error);
148
 
}
149
 
 
150
 
static void
151
 
mail_junk_hook_report_notjunk (CamelJunkPlugin *junk_plugin,
152
 
                               CamelMimeMessage *mime_message)
153
 
{
154
 
        EMJunkTarget target = { mime_message, NULL };
155
 
        EMJunkInterface *interface;
156
 
 
157
 
        interface = (EMJunkInterface *) junk_plugin;
158
 
 
159
 
        if (!interface->hook->plugin->enabled)
160
 
                return;
161
 
 
162
 
        e_plugin_invoke (
163
 
                interface->hook->plugin,
164
 
                interface->report_notjunk, &target);
165
 
 
166
 
        if (target.error != NULL)
167
 
                mail_junk_hook_error (
168
 
                        "mail:junk-not-report-error", target.error);
169
 
}
170
 
 
171
 
static void
172
 
mail_junk_hook_commit_reports (CamelJunkPlugin *junk_plugin)
173
 
{
174
 
        EMJunkInterface *interface;
175
 
 
176
 
        interface = (EMJunkInterface *) junk_plugin;
177
 
 
178
 
        if (!interface->hook->plugin->enabled)
179
 
                return;
180
 
 
181
 
        e_plugin_invoke (
182
 
                interface->hook->plugin,
183
 
                interface->commit_reports, NULL);
184
 
}
185
 
 
186
 
static void
187
 
mail_junk_hook_finalize (GObject *object)
188
 
{
189
 
        EMailJunkHookPrivate *priv;
190
 
 
191
 
        priv = E_MAIL_JUNK_HOOK_GET_PRIVATE (object);
192
 
 
193
 
        g_free (priv->interface.check_junk);
194
 
        g_free (priv->interface.report_junk);
195
 
        g_free (priv->interface.report_notjunk);
196
 
        g_free (priv->interface.commit_reports);
197
 
        g_free (priv->interface.validate_binary);
198
 
        g_free (priv->interface.plugin_name);
199
 
 
200
 
        /* Chain up to parent's finalize() method. */
201
 
        G_OBJECT_CLASS (parent_class)->finalize (object);
202
 
}
203
 
 
204
 
static gint
205
 
mail_junk_hook_construct (EPluginHook *hook,
206
 
                          EPlugin *plugin,
207
 
                          xmlNodePtr node)
208
 
{
209
 
        EMailJunkHookPrivate *priv;
210
 
        gchar *property;
211
 
 
212
 
        priv = E_MAIL_JUNK_HOOK_GET_PRIVATE (hook);
213
 
 
214
 
        /* Chain up to parent's construct() method. */
215
 
        if (E_PLUGIN_HOOK_CLASS (parent_class)->construct (hook, plugin, node) == -1)
216
 
                return -1;
217
 
 
218
 
        if (!plugin->enabled)
219
 
                return -1;
220
 
 
221
 
        node = xmlFirstElementChild (node);
222
 
 
223
 
        if (node == NULL)
224
 
                return -1;
225
 
 
226
 
        if (g_strcmp0 ((gchar *) node->name, "interface") != 0)
227
 
                return -1;
228
 
 
229
 
        property = e_plugin_xml_prop (node, "check_junk");
230
 
        priv->interface.check_junk = property;
231
 
 
232
 
        property = e_plugin_xml_prop (node, "report_junk");
233
 
        priv->interface.report_junk = property;
234
 
 
235
 
        property = e_plugin_xml_prop (node, "report_non_junk");
236
 
        priv->interface.report_notjunk = property;
237
 
 
238
 
        property = e_plugin_xml_prop (node, "commit_reports");
239
 
        priv->interface.commit_reports = property;
240
 
 
241
 
        property = e_plugin_xml_prop (node, "validate_binary");
242
 
        priv->interface.validate_binary = property;
243
 
 
244
 
        property = e_plugin_xml_prop (node, "name");
245
 
        priv->interface.plugin_name = property;
246
 
 
247
 
        if (priv->interface.check_junk == NULL)
248
 
                return -1;
249
 
 
250
 
        if (priv->interface.report_junk == NULL)
251
 
                return -1;
252
 
 
253
 
        if (priv->interface.report_notjunk == NULL)
254
 
                return -1;
255
 
 
256
 
        if (priv->interface.commit_reports == NULL)
257
 
                return -1;
258
 
 
259
 
        mail_session_add_junk_plugin (
260
 
                priv->interface.plugin_name, &priv->interface.camel);
261
 
 
262
 
        return 0;
263
 
}
264
 
 
265
 
static void
266
 
mail_junk_hook_class_init (EMailJunkHookClass *class)
267
 
{
268
 
        GObjectClass *object_class;
269
 
        EPluginHookClass *plugin_hook_class;
270
 
 
271
 
        parent_class = g_type_class_peek_parent (class);
272
 
        g_type_class_add_private (class, sizeof (EMailJunkHookPrivate));
273
 
 
274
 
        object_class = G_OBJECT_CLASS (class);
275
 
        object_class->finalize = mail_junk_hook_finalize;
276
 
 
277
 
        plugin_hook_class = E_PLUGIN_HOOK_CLASS (class);
278
 
        plugin_hook_class->construct = mail_junk_hook_construct;
279
 
        plugin_hook_class->id = "org.gnome.evolution.mail.junk:1.0";
280
 
}
281
 
 
282
 
static void
283
 
mail_junk_hook_init (EMailJunkHook *mail_junk_hook)
284
 
{
285
 
        EMJunkInterface *interface;
286
 
 
287
 
        mail_junk_hook->priv = E_MAIL_JUNK_HOOK_GET_PRIVATE (mail_junk_hook);
288
 
 
289
 
        interface = &mail_junk_hook->priv->interface;
290
 
        interface->camel.get_name = mail_junk_hook_get_name;
291
 
        interface->camel.api_version = 1;
292
 
        interface->camel.check_junk = mail_junk_hook_check_junk;
293
 
        interface->camel.report_junk = mail_junk_hook_report_junk;
294
 
        interface->camel.report_notjunk = mail_junk_hook_report_notjunk;
295
 
        interface->camel.commit_reports = mail_junk_hook_commit_reports;
296
 
        interface->camel.init = mail_junk_hook_plugin_init;
297
 
        interface->hook = E_PLUGIN_HOOK (mail_junk_hook);
298
 
}
299
 
 
300
 
GType
301
 
e_mail_junk_hook_get_type (void)
302
 
{
303
 
        return mail_junk_hook_type;
304
 
}
305
 
 
306
 
void
307
 
e_mail_junk_hook_register_type (GTypeModule *type_module)
308
 
{
309
 
        const GTypeInfo type_info = {
310
 
                sizeof (EMailJunkHookClass),
311
 
                (GBaseInitFunc) NULL,
312
 
                (GBaseFinalizeFunc) NULL,
313
 
                (GClassInitFunc) mail_junk_hook_class_init,
314
 
                (GClassFinalizeFunc) NULL,
315
 
                NULL,  /* class_data */
316
 
                sizeof (EMailJunkHook),
317
 
                0,     /* n_preallocs */
318
 
                (GInstanceInitFunc) mail_junk_hook_init,
319
 
                NULL   /* value_table */
320
 
        };
321
 
 
322
 
        mail_junk_hook_type = g_type_module_register_type (
323
 
                type_module, E_TYPE_PLUGIN_HOOK,
324
 
                "EMailJunkHook", &type_info, 0);
325
 
}