]> err.no Git - scalable-opengroupware.org/blob - SOGo/Main/SOGoUserHomePage.m
implemented homepage (almost done), removed 'schedule' tab
[scalable-opengroupware.org] / SOGo / Main / 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 "SOGoUser.h"
41 #include "common.h"
42
43 @implementation SOGoUserHomePage
44
45 static NSArray *internetAccessStates = nil;
46
47 + (void)initialize {
48   static BOOL didInit = NO;
49   
50   if (didInit) return;
51   didInit = YES;
52   
53   internetAccessStates = [[NSArray alloc] initWithObjects:@"0", @"1", nil];
54 }
55
56 - (void)dealloc {
57   [self->item release];
58   [super dealloc];
59 }
60
61 /* lookup */
62
63 - (id)lookupName:(NSString *)_key inContext:(id)_ctx acquire:(BOOL)_flag {
64   // Note: we do no acquisition
65   id obj;
66   
67   /* first check attributes directly bound to the object */
68   if ((obj = [super lookupName:_key inContext:_ctx acquire:NO]))
69     return obj;
70   
71   return nil;
72 }
73
74 /* accessors */
75
76 - (void)setItem:(id)_item {
77   ASSIGN(self->item, _item);
78 }
79 - (id)item {
80   return self->item;
81 }
82
83 - (NSArray *)internetAccessStates {
84   return internetAccessStates;
85 }
86
87 - (NSString *)internetAccessState {
88   NSUserDefaults *ud;
89   NSNumber       *value;
90
91   ud    = [[[self context] activeUser] userDefaults];
92   value = [ud objectForKey:@"allowinternet"];
93   return [NSString stringWithFormat:@"%d", [value boolValue]];
94 }
95 - (void)setInternetAccessState:(NSString *)_internetAccessState {
96   NSUserDefaults *ud;
97   
98   ud = [[[self context] activeUser] userDefaults];
99   [ud setObject:_internetAccessState forKey:@"allowinternet"];
100   [ud synchronize];
101 }
102
103 - (NSString *)itemInternetAccessStateText {
104   NSString *key;
105   
106   key = [NSString stringWithFormat:@"internetAccessState_%@", self->item];
107   return key;
108 }
109
110 /* paths */
111
112 - (NSString *)ownPath {
113   NSString *uri;
114   NSRange  r;
115   
116   uri = [[[self context] request] uri];
117   
118   /* first: cut off query parameters */
119   
120   r = [uri rangeOfString:@"?" options:NSBackwardsSearch];
121   if (r.length > 0)
122     uri = [uri substringToIndex:r.location];
123   return uri;
124 }
125
126 - (NSString *)userFolderPath {
127   WOContext *ctx;
128   NSArray   *traversalObjects;
129   NSString  *url;
130   
131   ctx = [self context];
132   traversalObjects = [ctx objectTraversalStack];
133   url = [[traversalObjects objectAtIndex:1]
134                            baseURLInContext:ctx];
135   return [[NSURL URLWithString:url] path];
136 }
137
138 - (NSString *)relativePathToUserFolderSubPath:(NSString *)_sub {
139   NSString *dst, *rel;
140   
141   dst = [[self userFolderPath] stringByAppendingPathComponent:_sub];
142   rel = [dst urlPathRelativeToPath:[self ownPath]];
143   return rel;
144 }
145
146 - (NSString *)relativeCalendarPath {
147   return [self relativePathToUserFolderSubPath:@"Calendar/"];
148 }
149
150 - (NSString *)relativeContactsPath {
151   return [self relativePathToUserFolderSubPath:@"Contacts/"];
152 }
153
154 - (NSString *)relativeMailPath {
155   return [self relativePathToUserFolderSubPath:@"Mail/"];
156 }
157
158 /* objects */
159
160 - (id)calendarFolder {
161   return [[self clientObject] lookupName:@"Calendar"
162                               inContext:[self context]
163                               acquire:NO];
164 }
165
166 /* checking access */
167
168 - (BOOL)canAccess {
169   WOContext *ctx;
170   NSString  *owner;
171
172   ctx   = [self context];
173   owner = [[self clientObject] ownerInContext:ctx];
174   return [owner isEqualToString:[[ctx activeUser] login]];
175 }
176
177 - (BOOL)isNotAllowedToChangeInternetAccess {
178   AgenorUserManager *um;
179   WOContext         *ctx;
180   NSString          *uid;
181
182   ctx = [self context];
183   /* do not allow changes when access is from Internet */
184   if (![ctx isAccessFromIntranet])
185     return YES;
186   um  = [AgenorUserManager sharedUserManager];
187   uid = [[ctx activeUser] login];
188   return [um isUserAllowedToChangeSOGoInternetAccess:uid] ? NO : YES;
189 }
190
191 - (BOOL)isVacationMessageEnabledForInternet {
192   return NO;
193 }
194
195 - (BOOL)isVacationMessageEnabledForIntranet {
196   return YES;
197 }
198
199 /* actions */
200
201 #if 0
202 - (id)defaultAction {
203   return [self redirectToLocation:[self relativeCalendarPath]];
204 }
205 #endif
206
207 /* this is triggered by an XMLHTTPRequest */
208 - (id)saveInternetAccessStateAction {
209   NSString *state;
210   
211   if ([self isNotAllowedToChangeInternetAccess])
212     return [NSException exceptionWithHTTPStatus:403 /* Forbidden */];
213
214   state = [[[self context] request] formValueForKey:@"allowinternet"];
215   [self setInternetAccessState:state];
216   return [NSException exceptionWithHTTPStatus:200 /* OK */];
217 }
218
219 @end /* SOGoUserHomePage */