]> err.no Git - scalable-opengroupware.org/blob - UI/Common/UIxFolderActions.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1064 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / Common / UIxFolderActions.m
1 /* UIxFolderActions.m - this file is part of SOGo
2  *
3  * Copyright (C) 2007 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/NSDictionary.h>
24 #import <Foundation/NSString.h>
25 #import <Foundation/NSUserDefaults.h>
26 #import <Foundation/NSValue.h>
27
28 #import <NGObjWeb/WOContext.h>
29 #import <NGObjWeb/WOContext+SoObjects.h>
30 #import <NGObjWeb/WORequest.h>
31 #import <NGObjWeb/WOResponse.h>
32 #import <NGObjWeb/SoSecurityManager.h>
33
34 #import <SoObjects/SOGo/LDAPUserManager.h>
35 #import <SoObjects/SOGo/SOGoUser.h>
36 #import <SoObjects/SOGo/SOGoObject.h>
37 #import <SoObjects/SOGo/SOGoPermissions.h>
38
39 #import "UIxFolderActions.h"
40
41 @implementation UIxFolderActions
42
43 #warning some of this code could probably be moved in one of the \
44          clientObject classes...
45
46 - (void) _setupContext
47 {
48   NSString *clientClass;
49   SOGoUser *activeUser;
50
51   activeUser = [context activeUser];
52   login = [activeUser login];
53   clientObject = [self clientObject];
54   owner = [clientObject ownerInContext: nil];
55
56   clientClass = NSStringFromClass([clientObject class]);
57   if ([clientClass isEqualToString: @"SOGoContactGCSFolder"])
58     baseFolder = @"Contacts";
59   else if ([clientClass isEqualToString: @"SOGoAppointmentFolder"])
60     baseFolder = @"Calendar";
61   else
62     baseFolder = nil;
63
64   um = [LDAPUserManager sharedUserManager];
65   ud = [activeUser userSettings];
66   moduleSettings = [ud objectForKey: baseFolder];
67   if (!moduleSettings)
68     {
69       moduleSettings = [NSMutableDictionary new];
70       [moduleSettings autorelease];
71     }
72   [ud setObject: moduleSettings forKey: baseFolder];
73
74   subscriptionPointer = [NSMutableString stringWithFormat: @"%@:%@",
75                                          owner, baseFolder];
76   if ([baseFolder isEqualToString: @"Contacts"])
77     [subscriptionPointer appendFormat: @"/%@",
78                          [clientObject nameInContainer]];
79 }
80
81 - (WOResponse *) _realActionWithFolderName: (NSDictionary *) folderDict
82 {
83   WOResponse *response;
84   NSMutableDictionary *folderSubscription;
85
86   response = [context response];
87   if ([owner isEqualToString: login])
88     {
89       [response setStatus: 403];
90       [response appendContentString:
91                  @"You cannot (un)subscribe to a folder that you own!"];
92     }
93   else
94     {
95       folderSubscription
96         = [moduleSettings objectForKey: @"SubscribedFolders"];
97       if (!folderSubscription)
98         {
99           folderSubscription = [NSMutableDictionary dictionary];
100           [moduleSettings setObject: folderSubscription
101                           forKey: @"SubscribedFolders"];
102         }
103       if (folderDict)
104         [folderSubscription setObject: folderDict
105                             forKey: subscriptionPointer];
106       else
107         [folderSubscription removeObjectForKey: subscriptionPointer];
108
109       [ud synchronize];
110       [response setStatus: 204];
111     }
112
113   return response;
114 }
115
116 - (WOResponse *) subscribeAction
117 {
118   NSString *email;
119   NSMutableDictionary *folderDict;
120   NSString *folderName;
121
122   [self _setupContext];
123   email = [NSString stringWithFormat: @"%@ <%@>",
124                     [um getCNForUID: owner],
125                     [um getEmailForUID: owner]];
126   if ([baseFolder isEqualToString: @"Contacts"])
127     folderName = [NSString stringWithFormat: @"%@ (%@)",
128                            [clientObject nameInContainer], email];
129   else
130     folderName = email;
131
132   folderDict = [NSMutableDictionary dictionary];
133   [folderDict setObject: folderName forKey: @"displayName"];
134   [folderDict setObject: [NSNumber numberWithBool: NO] forKey: @"active"];
135
136   return [self _realActionWithFolderName: folderDict];
137 }
138
139 - (WOResponse *) unsubscribeAction
140 {
141   [self _setupContext];
142
143   return [self _realActionWithFolderName: nil];
144 }
145
146 - (WOResponse *) canAccessContentAction
147 {
148   WOResponse *response;
149   SoSecurityManager *securityManager;
150   BOOL result;
151
152   securityManager = [SoSecurityManager sharedSecurityManager];
153   result = (![securityManager validatePermission: SoPerm_AccessContentsInformation
154                               onObject: [self clientObject]
155                               inContext: context]);
156
157   response = [context response];
158   [response setStatus: 200];
159   [response setHeader: @"text/plain; charset=\"ascii\""
160             forKey: @"content-type"];
161   [response appendContentString: (result) ? @"1" : @"0"];
162
163   return response;
164 }
165
166 - (WOResponse *) _realFolderActivation: (BOOL) makeActive
167 {
168   WOResponse *response;
169   NSMutableDictionary *folderSubscription, *folderDict;
170   NSNumber *active;
171   
172   response = [context response];
173
174   [self _setupContext];
175   active = [NSNumber numberWithBool: makeActive];
176   if ([owner isEqualToString: login])
177     [moduleSettings setObject: active forKey: @"activateUserFolder"];
178   else
179     {
180       folderSubscription
181         = [moduleSettings objectForKey: @"SubscribedFolders"];
182       if (folderSubscription)
183         {
184           folderDict = [folderSubscription objectForKey: subscriptionPointer];
185           if (folderDict)
186             [folderDict setObject: active
187                         forKey: @"active"];
188         }
189     }
190
191   [ud synchronize];
192   [response setStatus: 204];
193
194   return response;
195 }
196
197 - (WOResponse *) activateFolderAction
198 {
199   return [self _realFolderActivation: YES];
200 }
201
202 - (WOResponse *) deactivateFolderAction
203 {
204   return [self _realFolderActivation: NO];
205 }
206
207 @end