1 /* UIxFolderActions.m - this file is part of SOGo
3 * Copyright (C) 2007 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/NSDictionary.h>
24 #import <Foundation/NSString.h>
25 #import <Foundation/NSUserDefaults.h>
26 #import <Foundation/NSValue.h>
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>
34 #import <SoObjects/SOGo/LDAPUserManager.h>
35 #import <SoObjects/SOGo/SOGoUser.h>
36 #import <SoObjects/SOGo/SOGoObject.h>
37 #import <SoObjects/SOGo/SOGoPermissions.h>
39 #import "UIxFolderActions.h"
41 @implementation UIxFolderActions
43 #warning some of this code could probably be moved in one of the \
44 clientObject classes...
46 - (void) _setupContext
48 NSString *clientClass;
51 activeUser = [context activeUser];
52 login = [activeUser login];
53 clientObject = [self clientObject];
54 owner = [clientObject ownerInContext: nil];
56 clientClass = NSStringFromClass([clientObject class]);
57 if ([clientClass isEqualToString: @"SOGoContactGCSFolder"])
58 baseFolder = @"Contacts";
59 else if ([clientClass isEqualToString: @"SOGoAppointmentFolder"])
60 baseFolder = @"Calendar";
64 um = [LDAPUserManager sharedUserManager];
65 ud = [activeUser userSettings];
66 moduleSettings = [ud objectForKey: baseFolder];
69 moduleSettings = [NSMutableDictionary new];
70 [moduleSettings autorelease];
72 [ud setObject: moduleSettings forKey: baseFolder];
74 subscriptionPointer = [NSMutableString stringWithFormat: @"%@:%@",
76 if ([baseFolder isEqualToString: @"Contacts"])
77 [subscriptionPointer appendFormat: @"/%@",
78 [clientObject nameInContainer]];
81 - (WOResponse *) _realActionWithFolderName: (NSDictionary *) folderDict
84 NSMutableDictionary *folderSubscription;
86 response = [context response];
87 if ([owner isEqualToString: login])
89 [response setStatus: 403];
90 [response appendContentString:
91 @"You cannot (un)subscribe to a folder that you own!"];
96 = [moduleSettings objectForKey: @"SubscribedFolders"];
97 if (!folderSubscription)
99 folderSubscription = [NSMutableDictionary dictionary];
100 [moduleSettings setObject: folderSubscription
101 forKey: @"SubscribedFolders"];
104 [folderSubscription setObject: folderDict
105 forKey: subscriptionPointer];
107 [folderSubscription removeObjectForKey: subscriptionPointer];
110 [response setStatus: 204];
116 - (WOResponse *) subscribeAction
119 NSMutableDictionary *folderDict;
120 NSString *folderName;
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];
132 folderDict = [NSMutableDictionary dictionary];
133 [folderDict setObject: folderName forKey: @"displayName"];
134 [folderDict setObject: [NSNumber numberWithBool: NO] forKey: @"active"];
136 return [self _realActionWithFolderName: folderDict];
139 - (WOResponse *) unsubscribeAction
141 [self _setupContext];
143 return [self _realActionWithFolderName: nil];
146 - (WOResponse *) canAccessContentAction
148 WOResponse *response;
149 SoSecurityManager *securityManager;
152 securityManager = [SoSecurityManager sharedSecurityManager];
153 result = (![securityManager validatePermission: SoPerm_AccessContentsInformation
154 onObject: [self clientObject]
155 inContext: context]);
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"];
166 - (WOResponse *) _realFolderActivation: (BOOL) makeActive
168 WOResponse *response;
169 NSMutableDictionary *folderSubscription, *folderDict;
172 response = [context response];
174 [self _setupContext];
175 active = [NSNumber numberWithBool: makeActive];
176 if ([owner isEqualToString: login])
177 [moduleSettings setObject: active forKey: @"activateUserFolder"];
181 = [moduleSettings objectForKey: @"SubscribedFolders"];
182 if (folderSubscription)
184 folderDict = [folderSubscription objectForKey: subscriptionPointer];
186 [folderDict setObject: active
192 [response setStatus: 204];
197 - (WOResponse *) activateFolderAction
199 return [self _realFolderActivation: YES];
202 - (WOResponse *) deactivateFolderAction
204 return [self _realFolderActivation: NO];