Moved the cards' logic into a category and added support for more AS ones

This commit is contained in:
Ludovic Marcotte
2014-01-22 11:27:27 -05:00
parent f7ba5d2346
commit e7f38f940e
4 changed files with 297 additions and 116 deletions
+106 -6
View File
@@ -35,24 +35,124 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#import <NGCards/CardElement.h>
#import <Contacts/NGVCard+SOGo.h>
@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 xmlns=\"Contacts:\">%@</LastName>", lastName];
if ((o = [n flattenedValueAtIndex: 0 forKey: @""]))
[s appendFormat: @"<LastName xmlns=\"Contacts:\">%@</LastName>", o];
if ((o = [n flattenedValueAtIndex: 1 forKey: @""]))
[s appendFormat: @"<FirstName xmlns=\"Contacts:\">%@</FirstName>", o];
firstName = [n flattenedValueAtIndex: 1 forKey: @""];
[s appendFormat: @"<FirstName xmlns=\"Contacts:\">%@</FirstName>", firstName];
if ((o = [self workCompany]))
[s appendFormat: @"<CompanyName xmlns=\"Contacts:\">%@</CompanyName>", o];
if ((o = [self title]))
[s appendFormat: @"<JobTitle xmlns=\"Contacts:\">%@</JobTitle>", o];
if ((o = [self preferredEMail])) [s appendFormat: @"<HomePhoneNumber xmlns=\"Contacts:\">%@</HomePhoneNumber>", o];
[s appendFormat: @"<Email1Address xmlns=\"Contacts:\">%@</Email1Address>", o]; [s appendFormat: @"<HomePhoneNumber xmlns=\"Contacts:\">%@</HomePhoneNumber>", o];
[s appendFormat: @"<HomePhoneNumber xmlns=\"Contacts:\">%@</HomePhoneNumber>", o];
// Secondary email addresses
emails = [self secondaryEmails];
[s appendFormat: @"<HomePhoneNumber xmlns=\"Contacts:\">%@</HomePhoneNumber>", o];
for (i = 0; i < [emails count]; i++)
{
o = [[emails objectAtIndex: i] flattenedValuesForKey: @""];
[s appendFormat: @"<Email%dAddress xmlns=\"Contacts:\">%@</Email%dAddress>", i+2, o, i+2];
if (i == 1)
break;
}
// Telephone numbers
if ((o = [self workPhone]))
[s appendFormat: @"<BusinessPhoneNumber xmlns=\"Contacts:\">%@</BusinessPhoneNumber>", o];
if ((o = [self homePhone]))
[s appendFormat: @"<HomePhoneNumber xmlns=\"Contacts:\">%@</HomePhoneNumber>", o];
if ((o = [self fax]))
[s appendFormat: @"<BusinessFaxNumber xmlns=\"Contacts:\">%@</BusinessFaxNumber>", o];
if ((o = [self mobile]))
[s appendFormat: @"<MobilePhoneNumber xmlns=\"Contacts:\">%@</MobilePhoneNumber>", o];
if ((o = [self pager]))
[s appendFormat: @"<PagerNumber xmlns=\"Contacts:\">%@</PagerNumber>", o];
// Home Address
homeAdr = [[self childrenWithTag: @"adr"
andAttribute: @"type"
havingValue: @"home"] objectAtIndex: 0];
if ((o = [homeAdr flattenedValueAtIndex: 2 forKey: @""]))
[s appendFormat: @"<HomeStreet xmlns=\"Contacts:\">%@</HomeStreet>", o];
if ((o = [homeAdr flattenedValueAtIndex: 3 forKey: @""]))
[s appendFormat: @"<HomeCity xmlns=\"Contacts:\">%@</HomeCity>", o];
if ((o = [homeAdr flattenedValueAtIndex: 4 forKey: @""]))
[s appendFormat: @"<HomeState xmlns=\"Contacts:\">%@</HomeState>", o];
if ((o = [homeAdr flattenedValueAtIndex: 5 forKey: @""]))
[s appendFormat: @"<HomePostalCode xmlns=\"Contacts:\">%@</HomePostalCode>", o];
if ((o = [homeAdr flattenedValueAtIndex: 6 forKey: @""]))
[s appendFormat: @"<HomeCountry xmlns=\"Contacts:\">%@</HomeCountry>", o];
// Work Address
workAdr = [[self childrenWithTag: @"adr"
andAttribute: @"type"
havingValue: @"work"] objectAtIndex: 0];
if ((o = [workAdr flattenedValueAtIndex: 2 forKey: @""]))
[s appendFormat: @"<BusinessStreet xmlns=\"Contacts:\">%@</BusinessStreet>", o];
if ((o = [workAdr flattenedValueAtIndex: 3 forKey: @""]))
[s appendFormat: @"<BusinessCity xmlns=\"Contacts:\">%@</BusinessCity>", o];
if ((o = [workAdr flattenedValueAtIndex: 4 forKey: @""]))
[s appendFormat: @"<BusinessState xmlns=\"Contacts:\">%@</BusinessState>", o];
if ((o = [workAdr flattenedValueAtIndex: 5 forKey: @""]))
[s appendFormat: @"<BusinessPostalCode xmlns=\"Contacts:\">%@</BusinessPostalCode>", o];
if ((o = [workAdr flattenedValueAtIndex: 6 forKey: @""]))
[s appendFormat: @"<BusinessCountry xmlns=\"Contacts:\">%@</BusinessCountry>", o];
// Other, less important fields
if ((o = [self birthday]))
[s appendFormat: @"<Birthday xmlns=\"Contacts:\">%@</Birthday>", [o activeSyncRepresentation]];
if ((o = [self note]))
{
[s appendString: @"<Body xmlns=\"AirSyncBase:\">"];
[s appendFormat: @"<Type>%d</Type>", 1];
[s appendFormat: @"<EstimatedDataSize>%d</EstimatedDataSize>", [o length]];
[s appendFormat: @"<Truncated>%d</Truncated>", 0];
[s appendFormat: @"<Data>%@</Data>", o];
[s appendString: @"</Body>"];
}
return s;
}
+12 -3
View File
@@ -1,8 +1,6 @@
/* NGVCard+SOGo.h - this file is part of SOGo
*
* Copyright (C) 2009 Inverse inc.
*
* Author: Cyril Robert <crobert@inverse.ca>
* 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 */
+151
View File
@@ -27,6 +27,8 @@
#import <NGCards/NSArray+NGCards.h>
#import <NGCards/NSString+NGCards.h>
#import <SOGo/NSCalendarDate+SOGo.h>
#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 */
+28 -107
View File
@@ -33,6 +33,8 @@
#import <SOGo/NSCalendarDate+SOGo.h>
#import <SOGo/SOGoDateFormatter.h>
#import <SOGo/SOGoUser.h>
#import <Contacts/NGVCard+SOGo.h>
#import <Contacts/SOGoContactObject.h>
#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: @"<a href=\"mailto:%@\""
@" onclick=\"return openMailTo('%@ <%@>');\">"
@"%@</a>", 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: @"<a href=\"mailto:%@\""
@" onclick=\"return openMailTo('%@ <%@>');\">"
@"%@</a>", 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];
}