From de2731ec74cd42770bbd46f04ddd9412ab37fd72 Mon Sep 17 00:00:00 2001
From: root <root@example.com>
Date: Sun, 18 May 2014 16:17:41 +0200
Subject: [PATCH 2/2] category sync

---
 ActiveSync/NGDOMElement+ActiveSync.m       |    5 ++-
 ActiveSync/NGVCard+ActiveSync.m            |   66 +++++++++++++++++++++++++---
 ActiveSync/SOGoActiveSyncDispatcher+Sync.m |    2 +-
 3 files changed, 66 insertions(+), 7 deletions(-)

diff --git a/ActiveSync/NGDOMElement+ActiveSync.m b/ActiveSync/NGDOMElement+ActiveSync.m
index 1a44e8a..b5e7764 100644
--- a/ActiveSync/NGDOMElement+ActiveSync.m
+++ b/ActiveSync/NGDOMElement+ActiveSync.m
@@ -101,7 +101,7 @@ static NSArray *asElementArray = nil;
   int i, count;
   
   if (!asElementArray)
-    asElementArray = [[NSArray alloc] initWithObjects: @"Attendee", nil];
+    asElementArray = [[NSArray alloc] initWithObjects: @"Attendee", @"Category", nil];
 
   data = [NSMutableDictionary dictionary];
 
@@ -153,6 +153,9 @@ static NSArray *asElementArray = nil;
 
                       if ([innerTag isEqualToString: [innerElement tagName]])
                         {
+                         if ([(id)innerElement isTextNode])
+                          [innerElements addObject: [(NGDOMElement *)innerElement textValue]];
+                         else
                           [innerElements addObject: [(NGDOMElement *)innerElement applicationData]];
                         }
                       else
diff --git a/ActiveSync/NGVCard+ActiveSync.m b/ActiveSync/NGVCard+ActiveSync.m
index d6f8fa1..dd326c3 100644
--- a/ActiveSync/NGVCard+ActiveSync.m
+++ b/ActiveSync/NGVCard+ActiveSync.m
@@ -48,7 +48,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 - (NSString *) activeSyncRepresentationInContext: (WOContext *) context
 {
   CardElement *n, *homeAdr, *workAdr;
-  NSArray *emails, *addresses;
+  NSArray *emails, *addresses, *categories;
   NSMutableString *s;
   id o;
 
@@ -65,7 +65,39 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   
   if ((o = [self workCompany]))
     [s appendFormat: @"<CompanyName xmlns=\"Contacts:\">%@</CompanyName>", [o activeSyncRepresentationInContext: context]];
-  
+
+  if ((o = [[self org] flattenedValueAtIndex: 1 forKey: @""]))
+    [s appendFormat: @"<Department xmlns=\"Contacts:\">%@</Department>", [o activeSyncRepresentationInContext: context]];
+
+  categories = [self categories];
+  if ([categories count]) {
+    [s appendFormat: @"<Categories xmlns=\"Contacts:\">"];
+  for (i = 0; i < [categories count]; i++)
+    {
+      [s appendFormat: @"<Category xmlns=\"Contacts:\">%@</Category>", [categories objectAtIndex: i]];
+    }
+    [s appendFormat: @"</Categories>"];
+  }
+
+  NSArray *elements;
+  NSString *url;
+
+  elements = [self childrenWithTag: @"url"
+                      andAttribute: @"type"
+                       havingValue: @"work"];
+  if ([elements count] > 0) {
+    url = [[elements objectAtIndex: 0] flattenedValuesForKey: @""];
+    [s appendFormat: @"<WebPage xmlns=\"Contacts:\">%@</WebPage>", [url activeSyncRepresentationInContext: context]];
+  }
+
+
+   if ((o = [[self uniqueChildWithTag: @"x-aim"] flattenedValuesForKey: @""]))
+    [s appendFormat: @"<IMAddress xmlns=\"Contacts:\">%@</IMAddress>", [o activeSyncRepresentationInContext: context]];
+
+  if ((o = [self nickname]))
+    [s appendFormat: @"<NickName xmlns=\"Contacts:\">%@</NickName>", [o activeSyncRepresentationInContext: context]];
+
+
   if ((o = [self title]))
     [s appendFormat: @"<JobTitle xmlns=\"Contacts:\">%@</JobTitle>", [o activeSyncRepresentationInContext: context]];
 
@@ -183,6 +215,11 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   if ((o = [[theValues objectForKey: @"Body"] objectForKey: @"Data"]))
     [self setNote: o];
 
+  // Categories
+  if ((o = [theValues objectForKey: @"Categories"])) {
+      [self setCategories: o];
+  }
+
   // Birthday
   if ((o = [theValues objectForKey: @"Birthday"]))
     {
@@ -241,21 +278,36 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     {
       [self setOrg: o  units: nil];
     }
+
+
+  // Department
+  if ((o = [theValues objectForKey: @"Department"]))
+    {
+      [self setOrg: nil  units: [NSArray arrayWithObjects:o,nil]];
+    }
+
   
   // Email addresses
   if ((o = [theValues objectForKey: @"Email1Address"]))
     {
-      [self addEmail: o  types: [NSArray arrayWithObject: @"pref"]];
+      element = [self elementWithTag: @"email" ofType: @"work"];
+      [element setSingleValue: o forKey: @""];
+
     }
   
   if ((o = [theValues objectForKey: @"Email2Address"]))
     {
-      [self addEmail: o types: nil];
+      element = [self elementWithTag: @"email" ofType: @"home"];
+      [element setSingleValue: o  forKey: @""];
+
     }
   
+  // sogo supports just 2 emails ... but mobile might send 3
   if ((o = [theValues objectForKey: @"Email3Address"]))
     {
-      [self addEmail: o  types: nil];
+      element = [self elementWithTag: @"email" ofType: @"three"];
+      [element setSingleValue: o  forKey: @""];
+
     }
   
   // Formatted name
@@ -303,6 +355,10 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       [[self elementWithTag: @"url" ofType: @"work"]
           setSingleValue: o  forKey: @""];
     }
+
+  if ((o = [theValues objectForKey: @"NickName"]))
+    [self setNickname: o];
+
 }
 
 @end
diff --git a/ActiveSync/SOGoActiveSyncDispatcher+Sync.m b/ActiveSync/SOGoActiveSyncDispatcher+Sync.m
index 14fb5db..a3eec3b 100644
--- a/ActiveSync/SOGoActiveSyncDispatcher+Sync.m
+++ b/ActiveSync/SOGoActiveSyncDispatcher+Sync.m
@@ -800,7 +800,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                       break;
                   }
 
-                  if ([[dateCache objectForKey:key] compare: theFilterType ] == NSOrderedAscending ) 
+                  if ([[dateCache objectForKey:key] compare: theFilterType ] == NSOrderedAscending )
                   {
                      [s appendString: @"<SoftDelete xmlns=\"AirSync:\">"];
                      [s appendFormat: @"<ServerId xmlns=\"AirSync:\">%@</ServerId>", key];
-- 
1.7.9.5

