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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import <Foundation/NSString.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
|
|
|
|
|
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
|
|
|
|
2006-10-04 00:07:56 +02:00
|
|
|
- (id) newAction
|
|
|
|
{
|
|
|
|
return [self _selectActionForApplication: @"new"];
|
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
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (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
|
|
|
|
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];
|
|
|
|
|
|
|
|
// return (([securityManager validatePermission: SoPerm_AccessContentsInformation
|
|
|
|
// onObject: contactFolder
|
|
|
|
// inContext: context] == nil)
|
|
|
|
|
|
|
|
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];
|
|
|
|
currentFolder = [subfolders nextObject];
|
|
|
|
while (currentFolder)
|
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
|
|
|
currentFolder = [subfolders nextObject];
|
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
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *) _foldersForUID: (NSString *) uid
|
|
|
|
ofType: (NSString *) folderType
|
|
|
|
{
|
|
|
|
NSObject *topFolder, *userFolder;
|
2007-09-15 00:01:02 +02:00
|
|
|
SOGoParentFolder *parentFolder;
|
2007-03-29 19:12:44 +02:00
|
|
|
NSMutableArray *folders;
|
|
|
|
|
|
|
|
folders = [NSMutableArray new];
|
|
|
|
[folders autorelease];
|
|
|
|
|
|
|
|
topFolder = [[[self clientObject] container] container];
|
|
|
|
userFolder = [topFolder lookupName: uid inContext: context acquire: NO];
|
|
|
|
|
|
|
|
/* FIXME: should be moved in the SOGo* classes. Maybe by having a SOGoFolderManager. */
|
|
|
|
if ([folderType length] == 0 || [folderType isEqualToString: @"calendar"])
|
|
|
|
{
|
2007-09-15 00:01:02 +02:00
|
|
|
parentFolder = [userFolder lookupName: @"Calendar"
|
|
|
|
inContext: context acquire: NO];
|
|
|
|
[folders
|
|
|
|
addObjectsFromArray: [self _subFoldersFromFolder: parentFolder]];
|
2007-03-29 19:12:44 +02:00
|
|
|
}
|
|
|
|
if ([folderType length] == 0 || [folderType isEqualToString: @"contact"])
|
|
|
|
{
|
2007-09-15 00:01:02 +02:00
|
|
|
parentFolder = [userFolder lookupName: @"Contacts"
|
|
|
|
inContext: context acquire: NO];
|
2007-03-29 19:12:44 +02:00
|
|
|
[folders
|
2007-09-15 00:01:02 +02:00
|
|
|
addObjectsFromArray: [self _subFoldersFromFolder: parentFolder]];
|
2007-03-29 19:12:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return folders;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) _foldersStringForFolders: (NSEnumerator *) folders
|
|
|
|
{
|
|
|
|
NSMutableString *foldersString;
|
|
|
|
NSDictionary *currentFolder;
|
|
|
|
|
|
|
|
foldersString = [NSMutableString new];
|
|
|
|
[foldersString autorelease];
|
|
|
|
|
|
|
|
currentFolder = [folders nextObject];
|
|
|
|
while (currentFolder)
|
|
|
|
{
|
|
|
|
[foldersString appendFormat: @";%@:%@:%@",
|
|
|
|
[currentFolder objectForKey: @"displayName"],
|
|
|
|
[currentFolder objectForKey: @"name"],
|
|
|
|
[currentFolder objectForKey: @"type"]];
|
|
|
|
currentFolder = [folders nextObject];
|
|
|
|
}
|
|
|
|
|
|
|
|
return foldersString;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (WOResponse *) _foldersResponseForResults: (NSArray *) results
|
|
|
|
withType: (NSString *) folderType
|
2006-11-01 21:37:02 +01:00
|
|
|
{
|
|
|
|
WOResponse *response;
|
2007-03-29 19:12:44 +02:00
|
|
|
NSString *uid, *foldersString;
|
|
|
|
NSMutableString *responseString;
|
2007-05-09 21:23:45 +02:00
|
|
|
NSDictionary *contact;
|
|
|
|
NSEnumerator *contacts;
|
2007-03-29 19:12:44 +02:00
|
|
|
NSArray *folders;
|
2006-11-01 21:37:02 +01:00
|
|
|
|
|
|
|
response = [context response];
|
2007-03-29 19:12:44 +02:00
|
|
|
|
|
|
|
if ([results count])
|
|
|
|
{
|
|
|
|
[response setStatus: 200];
|
2007-05-09 21:23:45 +02:00
|
|
|
[response setHeader: @"text/plain; charset=utf-8"
|
2007-03-29 19:12:44 +02:00
|
|
|
forKey: @"Content-Type"];
|
|
|
|
|
|
|
|
responseString = [NSMutableString new];
|
2007-05-09 21:23:45 +02:00
|
|
|
contacts = [results objectEnumerator];
|
|
|
|
contact = [contacts nextObject];
|
|
|
|
while (contact)
|
2007-03-29 19:12:44 +02:00
|
|
|
{
|
2007-05-09 21:23:45 +02:00
|
|
|
uid = [contact objectForKey: @"c_uid"];
|
2007-05-10 21:01:09 +02:00
|
|
|
folders = [self _foldersForUID: uid ofType: folderType];
|
|
|
|
foldersString
|
|
|
|
= [self _foldersStringForFolders: [folders objectEnumerator]];
|
|
|
|
[responseString appendFormat: @"%@:%@:%@%@\n", uid,
|
|
|
|
[contact objectForKey: @"cn"],
|
|
|
|
[contact objectForKey: @"c_email"],
|
|
|
|
foldersString];
|
2007-05-09 21:23:45 +02:00
|
|
|
contact = [contacts nextObject];
|
2007-03-29 19:12:44 +02:00
|
|
|
}
|
|
|
|
[response appendContentString: responseString];
|
|
|
|
[responseString release];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
[response setStatus: 404];
|
2006-11-01 21:37:02 +01:00
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2007-03-29 19:12:44 +02:00
|
|
|
- (id <WOActionResults>) foldersSearchAction
|
|
|
|
{
|
|
|
|
NSString *contact, *folderType;
|
|
|
|
id <WOActionResults> result;
|
2007-05-09 21:23:45 +02:00
|
|
|
LDAPUserManager *um;
|
2007-03-29 19:12:44 +02:00
|
|
|
|
2007-05-09 21:23:45 +02:00
|
|
|
um = [LDAPUserManager sharedUserManager];
|
2007-03-29 19:12:44 +02:00
|
|
|
contact = [self queryParameterForKey: @"search"];
|
|
|
|
if ([contact length] > 0)
|
|
|
|
{
|
|
|
|
folderType = [self queryParameterForKey: @"type"];
|
2007-05-09 21:23:45 +02:00
|
|
|
result
|
|
|
|
= [self _foldersResponseForResults: [um fetchContactsMatching: contact]
|
|
|
|
withType: folderType];
|
2007-03-29 19:12:44 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
result = [NSException exceptionWithHTTPStatus: 400
|
|
|
|
reason: @"missing 'search' parameter"];
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2006-08-05 00:51:42 +02:00
|
|
|
@end
|