aboutsummaryrefslogtreecommitdiffstats
path: root/mail/em-filter-source-element.c
blob: 007591d3460bed4337d212e22c31444df783afae (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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
 * 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:
 *      Jon Trowbridge <trow@ximian.com>
 *      Jeffrey Stedfast <fejj@ximian.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

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

#include <string.h>

#include "em-filter-source-element.h"

#include <gtk/gtk.h>
#include <camel/camel.h>

#include <shell/e-shell.h>
#include <filter/e-filter-part.h>

#define EM_FILTER_SOURCE_ELEMENT_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE \
    ((obj), EM_TYPE_FILTER_SOURCE_ELEMENT, EMFilterSourceElementPrivate))

struct _EMFilterSourceElementPrivate {
    EMailSession *session;
    gchar *active_id;
};

G_DEFINE_TYPE (
    EMFilterSourceElement,
    em_filter_source_element,
    E_TYPE_FILTER_ELEMENT)

enum {
    PROP_0,
    PROP_SESSION
};

static void
filter_source_element_source_changed (GtkComboBox *combo_box,
                                      EMFilterSourceElement *fs)
{
    const gchar *active_id;

    active_id = gtk_combo_box_get_active_id (combo_box);

    g_free (fs->priv->active_id);
    fs->priv->active_id = g_strdup (active_id);
}

static void
filter_source_element_set_session (EMFilterSourceElement *element,
                                   EMailSession *session)
{
    g_return_if_fail (E_IS_MAIL_SESSION (session));
    g_return_if_fail (element->priv->session == NULL);

    element->priv->session = g_object_ref (session);
}

static void
filter_source_element_set_property (GObject *object,
                                    guint property_id,
                                    const GValue *value,
                                    GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_SESSION:
            filter_source_element_set_session (
                EM_FILTER_SOURCE_ELEMENT (object),
                g_value_get_object (value));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
filter_source_element_get_property (GObject *object,
                                    guint property_id,
                                    GValue *value,
                                    GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_SESSION:
            g_value_set_object (
                value,
                em_filter_source_element_get_session (
                EM_FILTER_SOURCE_ELEMENT (object)));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
filter_source_element_dispose (GObject *object)
{
    EMFilterSourceElementPrivate *priv;

    priv = EM_FILTER_SOURCE_ELEMENT_GET_PRIVATE (object);

    if (priv->session != NULL) {
        g_object_unref (priv->session);
        priv->session = NULL;
    }

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

static void
filter_source_element_finalize (GObject *object)
{
    EMFilterSourceElementPrivate *priv;

    priv = EM_FILTER_SOURCE_ELEMENT_GET_PRIVATE (object);

    g_free (priv->active_id);

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

static gint
filter_source_element_eq (EFilterElement *fe,
                          EFilterElement *cm)
{
    EMFilterSourceElement *fs = (EMFilterSourceElement *) fe;
    EMFilterSourceElement *cs = (EMFilterSourceElement *) cm;

    return E_FILTER_ELEMENT_CLASS (em_filter_source_element_parent_class)->eq (fe, cm)
        && g_strcmp0 (fs->priv->active_id, cs->priv->active_id) == 0;
}

static xmlNodePtr
filter_source_element_xml_encode (EFilterElement *fe)
{
    xmlNodePtr value;

    EMFilterSourceElement *fs = (EMFilterSourceElement *) fe;

    value = xmlNewNode (NULL, (const guchar *) "value");
    xmlSetProp (value, (const guchar *) "name", (guchar *)fe->name);
    xmlSetProp (value, (const guchar *) "type", (const guchar *) "uid");

    if (fs->priv->active_id != NULL)
        xmlNewTextChild (
            value, NULL, (const guchar *) "uid",
            (guchar *) fs->priv->active_id);

    return value;
}

static gint
filter_source_element_xml_decode (EFilterElement *fe,
                                  xmlNodePtr node)
{
    EMFilterSourceElement *fs = (EMFilterSourceElement *) fe;
    EMailSession *session;
    gchar *active_id = NULL;

    session = em_filter_source_element_get_session (fs);

    node = node->children;
    while (node != NULL) {

        if (strcmp ((gchar *) node->name, "uid") == 0) {
            xmlChar *content;

            content = xmlNodeGetContent (node);
            active_id = g_strdup ((gchar *) content);
            xmlFree (content);

            break;
        }

        /* For backward-compatibility: We used to store
         * sources by their URI string, which can change. */
        if (strcmp ((gchar *)node->name, "uri") == 0) {
            CamelService *service = NULL;
            xmlChar *content;
            CamelURL *url;

            content = xmlNodeGetContent (node);
            url = camel_url_new ((gchar *) content, NULL);
            xmlFree (content);

            if (url != NULL) {
                service = camel_session_get_service_by_url (
                    CAMEL_SESSION (session),
                    url, CAMEL_PROVIDER_STORE);
                camel_url_free (url);
            }

            if (service != NULL) {
                const gchar *uid;

                uid = camel_service_get_uid (service);
                active_id = g_strdup (uid);
            }

            break;
        }

        node = node->next;
    }

    if (active_id != NULL) {
        g_free (fs->priv->active_id);
        fs->priv->active_id = active_id;
    } else
        g_free (active_id);

    return 0;
}

static EFilterElement *
filter_source_element_clone (EFilterElement *fe)
{
    EMFilterSourceElement *fs = (EMFilterSourceElement *) fe;
    EMFilterSourceElement *cpy;
    EMailSession *session;

    session = em_filter_source_element_get_session (fs);
    cpy = (EMFilterSourceElement *) em_filter_source_element_new (session);
    ((EFilterElement *) cpy)->name = (gchar *) xmlStrdup ((guchar *) fe->name);

    cpy->priv->active_id = g_strdup (fs->priv->active_id);

    return (EFilterElement *) cpy;
}

static GtkWidget *
filter_source_element_get_widget (EFilterElement *fe)
{
    EMFilterSourceElement *fs = (EMFilterSourceElement *) fe;
    EMailSession *session;
    ESourceRegistry *registry;
    GList *list, *link;
    GtkWidget *widget;
    GtkComboBox *combo_box;
    const gchar *extension_name;

    widget = gtk_combo_box_text_new ();
    combo_box = GTK_COMBO_BOX (widget);

    session = em_filter_source_element_get_session (fs);
    registry = e_mail_session_get_registry (session);

    extension_name = E_SOURCE_EXTENSION_MAIL_ACCOUNT;
    list = e_source_registry_list_sources (registry, extension_name);

    for (link = list; link != NULL; link = g_list_next (link)) {
        ESource *source = E_SOURCE (link->data);
        ESourceMailIdentity *extension;
        const gchar *display_name;
        const gchar *address;
        const gchar *name;
        const gchar *uid;
        gchar *label;

        uid = e_source_get_uid (source);
        display_name = e_source_get_display_name (source);

        extension_name = E_SOURCE_EXTENSION_MAIL_IDENTITY;
        extension = e_source_get_extension (source, extension_name);

        name = e_source_mail_identity_get_name (extension);
        address = e_source_mail_identity_get_address (extension);

        if (name == NULL || address == NULL)
            continue;

        if (g_strcmp0 (display_name, address) == 0)
            label = g_strdup_printf (
                "%s <%s>", name, address);
        else
            label = g_strdup_printf (
                "%s <%s> (%s)", name,
                address, display_name);

        gtk_combo_box_text_append (
            GTK_COMBO_BOX_TEXT (combo_box), uid, label);

        g_free (label);
    }

    g_list_free_full (list, (GDestroyNotify) g_object_unref);

    if (fs->priv->active_id != NULL) {
        gtk_combo_box_set_active_id (combo_box, fs->priv->active_id);
    } else {
        const gchar *active_id;

        gtk_combo_box_set_active (combo_box, 0);
        active_id = gtk_combo_box_get_active_id (combo_box);

        g_free (fs->priv->active_id);
        fs->priv->active_id = g_strdup (active_id);
    }

    g_signal_connect (
        widget, "changed",
        G_CALLBACK (filter_source_element_source_changed), fs);

    return widget;
}

static void
filter_source_element_build_code (EFilterElement *fe,
                                  GString *out,
                                  EFilterPart *ff)
{
    /* We are doing nothing on purpose. */
}

static void
filter_source_element_format_sexp (EFilterElement *fe,
                                   GString *out)
{
    EMFilterSourceElement *fs = (EMFilterSourceElement *) fe;

    camel_sexp_encode_string (out, fs->priv->active_id);
}

static void
em_filter_source_element_class_init (EMFilterSourceElementClass *class)
{
    GObjectClass *object_class;
    EFilterElementClass *filter_element_class;

    g_type_class_add_private (class, sizeof (EMFilterSourceElementPrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->set_property = filter_source_element_set_property;
    object_class->get_property = filter_source_element_get_property;
    object_class->dispose = filter_source_element_dispose;
    object_class->finalize = filter_source_element_finalize;

    filter_element_class = E_FILTER_ELEMENT_CLASS (class);
    filter_element_class->eq = filter_source_element_eq;
    filter_element_class->xml_encode = filter_source_element_xml_encode;
    filter_element_class->xml_decode = filter_source_element_xml_decode;
    filter_element_class->clone = filter_source_element_clone;
    filter_element_class->get_widget = filter_source_element_get_widget;
    filter_element_class->build_code = filter_source_element_build_code;
    filter_element_class->format_sexp = filter_source_element_format_sexp;

    g_object_class_install_property (
        object_class,
        PROP_SESSION,
        g_param_spec_object (
            "session",
            NULL,
            NULL,
            E_TYPE_MAIL_SESSION,
            G_PARAM_READWRITE |
            G_PARAM_CONSTRUCT_ONLY |
            G_PARAM_STATIC_STRINGS));
}

static void
em_filter_source_element_init (EMFilterSourceElement *element)
{
    element->priv = EM_FILTER_SOURCE_ELEMENT_GET_PRIVATE (element);
}

EFilterElement *
em_filter_source_element_new (EMailSession *session)
{
    g_return_val_if_fail (E_IS_MAIL_SESSION (session), NULL);

    return g_object_new (
        EM_TYPE_FILTER_SOURCE_ELEMENT,
        "session", session, NULL);
}

EMailSession *
em_filter_source_element_get_session (EMFilterSourceElement *element)
{
    g_return_val_if_fail (EM_IS_FILTER_SOURCE_ELEMENT (element), NULL);

    return element->priv->session;
}
ation updated by Ivar Smolin.Priit Laes2006-11-022-6/+12 * Translation updated by Ivar Smolin.Priit Laes2006-10-282-12/+18 * Updated Finnish translationIlkka Tuohela2006-10-262-77/+81 * Translation updated by Ivar Smolin.Priit Laes2006-10-252-8/+14 * Updated Spanish translation.Francisco Javier F. Serrador2006-10-242-120/+215 * Updated French translation.Christophe Merlet2006-10-232-263/+315 * Translation updated.Priit Laes2006-10-222-83/+95 * Updated Nepali TranslationPawan Chitrakar2006-10-202-1580/+1676 * Translation updated.Gabor Kelemen2006-10-202-441/+625 * Translation updated by Ivar Smolin.Priit Laes2006-10-202-43/+48 * Translation updated by Ivar Smolin.Priit Laes2006-10-192-37/+43 * Updated Catalan translation.Josep Puigdemont i Casamajó2006-10-191-5/+5 * Added Catalan translation.Josep Puigdemont i Casamajó2006-10-192-649/+782 * Translation updated by Ivar Smolin.Priit Laes2006-10-192-6/+29 * Updated Finnish translationIlkka Tuohela2006-10-162-177/+218 * Add two files here. Update the translation.Kjartan Maraas2006-10-163-949/+1046 * Translation updated by Ivar Smolin.Priit Laes2006-10-162-4/+10 * 2006-10-15 Jovan Naumovski <jovanna@cvs.gnome.org> *mk.po: Updated Macedonian...Jovan Naumovski2006-10-162-8/+12 * Updated Finnish translationIlkka Tuohela2006-10-152-23/+24 * Updated Spanish translation.Francisco Javier F. Serrador2006-10-152-33/+37 * Updated Finnish translationIlkka Tuohela2006-10-152-9/+13 * Updated Nepali TranslationPawan Chitrakar2006-10-151-2190/+2269 * Updated Swedish Translation and my emailJohan Dahlin2006-10-152-6/+10 * Fixed plurals-related problem.Satoru SATOH2006-10-142-753/+833 * Translation updated by Ivar Smolin.Priit Laes2006-10-142-8/+18 * Updated Bulgarian translation by Alexander Shopov <ash@contact.bg>Alexander Shopov2006-10-142-403/+467 * Updated Italian translation.Luca Ferretti2006-10-112-289/+382 * Updated Lithuanian translation.Žygimantas Beručka2006-10-102-469/+542 * Updated Finnish translationIlkka Tuohela2006-10-092-4/+8 * Small fixes to Catalan translationJordi Mas2006-10-091-6/+5 * Updated French translation.Christophe Merlet2006-10-092-250/+316 * Catalan translation fixesJordi Mas2006-10-082-5/+9 * Translation updated by Ivar Smolin.Priit Laes2006-10-062-2/+6 * Updated Finnish translationIlkka Tuohela2006-10-052-61/+89 * Translation updated by Ivar Smolin.Priit Laes2006-10-052-2/+22 * Translation updated by Tino Meinen.Vincent van Adrighem2006-10-042-405/+471 * Translation updated by Ivar Smolin.Priit Laes2006-10-042-2/+22 * Updated TranslationAnkitkumar Rameshchandra Patel2006-10-042-403/+467 * Updated Swedish translation.Daniel Nylander2006-10-042-65/+91 * Translation updated by Ivar Smolin.Priit Laes2006-10-042-403/+450 * 2006-10-03 Jovan Naumovski <jovanna@cvs.gnome.org> *mk.po: Updated Macedonian...Jovan Naumovski2006-10-041-97/+120 * Updated Spanish translation.Francisco Javier F. Serrador2006-10-032-191/+117 * Make two strings translatable.Francisco Javier F. Serrador2006-10-032-61/+73 * *** empty log message ***Ignacio Casal Quinteiro2006-10-022-2148/+2500 * Updated Finnish translationIlkka Tuohela2006-10-012-389/+432 * Updated Spanish translation.Francisco Javier Fernandez Serrador2006-10-012-233/+275 * Updated Swedish translation.Daniel Nylander2006-09-302-240/+279 * 2006-09-29 Jovan Naumovski <jovanna@cvs.gnome.org> *mk.po: Updated Macedonian...Jovan Naumovski2006-09-292-357/+397 * *** empty log message ***Pema Geyleg2006-09-292-302/+318 * Translation updated by Ivar Smolin.Priit Laes2006-09-282-11/+17 * Updated Norwegian Nynorsk translation.Åsmund Skjæveland2006-09-262-172/+177 * Updated German translation.Hendrik Richter2006-09-242-521/+538 * Updated Russian translation.Nickolay V. Shmyrev2006-09-242-666/+763 * Updated Swedish translation.Daniel Nylander2006-09-242-541/+558 * Updated Korean translation.Changwoo Ryu2006-09-232-401/+419 * Translation updated by Ivar Smolin.Priit Laes2006-09-232-4/+10 * Translation updated by Wouter Bolsterlee.Wouter Bolsterlee2006-09-232-1/+5 * Updated Italian translation.Luca Ferretti2006-09-222-294/+247 * Translation updated.Gabor Kelemen2006-09-212-282/+319 * Updated French translation.Christophe Merlet2006-09-212-229/+248 * Added String for Updated Oriya TranslationSubhransu Behera2006-09-201-0/+4 * Updated Oriya TranslationSubhransu Behera2006-09-201-32/+33 * Updated Nepali TranslationPawan Chitrakar2006-09-202-1456/+1435 * Updated Oriya TranslationSubhransu Behera2006-09-201-101/+101 * Added String for Updated Oriya TranslationSubhransu Behera2006-09-201-0/+4 * Translation updated by Ivar Smolin.Priit Laes2006-09-202-8/+13 * Added String for Updated Oriya TranslationSubhransu Behera2006-09-191-0/+4 * Updated Oriya TranslationSubhransu Behera2006-09-191-53/+61 * Updated Spanish translation.Francisco Javier F. Serrador2006-09-192-526/+541 * Translation updated by Ivar Smolin.Priit Laes2006-09-182-2/+6 * Updated Norwegian Nynorsk translation.Åsmund Skjæveland2006-09-182-77/+91 * Updated Italian translationLuca Ferretti2006-09-172-335/+263 * Translation updated by Ivar Smolin.Priit Laes2006-09-172-5/+9 * Updated Finnish translationIlkka Tuohela2006-09-162-2/+6 * Updated Finnish translationIlkka Tuohela2006-09-162-17/+21 * Updated Bulgarian translation by Alexander Shopov <ash@contact.bg>Alexander Shopov2006-09-162-163/+182 * Updated English (British) translationDavid Lodge2006-09-162-294/+325 * Translation updated by Ivar Smolin.Priit Laes2006-09-162-158/+177 * 2006-09-15 Jovan Naumovski <jovanna@cvs.gnome.org> * mk.po: Updated Macedonia...Jovan Naumovski2006-09-152-104/+120 * Translation updated by Wouter Bolsterlee.Wouter Bolsterlee2006-09-152-447/+467 * Updated Finnish translationIlkka Tuohela2006-09-152-154/+172 * Updated TranslationAnkitkumar Rameshchandra Patel2006-09-152-466/+482 * Updated Georgian translationVladimer Sichinava2006-09-142-5827/+7422 * Translation updated by Ivar Smolin.Priit Laes2006-09-132-6/+12 * Translation updated by Ivar Smolin.Priit Laes2006-09-122-2/+6 * Updated Greek translationKostas Papadimas2006-09-112-12/+16 * Translation updated by Ivar Smolin.Priit Laes2006-09-102-292/+35 * Updated English (British) translation.David Lodge2006-09-102-5095/+6992 * bn.po: Updated Bengali Translation from bn_INJamil Ahmed2006-09-092-4903/+6456 * *** empty log message ***Pema Geyleg2006-09-07