JSON-based operations on contacts

This commit is contained in:
Francis Lachapelle
2014-06-19 15:51:57 -04:00
parent a4ac4db1e0
commit 065acc9ff1
13 changed files with 815 additions and 102 deletions
+129 -39
View File
@@ -22,6 +22,7 @@
#import <Foundation/NSDictionary.h>
#import <Foundation/NSEnumerator.h>
#import <Foundation/NSString.h>
#import <Foundation/NSValue.h>
#import <NGObjWeb/NSException+HTTP.h>
#import <NGObjWeb/SoObject.h>
@@ -158,23 +159,88 @@ Class SOGoContactSourceFolderK, SOGoGCSFolderK;
- (id <WOActionResults>) allContactSearchAction
{
id <WOActionResults> result;
NSString *searchText;
SOGoFolder <SOGoContactFolder> *folder;
NSString *searchText, *mail, *domain;
NSDictionary *data;
NSArray *sortedContacts;
NSArray *folders, *contacts, *descriptors, *sortedContacts;
NSMutableArray *sortedFolders;
NSMutableDictionary *contact, *uniqueContacts;
unsigned int i, j, max;
NSSortDescriptor *commonNameDescriptor;
BOOL excludeGroups, excludeLists;
searchText = [self queryParameterForKey: @"search"];
if ([searchText length] > 0)
{
// NSLog(@"Search all contacts: %@", searchText);
excludeGroups = [[self queryParameterForKey: @"excludeGroups"] boolValue];
excludeLists = [[self queryParameterForKey: @"excludeLists"] boolValue];
sortedContacts = [[self clientObject] allContactsFromFilter: searchText
excludeGroups: excludeGroups
excludeLists: excludeLists];
domain = [[context activeUser] domain];
folders = nil;
NS_DURING
folders = [[self clientObject] subFolders];
NS_HANDLER
/* We need to specifically test for @"SOGoDBException", which is
raised explicitly in SOGoParentFolder. Any other exception should
be re-raised. */
if ([[localException name] isEqualToString: @"SOGoDBException"])
folders = nil;
else
[localException raise];
NS_ENDHANDLER;
max = [folders count];
sortedFolders = [NSMutableArray arrayWithCapacity: max];
uniqueContacts = [NSMutableDictionary dictionary];
for (i = 0; i < max; i++)
{
folder = [folders objectAtIndex: i];
/* We first search in LDAP folders (in case of duplicated entries in GCS folders) */
if ([folder isKindOfClass: SOGoContactSourceFolderK])
[sortedFolders insertObject: folder atIndex: 0];
else
[sortedFolders addObject: folder];
}
for (i = 0; i < max; i++)
{
folder = [sortedFolders objectAtIndex: i];
//NSLog(@" Address book: %@ (%@)", [folder displayName], [folder class]);
contacts = [folder lookupContactsWithFilter: searchText
onCriteria: @"name_or_address"
sortBy: @"c_cn"
ordering: NSOrderedAscending
inDomain: domain];
for (j = 0; j < [contacts count]; j++)
{
contact = [contacts objectAtIndex: j];
mail = [contact objectForKey: @"c_mail"];
//NSLog(@" found %@ (%@) ? %@", [contact objectForKey: @"c_name"], mail,
// [contact description]);
if (!excludeLists && [[contact objectForKey: @"c_component"]
isEqualToString: @"vlist"])
{
[contact setObject: [folder nameInContainer]
forKey: @"container"];
[uniqueContacts setObject: contact
forKey: [contact objectForKey: @"c_name"]];
}
else if ([mail length]
&& [uniqueContacts objectForKey: mail] == nil
&& !(excludeGroups && [contact objectForKey: @"isGroup"]))
[uniqueContacts setObject: contact forKey: mail];
}
}
if ([uniqueContacts count] > 0)
{
// Sort the contacts by display name
commonNameDescriptor = [[NSSortDescriptor alloc] initWithKey: @"c_cn"
ascending:YES];
descriptors = [NSArray arrayWithObjects: commonNameDescriptor, nil];
[commonNameDescriptor release];
sortedContacts = [[uniqueContacts allValues]
sortedArrayUsingDescriptors: descriptors];
}
else
sortedContacts = [NSArray array];
data = [NSDictionary dictionaryWithObjectsAndKeys: searchText, @"searchText",
sortedContacts, @"contacts",
nil];
@@ -208,48 +274,72 @@ Class SOGoContactSourceFolderK, SOGoGCSFolderK;
return [[self queryParameterForKey: @"popup"] boolValue];
}
- (NSArray *) contactFolders
- (NSString *) contactFolders
{
SOGoContactFolders *folderContainer;
NSArray *folders;
NSMutableArray *foldersAttrs;
NSDictionary *folderAttrs;
id currentFolder;
int max, i;
folderContainer = [self clientObject];
return [folderContainer subFolders];
// return [folderContainer subFolders];
folders = [folderContainer subFolders];
max = [folders count];
foldersAttrs = [NSMutableArray arrayWithCapacity: max];
for (i = 0; i < max; i++)
{
currentFolder = [folders objectAtIndex: i];
folderAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithFormat: @"%@", [currentFolder nameInContainer]], @"id",
[currentFolder displayName], @"name",
[currentFolder ownerInContext: context], @"owner",
[NSNumber numberWithBool: [currentFolder isKindOfClass: SOGoGCSFolderK]], @"isEditable",
[NSNumber numberWithBool: [currentFolder isKindOfClass: SOGoContactSourceFolderK]
&& ![currentFolder isPersonalSource]], @"isRemote",
nil];
[foldersAttrs addObject: folderAttrs];
}
return [foldersAttrs jsonRepresentation];
}
- (NSString *) currentContactFolderId
{
return [NSString stringWithFormat: @"/%@", [currentFolder nameInContainer]];
}
// - (NSString *) currentContactFolderId
// {
// return [NSString stringWithFormat: @"/%@", [currentFolder nameInContainer]];
// }
- (NSString *) currentContactFolderName
{
return [currentFolder displayName];
}
// - (NSString *) currentContactFolderName
// {
// return [currentFolder displayName];
// }
- (NSString *) currentContactFolderOwner
{
return [currentFolder ownerInContext: context];
}
// - (NSString *) currentContactFolderOwner
// {
// return [currentFolder ownerInContext: context];
//}
- (NSString *) currentContactFolderClass
{
return (([currentFolder isKindOfClass: SOGoContactSourceFolderK]
&& ![currentFolder isPersonalSource])
? @"remote" : @"local");
}
// - (NSString *) currentContactFolderClass
// {
// return (([currentFolder isKindOfClass: SOGoContactSourceFolderK]
// && ![currentFolder isPersonalSource])
// ? @"remote" : @"local");
// }
- (NSString *) currentContactFolderAclEditing
{
return ([currentFolder isKindOfClass: SOGoGCSFolderK]
? @"available": @"unavailable");
}
// - (NSString *) currentContactFolderAclEditing
// {
// return ([currentFolder isKindOfClass: SOGoGCSFolderK]
// ? @"available": @"unavailable");
// }
- (NSString *) currentContactFolderListEditing
{
return ([currentFolder isKindOfClass: SOGoGCSFolderK]
? @"available": @"unavailable");
}
// - (NSString *) currentContactFolderListEditing
// {
// return ([currentFolder isKindOfClass: SOGoGCSFolderK]
// ? @"available": @"unavailable");
//}
- (NSString *) verticalDragHandleStyle
{