From 527faa38cc5cf50c3121cf40ad156fc2ea64a904 Mon Sep 17 00:00:00 2001
From: Felix Engel <fcengel@lina.(none)>
Date: Fri, 22 Oct 2010 17:05:06 +0200
Subject: [PATCH 1/2] Factor out address handling in nk6510.c to
 function PACK_ADDRESS

---
 common/phones/nk6510.c |  149 ++++++++++++++++++++++++++++++++----------------
 1 files changed, 100 insertions(+), 49 deletions(-)

diff --git a/common/phones/nk6510.c b/common/phones/nk6510.c
index a6aa049..bf3e68b 100644
--- a/common/phones/nk6510.c
+++ b/common/phones/nk6510.c
@@ -105,6 +105,13 @@ static path2mt_t s40_30_mt_mappings[] = {
 	{ -1, NULL }
 };
 
+static int addr2string(char* buf,
+ size_t size, gn_phonebook_address *adr);
+ 
+int PACK_ADDRESS(gn_phonebook_entry * entry, int count, char * req, int*
+block);
+
+
 static int match_sms_folder_mt(gn_memory_type mt)
 {
 	int i;
@@ -3058,7 +3065,6 @@ static gn_error NK6510_WritePhonebookLocation(gn_data *data, struct gn_statemach
 	unsigned int count;
 	gn_phonebook_entry *entry;
 	unsigned int postal_count;
-	int postal_block = 0;
 	gn_error error = GN_ERR_NONE;
 
 	if (data->phonebook_entry)
@@ -3220,54 +3226,8 @@ retry:
 				}
 			}
 			/* Addresses */
-			if (GN_PHONEBOOK_ENTRY_MAX_LENGTH - count > 13) { /* 13 is size of base address main part */
-				postal_count = count;
-				req[count++] = GN_PHONEBOOK_ENTRY_PostalAddress;
-				req[count++] = 0x00;
-				req[count++] = 0x00;
-				req[count++] = 0x08;
-				req[count++] = 0xff;
-				req[count++] = 0x00;
-				req[count++] = 0x00;
-				req[count++] = 0x00;
-				for (i = 0; i < entry->subentries_count; i++) {
-					int duplicate = 0;
-					/* Ignore duplicates */
-					for (j = 0; j < i; j++) {
-						if (entry->subentries[i].entry_type == entry->subentries[j].entry_type &&
-						    entry->subentries[i].number_type == entry->subentries[j].number_type &&
-						    !strcmp(entry->subentries[i].data.number, entry->subentries[j].data.number)) {
-							duplicate = 1;
-							break;
-						}
-					}
-					if (duplicate)
-						break;
-					switch (entry->subentries[i].entry_type) {
-					case GN_PHONEBOOK_ENTRY_ExtendedAddress:
-					case GN_PHONEBOOK_ENTRY_Postal:
-					case GN_PHONEBOOK_ENTRY_Street:
-					case GN_PHONEBOOK_ENTRY_City:
-					case GN_PHONEBOOK_ENTRY_StateProvince:
-					case GN_PHONEBOOK_ENTRY_ZipCode:
-					case GN_PHONEBOOK_ENTRY_Country:
-						j = strlen(entry->subentries[i].data.number);
-						j = char_unicode_encode((string + 1), entry->subentries[i].data.number, j);
-						string[j + 1] = 0;
-						string[0] = j;
-						count += PackBlock(entry->subentries[i].entry_type, j + 1, &postal_block, string, req + count, GN_PHONEBOOK_ENTRY_MAX_LENGTH - count);
-						break;
-					default:
-						break;
-					}
-				}
-				if (postal_block > 0) {
-					req[postal_count + 7] = postal_block;
-					block++;
-				} else {
-					count = postal_count;
-				}
-			}
+           count = PACK_ADDRESS(entry,count, req, &block);
+			
 		}
 		req[21] = block - 1;
 		dprintf("Writing phonebook entry %s...\n",entry->name);
@@ -6650,3 +6610,94 @@ static int sms_encode(gn_data *data, struct gn_statemachine *state, unsigned cha
 	return pos;
 }
 
+static int addr2string(char* buf, size_t size, gn_phonebook_address *adr)
+{
+    char empty[] = "";
+    int retval = 0;
+
+    retval = snprintf(buf,
+            GN_PHONEBOOK_ENTRY_MAX_LENGTH - 1,
+         "%s\n%s\n%s %s\n%s",
+        adr->extended_address,
+        adr->street,
+        adr->zipcode,
+        adr->city,
+        adr->country);
+   return retval;
+}
+int PACK_ADDRESS(gn_phonebook_entry * entry, int count, char * req, int*
+block)
+{
+	int postal_block = 0;
+    int postal_count = 0;
+    char buf[1024];  /* TODO: FIX BUFFER */
+    char string[1024]; /*TODO: FIX BUFFER */
+    
+    int i,j;
+#define has_all_address_fields 0
+    if(has_all_address_fields){     /* Todo: get from driver ? */
+if (GN_PHONEBOOK_ENTRY_MAX_LENGTH - count > 13) { 
+                /* 13 is size of base address main part */
+				postal_count = count;
+				req[count++] = GN_PHONEBOOK_ENTRY_PostalAddress;
+				req[count++] = 0x00;
+				req[count++] = 0x00;
+				req[count++] = 0x08;
+				req[count++] = 0xff;
+				req[count++] = 0x00;
+				req[count++] = 0x00;
+				req[count++] = 0x00;
+				for (i = 0; i < entry->subentries_count; i++) {
+					int duplicate = 0;
+					/* Ignore duplicates */
+					for (j = 0; j < i; j++) {
+						if (entry->subentries[i].entry_type == entry->subentries[j].entry_type &&
+						    entry->subentries[i].number_type == entry->subentries[j].number_type &&
+						    !strcmp(entry->subentries[i].data.number, entry->subentries[j].data.number)) {
+							duplicate = 1;
+							break;
+						}
+					}
+					if (duplicate)
+						break;
+					switch (entry->subentries[i].entry_type) {
+					case GN_PHONEBOOK_ENTRY_ExtendedAddress:
+					case GN_PHONEBOOK_ENTRY_Postal:
+					case GN_PHONEBOOK_ENTRY_Street:
+					case GN_PHONEBOOK_ENTRY_City:
+					case GN_PHONEBOOK_ENTRY_StateProvince:
+					case GN_PHONEBOOK_ENTRY_ZipCode:
+					case GN_PHONEBOOK_ENTRY_Country:
+						j = strlen(entry->subentries[i].data.number);
+						j = char_unicode_encode((string + 1), entry->subentries[i].data.number, j);
+						string[j + 1] = 0;
+						string[0] = j;
+						count += PackBlock(entry->subentries[i].entry_type, j + 1, &postal_block, string, req + count, GN_PHONEBOOK_ENTRY_MAX_LENGTH - count);
+						break;
+					default:
+						break;
+					}
+				}
+				if (postal_block > 0) {
+					req[postal_count + 7] = postal_block;
+				    (*block)++;  
+				} else {
+					count = postal_count;
+				}
+			}
+    } else {
+        if(entry->address.has_address){
+            addr2string(buf,1024,&(entry->address));
+            j = strlen(buf);
+            j = char_unicode_encode((string + 1), buf, j);
+            string[j + 1] = 0;
+            string[0] = j;
+            dprintf("[I] Adding Address as type 0x09: %s\n",buf);
+            count += PackBlock(GN_PHONEBOOK_ENTRY_Postal,
+                j + 1, block, string, req + count,
+                GN_PHONEBOOK_ENTRY_MAX_LENGTH - count);
+        }
+
+    }
+    return count;
+}
-- 
1.7.0.4

