]> err.no Git - scalable-opengroupware.org/blob - UI/Contacts/UIxContactFoldersView.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1026 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / Contacts / UIxContactFoldersView.m
1 /* UIxContactFoldersView.m - this file is part of SOGo
2  *
3  * Copyright (C) 2006 Inverse groupe conseil
4  *
5  * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
6  *
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)
10  * any later version.
11  *
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.
16  *
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.
21  */
22
23 #import <Foundation/NSString.h>
24
25 #import <NGObjWeb/NSException+HTTP.h>
26 #import <NGObjWeb/SoObject.h>
27 #import <NGObjWeb/WOResponse.h>
28 #import <NGObjWeb/WOContext.h>
29
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>
35
36 #import "common.h"
37
38 #import "UIxContactFoldersView.h"
39
40 @implementation UIxContactFoldersView
41
42 - (id) _selectActionForApplication: (NSString *) actionName
43 {
44   SOGoContactFolders *folders;
45   NSString *url, *action;
46   WORequest *request;
47
48   folders = [self clientObject];
49   action = [NSString stringWithFormat: @"../%@/%@",
50                      [folders defaultSourceName],
51                      actionName];
52
53   request = [[self context] request];
54
55   url = [[request uri] composeURLWithAction: action
56                        parameters: [self queryParameters]
57                        andHash: NO];
58
59   return [self redirectToLocation: url];
60 }
61
62 - (id) defaultAction
63 {
64   return [self _selectActionForApplication: @"view"];
65 }
66
67 - (id) newAction
68 {
69   return [self _selectActionForApplication: @"new"];
70 }
71
72 - (id <WOActionResults>) newAbAction
73 {
74   id <WOActionResults> response;
75   NSString *name;
76
77   name = [self queryParameterForKey: @"name"];
78   if ([name length] > 0)
79     response = [[self clientObject] newFolderWithName: name];
80   else
81     response = [NSException exceptionWithHTTPStatus: 400
82                             reason: @"The name is missing"];
83   
84   return response;
85 }
86
87 - (id) selectForSchedulerAction
88 {
89   return [self _selectActionForApplication: @"scheduler-contacts"];
90 }
91
92 - (id) selectForMailerAction
93 {
94   return [self _selectActionForApplication: @"mailer-contacts"];
95 }
96
97 - (id) selectForCalendarsAction
98 {
99   return [self _selectActionForApplication: @"calendars-contacts"];
100 }
101
102 - (id) selectForAddressBooksAction
103 {
104   return [self _selectActionForApplication: @"addressbooks-contacts"];
105 }
106
107 - (id) selectForAclsAction
108 {
109   return [self _selectActionForApplication: @"acls-contacts"];
110 }
111
112 - (NSArray *) _searchResults: (NSString *) contact
113 {
114   NSMutableArray *results;
115   SOGoContactFolders *topFolder;
116   NSEnumerator *sogoContactFolders;
117   id <SOGoContactFolder> currentFolder;
118
119   results = [NSMutableArray new];
120   [results autorelease];
121
122   topFolder = [self clientObject];
123   sogoContactFolders = [[topFolder contactFolders] objectEnumerator];
124   currentFolder = [sogoContactFolders nextObject];
125   while (currentFolder)
126     {
127       [results addObjectsFromArray: [currentFolder
128                                       lookupContactsWithFilter: contact
129                                       sortBy: @"cn"
130                                       ordering: NSOrderedAscending]];
131       currentFolder = [sogoContactFolders nextObject];
132     }
133   [topFolder release];
134
135   return results;
136 }
137
138 - (NSString *) _emailForResult: (NSDictionary *) result
139 {
140   NSMutableString *email;
141   NSString *name, *mail;
142
143   email = [NSMutableString string];
144   name = [result objectForKey: @"displayName"];
145   if (![name length])
146     name = [result objectForKey: @"cn"];
147   mail = [result objectForKey: @"mail"];
148   if ([name length])
149     [email appendFormat: @"%@ <%@>", name, mail];
150   else
151     [email appendString: mail];
152
153   return email;
154 }
155
156 - (NSDictionary *) _nextResultWithUid: (NSEnumerator *) results
157 {
158   NSDictionary *result, *possibleResult;
159
160   result = nil;
161   possibleResult = [results nextObject];
162   while (possibleResult && !result)
163     if ([[possibleResult objectForKey: @"c_uid"] length])
164       result = possibleResult;
165     else
166       possibleResult = [results nextObject];
167
168   return result;
169 }
170
171 - (WOResponse *) _responseForResults: (NSArray *) results
172 {
173   WOResponse *response;
174   NSString *email, *responseString;
175   NSDictionary *result;
176
177   response = [context response];
178
179   if ([results count])
180     {
181       result = [self _nextResultWithUid: [results objectEnumerator]];
182       if (!result)
183         result = [results objectAtIndex: 0];
184       email = [self _emailForResult: result];
185       responseString = [NSString stringWithFormat: @"%@:%@",
186                                  [result objectForKey: @"c_uid"],
187                                  email];
188       [response setStatus: 200];
189       [response setHeader: @"text/plain; charset=iso-8859-1"
190                 forKey: @"Content-Type"];
191       [response appendContentString: responseString];
192     }
193   else
194     [response setStatus: 404];
195
196   return response;
197 }
198
199 - (id <WOActionResults>) contactSearchAction
200 {
201   NSString *contact;
202   id <WOActionResults> result;
203
204   contact = [self queryParameterForKey: @"search"];
205   if ([contact length])
206     result = [self _responseForResults: [self _searchResults: contact]];
207   else
208     result = [NSException exceptionWithHTTPStatus: 400
209                           reason: @"missing 'search' parameter"];
210
211   return result;
212 }
213
214 - (id <WOActionResults>) updateAdditionalAddressBooksAction
215 {
216   WOResponse *response;
217   NSUserDefaults *ud;
218
219   ud = [[context activeUser] userDefaults];
220   [ud setObject: [self queryParameterForKey: @"ids"]
221       forKey: @"additionaladdressbooks"];
222   [ud synchronize];
223   response = [context response];
224   [response setStatus: 200];
225   [response setHeader: @"text/html; charset=\"utf-8\"" forKey: @"content-type"];
226
227   return response;
228 }
229
230 - (SOGoContactGCSFolder *) contactFolderForUID: (NSString *) uid
231 {
232   SOGoFolder *upperContainer;
233   SOGoUserFolder *userFolder;
234   SOGoContactFolders *contactFolders;
235   SOGoContactGCSFolder *contactFolder;
236   SoSecurityManager *securityManager;
237
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];
245   [contactFolder
246     setOCSPath: [NSString stringWithFormat: @"/Users/%@/Contacts/personal", uid]];
247   [contactFolder setOwner: uid];
248
249   securityManager = [SoSecurityManager sharedSecurityManager];
250
251   return (([securityManager validatePermission: SoPerm_AccessContentsInformation
252                             onObject: contactFolder
253                             inContext: context] == nil)
254           ? contactFolder : nil);
255 }
256
257 - (id <WOActionResults>) checkRightsAction
258 {
259   WOResponse *response;
260   NSUserDefaults *ud;
261   NSString *uids;
262   NSMutableString *rights;
263   NSArray *ids;
264   unsigned int count, max;
265   BOOL result;
266
267   ud = [[context activeUser] userDefaults];
268   uids = [ud objectForKey: @"additionaladdressbooks"];
269
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)
276     {
277       ids = [uids componentsSeparatedByString: @","];
278       max = [ids count];
279       for (count = 0; count < max; count++)
280         {
281           result = ([self contactFolderForUID: [ids objectAtIndex: count]] != nil);
282           if (count == 0)
283             [rights appendFormat: @"%d", result];
284           else
285             [rights appendFormat: @",%d", result];
286         }
287     }
288   [response appendContentString: rights];
289
290   return response;
291 }
292
293 @end