aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/gui/e-pub-utils.c
blob: ab39e13d8187b299cf508b47c9f0d7a20fe3ae95 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/* Evolution calendar Free/Busy utilities and types
 *
 * Copyright (C) 2004 Ximian, Inc.
 *
 * Author: Gary Ekker <gekker@novell.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */

#include <string.h>
#include <glib.h>
#include <gconf/gconf.h>
#include <gconf/gconf-client.h>
#include <libedataserver/e-source.h>
#include <libedataserver/e-source-list.h>
#include <libedataserverui/e-passwords.h>
#include <libecal/e-cal-time-util.h>
#include <libgnome/gnome-i18n.h>
#include "calendar-config.h"
#include "common/authentication.h"
#include "itip-utils.h"
#include "e-pub-utils.h"

static gboolean updated_last_pub_time = FALSE;

void
e_pub_uri_from_xml (EPublishUri *uri, const gchar *xml)
{
    xmlDocPtr doc;
    xmlNodePtr root, p;
    xmlChar *location, *enabled, *frequency;
    xmlChar *username, *publish_time;
    GSList *l = NULL;
    
    uri->location = NULL;
    doc = xmlParseDoc ((char *)xml);
    if (doc == NULL) {
        uri->location = NULL;
        return;
    }
    
    root = doc->children;
    if (strcmp (root->name, "uri") != 0) {
        return;
    }
    location = xmlGetProp (root, "location");
    enabled = xmlGetProp (root, "enabled");
    frequency = xmlGetProp (root, "frequency");
    username = xmlGetProp (root, "username");
    publish_time = xmlGetProp (root, "publish_time");
    
    if (location != NULL)
        uri->location = g_strdup (location);
    if (enabled != NULL)
        uri->enabled = atoi (enabled);
    if (frequency != NULL)
        uri->publish_freq = atoi (frequency);
    if (username != NULL)
        uri->username = g_strdup (username);
    if (publish_time != NULL)
        uri->last_pub_time = g_strdup (publish_time);
    
    uri->password = g_strdup ("");

    for (p = root->children; p != NULL; p = p->next) {
        xmlChar *uid = xmlGetProp (p, "uid");

        l = g_slist_append (l, uid);
    }   
    uri->calendars = l;
    
    xmlFree(location);
    xmlFree(enabled);
    xmlFreeDoc(doc);
    return;
}

gchar *
e_pub_uri_to_xml (EPublishUri *uri)
{
    xmlDocPtr doc;
    xmlNodePtr root;
    gchar *enabled, *frequency;
    GSList *cals = NULL;
    xmlChar *xml_buffer;
    char *returned_buffer;
    int xml_buffer_size;

    g_return_val_if_fail (uri != NULL, NULL);
    g_return_val_if_fail (uri->location != NULL, NULL);

    doc = xmlNewDoc ("1.0");

    root = xmlNewDocNode (doc, NULL, "uri", NULL);
    enabled = g_strdup_printf ("%d", uri->enabled);
    frequency = g_strdup_printf ("%d", uri->publish_freq);
    xmlSetProp (root, "location", uri->location);
    xmlSetProp (root, "enabled", enabled);
    xmlSetProp (root, "frequency", frequency);
    xmlSetProp (root, "username", uri->username);
    xmlSetProp (root, "publish_time", uri->last_pub_time);
    
    for (cals = uri->calendars; cals != NULL; cals = cals->next) {
        xmlNodePtr node;
        
        node = xmlNewChild (root, NULL, "source", NULL);
        xmlSetProp (node, "uid", cals->data);
    }
    xmlDocSetRootElement (doc, root);

    xmlDocDumpMemory (doc, &xml_buffer, &xml_buffer_size);
    xmlFreeDoc (doc);

    returned_buffer = g_malloc (xml_buffer_size + 1);
    memcpy (returned_buffer, xml_buffer, xml_buffer_size);
    returned_buffer [xml_buffer_size] = '\0';
    xmlFree (xml_buffer);
    g_free (enabled);

    return returned_buffer;
}

static gboolean 
is_publish_time (EPublishUri *uri) {
    icaltimezone *utc;
    struct icaltimetype current_itt, adjust_itt;

    utc = icaltimezone_get_utc_timezone ();
    current_itt = icaltime_current_time_with_zone (utc);        

    if (!uri->last_pub_time ||(strlen (uri->last_pub_time)== 0)) {
        uri->last_pub_time = g_strdup (icaltime_as_ical_string (current_itt));
        return TRUE;
        
    } else {
        struct icaltimetype adjust_itt = icaltime_from_string (uri->last_pub_time);
        
        switch (uri->publish_freq) {
            case URI_PUBLISH_DAILY:
                icaltime_adjust (&adjust_itt, 1, 0, 0, 0);
                if (icaltime_compare_date_only (adjust_itt, current_itt ) < 0) {
                    uri->last_pub_time = g_strdup (icaltime_as_ical_string (current_itt));
                    return TRUE;
                }
                break;
            case URI_PUBLISH_WEEKLY:
                icaltime_adjust (&adjust_itt, 7, 0, 0, 0);
                if (icaltime_compare_date_only (adjust_itt, current_itt ) < 0) {
                    uri->last_pub_time = g_strdup (icaltime_as_ical_string (current_itt));
                    return TRUE;
                }
                break;
        }
    }
    
    return FALSE;
}

/* FIXME we need to add a timeout function to check if the next publishing time is reached. 
   If the freqency is daily and evolution runs continuously for more than one day, the freebusy
   will only be published for the first day */
void
e_pub_publish (gboolean publish) {
    icaltimezone *utc;
    time_t start = time (NULL), end;
    GSList *uri_config_list, *l, *uri_list = NULL;
    ESourceList *source_list;
    GConfClient *gconf_client;
    gboolean published = FALSE;
    
    gconf_client = gconf_client_get_default ();
    source_list = e_source_list_new_for_gconf (gconf_client, "/apps/evolution/calendar/sources");
    
    utc = icaltimezone_get_utc_timezone (); 
    start = time_day_begin_with_zone (start, utc);
    end = time_add_week_with_zone (start, 6, utc);
        
    uri_config_list = calendar_config_get_free_busy ();

    for (l = uri_config_list; l != NULL; l = l->next) {
        GSList *p =NULL, *q;
        EPublishUri *uri;
        ECalComponent *clone = NULL;
        gboolean cloned = FALSE;
        ECal *client = NULL;
        char *prompt;
        gboolean remember = FALSE;
        gchar *password;

        gchar *xml = (gchar *)l->data;
        
        uri = g_new0 (EPublishUri, 1);      
        e_pub_uri_from_xml (uri, xml);
    
        /*FIXME this is just a hack to avoid publishing again and again 
          ,we need to make the last publish time a seperate key */
        if (updated_last_pub_time) {
            updated_last_pub_time = FALSE;
            return;
        }   
        
        /* TODO: make sure we're online */
        /* skip this url if it isn't enabled or if it is manual */
        if (!uri->enabled) {
            uri_config_list = g_slist_next (uri_config_list);
            continue;
        }   
        
        if (!publish) {
            /* a g_idle publish, make sure we are not set to user only */
            if (uri->publish_freq == URI_PUBLISH_USER) {
                uri_config_list = g_slist_next (uri_config_list);
                continue;
            }
            
            /* If not is it time to publish again? */
            publish = is_publish_time (uri);
                
        }
        
        /* User published or config change */
        if (publish) {
            /* We still need to set the last_pub_time */
            uri->last_pub_time = 0;
            is_publish_time (uri);
            q = NULL;
            for (p = uri->calendars; p != NULL; p = p->next) {
                
                GList *comp_list = NULL;
                gchar *source_uid;
                ESource * source;
                char *email = NULL;
                GError *error = NULL;
                GList *users = NULL;
    
                source_uid = g_strdup (p->data);
                source =  e_source_list_peek_source_by_uid (source_list, source_uid);

                if (source)
                    client = auth_new_cal_from_source (source, E_CAL_SOURCE_TYPE_EVENT);

                if (!client) {
                    g_warning (G_STRLOC ": Could not publish Free/Busy: Calendar backend no longer exists");
                    g_free (source_uid);
                    g_free (p->data);
                    q = g_slist_append (q, p);

                    continue;
                }
            
                if (!e_cal_open (client, TRUE, &error)) {
                    g_warning ("Could not open the calendar %s \n", error->message);
                    g_error_free (error);
                    error = NULL;

                    g_object_unref (client);
                    client = NULL;
                    g_free (source_uid);
                    continue;
                }
                
                if (e_cal_get_cal_address (client, &email, &error)) {
                    if (email && *email)    
                        users = g_list_append (users, email);
                } else {
                    g_warning ("Could not get the email: %s \n", error->message);
                    g_error_free (error);
                    error = NULL;
                }

                if (e_cal_get_free_busy ((ECal *) client, users,
                             start, end, 
                             &comp_list, &error)) {
                    GList *l;
    
                    for (l = comp_list; l; l = l->next) {
                        ECalComponent *comp = E_CAL_COMPONENT (l->data);
                    
                        cloned = itip_publish_begin (comp, (ECal *) client, cloned, &clone);
                        g_object_unref (comp);
                    }
                    g_list_free (comp_list);
                } else {
                    g_warning ("Could not get the free busy information %s \n", error->message);
                    g_error_free (error);
                    error = NULL;
                }
                
                if (users) 
                    g_list_free (users);

                g_free (email);
                g_object_unref (client);
                client = NULL;
                g_free (source_uid);
            } 
        }

        for(p = q; p!=NULL; p = p->next) {
            uri->calendars = g_slist_delete_link (uri->calendars, p->data);
        }
        g_slist_free (q);
        
        /* add password to the uri */
        password = e_passwords_get_password ("Calendar", 
                (gchar *)uri->location);

        if (!password) {
            prompt = g_strdup_printf (_("Enter the password for %s"), (gchar *)uri->location);
            password = e_passwords_ask_password (_("Enter password"), 
                    "Calendar", (gchar *)uri->location, 
                    prompt,
                    E_PASSWORDS_REMEMBER_FOREVER|E_PASSWORDS_SECRET|E_PASSWORDS_ONLINE,
                    &remember, NULL);

            g_free (prompt);

            if (!password) {
                g_slist_free (p);
                continue;
            }
        }

        g_slist_free (p);

        if (cloned && clone)
            published = itip_publish_comp ((ECal *) client,
                    uri->location,
                    uri->username, 
                    password, &clone);

        xml = e_pub_uri_to_xml (uri);
        if (xml != NULL) {
            uri_list = g_slist_append (uri_list, xml);
        }
        g_free (uri);
    }
    
    if (published) {
        /* Update gconf so we have the last_pub_time */
        calendar_config_set_free_busy (uri_list);
        updated_last_pub_time = TRUE;
    }
        
    g_slist_free (uri_config_list);
    g_slist_free (uri_list);
}