aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/publish-calendar/publish-format-ical.c
blob: 5f620accc9d8830ef1f6d1c4ee5a799626907ad6 (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
/*
 * Authors: David Trowbridge <trowbrds@cs.colorado.edu>
 *
 * Copyright (C) 2005 Novell, Inc. (www.novell.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU 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 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

#include <string.h>
#include <gconf/gconf-client.h>
#include <libgnomevfs/gnome-vfs.h>
#include <libedataserver/e-source.h>
#include <libedataserver/e-source-list.h>
#include <libecal/e-cal.h>
#include <libecal/e-cal-util.h>
#include <calendar/common/authentication.h>
#include "publish-format-ical.h"

typedef struct {
    GHashTable *zones;
    ECal *ecal;
} CompTzData;

 static void
insert_tz_comps (icalparameter *param, void *cb_data)
{
    const char *tzid;
    CompTzData *tdata = cb_data;
    icaltimezone *zone = NULL;
    icalcomponent *tzcomp;
    GError *error = NULL;

    tzid = icalparameter_get_tzid (param);

    if (g_hash_table_lookup (tdata->zones, tzid))
        return;

    if (!e_cal_get_timezone (tdata->ecal, tzid, &zone, &error)) {
        g_warning ("Could not get the timezone information for %s :  %s \n", tzid, error->message);
        g_error_free (error);
        return;
    }

    tzcomp = icalcomponent_new_clone (icaltimezone_get_component (zone));
    g_hash_table_insert (tdata->zones, (gpointer) tzid, (gpointer) tzcomp);
}

static void
append_tz_to_comp (gpointer key, gpointer value, icalcomponent *toplevel)
{
    icalcomponent_add_component (toplevel, (icalcomponent *) value);
}

static gboolean
write_calendar (gchar *uid, ESourceList *source_list, GnomeVFSHandle *handle)
{
    ESource *source;
    ECal *client = NULL;
    GError *error = NULL;
    GList *objects;
    icalcomponent *top_level;

    source = e_source_list_peek_source_by_uid (source_list, uid);
    if (source)
        client = auth_new_cal_from_source (source, E_CAL_SOURCE_TYPE_EVENT);
    if (!client) {
        g_warning (G_STRLOC ": Could not publish calendar: Calendar backend no longer exists");
        return FALSE;
    }

    if (!e_cal_open (client, TRUE, &error)) {
        /* FIXME: EError */
        g_object_unref (client);
        g_error_free (error);
        return FALSE;
    }

    top_level = e_cal_util_new_top_level ();
    error = NULL;

    if (e_cal_get_object_list (client, "#t", &objects, &error)) {
        char *ical_string;
        GnomeVFSFileSize bytes_written = 0;
        GnomeVFSResult result;
        CompTzData tdata;

        tdata.zones = g_hash_table_new (g_str_hash, g_str_equal);
        tdata.ecal = client;

        while (objects) {
            icalcomponent *icalcomp = objects->data;
            icalcomponent_foreach_tzid (icalcomp, insert_tz_comps, &tdata);
            icalcomponent_add_component (top_level, icalcomp);
            objects = g_list_remove (objects, icalcomp);
        }

        g_hash_table_foreach (tdata.zones, (GHFunc) append_tz_to_comp, top_level);

        g_hash_table_destroy (tdata.zones);
        tdata.zones = NULL;

        ical_string = icalcomponent_as_ical_string (top_level);
        if ((result = gnome_vfs_write (handle, (gconstpointer) ical_string, strlen (ical_string), &bytes_written)) != GNOME_VFS_OK) {
            /* FIXME: EError */
            gnome_vfs_close (handle);
            return FALSE;
        }
    } else {
        /* FIXME: EError */
        g_object_unref (client);
        g_error_free (error);
        return FALSE;
    }

    g_object_unref (client);
    return TRUE;
}

void
publish_calendar_as_ical (GnomeVFSHandle *handle, EPublishUri *uri)
{
    GSList *l;
    ESourceList *source_list;
    GConfClient *gconf_client;

    gconf_client = gconf_client_get_default ();

    /* events */
    source_list = e_source_list_new_for_gconf (gconf_client, "/apps/evolution/calendar/sources");
    l = uri->events;
    while (l) {
        gchar *uid = l->data;
        write_calendar (uid, source_list, handle);
        l = g_slist_next (l);
    }
    g_object_unref (source_list);

    g_object_unref (gconf_client);
}