aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/webdav-account-setup/webdav-contacts-source.c
blob: 8f944de5edc458cf24248c3190b25b8520eeb93e (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
/*
 *
 * Copyright (C) 2008 Matthias Braun <matze@braunis.de>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * 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 this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>

#include <glib/gi18n-lib.h>
#include <glib.h>

#include <gtk/gtk.h>

#include <e-util/e-config.h>
#include <e-util/e-plugin.h>
#include <addressbook/gui/widgets/eab-config.h>

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

#define BASE_URI "webdav://"

typedef struct {
    ESource         *source;
    GtkWidget       *box;
    GtkEntry        *url_entry;
    GtkEntry        *username_entry;
    GtkToggleButton *avoid_ifmatch_toggle;
} ui_data;

GtkWidget *
plugin_webdav_contacts(EPlugin *epl, EConfigHookItemFactoryData *data);

int
e_plugin_lib_enable(EPluginLib *ep, int enable);

static void
ensure_webdav_contacts_source_group(void)
{
    ESourceList  *source_list;
    ESourceGroup *group;

    source_list = e_source_list_new_for_gconf_default("/apps/evolution/addressbook/sources");

    if (source_list == NULL) {
        return;
    }

    group = e_source_list_peek_group_by_name(source_list, _("WebDAV"));

    if (group == NULL) {
        gboolean res;

        group = e_source_group_new(_("WebDAV"), BASE_URI);
        res = e_source_list_add_group(source_list, group, -1);

        if (res == FALSE) {
            g_warning("Could not add WebDAV source group!");
        } else {
            e_source_list_sync(source_list, NULL);
        }

        g_object_unref(group);
    }
    g_object_unref(source_list);
}

static void
remove_webdav_contacts_source_group(void)
{
    ESourceList  *source_list;
    ESourceGroup *group;

    source_list = e_source_list_new_for_gconf_default("/apps/evolution/addressbook/sources");

    if (source_list == NULL) {
        return;
    }

    group = e_source_list_peek_group_by_name(source_list, _("WebDAV"));

    if (group) {
        GSList *sources;

        sources = e_source_group_peek_sources(group);

        if (NULL == sources) {
            e_source_list_remove_group(source_list, group);
            e_source_list_sync(source_list, NULL);
        }
    }
    g_object_unref(source_list);
}

/* stolen from caldav plugin which stole it from calendar-weather eplugin */
static gchar *
print_uri_noproto(EUri *uri)
{
    gchar *uri_noproto;

    if (uri->port != 0)
        uri_noproto = g_strdup_printf(
                "%s%s%s%s%s%s%s:%d%s%s%s",
                uri->user ? uri->user : "",
                uri->authmech ? ";auth=" : "",
                uri->authmech ? uri->authmech : "",
                uri->passwd ? ":" : "",
                uri->passwd ? uri->passwd : "",
                uri->user ? "@" : "",
                uri->host ? uri->host : "",
                uri->port,
                uri->path ? uri->path : "",
                uri->query ? "?" : "",
                uri->query ? uri->query : "");
    else
        uri_noproto = g_strdup_printf(
                "%s%s%s%s%s%s%s%s%s%s",
                uri->user ? uri->user : "",
                uri->authmech ? ";auth=" : "",
                uri->authmech ? uri->authmech : "",
                uri->passwd ? ":" : "",
                uri->passwd ? uri->passwd : "",
                uri->user ? "@" : "",
                uri->host ? uri->host : "",
                uri->path ? uri->path : "",
                uri->query ? "?" : "",
                uri->query ? uri->query : "");
    return uri_noproto;
}

static void
set_ui_from_source(ui_data *data)
{
    ESource    *source  = data->source;
    const char *url     = e_source_get_uri(source);
    EUri       *uri     = e_uri_new(url);
    char       *url_ui;
    const char *property;
    gboolean    use_ssl;
    gboolean    avoid_ifmatch;

    property = e_source_get_property(source, "use_ssl");
    if (property != NULL && strcmp(property, "1") == 0) {
        use_ssl = TRUE;
    } else {
        use_ssl = FALSE;
    }

    property = e_source_get_property(source, "avoid_ifmatch");
    if (property != NULL && strcmp(property, "1") == 0) {
        avoid_ifmatch = TRUE;
    } else {
        avoid_ifmatch = FALSE;
    }
    gtk_toggle_button_set_active(data->avoid_ifmatch_toggle, avoid_ifmatch);

    /* it's really a http or https protocol */
    g_free(uri->protocol);
    uri->protocol = g_strdup(use_ssl ? "https" : "http");

    /* remove user/username and set user field */
    if (uri->user != NULL) {
        gtk_entry_set_text(data->username_entry, uri->user);
        g_free(uri->user);
        uri->user = NULL;
    } else {
        gtk_entry_set_text(data->username_entry, "");
    }

    url_ui = e_uri_to_string(uri, TRUE);
    gtk_entry_set_text(data->url_entry, url_ui);

    g_free(url_ui);
    e_uri_free(uri);
}

static void
set_source_from_ui(ui_data *data)
{
    ESource    *source        = data->source;
    gboolean    avoid_ifmatch = gtk_toggle_button_get_active(data->avoid_ifmatch_toggle);
    const char *url           = gtk_entry_get_text(data->url_entry);
    EUri       *uri           = e_uri_new(url);
    char       *url_noprotocol;
    gboolean    use_ssl;

    e_source_set_property(source, "avoid_ifmatch", avoid_ifmatch ? "1" : "0");

    /* put username into uri */
    g_free(uri->user);
    uri->user = g_strdup(gtk_entry_get_text(data->username_entry));

    if (uri->user[0] != '\0') {
        e_source_set_property(source, "auth", "plain/password");
        e_source_set_property(source, "username", uri->user);
    } else {
        e_source_set_property(source, "auth", NULL);
        e_source_set_property(source, "username", NULL);
    }

    /* set use_ssl based on protocol in URL */
    if (strcmp(uri->protocol, "https") == 0) {
        use_ssl = TRUE;
    } else {
        use_ssl = FALSE;
    }
    e_source_set_property(source, "use_ssl", use_ssl ? "1" : "0");

    url_noprotocol = print_uri_noproto(uri);
    e_source_set_relative_uri(source, url_noprotocol);
    g_free(url_noprotocol);
    e_uri_free(uri);
}

static void
on_entry_changed(GtkEntry *entry, gpointer user_data)
{
    (void) entry;
    set_source_from_ui(user_data);
}

static void
on_toggle_changed(GtkToggleButton *tb, gpointer user_data)
{
    (void) tb;
    set_source_from_ui(user_data);
}

static void
destroy_ui_data(gpointer data)
{
    ui_data *ui_data = data;

    gtk_widget_destroy(ui_data->box);
    g_free(ui_data);
}

GtkWidget *
plugin_webdav_contacts(EPlugin *epl, EConfigHookItemFactoryData *data)
{
    EABConfigTargetSource *t = (EABConfigTargetSource *) data->target;
    ESource      *source;
    ESourceGroup *group;
    const char   *base_uri;
    GtkWidget    *parent;
    GtkWidget    *vbox;

    GtkWidget    *section;
    GtkWidget    *vbox2;

    GtkBox       *hbox;
    GtkWidget    *spacer;
    GtkWidget    *label;

    ui_data      *uidata;

    source = t->source;
    group  = e_source_peek_group (source);

    base_uri = e_source_group_peek_base_uri (group);

    g_object_set_data_full (G_OBJECT (epl), "widget", NULL,
            (GDestroyNotify)gtk_widget_destroy);

    if (strcmp(base_uri, BASE_URI) != 0) {
        return NULL;
    }

    uidata         = g_malloc0(sizeof(uidata[0]));
    uidata->source = source;

    /* Build up the UI */
    parent = data->parent;
    vbox   = gtk_widget_get_ancestor(gtk_widget_get_parent(parent), GTK_TYPE_VBOX);

    vbox2 = gtk_vbox_new(FALSE, 6);
    gtk_box_pack_start(GTK_BOX(vbox), vbox2, FALSE, FALSE, 0);

    section = gtk_label_new(NULL);
    gtk_label_set_markup(GTK_LABEL(section), _("<b>Server</b>"));
    gtk_misc_set_alignment(GTK_MISC(section), 0.0, 0.0);
    gtk_box_pack_start(GTK_BOX(vbox2), section, FALSE, FALSE, 0);


    hbox = GTK_BOX(gtk_hbox_new(FALSE, 10));
    gtk_box_pack_start(GTK_BOX(vbox2), GTK_WIDGET(hbox), TRUE, TRUE, 0);

    spacer = gtk_label_new("   ");
    gtk_box_pack_start(hbox, spacer, FALSE, FALSE, 0);

    label = gtk_label_new(_("URL:"));
    gtk_box_pack_start(hbox, label, FALSE, FALSE, 0);

    uidata->url_entry = GTK_ENTRY(gtk_entry_new());
    gtk_box_pack_start(hbox, GTK_WIDGET(uidata->url_entry), TRUE, TRUE, 0);

    hbox = GTK_BOX(gtk_hbox_new(FALSE, 10));
    gtk_box_pack_start(GTK_BOX(vbox2), GTK_WIDGET(hbox), TRUE, TRUE, 0);

    spacer = gtk_label_new("   ");
    gtk_box_pack_start(hbox, spacer, FALSE, FALSE, 0);

    label = gtk_label_new_with_mnemonic(_("User_name:"));
    gtk_box_pack_start(hbox, label, FALSE, FALSE, 0);

    uidata->username_entry = GTK_ENTRY(gtk_entry_new());
    gtk_box_pack_start(hbox, GTK_WIDGET(uidata->username_entry), TRUE, TRUE, 0);

    
    hbox = GTK_BOX(gtk_hbox_new(FALSE, 10));
    gtk_box_pack_start(GTK_BOX(vbox2), GTK_WIDGET(hbox), TRUE, TRUE, 0);

    spacer = gtk_label_new("   ");
    gtk_box_pack_start(hbox, spacer, FALSE, FALSE, 0);

    uidata->avoid_ifmatch_toggle = GTK_TOGGLE_BUTTON(
            gtk_check_button_new_with_mnemonic(
                _("_Avoid IfMatch (needed on apache < 2.2.8)")));
    gtk_box_pack_start(hbox, GTK_WIDGET(uidata->avoid_ifmatch_toggle),
                       FALSE, FALSE, 0);

    set_ui_from_source(uidata);

    gtk_widget_show_all(vbox2);

    uidata->box = vbox2;
    g_object_set_data_full(G_OBJECT(epl), "widget", uidata,
            destroy_ui_data);

    g_signal_connect(G_OBJECT(uidata->username_entry), "changed",
            G_CALLBACK(on_entry_changed), uidata);
    g_signal_connect(G_OBJECT(uidata->url_entry), "changed",
            G_CALLBACK(on_entry_changed), uidata);
    g_signal_connect(G_OBJECT(uidata->avoid_ifmatch_toggle), "toggled",
            G_CALLBACK(on_toggle_changed), uidata);

    return NULL;
}

int
e_plugin_lib_enable(EPluginLib *ep, int enable)
{
    if (enable) {
        ensure_webdav_contacts_source_group();
    } else {
        remove_webdav_contacts_source_group();
    }
    return 0;
}