2006-08-05 00:51:42 +02:00
|
|
|
/* UIxContactFoldersView.m - this file is part of SOGo
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Inverse groupe conseil
|
|
|
|
*
|
|
|
|
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
|
|
|
|
*
|
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This file 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2008-07-09 04:01:32 +02:00
|
|
|
#import <Foundation/NSArray.h>
|
2008-07-04 18:06:09 +02:00
|
|
|
#import <Foundation/NSDictionary.h>
|
|
|
|
#import <Foundation/NSEnumerator.h>
|
2006-08-05 00:51:42 +02:00
|
|
|
#import <Foundation/NSString.h>
|
2008-02-04 23:22:14 +01:00
|
|
|
#import <Foundation/NSUserDefaults.h>
|
2006-11-01 21:37:02 +01:00
|
|
|
|
2006-10-26 02:15:12 +02:00
|
|
|
#import <NGObjWeb/NSException+HTTP.h>
|
|
|
|
#import <NGObjWeb/SoObject.h>
|
2007-08-06 16:46:38 +02:00
|
|
|
#import <NGObjWeb/SoSecurityManager.h>
|
2007-02-09 22:24:15 +01:00
|
|
|
#import <NGObjWeb/WOContext.h>
|
2007-08-06 16:46:38 +02:00
|
|
|
#import <NGObjWeb/WORequest.h>
|
|
|
|
#import <NGObjWeb/WOResponse.h>
|
2006-08-05 00:51:42 +02:00
|
|
|
|
2007-03-29 19:12:44 +02:00
|
|
|
#import <GDLContentStore/GCSFolder.h>
|
|
|
|
#import <GDLContentStore/GCSFolderManager.h>
|
|
|
|
|
2007-05-09 21:23:45 +02:00
|
|
|
#import <SoObjects/SOGo/LDAPUserManager.h>
|
2007-08-06 16:46:38 +02:00
|
|
|
#import <SoObjects/SOGo/SOGoPermissions.h>
|
2006-11-01 21:37:02 +01:00
|
|
|
#import <SoObjects/SOGo/SOGoUser.h>
|
2007-11-13 23:51:32 +01:00
|
|
|
#import <SoObjects/SOGo/NSArray+Utilities.h>
|
2006-12-14 22:20:13 +01:00
|
|
|
#import <SoObjects/SOGo/NSString+Utilities.h>
|
2006-10-26 02:15:12 +02:00
|
|
|
#import <SoObjects/Contacts/SOGoContactFolders.h>
|
|
|
|
#import <SoObjects/Contacts/SOGoContactFolder.h>
|
2006-12-20 23:52:32 +01:00
|
|
|
#import <SoObjects/Contacts/SOGoContactGCSFolder.h>
|
2007-03-29 19:12:44 +02:00
|
|
|
#import <SoObjects/Contacts/SOGoContactLDAPFolder.h>
|
2006-09-19 23:05:18 +02:00
|
|
|
|
2006-08-05 00:51:42 +02:00
|
|
|
#import "UIxContactFoldersView.h"
|
|
|
|
|
|
|
|
@implementation UIxContactFoldersView
|
|
|
|
|
2008-02-04 23:22:14 +01:00
|
|
|
- (void) _setupContext
|
|
|
|
{
|
|
|
|
SOGoUser *activeUser;
|
|
|
|
NSString *module;
|
|
|
|
SOGoContactFolders *clientObject;
|
|
|
|
|
|
|
|
activeUser = [context activeUser];
|
|
|
|
clientObject = [self clientObject];
|
|
|
|
|
|
|
|
module = [clientObject nameInContainer];
|
|
|
|
|
|
|
|
ud = [activeUser userSettings];
|
|
|
|
moduleSettings = [ud objectForKey: module];
|
|
|
|
if (!moduleSettings)
|
|
|
|
{
|
|
|
|
moduleSettings = [NSMutableDictionary new];
|
|
|
|
[moduleSettings autorelease];
|
|
|
|
}
|
|
|
|
[ud setObject: moduleSettings forKey: module];
|
|
|
|
}
|
|
|
|
|
2006-10-04 00:07:56 +02:00
|
|
|
- (id) _selectActionForApplication: (NSString *) actionName
|
2006-08-05 00:51:42 +02:00
|
|
|
{
|
|
|
|
SOGoContactFolders *folders;
|
2006-10-04 00:07:56 +02:00
|
|
|
NSString *url, *action;
|
|
|
|
WORequest *request;
|
2006-08-05 00:51:42 +02:00
|
|
|
|
|
|
|
folders = [self clientObject];
|
2007-09-15 00:01:02 +02:00
|
|
|
action = [NSString stringWithFormat: @"../personal/%@", actionName];
|
2006-08-05 00:51:42 +02:00
|
|
|
|
2006-10-04 00:07:56 +02:00
|
|
|
request = [[self context] request];
|
2006-08-05 00:51:42 +02:00
|
|
|
|
2006-10-04 00:07:56 +02:00
|
|
|
url = [[request uri] composeURLWithAction: action
|
|
|
|
parameters: [self queryParameters]
|
|
|
|
andHash: NO];
|
2006-08-21 19:45:35 +02:00
|
|
|
|
|
|
|
return [self redirectToLocation: url];
|
|
|
|
}
|
|
|
|
|
2006-10-04 00:07:56 +02:00
|
|
|
- (id) defaultAction
|
2006-08-14 22:41:04 +02:00
|
|
|
{
|
2006-10-04 00:07:56 +02:00
|
|
|
return [self _selectActionForApplication: @"view"];
|
|
|
|
}
|
2006-08-14 22:41:04 +02:00
|
|
|
|
2007-03-29 20:10:07 +02:00
|
|
|
- (id) selectForMailerAction
|
2006-12-14 22:20:13 +01:00
|
|
|
{
|
2007-03-29 20:10:07 +02:00
|
|
|
return [self _selectActionForApplication: @"mailer-contacts"];
|
2006-12-14 22:20:13 +01:00
|
|
|
}
|
|
|
|
|
2007-05-09 21:23:45 +02:00
|
|
|
- (void) _fillResults: (NSMutableDictionary *) results
|
2008-07-08 13:32:43 +02:00
|
|
|
inFolder: (id <SOGoContactFolder>) folder
|
|
|
|
withSearchOn: (NSString *) contact
|
2006-10-26 02:15:12 +02:00
|
|
|
{
|
2007-05-09 21:23:45 +02:00
|
|
|
NSEnumerator *folderResults;
|
|
|
|
NSDictionary *currentContact;
|
|
|
|
NSString *uid;
|
|
|
|
|
|
|
|
folderResults = [[folder lookupContactsWithFilter: contact
|
|
|
|
sortBy: @"cn"
|
|
|
|
ordering: NSOrderedAscending] objectEnumerator];
|
|
|
|
currentContact = [folderResults nextObject];
|
|
|
|
while (currentContact)
|
|
|
|
{
|
|
|
|
uid = [currentContact objectForKey: @"c_uid"];
|
|
|
|
if (uid && ![results objectForKey: uid])
|
2007-09-11 22:27:13 +02:00
|
|
|
[results setObject: currentContact forKey: uid];
|
2007-05-09 21:23:45 +02:00
|
|
|
currentContact = [folderResults nextObject];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-26 02:15:12 +02:00
|
|
|
- (NSString *) _emailForResult: (NSDictionary *) result
|
|
|
|
{
|
|
|
|
NSMutableString *email;
|
|
|
|
NSString *name, *mail;
|
|
|
|
|
|
|
|
email = [NSMutableString string];
|
|
|
|
name = [result objectForKey: @"displayName"];
|
|
|
|
if (![name length])
|
|
|
|
name = [result objectForKey: @"cn"];
|
|
|
|
mail = [result objectForKey: @"mail"];
|
|
|
|
if ([name length])
|
|
|
|
[email appendFormat: @"%@ <%@>", name, mail];
|
|
|
|
else
|
|
|
|
[email appendString: mail];
|
|
|
|
|
|
|
|
return email;
|
|
|
|
}
|
|
|
|
|
2007-05-10 21:01:09 +02:00
|
|
|
- (WOResponse *) _responseForResults: (NSArray *) results
|
2006-10-26 02:15:12 +02:00
|
|
|
{
|
|
|
|
WOResponse *response;
|
2007-05-10 21:01:09 +02:00
|
|
|
NSEnumerator *contacts;
|
2007-11-13 23:51:32 +01:00
|
|
|
NSString *email;
|
2007-05-10 21:01:09 +02:00
|
|
|
NSDictionary *contact;
|
2007-11-13 23:51:32 +01:00
|
|
|
NSMutableArray *formattedContacts;
|
|
|
|
NSMutableDictionary *formattedContact;
|
2006-10-26 02:15:12 +02:00
|
|
|
|
|
|
|
response = [context response];
|
|
|
|
|
2007-05-10 21:01:09 +02:00
|
|
|
if ([results count] > 0)
|
2006-10-26 02:15:12 +02:00
|
|
|
{
|
2007-09-10 23:40:39 +02:00
|
|
|
[response setStatus: 200];
|
2007-05-10 21:01:09 +02:00
|
|
|
contacts = [results objectEnumerator];
|
|
|
|
contact = [contacts nextObject];
|
2007-11-13 23:51:32 +01:00
|
|
|
formattedContacts = [[NSMutableArray alloc] initWithCapacity: [results count]];
|
|
|
|
while (contact)
|
2007-05-09 21:23:45 +02:00
|
|
|
{
|
2007-09-10 23:40:39 +02:00
|
|
|
email = [contact objectForKey: @"c_email"];
|
|
|
|
if ([email length])
|
|
|
|
{
|
2007-11-13 23:51:32 +01:00
|
|
|
formattedContact = [NSMutableDictionary dictionary];
|
|
|
|
[formattedContact setObject: [contact objectForKey: @"c_uid"]
|
|
|
|
forKey: @"uid"];
|
|
|
|
[formattedContact setObject: [contact objectForKey: @"cn"]
|
|
|
|
forKey: @"name"];
|
|
|
|
[formattedContact setObject: email
|
|
|
|
forKey: @"email"];
|
|
|
|
[formattedContacts addObject: formattedContact];
|
2007-09-10 23:40:39 +02:00
|
|
|
}
|
2007-11-13 23:51:32 +01:00
|
|
|
contact = [contacts nextObject];
|
2007-05-09 21:23:45 +02:00
|
|
|
}
|
2007-11-13 23:51:32 +01:00
|
|
|
[response appendContentString: [formattedContacts jsonRepresentation]];
|
|
|
|
[formattedContacts release];
|
2006-10-26 02:15:12 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
[response setStatus: 404];
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2008-07-04 11:43:28 +02:00
|
|
|
- (id <WOActionResults>) allContactSearchAction
|
|
|
|
{
|
|
|
|
id <WOActionResults> result;
|
|
|
|
id <SOGoContactFolder> folder;
|
2008-08-09 21:09:02 +02:00
|
|
|
NSString *searchText, *mail;
|
2008-07-09 04:01:32 +02:00
|
|
|
NSDictionary *contact;
|
2008-07-04 11:43:28 +02:00
|
|
|
NSArray *folders, *contacts, *descriptors, *sortedContacts;
|
2008-07-09 04:01:32 +02:00
|
|
|
NSMutableDictionary *uniqueContacts;
|
|
|
|
unsigned int i, j;
|
2008-07-04 11:43:28 +02:00
|
|
|
NSSortDescriptor *displayNameDescriptor;
|
2008-07-08 13:32:43 +02:00
|
|
|
|
2008-07-04 11:43:28 +02:00
|
|
|
searchText = [self queryParameterForKey: @"search"];
|
|
|
|
if ([searchText length] > 0)
|
|
|
|
{
|
2008-07-09 09:35:10 +02:00
|
|
|
//NSLog(@"Search all contacts: %@", searchText);
|
2008-07-04 11:43:28 +02:00
|
|
|
folders = [[self clientObject] subFolders];
|
2008-07-09 04:01:32 +02:00
|
|
|
uniqueContacts = [NSMutableDictionary dictionary];
|
2008-07-08 13:32:43 +02:00
|
|
|
for (i = 0; i < [folders count]; i++)
|
|
|
|
{
|
|
|
|
folder = [folders objectAtIndex: i];
|
2008-07-09 09:35:10 +02:00
|
|
|
//NSLog(@" Address book: %@ (%@)", [folder displayName], [folder class]);
|
2008-07-08 13:32:43 +02:00
|
|
|
contacts = [folder lookupContactsWithFilter: searchText
|
|
|
|
sortBy: @"displayName"
|
|
|
|
ordering: NSOrderedAscending];
|
2008-07-09 04:01:32 +02:00
|
|
|
for (j = 0; j < [contacts count]; j++)
|
|
|
|
{
|
|
|
|
contact = [contacts objectAtIndex: j];
|
2008-08-09 21:09:02 +02:00
|
|
|
mail = [contact objectForKey: @"mail"];
|
|
|
|
//NSLog(@" found %@ (%@)", [contact objectForKey: @"displayName"], mail);
|
|
|
|
if ([mail isNotNull] && [uniqueContacts objectForKey: mail] == nil)
|
2008-07-09 09:35:10 +02:00
|
|
|
[uniqueContacts setObject: contact forKey: [contact objectForKey: @"mail"]];
|
2008-07-09 04:01:32 +02:00
|
|
|
}
|
2008-07-04 11:43:28 +02:00
|
|
|
}
|
2008-07-08 13:32:43 +02:00
|
|
|
|
2008-07-04 11:43:28 +02:00
|
|
|
result = [context response];
|
2008-07-09 04:01:32 +02:00
|
|
|
if ([uniqueContacts count] > 0)
|
2008-07-08 13:32:43 +02:00
|
|
|
{
|
|
|
|
// Sort the contacts by display name
|
|
|
|
displayNameDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"displayName"
|
|
|
|
ascending:YES] autorelease];
|
|
|
|
descriptors = [NSArray arrayWithObjects: displayNameDescriptor, nil];
|
2008-07-09 04:01:32 +02:00
|
|
|
sortedContacts = [[uniqueContacts allValues] sortedArrayUsingDescriptors: descriptors];
|
2008-07-08 13:32:43 +02:00
|
|
|
|
|
|
|
[(WOResponse*)result appendContentString: [sortedContacts jsonRepresentation]];
|
|
|
|
}
|
2008-07-04 11:43:28 +02:00
|
|
|
else
|
|
|
|
[(WOResponse*)result setStatus: 404];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
result = [NSException exceptionWithHTTPStatus: 400
|
2008-07-08 13:32:43 +02:00
|
|
|
reason: @"missing 'search' parameter"];
|
|
|
|
|
2008-07-04 11:43:28 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2006-10-26 02:15:12 +02:00
|
|
|
- (id <WOActionResults>) contactSearchAction
|
|
|
|
{
|
|
|
|
NSString *contact;
|
|
|
|
id <WOActionResults> result;
|
2007-05-10 21:01:09 +02:00
|
|
|
LDAPUserManager *um;
|
|
|
|
|
2006-10-26 02:15:12 +02:00
|
|
|
contact = [self queryParameterForKey: @"search"];
|
2007-03-29 19:12:44 +02:00
|
|
|
if ([contact length] > 0)
|
2007-05-10 21:01:09 +02:00
|
|
|
{
|
|
|
|
um = [LDAPUserManager sharedUserManager];
|
|
|
|
result
|
|
|
|
= [self _responseForResults: [um fetchContactsMatching: contact]];
|
|
|
|
}
|
2006-10-26 02:15:12 +02:00
|
|
|
else
|
|
|
|
result = [NSException exceptionWithHTTPStatus: 400
|
2008-07-08 13:32:43 +02:00
|
|
|
reason: @"missing 'search' parameter"];
|
2007-03-29 19:12:44 +02:00
|
|
|
|
2006-10-26 02:15:12 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2007-09-15 00:01:02 +02:00
|
|
|
- (NSArray *) _subFoldersFromFolder: (SOGoParentFolder *) parentFolder
|
2007-03-29 19:12:44 +02:00
|
|
|
{
|
2007-09-15 00:01:02 +02:00
|
|
|
NSMutableArray *folders;
|
|
|
|
NSEnumerator *subfolders;
|
2007-11-07 16:58:43 +01:00
|
|
|
SOGoGCSFolder *currentFolder;
|
2007-09-15 00:01:02 +02:00
|
|
|
NSString *folderName;
|
2007-03-29 19:12:44 +02:00
|
|
|
NSMutableDictionary *currentDictionary;
|
2007-09-15 00:01:02 +02:00
|
|
|
SoSecurityManager *securityManager;
|
2007-03-29 19:12:44 +02:00
|
|
|
|
2007-09-15 00:01:02 +02:00
|
|
|
securityManager = [SoSecurityManager sharedSecurityManager];
|
2007-12-05 00:19:47 +01:00
|
|
|
|
2008-07-08 13:32:43 +02:00
|
|
|
// return (([securityManager validatePermission: SoPerm_AccessContentsInformation
|
|
|
|
// onObject: contactFolder
|
|
|
|
// inContext: context] == nil)
|
2007-09-15 00:01:02 +02:00
|
|
|
|
|
|
|
folders = [NSMutableArray new];
|
|
|
|
[folders autorelease];
|
2007-03-29 19:12:44 +02:00
|
|
|
|
2007-09-15 00:01:02 +02:00
|
|
|
subfolders = [[parentFolder subFolders] objectEnumerator];
|
2007-11-27 22:02:33 +01:00
|
|
|
while ((currentFolder = [subfolders nextObject]))
|
2007-03-29 19:12:44 +02:00
|
|
|
{
|
2007-09-15 00:01:02 +02:00
|
|
|
if (![securityManager validatePermission: SOGoPerm_AccessObject
|
|
|
|
onObject: currentFolder inContext: context])
|
2007-03-29 19:12:44 +02:00
|
|
|
{
|
2007-09-15 00:01:02 +02:00
|
|
|
folderName = [NSString stringWithFormat: @"/%@/%@",
|
|
|
|
[parentFolder nameInContainer],
|
|
|
|
[currentFolder nameInContainer]];
|
|
|
|
currentDictionary
|
|
|
|
= [NSMutableDictionary dictionaryWithCapacity: 3];
|
|
|
|
[currentDictionary setObject: [currentFolder displayName]
|
2007-11-08 20:55:38 +01:00
|
|
|
forKey: @"displayName"];
|
2007-09-15 00:01:02 +02:00
|
|
|
[currentDictionary setObject: folderName forKey: @"name"];
|
|
|
|
[currentDictionary setObject: [currentFolder folderType]
|
|
|
|
forKey: @"type"];
|
|
|
|
[folders addObject: currentDictionary];
|
2007-03-29 19:12:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-15 00:01:02 +02:00
|
|
|
return folders;
|
2007-03-29 19:12:44 +02:00
|
|
|
}
|
|
|
|
|
2007-09-15 00:01:02 +02:00
|
|
|
// - (SOGoContactGCSFolder *) contactFolderForUID: (NSString *) uid
|
|
|
|
// {
|
|
|
|
// SOGoFolder *upperContainer;
|
|
|
|
// SOGoUserFolder *userFolder;
|
|
|
|
// SOGoContactFolders *contactFolders;
|
|
|
|
// SOGoContactGCSFolder *contactFolder;
|
|
|
|
// SoSecurityManager *securityManager;
|
|
|
|
|
|
|
|
// upperContainer = [[[self clientObject] container] container];
|
|
|
|
// userFolder = [SOGoUserFolder objectWithName: uid
|
|
|
|
// inContainer: upperContainer];
|
|
|
|
// contactFolders = [SOGoUserFolder lookupName: @"Contacts"
|
|
|
|
// inContext: context
|
|
|
|
// acquire: NO];
|
|
|
|
// contactFolder = [contactFolders lookupName: @"personal"
|
|
|
|
// inContext: context
|
|
|
|
// acquire: NO];
|
|
|
|
|
|
|
|
// securityManager = [SoSecurityManager sharedSecurityManager];
|
|
|
|
|
|
|
|
// return (([securityManager validatePermission: SoPerm_AccessContentsInformation
|
|
|
|
// onObject: contactFolder
|
|
|
|
// inContext: context] == nil)
|
|
|
|
// ? contactFolder : nil);
|
|
|
|
// }
|
2006-12-20 23:52:32 +01:00
|
|
|
|
2008-02-04 23:22:14 +01:00
|
|
|
- (WOResponse *) saveDragHandleStateAction
|
|
|
|
{
|
|
|
|
WORequest *request;
|
|
|
|
NSString *dragHandle;
|
|
|
|
|
|
|
|
[self _setupContext];
|
|
|
|
request = [context request];
|
|
|
|
|
|
|
|
if ((dragHandle = [request formValueForKey: @"vertical"]) != nil)
|
|
|
|
[moduleSettings setObject: dragHandle
|
|
|
|
forKey: @"DragHandleVertical"];
|
|
|
|
else if ((dragHandle = [request formValueForKey: @"horizontal"]) != nil)
|
|
|
|
[moduleSettings setObject: dragHandle
|
|
|
|
forKey: @"DragHandleHorizontal"];
|
|
|
|
else
|
|
|
|
return [self responseWithStatus: 400];
|
|
|
|
|
|
|
|
[ud synchronize];
|
|
|
|
|
|
|
|
return [self responseWithStatus: 204];
|
|
|
|
}
|
|
|
|
|
2006-08-05 00:51:42 +02:00
|
|
|
@end
|