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