2006-08-09 23:11:49 +02:00
|
|
|
/*
|
|
|
|
Copyright (C) 2004-2005 SKYRIX Software AG
|
|
|
|
|
|
|
|
This file is part of OpenGroupware.org.
|
|
|
|
|
|
|
|
OGo is free software; you can redistribute it and/or modify it under
|
|
|
|
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.
|
|
|
|
|
|
|
|
OGo is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
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>
|
|
|
|
#import <Foundation/NSString.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>
|
2007-07-22 22:44:46 +02:00
|
|
|
#import <EOControl/EOQualifier.h>
|
|
|
|
#import <EOControl/EOSortOrdering.h>
|
2006-08-09 23:11:49 +02:00
|
|
|
#import <GDLContentStore/GCSFolder.h>
|
|
|
|
|
2007-08-20 23:50:47 +02:00
|
|
|
#import <SoObjects/SOGo/NSDictionary+Utilities.h>
|
2006-08-09 23:11:49 +02:00
|
|
|
#import "SOGoContactGCSEntry.h"
|
|
|
|
#import "SOGoContactGCSFolder.h"
|
|
|
|
|
2007-07-24 20:45:52 +02:00
|
|
|
#define folderListingFields [NSArray arrayWithObjects: @"c_name", @"c_cn", \
|
2007-08-20 23:50:47 +02:00
|
|
|
@"c_givenname", @"c_sn", @"c_screenname", \
|
2007-07-24 20:45:52 +02:00
|
|
|
@"c_o", @"c_mail", @"c_telephonenumber", \
|
2006-08-14 22:36:36 +02:00
|
|
|
nil]
|
|
|
|
|
2006-08-09 23:11:49 +02:00
|
|
|
@implementation SOGoContactGCSFolder
|
|
|
|
|
|
|
|
/* name lookup */
|
|
|
|
|
|
|
|
- (id <SOGoContactObject>) lookupContactWithId: (NSString *) recordId
|
|
|
|
{
|
|
|
|
SOGoContactGCSEntry *contact;
|
|
|
|
|
|
|
|
if ([recordId length] > 0)
|
2006-08-21 19:43:36 +02:00
|
|
|
contact = [SOGoContactGCSEntry objectWithName: recordId
|
|
|
|
inContainer: self];
|
2006-08-09 23:11:49 +02:00
|
|
|
else
|
|
|
|
contact = nil;
|
|
|
|
|
|
|
|
return contact;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) lookupName: (NSString *) _key
|
2006-08-11 00:03:48 +02:00
|
|
|
inContext: (WOContext *) _ctx
|
2006-08-09 23:11:49 +02:00
|
|
|
acquire: (BOOL) _flag
|
|
|
|
{
|
|
|
|
id obj;
|
2007-06-05 00:38:43 +02:00
|
|
|
BOOL isPut;
|
2006-08-11 00:03:48 +02:00
|
|
|
|
2007-06-05 00:38:43 +02:00
|
|
|
isPut = NO;
|
2006-08-09 23:11:49 +02:00
|
|
|
obj = [super lookupName:_key inContext:_ctx acquire:NO];
|
2007-11-05 15:51:50 +01:00
|
|
|
|
2006-08-09 23:11:49 +02:00
|
|
|
if (!obj)
|
2006-08-11 00:03:48 +02:00
|
|
|
{
|
|
|
|
if ([[[_ctx request] method] isEqualToString: @"PUT"])
|
2007-06-05 00:38:43 +02:00
|
|
|
{
|
|
|
|
if ([_key isEqualToString: @"PUT"])
|
|
|
|
isPut = YES;
|
|
|
|
else
|
|
|
|
obj = [SOGoContactGCSEntry objectWithName: _key
|
|
|
|
inContainer: self];
|
|
|
|
}
|
2006-08-11 00:03:48 +02:00
|
|
|
else
|
|
|
|
obj = [self lookupContactWithId: _key];
|
|
|
|
}
|
2007-06-05 00:38:43 +02:00
|
|
|
// if (!(obj || isPut))
|
|
|
|
// obj = [NSException exceptionWithHTTPStatus:404 /* Not Found */];
|
2006-08-09 23:11:49 +02:00
|
|
|
|
|
|
|
// #if 0
|
|
|
|
// if ([[self ocsFolder] versionOfContentWithName:_key])
|
|
|
|
// #endif
|
|
|
|
// return [self contactWithName:_key inContext:_ctx];
|
|
|
|
// }
|
|
|
|
|
|
|
|
/* return 404 to stop acquisition */
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* fetching */
|
|
|
|
- (EOQualifier *) _qualifierForFilter: (NSString *) filter
|
|
|
|
{
|
|
|
|
NSString *qs;
|
|
|
|
EOQualifier *qualifier;
|
|
|
|
|
|
|
|
if (filter && [filter length] > 0)
|
|
|
|
{
|
|
|
|
qs = [NSString stringWithFormat:
|
2007-07-24 20:45:52 +02:00
|
|
|
@"(c_sn isCaseInsensitiveLike: '%@%%') OR "
|
|
|
|
@"(c_givenname isCaseInsensitiveLike: '%@%%') OR "
|
2007-11-23 22:16:04 +01:00
|
|
|
@"(c_mail isCaseInsensitiveLike: '%%%@%%') OR "
|
2007-07-24 20:45:52 +02:00
|
|
|
@"(c_telephonenumber isCaseInsensitiveLike: '%%%@%%')",
|
2006-08-09 23:11:49 +02:00
|
|
|
filter, filter, filter, filter];
|
|
|
|
qualifier = [EOQualifier qualifierWithQualifierFormat: qs];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
qualifier = nil;
|
|
|
|
|
|
|
|
return qualifier;
|
|
|
|
}
|
|
|
|
|
2007-07-24 21:57:51 +02:00
|
|
|
- (NSArray *) _flattenedRecords: (NSArray *) records
|
|
|
|
{
|
|
|
|
NSMutableArray *newRecords;
|
|
|
|
NSEnumerator *oldRecords;
|
|
|
|
NSDictionary *oldRecord;
|
|
|
|
NSMutableDictionary *newRecord;
|
|
|
|
NSString *data;
|
|
|
|
|
|
|
|
newRecords = [NSMutableArray arrayWithCapacity: [records count]];
|
|
|
|
|
|
|
|
oldRecords = [records objectEnumerator];
|
|
|
|
oldRecord = [oldRecords nextObject];
|
|
|
|
while (oldRecord)
|
|
|
|
{
|
|
|
|
newRecord = [NSMutableDictionary new];
|
|
|
|
[newRecord autorelease];
|
|
|
|
|
|
|
|
[newRecord setObject: [oldRecord objectForKey: @"c_name"]
|
|
|
|
forKey: @"c_uid"];
|
|
|
|
[newRecord setObject: [oldRecord objectForKey: @"c_name"]
|
|
|
|
forKey: @"c_name"];
|
|
|
|
|
|
|
|
data = [oldRecord objectForKey: @"c_cn"];
|
2007-08-20 23:50:47 +02:00
|
|
|
if (![data length])
|
|
|
|
data = [oldRecord keysWithFormat: @"%{c_givenname} %{c_sn}"];
|
2007-07-24 21:57:51 +02:00
|
|
|
[newRecord setObject: data
|
|
|
|
forKey: @"displayName"];
|
|
|
|
|
|
|
|
data = [oldRecord objectForKey: @"c_mail"];
|
|
|
|
if (!data)
|
|
|
|
data = @"";
|
|
|
|
[newRecord setObject: data forKey: @"mail"];
|
|
|
|
|
|
|
|
data = [oldRecord objectForKey: @"c_screenname"];
|
|
|
|
if (!data)
|
|
|
|
data = @"";
|
|
|
|
[newRecord setObject: data forKey: @"screenName"];
|
|
|
|
|
|
|
|
data = [oldRecord objectForKey: @"c_o"];
|
|
|
|
if (!data)
|
|
|
|
data = @"";
|
|
|
|
[newRecord setObject: data forKey: @"org"];
|
|
|
|
|
|
|
|
data = [oldRecord objectForKey: @"c_telephonenumber"];
|
|
|
|
if (![data length])
|
|
|
|
data = @"";
|
|
|
|
[newRecord setObject: data forKey: @"phone"];
|
|
|
|
|
|
|
|
[newRecords addObject: newRecord];
|
|
|
|
oldRecord = [oldRecords nextObject];
|
|
|
|
}
|
|
|
|
|
|
|
|
return newRecords;
|
|
|
|
}
|
|
|
|
|
2007-11-05 15:51:50 +01:00
|
|
|
- (NSArray *) lookupContactsWithFilter: (NSString *) filter
|
|
|
|
sortBy: (NSString *) sortKey
|
|
|
|
ordering: (NSComparisonResult) sortOrdering
|
2007-10-10 20:44:26 +02:00
|
|
|
{
|
2007-11-05 15:51:50 +01:00
|
|
|
NSArray *fields, *dbRecords, *records;
|
|
|
|
EOQualifier *qualifier;
|
|
|
|
EOSortOrdering *ordering;
|
2007-10-10 20:44:26 +02:00
|
|
|
|
2007-11-05 15:51:50 +01:00
|
|
|
fields = folderListingFields;
|
|
|
|
qualifier = [self _qualifierForFilter: filter];
|
|
|
|
dbRecords = [[self ocsFolder] fetchFields: fields
|
|
|
|
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;
|
|
|
|
// else
|
|
|
|
//[self errorWithFormat:@"(%s): fetch failed!", __PRETTY_FUNCTION__];
|
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
|
|
|
}
|
|
|
|
|
2007-11-08 00:34:25 +01:00
|
|
|
#warning this should be unified within SOGoFolder
|
2007-10-10 20:44:26 +02:00
|
|
|
- (void) appendObject: (NSDictionary *) object
|
|
|
|
withBaseURL: (NSString *) baseURL
|
|
|
|
toREPORTResponse: (WOResponse *) r
|
|
|
|
{
|
|
|
|
SOGoContactGCSEntry *component;
|
|
|
|
NSString *name, *etagLine, *contactString;
|
|
|
|
|
|
|
|
name = [object objectForKey: @"c_name"];
|
2007-11-08 00:34:25 +01:00
|
|
|
component = [self lookupName: name inContext: context acquire: NO];
|
2007-10-10 20:44:26 +02:00
|
|
|
|
|
|
|
[r appendContentString: @" <D:response>\r\n"];
|
|
|
|
[r appendContentString: @" <D:href>"];
|
|
|
|
[r appendContentString: baseURL];
|
|
|
|
if (![baseURL hasSuffix: @"/"])
|
|
|
|
[r appendContentString: @"/"];
|
|
|
|
[r appendContentString: name];
|
|
|
|
[r appendContentString: @"</D:href>\r\n"];
|
|
|
|
|
|
|
|
[r appendContentString: @" <D:propstat>\r\n"];
|
|
|
|
[r appendContentString: @" <D:prop>\r\n"];
|
|
|
|
etagLine = [NSString stringWithFormat: @" <D:getetag>%@</D:getetag>\r\n",
|
|
|
|
[component davEntityTag]];
|
|
|
|
[r appendContentString: etagLine];
|
|
|
|
[r appendContentString: @" </D:prop>\r\n"];
|
|
|
|
[r appendContentString: @" <D:status>HTTP/1.1 200 OK</D:status>\r\n"];
|
|
|
|
[r appendContentString: @" </D:propstat>\r\n"];
|
|
|
|
[r appendContentString: @" <C:addressbook-data>"];
|
|
|
|
contactString = [[component contentAsString] stringByEscapingXMLString];
|
|
|
|
[r appendContentString: contactString];
|
|
|
|
[r appendContentString: @"</C:addressbook-data>\r\n"];
|
|
|
|
[r appendContentString: @" </D:response>\r\n"];
|
|
|
|
}
|
|
|
|
|
2007-07-22 22:44:46 +02:00
|
|
|
- (NSArray *) davComplianceClassesInContext: (id)_ctx
|
|
|
|
{
|
|
|
|
NSMutableArray *classes;
|
|
|
|
NSArray *primaryClasses;
|
|
|
|
|
|
|
|
classes = [NSMutableArray new];
|
|
|
|
[classes autorelease];
|
|
|
|
|
|
|
|
primaryClasses = [super davComplianceClassesInContext: _ctx];
|
|
|
|
if (primaryClasses)
|
|
|
|
[classes addObjectsFromArray: primaryClasses];
|
|
|
|
[classes addObject: @"access-control"];
|
|
|
|
[classes addObject: @"addressbook-access"];
|
|
|
|
|
|
|
|
return classes;
|
|
|
|
}
|
2007-11-05 15:51:50 +01:00
|
|
|
|
|
|
|
- (NSArray *) davNamespaces
|
|
|
|
{
|
|
|
|
return [NSArray arrayWithObject: @"urn:ietf:params:xml:ns:carddav"];
|
|
|
|
}
|
2007-07-22 22:44:46 +02:00
|
|
|
|
2006-12-14 22:20:13 +01:00
|
|
|
- (NSString *) groupDavResourceType
|
|
|
|
{
|
|
|
|
return @"vcard-collection";
|
|
|
|
}
|
|
|
|
|
2007-09-17 05:42:16 +02:00
|
|
|
/* sorting */
|
|
|
|
- (NSComparisonResult) compare: (id) otherFolder
|
|
|
|
{
|
|
|
|
NSComparisonResult comparison;
|
|
|
|
|
|
|
|
if ([NSStringFromClass([otherFolder class])
|
|
|
|
isEqualToString: @"SOGoContactLDAPFolder"])
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
|
|
|
|
@end /* SOGoContactGCSFolder */
|