1 /* UIxContactFoldersView.m - this file is part of SOGo
3 * Copyright (C) 2006 Inverse groupe conseil
5 * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
7 * This file is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * This file is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 #import <Foundation/NSString.h>
25 #import <NGObjWeb/NSException+HTTP.h>
26 #import <NGObjWeb/SoObject.h>
27 #import <NGObjWeb/WOResponse.h>
28 #import <NGObjWeb/WOContext.h>
30 #import <SoObjects/SOGo/SOGoUser.h>
31 #import <SoObjects/SOGo/NSString+Utilities.h>
32 #import <SoObjects/Contacts/SOGoContactFolders.h>
33 #import <SoObjects/Contacts/SOGoContactFolder.h>
34 #import <SoObjects/Contacts/SOGoContactGCSFolder.h>
38 #import "UIxContactFoldersView.h"
40 @implementation UIxContactFoldersView
42 - (id) _selectActionForApplication: (NSString *) actionName
44 SOGoContactFolders *folders;
45 NSString *url, *action;
48 folders = [self clientObject];
49 action = [NSString stringWithFormat: @"../%@/%@",
50 [folders defaultSourceName],
53 request = [[self context] request];
55 url = [[request uri] composeURLWithAction: action
56 parameters: [self queryParameters]
59 return [self redirectToLocation: url];
64 return [self _selectActionForApplication: @"view"];
69 return [self _selectActionForApplication: @"new"];
72 - (id <WOActionResults>) newAbAction
74 id <WOActionResults> response;
77 name = [self queryParameterForKey: @"name"];
78 if ([name length] > 0)
79 response = [[self clientObject] newFolderWithName: name];
81 response = [NSException exceptionWithHTTPStatus: 400
82 reason: @"The name is missing"];
87 - (id) selectForSchedulerAction
89 return [self _selectActionForApplication: @"scheduler-contacts"];
92 - (id) selectForMailerAction
94 return [self _selectActionForApplication: @"mailer-contacts"];
97 - (id) selectForCalendarsAction
99 return [self _selectActionForApplication: @"calendars-contacts"];
102 - (id) selectForAddressBooksAction
104 return [self _selectActionForApplication: @"addressbooks-contacts"];
107 - (id) selectForAclsAction
109 return [self _selectActionForApplication: @"acls-contacts"];
112 - (NSArray *) _searchResults: (NSString *) contact
114 NSMutableArray *results;
115 SOGoContactFolders *topFolder;
116 NSEnumerator *sogoContactFolders;
117 id <SOGoContactFolder> currentFolder;
119 results = [NSMutableArray new];
120 [results autorelease];
122 topFolder = [self clientObject];
123 sogoContactFolders = [[topFolder contactFolders] objectEnumerator];
124 currentFolder = [sogoContactFolders nextObject];
125 while (currentFolder)
127 [results addObjectsFromArray: [currentFolder
128 lookupContactsWithFilter: contact
130 ordering: NSOrderedAscending]];
131 currentFolder = [sogoContactFolders nextObject];
138 - (NSString *) _emailForResult: (NSDictionary *) result
140 NSMutableString *email;
141 NSString *name, *mail;
143 email = [NSMutableString string];
144 name = [result objectForKey: @"displayName"];
146 name = [result objectForKey: @"cn"];
147 mail = [result objectForKey: @"mail"];
149 [email appendFormat: @"%@ <%@>", name, mail];
151 [email appendString: mail];
156 - (NSDictionary *) _nextResultWithUid: (NSEnumerator *) results
158 NSDictionary *result, *possibleResult;
161 possibleResult = [results nextObject];
162 while (possibleResult && !result)
163 if ([[possibleResult objectForKey: @"c_uid"] length])
164 result = possibleResult;
166 possibleResult = [results nextObject];
171 - (WOResponse *) _responseForResults: (NSArray *) results
173 WOResponse *response;
174 NSString *email, *responseString;
175 NSDictionary *result;
177 response = [context response];
181 result = [self _nextResultWithUid: [results objectEnumerator]];
183 result = [results objectAtIndex: 0];
184 email = [self _emailForResult: result];
185 responseString = [NSString stringWithFormat: @"%@:%@",
186 [result objectForKey: @"c_uid"],
188 [response setStatus: 200];
189 [response setHeader: @"text/plain; charset=iso-8859-1"
190 forKey: @"Content-Type"];
191 [response appendContentString: responseString];
194 [response setStatus: 404];
199 - (id <WOActionResults>) contactSearchAction
202 id <WOActionResults> result;
204 contact = [self queryParameterForKey: @"search"];
205 if ([contact length])
206 result = [self _responseForResults: [self _searchResults: contact]];
208 result = [NSException exceptionWithHTTPStatus: 400
209 reason: @"missing 'search' parameter"];
214 - (id <WOActionResults>) updateAdditionalAddressBooksAction
216 WOResponse *response;
219 ud = [[context activeUser] userDefaults];
220 [ud setObject: [self queryParameterForKey: @"ids"]
221 forKey: @"additionaladdressbooks"];
223 response = [context response];
224 [response setStatus: 200];
225 [response setHeader: @"text/html; charset=\"utf-8\"" forKey: @"content-type"];
230 - (SOGoContactGCSFolder *) contactFolderForUID: (NSString *) uid
232 SOGoFolder *upperContainer;
233 SOGoUserFolder *userFolder;
234 SOGoContactFolders *contactFolders;
235 SOGoContactGCSFolder *contactFolder;
236 SoSecurityManager *securityManager;
238 upperContainer = [[[self clientObject] container] container];
239 userFolder = [SOGoUserFolder objectWithName: uid
240 inContainer: upperContainer];
241 contactFolders = [SOGoContactFolders objectWithName: @"Contacts"
242 inContainer: userFolder];
243 contactFolder = [SOGoContactGCSFolder objectWithName: @"personal"
244 inContainer: contactFolders];
246 setOCSPath: [NSString stringWithFormat: @"/Users/%@/Contacts/personal", uid]];
247 [contactFolder setOwner: uid];
249 securityManager = [SoSecurityManager sharedSecurityManager];
251 return (([securityManager validatePermission: SoPerm_AccessContentsInformation
252 onObject: contactFolder
253 inContext: context] == nil)
254 ? contactFolder : nil);
257 - (id <WOActionResults>) checkRightsAction
259 WOResponse *response;
262 NSMutableString *rights;
264 unsigned int count, max;
267 ud = [[context activeUser] userDefaults];
268 uids = [ud objectForKey: @"additionaladdressbooks"];
270 response = [context response];
271 [response setStatus: 200];
272 [response setHeader: @"text/plain; charset=\"utf-8\""
273 forKey: @"content-type"];
274 rights = [NSMutableString string];
275 if ([uids length] > 0)
277 ids = [uids componentsSeparatedByString: @","];
279 for (count = 0; count < max; count++)
281 result = ([self contactFolderForUID: [ids objectAtIndex: count]] != nil);
283 [rights appendFormat: @"%d", result];
285 [rights appendFormat: @",%d", result];
288 [response appendContentString: rights];