2014-01-10 20:12:53 +01:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2014, Inverse inc.
|
|
|
|
All rights reserved.
|
|
|
|
|
2014-01-13 17:46:32 +01:00
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the Inverse inc. nor the
|
|
|
|
names of its contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
|
|
|
|
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2014-01-10 20:12:53 +01:00
|
|
|
|
|
|
|
*/
|
|
|
|
#import "NGVCard+ActiveSync.h"
|
|
|
|
|
|
|
|
#import <Foundation/NSArray.h>
|
2014-02-07 01:35:23 +01:00
|
|
|
#import <Foundation/NSCalendarDate.h>
|
2014-01-10 20:12:53 +01:00
|
|
|
#import <Foundation/NSDictionary.h>
|
|
|
|
#import <Foundation/NSString.h>
|
|
|
|
|
2014-01-29 16:30:31 +01:00
|
|
|
#import <NGExtensions/NSString+misc.h>
|
|
|
|
|
2014-01-10 20:12:53 +01:00
|
|
|
#import <NGCards/CardElement.h>
|
|
|
|
|
2014-01-22 17:27:27 +01:00
|
|
|
#import <Contacts/NGVCard+SOGo.h>
|
|
|
|
|
2014-02-06 23:59:33 +01:00
|
|
|
#include "NSDate+ActiveSync.h"
|
2014-02-07 01:35:23 +01:00
|
|
|
#include "NSString+ActiveSync.h"
|
2014-02-06 23:59:33 +01:00
|
|
|
|
2014-01-10 20:12:53 +01:00
|
|
|
@implementation NGVCard (ActiveSync)
|
|
|
|
|
|
|
|
- (NSString *) activeSyncRepresentation
|
|
|
|
{
|
2014-01-22 17:27:27 +01:00
|
|
|
CardElement *n, *homeAdr, *workAdr;
|
2014-01-27 17:30:43 +01:00
|
|
|
NSArray *emails, *addresses;
|
2014-01-10 20:12:53 +01:00
|
|
|
NSMutableString *s;
|
2014-01-22 17:27:27 +01:00
|
|
|
id o;
|
|
|
|
|
|
|
|
int i;
|
2014-01-10 20:12:53 +01:00
|
|
|
|
|
|
|
s = [NSMutableString string];
|
|
|
|
n = [self n];
|
|
|
|
|
2014-01-22 17:27:27 +01:00
|
|
|
if ((o = [n flattenedValueAtIndex: 0 forKey: @""]))
|
2014-02-04 21:03:02 +01:00
|
|
|
[s appendFormat: @"<LastName xmlns=\"Contacts:\">%@</LastName>", [o activeSyncRepresentation]];
|
2014-01-22 17:27:27 +01:00
|
|
|
|
|
|
|
if ((o = [n flattenedValueAtIndex: 1 forKey: @""]))
|
2014-02-04 21:03:02 +01:00
|
|
|
[s appendFormat: @"<FirstName xmlns=\"Contacts:\">%@</FirstName>", [o activeSyncRepresentation]];
|
2014-01-22 17:27:27 +01:00
|
|
|
|
|
|
|
if ((o = [self workCompany]))
|
2014-02-04 21:03:02 +01:00
|
|
|
[s appendFormat: @"<CompanyName xmlns=\"Contacts:\">%@</CompanyName>", [o activeSyncRepresentation]];
|
2014-01-22 17:27:27 +01:00
|
|
|
|
|
|
|
if ((o = [self title]))
|
2014-02-04 21:03:02 +01:00
|
|
|
[s appendFormat: @"<JobTitle xmlns=\"Contacts:\">%@</JobTitle>", [o activeSyncRepresentation]];
|
2014-01-22 17:27:27 +01:00
|
|
|
|
2014-01-29 16:30:31 +01:00
|
|
|
if ((o = [self preferredEMail]))
|
|
|
|
[s appendFormat: @"<Email1Address xmlns=\"Contacts:\">%@</Email1Address>", o];
|
|
|
|
|
2014-01-22 17:27:27 +01:00
|
|
|
|
2014-02-06 23:57:01 +01:00
|
|
|
// Secondary email addresses (2 and 3)
|
2014-01-22 17:27:27 +01:00
|
|
|
emails = [self secondaryEmails];
|
|
|
|
|
|
|
|
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
|
2014-01-29 16:30:31 +01:00
|
|
|
if ((o = [self workPhone]) && [o length])
|
2014-02-04 21:03:02 +01:00
|
|
|
[s appendFormat: @"<BusinessPhoneNumber xmlns=\"Contacts:\">%@</BusinessPhoneNumber>", [o activeSyncRepresentation]];
|
2014-01-22 17:27:27 +01:00
|
|
|
|
2014-01-29 16:30:31 +01:00
|
|
|
if ((o = [self homePhone]) && [o length])
|
2014-02-04 21:03:02 +01:00
|
|
|
[s appendFormat: @"<HomePhoneNumber xmlns=\"Contacts:\">%@</HomePhoneNumber>", [o activeSyncRepresentation]];
|
2014-01-22 17:27:27 +01:00
|
|
|
|
2014-01-29 16:30:31 +01:00
|
|
|
if ((o = [self fax]) && [o length])
|
2014-02-04 21:03:02 +01:00
|
|
|
[s appendFormat: @"<BusinessFaxNumber xmlns=\"Contacts:\">%@</BusinessFaxNumber>", [o activeSyncRepresentation]];
|
2014-01-10 20:12:53 +01:00
|
|
|
|
2014-01-29 16:30:31 +01:00
|
|
|
if ((o = [self mobile]) && [o length])
|
2014-02-04 21:03:02 +01:00
|
|
|
[s appendFormat: @"<MobilePhoneNumber xmlns=\"Contacts:\">%@</MobilePhoneNumber>", [o activeSyncRepresentation]];
|
2014-01-10 20:12:53 +01:00
|
|
|
|
2014-01-29 16:30:31 +01:00
|
|
|
if ((o = [self pager]) && [o length])
|
2014-02-04 21:03:02 +01:00
|
|
|
[s appendFormat: @"<PagerNumber xmlns=\"Contacts:\">%@</PagerNumber>", [o activeSyncRepresentation]];
|
2014-01-10 20:12:53 +01:00
|
|
|
|
2014-01-22 17:27:27 +01:00
|
|
|
// Home Address
|
2014-01-27 17:30:43 +01:00
|
|
|
addresses = [self childrenWithTag: @"adr"
|
|
|
|
andAttribute: @"type"
|
|
|
|
havingValue: @"home"];
|
2014-01-22 17:27:27 +01:00
|
|
|
|
2014-01-27 17:30:43 +01:00
|
|
|
if ([addresses count])
|
|
|
|
{
|
|
|
|
homeAdr = [addresses objectAtIndex: 0];
|
|
|
|
|
|
|
|
if ((o = [homeAdr flattenedValueAtIndex: 2 forKey: @""]))
|
2014-02-04 21:03:02 +01:00
|
|
|
[s appendFormat: @"<HomeStreet xmlns=\"Contacts:\">%@</HomeStreet>", [o activeSyncRepresentation]];
|
2014-01-27 17:30:43 +01:00
|
|
|
|
|
|
|
if ((o = [homeAdr flattenedValueAtIndex: 3 forKey: @""]))
|
2014-02-04 21:03:02 +01:00
|
|
|
[s appendFormat: @"<HomeCity xmlns=\"Contacts:\">%@</HomeCity>", [o activeSyncRepresentation]];
|
2014-01-27 17:30:43 +01:00
|
|
|
|
|
|
|
if ((o = [homeAdr flattenedValueAtIndex: 4 forKey: @""]))
|
2014-02-04 21:03:02 +01:00
|
|
|
[s appendFormat: @"<HomeState xmlns=\"Contacts:\">%@</HomeState>", [o activeSyncRepresentation]];
|
2014-01-27 17:30:43 +01:00
|
|
|
|
|
|
|
if ((o = [homeAdr flattenedValueAtIndex: 5 forKey: @""]))
|
2014-02-04 21:03:02 +01:00
|
|
|
[s appendFormat: @"<HomePostalCode xmlns=\"Contacts:\">%@</HomePostalCode>", [o activeSyncRepresentation]];
|
2014-01-27 17:30:43 +01:00
|
|
|
|
|
|
|
if ((o = [homeAdr flattenedValueAtIndex: 6 forKey: @""]))
|
2014-02-04 21:03:02 +01:00
|
|
|
[s appendFormat: @"<HomeCountry xmlns=\"Contacts:\">%@</HomeCountry>", [o activeSyncRepresentation]];
|
2014-01-27 17:30:43 +01:00
|
|
|
}
|
2014-01-22 17:27:27 +01:00
|
|
|
|
|
|
|
// Work Address
|
2014-01-27 17:30:43 +01:00
|
|
|
addresses = [self childrenWithTag: @"adr"
|
|
|
|
andAttribute: @"type"
|
|
|
|
havingValue: @"work"];
|
2014-01-22 17:27:27 +01:00
|
|
|
|
2014-01-27 17:30:43 +01:00
|
|
|
if ([addresses count])
|
|
|
|
{
|
|
|
|
workAdr = [addresses objectAtIndex: 0];
|
|
|
|
|
|
|
|
if ((o = [workAdr flattenedValueAtIndex: 2 forKey: @""]))
|
2014-02-04 21:03:02 +01:00
|
|
|
[s appendFormat: @"<BusinessStreet xmlns=\"Contacts:\">%@</BusinessStreet>", [o activeSyncRepresentation]];
|
2014-01-27 17:30:43 +01:00
|
|
|
|
|
|
|
if ((o = [workAdr flattenedValueAtIndex: 3 forKey: @""]))
|
2014-02-04 21:03:02 +01:00
|
|
|
[s appendFormat: @"<BusinessCity xmlns=\"Contacts:\">%@</BusinessCity>", [o activeSyncRepresentation]];
|
2014-01-27 17:30:43 +01:00
|
|
|
|
|
|
|
if ((o = [workAdr flattenedValueAtIndex: 4 forKey: @""]))
|
2014-02-04 21:03:02 +01:00
|
|
|
[s appendFormat: @"<BusinessState xmlns=\"Contacts:\">%@</BusinessState>", [o activeSyncRepresentation]];
|
2014-01-27 17:30:43 +01:00
|
|
|
|
|
|
|
if ((o = [workAdr flattenedValueAtIndex: 5 forKey: @""]))
|
2014-02-04 21:03:02 +01:00
|
|
|
[s appendFormat: @"<BusinessPostalCode xmlns=\"Contacts:\">%@</BusinessPostalCode>", [o activeSyncRepresentation]];
|
2014-01-27 17:30:43 +01:00
|
|
|
|
|
|
|
if ((o = [workAdr flattenedValueAtIndex: 6 forKey: @""]))
|
2014-02-04 21:03:02 +01:00
|
|
|
[s appendFormat: @"<BusinessCountry xmlns=\"Contacts:\">%@</BusinessCountry>", [o activeSyncRepresentation]];
|
2014-01-27 17:30:43 +01:00
|
|
|
}
|
2014-01-22 17:27:27 +01:00
|
|
|
|
|
|
|
// Other, less important fields
|
|
|
|
if ((o = [self birthday]))
|
2014-02-06 23:57:01 +01:00
|
|
|
[s appendFormat: @"<Birthday xmlns=\"Contacts:\">%@</Birthday>", [o activeSyncRepresentationWithoutSeparators]];
|
2014-01-22 17:27:27 +01:00
|
|
|
|
|
|
|
if ((o = [self note]))
|
|
|
|
{
|
2014-02-04 21:03:02 +01:00
|
|
|
o = [o activeSyncRepresentation];
|
2014-01-22 17:27:27 +01:00
|
|
|
[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>"];
|
|
|
|
}
|
|
|
|
|
2014-01-10 20:12:53 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2014-01-29 16:30:31 +01:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2014-01-10 20:12:53 +01:00
|
|
|
- (void) takeActiveSyncValues: (NSDictionary *) theValues
|
|
|
|
{
|
2014-02-06 02:56:01 +01:00
|
|
|
CardElement *element;
|
2014-01-10 20:12:53 +01:00
|
|
|
id o;
|
2014-02-06 02:56:01 +01:00
|
|
|
|
|
|
|
// Contact's note
|
|
|
|
if ((o = [[theValues objectForKey: @"Body"] objectForKey: @"Data"]))
|
|
|
|
[self setNote: o];
|
|
|
|
|
2014-02-07 01:35:23 +01:00
|
|
|
// Birthday
|
|
|
|
if ((o = [theValues objectForKey: @"Birthday"]))
|
|
|
|
{
|
|
|
|
o = [o calendarDate];
|
|
|
|
[self setBday: [o descriptionWithCalendarFormat: @"%Y-%m-%d"]];
|
|
|
|
}
|
|
|
|
|
2014-02-06 02:56:01 +01:00
|
|
|
//
|
|
|
|
// Business address information
|
|
|
|
//
|
|
|
|
// BusinessStreet
|
|
|
|
// BusinessCity
|
|
|
|
// BusinessPostalCode
|
|
|
|
// BusinessState
|
|
|
|
// BusinessCountry
|
|
|
|
//
|
|
|
|
element = [self elementWithTag: @"adr" ofType: @"work"];
|
|
|
|
[element setSingleValue: @""
|
|
|
|
atIndex: 1 forKey: @""];
|
|
|
|
[element setSingleValue: [theValues objectForKey: @"BusinessStreet"]
|
|
|
|
atIndex: 2 forKey: @""];
|
|
|
|
[element setSingleValue: [theValues objectForKey: @"BusinessCity"]
|
|
|
|
atIndex: 3 forKey: @""];
|
|
|
|
[element setSingleValue: [theValues objectForKey: @"BusinessState"]
|
|
|
|
atIndex: 4 forKey: @""];
|
|
|
|
[element setSingleValue: [theValues objectForKey: @"BusinessPostalCode"]
|
|
|
|
atIndex: 5 forKey: @""];
|
|
|
|
[element setSingleValue: [theValues objectForKey: @"BusinessCountry"]
|
|
|
|
atIndex: 6 forKey: @""];
|
|
|
|
|
|
|
|
//
|
|
|
|
// Home address information
|
|
|
|
//
|
|
|
|
// HomeStreet
|
|
|
|
// HomeCity
|
|
|
|
// HomePostalCode
|
|
|
|
// HomeState
|
|
|
|
// HomeCountry
|
|
|
|
//
|
|
|
|
element = [self elementWithTag: @"adr" ofType: @"home"];
|
|
|
|
[element setSingleValue: @""
|
|
|
|
atIndex: 1 forKey: @""];
|
|
|
|
[element setSingleValue: [theValues objectForKey: @"HomeStreet"]
|
|
|
|
atIndex: 2 forKey: @""];
|
|
|
|
[element setSingleValue: [theValues objectForKey: @"HomeCity"]
|
|
|
|
atIndex: 3 forKey: @""];
|
|
|
|
[element setSingleValue: [theValues objectForKey: @"HomeState"]
|
|
|
|
atIndex: 4 forKey: @""];
|
|
|
|
[element setSingleValue: [theValues objectForKey: @"HomePostalCode"]
|
|
|
|
atIndex: 5 forKey: @""];
|
|
|
|
[element setSingleValue: [theValues objectForKey: @"HomeCountry"]
|
|
|
|
atIndex: 6 forKey: @""];
|
|
|
|
|
|
|
|
// Company's name
|
|
|
|
if ((o = [theValues objectForKey: @"CompanyName"]))
|
|
|
|
{
|
|
|
|
[self setOrg: o units: nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Email addresses
|
|
|
|
if ((o = [theValues objectForKey: @"Email1Address"]))
|
|
|
|
{
|
|
|
|
[self addEmail: o types: [NSArray arrayWithObject: @"pref"]];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((o = [theValues objectForKey: @"Email2Address"]))
|
|
|
|
{
|
|
|
|
[self addEmail: o types: nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((o = [theValues objectForKey: @"Email3Address"]))
|
|
|
|
{
|
|
|
|
[self addEmail: o types: nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Formatted name
|
|
|
|
// MiddleName
|
|
|
|
// Suffix (II)
|
|
|
|
// Title (Mr.)
|
|
|
|
[self setFn: [theValues objectForKey: @"FileAs"]];
|
|
|
|
|
|
|
|
[self setNWithFamily: [theValues objectForKey: @"LastName"]
|
|
|
|
given: [theValues objectForKey: @"FirstName"]
|
|
|
|
additional: nil prefixes: nil suffixes: nil];
|
|
|
|
|
|
|
|
// IM information
|
|
|
|
[[self uniqueChildWithTag: @"x-aim"]
|
|
|
|
setSingleValue: [theValues objectForKey: @"IMAddress"]
|
|
|
|
forKey: @""];
|
|
|
|
|
|
|
|
//
|
|
|
|
// Phone numbrrs
|
|
|
|
//
|
|
|
|
element = [self elementWithTag: @"tel" ofType: @"work"];
|
|
|
|
[element setSingleValue: [theValues objectForKey: @"BusinessPhoneNumber"] forKey: @""];
|
|
|
|
|
|
|
|
element = [self elementWithTag: @"tel" ofType: @"home"];
|
|
|
|
[element setSingleValue: [theValues objectForKey: @"HomePhoneNumber"] forKey: @""];
|
|
|
|
|
|
|
|
element = [self elementWithTag: @"tel" ofType: @"cell"];
|
|
|
|
[element setSingleValue: [theValues objectForKey: @"MobilePhoneNumber"] forKey: @""];
|
2014-01-10 20:12:53 +01:00
|
|
|
|
2014-02-06 02:56:01 +01:00
|
|
|
element = [self elementWithTag: @"tel" ofType: @"fax"];
|
|
|
|
[element setSingleValue: [theValues objectForKey: @"BusinessFaxNumber"] forKey: @""];
|
|
|
|
|
|
|
|
element = [self elementWithTag: @"tel" ofType: @"pager"];
|
|
|
|
[element setSingleValue: [theValues objectForKey: @"PagerNumber"] forKey: @""];
|
|
|
|
|
|
|
|
// Job's title
|
|
|
|
if ((o = [theValues objectForKey: @"JobTitle"]))
|
|
|
|
{
|
|
|
|
[self setTitle: o];
|
|
|
|
}
|
|
|
|
|
|
|
|
// WebPage (work)
|
|
|
|
if ((o = [theValues objectForKey: @"WebPage"]))
|
|
|
|
{
|
|
|
|
[[self elementWithTag: @"url" ofType: @"work"]
|
|
|
|
setSingleValue: o forKey: @""];
|
|
|
|
}
|
2014-01-10 20:12:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|