2006-06-15 21:34:10 +02:00
|
|
|
/*
|
2015-08-14 21:11:07 +02:00
|
|
|
Copyright (C) 2006-2015 Inverse inc.
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2012-11-17 23:16:00 +01:00
|
|
|
This file is part of SOGo.
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2010-03-19 14:08:15 +01:00
|
|
|
SOGo is free software; you can redistribute it and/or modify it under
|
2006-06-15 21:34:10 +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.
|
|
|
|
|
2010-03-19 14:08:15 +01:00
|
|
|
SOGo is distributed in the hope that it will be useful, but WITHOUT ANY
|
2006-06-15 21:34:10 +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
|
2010-03-19 14:08:15 +01:00
|
|
|
License along with SOGo; see the file COPYING. If not, write to the
|
2006-06-15 21:34:10 +02:00
|
|
|
Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
|
|
|
|
02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2009-08-24 22:26:52 +02:00
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
#import <SoObjects/SOGo/NSArray+Utilities.h>
|
|
|
|
#import <SoObjects/SOGo/NSDictionary+Utilities.h>
|
|
|
|
#import <SoObjects/SOGo/NSString+Utilities.h>
|
2014-11-25 15:36:24 +01:00
|
|
|
#import <SoObjects/SOGo/SOGoSystemDefaults.h>
|
2009-08-24 22:26:52 +02:00
|
|
|
|
|
|
|
#import <NGObjWeb/NSException+HTTP.h>
|
2012-02-11 08:08:02 +01:00
|
|
|
#import <NGObjWeb/WOContext+SoObjects.h>
|
2007-03-21 15:28:42 +01:00
|
|
|
#import <NGObjWeb/WOResponse.h>
|
2009-09-03 16:18:13 +02:00
|
|
|
#import <NGObjWeb/WORequest.h>
|
2007-08-20 21:12:50 +02:00
|
|
|
#import <NGExtensions/NSString+misc.h>
|
2009-08-25 19:51:20 +02:00
|
|
|
#import <NGExtensions/NSNull+misc.h>
|
2007-03-21 15:28:42 +01:00
|
|
|
|
2011-04-14 20:01:02 +02:00
|
|
|
#import <Common/WODirectAction+SOGo.h>
|
|
|
|
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2015-08-14 21:11:07 +02:00
|
|
|
#import <SOGo/SOGoUser.h>
|
2015-08-12 20:34:32 +02:00
|
|
|
#import <SOGo/SOGoUserSettings.h>
|
|
|
|
|
2009-09-02 15:24:02 +02:00
|
|
|
#import <SoObjects/Contacts/SOGoContactGCSEntry.h>
|
2009-09-03 16:18:13 +02:00
|
|
|
#import <SoObjects/Contacts/SOGoContactGCSFolder.h>
|
2009-09-02 15:24:02 +02:00
|
|
|
|
2011-04-14 20:01:02 +02:00
|
|
|
#import "UIxContactsListActions.h"
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2011-04-14 20:01:02 +02:00
|
|
|
@implementation UIxContactsListActions
|
2006-10-04 00:04:48 +02:00
|
|
|
|
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
if ((self = [super init]))
|
2009-07-17 23:25:38 +02:00
|
|
|
contactInfos = nil;
|
|
|
|
|
2006-10-04 00:04:48 +02:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2007-07-20 22:22:16 +02:00
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
[contactInfos release];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2006-10-04 00:04:48 +02:00
|
|
|
/* accessors */
|
|
|
|
|
|
|
|
- (NSString *) defaultSortKey
|
|
|
|
{
|
2009-02-19 23:21:47 +01:00
|
|
|
return @"c_cn";
|
2006-10-04 00:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) sortKey
|
|
|
|
{
|
|
|
|
NSString *s;
|
2011-04-14 20:01:02 +02:00
|
|
|
WORequest *rq;
|
2006-10-04 00:04:48 +02:00
|
|
|
|
2011-04-14 20:01:02 +02:00
|
|
|
rq = [context request];
|
|
|
|
s = [rq formValueForKey: @"sort"];
|
2007-06-28 22:37:27 +02:00
|
|
|
if (![s length])
|
2006-10-04 00:04:48 +02:00
|
|
|
s = [self defaultSortKey];
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2015-08-14 21:11:07 +02:00
|
|
|
- (void) saveSortValue
|
|
|
|
{
|
2015-08-12 20:34:32 +02:00
|
|
|
NSMutableDictionary *contactSettings;
|
2015-08-14 21:11:07 +02:00
|
|
|
NSString *ascending, *sort;
|
|
|
|
SOGoUserSettings *us;
|
|
|
|
|
|
|
|
sort = [[context request] formValueForKey: @"sort"];
|
2015-08-12 20:34:32 +02:00
|
|
|
ascending = [[context request] formValueForKey: @"asc"];
|
2015-08-14 21:11:07 +02:00
|
|
|
|
2015-08-12 20:34:32 +02:00
|
|
|
if ([sort length])
|
|
|
|
{
|
2015-08-14 21:11:07 +02:00
|
|
|
us = [[context activeUser] userSettings];
|
|
|
|
contactSettings = [us objectForKey: @"Contact"];
|
|
|
|
// Must create if it doesn't exist
|
|
|
|
if (!contactSettings)
|
|
|
|
{
|
|
|
|
contactSettings = [NSMutableDictionary dictionary];
|
|
|
|
[us setObject: contactSettings forKey: @"Contact"];
|
|
|
|
}
|
|
|
|
[contactSettings setObject: [NSArray arrayWithObjects: [sort lowercaseString], [NSString stringWithFormat: @"%d", [ascending intValue]], nil]
|
|
|
|
forKey: @"SortingState"];
|
|
|
|
[us synchronize];
|
2015-08-12 20:34:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-04 00:04:48 +02:00
|
|
|
- (NSArray *) contactInfos
|
|
|
|
{
|
|
|
|
id <SOGoContactFolder> folder;
|
2007-06-28 22:37:27 +02:00
|
|
|
NSString *ascending, *searchText, *valueText;
|
2014-09-05 21:12:05 +02:00
|
|
|
NSArray *results;
|
|
|
|
NSMutableArray *filteredContacts;
|
|
|
|
NSDictionary *contact;
|
|
|
|
BOOL excludeLists;
|
2007-06-28 22:37:27 +02:00
|
|
|
NSComparisonResult ordering;
|
2011-04-14 20:01:02 +02:00
|
|
|
WORequest *rq;
|
2014-09-05 21:12:05 +02:00
|
|
|
unsigned int i;
|
2006-10-04 00:04:48 +02:00
|
|
|
|
2015-08-14 21:11:07 +02:00
|
|
|
[self saveSortValue];
|
|
|
|
|
2007-07-20 22:22:16 +02:00
|
|
|
if (!contactInfos)
|
|
|
|
{
|
|
|
|
folder = [self clientObject];
|
2011-04-14 20:01:02 +02:00
|
|
|
rq = [context request];
|
2007-07-20 22:22:16 +02:00
|
|
|
|
2011-04-14 20:01:02 +02:00
|
|
|
ascending = [rq formValueForKey: @"asc"];
|
2007-07-20 22:22:16 +02:00
|
|
|
ordering = ((![ascending length] || [ascending boolValue])
|
|
|
|
? NSOrderedAscending : NSOrderedDescending);
|
|
|
|
|
2011-04-14 20:01:02 +02:00
|
|
|
searchText = [rq formValueForKey: @"search"];
|
2007-07-20 22:22:16 +02:00
|
|
|
if ([searchText length] > 0)
|
2011-04-14 20:01:02 +02:00
|
|
|
valueText = [rq formValueForKey: @"value"];
|
2007-07-20 22:22:16 +02:00
|
|
|
else
|
|
|
|
valueText = nil;
|
|
|
|
|
2014-09-05 21:12:05 +02:00
|
|
|
excludeLists = [[rq formValueForKey: @"excludeLists"] boolValue];
|
|
|
|
|
2007-07-24 21:59:49 +02:00
|
|
|
[contactInfos release];
|
2014-09-05 21:12:05 +02:00
|
|
|
results = [folder lookupContactsWithFilter: valueText
|
|
|
|
onCriteria: searchText
|
|
|
|
sortBy: [self sortKey]
|
|
|
|
ordering: ordering
|
|
|
|
inDomain: [[context activeUser] domain]];
|
|
|
|
if (excludeLists)
|
|
|
|
{
|
|
|
|
filteredContacts = [NSMutableArray array];
|
|
|
|
for (i = 0; i < [results count]; i++)
|
|
|
|
{
|
|
|
|
contact = [results objectAtIndex: i];
|
|
|
|
if (![[contact objectForKey: @"c_component"] isEqualToString: @"vlist"])
|
|
|
|
{
|
|
|
|
[filteredContacts addObject: contact];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
contactInfos = [NSArray arrayWithArray: filteredContacts];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
contactInfos = results;
|
|
|
|
}
|
2007-07-24 21:59:49 +02:00
|
|
|
[contactInfos retain];
|
2007-07-20 22:22:16 +02:00
|
|
|
}
|
2006-10-04 00:04:48 +02:00
|
|
|
|
2007-07-20 22:22:16 +02:00
|
|
|
return contactInfos;
|
2006-10-04 00:04:48 +02:00
|
|
|
}
|
|
|
|
|
2011-04-14 20:01:02 +02:00
|
|
|
/**
|
2015-01-22 15:17:41 +01:00
|
|
|
* @api {get} /so/:username/Contacts/:addressbookId/view List cards
|
|
|
|
* @apiVersion 1.0.0
|
|
|
|
* @apiName GetContactsList
|
|
|
|
* @apiGroup Contacts
|
|
|
|
* @apiExample {curl} Example usage:
|
|
|
|
* curl -i http://localhost/SOGo/so/sogo1/Contacts/personal/view?search=name_or_address\&value=Bob
|
|
|
|
*
|
|
|
|
* @apiParam {Boolean} [asc] Descending sort when false. Defaults to true (ascending).
|
|
|
|
* @apiParam {String} [sort] Sort field. Either c_cn, c_mail, c_screenname, c_o, or c_telephonenumber.
|
|
|
|
* @apiParam {String} [search] Field criteria. Either name_or_address, category, or organization.
|
|
|
|
* @apiParam {String} [value] String to match
|
|
|
|
*
|
|
|
|
* @apiSuccess (Success 200) {String} id Address book ID
|
|
|
|
* @apiSuccess (Success 200) {String} [publicCardDavURL] Public CardDAV URL of the address book
|
|
|
|
* @apiSuccess (Success 200) {String} cardDavURL CardDAV URL of the address book
|
|
|
|
* @apiSuccess (Success 200) {Object[]} [cards] Matching cards
|
|
|
|
* @apiSuccess (Success 200) {String} cards.id Card ID
|
|
|
|
* @apiSuccess (Success 200) {String} cards.c_name Card ID
|
|
|
|
* @apiSuccess (Success 200) {String} cards.c_component Either vcard or vlist
|
|
|
|
* @apiSuccess (Success 200) {String} cards.c_cn Fullname
|
|
|
|
* @apiSuccess (Success 200) {String} cards.c_givenname Firstname
|
|
|
|
* @apiSuccess (Success 200) {String} cards.c_sn Lastname
|
|
|
|
* @apiSuccess (Success 200) {String} cards.c_screenname Screenname
|
|
|
|
* @apiSuccess (Success 200) {String} cards.c_o Organization name
|
|
|
|
* @apiSuccess (Success 200) {Object} cards.emails Preferred email address
|
|
|
|
* @apiSuccess (Success 200) {String} cards.emails.type Type (e.g., home or work)
|
|
|
|
* @apiSuccess (Success 200) {String} cards.emails.value Email address
|
|
|
|
* @apiSuccess (Success 200) {String} cards.c_mail Preferred email address
|
|
|
|
* @apiSuccess (Success 200) {String} cards.c_telephonenumber Preferred telephone number
|
|
|
|
* @apiSuccess (Success 200) {String} cards.c_categories Comma-separated list of categories
|
|
|
|
*
|
|
|
|
* See [SOGoContactGCSFolder fixupContactRecord:]
|
2011-04-14 20:01:02 +02:00
|
|
|
*/
|
|
|
|
- (id <WOActionResults>) contactsListAction
|
2011-03-24 16:27:53 +01:00
|
|
|
{
|
2011-04-14 20:01:02 +02:00
|
|
|
id <WOActionResults> result;
|
2014-06-19 21:51:57 +02:00
|
|
|
NSDictionary *data;
|
2011-04-14 20:01:02 +02:00
|
|
|
NSArray *contactsList;
|
|
|
|
|
|
|
|
contactsList = [self contactInfos];
|
2014-02-07 21:52:43 +01:00
|
|
|
|
2014-06-19 21:51:57 +02:00
|
|
|
data = [NSDictionary dictionaryWithObjectsAndKeys:
|
|
|
|
[[self clientObject] nameInContainer], @"id",
|
2014-11-25 15:36:24 +01:00
|
|
|
[self cardDavURL], @"cardDavURL",
|
|
|
|
[self publicCardDavURL], @"publicCardDavURL",
|
2015-01-22 15:17:41 +01:00
|
|
|
contactsList, @"cards",
|
2014-06-19 21:51:57 +02:00
|
|
|
nil];
|
|
|
|
|
2011-04-14 20:01:02 +02:00
|
|
|
result = [self responseWithStatus: 200
|
2014-06-19 21:51:57 +02:00
|
|
|
andString: [data jsonRepresentation]];
|
2011-04-14 20:01:02 +02:00
|
|
|
|
|
|
|
return result;
|
2011-03-24 16:27:53 +01:00
|
|
|
}
|
|
|
|
|
2009-08-24 22:26:52 +02:00
|
|
|
- (id <WOActionResults>) contactSearchAction
|
|
|
|
{
|
|
|
|
id <WOActionResults> result;
|
|
|
|
id <SOGoContactFolder> folder;
|
2014-09-05 21:12:05 +02:00
|
|
|
BOOL excludeLists;
|
2012-02-11 08:08:02 +01:00
|
|
|
NSString *searchText, *mail, *domain;
|
2009-08-24 22:26:52 +02:00
|
|
|
NSDictionary *contact, *data;
|
|
|
|
NSArray *contacts, *descriptors, *sortedContacts;
|
|
|
|
NSMutableDictionary *uniqueContacts;
|
|
|
|
unsigned int i;
|
|
|
|
NSSortDescriptor *commonNameDescriptor;
|
2011-04-14 20:01:02 +02:00
|
|
|
WORequest *rq;
|
2009-08-24 22:26:52 +02:00
|
|
|
|
2011-04-14 20:01:02 +02:00
|
|
|
rq = [context request];
|
2014-09-05 21:12:05 +02:00
|
|
|
excludeLists = [[rq formValueForKey: @"excludeLists"] boolValue];
|
2011-04-14 20:01:02 +02:00
|
|
|
searchText = [rq formValueForKey: @"search"];
|
2009-08-24 22:26:52 +02:00
|
|
|
if ([searchText length] > 0)
|
|
|
|
{
|
|
|
|
NS_DURING
|
|
|
|
folder = [self clientObject];
|
|
|
|
NS_HANDLER
|
|
|
|
if ([[localException name] isEqualToString: @"SOGoDBException"])
|
|
|
|
folder = nil;
|
|
|
|
else
|
|
|
|
[localException raise];
|
|
|
|
NS_ENDHANDLER
|
2012-02-11 08:08:02 +01:00
|
|
|
|
|
|
|
domain = [[context activeUser] domain];
|
2009-08-24 22:26:52 +02:00
|
|
|
uniqueContacts = [NSMutableDictionary dictionary];
|
|
|
|
contacts = [folder lookupContactsWithFilter: searchText
|
2010-10-26 22:19:37 +02:00
|
|
|
onCriteria: @"name_or_address"
|
2009-08-24 22:26:52 +02:00
|
|
|
sortBy: @"c_cn"
|
2012-02-11 08:08:02 +01:00
|
|
|
ordering: NSOrderedAscending
|
2012-03-21 16:50:02 +01:00
|
|
|
inDomain: domain];
|
2009-08-24 22:26:52 +02:00
|
|
|
for (i = 0; i < [contacts count]; i++)
|
|
|
|
{
|
|
|
|
contact = [contacts objectAtIndex: i];
|
2014-09-05 21:12:05 +02:00
|
|
|
if (!excludeLists || ![[contact objectForKey: @"c_component"] isEqualToString: @"vlist"])
|
|
|
|
{
|
|
|
|
mail = [contact objectForKey: @"c_mail"];
|
|
|
|
if ([mail isNotNull] && [uniqueContacts objectForKey: mail] == nil)
|
|
|
|
[uniqueContacts setObject: contact forKey: mail];
|
|
|
|
}
|
2009-08-24 22:26:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ([uniqueContacts count] > 0)
|
|
|
|
{
|
|
|
|
// Sort the contacts by display name
|
|
|
|
commonNameDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"c_cn"
|
|
|
|
ascending:YES] autorelease];
|
|
|
|
descriptors = [NSArray arrayWithObjects: commonNameDescriptor, nil];
|
|
|
|
sortedContacts = [[uniqueContacts allValues] sortedArrayUsingDescriptors: descriptors];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
sortedContacts = [NSArray array];
|
|
|
|
|
|
|
|
data = [NSDictionary dictionaryWithObjectsAndKeys: searchText, @"searchText",
|
2014-08-06 22:06:58 +02:00
|
|
|
sortedContacts, @"cards", nil];
|
2011-04-14 20:01:02 +02:00
|
|
|
result = [self responseWithStatus: 200
|
|
|
|
andString: [data jsonRepresentation]];
|
2009-08-24 22:26:52 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
result = [NSException exceptionWithHTTPStatus: 400
|
|
|
|
reason: @"missing 'search' parameter"];
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-11-25 15:36:24 +01:00
|
|
|
- (NSString *) cardDavURL
|
|
|
|
{
|
|
|
|
NSString *davURL, *baseCardDAVURL;
|
|
|
|
|
|
|
|
davURL = @"";
|
|
|
|
|
|
|
|
if ([[self clientObject] respondsToSelector: @selector(realDavURL)])
|
|
|
|
davURL = [[[self clientObject] realDavURL] absoluteString];
|
|
|
|
|
|
|
|
if ([davURL hasSuffix: @"/"])
|
|
|
|
baseCardDAVURL = [davURL substringToIndex: [davURL length] - 1];
|
|
|
|
else
|
|
|
|
baseCardDAVURL = davURL;
|
|
|
|
|
|
|
|
return [NSString stringWithFormat: @"%@/", baseCardDAVURL];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) publicCardDavURL
|
|
|
|
{
|
|
|
|
if ([[self clientObject] respondsToSelector: @selector(publicDavURL)] &&
|
|
|
|
[[SOGoSystemDefaults sharedSystemDefaults] enablePublicAccess])
|
|
|
|
{
|
|
|
|
NSString *davURL, *basePublicCardDAVURL;
|
|
|
|
|
|
|
|
davURL = [[[self clientObject] publicDavURL] absoluteString];
|
|
|
|
if ([davURL hasSuffix: @"/"])
|
|
|
|
basePublicCardDAVURL = [davURL substringToIndex: [davURL length] - 1];
|
|
|
|
else
|
|
|
|
basePublicCardDAVURL = davURL;
|
|
|
|
|
|
|
|
return [NSString stringWithFormat: @"%@/", basePublicCardDAVURL];
|
|
|
|
}
|
|
|
|
|
|
|
|
return @"";
|
|
|
|
}
|
|
|
|
|
2011-04-14 20:01:02 +02:00
|
|
|
@end /* UIxContactsListActions */
|