aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/caldav/caldav-source.c
blob: 11c651f7d961f87efe0e644a2f549fbd44e399ba (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
/*
 * 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:
 *      Christian Kellner <gicmo@gnome.org>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

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

#include <glib/gi18n-lib.h>

#include <gtk/gtk.h>

#include <e-util/e-config.h>
#include <e-util/e-plugin.h>
#include <e-util/e-plugin-util.h>
#include <calendar/gui/e-cal-config.h>
#include <libedataserver/e-account-list.h>
#include <libecal/e-cal.h>

#include <string.h>

#include "caldav-browse-server.h"

#define d(x)

/*****************************************************************************/
/* prototypes */
gint              e_plugin_lib_enable      (EPlugin                 *ep,
                       gint                         enable);

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

/*****************************************************************************/
/* plugin intialization */

static void
ensure_caldav_source_group (ECalSourceType source_type)
{
    ESourceList  *slist;

    if (!e_cal_get_sources (&slist, source_type, NULL)) {
        g_warning ("Could not get calendar source list from GConf!");
        return;
    }

    e_source_list_ensure_group (slist, _("CalDAV"), "caldav://", FALSE);
    g_object_unref (slist);
}

gint
e_plugin_lib_enable (EPlugin *ep, gint enable)
{

    if (enable) {
        d(g_print ("CalDAV Eplugin starting up ...\n"));
        ensure_caldav_source_group (E_CAL_SOURCE_TYPE_EVENT);
        ensure_caldav_source_group (E_CAL_SOURCE_TYPE_TODO);
        ensure_caldav_source_group (E_CAL_SOURCE_TYPE_JOURNAL);
    }

    return 0;
}

/*****************************************************************************/

static void
location_changed_cb (GtkEntry *editable, ESource *source)
{
    SoupURI     *suri;
    gchar       *ruri;
    const gchar *uri, *username;

    uri = gtk_entry_get_text (editable);

    suri = soup_uri_new (uri);
    if (!suri)
        return;

    username = e_source_get_property (source, "username");
    if (username && !*username)
        username = NULL;

    soup_uri_set_user (suri, username);

    ruri = e_plugin_util_uri_no_proto (suri);
    e_source_set_relative_uri (source, ruri);
    g_free (ruri);
    soup_uri_free (suri);
}

static void
user_changed_cb (GtkEntry *editable, ESource *source)
{
    SoupURI     *suri;
    gchar       *uri, *ruri;
    const gchar *user;

    uri = e_source_get_uri (source);
    user = gtk_entry_get_text (editable);

    if (uri == NULL)
        return;

    suri = soup_uri_new (uri);
    g_free (uri);

    if (suri == NULL)
        return;

    soup_uri_set_user (suri, NULL);

    if (user != NULL && *user) {
        soup_uri_set_user (suri, user);
        e_source_set_property (source, "auth", "1");
    } else {
        e_source_set_property (source, "auth", NULL);
    }

    e_source_set_property (source, "username", user);
    ruri = e_plugin_util_uri_no_proto (suri);
    e_source_set_relative_uri (source, ruri);
    g_free (ruri);
    soup_uri_free (suri);
}

static void
browse_cal_clicked_cb (GtkButton *button, gpointer user_data)
{
    GtkEntry *url, *username, *usermail;
    GtkToggleButton *ssl, *autoschedule;
    gchar *new_url, *new_usermail;
    gboolean new_autoschedule;

    g_return_if_fail (button != NULL);

    url = g_object_get_data (G_OBJECT (button), "caldav-url");
    ssl = g_object_get_data (G_OBJECT (button), "caldav-ssl");
    username = g_object_get_data (G_OBJECT (button), "caldav-username");
    usermail = g_object_get_data (G_OBJECT (button), "caldav-usermail");
    autoschedule = g_object_get_data (G_OBJECT (button), "caldav-autoschedule");

    g_return_if_fail (url != NULL);
    g_return_if_fail (GTK_IS_ENTRY (url));
    g_return_if_fail (ssl != NULL);
    g_return_if_fail (GTK_IS_TOGGLE_BUTTON (ssl));
    g_return_if_fail (username != NULL);
    g_return_if_fail (GTK_IS_ENTRY (username));
    g_return_if_fail (usermail != NULL);
    g_return_if_fail (GTK_IS_ENTRY (usermail));
    g_return_if_fail (autoschedule != NULL);
    g_return_if_fail (GTK_IS_TOGGLE_BUTTON (autoschedule));

    new_usermail = g_strdup (gtk_entry_get_text (usermail)),
    new_autoschedule = gtk_toggle_button_get_active (autoschedule);

    new_url = caldav_browse_server (
        GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (button))),
        gtk_entry_get_text (url),
        gtk_entry_get_text (username),
        gtk_toggle_button_get_active (ssl),
        &new_usermail,
        &new_autoschedule,
        GPOINTER_TO_INT (user_data));

    if (new_url) {
        gtk_entry_set_text (url, new_url);
        g_free (new_url);

        if (new_usermail)
            gtk_entry_set_text (usermail, new_usermail);

        gtk_toggle_button_set_active (autoschedule, new_autoschedule);
    }

    g_free (new_usermail);
}

GtkWidget *
oge_caldav  (EPlugin                    *epl,
         EConfigHookItemFactoryData *data)
{
    ECalConfigTargetSource *t = (ECalConfigTargetSource *) data->target;
    ESource      *source;
    SoupURI      *suri;
    GtkWidget    *parent, *location, *ssl, *user, *mail, *autoschedule, *browse_cal;
    gchar        *uri, *username;
    guint         n_rows;

    source = t->source;

    if (!e_plugin_util_is_group_proto (e_source_peek_group (source), "caldav")) {
        return NULL;
    }

    /* Extract the username from the uri so we can prefill the
     * dialog right, remove the username from the url then */
    uri = e_source_get_uri (source);
    suri = soup_uri_new (uri);
    g_free (uri);

    if (suri) {
        soup_uri_set_user (suri, NULL);
        soup_uri_set_password (suri, NULL);
        uri = soup_uri_to_string (suri, FALSE);
        soup_uri_free (suri);
    } else {
        uri = g_strdup ("");
    }

    username = e_source_get_duped_property (source, "username");

    /* Build up the UI */
    parent = data->parent;

    location = e_plugin_util_add_entry (parent, _("_URL:"), NULL, NULL);
    gtk_entry_set_text (GTK_ENTRY (location), uri);

    g_signal_connect (
        location, "changed",
        G_CALLBACK (location_changed_cb), source);

    ssl = e_plugin_util_add_check (parent, _("Use _SSL"), source, "ssl", "1", "0");

    user = e_plugin_util_add_entry (parent, _("User_name:"), NULL, NULL);
    gtk_entry_set_text (GTK_ENTRY (user), username ? username : "");

    g_signal_connect (
        user, "changed",
        G_CALLBACK (user_changed_cb), source);

    g_free (uri);
    g_free (username);

    mail = e_plugin_util_add_entry (parent, _("User e-_mail:"), source, "usermail");
    autoschedule = e_plugin_util_add_check (
        parent, _("Server _handles meeting invitations"),
        source, "autoschedule", "1", "0");

    browse_cal = gtk_button_new_with_mnemonic (_("Brows_e server for a calendar"));
    gtk_widget_show (browse_cal);
    g_object_get (parent, "n-rows", &n_rows, NULL);
    gtk_table_attach (
        GTK_TABLE (parent), browse_cal, 1, 2,
        n_rows, n_rows + 1, GTK_FILL, 0, 0, 0);

    g_object_set_data (G_OBJECT (browse_cal), "caldav-url", location);
    g_object_set_data (G_OBJECT (browse_cal), "caldav-ssl", ssl);
    g_object_set_data (G_OBJECT (browse_cal), "caldav-username", user);
    g_object_set_data (G_OBJECT (browse_cal), "caldav-usermail", mail);
    g_object_set_data (G_OBJECT (browse_cal), "caldav-autoschedule", autoschedule);

    g_signal_connect  (
        browse_cal, "clicked",
        G_CALLBACK (browse_cal_clicked_cb),
        GINT_TO_POINTER (t->source_type));

    e_plugin_util_add_refresh (parent, _("Re_fresh:"), source, "refresh");

    return location;
}