aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/imap-features/imap-headers.c
blob: e3d20334b9b2f343abff635c1555683e37226f4e (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
/*
 * 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:
 *      Sankar P <psankar@novell.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

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

#include <string.h>

#include <mail/em-config.h>
#include <mail/mail-config.h>

#include <gtk/gtk.h>

#include <libedataserver/e-account.h>
#include <libedataserver/e-account-list.h>

#include <camel/camel-url.h>
#include <camel/camel-exception.h>

#include <glade/glade.h>

#include <glib/gi18n.h>

typedef struct _epif_data EPImapFeaturesData;
struct _epif_data {
    GtkWidget *all_headers;
    GtkWidget *basic_headers;
    GtkWidget *mailing_list_headers;
    GtkWidget *custom_headers_box;

    GtkEntry *entry_header;

    GtkButton *add_header;
    GtkButton *remove_header;

    GtkTreeView *custom_headers_tree;
    GtkTreeStore *store;

    gchar **custom_headers_array;
};

static EPImapFeaturesData *ui = NULL;

void imap_headers_abort (EPlugin *efp, EConfigHookItemFactoryData *data);
void imap_headers_commit (EPlugin *efp, EConfigHookItemFactoryData *data);
GtkWidget * org_gnome_imap_headers (EPlugin *epl, EConfigHookItemFactoryData *data);

void
imap_headers_abort (EPlugin *efp, EConfigHookItemFactoryData *data)
{
    /* Nothing to do here */
}

void
imap_headers_commit (EPlugin *efp, EConfigHookItemFactoryData *data)
{
    EMConfigTargetAccount *target_account;
    EAccount *account;
    gboolean use_imap = g_getenv ("USE_IMAP") != NULL;

    target_account = (EMConfigTargetAccount *)data->config->target;
    account = target_account->account;

    if (g_str_has_prefix (account->source->url, "imap://") ||
            (use_imap && g_str_has_prefix (account->source->url, "groupwise://"))) {
        EAccount *temp = NULL;
        EAccountList *accounts = mail_config_get_accounts ();
        CamelURL *url = NULL;
        CamelException ex;
        GtkTreeModel *model;
        GtkTreeIter iter;
        GString *str;
        gchar *header = NULL;

        str = g_string_new("");

        temp = mail_config_get_account_by_source_url (account->source->url);

        url = camel_url_new (e_account_get_string(account, E_ACCOUNT_SOURCE_URL), &ex);

        model = gtk_tree_view_get_model (ui->custom_headers_tree);
        if (gtk_tree_model_get_iter_first(model, &iter)) {
            do
            {
                header = NULL;
                gtk_tree_model_get (model, &iter, 0, &header, -1);
                str = g_string_append (str, g_strstrip(header));
                str = g_string_append (str, " ");
                g_free (header);
            } while (gtk_tree_model_iter_next(model, &iter));
        }

        header = g_strstrip(g_strdup(str->str));
        camel_url_set_param (url, "imap_custom_headers", header);
        g_free (header);

        if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ui->all_headers))) {
            camel_url_set_param (url, "all_headers", "1");
            camel_url_set_param (url, "basic_headers", NULL);
        } else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ui->basic_headers))) {
            camel_url_set_param (url, "basic_headers", "1");
            camel_url_set_param (url, "all_headers", NULL);
        } else {
            camel_url_set_param (url, "all_headers", NULL);
            camel_url_set_param (url, "basic_headers", NULL);
        }

        e_account_set_string (temp, E_ACCOUNT_SOURCE_URL, camel_url_to_string (url, 0));
        camel_url_free (url);
        g_string_free (str, TRUE);
        e_account_list_change (accounts, temp);
        e_account_list_save (accounts);
    }
}

/* return true is the header is considered valid */
static gboolean
epif_header_is_valid (const char *header)
{
    gint len = g_utf8_strlen (header, -1);

    if (header[0] == 0
        || g_utf8_strchr (header, len, ':') != NULL
        || g_utf8_strchr (header, len, ' ') != NULL)
        return FALSE;

    return TRUE;
}

static void
epif_add_sensitivity (EPImapFeaturesData *ui)
{
    const char *entry_contents;
    GtkTreeIter iter;
    gboolean valid;

    /* the add header button should be sensitive if the text box contains
     * a valid header string, that is not a duplicate with something already
     * in the list view */
    entry_contents = gtk_entry_get_text (GTK_ENTRY (ui->entry_header));
    if (!epif_header_is_valid (entry_contents)) {
        gtk_widget_set_sensitive (GTK_WIDGET (ui->add_header), FALSE);
        return;
    }

    /* check if this is a duplicate */
    valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (ui->store), &iter);
    while (valid) {
        char *header_name;

        gtk_tree_model_get (GTK_TREE_MODEL (ui->store), &iter,
                            0, &header_name,
                            -1);
        if (g_ascii_strcasecmp (header_name, entry_contents) == 0) {
            gtk_widget_set_sensitive (GTK_WIDGET (ui->add_header), FALSE);
            return;
        }

        valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (ui->store), &iter);
    }

    gtk_widget_set_sensitive (GTK_WIDGET (ui->add_header), TRUE);
}

static void
epif_add_header (GtkButton *button, EPImapFeaturesData *ui)
{
    GtkTreeModel *model;
    GtkTreeIter iter, first;

    model = gtk_tree_view_get_model (ui->custom_headers_tree);
    gtk_tree_store_append (GTK_TREE_STORE(model), &iter, NULL);
    gtk_tree_store_set (GTK_TREE_STORE(model), &iter, 0, gtk_entry_get_text (ui->entry_header), -1);

    if (gtk_tree_model_get_iter_first (model, &first)!=FALSE)
        gtk_widget_set_sensitive (GTK_WIDGET (ui->remove_header), TRUE);

    gtk_entry_set_text (ui->entry_header, "");
    epif_add_sensitivity (ui);
}

static void
epif_remove_header_clicked (GtkButton *button, EPImapFeaturesData *ui)
{
    GtkTreeSelection *select;
    GtkTreeModel *model;
    GtkTreeIter iter, first;
    GtkTreePath *path;
    gboolean valid = TRUE;

    select = gtk_tree_view_get_selection (ui->custom_headers_tree);

    if (gtk_tree_selection_get_selected (select, &model, &iter))
    {
        path = gtk_tree_model_get_path (model, &iter);
        gtk_tree_store_remove(GTK_TREE_STORE(model), &iter);

        if (gtk_tree_path_prev (path)) {
            gtk_tree_model_get_iter (model, &iter, path);
        } else {
            valid = gtk_tree_model_get_iter_first (model, &iter);
        }

        if (valid)
            gtk_tree_selection_select_iter (select, &iter);
    }

    if (gtk_tree_model_get_iter_first (model, &first)==FALSE)
        gtk_widget_set_sensitive (GTK_WIDGET (button), FALSE);

    epif_add_sensitivity (ui);
}

static void
epif_fetch_all_headers_toggled (GtkWidget *all_option, EPImapFeaturesData *ui)
{
    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(all_option)))
        gtk_widget_set_sensitive (ui->custom_headers_box, FALSE);
    else
        gtk_widget_set_sensitive (ui->custom_headers_box, TRUE);
}

static void
epif_entry_changed (GtkWidget *entry, EPImapFeaturesData *ui)
{
    epif_add_sensitivity (ui);
}

GtkWidget *
org_gnome_imap_headers (EPlugin *epl, EConfigHookItemFactoryData *data)
{
    EMConfigTargetAccount *target_account;
    EAccount *account;
    GtkWidget *vbox;
    CamelURL *url = NULL;
    CamelException ex;
    char *gladefile;
    GladeXML *gladexml;
    GtkCellRenderer *renderer;
    GtkTreeViewColumn *column;
    GtkTreeIter first, iter;
    gboolean use_imap = g_getenv ("USE_IMAP") != NULL;
    
    ui = g_new0 (EPImapFeaturesData, 1);

    target_account = (EMConfigTargetAccount *)data->config->target;
    account = target_account->account;

    if(!g_str_has_prefix (account->source->url, "imap://") && !(use_imap && g_str_has_prefix (account->source->url, "groupwise://")))
        return NULL;

    gladefile = g_build_filename (EVOLUTION_GLADEDIR, "imap-headers.glade", NULL);
    gladexml = glade_xml_new (gladefile, "vbox2", NULL);
    g_free (gladefile);

    vbox = glade_xml_get_widget (gladexml, "vbox2");
    ui->all_headers = glade_xml_get_widget (gladexml, "allHeaders");
    ui->basic_headers = glade_xml_get_widget (gladexml, "basicHeaders");
    ui->mailing_list_headers = glade_xml_get_widget (gladexml, "mailingListHeaders");
    ui->custom_headers_box = glade_xml_get_widget (gladexml, "custHeaderHbox");
    ui->custom_headers_tree = GTK_TREE_VIEW(glade_xml_get_widget (gladexml, "custHeaderTree"));
    ui->add_header = GTK_BUTTON(glade_xml_get_widget (gladexml, "addHeader"));
    ui->remove_header = GTK_BUTTON(glade_xml_get_widget (gladexml, "removeHeader"));
    ui->entry_header = GTK_ENTRY (glade_xml_get_widget (gladexml, "customHeaderEntry"));

    url = camel_url_new (e_account_get_string(account, E_ACCOUNT_SOURCE_URL), &ex);

    ui->store = gtk_tree_store_new (1, G_TYPE_STRING);
    gtk_tree_view_set_model (ui->custom_headers_tree, GTK_TREE_MODEL(ui->store));

    if (url) {
        char *custom_headers;

        custom_headers = g_strdup(camel_url_get_param (url, "imap_custom_headers"));
        if (custom_headers) {
            int i=0;

            ui->custom_headers_array = g_strsplit (custom_headers, " ", -1);
            while (ui->custom_headers_array[i] ) {
                if (strlen(g_strstrip(ui->custom_headers_array[i]))) {
                    gtk_tree_store_append (ui->store, &iter, NULL);
                    gtk_tree_store_set (ui->store, &iter, 0, ui->custom_headers_array[i], -1);
                }
                i++;
            }
            g_strfreev (ui->custom_headers_array);

        }
        g_free (custom_headers);

        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ui->mailing_list_headers), TRUE);
        if (camel_url_get_param (url, "all_headers")) {
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ui->all_headers), TRUE);
                gtk_widget_set_sensitive (ui->custom_headers_box, FALSE);
        } else if (camel_url_get_param (url, "basic_headers"))
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ui->basic_headers), TRUE);
        camel_url_free (url);
    }
    renderer = gtk_cell_renderer_text_new ();
    column = gtk_tree_view_column_new_with_attributes (_("Custom Headers"), renderer, "text", 0, NULL);
    gtk_tree_view_append_column (ui->custom_headers_tree , column);

    gtk_widget_set_sensitive (GTK_WIDGET (ui->add_header), FALSE);
    if (gtk_tree_model_get_iter_first (gtk_tree_view_get_model (ui->custom_headers_tree), &first)==FALSE)
        gtk_widget_set_sensitive (GTK_WIDGET (ui->remove_header), FALSE);

    g_signal_connect (ui->all_headers, "toggled", G_CALLBACK (epif_fetch_all_headers_toggled), ui);
    g_signal_connect (ui->add_header, "clicked", G_CALLBACK (epif_add_header), ui);
    g_signal_connect (ui->remove_header, "clicked", G_CALLBACK (epif_remove_header_clicked), ui);
    g_signal_connect (ui->entry_header, "changed", G_CALLBACK (epif_entry_changed), ui);
    g_signal_connect (ui->entry_header, "activate", G_CALLBACK (epif_add_header), ui);

    gtk_notebook_append_page ((GtkNotebook *)(data->parent), vbox, gtk_label_new(_("IMAP Headers")));
    gtk_widget_show_all (vbox);

    return GTK_WIDGET (vbox);
}