aboutsummaryrefslogtreecommitdiffstats
path: root/addressbook/importers/evolution-ldif-importer.c
blob: c6f70fbca28f090d21f6490c714ce3583df9fdc9 (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
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
/*
 * 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/>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 */

/*
 * 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 <gtk/gtk.h>
#include <glib/gi18n.h>
#include <glib/gstdio.h>

#include <libebook/e-book-client.h>
#include <libedataserverui/e-client-utils.h>
#include <libedataserverui/e-source-selector.h>
#include <libedataserver/e-source-address-book.h>

#include <libebook/e-destination.h>

#include <shell/e-shell.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;

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

    EBookClient *book_client;

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

static void ldif_import_done (LDIFImporter *gci);

static struct {
    const gchar *ldif_attribute;
    EContactField contact_field;
#define FLAG_HOME_ADDRESS   0x01
#define FLAG_WORK_ADDRESS   0x02
#define FLAG_LIST       0x04
#define FLAG_BOOLEAN        0x08
    gint 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 GString *
getValue (gchar **src)
{
    GString *dest = g_string_new("");
    gchar *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) {
        guchar *data;
        gsize length;

        /* XXX g_string_assign_len() would be nice here */
        data = g_base64_decode (dest->str + 2, &length);
        g_string_truncate (dest, 0);
        g_string_append_len (dest, (gchar *) data, length);
        g_free (data);
    }

    *src = s;

    return dest;
}

static void
populate_contact_address (EContactAddress *address,
                          gchar *attr,
                          gchar *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) {
            gchar *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")) {
        gchar *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) {
            gchar *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 (GHashTable *dn_contact_hash,
           EContact *contact,
           EContactAddress *work_address,
           EContactAddress *home_address,
           gchar **buf)
{
    gchar *ptr;
    gchar *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 = (gchar *) strchr ( ptr, ':' );
    if (colon) {
        gint i;

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

        ldif_value = getValue (&value );

        field_handled = FALSE;
        for (i = 0; i < G_N_ELEMENTS (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 (
                    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 (GHashTable *dn_contact_hash,
                  FILE *f)
{
    EContact *contact;
    EContactAddress *work_address, *home_address;
    GString *str;
    gchar line[1024];
    gchar *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 (dn_contact_hash, 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;
    gchar *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 */
        gchar *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 (gpointer d)
{
    LDIFImporter *gci = d;
    EContact *contact;
    GSList *iter;
    gint 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->dn_contact_hash, gci->file))) {
            if (e_contact_get (contact, E_CONTACT_IS_LIST)) {
                gci->list_contacts = g_slist_prepend (
                    gci->list_contacts, contact);
            } else {
                gchar *uid = NULL;

                add_to_notes (contact, E_CONTACT_OFFICE);
                add_to_notes (contact, E_CONTACT_SPOUSE);
                add_to_notes (contact, E_CONTACT_BLOG_URL);
                if (e_book_client_add_contact_sync (gci->book_client, contact, &uid, NULL, NULL) && uid) {
                    e_contact_set (contact, E_CONTACT_UID, uid);
                    g_free (uid);
                }
                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) {
            gchar *uid = NULL;

            contact = iter->data;
            resolve_list_card (gci, contact);
            if (e_book_client_add_contact_sync (gci->book_client, contact, &uid, NULL, NULL) && uid) {
                e_contact_set (contact, E_CONTACT_UID, uid);
                g_free (uid);
            }
            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)
{
    ESource *source;

    source = e_source_selector_ref_primary_selection (selector);
    g_return_if_fail (source != NULL);

    g_datalist_set_data_full (
        &target->data, "ldif-source",
        source, (GDestroyNotify) g_object_unref);
}

static GtkWidget *
ldif_getwidget (EImport *ei,
                EImportTarget *target,
                EImportImporter *im)
{
    EShell *shell;
    GtkWidget *vbox, *selector;
    ESourceRegistry *registry;
    ESource *primary;
    const gchar *extension_name;

    vbox = gtk_vbox_new (FALSE, FALSE);

    shell = e_shell_get_default ();
    registry = e_shell_get_registry (shell);
    extension_name = E_SOURCE_EXTENSION_ADDRESS_BOOK;
    selector = e_source_selector_new (registry, extension_name);
    e_source_selector_set_show_toggles (
        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) {
        GList *list;

        list = e_source_registry_list_sources (registry, extension_name);
        if (list != NULL) {
            primary = g_object_ref (list->data);
            g_datalist_set_data_full (
                &target->data, "ldif-source", primary,
                (GDestroyNotify) g_object_unref);
        }

        g_list_free_full (list, (GDestroyNotify) g_object_unref);
    }
    e_source_selector_set_primary_selection (
        E_SOURCE_SELECTOR (selector), primary);

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

    gtk_widget_show_all (vbox);

    return vbox;
}

static const gchar *supported_extensions[3] = {
    ".ldif", ".ldi", NULL
};

static gboolean
ldif_supported (EImport *ei,
                EImportTarget *target,
                EImportImporter *im)
{
    gchar *ext;
    gint 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
ldif_import_done (LDIFImporter *gci)
{
    if (gci->idle_id)
        g_source_remove (gci->idle_id);

    fclose (gci->file);
    g_object_unref (gci->book_client);
    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_destroy (gci->dn_contact_hash);

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

    g_free (gci);
}

static void
book_loaded_cb (GObject *source_object,
                GAsyncResult *result,
                gpointer user_data)
{
    ESource *source = E_SOURCE (source_object);
    LDIFImporter *gci = user_data;
    EClient *client = NULL;

    e_client_utils_open_new_finish (source, result, &client, NULL);

    if (client == NULL) {
        ldif_import_done (gci);
        return;
    }

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

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

    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);
        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->file = file;
    fseek (file, 0, SEEK_END);
    gci->size = ftell (file);
    fseek (file, 0, SEEK_SET);
    gci->dn_contact_hash = g_hash_table_new_full (
        g_str_hash, g_str_equal,
        (GDestroyNotify) g_free,
        (GDestroyNotify) NULL);

    source = g_datalist_get_data (&target->data, "ldif-source");

    e_client_utils_open_new (
        source, E_CLIENT_SOURCE_TYPE_CONTACTS, FALSE, NULL,
        book_loaded_cb, 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 GtkWidget *
ldif_get_preview (EImport *ei,
                  EImportTarget *target,
                  EImportImporter *im)
{
    GtkWidget *preview;
    GSList *contacts = NULL;
    EContact *contact;
    EImportTargetURI *s = (EImportTargetURI *) target;
    gchar *filename;
    GHashTable *dn_contact_hash;
    FILE *file;

    filename = g_filename_from_uri (s->uri_src, NULL, NULL);
    if (filename == NULL) {
        g_message (G_STRLOC ": Couldn't get filename from URI '%s'", s->uri_src);
        return NULL;
    }

    file = g_fopen(filename, "r");
    g_free (filename);

    if (file == NULL) {
        g_message (G_STRLOC ": Can't open .ldif file");
        return NULL;
    }

    dn_contact_hash = g_hash_table_new_full (
        g_str_hash, g_str_equal,
        (GDestroyNotify) g_free,
        (GDestroyNotify) NULL);

    while (contact = getNextLDIFEntry (dn_contact_hash, file), contact != NULL) {
        if (!e_contact_get (contact, E_CONTACT_IS_LIST)) {
            add_to_notes (contact, E_CONTACT_OFFICE);
            add_to_notes (contact, E_CONTACT_SPOUSE);
            add_to_notes (contact, E_CONTACT_BLOG_URL);
        }

        contacts = g_slist_prepend (contacts, contact);
    }

    g_hash_table_destroy (dn_contact_hash);

    contacts = g_slist_reverse (contacts);
    preview = evolution_contact_importer_get_preview_widget (contacts);

    e_client_util_free_object_slist (contacts);
    fclose (file);

    return preview;
}

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

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

    return &ldif_importer;
}