1 /*
2  * This program is free software; you can redistribute it and/or modify it
3  * under the terms of the GNU Lesser General Public License as published by
4  * the Free Software Foundation.
5  *
6  * This program is distributed in the hope that it will be useful, but
7  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
8  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9  * for more details.
10  *
11  * You should have received a copy of the GNU Lesser General Public License
12  * along with this program; if not, see <http://www.gnu.org/licenses/>.
13  *
14  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
15  */
16 
17 /*
18  * LDIF importer.  LDIF is the file format of an exported Netscape
19  * addressbook.
20  *
21  * Framework copied from evolution-gnomecard-importer.c
22  *
23  * Michael M. Morrison (mmorrison@kqcorp.com)
24  *
25  * Multi-line value support, mailing list support, base64 support, and
26  * various fixups: Chris Toshok (toshok@ximian.com)
27  *
28  * Made re-entrant, converted to eplugin, Michael Zucchi <notzed@ximian.com>
29  */
30 
31 #include "evolution-config.h"
32 
33 #include <errno.h>
34 #include <stdio.h>
35 #include <ctype.h>
36 #include <string.h>
37 
38 #include <gtk/gtk.h>
39 #include <glib/gi18n.h>
40 #include <glib/gstdio.h>
41 
42 #include <libebook/libebook.h>
43 
44 #include <shell/e-shell.h>
45 
46 #include "evolution-addressbook-importers.h"
47 
48 typedef struct {
49 	EImport *import;
50 	EImportTarget *target;
51 
52 	guint idle_id;
53 
54 	GHashTable *dn_contact_hash;
55 
56 	gint state;		/* 0 - initial scan, 1 - list cards, 2 - cancelled/complete */
57 	FILE *file;
58 	gulong size;
59 
60 	EBookClient *book_client;
61 
62 	GSList *contacts;
63 	GSList *list_contacts;
64 	GSList *list_iterator;
65 } LDIFImporter;
66 
67 static void ldif_import_done (LDIFImporter *gci);
68 
69 static struct {
70 	const gchar *ldif_attribute;
71 	EContactField contact_field;
72 #define FLAG_HOME_ADDRESS	0x01
73 #define FLAG_WORK_ADDRESS	0x02
74 #define FLAG_LIST		0x04
75 #define FLAG_BOOLEAN		0x08
76 	gint flags;
77 }
78 ldif_fields[] = {
79 	{ "cn", E_CONTACT_FULL_NAME },
80 	{ "mail", E_CONTACT_EMAIL, FLAG_LIST },
81 	{ "mozillaSecondEmail", E_CONTACT_EMAIL_2},
82 #if 0
83 	{ "givenname", E_CONTACT_GIVEN_NAME },
84 #endif
85 	{ "sn", E_CONTACT_FAMILY_NAME },
86 	{ "xmozillanickname", E_CONTACT_NICKNAME },
87 
88 	{ "o", E_CONTACT_ORG },
89 	{ "ou", E_CONTACT_ORG_UNIT },
90 	{ "title", E_CONTACT_TITLE },
91 
92 	{ "locality", 0, FLAG_WORK_ADDRESS },
93 	{ "l", 0, FLAG_WORK_ADDRESS },
94 	{ "mozillahomelocalityname", 0, FLAG_HOME_ADDRESS },
95 	{ "st", 0, FLAG_WORK_ADDRESS },
96 	{ "mozillaHomeState", 0, FLAG_HOME_ADDRESS },
97 	{ "streetaddress", 0, FLAG_WORK_ADDRESS },
98 	{ "postalcode", 0, FLAG_WORK_ADDRESS },
99 	{ "mozillaHomePostalCode", 0, FLAG_HOME_ADDRESS },
100 	{ "countryname", 0, FLAG_WORK_ADDRESS },
101 	{ "c", 0, FLAG_WORK_ADDRESS },
102 	{ "mozillaHomeCountryName", 0, FLAG_HOME_ADDRESS },
103 	{ "postalAddress", 0, FLAG_WORK_ADDRESS },
104 	{ "homePostalAddress", 0, FLAG_HOME_ADDRESS },
105 	{ "mozillaPostalAddress2", 0, FLAG_WORK_ADDRESS },
106 	{ "mozillaHomePostalAddress2", 0, FLAG_HOME_ADDRESS },
107 
108 	{ "telephonenumber", E_CONTACT_PHONE_BUSINESS},
109 	{ "homephone", E_CONTACT_PHONE_HOME },
110 	{ "facsimiletelephonenumber", E_CONTACT_PHONE_BUSINESS_FAX },
111 	{ "pagerphone", E_CONTACT_PHONE_PAGER },
112 	{ "cellphone", E_CONTACT_PHONE_MOBILE },
113 	{ "mobile", E_CONTACT_PHONE_MOBILE },
114 
115 	{ "homeurl", E_CONTACT_HOMEPAGE_URL },
116 	{ "mozillaHomeUrl", E_CONTACT_HOMEPAGE_URL },
117 
118 	{ "description", E_CONTACT_NOTE },
119 
120 	{ "xmozillausehtmlmail", E_CONTACT_WANTS_HTML, FLAG_BOOLEAN },
121 
122 	{ "nsAIMid", E_CONTACT_IM_AIM, FLAG_LIST },
123 	{ "mozilla_AimScreenName", E_CONTACT_IM_AIM, FLAG_LIST }
124 };
125 
126 static GString *
getValue(gchar ** src)127 getValue (gchar **src)
128 {
129 	GString *dest = g_string_new ("");
130 	gchar *s = *src;
131 	gboolean need_base64 = (*s == ':');
132 
133  copy_line:
134 	while (*s != 0 && *s != '\n' && *s != '\r')
135 		g_string_append_c (dest, *s++);
136 
137 	if (*s == '\r') s++;
138 	if (*s == '\n')	s++;
139 
140 	/* check for continuation here */
141 	if (*s == ' ') {
142 		s++;
143 		goto copy_line;
144 	}
145 
146 	if (need_base64) {
147 		guchar *data;
148 		gsize length;
149 
150 		/* XXX g_string_assign_len() would be nice here */
151 		data = g_base64_decode (dest->str + 2, &length);
152 		g_string_truncate (dest, 0);
153 		g_string_append_len (dest, (gchar *) data, length);
154 		g_free (data);
155 	}
156 
157 	*src = s;
158 
159 	return dest;
160 }
161 
162 static void
populate_contact_address(EContactAddress * address,gchar * attr,gchar * value)163 populate_contact_address (EContactAddress *address,
164                           gchar *attr,
165                           gchar *value)
166 {
167 	if (!g_ascii_strcasecmp (attr, "locality") ||
168 	    !g_ascii_strcasecmp (attr, "l") ||
169 	    !g_ascii_strcasecmp (attr, "mozillaHomeLocalityName"))
170 		address->locality = g_strdup (value);
171 	else if (!g_ascii_strcasecmp (attr, "countryname") ||
172 		 !g_ascii_strcasecmp (attr, "c") ||
173 		 !g_ascii_strcasecmp (attr, "mozillaHomeCountryName"))
174 		address->country = g_strdup (value);
175 	else if (!g_ascii_strcasecmp (attr, "postalcode") ||
176 		 !g_ascii_strcasecmp (attr, "mozillaHomePostalCode"))
177 		address->code = g_strdup (value);
178 	else if (!g_ascii_strcasecmp (attr, "st") ||
179 		 !g_ascii_strcasecmp (attr, "mozillaHomeState"))
180 		address->region = g_strdup (value);
181 	else if (!g_ascii_strcasecmp (attr, "streetaddress"))
182 		address->street = g_strdup (value);
183 	else if (!g_ascii_strcasecmp (attr, "mozillaPostalAddress2") ||
184 		 !g_ascii_strcasecmp (attr, "mozillaHomePostalAddress2")) {
185 		if (address->ext && *address->ext) {
186 			gchar *temp = g_strdup (address->ext);
187 			g_free (address->ext);
188 			address->ext = g_strconcat (temp, ",\n", value, NULL);
189 			g_free (temp);
190 		}
191 		else {
192 			address->ext = g_strdup (value);
193 		}
194 	}
195 	else if (!g_ascii_strcasecmp (attr, "postalAddress") ||
196 		 !g_ascii_strcasecmp (attr, "homepostalAddress")) {
197 		gchar *c, *i, *addr_field;
198 
199 		addr_field = g_strdup (value);
200 		i = addr_field;
201 		for (c = addr_field; *c != '\0'; c++) {
202 			i++;
203 			if (*c == ',' &&  *i != '\0' && *i == ' ') {
204 				*i = '\n';
205 			}
206 		}
207 		if (address->ext && *address->ext) {
208 			gchar *temp = g_strdup (address->ext);
209 			g_free (address->ext);
210 			address->ext = g_strconcat (addr_field, ",\n", temp, NULL);
211 			g_free (temp);
212 			g_free (addr_field);
213 		}
214 		else {
215 			address->ext = addr_field;
216 		}
217 	}
218 }
219 
220 static gboolean
parseLine(GHashTable * dn_contact_hash,EContact * contact,EContactAddress * work_address,EContactAddress * home_address,gchar ** buf)221 parseLine (GHashTable *dn_contact_hash,
222            EContact *contact,
223            EContactAddress *work_address,
224            EContactAddress *home_address,
225            gchar **buf)
226 {
227 	gchar *ptr;
228 	gchar *colon, *value;
229 	gboolean field_handled;
230 	GString *ldif_value;
231 
232 	ptr = *buf;
233 
234 	/* if the string is empty, return */
235 	if (*ptr == '\0') {
236 		*buf = NULL;
237 		return TRUE;
238 	}
239 
240 	/* skip comment lines */
241 	if (*ptr == '#') {
242 		ptr = strchr (ptr, '\n');
243 		if (!ptr)
244 			*buf = NULL;
245 		else
246 			*buf = ptr + 1;
247 		return TRUE;
248 	}
249 
250 	/* first, check for a 'continuation' line */
251 	if (ptr[0] == ' ' && ptr[1] != '\n') {
252 		g_warning ("unexpected continuation line");
253 		return FALSE;
254 	}
255 
256 	colon = (gchar *) strchr (ptr, ':');
257 	if (colon) {
258 		gint i;
259 
260 		*colon = 0;
261 		value = colon + 1;
262 		while (isspace (*value))
263 			value++;
264 
265 		ldif_value = getValue (&value);
266 
267 		field_handled = FALSE;
268 		for (i = 0; i < G_N_ELEMENTS (ldif_fields); i++) {
269 			if (!g_ascii_strcasecmp (ptr, ldif_fields[i].ldif_attribute)) {
270 				if (ldif_fields[i].flags & FLAG_WORK_ADDRESS) {
271 					populate_contact_address (work_address, ptr, ldif_value->str);
272 				}
273 				else if (ldif_fields[i].flags & FLAG_HOME_ADDRESS) {
274 					populate_contact_address (home_address, ptr, ldif_value->str);
275 				}
276 				else if (ldif_fields[i].flags & FLAG_LIST) {
277 					GList *list;
278 
279 					list = e_contact_get (contact, ldif_fields[i].contact_field);
280 					list = g_list_append (list, g_strdup (ldif_value->str));
281 					e_contact_set (contact, ldif_fields[i].contact_field, list);
282 
283 					g_list_foreach (list, (GFunc) g_free, NULL);
284 					g_list_free (list);
285 				}
286 				else if (ldif_fields[i].flags & FLAG_BOOLEAN) {
287 					if (!g_ascii_strcasecmp (ldif_value->str, "true")) {
288 						e_contact_set (
289 							contact,
290 							ldif_fields[i].contact_field,
291 							GINT_TO_POINTER (TRUE));
292 					}
293 					else {
294 						e_contact_set (
295 							contact,
296 							ldif_fields[i].contact_field,
297 							GINT_TO_POINTER (FALSE));
298 					}
299 					g_message ("set %s to %s", ptr, ldif_value->str);
300 				}
301 				else {
302 					/* FIXME is everything a string? */
303 					e_contact_set (
304 						contact,
305 						ldif_fields[i].contact_field,
306 						ldif_value->str);
307 					g_message ("set %s to %s", ptr, ldif_value->str);
308 				}
309 				field_handled = TRUE;
310 				break;
311 			}
312 		}
313 
314 		/* handle objectclass/dn/member out here */
315 		if (!field_handled) {
316 			if (!g_ascii_strcasecmp (ptr, "dn"))
317 				g_hash_table_insert (
318 					dn_contact_hash,
319 					g_strdup (ldif_value->str), contact);
320 			else if (!g_ascii_strcasecmp (ptr, "objectclass") &&
321 				!g_ascii_strcasecmp (ldif_value->str, "groupofnames")) {
322 				e_contact_set (
323 					contact, E_CONTACT_IS_LIST,
324 					GINT_TO_POINTER (TRUE));
325 			} else if (!g_ascii_strcasecmp (ptr, "member")) {
326 				GList *email;
327 
328 				email = e_contact_get (contact, E_CONTACT_EMAIL);
329 				email = g_list_append (email, g_strdup (ldif_value->str));
330 				e_contact_set (contact, E_CONTACT_EMAIL, email);
331 
332 				g_list_foreach (email, (GFunc) g_free, NULL);
333 				g_list_free (email);
334 			}
335 		}
336 
337 		/* put the colon back the way it was, just for kicks */
338 		*colon = ':';
339 
340 		g_string_free (ldif_value, TRUE);
341 
342 	} else {
343 		g_warning ("unrecognized entry %s", ptr);
344 		return FALSE;
345 	}
346 
347 	*buf = value;
348 
349 	return TRUE;
350 }
351 
352 static EContact *
getNextLDIFEntry(GHashTable * dn_contact_hash,FILE * f)353 getNextLDIFEntry (GHashTable *dn_contact_hash,
354                   FILE *f)
355 {
356 	EContact *contact;
357 	EContactAddress *work_address, *home_address;
358 	GString *str;
359 	gchar line[1024];
360 	gchar *buf;
361 
362 	str = g_string_new ("");
363 	/* read from the file until we get to a blank line (or eof) */
364 	while (!feof (f)) {
365 		if (!fgets (line, sizeof (line), f))
366 			break;
367 		if (line[0] == '\n' || (line[0] == '\r' && line[1] == '\n'))
368 			break;
369 		g_string_append (str, line);
370 	}
371 
372 	if (strlen (str->str) == 0) {
373 		g_string_free (str, TRUE);
374 		return NULL;
375 	}
376 
377 	/* now parse that entry */
378 	contact = e_contact_new ();
379 	work_address = g_new0 (EContactAddress, 1);
380 	home_address = g_new0 (EContactAddress, 1);
381 
382 	buf = str->str;
383 	while (buf) {
384 		if (!parseLine (dn_contact_hash, contact, work_address, home_address, &buf)) {
385 			/* parsing error */
386 			g_string_free (str, TRUE);
387 			e_contact_address_free (work_address);
388 			e_contact_address_free (home_address);
389 			g_object_unref (contact);
390 			return NULL;
391 		}
392 	}
393 
394 	/* fill in the address */
395 	if (work_address->locality || work_address->country || work_address->ext ||
396 	    work_address->code || work_address->region || work_address->street) {
397 		e_contact_set (contact, E_CONTACT_ADDRESS_WORK, work_address);
398 	}
399 	if (home_address->locality || home_address->country || home_address->ext ||
400 	    home_address->code || home_address->region || home_address->street) {
401 		e_contact_set (contact, E_CONTACT_ADDRESS_HOME, home_address);
402 	}
403 	e_contact_address_free (work_address);
404 	e_contact_address_free (home_address);
405 
406 	g_string_free (str, TRUE);
407 
408 	return contact;
409 }
410 
411 static void
resolve_list_card(LDIFImporter * gci,EContact * contact)412 resolve_list_card (LDIFImporter *gci,
413                    EContact *contact)
414 {
415 	GList *email, *l;
416 	GList *email_attrs = NULL;
417 	gchar *full_name;
418 
419 	/* set file_as to full_name so we don't later try and figure
420 	 * out a first/last name for the list. */
421 	full_name = e_contact_get (contact, E_CONTACT_FULL_NAME);
422 	if (full_name)
423 		e_contact_set (contact, E_CONTACT_FILE_AS, full_name);
424 	g_free (full_name);
425 
426 	/* FIMXE getting might not be implemented in ebook */
427 	email = e_contact_get (contact, E_CONTACT_EMAIL);
428 	for (l = email; l; l = l->next) {
429 		/* mozilla stuffs dn's in the EMAIL list for contact lists */
430 		gchar *dn = l->data;
431 		EContact *dn_contact = g_hash_table_lookup (gci->dn_contact_hash, dn);
432 
433 		/* break list chains here, since we don't support them just yet */
434 		if (dn_contact && !e_contact_get (dn_contact, E_CONTACT_IS_LIST)) {
435 			EDestination *dest;
436 			EVCardAttribute *attr = e_vcard_attribute_new (NULL, EVC_EMAIL);
437 
438 			/* Hard-wired for default e-mail, since
439 			 * netscape only exports 1 email address. */
440 			dest = e_destination_new ();
441 			e_destination_set_contact (dest, dn_contact, 0);
442 
443 			e_destination_export_to_vcard_attribute (dest, attr);
444 
445 			g_object_unref (dest);
446 
447 			email_attrs = g_list_append (email_attrs, attr);
448 		}
449 	}
450 	e_contact_set_attributes (contact, E_CONTACT_EMAIL, email_attrs);
451 
452 	g_list_foreach (email, (GFunc) g_free, NULL);
453 	g_list_free (email);
454 	g_list_foreach (email_attrs, (GFunc) e_vcard_attribute_free, NULL);
455 	g_list_free (email_attrs);
456 }
457 
458 static void
add_to_notes(EContact * contact,EContactField field)459 add_to_notes (EContact *contact,
460               EContactField field)
461 {
462 	const gchar *old_text;
463 	const gchar *field_text;
464 	gchar       *new_text;
465 
466 	old_text = e_contact_get_const (contact, E_CONTACT_NOTE);
467 	if (old_text && strstr (old_text, e_contact_pretty_name (field)))
468 		return;
469 
470 	field_text = e_contact_get_const (contact, field);
471 	if (!field_text || !*field_text)
472 		return;
473 
474 	new_text = g_strdup_printf (
475 		"%s%s%s: %s",
476 		old_text ? old_text : "",
477 		old_text && *old_text &&
478 		*(old_text + strlen (old_text) - 1) != '\n' ? "\n" : "",
479 		e_contact_pretty_name (field), field_text);
480 	e_contact_set (contact, E_CONTACT_NOTE, new_text);
481 	g_free (new_text);
482 }
483 
484 static gboolean
ldif_import_contacts(gpointer d)485 ldif_import_contacts (gpointer d)
486 {
487 	LDIFImporter *gci = d;
488 	EContact *contact;
489 	GSList *iter;
490 	gint count = 0;
491 
492 	/* We process all normal cards immediately and keep the list
493 	 * ones till the end */
494 
495 	if (gci->state == 0) {
496 		while (count < 50 && (contact = getNextLDIFEntry (
497 			gci->dn_contact_hash, gci->file))) {
498 			if (e_contact_get (contact, E_CONTACT_IS_LIST)) {
499 				gci->list_contacts = g_slist_prepend (
500 					gci->list_contacts, contact);
501 			} else {
502 				gchar *uid = NULL;
503 
504 				add_to_notes (contact, E_CONTACT_OFFICE);
505 				add_to_notes (contact, E_CONTACT_SPOUSE);
506 				add_to_notes (contact, E_CONTACT_BLOG_URL);
507 
508 				e_book_client_add_contact_sync (
509 					gci->book_client,
510 					contact, E_BOOK_OPERATION_FLAG_NONE, &uid, NULL, NULL);
511 				if (uid != NULL) {
512 					e_contact_set (contact, E_CONTACT_UID, uid);
513 					g_free (uid);
514 				}
515 				gci->contacts = g_slist_prepend (gci->contacts, contact);
516 			}
517 			count++;
518 		}
519 		if (contact == NULL) {
520 			gci->state = 1;
521 			gci->list_iterator = gci->list_contacts;
522 		}
523 	}
524 	if (gci->state == 1) {
525 		for (iter = gci->list_iterator; count < 50 && iter; iter = iter->next) {
526 			gchar *uid = NULL;
527 
528 			contact = iter->data;
529 			resolve_list_card (gci, contact);
530 			e_book_client_add_contact_sync (
531 				gci->book_client, contact, E_BOOK_OPERATION_FLAG_NONE, &uid, NULL, NULL);
532 			if (uid != NULL) {
533 				e_contact_set (contact, E_CONTACT_UID, uid);
534 				g_free (uid);
535 			}
536 			count++;
537 		}
538 		gci->list_iterator = iter;
539 		if (iter == NULL)
540 			gci->state = 2;
541 	}
542 	if (gci->state == 2) {
543 		ldif_import_done (gci);
544 		return FALSE;
545 	} else {
546 		e_import_status (
547 			gci->import, gci->target, _("Importing…"),
548 			ftell (gci->file) * 100 / gci->size);
549 		return TRUE;
550 	}
551 }
552 
553 static void
primary_selection_changed_cb(ESourceSelector * selector,EImportTarget * target)554 primary_selection_changed_cb (ESourceSelector *selector,
555                               EImportTarget *target)
556 {
557 	ESource *source;
558 
559 	source = e_source_selector_ref_primary_selection (selector);
560 	g_return_if_fail (source != NULL);
561 
562 	g_datalist_set_data_full (
563 		&target->data, "ldif-source",
564 		source, (GDestroyNotify) g_object_unref);
565 }
566 
567 static GtkWidget *
ldif_getwidget(EImport * ei,EImportTarget * target,EImportImporter * im)568 ldif_getwidget (EImport *ei,
569                 EImportTarget *target,
570                 EImportImporter *im)
571 {
572 	EShell *shell;
573 	GtkWidget *vbox, *selector, *scrolled_window;
574 	ESourceRegistry *registry;
575 	ESource *primary;
576 	const gchar *extension_name;
577 
578 	vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
579 
580 	shell = e_shell_get_default ();
581 	registry = e_shell_get_registry (shell);
582 	extension_name = E_SOURCE_EXTENSION_ADDRESS_BOOK;
583 
584 	scrolled_window = gtk_scrolled_window_new (NULL, NULL);
585 	g_object_set (G_OBJECT (scrolled_window),
586 		"hscrollbar-policy", GTK_POLICY_AUTOMATIC,
587 		"vscrollbar-policy", GTK_POLICY_AUTOMATIC,
588 		NULL);
589 	gtk_box_pack_start (GTK_BOX (vbox), scrolled_window, TRUE, TRUE, 6);
590 
591 	selector = e_source_selector_new (registry, extension_name);
592 	e_source_selector_set_show_toggles (
593 		E_SOURCE_SELECTOR (selector), FALSE);
594 	gtk_container_add (GTK_CONTAINER (scrolled_window), selector);
595 
596 	primary = g_datalist_get_data (&target->data, "ldif-source");
597 	if (primary == NULL) {
598 		GList *list;
599 
600 		list = e_source_registry_list_sources (registry, extension_name);
601 		if (list != NULL) {
602 			primary = g_object_ref (list->data);
603 			g_datalist_set_data_full (
604 				&target->data, "ldif-source", primary,
605 				(GDestroyNotify) g_object_unref);
606 		}
607 
608 		g_list_free_full (list, (GDestroyNotify) g_object_unref);
609 	}
610 	e_source_selector_set_primary_selection (
611 		E_SOURCE_SELECTOR (selector), primary);
612 
613 	g_signal_connect (
614 		selector, "primary_selection_changed",
615 		G_CALLBACK (primary_selection_changed_cb), target);
616 
617 	gtk_widget_show_all (vbox);
618 
619 	return vbox;
620 }
621 
622 static const gchar *supported_extensions[3] = {
623 	".ldif", ".ldi", NULL
624 };
625 
626 static gboolean
ldif_supported(EImport * ei,EImportTarget * target,EImportImporter * im)627 ldif_supported (EImport *ei,
628                 EImportTarget *target,
629                 EImportImporter *im)
630 {
631 	gchar *ext;
632 	gint i;
633 	EImportTargetURI *s;
634 
635 	if (target->type != E_IMPORT_TARGET_URI)
636 		return FALSE;
637 
638 	s = (EImportTargetURI *) target;
639 	if (s->uri_src == NULL)
640 		return TRUE;
641 
642 	if (strncmp (s->uri_src, "file:///", 8) != 0)
643 		return FALSE;
644 
645 	ext = strrchr (s->uri_src, '.');
646 	if (ext == NULL)
647 		return FALSE;
648 
649 	for (i = 0; supported_extensions[i] != NULL; i++) {
650 		if (g_ascii_strcasecmp (supported_extensions[i], ext) == 0)
651 			return TRUE;
652 	}
653 
654 	return FALSE;
655 }
656 
657 static void
ldif_import_done(LDIFImporter * gci)658 ldif_import_done (LDIFImporter *gci)
659 {
660 	if (gci->idle_id)
661 		g_source_remove (gci->idle_id);
662 
663 	fclose (gci->file);
664 	g_object_unref (gci->book_client);
665 	g_slist_foreach (gci->contacts, (GFunc) g_object_unref, NULL);
666 	g_slist_foreach (gci->list_contacts, (GFunc) g_object_unref, NULL);
667 	g_slist_free (gci->contacts);
668 	g_slist_free (gci->list_contacts);
669 	g_hash_table_destroy (gci->dn_contact_hash);
670 
671 	e_import_complete (gci->import, gci->target, NULL);
672 	g_object_unref (gci->import);
673 
674 	g_free (gci);
675 }
676 
677 static void
book_client_connect_cb(GObject * source_object,GAsyncResult * result,gpointer user_data)678 book_client_connect_cb (GObject *source_object,
679                         GAsyncResult *result,
680                         gpointer user_data)
681 {
682 	LDIFImporter *gci = user_data;
683 	EClient *client;
684 
685 	client = e_book_client_connect_finish (result, NULL);
686 
687 	if (client == NULL) {
688 		ldif_import_done (gci);
689 		return;
690 	}
691 
692 	gci->book_client = E_BOOK_CLIENT (client);
693 	gci->idle_id = g_idle_add (ldif_import_contacts, gci);
694 }
695 
696 static void
ldif_import(EImport * ei,EImportTarget * target,EImportImporter * im)697 ldif_import (EImport *ei,
698              EImportTarget *target,
699              EImportImporter *im)
700 {
701 	LDIFImporter *gci;
702 	ESource *source;
703 	FILE *file = NULL;
704 	EImportTargetURI *s = (EImportTargetURI *) target;
705 	gchar *filename;
706 	gint errn = 0;
707 
708 	filename = g_filename_from_uri (s->uri_src, NULL, NULL);
709 	if (filename != NULL) {
710 		file = g_fopen (filename, "r");
711 		errn = errno;
712 		g_free (filename);
713 	}
714 	if (file == NULL) {
715 		GError *error;
716 
717 		error = g_error_new_literal (G_IO_ERROR, g_io_error_from_errno (errn), _("Can’t open .ldif file"));
718 		e_import_complete (ei, target, error);
719 		g_clear_error (&error);
720 
721 		return;
722 	}
723 
724 	gci = g_malloc0 (sizeof (*gci));
725 	g_datalist_set_data (&target->data, "ldif-data", gci);
726 	gci->import = g_object_ref (ei);
727 	gci->target = target;
728 	gci->file = file;
729 	fseek (file, 0, SEEK_END);
730 	gci->size = ftell (file);
731 	fseek (file, 0, SEEK_SET);
732 	gci->dn_contact_hash = g_hash_table_new_full (
733 		g_str_hash, g_str_equal,
734 		(GDestroyNotify) g_free,
735 		(GDestroyNotify) NULL);
736 
737 	source = g_datalist_get_data (&target->data, "ldif-source");
738 
739 	e_book_client_connect (source, 30, NULL, book_client_connect_cb, gci);
740 }
741 
742 static void
ldif_cancel(EImport * ei,EImportTarget * target,EImportImporter * im)743 ldif_cancel (EImport *ei,
744              EImportTarget *target,
745              EImportImporter *im)
746 {
747 	LDIFImporter *gci = g_datalist_get_data (&target->data, "ldif-data");
748 
749 	if (gci)
750 		gci->state = 2;
751 }
752 
753 static GtkWidget *
ldif_get_preview(EImport * ei,EImportTarget * target,EImportImporter * im)754 ldif_get_preview (EImport *ei,
755                   EImportTarget *target,
756                   EImportImporter *im)
757 {
758 	GtkWidget *preview;
759 	GSList *contacts = NULL;
760 	EContact *contact;
761 	EImportTargetURI *s = (EImportTargetURI *) target;
762 	gchar *filename;
763 	GHashTable *dn_contact_hash;
764 	FILE *file;
765 
766 	filename = g_filename_from_uri (s->uri_src, NULL, NULL);
767 	if (filename == NULL) {
768 		g_message (G_STRLOC ": Couldn't get filename from URI '%s'", s->uri_src);
769 		return NULL;
770 	}
771 
772 	file = g_fopen (filename, "r");
773 	g_free (filename);
774 
775 	if (file == NULL) {
776 		g_message (G_STRLOC ": Can't open .ldif file");
777 		return NULL;
778 	}
779 
780 	dn_contact_hash = g_hash_table_new_full (
781 		g_str_hash, g_str_equal,
782 		(GDestroyNotify) g_free,
783 		(GDestroyNotify) NULL);
784 
785 	while (contact = getNextLDIFEntry (dn_contact_hash, file), contact != NULL) {
786 		if (!e_contact_get (contact, E_CONTACT_IS_LIST)) {
787 			add_to_notes (contact, E_CONTACT_OFFICE);
788 			add_to_notes (contact, E_CONTACT_SPOUSE);
789 			add_to_notes (contact, E_CONTACT_BLOG_URL);
790 		}
791 
792 		contacts = g_slist_prepend (contacts, contact);
793 	}
794 
795 	g_hash_table_destroy (dn_contact_hash);
796 
797 	contacts = g_slist_reverse (contacts);
798 	preview = evolution_contact_importer_get_preview_widget (contacts);
799 
800 	g_slist_free_full (contacts, (GDestroyNotify) g_object_unref);
801 	fclose (file);
802 
803 	return preview;
804 }
805 
806 static EImportImporter ldif_importer = {
807 	E_IMPORT_TARGET_URI,
808 	0,
809 	ldif_supported,
810 	ldif_getwidget,
811 	ldif_import,
812 	ldif_cancel,
813 	ldif_get_preview,
814 };
815 
816 EImportImporter *
evolution_ldif_importer_peek(void)817 evolution_ldif_importer_peek (void)
818 {
819 	ldif_importer.name = _("LDAP Data Interchange Format (.ldif)");
820 	ldif_importer.description = _("Evolution LDIF importer");
821 
822 	return &ldif_importer;
823 }
824