2006-06-15 21:34:10 +02:00
|
|
|
/*
|
2012-11-17 23:16:00 +01:00
|
|
|
Copyright (C) 2006-2012 Inverse inc.
|
2006-06-15 21:34:10 +02:00
|
|
|
Copyright (C) 2004-2005 SKYRIX Software AG
|
|
|
|
|
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>
|
2016-11-28 20:10:00 +01:00
|
|
|
#import <SoObjects/SOGo/SOGoUserSettings.h>
|
2009-08-24 22:26:52 +02:00
|
|
|
|
|
|
|
#import <NGObjWeb/NSException+HTTP.h>
|
2007-03-21 15:28:42 +01:00
|
|
|
#import <NGObjWeb/WOContext.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-10-04 00:04:48 +02:00
|
|
|
#import <Contacts/SOGoContactObject.h>
|
|
|
|
#import <Contacts/SOGoContactFolder.h>
|
|
|
|
#import <Contacts/SOGoContactFolders.h>
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2009-09-02 15:24:02 +02:00
|
|
|
#import <NGCards/NGVCard.h>
|
|
|
|
#import <NGCards/NGVList.h>
|
|
|
|
#import <SoObjects/Contacts/SOGoContactGCSEntry.h>
|
2009-09-03 16:18:13 +02:00
|
|
|
#import <SoObjects/Contacts/SOGoContactLDIFEntry.h>
|
2009-09-02 15:24:02 +02:00
|
|
|
#import <SoObjects/Contacts/SOGoContactGCSList.h>
|
2009-09-03 16:18:13 +02:00
|
|
|
#import <SoObjects/Contacts/SOGoContactGCSFolder.h>
|
|
|
|
#import <GDLContentStore/GCSFolder.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;
|
2016-11-28 16:33:38 +01:00
|
|
|
static NSArray *sortKeys = nil;
|
|
|
|
|
|
|
|
if (!sortKeys)
|
|
|
|
{
|
|
|
|
sortKeys = [NSArray arrayWithObjects: @"c_cn", @"c_sn", @"c_givenname", @"c_mail",
|
|
|
|
@"c_screenname", @"c_o", @"c_telephonenumber", nil];
|
|
|
|
[sortKeys retain];
|
|
|
|
}
|
|
|
|
|
2011-04-14 20:01:02 +02:00
|
|
|
rq = [context request];
|
|
|
|
s = [rq formValueForKey: @"sort"];
|
2016-11-28 16:33:38 +01:00
|
|
|
if (![s length] || ![sortKeys containsObject: s])
|
2006-10-04 00:04:48 +02:00
|
|
|
s = [self defaultSortKey];
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2016-11-28 16:33:38 +01:00
|
|
|
- (void) saveSortValue
|
|
|
|
{
|
|
|
|
NSMutableDictionary *contactSettings;
|
|
|
|
NSString *ascending, *sort;
|
|
|
|
SOGoUserSettings *us;
|
|
|
|
|
|
|
|
sort = [[context request] formValueForKey: @"sort"];
|
|
|
|
ascending = [[context request] formValueForKey: @"asc"];
|
|
|
|
|
|
|
|
if ([sort length])
|
|
|
|
{
|
|
|
|
sort = [self sortKey];
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
NSComparisonResult ordering;
|
2011-04-14 20:01:02 +02:00
|
|
|
WORequest *rq;
|
2006-10-04 00:04:48 +02:00
|
|
|
|
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;
|
|
|
|
|
2007-07-24 21:59:49 +02:00
|
|
|
[contactInfos release];
|
|
|
|
contactInfos = [folder lookupContactsWithFilter: valueText
|
2010-10-26 22:19:37 +02:00
|
|
|
onCriteria: searchText
|
|
|
|
sortBy: [self sortKey]
|
2012-02-11 08:08:02 +01:00
|
|
|
ordering: ordering
|
|
|
|
inDomain: [[context activeUser] domain]];
|
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
|
|
|
/**
|
|
|
|
* Retrieve the addressbook contacts with respect to the sort and
|
|
|
|
* search criteria.
|
|
|
|
* @return a JSON array of dictionaries representing the contacts.
|
|
|
|
*/
|
|
|
|
- (id <WOActionResults>) contactsListAction
|
2011-03-24 16:27:53 +01:00
|
|
|
{
|
2011-04-14 20:01:02 +02:00
|
|
|
id <WOActionResults> result;
|
2014-02-07 21:52:43 +01:00
|
|
|
id currentInfo;
|
2011-04-14 20:01:02 +02:00
|
|
|
NSArray *contactsList;
|
2014-02-07 21:52:43 +01:00
|
|
|
NSEnumerator *contactsListEnumerator, *keysEnumerator;
|
|
|
|
NSMutableArray *newContactsList;
|
|
|
|
NSMutableDictionary *currentContactDictionary;
|
|
|
|
NSString *key;
|
2011-04-14 20:01:02 +02:00
|
|
|
|
|
|
|
contactsList = [self contactInfos];
|
2014-02-07 21:52:43 +01:00
|
|
|
contactsListEnumerator = [contactsList objectEnumerator];
|
|
|
|
newContactsList = [NSMutableArray arrayWithCapacity: [contactsList count]];
|
|
|
|
|
|
|
|
// Escape HTML
|
|
|
|
while ((currentContactDictionary = [contactsListEnumerator nextObject]))
|
|
|
|
{
|
|
|
|
keysEnumerator = [currentContactDictionary keyEnumerator];
|
|
|
|
while ((key = [keysEnumerator nextObject]))
|
|
|
|
{
|
|
|
|
currentInfo = [currentContactDictionary objectForKey: key];
|
|
|
|
if ([currentInfo respondsToSelector: @selector (stringByEscapingHTMLString)])
|
2014-03-20 20:29:32 +01:00
|
|
|
[currentContactDictionary setObject: currentInfo forKey: key];
|
2014-02-07 21:52:43 +01:00
|
|
|
}
|
|
|
|
[newContactsList addObject: currentContactDictionary];
|
|
|
|
}
|
|
|
|
|
2011-04-14 20:01:02 +02:00
|
|
|
result = [self responseWithStatus: 200
|
2014-02-07 21:52:43 +01:00
|
|
|
andString: [newContactsList 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;
|
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];
|
|
|
|
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];
|
|
|
|
mail = [contact objectForKey: @"c_mail"];
|
|
|
|
if ([mail isNotNull] && [uniqueContacts objectForKey: mail] == nil)
|
|
|
|
[uniqueContacts setObject: contact forKey: mail];
|
|
|
|
}
|
|
|
|
|
|
|
|
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",
|
|
|
|
sortedContacts, @"contacts", 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;
|
|
|
|
}
|
|
|
|
|
2011-04-14 20:01:02 +02:00
|
|
|
@end /* UIxContactsListActions */
|