aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/save-calendar/ical-format.c
blob: a390eb2052848b755778ab8b06467479c0fad1e9 (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
/*
 * 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.
 *
 * 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 Lesser General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 *
 * Authors:
 *      Rodrigo Moya <rodrigo@novell.com>
 *      Philip Van Hoof <pvanhoof@gnome.org>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

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

#include <string.h>
#include <glib/gi18n.h>

#include "format-handler.h"

static void
display_error_message (GtkWidget *parent,
                       const gchar *message)
{
    GtkWidget *dialog;

    dialog = gtk_message_dialog_new (
        GTK_WINDOW (parent), 0,
        GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
        "%s", message);
    gtk_dialog_run (GTK_DIALOG (dialog));
    gtk_widget_destroy (dialog);
}

typedef struct {
    GHashTable *zones;
    ECalClient *client;
} CompTzData;

static void
insert_tz_comps (icalparameter *param,
                 gpointer cb_data)
{
    const gchar *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;

    e_cal_client_get_timezone_sync (
        tdata->client, tzid, &zone, NULL, &error);

    if (error != NULL) {
        g_warning (
            "Could not get the timezone information for %s: %s",
            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 void
do_save_calendar_ical (FormatHandler *handler,
                       ESourceSelector *selector,
                       ECalClientSourceType type,
                       gchar *dest_uri)
{
    ESource *primary_source;
    EClient *source_client;
    GError *error = NULL;
    GSList *objects = NULL;
    icalcomponent *top_level = NULL;

    if (!dest_uri)
        return;

    /* open source client */
    primary_source = e_source_selector_ref_primary_selection (selector);
    source_client = e_cal_client_connect_sync (
        primary_source, type, NULL, &error);
    g_object_unref (primary_source);

    /* Sanity check. */
    g_return_if_fail (
        ((source_client != NULL) && (error == NULL)) ||
        ((source_client == NULL) && (error != NULL)));

    if (error != NULL) {
        display_error_message (
            gtk_widget_get_toplevel (GTK_WIDGET (selector)),
            error->message);
        g_error_free (error);
        return;
    }

    /* create destination file */
    top_level = e_cal_util_new_top_level ();

    e_cal_client_get_object_list_sync (
        E_CAL_CLIENT (source_client), "#t", &objects, NULL, &error);

    if (objects != NULL) {
        CompTzData tdata;
        GOutputStream *stream;
        GSList *iter;

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

        for (iter = objects; iter; iter = iter->next) {
            icalcomponent *icalcomp = icalcomponent_new_clone (iter->data);

            icalcomponent_foreach_tzid (icalcomp, insert_tz_comps, &tdata);
            icalcomponent_add_component (top_level, icalcomp);
        }

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

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

        /* save the file */
        stream = open_for_writing (GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (selector))), dest_uri, &error);

        if (stream) {
            gchar *ical_str = icalcomponent_as_ical_string_r (top_level);

            g_output_stream_write_all (stream, ical_str, strlen (ical_str), NULL, NULL, &error);
            g_output_stream_close (stream, NULL, NULL);

            g_object_unref (stream);
            g_free (ical_str);
        }

        e_cal_client_free_icalcomp_slist (objects);
    }

    if (error != NULL) {
        display_error_message (
            gtk_widget_get_toplevel (GTK_WIDGET (selector)),
            error->message);
        g_error_free (error);
    }

    /* terminate */
    g_object_unref (source_client);
    icalcomponent_free (top_level);
}

FormatHandler *
ical_format_handler_new (void)
{
    FormatHandler *handler = g_new (FormatHandler, 1);

    handler->isdefault = TRUE;
    handler->combo_label = _("iCalendar (.ics)");
    handler->filename_ext = ".ics";
    handler->options_widget = NULL;
    handler->save = do_save_calendar_ical;
    handler->data = NULL;

    return handler;
}