aboutsummaryrefslogtreecommitdiffstats
path: root/filter/filter-folder.c
blob: 4b43997c2be4bdad00b1fdb822d752c32f49465e (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
/*
 *  Copyright (C) 2000 Ximian Inc.
 *
 *  Authors: Not Zed <notzed@lostzed.mmc.com.au>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public License
 *  as published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *
 *  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 Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <config.h>

#define SHELL

#include <glib.h>
#include <libgnome/gnome-defs.h>
#include <libgnome/gnome-i18n.h>
#include <libgnomeui/gnome-dialog.h>
#include <libgnomeui/gnome-dialog-util.h>
#include <gal/widgets/e-unicode.h>

#include "filter-folder.h"
#ifdef SHELL
#include "shell/evolution-shell-client.h"
#endif
#include "e-util/e-sexp.h"

#define d(x)

static gboolean validate (FilterElement *fe);
static void xml_create(FilterElement *fe, xmlNodePtr node);
static xmlNodePtr xml_encode(FilterElement *fe);
static int xml_decode(FilterElement *fe, xmlNodePtr node);
static GtkWidget *get_widget(FilterElement *fe);
static void build_code(FilterElement *fe, GString *out, struct _FilterPart *ff);
static void format_sexp(FilterElement *, GString *);

#ifdef SHELL
extern EvolutionShellClient *global_shell_client;
#endif

static void filter_folder_class_init    (FilterFolderClass *class);
static void filter_folder_init  (FilterFolder *gspaper);
static void filter_folder_finalise  (GtkObject *obj);

#define _PRIVATE(x) (((FilterFolder *)(x))->priv)

struct _FilterFolderPrivate {
};

static FilterElementClass *parent_class;

guint
filter_folder_get_type (void)
{
    static guint type = 0;
    
    if (!type) {
        GtkTypeInfo type_info = {
            "FilterFolder",
            sizeof(FilterFolder),
            sizeof(FilterFolderClass),
            (GtkClassInitFunc)filter_folder_class_init,
            (GtkObjectInitFunc)filter_folder_init,
            (GtkArgSetFunc)NULL,
            (GtkArgGetFunc)NULL
        };
        
        type = gtk_type_unique (filter_element_get_type (), &type_info);
    }
    
    return type;
}

static void
filter_folder_class_init (FilterFolderClass *class)
{
    GtkObjectClass *object_class;
    FilterElementClass *filter_element = (FilterElementClass *)class;
    
    object_class = (GtkObjectClass *)class;
    parent_class = gtk_type_class (filter_element_get_type ());

    object_class->finalize = filter_folder_finalise;

    /* override methods */
    filter_element->validate = validate;
    filter_element->xml_create = xml_create;
    filter_element->xml_encode = xml_encode;
    filter_element->xml_decode = xml_decode;
    filter_element->get_widget = get_widget;
    filter_element->build_code = build_code;
    filter_element->format_sexp = format_sexp;
}

static void
filter_folder_init (FilterFolder *o)
{
    o->priv = g_malloc0 (sizeof (*o->priv));
}

static void
filter_folder_finalise (GtkObject *obj)
{
    FilterFolder *o = (FilterFolder *)obj;
    
    g_free (o->uri);
    g_free (o->name);
    
        ((GtkObjectClass *)(parent_class))->finalize(obj);
}

/**
 * filter_folder_new:
 *
 * Create a new FilterFolder object.
 * 
 * Return value: A new #FilterFolder object.
 **/
FilterFolder *
filter_folder_new (void)
{
    FilterFolder *o = (FilterFolder *)gtk_type_new (filter_folder_get_type ());
    return o;
}

static gboolean
validate (FilterElement *fe)
{
    FilterFolder *ff = (FilterFolder *) fe;
    
    if (ff->uri && *ff->uri) {
        return TRUE;
    } else {
        GtkWidget *dialog;
        
        dialog = gnome_ok_dialog (_("Oops, you forgot to choose a folder.\n"
                        "Please go back and specify a valid folder to deliver mail to."));
        
        gnome_dialog_run_and_close (GNOME_DIALOG (dialog));
        
        return FALSE;
    }
}

static void
xml_create (FilterElement *fe, xmlNodePtr node)
{
    /* parent implementation */
        ((FilterElementClass *)(parent_class))->xml_create(fe, node);
}

static xmlNodePtr
xml_encode (FilterElement *fe)
{
    xmlNodePtr value, work;
    FilterFolder *ff = (FilterFolder *)fe;
    
    d(printf ("Encoding folder as xml\n"));
    
    value = xmlNewNode (NULL, "value");
    xmlSetProp (value, "name", fe->name);
    xmlSetProp (value, "type", "folder");
    
    work = xmlNewChild (value, NULL, "folder", NULL);
    xmlSetProp (work, "name", ff->name);
    xmlSetProp (work, "uri", ff->uri);
    
    return value;
}

static int
xml_decode (FilterElement *fe, xmlNodePtr node)
{
    FilterFolder *ff = (FilterFolder *)fe;
    xmlNodePtr n;
    
    d(printf ("Decoding folder from xml %p\n", fe));
    
    xmlFree (fe->name);
    fe->name = xmlGetProp (node, "name");
    
    n = node->childs;
    while (n) {
        if (!strcmp (n->name, "folder")) {
            xmlFree (ff->name);
            xmlFree (ff->uri);
            ff->name = xmlGetProp (n, "name");
            ff->uri = xmlGetProp (n, "uri");
            break;
        }
        n = n->next;
    }
    
    return 0;
}

static void
button_clicked (GtkButton *button, FilterFolder *ff)
{
#ifdef SHELL
    const char *allowed_types[] = { "mail", NULL };
    char *def, *physical_uri, *evolution_uri;
    static gboolean is_active = FALSE;
    gchar *s;
    
    if (is_active)
        return;
    
    is_active = TRUE;
    
    def = ff->uri ? ff->uri : "";
    
    evolution_shell_client_user_select_folder (global_shell_client,
                           _("Select Folder"),
                           def, allowed_types,
                           &evolution_uri,
                           &physical_uri);
    
    if (physical_uri != NULL && physical_uri[0] != '\0') {
        g_free (ff->uri);
        ff->uri = physical_uri;
        
        g_free (ff->name);
        ff->name = g_strdup (g_basename (evolution_uri));
        s = e_utf8_to_gtk_string (GTK_WIDGET (button), ff->name);
        gtk_label_set_text (GTK_LABEL (GTK_BIN (button)->child), s);
        g_free (s);
    } else {
        g_free (physical_uri);
    }
    g_free (evolution_uri);
    
    is_active = FALSE;
#else
    GnomeDialog *gd;
    GtkEntry *entry;
    char *uri, *str;

    gd = (GnomeDialog *)gnome_dialog_new(_("Enter folder URI"),
                         GNOME_STOCK_BUTTON_OK, GNOME_STOCK_BUTTON_CANCEL,
                         NULL);
    gtk_window_set_policy(GTK_WINDOW(gd), FALSE, TRUE, FALSE);
    entry = (GtkEntry *)gtk_entry_new();
    if (ff->uri) {
        e_utf8_gtk_entry_set_text(entry, ff->uri);
    }
    gtk_box_pack_start((GtkBox *)gd->vbox, (GtkWidget *)entry, TRUE, TRUE, 3);
    gtk_widget_show((GtkWidget *)entry);
    switch (gnome_dialog_run(gd)) {
    case 0:
        g_free(ff->uri);
        g_free(ff->name);
        uri = e_utf8_gtk_entry_get_text(entry);
        ff->uri = uri;
        str = strstr(uri, "//");
        if (str)
            str = strchr(str+2, '/');
        if (str)
            str++;
        else
            str = uri;
        ff->name = g_strdup(str);
        s = e_utf8_to_gtk_string ((GtkWidget *) button, ff->name);
        gtk_label_set_text((GtkLabel *)GTK_BIN(button)->child, s);
        g_free (s);
    case 1:
        gnome_dialog_close(gd);
    case -1:
        /* nothing */
    }

#endif
}

static GtkWidget *
get_widget (FilterElement *fe)
{
    FilterFolder *ff = (FilterFolder *)fe;
    GtkWidget *button;
    GtkWidget *label;
    
    if (ff->name && ff->name[0])
        label = gtk_label_new (g_basename (ff->name));
    else
        label = gtk_label_new (_("<click here to select a folder>"));
    
    button = gtk_button_new ();
    gtk_container_add (GTK_CONTAINER (button), label);
    gtk_widget_show (button);
    gtk_widget_show (label);
    gtk_signal_connect (GTK_OBJECT (button), "clicked", button_clicked, ff);
    
    return button;
}

static void
build_code (FilterElement *fe, GString *out, struct _FilterPart *ff)
{
    return;
}

static void
format_sexp (FilterElement *fe, GString *out)
{
    FilterFolder *ff = (FilterFolder *)fe;
    
    e_sexp_encode_string (out, ff->uri);
}