Re: [evolution-patches] improve import of .vcf files containing multiple vcards



The attached patch should handle embedded vcards properly. It will break
if there are several fields embedding vcards though. I can fix that if
you feel that's important to get that right.

Christophe

Le sam, 01/05/2004 à 09:11 -0700, Chris Toshok a écrit :
> this won't work, as vcards can actually contain other embedded vcards.
> Of course, those vcards probably break the parser, but we shouldn't be
> breaking them elsewhere as well.
> 
> Chris
> 
> On Sat, 2004-05-01 at 14:25 +0200, Christophe Fergeau wrote:
> > Hi,
> > 
> > eab_contact_list_from_string currently expects vcards to be separated by
> > 2 new lines characters when they are concatenated in a single file. The
> > attached patch looks for an "END:VCARD" string instead.
> > 
> > Christophe
> _______________________________________________
> Evolution-patches mailing list
> Evolution-patches lists ximian com
> http://lists.ximian.com/mailman/listinfo/evolution-patches
> 
> 
Index: addressbook/ChangeLog
===================================================================
RCS file: /cvs/gnome/evolution/addressbook/ChangeLog,v
retrieving revision 1.1684
diff -u -r1.1684 ChangeLog
--- addressbook/ChangeLog	30 Apr 2004 18:23:16 -0000	1.1684
+++ addressbook/ChangeLog	2 May 2004 11:15:18 -0000
@@ -1,3 +1,9 @@
+2004-05-01  Christophe Fergeau  <teuf gnome org>
+
+	* util/eab-book-util.c (eab_contact_list_from_string): don't assume
+	that if a file contains several vcards, they will be neatly separated
+	by \n\n
+
 2004-04-30  Chris Toshok  <toshok ximian com>
 
 	* util/e-destination.c (e_destination_set_contact): don't use
Index: addressbook/util/eab-book-util.c
===================================================================
RCS file: /cvs/gnome/evolution/addressbook/util/eab-book-util.c,v
retrieving revision 1.4
diff -u -r1.4 eab-book-util.c
--- addressbook/util/eab-book-util.c	23 Jan 2004 19:35:02 -0000	1.4
+++ addressbook/util/eab-book-util.c	2 May 2004 11:15:18 -0000
@@ -179,46 +179,80 @@
 	return retval;
 }
 
-GList*
-eab_contact_list_from_string (const char *str)
+
+#define VCARD_START "BEGIN:VCARD"
+#define VCARD_END   "END:VCARD"
+
+static gchar *
+eab_get_contact_from_string (const char *str)
 {
-	GList *contacts = NULL;
-	GString *gstr = g_string_new ("");
-	char *p = (char*)str;
-	char *q;
-	char *blank_line;
-
-	while (*p) {
-		if (*p != '\r') g_string_append_c (gstr, *p);
-		
-		p++;
-	}
-
-	p = g_string_free (gstr, FALSE);
-	q = p;
-	do {
-		char *temp;
-
-		blank_line = strstr (q, "\n\n");
-		if (blank_line) {
-			temp = g_strndup (q, blank_line - q);
-		}
-		else {
-			temp = g_strdup (q);
+	gchar *begin;
+	gchar *end;
+	gchar *it;
+	int count;
+	int i;
+	gchar *result;
+
+	if (str == NULL) {
+		return NULL;
+	}
+
+	begin = strstr (str, VCARD_START);
+	if (begin == NULL) {
+		return NULL;
+	}
+	end = strstr (begin, VCARD_END);
+	if (end == NULL) {
+		return NULL;
+	}
+	end += strlen (VCARD_END);
+
+	it = strstr (begin + strlen (VCARD_START), VCARD_START);
+	count = 0;
+	/* Skip BEGIN:VCARD tags of embedded VCards */
+	while ((it != NULL) && (it < end)) {
+		it = strstr (it + strlen (VCARD_START), VCARD_START);
+		count++;
+	}
+	if (it == NULL) {
+		goto done;
+	}
+
+	it = end;
+	/* Find the real END:VCARD tag if the VCard contains embedded VCards */
+	for (i = 0; i < count; i++) {
+		it = strstr (it,  VCARD_END);
+		if (it == NULL) {
+			goto done;
 		}
+		it += strlen (VCARD_END);
+	}
+	end = it;
 
-		contacts = g_list_append (contacts, e_contact_new_from_vcard (temp));
+ done:
+	result = g_strndup (begin, end - begin + 1);
+	return g_strstrip (result);
+}
 
-		g_free (temp);
 
-		if (blank_line)
-			q = blank_line + 2;
-		else
-			q = NULL;
-	} while (blank_line);
+GList*
+eab_contact_list_from_string (const char *str)
+{
+	GList *contacts = NULL;
+	gchar *it = str;
+	gchar *vcard = g_strdup ("");
 
-	g_free (p);
+	while (vcard != NULL) {
+		gchar *next;
 
+		it += strlen (vcard);
+		next = eab_get_contact_from_string (it);
+		g_free (vcard);
+		vcard = next;
+		if ((vcard != NULL) && (vcard != '\0')) {
+			contacts = g_list_append (contacts, e_contact_new_from_vcard (vcard));
+		}
+	}
 	return contacts;
 }
 

Attachment: signature.asc
Description: Ceci est une partie de message =?ISO-8859-1?Q?num=E9riquement?= =?ISO-8859-1?Q?_sign=E9e?=



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]