aboutsummaryrefslogtreecommitdiffstats
path: root/addressbook/importers/evolution-ldif-importer.c
blob: 9259b8f8796dce4189fa06cf6f3edceb38668a29 (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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/* 
 * LDIF importer.  LDIF is the file format of an exported Netscape
 * addressbook.
 *
 * Framework copied from evolution-gnomecard-importer.c
 *
 * Michael M. Morrison (mmorrison@kqcorp.com)
 *
 * Multi-line value support, mailing list support, base64 support, and
 * various fixups: Chris Toshok (toshok@ximian.com)
 *
 * Made re-entrant, converted to eplugin, Michael Zucchi <notzed@ximian.com>
 */

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

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

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

#include <gtk/gtkvbox.h>

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

#include <libebook/e-destination.h>

#include "e-util/e-import.h"

#include "evolution-addressbook-importers.h"

typedef struct {
    EImport *import;
    EImportTarget *target;

    guint idle_id;

    GHashTable *dn_contact_hash;

    int state;      /* 0 - initial scan, 1 - list cards, 2 - cancelled/complete */
    FILE *file;
    gulong size;

    EBook *book;

    GSList *contacts;
    GSList *list_contacts;
    GSList *list_iterator;
} LDIFImporter;

static void ldif_import_done(LDIFImporter *gci);


static struct {
    char *ldif_attribute;
    EContactField contact_field;
#define FLAG_HOME_ADDRESS   0x01
#define FLAG_WORK_ADDRESS   0x02
#define FLAG_LIST       0x04
#define FLAG_BOOLEAN        0x08
    int flags;
}
ldif_fields[] = {
    { "cn", E_CONTACT_FULL_NAME },
    { "mail", E_CONTACT_EMAIL, FLAG_LIST },
    { "mozillaSecondEmail", E_CONTACT_EMAIL_2},
#if 0
    { "givenname", E_CONTACT_GIVEN_NAME },
#endif
    { "sn", E_CONTACT_FAMILY_NAME },
    { "xmozillanickname", E_CONTACT_NICKNAME },

    { "o", E_CONTACT_ORG },
    { "ou", E_CONTACT_ORG_UNIT },
    { "title", E_CONTACT_TITLE },

    { "locality", 0, FLAG_WORK_ADDRESS },
    { "l", 0, FLAG_WORK_ADDRESS },
    { "mozillahomelocalityname", 0, FLAG_HOME_ADDRESS },
    { "st", 0, FLAG_WORK_ADDRESS },
    { "mozillaHomeState", 0, FLAG_HOME_ADDRESS },
    { "streetaddress", 0, FLAG_WORK_ADDRESS },
    { "postalcode", 0, FLAG_WORK_ADDRESS },
    { "mozillaHomePostalCode", 0, FLAG_HOME_ADDRESS },
    { "countryname", 0, FLAG_WORK_ADDRESS },
    { "c", 0, FLAG_WORK_ADDRESS },
    { "mozillaHomeCountryName", 0, FLAG_HOME_ADDRESS },
    { "postalAddress", 0, FLAG_WORK_ADDRESS },
    { "homePostalAddress", 0, FLAG_HOME_ADDRESS },
    { "mozillaPostalAddress2", 0, FLAG_WORK_ADDRESS },
    { "mozillaHomePostalAddress2", 0, FLAG_HOME_ADDRESS },

    { "telephonenumber", E_CONTACT_PHONE_BUSINESS},
    { "homephone", E_CONTACT_PHONE_HOME },
    { "facsimiletelephonenumber", E_CONTACT_PHONE_BUSINESS_FAX },
    { "pagerphone", E_CONTACT_PHONE_PAGER },
    { "cellphone", E_CONTACT_PHONE_MOBILE },
    { "mobile", E_CONTACT_PHONE_MOBILE },

    { "homeurl", E_CONTACT_HOMEPAGE_URL },
    { "mozillaHomeUrl", E_CONTACT_HOMEPAGE_URL },

    { "description", E_CONTACT_NOTE },

    { "xmozillausehtmlmail", E_CONTACT_WANTS_HTML, FLAG_BOOLEAN },

    { "nsAIMid", E_CONTACT_IM_AIM, FLAG_LIST },
    { "mozilla_AimScreenName", E_CONTACT_IM_AIM, FLAG_LIST }
};
static int num_ldif_fields = sizeof(ldif_fields) / sizeof (ldif_fields[0]);

static unsigned char base64_rank[256] = {
    255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
    255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
    255,255,255,255,255,255,255,255,255,255,255, 62,255,255,255, 63,
     52, 53, 54, 55, 56, 57, 58, 59, 60, 61,255,255,255,  0,255,255,
    255,  0,  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,255,255,255,255,255,
    255, 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,255,255,255,255,255,
    255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
    255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
    255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
    255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
    255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
    255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
    255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
    255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
};

/**
 * base64_decode_step: decode a chunk of base64 encoded data
 * @in: input stream
 * @len: max length of data to decode
 * @out: output stream
 * @state: holds the number of bits that are stored in @save
 * @save: leftover bits that have not yet been decoded
 *
 * Decodes a chunk of base64 encoded data
 **/
static int
base64_decode_step(unsigned char *in, int len, unsigned char *out, int *state, unsigned int *save)
{
    register unsigned char *inptr, *outptr;
    unsigned char *inend, c;
    register unsigned int v;
    int i;

    inend = in+len;
    outptr = out;

    /* convert 4 base64 bytes to 3 normal bytes */
    v=*save;
    i=*state;
    inptr = in;
    while (inptr<inend) {
        c = base64_rank[*inptr++];
        if (c != 0xff) {
            v = (v<<6) | c;
            i++;
            if (i==4) {
                *outptr++ = v>>16;
                *outptr++ = v>>8;
                *outptr++ = v;
                i=0;
            }
        }
    }

    *save = v;
    *state = i;

    /* quick scan back for '=' on the end somewhere */
    /* fortunately we can drop 1 output char for each trailing = (upto 2) */
    i=2;
    while (inptr>in && i) {
        inptr--;
        if (base64_rank[*inptr] != 0xff) {
            if (*inptr == '=')
                outptr--;
            i--;
        }
    }

    /* if i!= 0 then there is a truncation error! */
    return outptr-out;
}

static int
base64_decode_simple (char *data, int len)
{
    int state = 0;
    unsigned int save = 0;

    return base64_decode_step ((unsigned char *)data, len,
                   (unsigned char *)data, &state, &save);
}

static GString *
getValue( char **src )
{
    GString *dest = g_string_new("");
    char *s = *src;
    gboolean need_base64 = (*s == ':');

 copy_line:
    while( *s != 0 && *s != '\n' && *s != '\r' )
        dest = g_string_append_c (dest, *s++);

    if (*s == '\r') s++;
    if (*s == '\n') s++;

    /* check for continuation here */
    if (*s == ' ') {
        s++;
        goto copy_line;
    }

    if (need_base64) {
        int new_len;
        /* it's base64 encoded */
        dest = g_string_erase (dest, 0, 2);
        new_len = base64_decode_simple (dest->str, strlen (dest->str));
        dest = g_string_truncate (dest, new_len);
    }

    *src = s;

    return dest;
}

static void
populate_contact_address (EContactAddress *address, char *attr, char *value)
{
    if (!g_ascii_strcasecmp (attr, "locality") || 
        !g_ascii_strcasecmp (attr, "l") ||
        !g_ascii_strcasecmp (attr, "mozillaHomeLocalityName"))
        address->locality = g_strdup (value);
    else if (!g_ascii_strcasecmp (attr, "countryname") ||
         !g_ascii_strcasecmp (attr, "c") ||
         !g_ascii_strcasecmp (attr, "mozillaHomeCountryName"))
        address->country = g_strdup (value);
    else if (!g_ascii_strcasecmp (attr, "postalcode") ||
         !g_ascii_strcasecmp (attr, "mozillaHomePostalCode"))
        address->code = g_strdup (value);
    else if (!g_ascii_strcasecmp (attr, "st") ||
         !g_ascii_strcasecmp (attr, "mozillaHomeState"))
        address->region = g_strdup (value);
    else if (!g_ascii_strcasecmp (attr, "streetaddress"))
        address->street = g_strdup (value);
    else if (!g_ascii_strcasecmp (attr, "mozillaPostalAddress2") ||
         !g_ascii_strcasecmp (attr, "mozillaHomePostalAddress2")) {
        if (address->ext && *address->ext) {
            char *temp = g_strdup (address->ext);
            g_free (address->ext);
            address->ext = g_strconcat (temp, ",\n", value, NULL);
            g_free (temp);
        }
        else { 
            address->ext = g_strdup (value);
        }
    }
    else if (!g_ascii_strcasecmp (attr, "postalAddress") ||
         !g_ascii_strcasecmp (attr, "homepostalAddress")) {
        char *c, *i, *addr_field;

        addr_field =  g_strdup (value);
        i = addr_field;
        for (c = addr_field; *c != '\0'; c++) {
            i++;
            if (*c == ',' &&  *i != '\0' && *i == ' ') {
                *i = '\n';
            }
        }
        if (address->ext && *address->ext) {
            char *temp = g_strdup (address->ext);
            g_free (address->ext);
            address->ext = g_strconcat (addr_field, ",\n", temp, NULL);
            g_free (temp);
            g_free (addr_field);
        }
        else {
            address->ext = addr_field;
        }
    }
}

static gboolean
parseLine (LDIFImporter *gci, EContact *contact, 
       EContactAddress *work_address, EContactAddress *home_address, 
       char **buf)
{
    char *ptr;
    char *colon, *value;
    gboolean field_handled;
    GString *ldif_value;

    ptr = *buf;

    /* if the string is empty, return */
    if (*ptr == '\0') {
        *buf = NULL;
        return TRUE;
    }

    /* skip comment lines */
    if (*ptr == '#') {
        ptr = strchr (ptr, '\n');
        if (!ptr)
            *buf = NULL;
        else
            *buf = ptr + 1;
        return TRUE;
    }

    /* first, check for a 'continuation' line */
    if( ptr[0] == ' ' && ptr[1] != '\n' ) {
        g_warning ("unexpected continuation line");
        return FALSE;
    }

    colon = (char *)strchr( ptr, ':' );
    if (colon) {
        int i;

        *colon = 0;
        value = colon + 1;
        while ( isspace(*value) )
            value++;

        ldif_value = getValue(&value );

        field_handled = FALSE;
        for (i = 0; i < num_ldif_fields; i ++) {
            if (!g_ascii_strcasecmp (ptr, ldif_fields[i].ldif_attribute)) {
                if (ldif_fields[i].flags & FLAG_WORK_ADDRESS) {
                    populate_contact_address (work_address, ptr, ldif_value->str);
                }
                else if (ldif_fields[i].flags & FLAG_HOME_ADDRESS) {
                    populate_contact_address (home_address, ptr, ldif_value->str);
                }
                else if (ldif_fields[i].flags & FLAG_LIST) {
                    GList *list;

                    list = e_contact_get (contact, ldif_fields[i].contact_field);
                    list = g_list_append (list, g_strdup (ldif_value->str));
                    e_contact_set (contact, ldif_fields[i].contact_field, list);

                    g_list_foreach (list, (GFunc) g_free, NULL);
                    g_list_free (list);
                }
                else if (ldif_fields[i].flags & FLAG_BOOLEAN) {
                    if (!g_ascii_strcasecmp (ldif_value->str, "true")) {
                        e_contact_set (contact, ldif_fields[i].contact_field, GINT_TO_POINTER (TRUE));
                    }
                    else {
                        e_contact_set (contact, ldif_fields[i].contact_field, GINT_TO_POINTER (FALSE));
                    }
                    g_message ("set %s to %s", ptr, ldif_value->str);
                }
                else {
                    /* FIXME is everything a string? */
                    e_contact_set (contact, ldif_fields[i].contact_field, ldif_value->str);
                    g_message ("set %s to %s", ptr, ldif_value->str);
                }
                field_handled = TRUE;
                break;
            }
        }

        /* handle objectclass/dn/member out here */
        if (!field_handled) {
            if (!g_ascii_strcasecmp (ptr, "dn"))
                g_hash_table_insert (gci->dn_contact_hash, g_strdup(ldif_value->str), contact);
            else if (!g_ascii_strcasecmp (ptr, "objectclass") && !g_ascii_strcasecmp (ldif_value->str, "groupofnames")) {
                e_contact_set (contact, E_CONTACT_IS_LIST, GINT_TO_POINTER (TRUE));
            }
            else if (!g_ascii_strcasecmp (ptr, "member")) {
                GList *email;

                email = e_contact_get (contact, E_CONTACT_EMAIL);
                email = g_list_append (email, g_strdup (ldif_value->str));
                e_contact_set (contact, E_CONTACT_EMAIL, email);

                g_list_foreach (email, (GFunc) g_free, NULL);
                g_list_free (email);
            }
        }

        /* put the colon back the way it was, just for kicks */
        *colon = ':';

        g_string_free (ldif_value, TRUE);
    }
    else {
        g_warning ("unrecognized entry %s", ptr);
        return FALSE;
    }

    *buf = value;

    return TRUE;
}

static EContact *
getNextLDIFEntry(LDIFImporter *gci, FILE *f )
{
    EContact *contact;
    EContactAddress *work_address, *home_address;
    GString *str;
    char line[1024];
    char *buf;

    str = g_string_new ("");
    /* read from the file until we get to a blank line (or eof) */
    while (!feof (f)) {
        if (!fgets (line, sizeof(line), f))
            break;
        if (line[0] == '\n' || (line[0] == '\r' && line[1] == '\n'))
            break;
        str = g_string_append (str, line);
    }

    if (strlen (str->str) == 0) {
        g_string_free (str, TRUE);
        return NULL;
    }

    /* now parse that entry */
    contact = e_contact_new ();
    work_address = g_new0 (EContactAddress, 1);
    home_address = g_new0 (EContactAddress, 1);

    buf = str->str;
    while (buf) {
        if (!parseLine (gci, contact, work_address, home_address, &buf)) {
            /* parsing error */
            g_string_free (str, TRUE);
            e_contact_address_free (work_address);
            e_contact_address_free (home_address);
            g_object_unref (contact);
            return NULL;
        }
    }

    /* fill in the address */
    if (work_address->locality || work_address->country || work_address->ext ||
        work_address->code || work_address->region || work_address->street) {
        e_contact_set (contact, E_CONTACT_ADDRESS_WORK, work_address);
    }
    if (home_address->locality || home_address->country || home_address->ext || 
        home_address->code || home_address->region || home_address->street) {
        e_contact_set (contact, E_CONTACT_ADDRESS_HOME, home_address);
    }
    e_contact_address_free (work_address);
    e_contact_address_free (home_address);

    g_string_free (str, TRUE);

    return contact;
}

static void
resolve_list_card (LDIFImporter *gci, EContact *contact)
{
    GList *email, *l;
    GList *email_attrs = NULL;
    char *full_name;

    /* set file_as to full_name so we don't later try and figure
           out a first/last name for the list. */
    full_name = e_contact_get (contact, E_CONTACT_FULL_NAME);
    if (full_name)
        e_contact_set (contact, E_CONTACT_FILE_AS, full_name);
    g_free (full_name);

    /* FIMXE getting might not be implemented in ebook */
    email = e_contact_get (contact, E_CONTACT_EMAIL);
    for (l = email; l; l = l->next) {
        /* mozilla stuffs dn's in the EMAIL list for contact lists */
        char *dn = l->data;
        EContact *dn_contact = g_hash_table_lookup (gci->dn_contact_hash, dn);

        /* break list chains here, since we don't support them just yet */
        if (dn_contact && !e_contact_get (dn_contact, E_CONTACT_IS_LIST)) {
            EDestination *dest;
            EVCardAttribute *attr = e_vcard_attribute_new (NULL, EVC_EMAIL);

            /* Hard-wired for default e-mail, since netscape only exports 1 email address */
            dest = e_destination_new ();
            e_destination_set_contact (dest, dn_contact, 0);

            e_destination_export_to_vcard_attribute (dest, attr);

            g_object_unref (dest);

            email_attrs = g_list_append (email_attrs, attr);
        }
    }       
    e_contact_set_attributes (contact, E_CONTACT_EMAIL, email_attrs);

    g_list_foreach (email, (GFunc) g_free, NULL);
    g_list_free (email);
    g_list_foreach (email_attrs, (GFunc) e_vcard_attribute_free, NULL);
    g_list_free (email_attrs);
}

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);
}

static gboolean
ldif_import_contacts(void *d)
{
    LDIFImporter *gci = d;
    EContact *contact;
    GSList *iter;
    int count = 0;

    /* We process all normal cards immediately and keep the list
       ones till the end */

    if (gci->state == 0) {
        while (count < 50 && (contact = getNextLDIFEntry(gci, gci->file))) {
            if (e_contact_get (contact, E_CONTACT_IS_LIST)) {
                gci->list_contacts = g_slist_prepend(gci->list_contacts, contact);
            } else {
                add_to_notes(contact, E_CONTACT_OFFICE);
                add_to_notes(contact, E_CONTACT_SPOUSE);
                add_to_notes(contact, E_CONTACT_BLOG_URL);
                e_book_add_contact(gci->book, contact, NULL);
                gci->contacts = g_slist_prepend(gci->contacts, contact);
            }
            count++;
        }
        if (contact == NULL) {
            gci->state = 1;
            gci->list_iterator = gci->list_contacts;
        }
    }
    if (gci->state == 1) {
        for (iter = gci->list_iterator;count < 50 && iter;iter=iter->next) {
            contact = iter->data;
            resolve_list_card(gci, contact);
            e_book_add_contact(gci->book, contact, NULL);
            count++;
        }
        gci->list_iterator = iter;
        if (iter == NULL)
            gci->state = 2;
    }
    if (gci->state == 2) {
        ldif_import_done(gci);
        return FALSE;
    } else {
        e_import_status(gci->import, gci->target, _("Importing ..."), ftell(gci->file) * 100 / gci->size);
        return TRUE;
    }
}

static void
primary_selection_changed_cb (ESourceSelector *selector, EImportTarget *target)
{
    g_datalist_set_data_full(&target->data, "ldif-source",
                 g_object_ref(e_source_selector_peek_primary_selection(selector)),
                 g_object_unref);
}

static GtkWidget *
ldif_getwidget(EImport *ei, EImportTarget *target, EImportImporter *im)
{
    GtkWidget *vbox, *selector;
    ESource *primary;
    ESourceList *source_list;   

    /* FIXME Better error handling */
    if (!e_book_get_addressbooks (&source_list, NULL))
        return NULL;

    vbox = gtk_vbox_new (FALSE, FALSE);
    
    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);

    primary = g_datalist_get_data(&target->data, "ldif-source");
    if (primary == NULL) {
        primary = e_source_list_peek_source_any (source_list);
        g_object_ref(primary);
        g_datalist_set_data_full(&target->data, "ldif-source", primary, g_object_unref);
    }
    e_source_selector_set_primary_selection (E_SOURCE_SELECTOR (selector), primary);
    g_object_unref (source_list);

    g_signal_connect (selector, "primary_selection_changed", G_CALLBACK (primary_selection_changed_cb), target);

    gtk_widget_show_all (vbox);

    return vbox;
}

static char *supported_extensions[2] = {
    ".ldif", NULL
};

static gboolean
ldif_supported(EImport *ei, EImportTarget *target, EImportImporter *im)
{
    char *ext;
    int i;
    EImportTargetURI *s;

    if (target->type != E_IMPORT_TARGET_URI)
        return FALSE;

    s = (EImportTargetURI *)target;
    if (s->uri_src == NULL)
        return TRUE;

    if (strncmp(s->uri_src, "file:///", 8) != 0)
        return FALSE;

    ext = strrchr(s->uri_src, '.');
    if (ext == NULL)
        return FALSE;

    for (i = 0; supported_extensions[i] != NULL; i++) {
        if (g_ascii_strcasecmp(supported_extensions[i], ext) == 0)
            return TRUE;
    }

    return FALSE;
}

static void
free_dn_hash(void *k, void *v, void *d)
{
    g_free(k);
}

static void
ldif_import_done(LDIFImporter *gci)
{
    if (gci->idle_id)
        g_source_remove(gci->idle_id);

    fclose (gci->file);
    g_object_unref(gci->book);
    g_slist_foreach(gci->contacts, (GFunc) g_object_unref, NULL);
    g_slist_foreach(gci->list_contacts, (GFunc) g_object_unref, NULL);
    g_slist_free(gci->contacts);
    g_slist_free(gci->list_contacts);
    g_hash_table_foreach(gci->dn_contact_hash, free_dn_hash, NULL);
    g_hash_table_destroy(gci->dn_contact_hash);

    e_import_complete(gci->import, gci->target);
    g_object_unref(gci->import);

    g_free (gci);
}

static void
ldif_import(EImport *ei, EImportTarget *target, EImportImporter *im)
{
    LDIFImporter *gci;
    EBook *book;
    FILE *file = NULL;
    EImportTargetURI *s = (EImportTargetURI *)target;
    gchar *filename;

    book = e_book_new(g_datalist_get_data(&target->data, "ldif-source"), NULL);
    if (book == NULL) {
        g_message(G_STRLOC ":Couldn't create EBook.");
        e_import_complete(ei, target);
        return;
    }

    filename = g_filename_from_uri(s->uri_src, NULL, NULL);
    if (filename != NULL) {
        file = g_fopen(filename, "r");
        g_free (filename);
    }
    if (file == NULL) {
        g_message(G_STRLOC ":Can't open .ldif file");
        e_import_complete(ei, target);
        g_object_unref(book);
        return;
    }

    gci = g_malloc0(sizeof(*gci));
    g_datalist_set_data(&target->data, "ldif-data", gci);
    gci->import = g_object_ref(ei);
    gci->target = target;
    gci->book = book;
    gci->file = file;
    fseek(file, 0, SEEK_END);
    gci->size = ftell(file);
    fseek(file, 0, SEEK_SET);
    gci->dn_contact_hash = g_hash_table_new(g_str_hash, g_str_equal);

    e_book_open(gci->book, FALSE, NULL);

    gci->idle_id = g_idle_add(ldif_import_contacts, gci);
}

static void
ldif_cancel(EImport *ei, EImportTarget *target, EImportImporter *im)
{
    LDIFImporter *gci = g_datalist_get_data(&target->data, "ldif-data");

    if (gci)
        gci->state = 2;
}

static EImportImporter ldif_importer = {
    E_IMPORT_TARGET_URI,
    0,
    ldif_supported,
    ldif_getwidget,
    ldif_import,
    ldif_cancel,
};

EImportImporter *
evolution_ldif_importer_peek(void)
{
    ldif_importer.name = _("LDAP Data Interchange Format (.ldif)");
    ldif_importer.description = _("Evolution LDIF importer");

    return &ldif_importer;
}