2006-08-09 23:11:49 +02:00
|
|
|
/*
|
2011-07-29 15:26:14 +02:00
|
|
|
Copyright (C) 2006-2011 Inverse inc.
|
2006-08-09 23:11:49 +02:00
|
|
|
Copyright (C) 2004-2005 SKYRIX Software AG
|
|
|
|
|
2011-07-29 15:26:14 +02:00
|
|
|
This file is part of SOGo.
|
2006-08-09 23:11:49 +02:00
|
|
|
|
2011-07-29 15:26:14 +02:00
|
|
|
SOGo is free software; you can redistribute it and/or modify it under
|
2006-08-09 23:11:49 +02:00
|
|
|
the terms of the GNU Lesser General Public License as published by the
|
|
|
|
Free Software Foundation; either version 2, or (at your option) any
|
|
|
|
later version.
|
|
|
|
|
2011-07-29 15:26:14 +02:00
|
|
|
SOGo is distributed in the hope that it will be useful, but WITHOUT ANY
|
2006-08-09 23:11:49 +02:00
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with OGo; see the file COPYING. If not, write to the
|
|
|
|
Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
|
|
|
|
02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2007-07-22 22:44:46 +02:00
|
|
|
#import <Foundation/NSArray.h>
|
2008-07-04 18:06:09 +02:00
|
|
|
#import <Foundation/NSEnumerator.h>
|
2007-07-22 22:44:46 +02:00
|
|
|
#import <Foundation/NSString.h>
|
2010-08-12 16:02:21 +02:00
|
|
|
#import <Foundation/NSURL.h>
|
2007-09-15 00:01:02 +02:00
|
|
|
|
2007-07-22 22:44:46 +02:00
|
|
|
#import <NGObjWeb/NSException+HTTP.h>
|
|
|
|
#import <NGObjWeb/SoObject+SoDAV.h>
|
|
|
|
#import <NGObjWeb/WOContext.h>
|
|
|
|
#import <NGObjWeb/WORequest.h>
|
2007-11-08 21:35:09 +01:00
|
|
|
#import <NGObjWeb/WOResponse.h>
|
2007-07-22 22:44:46 +02:00
|
|
|
#import <NGExtensions/NSObject+Logs.h>
|
2007-10-17 00:14:37 +02:00
|
|
|
#import <NGExtensions/NSString+misc.h>
|
2010-08-12 16:02:21 +02:00
|
|
|
#import <SaxObjC/XMLNamespaces.h>
|
2007-07-22 22:44:46 +02:00
|
|
|
#import <EOControl/EOQualifier.h>
|
|
|
|
#import <EOControl/EOSortOrdering.h>
|
2010-08-12 16:02:21 +02:00
|
|
|
|
|
|
|
#import <NGCards/CardGroup.h>
|
2006-08-09 23:11:49 +02:00
|
|
|
#import <GDLContentStore/GCSFolder.h>
|
|
|
|
|
2010-08-12 16:02:21 +02:00
|
|
|
#import <SOGo/DOMNode+SOGo.h>
|
2008-03-07 23:12:40 +01:00
|
|
|
#import <SOGo/SOGoCache.h>
|
|
|
|
#import <SOGo/NSArray+Utilities.h>
|
|
|
|
#import <SOGo/NSDictionary+Utilities.h>
|
|
|
|
#import <SOGo/NSString+Utilities.h>
|
2010-08-12 16:02:21 +02:00
|
|
|
#import <SOGo/NSObject+DAV.h>
|
2008-03-07 23:12:40 +01:00
|
|
|
|
2006-08-09 23:11:49 +02:00
|
|
|
#import "SOGoContactGCSEntry.h"
|
2008-03-07 23:12:40 +01:00
|
|
|
#import "SOGoContactGCSList.h"
|
|
|
|
|
2006-08-09 23:11:49 +02:00
|
|
|
#import "SOGoContactGCSFolder.h"
|
|
|
|
|
2010-05-05 20:49:29 +02:00
|
|
|
static NSArray *folderListingFields = nil;
|
2006-08-14 22:36:36 +02:00
|
|
|
|
2006-08-09 23:11:49 +02:00
|
|
|
@implementation SOGoContactGCSFolder
|
|
|
|
|
2010-05-05 20:49:29 +02:00
|
|
|
+ (void) initialize
|
|
|
|
{
|
|
|
|
if (!folderListingFields)
|
|
|
|
folderListingFields = [[NSArray alloc] initWithObjects: @"c_name",
|
|
|
|
@"c_cn", @"c_givenname", @"c_sn",
|
|
|
|
@"c_screenname", @"c_o",
|
|
|
|
@"c_mail", @"c_telephonenumber",
|
2010-11-01 20:32:19 +01:00
|
|
|
@"c_categories",
|
2010-05-05 20:49:29 +02:00
|
|
|
@"c_component", nil];
|
|
|
|
}
|
|
|
|
|
2008-03-07 23:12:40 +01:00
|
|
|
- (Class) objectClassForContent: (NSString *) content
|
2006-08-09 23:11:49 +02:00
|
|
|
{
|
2008-03-07 23:12:40 +01:00
|
|
|
CardGroup *cardEntry;
|
|
|
|
NSString *firstTag;
|
|
|
|
Class objectClass;
|
2006-08-11 00:03:48 +02:00
|
|
|
|
2008-03-07 23:12:40 +01:00
|
|
|
objectClass = Nil;
|
|
|
|
|
|
|
|
cardEntry = [CardGroup parseSingleFromSource: content];
|
|
|
|
if (cardEntry)
|
|
|
|
{
|
|
|
|
firstTag = [[cardEntry tag] uppercaseString];
|
|
|
|
if ([firstTag isEqualToString: @"VCARD"])
|
|
|
|
objectClass = [SOGoContactGCSEntry class];
|
|
|
|
else if ([firstTag isEqualToString: @"VLIST"])
|
|
|
|
objectClass = [SOGoContactGCSList class];
|
|
|
|
}
|
|
|
|
|
|
|
|
return objectClass;
|
|
|
|
}
|
|
|
|
|
2008-06-13 22:18:03 +02:00
|
|
|
- (Class) objectClassForComponentName: (NSString *) componentName
|
|
|
|
{
|
|
|
|
Class objectClass;
|
2010-08-12 16:02:21 +02:00
|
|
|
|
2008-06-13 22:18:03 +02:00
|
|
|
if ([componentName isEqualToString: @"vcard"])
|
|
|
|
objectClass = [SOGoContactGCSEntry class];
|
|
|
|
else if ([componentName isEqualToString: @"vlist"])
|
|
|
|
objectClass = [SOGoContactGCSList class];
|
|
|
|
else
|
|
|
|
objectClass = Nil;
|
|
|
|
|
|
|
|
return objectClass;
|
|
|
|
}
|
|
|
|
|
2008-03-07 23:12:40 +01:00
|
|
|
- (Class) objectClassForResourceNamed: (NSString *) name
|
|
|
|
{
|
|
|
|
EOQualifier *qualifier;
|
|
|
|
NSArray *records;
|
|
|
|
NSString *component;
|
|
|
|
Class objectClass;
|
|
|
|
|
|
|
|
qualifier = [EOQualifier qualifierWithQualifierFormat:@"c_name = %@", name];
|
|
|
|
records = [[self ocsFolder] fetchFields: [NSArray arrayWithObject: @"c_component"]
|
|
|
|
matchingQualifier: qualifier];
|
|
|
|
|
|
|
|
if ([records count])
|
2006-08-11 00:03:48 +02:00
|
|
|
{
|
2008-03-07 23:12:40 +01:00
|
|
|
component = [[records objectAtIndex: 0] valueForKey: @"c_component"];
|
2008-06-13 22:18:03 +02:00
|
|
|
objectClass = [self objectClassForComponentName: component];
|
2006-08-11 00:03:48 +02:00
|
|
|
}
|
2008-03-07 23:12:40 +01:00
|
|
|
else
|
|
|
|
objectClass = Nil;
|
|
|
|
|
|
|
|
return objectClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) requestNamedIsHandledLater: (NSString *) name
|
|
|
|
{
|
|
|
|
return [name isEqualToString: @"OPTIONS"];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) lookupName: (NSString *)_key
|
|
|
|
inContext: (id)_ctx
|
|
|
|
acquire: (BOOL)_flag
|
|
|
|
{
|
|
|
|
id obj;
|
|
|
|
NSString *url;
|
|
|
|
BOOL handledLater;
|
|
|
|
|
|
|
|
/* first check attributes directly bound to the application */
|
|
|
|
handledLater = [self requestNamedIsHandledLater: _key];
|
|
|
|
if (handledLater)
|
|
|
|
obj = nil;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
obj = [super lookupName:_key inContext:_ctx acquire:NO];
|
|
|
|
if (!obj)
|
|
|
|
{
|
|
|
|
if ([self isValidContentName: _key])
|
|
|
|
{
|
|
|
|
url = [[[_ctx request] uri] urlWithoutParameters];
|
|
|
|
if ([url hasSuffix: @"AsContact"])
|
|
|
|
obj = [SOGoContactGCSEntry objectWithName: _key
|
|
|
|
inContainer: self];
|
|
|
|
else if ([url hasSuffix: @"AsList"])
|
|
|
|
obj = [SOGoContactGCSList objectWithName: _key
|
|
|
|
inContainer: self];
|
2008-07-15 22:21:40 +02:00
|
|
|
[obj setIsNew: YES];
|
2008-03-07 23:12:40 +01:00
|
|
|
}
|
|
|
|
}
|
2011-11-21 15:15:14 +01:00
|
|
|
if (!obj || ([obj isKindOfClass: [SOGoContactGCSList class]] && [[_ctx request] isMacOSXAddressBookApp]))
|
2008-03-07 23:12:40 +01:00
|
|
|
obj = [NSException exceptionWithHTTPStatus:404 /* Not Found */];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (obj)
|
|
|
|
[[SOGoCache sharedCache] registerObject: obj
|
|
|
|
withName: _key
|
|
|
|
inContainer: container];
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2006-08-09 23:11:49 +02:00
|
|
|
- (EOQualifier *) _qualifierForFilter: (NSString *) filter
|
2010-10-26 22:19:37 +02:00
|
|
|
onCriteria: (NSString *) criteria
|
2006-08-09 23:11:49 +02:00
|
|
|
{
|
|
|
|
NSString *qs;
|
|
|
|
EOQualifier *qualifier;
|
|
|
|
|
2010-10-26 22:19:37 +02:00
|
|
|
if ([filter length] > 0)
|
2006-08-09 23:11:49 +02:00
|
|
|
{
|
2009-06-04 16:42:50 +02:00
|
|
|
filter = [[filter stringByReplacingString: @"\\" withString: @"\\\\"]
|
2010-10-26 22:19:37 +02:00
|
|
|
stringByReplacingString: @"'" withString: @"\\'\\'"];
|
|
|
|
if ([criteria isEqualToString: @"name_or_address"])
|
|
|
|
qs = [NSString stringWithFormat:
|
|
|
|
@"(c_sn isCaseInsensitiveLike: '%%%@%%') OR "
|
|
|
|
@"(c_givenname isCaseInsensitiveLike: '%%%@%%') OR "
|
|
|
|
@"(c_cn isCaseInsensitiveLike: '%%%@%%') OR "
|
|
|
|
@"(c_mail isCaseInsensitiveLike: '%%%@%%')",
|
|
|
|
filter, filter, filter, filter];
|
|
|
|
else if ([criteria isEqualToString: @"category"])
|
|
|
|
qs = [NSString stringWithFormat:
|
|
|
|
@"(c_categories isCaseInsensitiveLike: '%%%@%%')",
|
|
|
|
filter];
|
|
|
|
else
|
|
|
|
qs = @"(1 == 0)";
|
|
|
|
|
|
|
|
if (qs)
|
|
|
|
qualifier = [EOQualifier qualifierWithQualifierFormat: qs];
|
|
|
|
else
|
|
|
|
qualifier = nil;
|
2006-08-09 23:11:49 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
qualifier = nil;
|
|
|
|
|
|
|
|
return qualifier;
|
|
|
|
}
|
|
|
|
|
2009-02-06 21:09:56 +01:00
|
|
|
- (void) fixupContactRecord: (NSMutableDictionary *) contactRecord
|
|
|
|
{
|
|
|
|
NSString *data;
|
|
|
|
|
|
|
|
data = [contactRecord objectForKey: @"c_cn"];
|
|
|
|
if (![data length])
|
|
|
|
{
|
|
|
|
data = [contactRecord keysWithFormat: @"%{c_givenname} %{c_sn}"];
|
|
|
|
[contactRecord setObject: data forKey: @"c_cn"];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (![contactRecord objectForKey: @"c_mail"])
|
|
|
|
[contactRecord setObject: @"" forKey: @"c_mail"];
|
|
|
|
if (![contactRecord objectForKey: @"c_screenname"])
|
|
|
|
[contactRecord setObject: @"" forKey: @"c_screenname"];
|
|
|
|
if (![contactRecord objectForKey: @"c_o"])
|
|
|
|
[contactRecord setObject: @"" forKey: @"c_o"];
|
|
|
|
if (![contactRecord objectForKey: @"c_telephonenumber"])
|
|
|
|
[contactRecord setObject: @"" forKey: @"c_telephonenumber"];
|
|
|
|
}
|
|
|
|
|
2007-07-24 21:57:51 +02:00
|
|
|
- (NSArray *) _flattenedRecords: (NSArray *) records
|
|
|
|
{
|
|
|
|
NSMutableArray *newRecords;
|
|
|
|
NSEnumerator *oldRecords;
|
|
|
|
NSDictionary *oldRecord;
|
|
|
|
NSMutableDictionary *newRecord;
|
|
|
|
|
|
|
|
newRecords = [NSMutableArray arrayWithCapacity: [records count]];
|
|
|
|
|
|
|
|
oldRecords = [records objectEnumerator];
|
|
|
|
oldRecord = [oldRecords nextObject];
|
|
|
|
while (oldRecord)
|
|
|
|
{
|
2009-02-06 21:09:56 +01:00
|
|
|
newRecord = [NSMutableDictionary dictionaryWithDictionary: oldRecord];
|
|
|
|
[self fixupContactRecord: newRecord];
|
2007-07-24 21:57:51 +02:00
|
|
|
[newRecords addObject: newRecord];
|
|
|
|
oldRecord = [oldRecords nextObject];
|
|
|
|
}
|
|
|
|
|
|
|
|
return newRecords;
|
|
|
|
}
|
|
|
|
|
2010-04-23 17:53:54 +02:00
|
|
|
/* This method returns the quick entry corresponding to the name passed as
|
|
|
|
parameter. */
|
2009-10-28 18:48:22 +01:00
|
|
|
- (NSDictionary *) lookupContactWithName: (NSString *) aName
|
|
|
|
{
|
2010-04-23 17:53:54 +02:00
|
|
|
NSArray *dbRecords;
|
|
|
|
NSMutableDictionary *record;
|
2009-10-28 18:48:22 +01:00
|
|
|
EOQualifier *qualifier;
|
|
|
|
NSString *qs;
|
|
|
|
|
|
|
|
record = nil;
|
|
|
|
|
|
|
|
if (aName && [aName length] > 0)
|
|
|
|
{
|
|
|
|
aName = [[aName stringByReplacingString: @"\\" withString: @"\\\\"]
|
|
|
|
stringByReplacingString: @"'" withString: @"\\'\\'"];
|
2010-04-23 17:53:54 +02:00
|
|
|
qs = [NSString stringWithFormat: @"(c_name='%@')", aName];
|
2009-10-28 18:48:22 +01:00
|
|
|
qualifier = [EOQualifier qualifierWithQualifierFormat: qs];
|
2010-04-23 17:53:54 +02:00
|
|
|
dbRecords = [[self ocsFolder] fetchFields: folderListingFields
|
2009-10-28 18:48:22 +01:00
|
|
|
matchingQualifier: qualifier];
|
|
|
|
if ([dbRecords count] > 0)
|
2010-04-23 17:53:54 +02:00
|
|
|
{
|
|
|
|
record = [dbRecords objectAtIndex: 0];
|
|
|
|
[self fixupContactRecord: record];
|
|
|
|
}
|
2009-10-28 18:48:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return record;
|
|
|
|
}
|
|
|
|
|
2007-11-05 15:51:50 +01:00
|
|
|
- (NSArray *) lookupContactsWithFilter: (NSString *) filter
|
2010-10-26 22:19:37 +02:00
|
|
|
onCriteria: (NSString *) criteria
|
2007-11-05 15:51:50 +01:00
|
|
|
sortBy: (NSString *) sortKey
|
|
|
|
ordering: (NSComparisonResult) sortOrdering
|
2007-10-10 20:44:26 +02:00
|
|
|
{
|
2010-04-23 17:53:54 +02:00
|
|
|
NSArray *dbRecords, *records;
|
2007-11-05 15:51:50 +01:00
|
|
|
EOQualifier *qualifier;
|
|
|
|
EOSortOrdering *ordering;
|
2007-10-10 20:44:26 +02:00
|
|
|
|
2010-10-26 22:19:37 +02:00
|
|
|
qualifier = [self _qualifierForFilter: filter onCriteria: criteria];
|
2010-04-23 17:53:54 +02:00
|
|
|
dbRecords = [[self ocsFolder] fetchFields: folderListingFields
|
2007-11-05 15:51:50 +01:00
|
|
|
matchingQualifier: qualifier];
|
2007-10-10 20:44:26 +02:00
|
|
|
|
2007-11-05 15:51:50 +01:00
|
|
|
if ([dbRecords count] > 0)
|
2007-10-10 20:44:26 +02:00
|
|
|
{
|
2007-11-05 15:51:50 +01:00
|
|
|
records = [self _flattenedRecords: dbRecords];
|
|
|
|
ordering
|
|
|
|
= [EOSortOrdering sortOrderingWithKey: sortKey
|
|
|
|
selector: ((sortOrdering == NSOrderedDescending)
|
|
|
|
? EOCompareCaseInsensitiveDescending
|
|
|
|
: EOCompareCaseInsensitiveAscending)];
|
|
|
|
records
|
|
|
|
= [records sortedArrayUsingKeyOrderArray:
|
|
|
|
[NSArray arrayWithObject: ordering]];
|
2007-10-10 20:44:26 +02:00
|
|
|
}
|
|
|
|
else
|
2007-11-05 15:51:50 +01:00
|
|
|
records = nil;
|
2007-10-10 20:44:26 +02:00
|
|
|
|
2007-11-05 15:51:50 +01:00
|
|
|
[self debugWithFormat:@"fetched %i records.", [records count]];
|
|
|
|
return records;
|
2007-10-10 20:44:26 +02:00
|
|
|
}
|
|
|
|
|
2010-08-12 16:02:21 +02:00
|
|
|
- (NSDictionary *) davSQLFieldsTable
|
2007-10-10 20:44:26 +02:00
|
|
|
{
|
2010-08-12 16:02:21 +02:00
|
|
|
static NSMutableDictionary *davSQLFieldsTable = nil;
|
|
|
|
|
|
|
|
if (!davSQLFieldsTable)
|
|
|
|
{
|
|
|
|
davSQLFieldsTable = [[super davSQLFieldsTable] mutableCopy];
|
|
|
|
[davSQLFieldsTable setObject: @"c_content" forKey: @"{urn:ietf:params:xml:ns:carddav}address-data"];
|
|
|
|
}
|
|
|
|
|
|
|
|
return davSQLFieldsTable;
|
2007-10-10 20:44:26 +02:00
|
|
|
}
|
|
|
|
|
2006-12-14 22:20:13 +01:00
|
|
|
- (NSString *) groupDavResourceType
|
|
|
|
{
|
|
|
|
return @"vcard-collection";
|
|
|
|
}
|
|
|
|
|
2007-12-06 23:48:41 +01:00
|
|
|
- (NSArray *) davResourceType
|
|
|
|
{
|
|
|
|
NSMutableArray *resourceType;
|
|
|
|
NSArray *cardDavCollection;
|
|
|
|
|
|
|
|
cardDavCollection
|
|
|
|
= [NSArray arrayWithObjects: @"addressbook",
|
|
|
|
@"urn:ietf:params:xml:ns:carddav", nil];
|
|
|
|
resourceType = [NSMutableArray arrayWithArray: [super davResourceType]];
|
|
|
|
[resourceType addObject: cardDavCollection];
|
|
|
|
|
|
|
|
return resourceType;
|
|
|
|
}
|
|
|
|
|
2010-08-12 16:02:21 +02:00
|
|
|
- (id) davAddressbookMultiget: (id) queryContext
|
|
|
|
{
|
|
|
|
return [self performMultigetInContext: queryContext
|
|
|
|
inNamespace: @"urn:ietf:params:xml:ns:carddav"];
|
|
|
|
}
|
|
|
|
|
2007-09-17 05:42:16 +02:00
|
|
|
/* sorting */
|
|
|
|
- (NSComparisonResult) compare: (id) otherFolder
|
|
|
|
{
|
|
|
|
NSComparisonResult comparison;
|
|
|
|
|
|
|
|
if ([NSStringFromClass([otherFolder class])
|
2009-09-25 16:42:33 +02:00
|
|
|
isEqualToString: @"SOGoContactSourceFolder"])
|
2007-09-17 05:42:16 +02:00
|
|
|
comparison = NSOrderedAscending;
|
|
|
|
else
|
|
|
|
comparison = [super compare: otherFolder];
|
|
|
|
|
|
|
|
return comparison;
|
|
|
|
}
|
|
|
|
|
2006-08-09 23:11:49 +02:00
|
|
|
/* folder type */
|
|
|
|
|
2007-02-09 22:21:35 +01:00
|
|
|
- (NSString *) folderType
|
|
|
|
{
|
|
|
|
return @"Contact";
|
|
|
|
}
|
|
|
|
|
2007-06-01 06:16:32 +02:00
|
|
|
- (NSString *) outlookFolderClass
|
|
|
|
{
|
2006-08-09 23:11:49 +02:00
|
|
|
return @"IPF.Contact";
|
|
|
|
}
|
|
|
|
|
2010-08-12 16:02:21 +02:00
|
|
|
/* TODO: multiget reorg */
|
|
|
|
- (NSString *) _nodeTagForProperty: (NSString *) property
|
|
|
|
{
|
|
|
|
NSString *namespace, *nodeName, *nsRep;
|
|
|
|
NSRange nsEnd;
|
|
|
|
|
|
|
|
nsEnd = [property rangeOfString: @"}"];
|
|
|
|
namespace
|
|
|
|
= [property substringFromRange: NSMakeRange (1, nsEnd.location - 1)];
|
|
|
|
nodeName = [property substringFromIndex: nsEnd.location + 1];
|
|
|
|
if ([namespace isEqualToString: XMLNS_CARDDAV])
|
|
|
|
nsRep = @"C";
|
|
|
|
else
|
|
|
|
nsRep = @"D";
|
|
|
|
|
|
|
|
return [NSString stringWithFormat: @"%@:%@", nsRep, nodeName];
|
|
|
|
}
|
|
|
|
|
2006-08-09 23:11:49 +02:00
|
|
|
@end /* SOGoContactGCSFolder */
|