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