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>
|
|
|
|
#import <NGObjWeb/NSException+HTTP.h>
|
|
|
|
#import <NGObjWeb/SoObject+SoDAV.h>
|
|
|
|
#import <NGObjWeb/WOContext.h>
|
|
|
|
#import <NGObjWeb/WORequest.h>
|
|
|
|
#import <NGExtensions/NSObject+Logs.h>
|
|
|
|
#import <EOControl/EOQualifier.h>
|
|
|
|
#import <EOControl/EOSortOrdering.h>
|
2006-08-09 23:11:49 +02:00
|
|
|
#import <GDLContentStore/GCSFolder.h>
|
|
|
|
|
|
|
|
#import "SOGoContactGCSEntry.h"
|
|
|
|
#import "SOGoContactGCSFolder.h"
|
|
|
|
|
2007-07-24 20:45:52 +02:00
|
|
|
#define folderListingFields [NSArray arrayWithObjects: @"c_name", @"c_cn", \
|
|
|
|
@"c_givenname", @"c_screenname", \
|
|
|
|
@"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
|
|
|
|
|
|
|
|
+ (id <SOGoContactFolder>) contactFolderWithName: (NSString *) aName
|
|
|
|
andDisplayName: (NSString *) aDisplayName
|
|
|
|
inContainer: (SOGoObject *) aContainer
|
|
|
|
{
|
|
|
|
SOGoContactGCSFolder *folder;
|
|
|
|
|
|
|
|
folder = [[self alloc] initWithName: aName
|
|
|
|
andDisplayName: aDisplayName
|
|
|
|
inContainer: aContainer];
|
|
|
|
[folder autorelease];
|
|
|
|
|
|
|
|
return folder;
|
|
|
|
}
|
|
|
|
|
2007-05-08 21:30:21 +02:00
|
|
|
- (void) dealloc
|
2006-08-09 23:11:49 +02:00
|
|
|
{
|
2007-05-08 21:30:21 +02:00
|
|
|
[displayName release];
|
|
|
|
[super dealloc];
|
2006-08-09 23:11:49 +02:00
|
|
|
}
|
|
|
|
|
2007-05-08 21:30:21 +02:00
|
|
|
- (id <SOGoContactFolder>) initWithName: (NSString *) newName
|
|
|
|
andDisplayName: (NSString *) newDisplayName
|
|
|
|
inContainer: (SOGoObject *) newContainer
|
2006-08-09 23:11:49 +02:00
|
|
|
{
|
2007-05-08 21:30:21 +02:00
|
|
|
if ((self = [self initWithName: newName
|
|
|
|
inContainer: newContainer]))
|
|
|
|
ASSIGN (displayName, newDisplayName);
|
2007-06-01 06:16:32 +02:00
|
|
|
|
2007-05-08 21:30:21 +02:00
|
|
|
return self;
|
2006-08-09 23:11:49 +02:00
|
|
|
}
|
|
|
|
|
2007-06-30 00:04:04 +02:00
|
|
|
- (BOOL) folderIsMandatory
|
|
|
|
{
|
|
|
|
return [nameInContainer isEqualToString: @"personal"];
|
|
|
|
}
|
|
|
|
|
2006-08-09 23:11:49 +02:00
|
|
|
- (NSString *) displayName
|
|
|
|
{
|
|
|
|
return displayName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
/* first check attributes directly bound to the application */
|
|
|
|
obj = [super lookupName:_key inContext:_ctx acquire:NO];
|
|
|
|
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 "
|
|
|
|
@"(c_mail isCaseInsensitiveLike: '%@%%') OR "
|
|
|
|
@"(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"];
|
|
|
|
if (!data)
|
|
|
|
data = @"";
|
|
|
|
[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;
|
|
|
|
}
|
|
|
|
|
2006-08-09 23:11:49 +02:00
|
|
|
- (NSArray *) lookupContactsWithFilter: (NSString *) filter
|
|
|
|
sortBy: (NSString *) sortKey
|
|
|
|
ordering: (NSComparisonResult) sortOrdering
|
|
|
|
{
|
2007-07-24 21:57:51 +02:00
|
|
|
NSArray *fields, *dbRecords, *records;
|
2006-08-09 23:11:49 +02:00
|
|
|
EOQualifier *qualifier;
|
|
|
|
EOSortOrdering *ordering;
|
|
|
|
|
2007-06-01 06:16:32 +02:00
|
|
|
// NSLog (@"fetching records matching '%@', sorted by '%@' in order %d",
|
|
|
|
// filter, sortKey, sortOrdering);
|
2006-08-09 23:11:49 +02:00
|
|
|
|
|
|
|
fields = folderListingFields;
|
|
|
|
qualifier = [self _qualifierForFilter: filter];
|
2007-07-24 21:57:51 +02:00
|
|
|
dbRecords = [[self ocsFolder] fetchFields: fields
|
|
|
|
matchingQualifier: qualifier];
|
2007-07-27 19:47:08 +02:00
|
|
|
if ([dbRecords count] > 0)
|
2006-08-09 23:11:49 +02:00
|
|
|
{
|
2007-07-24 21:57:51 +02:00
|
|
|
records = [self _flattenedRecords: dbRecords];
|
2006-08-09 23:11:49 +02:00
|
|
|
ordering
|
|
|
|
= [EOSortOrdering sortOrderingWithKey: sortKey
|
|
|
|
selector: ((sortOrdering == NSOrderedDescending)
|
|
|
|
? EOCompareCaseInsensitiveDescending
|
|
|
|
: EOCompareCaseInsensitiveAscending)];
|
|
|
|
records
|
|
|
|
= [records sortedArrayUsingKeyOrderArray:
|
|
|
|
[NSArray arrayWithObject: ordering]];
|
|
|
|
}
|
|
|
|
else
|
2007-07-24 21:57:51 +02:00
|
|
|
records = nil;
|
|
|
|
// else
|
|
|
|
// [self errorWithFormat:@"(%s): fetch failed!", __PRETTY_FUNCTION__];
|
2006-08-09 23:11:49 +02:00
|
|
|
|
|
|
|
//[self debugWithFormat:@"fetched %i records.", [records count]];
|
|
|
|
return records;
|
|
|
|
}
|
|
|
|
|
2007-07-22 22:44:46 +02:00
|
|
|
- (NSArray *) davNamespaces
|
|
|
|
{
|
|
|
|
return [NSArray arrayWithObject: @"urn:ietf:params:xml:ns:carddav"];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (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;
|
|
|
|
}
|
|
|
|
|
2006-12-14 22:20:13 +01:00
|
|
|
- (NSString *) groupDavResourceType
|
|
|
|
{
|
|
|
|
return @"vcard-collection";
|
|
|
|
}
|
|
|
|
|
2007-03-21 15:27:48 +01:00
|
|
|
- (NSException *) delete
|
|
|
|
{
|
|
|
|
return (([nameInContainer isEqualToString: @"personal"])
|
|
|
|
? [NSException exceptionWithHTTPStatus: 403
|
|
|
|
reason: @"the 'personal' folder cannot be deleted"]
|
|
|
|
: [super delete]);
|
|
|
|
}
|
|
|
|
|
2006-08-09 23:11:49 +02:00
|
|
|
// /* GET */
|
|
|
|
|
|
|
|
// - (id) GETAction: (id)_ctx
|
|
|
|
// {
|
|
|
|
// // TODO: I guess this should really be done by SOPE (redirect to
|
|
|
|
// // default method)
|
|
|
|
// WOResponse *r;
|
|
|
|
// NSString *uri;
|
|
|
|
|
|
|
|
// uri = [[_ctx request] uri];
|
|
|
|
// if (![uri hasSuffix:@"/"]) uri = [uri stringByAppendingString:@"/"];
|
|
|
|
// uri = [uri stringByAppendingString:@"view"];
|
|
|
|
|
|
|
|
// r = [_ctx response];
|
|
|
|
// [r setStatus:302 /* moved */];
|
|
|
|
// [r setHeader:uri forKey:@"location"];
|
|
|
|
// return r;
|
|
|
|
// }
|
|
|
|
|
|
|
|
/* 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 */
|