[couchdb-glib] Move from multiline 'street' field to 'address1' + 'address2'



commit 106d4116150b4f6f9e466b29eacc97bb041af435
Author: Rodrigo Moya <rodrigo gnome-db org>
Date:   Thu Apr 29 16:23:15 2010 +0200

    Move from multiline 'street' field to 'address1' + 'address2'

 desktopcouch-glib/desktopcouch-document-contact.c |   70 ++++++++++++++++++---
 1 files changed, 60 insertions(+), 10 deletions(-)
---
diff --git a/desktopcouch-glib/desktopcouch-document-contact.c b/desktopcouch-glib/desktopcouch-document-contact.c
index 0f2a965..800c597 100644
--- a/desktopcouch-glib/desktopcouch-document-contact.c
+++ b/desktopcouch-glib/desktopcouch-document-contact.c
@@ -505,9 +505,21 @@ desktopcouch_document_contact_set_addresses (CouchdbDocument *document, GSList *
 		street_str = desktopcouch_document_contact_address_get_street (sf);
 		if (street_str) {
 			JsonObject *this_address;
+			gchar **lines;
 
 			this_address = json_object_new ();
-			json_object_set_string_member (this_address, "street", street_str);
+
+			lines = g_strsplit (street_str, "\n", 2);
+			if (lines != NULL) {
+				g_debug ("lines[0] = %s, lines[1] = %s", lines[0], lines[1]);
+				json_object_set_string_member (this_address, "address1", lines[0]);
+				if (lines[1] != NULL)
+					json_object_set_string_member (this_address, "address2", lines[1]);
+
+				g_strfreev (lines);
+			} else
+				json_object_set_string_member (this_address, "address1", street_str);
+
 			json_object_set_string_member (this_address, "city",
 						       desktopcouch_document_contact_address_get_city (sf));
 			json_object_set_string_member (this_address, "state",
@@ -809,13 +821,13 @@ desktopcouch_document_contact_phone_set_description (CouchdbStructField *sf, con
 
 CouchdbStructField *
 desktopcouch_document_contact_address_new (const char *uuid,
-				      const char *street,
-				      const char *city,
-				      const char *state,
-				      const char *country,
-				      const char *postalcode,
-				      const char *pobox,
-				      const char *description)
+					   const char *street,
+					   const char *city,
+					   const char *state,
+					   const char *country,
+					   const char *postalcode,
+					   const char *pobox,
+					   const char *description)
 {
 	CouchdbStructField *sf;
 
@@ -849,17 +861,55 @@ desktopcouch_document_contact_address_new (const char *uuid,
 const char *
 desktopcouch_document_contact_address_get_street (CouchdbStructField *sf)
 {
+	const char *street;
+
 	g_return_val_if_fail (sf != NULL, NULL);
 
-	return couchdb_struct_field_get_string_field (sf, "street");
+	/* In previous versions of the Freedesktop specification for contacts,
+	   "street" was used to contain a multiline text for the street and
+	   extended street information. This has been changed to "address1" and
+	   "address2" to match what is used on the Ubuntu One web contacts web UI,
+	   so this is the migration path, to support old records */
+	street = couchdb_struct_field_get_string_field (sf, "street");
+	if (street == NULL) {
+		const char *address1, *address2;
+
+		address1 = couchdb_struct_field_get_string_field (sf, "address1");
+		if (address1 != NULL) {
+			address2 = couchdb_struct_field_get_string_field (sf, "address2");
+			if (address2 != NULL) {
+				static gchar *multiline_street = NULL;
+
+				if (multiline_street != NULL)
+					g_free (multiline_street);
+
+				multiline_street = g_strdup_printf ("%s\n%s", address1, address2);
+
+				return (const char *) multiline_street;
+			} else
+				return address1;
+		}
+	}
+
+	return street;
 }
 
 void
 desktopcouch_document_contact_address_set_street (CouchdbStructField *sf, const char *street)
 {
+	gchar **lines;
+
 	g_return_if_fail (sf != NULL);
 
-	couchdb_struct_field_set_string_field (sf, "street", street);
+	lines = g_strsplit (street, "\n", 2);
+	if (lines != NULL) {
+		couchdb_struct_field_set_string_field (sf, "address1", lines[0]);
+		if (lines[1] != NULL)
+			couchdb_struct_field_set_string_field (sf, "address2", lines[1]);
+
+		g_strfreev (lines);
+	} else
+		couchdb_struct_field_set_string_field (sf, "address1", street);
 }
 
 const char *



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