]> err.no Git - scalable-opengroupware.org/blob - UI/MainUI/SOGoUserHomePage.m
copied Main as MainUI to UI/
[scalable-opengroupware.org] / UI / MainUI / SOGoUserHomePage.m
1 /*
2   Copyright (C) 2004-2005 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
5
6   OGo is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with OGo; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #include <NGObjWeb/SoComponent.h>
23
24 @interface SOGoUserHomePage : SoComponent
25 {
26   id item;
27 }
28
29 - (NSString *)ownPath;
30 - (NSString *)userFolderPath;
31 - (NSString *)relativePathToUserFolderSubPath:(NSString *)_sub;
32
33 - (NSString *)relativeCalendarPath;
34 - (NSString *)relativeContactsPath;
35 - (NSString *)relativeMailPath;
36   
37 @end
38
39 #include <SOGo/AgenorUserManager.h>
40 #include <SOGo/WOContext+Agenor.h>
41 #include <SOGo/SOGoUser.h>
42 #include "common.h"
43
44 @implementation SOGoUserHomePage
45
46 static NSArray *internetAccessStates = nil;
47
48 + (void)initialize {
49   static BOOL didInit = NO;
50   
51   if (didInit) return;
52   didInit = YES;
53   
54   internetAccessStates = [[NSArray alloc] initWithObjects:@"0", @"1", nil];
55 }
56
57 - (void)dealloc {
58   [self->item release];
59   [super dealloc];
60 }
61
62 /* lookup */
63
64 - (id)lookupName:(NSString *)_key inContext:(id)_ctx acquire:(BOOL)_flag {
65   // Note: we do no acquisition
66   id obj;
67   
68   /* first check attributes directly bound to the object */
69   if ((obj = [super lookupName:_key inContext:_ctx acquire:NO]))
70     return obj;
71   
72   return nil;
73 }
74
75 /* accessors */
76
77 - (void)setItem:(id)_item {
78   ASSIGN(self->item, _item);
79 }
80 - (id)item {
81   return self->item;
82 }
83
84 - (NSArray *)internetAccessStates {
85   return internetAccessStates;
86 }
87
88 - (NSString *)internetAccessState {
89   NSUserDefaults *ud;
90   NSNumber       *value;
91
92   ud    = [[[self context] activeUser] userDefaults];
93   value = [ud objectForKey:@"allowinternet"];
94   return [NSString stringWithFormat:@"%d", [value boolValue]];
95 }
96 - (void)setInternetAccessState:(NSString *)_internetAccessState {
97   NSUserDefaults *ud;
98   
99   ud = [[[self context] activeUser] userDefaults];
100   [ud setObject:_internetAccessState forKey:@"allowinternet"];
101   [ud synchronize];
102 }
103
104 - (NSString *)itemInternetAccessStateText {
105   NSString *key;
106   
107   key = [NSString stringWithFormat:@"internetAccessState_%@", self->item];
108   return key;
109 }
110
111 /* paths */
112
113 - (NSString *)ownPath {
114   NSString *uri;
115   NSRange  r;
116   
117   uri = [[[self context] request] uri];
118   
119   /* first: cut off query parameters */
120   
121   r = [uri rangeOfString:@"?" options:NSBackwardsSearch];
122   if (r.length > 0)
123     uri = [uri substringToIndex:r.location];
124   return uri;
125 }
126
127 - (NSString *)userFolderPath {
128   WOContext *ctx;
129   NSArray   *traversalObjects;
130   NSString  *url;
131   
132   ctx = [self context];
133   traversalObjects = [ctx objectTraversalStack];
134   url = [[traversalObjects objectAtIndex:1]
135                            baseURLInContext:ctx];
136   return [[NSURL URLWithString:url] path];
137 }
138
139 - (NSString *)relativePathToUserFolderSubPath:(NSString *)_sub {
140   NSString *dst, *rel;
141   
142   dst = [[self userFolderPath] stringByAppendingPathComponent:_sub];
143   rel = [dst urlPathRelativeToPath:[self ownPath]];
144   return rel;
145 }
146
147 - (NSString *)relativeCalendarPath {
148   return [self relativePathToUserFolderSubPath:@"Calendar/"];
149 }
150
151 - (NSString *)relativeContactsPath {
152   return [self relativePathToUserFolderSubPath:@"Contacts/"];
153 }
154
155 - (NSString *)relativeMailPath {
156   return [self relativePathToUserFolderSubPath:@"Mail/"];
157 }
158
159 /* objects */
160
161 - (id)calendarFolder {
162   return [[self clientObject] lookupName:@"Calendar"
163                               inContext:[self context]
164                               acquire:NO];
165 }
166
167 /* checking access */
168
169 - (BOOL)canAccess {
170   WOContext *ctx;
171   NSString  *owner;
172
173   ctx   = [self context];
174   owner = [[self clientObject] ownerInContext:ctx];
175   return [owner isEqualToString:[[ctx activeUser] login]];
176 }
177
178 - (BOOL)isNotAllowedToChangeInternetAccess {
179   // TODO: should be a SOGoUser method
180   AgenorUserManager *um;
181   WOContext         *ctx;
182   NSString          *uid;
183
184   ctx = [self context];
185   /* do not allow changes when access is from Internet */
186   if (![ctx isAccessFromIntranet])
187     return YES;
188   um  = [AgenorUserManager sharedUserManager];
189   uid = [[ctx activeUser] login];
190   return [um isUserAllowedToChangeSOGoInternetAccess:uid] ? NO : YES;
191 }
192
193 - (BOOL)isVacationMessageEnabledForInternet {
194   // TODO: should be a SOGoUser method
195   AgenorUserManager *um;
196   NSString          *uid;
197
198   um  = [AgenorUserManager sharedUserManager];
199   uid = [[[self context] activeUser] login];
200   return [um isInternetAutoresponderEnabledForUser:uid];
201 }
202
203 - (BOOL)isVacationMessageEnabledForIntranet {
204   // TODO: should be a SOGoUser method
205   AgenorUserManager *um;
206   NSString          *uid;
207   
208   um  = [AgenorUserManager sharedUserManager];
209   uid = [[[self context] activeUser] login];
210   return [um isIntranetAutoresponderEnabledForUser:uid];
211 }
212
213 /* actions */
214
215 #if 0
216 - (id)defaultAction {
217   return [self redirectToLocation:[self relativeCalendarPath]];
218 }
219 #endif
220
221 /* this is triggered by an XMLHTTPRequest */
222 - (id)saveInternetAccessStateAction {
223   NSString *state;
224   
225   if ([self isNotAllowedToChangeInternetAccess])
226     return [NSException exceptionWithHTTPStatus:403 /* Forbidden */];
227
228   state = [[[self context] request] formValueForKey:@"allowinternet"];
229   [self setInternetAccessState:state];
230   return [NSException exceptionWithHTTPStatus:200 /* OK */];
231 }
232
233 @end /* SOGoUserHomePage */