aboutsummaryrefslogtreecommitdiffstats
path: root/mail/em-filter-folder-element.c
blob: 502ec5d45f26024c4856b6f00c5eb3e615a8a588 (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
/*
 *
 * 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:
 *      Not Zed <notzed@lostzed.mmc.com.au>
 *      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 <gtk/gtk.h>
#include <glib/gi18n.h>

#include "em-filter-folder-element.h"
#include "mail/em-folder-selection-button.h"
#include "mail/em-utils.h"
#include "shell/e-shell.h"
#include "filter/e-filter-part.h"
#include "libedataserver/e-sexp.h"
#include "e-util/e-alert.h"

#define EM_FILTER_FOLDER_ELEMENT_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE \
    ((obj), EM_TYPE_FILTER_FOLDER_ELEMENT, EMFilterFolderElementPrivate))

struct _EMFilterFolderElementPrivate {
    EMailBackend *backend;
    gchar *uri;
};

enum {
    PROP_0,
    PROP_BACKEND
};

G_DEFINE_TYPE (
    EMFilterFolderElement,
    em_filter_folder_element,
    E_TYPE_FILTER_ELEMENT)

static void
filter_folder_element_selected_cb (EMFolderSelectionButton *button,
                                   EMFilterFolderElement *ff)
{
    GtkWidget *toplevel;
    const gchar *uri;

    uri = em_folder_selection_button_get_selection (button);

    g_free (ff->priv->uri);
    ff->priv->uri = g_strdup (uri);

    toplevel = gtk_widget_get_toplevel (GTK_WIDGET (button));
    gtk_window_present (GTK_WINDOW (toplevel));
}

static void
filter_folder_element_set_backend (EMFilterFolderElement *element,
                                   EMailBackend *backend)
{
    /* FIXME Dirty hack.  Backend should be passed in always. */
    if (backend == NULL) {
        EShellBackend *shell_backend;
        EShell *shell;

        shell = e_shell_get_default ();
        shell_backend = e_shell_get_backend_by_name (shell, "mail");
        backend = E_MAIL_BACKEND (shell_backend);
    }

    g_return_if_fail (E_IS_MAIL_BACKEND (backend));
    g_return_if_fail (element->priv->backend == NULL);

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

static void
filter_folder_element_set_property (GObject *object,
                                    guint property_id,
                                    const GValue *value,
                                    GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_BACKEND:
            filter_folder_element_set_backend (
                EM_FILTER_FOLDER_ELEMENT (object),
                g_value_get_object (value));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
filter_folder_element_get_property (GObject *object,
                                    guint property_id,
                                    GValue *value,
                                    GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_BACKEND:
            g_value_set_object (
                value,
                em_filter_folder_element_get_backend (
                EM_FILTER_FOLDER_ELEMENT (object)));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
filter_folder_element_dispose (GObject *object)
{
    EMFilterFolderElementPrivate *priv;

    priv = EM_FILTER_FOLDER_ELEMENT_GET_PRIVATE (object);

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

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

static void
filter_folder_element_finalize (GObject *object)
{
    EMFilterFolderElementPrivate *priv;

    priv = EM_FILTER_FOLDER_ELEMENT_GET_PRIVATE (object);

    g_free (priv->uri);

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

static gboolean
filter_folder_element_validate (EFilterElement *fe,
                                EAlert **alert)
{
    EMFilterFolderElement *ff = (EMFilterFolderElement *) fe;

    g_warn_if_fail (alert == NULL || *alert == NULL);

    if (ff->priv->uri != NULL && *ff->priv->uri != '\0')
        return TRUE;

    if (alert)
        *alert = e_alert_new ("mail:no-folder", NULL);

    return FALSE;
}

static gint
filter_folder_element_eq (EFilterElement *fe,
                          EFilterElement *cm)
{
    return E_FILTER_ELEMENT_CLASS (
        em_filter_folder_element_parent_class)->eq (fe, cm) &&
        strcmp (((EMFilterFolderElement *) fe)->priv->uri,
        ((EMFilterFolderElement *) cm)->priv->uri)== 0;
}

static xmlNodePtr
filter_folder_element_xml_encode (EFilterElement *fe)
{
    xmlNodePtr value, work;
    EMFilterFolderElement *ff = (EMFilterFolderElement *) fe;

    value = xmlNewNode (NULL, (xmlChar *) "value");
    xmlSetProp (value, (xmlChar *) "name", (xmlChar *) fe->name);
    xmlSetProp (value, (xmlChar *) "type", (xmlChar *) "folder");

    work = xmlNewChild (value, NULL, (xmlChar *) "folder", NULL);
    xmlSetProp (work, (xmlChar *) "uri", (xmlChar *) ff->priv->uri);

    return value;
}

static gint
filter_folder_element_xml_decode (EFilterElement *fe,
                                  xmlNodePtr node)
{
    EMFilterFolderElement *ff = (EMFilterFolderElement *) fe;
    xmlNodePtr n;

    xmlFree (fe->name);
    fe->name = (gchar *) xmlGetProp(node, (xmlChar *) "name");

    n = node->children;
    while (n) {
        if (!strcmp((gchar *) n->name, "folder")) {
            gchar *uri;

            uri = (gchar *) xmlGetProp(n, (xmlChar *) "uri");
            g_free (ff->priv->uri);
            ff->priv->uri = g_strdup (uri);
            xmlFree (uri);
            break;
        }
        n = n->next;
    }

    return 0;
}

static GtkWidget *
filter_folder_element_get_widget (EFilterElement *fe)
{
    EMFilterFolderElement *ff = (EMFilterFolderElement *) fe;
    EMailBackend *backend;
    GtkWidget *button;

    backend = em_filter_folder_element_get_backend (ff);

    button = em_folder_selection_button_new (
        backend, _("Select Folder"), NULL);
    em_folder_selection_button_set_selection (
        EM_FOLDER_SELECTION_BUTTON (button), ff->priv->uri);
    gtk_widget_show (button);

    g_signal_connect (
        button, "selected",
        G_CALLBACK (filter_folder_element_selected_cb), ff);

    return button;
}

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

static void
filter_folder_element_format_sexp (EFilterElement *fe,
                                   GString *out)
{
    EMFilterFolderElement *ff = (EMFilterFolderElement *) fe;

    e_sexp_encode_string (out, ff->priv->uri);
}

static void
filter_folder_element_copy_value (EFilterElement *de,
                                  EFilterElement *se)
{
    if (EM_IS_FILTER_FOLDER_ELEMENT (se)) {
        em_filter_folder_element_set_uri (
            EM_FILTER_FOLDER_ELEMENT (de),
            EM_FILTER_FOLDER_ELEMENT (se)->priv->uri);
    } else {
        E_FILTER_ELEMENT_CLASS (
        em_filter_folder_element_parent_class)->copy_value (de, se);
    }
}
static void
em_filter_folder_element_class_init (EMFilterFolderElementClass *class)
{
    GObjectClass *object_class;
    EFilterElementClass *filter_element_class;

    g_type_class_add_private (class, sizeof (EMFilterFolderElementPrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->set_property = filter_folder_element_set_property;
    object_class->get_property = filter_folder_element_get_property;
    object_class->dispose = filter_folder_element_dispose;
    object_class->finalize = filter_folder_element_finalize;

    filter_element_class = E_FILTER_ELEMENT_CLASS (class);
    filter_element_class->validate = filter_folder_element_validate;
    filter_element_class->eq = filter_folder_element_eq;
    filter_element_class->xml_encode = filter_folder_element_xml_encode;
    filter_element_class->xml_decode = filter_folder_element_xml_decode;
    filter_element_class->get_widget = filter_folder_element_get_widget;
    filter_element_class->build_code = filter_folder_element_build_code;
    filter_element_class->format_sexp = filter_folder_element_format_sexp;
    filter_element_class->copy_value = filter_folder_element_copy_value;

    g_object_class_install_property (
        object_class,
        PROP_BACKEND,
        g_param_spec_object (
            "backend",
            NULL,
            NULL,
            E_TYPE_MAIL_BACKEND,
            G_PARAM_READWRITE |
            G_PARAM_CONSTRUCT_ONLY));
}

static void
em_filter_folder_element_init (EMFilterFolderElement *element)
{
    element->priv = EM_FILTER_FOLDER_ELEMENT_GET_PRIVATE (element);
}

EFilterElement *
em_filter_folder_element_new (EMailBackend *backend)
{
    g_return_val_if_fail (E_IS_MAIL_BACKEND (backend), NULL);

    return g_object_new (
        EM_TYPE_FILTER_FOLDER_ELEMENT,
        "backend", backend, NULL);
}

EMailBackend *
em_filter_folder_element_get_backend (EMFilterFolderElement *element)
{
    g_return_val_if_fail (EM_IS_FILTER_FOLDER_ELEMENT (element), NULL);

    return element->priv->backend;
}

const gchar *
em_filter_folder_element_get_uri (EMFilterFolderElement *element)
{
    g_return_val_if_fail (EM_IS_FILTER_FOLDER_ELEMENT (element), NULL);

    return element->priv->uri;
}

void
em_filter_folder_element_set_uri (EMFilterFolderElement *element,
                                  const gchar *uri)
{
    g_return_if_fail (EM_IS_FILTER_FOLDER_ELEMENT (element));

    g_free (element->priv->uri);
    element->priv->uri = g_strdup (uri);
}