aboutsummaryrefslogtreecommitdiffstats
path: root/addressbook/importers/evolution-vcard-importer.c
blob: 8a141466eb8424d5be7874731b068eb229d1f74e (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* Evolution calendar importer component
 *
 * Copyright (C) 2004  Novell, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU 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 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.
 *
 * Authors: Chris Toshok <toshok@ximian.com>
 *          JP Rosevear <jpr@ximian.com>
 *
 */

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

#include <stdio.h>
#include <string.h>

#include <gtk/gtkcheckbutton.h>
#include <gtk/gtkhbox.h>
#include <gtk/gtkvbox.h>
#include <gtk/gtkmain.h>
#include <gtk/gtklabel.h>
#include <gtk/gtkradiobutton.h>
#include <gtk/gtknotebook.h>
#include <bonobo/bonobo-context.h>
#include <bonobo/bonobo-shlib-factory.h>
#include <bonobo/bonobo-control.h>

#include <libebook/e-book.h>
#include <libedataserverui/e-source-selector.h>

#include <importer/evolution-importer.h>
#include <importer/GNOME_Evolution_Importer.h>
#include <util/eab-book-util.h>
#include <libebook/e-destination.h>

#define COMPONENT_FACTORY_IID "OAFIID:GNOME_Evolution_Addressbook_VCard_ImporterFactory:" BASE_VERSION
#define COMPONENT_IID "OAFIID:GNOME_Evolution_Addressbook_VCard_Importer:" BASE_VERSION

typedef struct {
    ESource *primary;
    
    GList *contactlist;
    GList *iterator;
    EBook *book;
    gboolean ready;
} VCardImporter;

static void
add_to_notes (EContact *contact, EContactField field)
{
    const gchar *old_text;
    const gchar *field_text;
    gchar       *new_text;

    old_text = e_contact_get_const (contact, E_CONTACT_NOTE);
    if (old_text && strstr (old_text, e_contact_pretty_name (field)))
        return;

    field_text = e_contact_get_const (contact, field);
    if (!field_text || !*field_text)
        return;

    new_text = g_strdup_printf ("%s%s%s: %s",
                    old_text ? old_text : "",
                    old_text && *old_text &&
                    *(old_text + strlen (old_text) - 1) != '\n' ? "\n" : "",
                    e_contact_pretty_name (field), field_text);
    e_contact_set (contact, E_CONTACT_NOTE, new_text);
    g_free (new_text);
}

/* EvolutionImporter methods */
static void
process_item_fn (EvolutionImporter *importer,
         CORBA_Object listener,
         void *closure,
         CORBA_Environment *ev)
{
    VCardImporter *gci = (VCardImporter *) closure;
    EContact *contact;
    EContactPhoto *photo;
    GList *attrs, *attr;

    if (gci->iterator == NULL)
        gci->iterator = gci->contactlist;
    
    if (gci->ready == FALSE) {
        GNOME_Evolution_ImporterListener_notifyResult (listener,
                                   GNOME_Evolution_ImporterListener_NOT_READY,
                                   gci->iterator ? TRUE : FALSE, 
                                   ev);
        return;
    }
    
    if (gci->iterator == NULL) {
        GNOME_Evolution_ImporterListener_notifyResult (listener,
                                   GNOME_Evolution_ImporterListener_UNSUPPORTED_OPERATION,
                                   FALSE, ev);
        return;
    }
    
    contact = gci->iterator->data;

    /* Apple's addressbook.app exports PHOTO's without a TYPE
       param, so let's figure out the format here if there's a
       PHOTO attribute missing a TYPE param.

       this is sort of a hack, as EContact sets the type for us if
       we use the setter.  so let's e_contact_get + e_contact_set
       on E_CONTACT_PHOTO.
    */
    photo = e_contact_get (contact, E_CONTACT_PHOTO);
    if (photo) {
        e_contact_set (contact, E_CONTACT_PHOTO, photo);
        e_contact_photo_free (photo);
    }

    /* Deal with our XML EDestination stuff in EMAIL attributes, if there is any. */
    attrs = e_contact_get_attributes (contact, E_CONTACT_EMAIL);
    for (attr = attrs; attr; attr = attr->next) {
        EVCardAttribute *a = attr->data;
        GList *v = e_vcard_attribute_get_values (a);

        if (v && v->data) {
            if (!strncmp ((char*)v->data, "<?xml", 5)) {
                EDestination *dest = e_destination_import ((char*)v->data);

                e_destination_export_to_vcard_attribute (dest, a);

                g_object_unref (dest);

            }
        }
    }
    e_contact_set_attributes (contact, E_CONTACT_EMAIL, attrs);

    /*
      Deal with TEL attributes that don't conform to what we need.

      1. if there's no location (HOME/WORK/OTHER), default to OTHER.
      2. if there's *only* a location specified, default to VOICE.
    */
    attrs = e_vcard_get_attributes (E_VCARD (contact));
    for (attr = attrs; attr; attr = attr->next) {
        EVCardAttribute *a = attr->data;
        gboolean location_only = TRUE;
        gboolean no_location = TRUE;
        GList *params, *param;

        if (g_ascii_strcasecmp (e_vcard_attribute_get_name (a),
                    EVC_TEL))
            continue;

        params = e_vcard_attribute_get_params (a);
        for (param = params; param; param = param->next) {
            EVCardAttributeParam *p = param->data;
            GList *vs, *v;

            if (g_ascii_strcasecmp (e_vcard_attribute_param_get_name (p),
                        EVC_TYPE))
                continue;

            vs = e_vcard_attribute_param_get_values (p);
            for (v = vs; v; v = v->next) {
                if (!g_ascii_strcasecmp ((char*)v->data, "WORK") ||
                    !g_ascii_strcasecmp ((char*)v->data, "HOME") ||
                    !g_ascii_strcasecmp ((char*)v->data, "OTHER"))
                    no_location = FALSE;
                else
                    location_only = FALSE;
            }
        }

        if (location_only) {
            /* add VOICE */
            e_vcard_attribute_add_param_with_value (a,
                                e_vcard_attribute_param_new (EVC_TYPE),
                                "VOICE");
        }
        if (no_location) {
            /* add OTHER */
            e_vcard_attribute_add_param_with_value (a,
                                e_vcard_attribute_param_new (EVC_TYPE),
                                "OTHER");
        }
    }

    /*
      Deal with ADR attributes that don't conform to what we need.

      if HOME or WORK isn't specified, add TYPE=OTHER.
    */
    attrs = e_vcard_get_attributes (E_VCARD (contact));
    for (attr = attrs; attr; attr = attr->next) {
        EVCardAttribute *a = attr->data;
        gboolean no_location = TRUE;
        GList *params, *param;

        if (g_ascii_strcasecmp (e_vcard_attribute_get_name (a),
                    EVC_ADR))
            continue;

        params = e_vcard_attribute_get_params (a);
        for (param = params; param; param = param->next) {
            EVCardAttributeParam *p = param->data;
            GList *vs, *v;

            if (g_ascii_strcasecmp (e_vcard_attribute_param_get_name (p),
                        EVC_TYPE))
                continue;

            vs = e_vcard_attribute_param_get_values (p);
            for (v = vs; v; v = v->next) {
                if (!g_ascii_strcasecmp ((char*)v->data, "WORK") ||
                    !g_ascii_strcasecmp ((char*)v->data, "HOME"))
                    no_location = FALSE;
            }
        }

        if (no_location) {
            /* add OTHER */
            e_vcard_attribute_add_param_with_value (a,
                                e_vcard_attribute_param_new (EVC_TYPE),
                                "OTHER");
        }
    }

    /* Work around the fact that these fields no longer show up in the UI */
    add_to_notes (contact, E_CONTACT_OFFICE);
    add_to_notes (contact, E_CONTACT_SPOUSE);
    add_to_notes (contact, E_CONTACT_BLOG_URL);

    /* FIXME Error checking */
    e_book_add_contact (gci->book, contact, NULL);
    
    gci->iterator = gci->iterator->next;
    
    GNOME_Evolution_ImporterListener_notifyResult (listener,
                               GNOME_Evolution_ImporterListener_OK,
                               gci->iterator ? TRUE : FALSE, 
                               ev);
    if (ev->_major != CORBA_NO_EXCEPTION) {
        g_warning ("Error notifying listeners.");
    }
    
    return;
}

static char *supported_extensions[3] = {
    ".vcf",
    ".gcrd",
    NULL
};

#define BOM (gunichar2)0xFEFF
#define ANTIBOM (gunichar2)0xFFFE

static gboolean
has_bom (const gunichar2 *utf16)
{
    
    if ((utf16 == NULL) || (*utf16 == '\0')) {
        return FALSE;
    }

    return ((*utf16 == BOM) || (*utf16 == ANTIBOM));
}

static void
fix_utf16_endianness (gunichar2 *utf16)
{
    gunichar2 *it;


    if ((utf16 == NULL) || (*utf16 == '\0')) {
        return;     
    }

    if (*utf16 != ANTIBOM) {
        return;
    }

    for (it = utf16; *it != '\0'; it++) {
        *it = GUINT16_SWAP_LE_BE (*it);
    }
}

/* Converts an UTF-16 string to an UTF-8 string removing the BOM character 
 * WARNING: this may modify the utf16 argument if the function detects the
 * string isn't using the local endianness
 */
static gchar *
utf16_to_utf8 (gunichar2 *utf16)
{
    
    if (utf16 == NULL) {
        return NULL;
    }

    fix_utf16_endianness (utf16);

    if (*utf16 == BOM) {
        utf16++;
    }

    return g_utf16_to_utf8 (utf16, -1, NULL, NULL, NULL);
}


enum _VCardEncoding {
    VCARD_ENCODING_NONE,
    VCARD_ENCODING_UTF8,
    VCARD_ENCODING_UTF16,
    VCARD_ENCODING_LOCALE
};

typedef enum _VCardEncoding VCardEncoding;


/* Actually check the contents of this file */
static VCardEncoding
guess_vcard_encoding (const char *filename)
{
    FILE *handle;
    char line[4096];
    char *line_utf8;
    VCardEncoding encoding = VCARD_ENCODING_NONE;

    handle = fopen (filename, "r");
    if (handle == NULL) {
        g_print ("\n");
        return VCARD_ENCODING_NONE;
    }
        
    fgets (line, 4096, handle);
    if (line == NULL) {
        fclose (handle);
        g_print ("\n");
        return VCARD_ENCODING_NONE;
    }
    fclose (handle);
    
    if (has_bom ((gunichar2*)line)) {
        gunichar2 *utf16 = (gunichar2*)line;
        /* Check for a BOM to try to detect UTF-16 encoded vcards
         * (MacOSX address book creates such vcards for example)
         */
        line_utf8 = utf16_to_utf8 (utf16);
        if (line_utf8 == NULL) {
            return VCARD_ENCODING_NONE;
        }
        encoding = VCARD_ENCODING_UTF16;
    } else if (g_utf8_validate (line, -1, NULL)) {
        line_utf8 = g_strdup (line);
        encoding = VCARD_ENCODING_UTF8;
    } else {
        line_utf8 = g_locale_to_utf8 (line, -1, NULL, NULL, NULL);
        if (line_utf8 == NULL) {
            return VCARD_ENCODING_NONE;
        }
        encoding = VCARD_ENCODING_LOCALE;
    }

    if (g_ascii_strncasecmp (line_utf8, "BEGIN:VCARD", 11) != 0) {
        encoding = VCARD_ENCODING_NONE;
    }

    g_free (line_utf8);
    return encoding;
}

static gboolean 
check_file_is_vcard (const char *filename)
{
    return guess_vcard_encoding (filename) != VCARD_ENCODING_NONE;
}

static void
primary_selection_changed_cb (ESourceSelector *selector, gpointer data)
{
    VCardImporter *gci = data;

    if (gci->primary)
        g_object_unref (gci->primary);
    gci->primary = g_object_ref (e_source_selector_peek_primary_selection (selector));
}

static void
create_control_fn (EvolutionImporter *importer, Bonobo_Control *control, void *closure)
{
    VCardImporter *gci = closure;
    GtkWidget *vbox, *selector;
    ESource *primary;
    ESourceList *source_list;   
    
    vbox = gtk_vbox_new (FALSE, FALSE);
    
    /* FIXME Better error handling */
    if (!e_book_get_addressbooks (&source_list, NULL))
        return;     

    selector = e_source_selector_new (source_list);
    e_source_selector_show_selection (E_SOURCE_SELECTOR (selector), FALSE);
    gtk_box_pack_start (GTK_BOX (vbox), selector, FALSE, TRUE, 6);
    
    /* FIXME What if no sources? */
    primary = e_source_list_peek_source_any (source_list);
    e_source_selector_set_primary_selection (E_SOURCE_SELECTOR (selector), primary);
    if (!gci->primary)
        gci->primary = g_object_ref (primary);
    g_object_unref (source_list);
        
    g_signal_connect (G_OBJECT (selector), "primary_selection_changed",
              G_CALLBACK (primary_selection_changed_cb), gci);

    gtk_widget_show_all (vbox);
    
    *control = BONOBO_OBJREF (bonobo_control_new (vbox));
}

static gboolean
support_format_fn (EvolutionImporter *importer,
           const char *filename,
           void *closure)
{
    char *ext;
    int i;

    ext = strrchr (filename, '.');
    if (ext == NULL) {
        return check_file_is_vcard (filename);
    }
    for (i = 0; supported_extensions[i] != NULL; i++) {
        if (g_ascii_strcasecmp (supported_extensions[i], ext) == 0) {
            return check_file_is_vcard (filename);
        }
    }

    return FALSE;
}

static void
importer_destroy_cb (gpointer data,
             GObject *where_object_was)
{
    VCardImporter *gci = data;

    if (gci->primary)
        g_object_unref (gci->primary);
    
    if (gci->book)
        g_object_unref (gci->book);

    g_list_foreach (gci->contactlist, (GFunc) g_object_unref, NULL);
    g_list_free (gci->contactlist);

    g_free (gci);
}

static gboolean
load_file_fn (EvolutionImporter *importer,
          const char *filename,
          void *closure)
{
    VCardImporter *gci;
    char *contents;
    VCardEncoding encoding;

    encoding = guess_vcard_encoding (filename);
    if (encoding == VCARD_ENCODING_NONE) {
        return FALSE;
    }

    gci = (VCardImporter *) closure;
    gci->contactlist = NULL;
    gci->iterator = NULL;
    gci->ready = FALSE;
    
    /* Load the book */
    gci->book = e_book_new (gci->primary, NULL);
    if (!gci->book) {
        g_message (G_STRLOC ":Couldn't create EBook.");
        return FALSE;
    }
    e_book_open (gci->book, TRUE, NULL);

    /* Load the file and the contacts */
    if (!g_file_get_contents (filename, &contents, NULL, NULL)) {
        g_message (G_STRLOC ":Couldn't read file.");
        return FALSE;
    }

    if (encoding == VCARD_ENCODING_UTF16) {
        gchar *tmp;
        gunichar2 *contents_utf16 = (gunichar2*)contents;
        tmp = utf16_to_utf8 (contents_utf16);
        g_free (contents);
        contents = tmp;
    } else if (encoding == VCARD_ENCODING_LOCALE) {
        gchar *tmp;
        tmp = g_locale_to_utf8 (contents, -1, NULL, NULL, NULL);
        g_free (contents);
        contents = tmp;
    }

    gci->contactlist = eab_contact_list_from_string (contents);
    g_free (contents);

    gci->ready = TRUE;

    return TRUE;
}
                       
static BonoboObject *
factory_fn (BonoboGenericFactory *_factory,
        const char *component_id,
        void *closure)
{
    EvolutionImporter *importer;
    VCardImporter *gci;

    if (!strcmp (component_id, COMPONENT_IID)) {
        gci = g_new0 (VCardImporter, 1);
        importer = evolution_importer_new (create_control_fn, support_format_fn, 
                           load_file_fn, process_item_fn, NULL, gci);
    
        g_object_weak_ref (G_OBJECT (importer),
                   importer_destroy_cb, gci);
        return BONOBO_OBJECT (importer);
    }
    else {
        g_warning (COMPONENT_FACTORY_IID ": Don't know what to do with %s", component_id);
        return NULL;
    }
}

BONOBO_ACTIVATION_SHLIB_FACTORY (COMPONENT_FACTORY_IID, "Evolution VCard Importer Factory", factory_fn, NULL)