aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/gui/dialogs/comp-editor-util.c
blob: 5d10942a5e0cc6de9034641b1ea83090964f3093 (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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/* Evolution calendar - Widget utilities
 *
 * Copyright (C) 2000 Ximian, Inc.
 *
 * Author: Federico Mena-Quintero <federico@ximian.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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */

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

#include <ctype.h>
#include <string.h>
#include <ical.h>
#include <glib.h>
#include <libgnome/gnome-defs.h>
#include <libgnome/gnome-i18n.h>
#include <liboaf/liboaf.h>
#include <bonobo/bonobo-control.h>
#include <bonobo/bonobo-widget.h>
#include <e-destination.h>
#include <e-util/e-time-utils.h>
#include <cal-util/timeutil.h>
#include "../calendar-config.h"
#include "comp-editor-util.h"



/**
 * comp_editor_dates:
 * @dates: A structure to be filled out with dates of a component
 * @comp: The component to extract the dates from
 * 
 * Extracts the dates from the calendar component into the
 * CompEditorPageDates structure. Call comp_editor_free_dates() to free the
 * results.
 **/
void
comp_editor_dates (CompEditorPageDates *dates, CalComponent *comp)
{
    CalComponentDateTime dt;

    dates->start = NULL;
    dates->end = NULL;
    dates->due = NULL;
    dates->complete = NULL;
    
    cal_component_get_dtstart (comp, &dt);
    if (dt.value) {
        dates->start = g_new (CalComponentDateTime, 1);
        *dates->start = dt;
    }

    cal_component_get_dtend (comp, &dt);
    if (dt.value) {
        dates->end = g_new (CalComponentDateTime, 1);
        *dates->end = dt;
    }

    cal_component_get_due (comp, &dt);
    if (dt.value) {
        dates->due = g_new (CalComponentDateTime, 1);
        *dates->due = dt;
    }

    cal_component_get_completed (comp, &dates->complete);
}


/* This frees the dates in the CompEditorPageDates struct. But it doesn't free
 * the struct (as that is usually static).
 */
void
comp_editor_free_dates (CompEditorPageDates *dates)
{
    if (dates->start)
        cal_component_free_datetime (dates->start);

    if (dates->end)
        cal_component_free_datetime (dates->end);

    if (dates->due)
        cal_component_free_datetime (dates->due);

    if (dates->complete)
        cal_component_free_icaltimetype (dates->complete);
}


static void
write_label_piece (struct icaltimetype *tt, char *buffer, int size,
           char *stext, char *etext)
{
    struct tm tmp_tm = { 0 };
    int len;
    
    /* FIXME: May want to convert the time to an appropriate zone. */

    if (stext != NULL)
        strcat (buffer, stext);

    tmp_tm.tm_year = tt->year - 1900;
    tmp_tm.tm_mon = tt->month - 1;
    tmp_tm.tm_mday = tt->day;
    tmp_tm.tm_hour = tt->hour;
    tmp_tm.tm_min = tt->minute;
    tmp_tm.tm_sec = tt->second;
    tmp_tm.tm_isdst = -1;

    tmp_tm.tm_wday = time_day_of_week (tt->day, tt->month - 1, tt->year);

    len = strlen (buffer);
    e_time_format_date_and_time (&tmp_tm,
                     calendar_config_get_24_hour_format (), 
                     FALSE, FALSE,
                     &buffer[len], size - len);
    if (etext != NULL)
        strcat (buffer, etext);
}

/**
 * comp_editor_date_label:
 * @dates: The dates to use in constructing a label
 * @label: The label whose text is to be set
 * 
 * Set the text of a label based on the dates available and the user's
 * formatting preferences
 **/
void
comp_editor_date_label (CompEditorPageDates *dates, GtkWidget *label)
{
    char buffer[1024];
    gboolean start_set = FALSE, end_set = FALSE;
    gboolean complete_set = FALSE, due_set = FALSE;

    buffer[0] = '\0';

    if (dates->start && !icaltime_is_null_time (*dates->start->value))
        start_set = TRUE;
    if (dates->end && !icaltime_is_null_time (*dates->end->value))
        end_set = TRUE;
    if (dates->complete && !icaltime_is_null_time (*dates->complete))
        complete_set = TRUE;
    if (dates->due && !icaltime_is_null_time (*dates->due->value))
        due_set = TRUE;

    if (start_set)
        write_label_piece (dates->start->value, buffer, 1024,
                   NULL, NULL);

    if (start_set && end_set)
        write_label_piece (dates->end->value, buffer, 1024,
                   _(" to "), NULL);

    if (complete_set) {
        if (start_set)
            write_label_piece (dates->complete, buffer, 1024, _(" (Completed "), ")");
        else
            write_label_piece (dates->complete, buffer, 1024, _("Completed "), NULL);
    }
    
    if (due_set && dates->complete == NULL) {
        if (start_set)
            write_label_piece (dates->due->value, buffer, 1024, _(" (Due "), ")");
        else
            write_label_piece (dates->due->value, buffer, 1024, _("Due "), NULL);
    }

    gtk_label_set_text (GTK_LABEL (label), buffer);
}

/**
 * comp_editor_new_date_edit:
 * @show_date: Whether to show a date picker in the widget.
 * @show_time: Whether to show a time picker in the widget.
 * @make_time_insensitive: Whether the time field is made insensitive rather
 * than hiding it. This is useful if you want to preserve the layout of the
 * widgets.
 * 
 * Creates a new #EDateEdit widget, configured using the calendar's preferences.
 * 
 * Return value: A newly-created #EDateEdit widget.
 **/
GtkWidget *
comp_editor_new_date_edit (gboolean show_date, gboolean show_time,
               gboolean make_time_insensitive)
{
    EDateEdit *dedit;

    dedit = E_DATE_EDIT (e_date_edit_new ());

    e_date_edit_set_show_date (dedit, show_date);
    e_date_edit_set_show_time (dedit, show_time);
#if 0
    e_date_edit_set_make_time_insensitive (dedit, make_time_insensitive);
#else
    e_date_edit_set_make_time_insensitive (dedit, FALSE);
#endif
    calendar_config_configure_e_date_edit (dedit);

    return GTK_WIDGET (dedit);
}


/* Returns the current time, for EDateEdit widgets and ECalendar items in the
   dialogs.
   FIXME: Should probably use the timezone from somewhere in the component
   rather than the current timezone. */
struct tm
comp_editor_get_current_time (GtkObject *object, gpointer data)
{
    char *location;
    icaltimezone *zone;
    struct icaltimetype tt;
    struct tm tmp_tm = { 0 };

    /* Get the current timezone. */
    location = calendar_config_get_timezone ();
    zone = icaltimezone_get_builtin_timezone (location);

    tt = icaltime_from_timet_with_zone (time (NULL), FALSE, zone);

    /* Now copy it to the struct tm and return it. */
    tmp_tm.tm_year  = tt.year - 1900;
    tmp_tm.tm_mon   = tt.month - 1;
    tmp_tm.tm_mday  = tt.day;
    tmp_tm.tm_hour  = tt.hour;
    tmp_tm.tm_min   = tt.minute;
    tmp_tm.tm_sec   = tt.second;
    tmp_tm.tm_isdst = -1;

    return tmp_tm;
}



/*
 * These are utility functions to handle the SelectNames control we use
 * for the contacts field, and its related dialog.
 */

#define SELECT_NAMES_OAFID "OAFIID:GNOME_Evolution_Addressbook_SelectNames"

GNOME_Evolution_Addressbook_SelectNames
comp_editor_create_contacts_component (void)
{
    GNOME_Evolution_Addressbook_SelectNames corba_select_names;
    CORBA_Environment ev;

    CORBA_exception_init (&ev);
    corba_select_names = oaf_activate_from_id (SELECT_NAMES_OAFID, 0,
                           NULL, &ev);

    /* OAF seems to be broken -- it can return a CORBA_OBJECT_NIL without
           raising an exception in `ev'.  */
    if (ev._major != CORBA_NO_EXCEPTION
        || corba_select_names == CORBA_OBJECT_NIL) {
        g_warning ("Cannot activate -- %s", SELECT_NAMES_OAFID);
        CORBA_exception_free (&ev);
        return CORBA_OBJECT_NIL;
    }

    CORBA_exception_free (&ev);

    return corba_select_names;
}


GtkWidget *
comp_editor_create_contacts_control (GNOME_Evolution_Addressbook_SelectNames corba_select_names)
{
    Bonobo_Control corba_control;
    GtkWidget *control_widget;
    CORBA_Environment ev;
    char *name = _("Contacts");

    CORBA_exception_init (&ev);

    GNOME_Evolution_Addressbook_SelectNames_addSection (
        corba_select_names, name, name, &ev);
    if (ev._major != CORBA_NO_EXCEPTION) {
        CORBA_exception_free (&ev);
        return NULL;
    }

    corba_control =
        GNOME_Evolution_Addressbook_SelectNames_getEntryBySection (
            corba_select_names, name, &ev);

    if (ev._major != CORBA_NO_EXCEPTION) {
        CORBA_exception_free (&ev);
        return NULL;
    }

    CORBA_exception_free (&ev);

    control_widget = bonobo_widget_new_control_from_objref (
        corba_control, CORBA_OBJECT_NIL);

    gtk_widget_show (control_widget);

    return control_widget;
}


Bonobo_EventSource_ListenerId
comp_editor_connect_contacts_changed (GtkWidget *contacts_entry,
                      BonoboListenerCallbackFn changed_cb,
                      gpointer changed_cb_data)
{
    BonoboControlFrame *cf;
    Bonobo_PropertyBag pb = CORBA_OBJECT_NIL;

    cf = bonobo_widget_get_control_frame (BONOBO_WIDGET (contacts_entry));
    pb = bonobo_control_frame_get_control_property_bag (cf, NULL);

    return bonobo_event_source_client_add_listener (
        pb, changed_cb,
        "Bonobo/Property:change:entry_changed",
        NULL, changed_cb_data);
}


void
comp_editor_show_contacts_dialog (GNOME_Evolution_Addressbook_SelectNames corba_select_names)
{
    CORBA_Environment ev;

    CORBA_exception_init (&ev);
    GNOME_Evolution_Addressbook_SelectNames_activateDialog (
        corba_select_names, _("Contacts"), &ev);
    CORBA_exception_free (&ev);
}


/* A simple 'name <email>' parser. Input should be UTF8.
   FIXME: Should probably use camel functions or something.
   Also note that this is broken wrt UTF8 - can't use strchr etc. */
static void
parse_contact_string (const char *value, char **name, char **email)
{
    char *lbracket, *rbracket, *name_end, *tmp_name, *tmp_email;

    if (!value) {
        *name = g_strdup ("");
        *email = g_strdup ("");
        return;
    }

    lbracket = strchr (value, '<');
    rbracket = strchr (value, '>');

    if (!lbracket || !rbracket || rbracket < lbracket) {
        *name = g_strdup (value);
        *email = g_strdup ("");
        return;
    }

    name_end = lbracket - 1;
    while (name_end > value && isspace (*name_end))
        name_end--;

    tmp_name = g_malloc (name_end - value + 2);
    strncpy (tmp_name, value, name_end - value + 1);
    tmp_name[name_end - value + 1] = '\0';
    *name = tmp_name;

    tmp_email = g_malloc (rbracket - lbracket);
    strncpy (tmp_email, lbracket + 1, rbracket - lbracket - 1);
    tmp_email[rbracket - lbracket - 1] = '\0';
    *email = tmp_email;

    g_print ("Parsed: %s\n  Name:'%s'\nEmail:'%s'\n",
         value, *name, *email);
}


void
comp_editor_contacts_to_widget (GtkWidget *contacts_entry,
                CalComponent *comp)
{
    GPtrArray *dest_array;
    EDestination *dest;
    GSList *contact_list, *elem;
    char *contacts_string;
    int i;

    cal_component_get_contact_list (comp, &contact_list);
    dest_array = g_ptr_array_new ();
    for (elem = contact_list; elem; elem = elem->next) {
        CalComponentText *t = elem->data;
        char *name, *email;

        parse_contact_string (t->value, &name, &email);

        dest = e_destination_new ();
        e_destination_set_name (dest, name);
        e_destination_set_email (dest, email);
        g_ptr_array_add (dest_array, dest);
        g_free (name);
        g_free (email);
    }
    cal_component_free_text_list (contact_list);

    /* we need destv to be NULL terminated */
    g_ptr_array_add (dest_array, NULL);

    contacts_string = e_destination_exportv ((EDestination**) dest_array->pdata);
    g_print ("Destinations: %s\n", contacts_string ? contacts_string : "");

    bonobo_widget_set_property (BONOBO_WIDGET (contacts_entry),
                    "destinations", contacts_string, NULL);

    g_free (contacts_string);

    /* We free all dest_array except the last NULL we added. */
    for (i = 0; i < dest_array->len - 1; i++) {
        dest = g_ptr_array_index (dest_array, i);
        gtk_object_unref (GTK_OBJECT (dest));
    }
    g_ptr_array_free (dest_array, TRUE);
}


void
comp_editor_contacts_to_component (GtkWidget *contacts_entry,
                   CalComponent *comp)
{
    EDestination **contact_destv;
    GSList *contact_list = NULL, *elem;
    char *contacts_string = NULL;
    CalComponentText *t;
    const char *name, *email;
    int i;

    bonobo_widget_get_property (BONOBO_WIDGET (contacts_entry),
                    "destinations", &contacts_string, NULL);
    g_print ("Contacts string: %s\n", contacts_string ? contacts_string : "");

    contact_destv = e_destination_importv (contacts_string);
    if (contact_destv) {
        for (i = 0; contact_destv[i] != NULL; i++) {
            name = e_destination_get_name (contact_destv[i]);
            email = e_destination_get_email (contact_destv[i]);

            t = g_new0 (CalComponentText, 1);
            t->altrep = NULL;

            /* If both name and email are given, use the standard
               'name <email>' form, otherwise use just the name
               or the email address.
               FIXME: I'm not sure this is correct syntax etc. */
            if (name && name[0] && email && email[0])
                t->value = g_strdup_printf ("%s <%s>",
                                name, email);
            else if (name && name[0])
                t->value = g_strdup (name);
            else
                t->value = g_strdup (email);

            contact_list = g_slist_prepend (contact_list, t);

            gtk_object_unref (GTK_OBJECT (contact_destv[i]));
        }
    }
    g_free (contact_destv);

    contact_list = g_slist_reverse (contact_list);
    cal_component_set_contact_list (comp, contact_list);

    for (elem = contact_list; elem; elem = elem->next) {
        t = elem->data;
        g_free ((char*)t->value);
        g_free (t);
    }
    g_slist_free (contact_list);
}

/**
 * comp_editor_strip_categories:
 * @categories: A string of category names entered by the user.
 * 
 * Takes a string of the form "categ, categ, categ, ..." and removes the
 * whitespace between categories to result in "categ,categ,categ,..."
 * 
 * Return value: The category names stripped of surrounding whitespace
 * and separated with commas.
 **/
char *
comp_editor_strip_categories (const char *categories)
{
    char *new_categories;
    const char *start, *end;
    const char *p;
    char *new_p;

    if (!categories)
        return NULL;

    new_categories = g_new (char, strlen (categories) + 1);

    start = end = NULL;
    new_p = new_categories;

    for (p = categories; *p; p++) {
        int c;

        c = *p;

        if (isspace (c))
            continue;
        else if (c == ',') {
            int len;

            if (!start)
                continue;

            g_assert (start <= end);

            len = end - start + 1;
            strncpy (new_p, start, len);
            new_p[len] = ',';
            new_p += len + 1;

            start = end = NULL;
        } else {
            if (!start) {
                start = p;
                end = p;
            } else
                end = p;
        }
    }

    if (start) {
        int len;

        g_assert (start <= end);

        len = end - start + 1;
        strncpy (new_p, start, len);
        new_p += len;
    }

    *new_p = '\0';

    return new_categories;
}