aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/save-calendar/csv-format.c
blob: 8bfec0df55473f46032a77188ee6469e68913107 (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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/*
 * 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; either
 * version 2 of the License, or (at your option) version 3.
 *
 * 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 *
 * Authors:
 *      Philip Van Hoof <pvanhoof@gnome.org>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

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

#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <libedataserver/e-source.h>
#include <libecal/e-cal-time-util.h>
#include <libedataserver/e-data-server-util.h>
#include <libedataserverui/e-source-selector.h>
#include <libecal/e-cal.h>
#include "calendar/common/authentication.h"
#include <string.h>

#include "format-handler.h"

typedef struct _CsvConfig CsvConfig;
struct _CsvConfig {
    gchar *newline;
    gchar *quote;
    gchar *delimiter;
    gboolean header;
};

static gboolean string_needsquotes (const gchar *value, CsvConfig *config);

typedef struct _CsvPluginData CsvPluginData;
struct _CsvPluginData
{
    GtkWidget *delimiter_entry, *newline_entry, *quote_entry, *header_check;
};

static void
display_error_message (GtkWidget *parent, GError *error)
{
    GtkWidget *dialog;

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

enum { /* CSV helper enum */
    ECALCOMPONENTTEXT,
    ECALCOMPONENTATTENDEE,
    CONSTCHAR
};

/* Some helpers for the csv stuff */
static GString *
add_list_to_csv (GString *line, GSList *list_in, CsvConfig *config, gint type)
{

    /*
     * This one will write 'ECalComponentText' and 'const char' GSLists. It will
     * put quotes around the complete written value if there's was only one value
     * but it required having quotes and if there was more than one value (in which
     * case delimiters are used to separate them, hence the need for the quotes).
     */

    if (list_in) {
        gboolean needquotes = FALSE;
        GSList *list = list_in;
        GString *tmp = NULL;
        gint cnt=0;
        while (list) {
            const gchar *str = NULL;
            if (cnt == 0)
                tmp = g_string_new ("");
            if (cnt > 0)
                needquotes = TRUE;
            switch (type) {
            case ECALCOMPONENTATTENDEE:
                str = ((ECalComponentAttendee*)list->data)->value;
                break;
            case ECALCOMPONENTTEXT:
                str = ((ECalComponentText*)list->data)->value;
                break;
            case CONSTCHAR:
            default:
                str = list->data;
                break;
            }
            if (!needquotes)
                needquotes = string_needsquotes (str, config);
            if (str)
                tmp = g_string_append (tmp, (const gchar *)str);
            list = g_slist_next (list); cnt++;
            if (list)
                tmp = g_string_append (tmp, config->delimiter);
        }

        if (needquotes)
            line = g_string_append (line, config->quote);
        line = g_string_append_len (line, tmp->str, tmp->len);
        g_string_free (tmp, TRUE);
        if (needquotes)
            line = g_string_append (line, config->quote);
    }

    line = g_string_append (line, config->delimiter);
    return line;
}

static GString *
add_nummeric_to_csv (GString *line, gint *nummeric, CsvConfig *config)
{

    /*
     * This one will write {-1}..{00}..{01}..{99}
     * it prepends a 0 if it's < 10 and > -1
     */

    if (nummeric)
        g_string_append_printf (
            line, "%s%d",
            (*nummeric < 10 && *nummeric > -1) ? "0" : "",
            *nummeric);

    return g_string_append (line, config->delimiter);
}

static GString *
add_time_to_csv (GString *line, icaltimetype *time, CsvConfig *config)
{

    if (time) {
        gboolean needquotes = FALSE;
        struct tm mytm =  icaltimetype_to_tm (time);
        gchar *str = (gchar *) g_malloc (sizeof (gchar) * 200);

        /*
         * Translator: the %F %T is the thirth argument for a strftime function.
         * It lets you define the formatting of the date in the csv-file.
         * */
        e_utf8_strftime (str, 200, _("%F %T"), &mytm);

        needquotes = string_needsquotes (str, config);

        if (needquotes)
            line = g_string_append (line, config->quote);

        line = g_string_append (line, str);

        if (needquotes)
            line = g_string_append (line, config->quote);

        g_free (str);

    }

    line = g_string_append (line, config->delimiter);

    return line;
}

static gboolean
string_needsquotes (const gchar *value, CsvConfig *config)
{

    /* This is the actual need for quotes-checker */

    /*
     * These are the simple substring-checks
     *
     * Example: {Mom, can you please do that for me?}
     * Will be written as {"Mom, can you please do that for me?"}
     */

    gboolean needquotes = strstr (value, config->delimiter) ? TRUE:FALSE;

    if (!needquotes) {
        needquotes = strstr (value, config->newline) ? TRUE:FALSE;
        if (!needquotes)
            needquotes = strstr (value, config->quote) ? TRUE:FALSE;
    }

    /*
     * If the special-char is char+onespace (so like {, } {" }, {\n }) and it occurs
     * the value that is going to be written
     *
     * In this case we don't trust the user . . . and are going to quote the string
     * just to play save -- Quoting is always allowed in the CSV format. If you can
     * avoid it, it's better to do so since a lot applications don't support CSV
     * correctly! --.
     *
     * Example: {Mom,can you please do that for me?}
     * This example will be written as {"Mom,can you please do that for me?"} because
     * there's a {,} behind {Mom} and the delimiter is {, } (so we searched only the
     * first character of {, } and didn't trust the user).
     */

    if (!needquotes) {
        gint len = strlen (config->delimiter);
        if ((len == 2) && (config->delimiter[1] == ' ')) {
            needquotes = strchr (value, config->delimiter[0])?TRUE:FALSE;
            if (!needquotes) {
                len = strlen (config->newline);
                if ((len == 2) && (config->newline[1] == ' ')) {
                    needquotes = strchr (value, config->newline[0])?TRUE:FALSE;
                    if (!needquotes) {
                        len = strlen (config->quote);
                        if ((len == 2) && (config->quote[1] == ' ')) {
                            needquotes = strchr
                                (value, config->quote[0])?TRUE:FALSE;
                        }
                    }
                }
            }
        }
    }

    return needquotes;
}

static GString *
add_string_to_csv (GString *line, const gchar *value, CsvConfig *config)
{
    /* Will add a string to the record and will check for the need for quotes */

    if ((value) && (strlen (value)>0)) {
        gboolean needquotes = string_needsquotes (value, config);

        if (needquotes)
            line = g_string_append (line, config->quote);
        line = g_string_append (line, (const gchar *)value);
        if (needquotes)
            line = g_string_append (line, config->quote);
    }
    line = g_string_append (line, config->delimiter);
    return line;
}

/* Convert what the user types to what he probably means */
static gchar *
userstring_to_systemstring (const gchar *userstring)
{
    const gchar *text = userstring;
    gint i=0, len = strlen (text);
    GString *str = g_string_new ("");
    gchar *retval = NULL;

    while (i < len) {
        if (text[i] == '\\') {
            switch (text[i+1]) {
            case 'n':
                str = g_string_append_c (str, '\n');
                i++;
                break;
            case '\\':
                str = g_string_append_c (str, '\\');
                i++;
                break;
            case 'r':
                str = g_string_append_c (str, '\r');
                i++;
                break;
            case 't':
                str = g_string_append_c (str, '\t');
                i++;
                break;
            }
        } else {
            str = g_string_append_c (str, text[i]);
        }

        i++;
    }

    retval = str->str;
    g_string_free (str, FALSE);

    return retval;
}

static void
do_save_calendar_csv (FormatHandler *handler,
                      ESourceSelector *selector,
                      ECalSourceType type,
                      gchar *dest_uri)
{

    /*
     * According to some documentation about CSV, newlines 'are' allowed
     * in CSV-files. But you 'do' have to put the value between quotes.
     * The helper 'string_needsquotes' will check for that
     *
     * http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm
     * http://www.creativyst.com/cgi-bin/Prod/15/eg/csv2xml.pl
     */

    ESource *primary_source;
    ECal *source_client;
    GError *error = NULL;
    GList *objects=NULL;
    GOutputStream *stream;
    GString *line = NULL;
    CsvConfig *config = NULL;
    CsvPluginData *d = handler->data;
    const gchar *tmp = NULL;

    if (!dest_uri)
        return;

    primary_source = e_source_selector_get_primary_selection (selector);

    /* open source client */
    source_client = e_auth_new_cal_from_source (primary_source, type);
    if (!e_cal_open (source_client, TRUE, &error)) {
        display_error_message (gtk_widget_get_toplevel (GTK_WIDGET (selector)), error);
        g_object_unref (source_client);
        g_error_free (error);
        return;
    }

    config = g_new (CsvConfig, 1);

    tmp = gtk_entry_get_text (GTK_ENTRY (d->delimiter_entry));
    config->delimiter = userstring_to_systemstring (tmp?tmp:", ");
    tmp = gtk_entry_get_text (GTK_ENTRY (d->newline_entry));
    config->newline = userstring_to_systemstring (tmp?tmp:"\\n");
    tmp = gtk_entry_get_text (GTK_ENTRY (d->quote_entry));
    config->quote = userstring_to_systemstring (tmp?tmp:"\"");
    config->header = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (d->header_check));

    stream = open_for_writing (
        GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (selector))),
        dest_uri, &error);

    if (stream && e_cal_get_object_list_as_comp (source_client, "#t", &objects, NULL)) {

        if (config->header) {

            gint i=0;

            static const gchar *labels[] = {
                 N_("UID"),
                 N_("Summary"),
                 N_("Description List"),
                 N_("Categories List"),
                 N_("Comment List"),
                 N_("Completed"),
                 N_("Created"),
                 N_("Contact List"),
                 N_("Start"),
                 N_("End"),
                 N_("Due"),
                 N_("percent Done"),
                 N_("Priority"),
                 N_("URL"),
                 N_("Attendees List"),
                 N_("Location"),
                 N_("Modified"),
            };

            line = g_string_new ("");
            for (i=0;i<G_N_ELEMENTS (labels);i++) {
                if (i>0)
                    line = g_string_append (line, config->delimiter);
                line = g_string_append (line, _(labels[i]));
            }

            line = g_string_append (line, config->newline);

            g_output_stream_write_all (stream, line->str, line->len, NULL, NULL, NULL);
            g_string_free (line, TRUE);
        }

        while (objects != NULL) {
            ECalComponent *comp = objects->data;
            gchar *delimiter_temp = NULL;
            const gchar *temp_constchar;
            GSList *temp_list;
            ECalComponentDateTime temp_dt;
            struct icaltimetype *temp_time;
            gint *temp_int;
            ECalComponentText temp_comptext;

            line = g_string_new ("");

            /* Getting the stuff */
            e_cal_component_get_uid (comp, &temp_constchar);
            line = add_string_to_csv (line, temp_constchar, config);

            e_cal_component_get_summary (comp, &temp_comptext);
            line = add_string_to_csv (line, temp_comptext.value, config);

            e_cal_component_get_description_list (comp, &temp_list);
            line = add_list_to_csv (line, temp_list, config, ECALCOMPONENTTEXT);
            if (temp_list)
                e_cal_component_free_text_list (temp_list);

            e_cal_component_get_categories_list (comp, &temp_list);
            line = add_list_to_csv (line, temp_list, config, CONSTCHAR);
            if (temp_list)
                e_cal_component_free_categories_list (temp_list);

            e_cal_component_get_comment_list (comp, &temp_list);
            line = add_list_to_csv (line, temp_list, config, ECALCOMPONENTTEXT);
            if (temp_list)
                e_cal_component_free_text_list (temp_list);

            e_cal_component_get_completed (comp, &temp_time);
            line = add_time_to_csv (line, temp_time, config);
            if (temp_time)
                e_cal_component_free_icaltimetype (temp_time);

            e_cal_component_get_created (comp, &temp_time);
            line = add_time_to_csv (line, temp_time, config);
            if (temp_time)
                e_cal_component_free_icaltimetype (temp_time);

            e_cal_component_get_contact_list (comp, &temp_list);
            line = add_list_to_csv (line, temp_list, config, ECALCOMPONENTTEXT);
            if (temp_list)
                e_cal_component_free_text_list (temp_list);

            e_cal_component_get_dtstart (comp, &temp_dt);
            line = add_time_to_csv (line, temp_dt.value ? temp_dt.value : NULL, config);
            e_cal_component_free_datetime (&temp_dt);

            e_cal_component_get_dtend (comp, &temp_dt);
            line = add_time_to_csv (line, temp_dt.value ? temp_dt.value : NULL, config);
            e_cal_component_free_datetime (&temp_dt);

            e_cal_component_get_due (comp, &temp_dt);
            line = add_time_to_csv (line, temp_dt.value ? temp_dt.value : NULL, config);
            e_cal_component_free_datetime (&temp_dt);

            e_cal_component_get_percent (comp, &temp_int);
            line = add_nummeric_to_csv (line, temp_int, config);

            e_cal_component_get_priority (comp, &temp_int);
            line = add_nummeric_to_csv (line, temp_int, config);

            e_cal_component_get_url (comp, &temp_constchar);
            line = add_string_to_csv (line, temp_constchar, config);

            if (e_cal_component_has_attendees (comp)) {
                e_cal_component_get_attendee_list (comp, &temp_list);
                line = add_list_to_csv (line, temp_list, config, ECALCOMPONENTATTENDEE);
                if (temp_list)
                    e_cal_component_free_attendee_list (temp_list);
            } else {
                line = add_list_to_csv (line, NULL, config, ECALCOMPONENTATTENDEE);
            }

            e_cal_component_get_location (comp, &temp_constchar);
            line = add_string_to_csv (line, temp_constchar, config);

            e_cal_component_get_last_modified (comp, &temp_time);

            /* Append a newline (record delimiter) */
            delimiter_temp = config->delimiter;
            config->delimiter = config->newline;

            line = add_time_to_csv (line, temp_time, config);

            /* And restore for the next record */
            config->delimiter = delimiter_temp;

            /* Important note!
             * The documentation is not requiring this!
             *
             * if (temp_time) e_cal_component_free_icaltimetype (temp_time);
             *
             * Please uncomment and fix documentation if untrue
             * http://www.gnome.org/projects/evolution/
             *  developer-doc/libecal/ECalComponent.html
             *  #e-cal-component-get-last-modified
             */
            g_output_stream_write_all (stream, line->str, line->len, NULL, NULL, &error);

            /* It's written, so we can free it */
            g_string_free (line, TRUE);

            objects = g_list_next (objects);
        }

        g_output_stream_close (stream, NULL, NULL);
    }

    if (stream)
        g_object_unref (stream);

    g_object_unref (source_client);

    g_free (config->delimiter);
    g_free (config->quote);
    g_free (config->newline);
    g_free (config);

    if (error) {
        display_error_message (gtk_widget_get_toplevel (GTK_WIDGET (selector)), error);
        g_error_free (error);
    }

    return;
}

static GtkWidget *
create_options_widget (FormatHandler *handler)
{
    GtkWidget *table = gtk_table_new (4, 2, FALSE), *label = NULL,
          *csv_options = gtk_expander_new_with_mnemonic (
            _("A_dvanced options for the CSV format")),
          *vbox = gtk_vbox_new (FALSE, 0);
    CsvPluginData *d = handler->data;

    d->delimiter_entry = gtk_entry_new ();
    d->newline_entry = gtk_entry_new ();
    d->quote_entry = gtk_entry_new ();
    d->header_check = gtk_check_button_new_with_mnemonic (_("Prepend a _header"));

    /* Advanced CSV options */
    gtk_entry_set_text (GTK_ENTRY(d->delimiter_entry), ", ");
    gtk_entry_set_text (GTK_ENTRY(d->quote_entry), "\"");
    gtk_entry_set_text (GTK_ENTRY(d->newline_entry), "\\n");

    gtk_table_set_row_spacings (GTK_TABLE (table), 5);
    gtk_table_set_col_spacings (GTK_TABLE (table), 5);
    label = gtk_label_new_with_mnemonic (_("_Value delimiter:"));
    gtk_misc_set_alignment (GTK_MISC (label), 0, 0.0);
    gtk_label_set_mnemonic_widget (GTK_LABEL (label), d->delimiter_entry);
    gtk_table_attach (GTK_TABLE (table), label, 0, 1, 0, 1,
              (GtkAttachOptions) (GTK_FILL), (GtkAttachOptions) (0), 0, 0);
    gtk_table_attach (GTK_TABLE (table), d->delimiter_entry, 1, 2, 0, 1,
              (GtkAttachOptions) (GTK_EXPAND | GTK_FILL), (GtkAttachOptions) (0), 0, 0);
    label = gtk_label_new_with_mnemonic (_("_Record delimiter:"));
    gtk_misc_set_alignment (GTK_MISC (label), 0, 0.0);
    gtk_label_set_mnemonic_widget (GTK_LABEL (label), d->newline_entry);
    gtk_table_attach (GTK_TABLE (table), label, 0, 1, 1, 2,
              (GtkAttachOptions) (GTK_FILL), (GtkAttachOptions) (0), 0, 0);
    gtk_table_attach (GTK_TABLE (table), d->newline_entry, 1, 2, 1, 2,
              (GtkAttachOptions) (GTK_EXPAND | GTK_FILL), (GtkAttachOptions) (0), 0, 0);
    label = gtk_label_new_with_mnemonic (_("_Encapsulate values with:"));
    gtk_misc_set_alignment (GTK_MISC (label), 0, 0.0);
    gtk_label_set_mnemonic_widget (GTK_LABEL (label), d->quote_entry);
    gtk_table_attach (GTK_TABLE (table), label, 0, 1, 2, 3,
              (GtkAttachOptions) (GTK_FILL), (GtkAttachOptions) (0), 0, 0);
    gtk_table_attach (GTK_TABLE (table), d->quote_entry, 1, 2, 2, 3,
              (GtkAttachOptions) (GTK_EXPAND | GTK_FILL), (GtkAttachOptions) (0), 0, 0);

    gtk_box_pack_start (GTK_BOX (vbox), d->header_check, TRUE, TRUE, 0);
    gtk_box_pack_start (GTK_BOX (vbox), table, TRUE, TRUE, 0);
    gtk_widget_show_all (vbox);

    gtk_container_add (GTK_CONTAINER (csv_options), vbox);

    return csv_options;
}

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

    handler->isdefault = FALSE;
    handler->combo_label = _("Comma separated value format (.csv)");
    handler->filename_ext = ".csv";
    handler->data = g_new (CsvPluginData, 1);
    handler->options_widget = create_options_widget (handler);
    handler->save = do_save_calendar_csv;

    return handler;
}