From e7f38f940ea02849b6fb8b1a2898066181969495 Mon Sep 17 00:00:00 2001 From: Ludovic Marcotte Date: Wed, 22 Jan 2014 11:27:27 -0500 Subject: [PATCH] Moved the cards' logic into a category and added support for more AS ones --- ActiveSync/NGVCard+ActiveSync.m | 112 ++++++++++++++++++++-- SoObjects/Contacts/NGVCard+SOGo.h | 15 ++- SoObjects/Contacts/NGVCard+SOGo.m | 151 ++++++++++++++++++++++++++++++ UI/Contacts/UIxContactView.m | 135 ++++++-------------------- 4 files changed, 297 insertions(+), 116 deletions(-) diff --git a/ActiveSync/NGVCard+ActiveSync.m b/ActiveSync/NGVCard+ActiveSync.m index 1255e90f4..f920ed6c7 100644 --- a/ActiveSync/NGVCard+ActiveSync.m +++ b/ActiveSync/NGVCard+ActiveSync.m @@ -35,24 +35,124 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #import +#import + @implementation NGVCard (ActiveSync) - (NSString *) activeSyncRepresentation { - NSString *firstName, *lastName; + CardElement *n, *homeAdr, *workAdr; NSMutableString *s; - CardElement *n; + NSArray *emails; + id o; + + int i; s = [NSMutableString string]; n = [self n]; - lastName = [n flattenedValueAtIndex: 0 forKey: @""]; - [s appendFormat: @"%@", lastName]; + if ((o = [n flattenedValueAtIndex: 0 forKey: @""])) + [s appendFormat: @"%@", o]; + if ((o = [n flattenedValueAtIndex: 1 forKey: @""])) + [s appendFormat: @"%@", o]; - firstName = [n flattenedValueAtIndex: 1 forKey: @""]; - [s appendFormat: @"%@", firstName]; + if ((o = [self workCompany])) + [s appendFormat: @"%@", o]; + + if ((o = [self title])) + [s appendFormat: @"%@", o]; + if ((o = [self preferredEMail])) [s appendFormat: @"%@", o]; + + [s appendFormat: @"%@", o]; [s appendFormat: @"%@", o]; + [s appendFormat: @"%@", o]; + + + // Secondary email addresses + emails = [self secondaryEmails]; + [s appendFormat: @"%@", o]; + + for (i = 0; i < [emails count]; i++) + { + o = [[emails objectAtIndex: i] flattenedValuesForKey: @""]; + + [s appendFormat: @"%@", i+2, o, i+2]; + + if (i == 1) + break; + } + + // Telephone numbers + if ((o = [self workPhone])) + [s appendFormat: @"%@", o]; + + if ((o = [self homePhone])) + [s appendFormat: @"%@", o]; + + if ((o = [self fax])) + [s appendFormat: @"%@", o]; + + if ((o = [self mobile])) + [s appendFormat: @"%@", o]; + + if ((o = [self pager])) + [s appendFormat: @"%@", o]; + + // Home Address + homeAdr = [[self childrenWithTag: @"adr" + andAttribute: @"type" + havingValue: @"home"] objectAtIndex: 0]; + + if ((o = [homeAdr flattenedValueAtIndex: 2 forKey: @""])) + [s appendFormat: @"%@", o]; + + if ((o = [homeAdr flattenedValueAtIndex: 3 forKey: @""])) + [s appendFormat: @"%@", o]; + + if ((o = [homeAdr flattenedValueAtIndex: 4 forKey: @""])) + [s appendFormat: @"%@", o]; + + if ((o = [homeAdr flattenedValueAtIndex: 5 forKey: @""])) + [s appendFormat: @"%@", o]; + + if ((o = [homeAdr flattenedValueAtIndex: 6 forKey: @""])) + [s appendFormat: @"%@", o]; + + // Work Address + workAdr = [[self childrenWithTag: @"adr" + andAttribute: @"type" + havingValue: @"work"] objectAtIndex: 0]; + + if ((o = [workAdr flattenedValueAtIndex: 2 forKey: @""])) + [s appendFormat: @"%@", o]; + + if ((o = [workAdr flattenedValueAtIndex: 3 forKey: @""])) + [s appendFormat: @"%@", o]; + + if ((o = [workAdr flattenedValueAtIndex: 4 forKey: @""])) + [s appendFormat: @"%@", o]; + + if ((o = [workAdr flattenedValueAtIndex: 5 forKey: @""])) + [s appendFormat: @"%@", o]; + + if ((o = [workAdr flattenedValueAtIndex: 6 forKey: @""])) + [s appendFormat: @"%@", o]; + + // Other, less important fields + if ((o = [self birthday])) + [s appendFormat: @"%@", [o activeSyncRepresentation]]; + + if ((o = [self note])) + { + [s appendString: @""]; + [s appendFormat: @"%d", 1]; + [s appendFormat: @"%d", [o length]]; + [s appendFormat: @"%d", 0]; + [s appendFormat: @"%@", o]; + [s appendString: @""]; + } + return s; } diff --git a/SoObjects/Contacts/NGVCard+SOGo.h b/SoObjects/Contacts/NGVCard+SOGo.h index 49dcd55d0..5ad1d8986 100644 --- a/SoObjects/Contacts/NGVCard+SOGo.h +++ b/SoObjects/Contacts/NGVCard+SOGo.h @@ -1,8 +1,6 @@ /* NGVCard+SOGo.h - this file is part of SOGo * - * Copyright (C) 2009 Inverse inc. - * - * Author: Cyril Robert + * Copyright (C) 2009-2014 Inverse inc. * * This file is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -33,6 +31,17 @@ - (void) updateFromLDIFRecord: (NSDictionary *) ldifRecord; - (NSMutableDictionary *) asLDIFRecord; +- (NSString *) workCompany; +- (NSString *) fullName; +- (NSArray *) secondaryEmails; + +- (NSString *) workPhone; +- (NSString *) homePhone; +- (NSString *) fax; +- (NSString *) mobile; +- (NSString *) pager; +- (NSCalendarDate *) birthday; + @end #endif /* NGVCARD_SOGO_H */ diff --git a/SoObjects/Contacts/NGVCard+SOGo.m b/SoObjects/Contacts/NGVCard+SOGo.m index 5e5a6b774..571da2aa0 100644 --- a/SoObjects/Contacts/NGVCard+SOGo.m +++ b/SoObjects/Contacts/NGVCard+SOGo.m @@ -27,6 +27,8 @@ #import #import +#import + #import "NSDictionary+LDIF.h" #import "NGVCard+SOGo.h" @@ -637,4 +639,153 @@ convention: return ldifRecord; } +- (NSString *) workCompany +{ + CardElement *org; + NSString *company; + + org = [self org]; + company = [org flattenedValueAtIndex: 0 forKey: @""]; + if ([company length] == 0) + company = nil; + + return company; +} + +- (NSString *) fullName +{ + CardElement *n; + NSString *fn, *firstName, *lastName, *org; + + fn = [self fn]; + if ([fn length] == 0) + { + n = [self n]; + lastName = [n flattenedValueAtIndex: 0 forKey: @""]; + firstName = [n flattenedValueAtIndex: 1 forKey: @""]; + if ([firstName length] > 0) + { + if ([lastName length] > 0) + fn = [NSString stringWithFormat: @"%@ %@", firstName, lastName]; + else + fn = firstName; + } + else if ([lastName length] > 0) + fn = lastName; + else + { + n = [self org]; + org = [n flattenedValueAtIndex: 0 forKey: @""]; + fn = org; + } + } + + return fn; +} + +- (NSArray *) secondaryEmails +{ + NSMutableArray *emails; + NSString *email; + int i; + + emails = [NSMutableArray array]; + + [emails addObjectsFromArray: [self childrenWithTag: @"email"]]; + [emails removeObjectsInArray: [self childrenWithTag: @"email" + andAttribute: @"type" + havingValue: @"pref"]]; + + for (i = [emails count]-1; i >= 0; i--) + { + email = [[emails objectAtIndex: i] flattenedValuesForKey: @""]; + + if ([email caseInsensitiveCompare: [self preferredEMail]] == NSOrderedSame) + [emails removeObjectAtIndex: i]; + } + + return emails; +} + +- (NSString *) _phoneOfType: (NSString *) aType + excluding: (NSString *) aTypeToExclude +{ + NSArray *elements, *phones; + NSString *phone; + + phones = [self childrenWithTag: @"tel"]; + elements = [phones cardElementsWithAttribute: @"type" + havingValue: aType]; + + phone = nil; + + if ([elements count] > 0) + { + CardElement *ce; + int i; + + for (i = 0; i < [elements count]; i++) + { + ce = [elements objectAtIndex: i]; + phone = [ce flattenedValuesForKey: @""]; + + if (!aTypeToExclude) + break; + + if (![ce hasAttribute: @"type" havingValue: aTypeToExclude]) + break; + + phone = nil; + } + } + + return phone; +} + +- (NSString *) workPhone +{ + // We do this (exclude FAX) in order to avoid setting the WORK number as the FAX + // one if we do see the FAX field BEFORE the WORK number. + return [self _phoneOfType: @"work" excluding: @"fax"]; +} + +- (NSString *) homePhone +{ + return [self _phoneOfType: @"home" excluding: @"fax"]; +} + +- (NSString *) fax +{ + return [self _phoneOfType: @"fax" excluding: nil]; +} + +- (NSString *) mobile +{ + return [self _phoneOfType: @"cell" excluding: nil]; +} + +- (NSString *) pager +{ + return [self _phoneOfType: @"pager" excluding: nil]; +} + +- (NSCalendarDate *) birthday +{ + NSString *bday, *value; + NSCalendarDate *date; + + bday = [self bday]; + date = nil; + if (bday) + { + // Expected format of BDAY is YYYY[-]MM[-]DD + value = [bday stringByReplacingString: @"-" withString: @""]; + date = [NSCalendarDate dateFromShortDateString: value + andShortTimeString: nil + inTimeZone: nil]; + } + + return date; +} + @end /* NGVCard */ diff --git a/UI/Contacts/UIxContactView.m b/UI/Contacts/UIxContactView.m index 830864fe7..d7e282762 100644 --- a/UI/Contacts/UIxContactView.m +++ b/UI/Contacts/UIxContactView.m @@ -33,6 +33,8 @@ #import #import #import + +#import #import #import "UIxContactView.h" @@ -107,33 +109,7 @@ - (NSString *) fullName { - CardElement *n; - NSString *fn, *firstName, *lastName, *org; - - fn = [card fn]; - if ([fn length] == 0) - { - n = [card n]; - lastName = [n flattenedValueAtIndex: 0 forKey: @""]; - firstName = [n flattenedValueAtIndex: 1 forKey: @""]; - if ([firstName length] > 0) - { - if ([lastName length] > 0) - fn = [NSString stringWithFormat: @"%@ %@", firstName, lastName]; - else - fn = firstName; - } - else if ([lastName length] > 0) - fn = lastName; - else - { - n = [card org]; - org = [n flattenedValueAtIndex: 0 forKey: @""]; - fn = org; - } - } - - return fn; + return [card fullName]; } - (NSString *) primaryEmail @@ -159,19 +135,14 @@ - (NSArray *) secondaryEmails { - NSString *email, *fn, *mailTo; - NSMutableArray *emails; NSMutableArray *secondaryEmails; + NSString *email, *fn, *mailTo; + NSArray *emails; - emails = [NSMutableArray array]; + emails = [card secondaryEmails]; secondaryEmails = [NSMutableArray array]; mailTo = nil; - [emails addObjectsFromArray: [card childrenWithTag: @"email"]]; - [emails removeObjectsInArray: [card childrenWithTag: @"email" - andAttribute: @"type" - havingValue: @"pref"]]; - // We might not have a preferred item but rather something like this: // EMAIL;TYPE=work:dd@ee.com // EMAIL;TYPE=home:ff@gg.com @@ -189,20 +160,16 @@ for (i = 0; i < [emails count]; i++) { email = [[emails objectAtIndex: i] flattenedValuesForKey: @""]; - - // skip primary email - if ([email caseInsensitiveCompare: [card preferredEMail]] != NSOrderedSame) - { - fn = [card fn]; - fn = [fn stringByReplacingString: @"\"" withString: @""]; - fn = [fn stringByReplacingString: @"'" withString: @"\\\'"]; - mailTo = [NSString stringWithFormat: @"');\">" - @"%@", email, fn, email, email]; - [secondaryEmails addObject: [self _cardStringWithLabel: nil - value: mailTo]]; - } - } + fn = [card fn]; + fn = [fn stringByReplacingString: @"\"" withString: @""]; + fn = [fn stringByReplacingString: @"'" withString: @"\\\'"]; + mailTo = [NSString stringWithFormat: @"');\">" + @"%@", email, fn, email, email]; + + [secondaryEmails addObject: [self _cardStringWithLabel: nil + value: mailTo]]; + } } else { @@ -256,66 +223,31 @@ return ([phones count] > 0); } -- (NSString *) _phoneOfType: (NSString *) aType - withLabel: (NSString *) aLabel - excluding: (NSString *) aTypeToExclude -{ - NSArray *elements; - NSString *phone; - - elements = [phones cardElementsWithAttribute: @"type" - havingValue: aType]; - - phone = nil; - - if ([elements count] > 0) - { - CardElement *ce; - int i; - - for (i = 0; i < [elements count]; i++) - { - ce = [elements objectAtIndex: i]; - phone = [ce flattenedValuesForKey: @""]; - - if (!aTypeToExclude) - break; - - if (![ce hasAttribute: @"type" havingValue: aTypeToExclude]) - break; - - phone = nil; - } - } - - return [self _cardStringWithLabel: aLabel value: phone url: @"tel"]; -} - - (NSString *) workPhone { // We do this (exclude FAX) in order to avoid setting the WORK number as the FAX // one if we do see the FAX field BEFORE the WORK number. - return [self _phoneOfType: @"work" withLabel: @"Work:" excluding: @"fax"]; + return [self _cardStringWithLabel: @"Work:" value: [card workPhone] url: @"tel"]; } - (NSString *) homePhone { - return [self _phoneOfType: @"home" withLabel: @"Home:" excluding: @"fax"]; + return [self _cardStringWithLabel: @"Home:" value: [card homePhone] url: @"tel"]; } - (NSString *) fax { - return [self _phoneOfType: @"fax" withLabel: @"Fax:" excluding: nil]; + return [self _cardStringWithLabel: @"Fax:" value: [card fax] url: @"tel"]; } - (NSString *) mobile { - return [self _phoneOfType: @"cell" withLabel: @"Mobile:" excluding: nil]; + return [self _cardStringWithLabel: @"Mobile:" value: [card mobile] url: @"tel"]; } - (NSString *) pager { - return [self _phoneOfType: @"pager" withLabel: @"Pager:" excluding: nil]; + return [self _cardStringWithLabel: @"Pager:" value: [card pager] url: @"tel"]; } - (BOOL) hasHomeInfos @@ -535,15 +467,7 @@ - (NSString *) workCompany { - CardElement *org; - NSString *company; - - org = [card org]; - company = [org flattenedValueAtIndex: 0 forKey: @""]; - if ([company length] == 0) - company = nil; - - return [self _cardStringWithLabel: nil value: company]; + return [self _cardStringWithLabel: nil value: [card workCompany]]; } - (NSString *) workPobox @@ -615,18 +539,15 @@ - (NSString *) bday { - NSString *bday, *value; - NSCalendarDate *date; SOGoDateFormatter *dateFormatter; + NSCalendarDate *date; + NSString *bday; + + date = [card birthday]; + bday = nil; - bday = [card bday]; - if (bday) + if (date) { - // Expected format of BDAY is YYYY[-]MM[-]DD - value = [bday stringByReplacingString: @"-" withString: @""]; - date = [NSCalendarDate dateFromShortDateString: value - andShortTimeString: nil - inTimeZone: nil]; dateFormatter = [[[self context] activeUser] dateFormatterInContext: context]; bday = [dateFormatter formattedDate: date]; }