aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/gui/itip-utils.c
blob: b407f38693ee98b158127e3e26c7d301ecb31a5d (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Authors:
 *  JP Rosevear <jpr@ximian.com>
 *
 * Copyright 2001, Ximian, Inc.
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

#include <bonobo/bonobo-object.h>
#include <bonobo/bonobo-object-client.h>
#include <libgnome/gnome-defs.h>
#include <libgnome/gnome-i18n.h>
#include <libgnomeui/gnome-dialog.h>
#include <libgnomeui/gnome-dialog-util.h>
#include <gtk/gtkwidget.h>
#include <ical.h>
#include <Evolution-Composer.h>
#include "itip-utils.h"

#define GNOME_EVOLUTION_COMPOSER_OAFIID "OAFIID:GNOME_Evolution_Mail_Composer"

static gchar *itip_methods[] = {
    "PUBLISH",
    "REQUEST",
    "REPLY",
    "ADD",
    "CANCEL",
    "RERESH",
    "COUNTER",
    "DECLINECOUNTER"
};

static icalproperty_method itip_methods_enum[] = {
    ICAL_METHOD_PUBLISH,
    ICAL_METHOD_REQUEST,
    ICAL_METHOD_REPLY,
    ICAL_METHOD_ADD,
    ICAL_METHOD_CANCEL,
    ICAL_METHOD_REFRESH,
    ICAL_METHOD_COUNTER,
    ICAL_METHOD_DECLINECOUNTER,
};

gchar *partstat_values[] = {
    "Needs action",
    "Accepted",
    "Declined",
    "Tentative",
    "Delegated",
    "Completed",
    "In Progress",
    "Unknown"
};

gchar *role_values[] = {
    "Chair",
    "Required Participant",
    "Optional Participant",
    "Non-Participant"
};



/* Note that I have to iterate and check myself because
   ical_property_get_xxx_parameter doesn't take into account the
   kind of parameter for which you wish to search! */
icalparameter *
get_icalparam_by_type (icalproperty *prop, icalparameter_kind kind)
{
    icalparameter *param;

    for (param = icalproperty_get_first_parameter (prop, ICAL_ANY_PARAMETER);
         param != NULL && icalparameter_isa (param) != kind;
         param = icalproperty_get_next_parameter (prop, ICAL_ANY_PARAMETER) );

    return param;
}

static void
error_dialog (gchar *str) 
{
    GtkWidget *dlg;
    
    dlg = gnome_error_dialog (str);
    gnome_dialog_run_and_close (GNOME_DIALOG (dlg));
}

void
itip_send_comp (CalComponentItipMethod method, CalComponent *comp)
{
    BonoboObjectClient *bonobo_server;
    GNOME_Evolution_Composer composer_server;
    CORBA_Environment ev;
    GSList *attendees, *l;
    GNOME_Evolution_Composer_RecipientList *to_list, *cc_list, *bcc_list;
    GNOME_Evolution_Composer_Recipient *recipient;
    CORBA_char *subject;
    gint cntr;
    gint len;
    CalComponentText caltext;
    CalComponentOrganizer organizer;
    CORBA_char *content_type, *filename, *description;
    GNOME_Evolution_Composer_AttachmentData *attach_data;
    CORBA_boolean show_inline;
    CORBA_char tempstr[200];
    
    CORBA_exception_init (&ev);

    /* First, I obtain an object reference that represents the Composer. */
    bonobo_server = bonobo_object_activate (GNOME_EVOLUTION_COMPOSER_OAFIID, 0);
    g_return_if_fail (bonobo_server != NULL);

    composer_server = BONOBO_OBJREF (bonobo_server);

    switch (method) {
    case CAL_COMPONENT_METHOD_PUBLISH:
        to_list = GNOME_Evolution_Composer_RecipientList__alloc ();
        to_list->_maximum = to_list->_length = 0;
        break;

    case CAL_COMPONENT_METHOD_REQUEST:
    case CAL_COMPONENT_METHOD_CANCEL:
        cal_component_get_attendee_list (comp, &attendees);
        len = g_slist_length (attendees);
        
        to_list = GNOME_Evolution_Composer_RecipientList__alloc ();
        to_list->_maximum = len;
        to_list->_length = len;
        to_list->_buffer = CORBA_sequence_GNOME_Evolution_Composer_Recipient_allocbuf (len);
        
        for (cntr = 0, l = attendees; cntr < len; cntr++, l = l->next) {
            CalComponentAttendee *att = l->data;
            
            recipient = &(to_list->_buffer[cntr]);
            if (att->cn)
                recipient->name = CORBA_string_dup (att->cn);
            else
                recipient->name = CORBA_string_dup ("");
            recipient->address = CORBA_string_dup (att->value);
        }
        break;

    case CAL_COMPONENT_METHOD_REPLY:
    case CAL_COMPONENT_METHOD_ADD:
    case CAL_COMPONENT_METHOD_REFRESH:
    case CAL_COMPONENT_METHOD_COUNTER:
    case CAL_COMPONENT_METHOD_DECLINECOUNTER:
        cal_component_get_organizer (comp, &organizer);
        if (organizer.value == NULL) {
            error_dialog (_("An organizer must be set."));
            return;
        }
        
        len = 1;

        to_list = GNOME_Evolution_Composer_RecipientList__alloc ();
        to_list->_maximum = len;
        to_list->_length = len;
        to_list->_buffer = CORBA_sequence_GNOME_Evolution_Composer_Recipient_allocbuf (len);
        recipient = &(to_list->_buffer[0]);

        if (organizer.cn != NULL)
            recipient->name = CORBA_string_dup (organizer.cn);
        else
            recipient->name = CORBA_string_dup ("");
        recipient->address = CORBA_string_dup (organizer.value);
        break;
    }

    cc_list = GNOME_Evolution_Composer_RecipientList__alloc ();
    cc_list->_maximum = cc_list->_length = 0;
    bcc_list = GNOME_Evolution_Composer_RecipientList__alloc ();
    bcc_list->_maximum = bcc_list->_length = 0;
    
    cal_component_get_summary (comp, &caltext);
    subject = CORBA_string_dup (caltext.value);

    GNOME_Evolution_Composer_setHeaders (composer_server, to_list, cc_list, bcc_list, subject, &ev);
    if (ev._major != CORBA_NO_EXCEPTION) {
        g_warning ("Unable to set composer headers while sending iTip message");
        CORBA_exception_free (&ev);
        return;
    }

    sprintf (tempstr, "text/calendar;METHOD=%s", itip_methods[method]);
    content_type = CORBA_string_dup (tempstr);
    filename = CORBA_string_dup ("");
    sprintf (tempstr, "Calendar attachment");
    description = CORBA_string_dup (tempstr);
    show_inline = FALSE;

    /* I need to create an encapsulating iCalendar component, and stuff our vEvent
       into it. */
    {
        icalcomponent *icomp, *clone;
        icalproperty *prop;
        icalvalue *value;
        gchar *ical_string;

        icomp = icalcomponent_new (ICAL_VCALENDAR_COMPONENT);
        
        prop = icalproperty_new (ICAL_PRODID_PROPERTY);
        value = icalvalue_new_text ("-//Ximian/Evolution//EN");
        icalproperty_set_value (prop, value);
        icalcomponent_add_property (icomp, prop);

        prop = icalproperty_new (ICAL_VERSION_PROPERTY);
        value = icalvalue_new_text ("2.0");
        icalproperty_set_value (prop, value);
        icalcomponent_add_property (icomp, prop);

        prop = icalproperty_new (ICAL_METHOD_PROPERTY);
        value = icalvalue_new_method (itip_methods_enum[method]);
        icalproperty_set_value (prop, value);
        icalcomponent_add_property (icomp, prop);

        clone = icalcomponent_new_clone (cal_component_get_icalcomponent (comp));
        icalcomponent_add_component (icomp, clone);

        ical_string = icalcomponent_as_ical_string (icomp);
        attach_data = GNOME_Evolution_Composer_AttachmentData__alloc ();
        attach_data->_maximum = attach_data->_length = strlen (ical_string);        
        attach_data->_buffer = CORBA_sequence_CORBA_char_allocbuf (attach_data->_length);   
        strcpy (attach_data->_buffer, ical_string);

        icalcomponent_free (icomp);
    }

    GNOME_Evolution_Composer_attachData (composer_server, 
                    content_type, filename, description,
                    show_inline, attach_data,
                    &ev);
    
    if (ev._major != CORBA_NO_EXCEPTION) {
        g_warning ("Unable to attach data to the composer while sending iTip message");
        CORBA_exception_free (&ev);
        return;
    }
    
    if (method == CAL_COMPONENT_METHOD_PUBLISH)
        GNOME_Evolution_Composer_show (composer_server, &ev);
    else
        GNOME_Evolution_Composer_send (composer_server, &ev);

    if (ev._major != CORBA_NO_EXCEPTION) {
        g_warning ("Unable to show the composer while sending iTip message");
        CORBA_exception_free (&ev);
        return;
    }
    
    CORBA_exception_free (&ev);

    /* Let's free shit up. */

    /* Beware--depending on whether CORBA_free is recursive, which I
       think is is, we might have memory leaks, in which case the code
       below is necessary. */
#if 0
    for (cntr = 0; cntr < priv->numentries; cntr++) {
        recipient = &(to_list->_buffer[cntr]);
        CORBA_free (recipient->name);
        CORBA_free (recipient->address);
        recipient->name = recipient->address = NULL;
    }
#endif

    if (CORBA_sequence_get_release (to_list) != FALSE)
        CORBA_free (to_list->_buffer);

    CORBA_free (to_list);
    CORBA_free (cc_list);
    CORBA_free (bcc_list);

    CORBA_free (subject);
    CORBA_free (content_type);
    CORBA_free (filename);
    CORBA_free (description);
    CORBA_free (attach_data);

    /* bonobo_object_unref (BONOBO_OBJECT (bonobo_server));   */
}