]> err.no Git - sope/blob - sope-appserver/samples/iCalPortal/iCalPortalPage.m
fixed copyrights for 2005
[sope] / sope-appserver / samples / iCalPortal / iCalPortalPage.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
6   SOPE 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   SOPE 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 SOPE; 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 "iCalPortalPage.h"
23 #include "common.h"
24
25 @interface iCalLabelDispatcher : NSObject
26 {
27   iCalPortalPage *component;
28 }
29
30 - (id)initWithComponent:(iCalPortalPage *)_comp;
31
32 @end
33
34 @implementation iCalPortalPage
35
36 + (int)version {
37   return [super version] + 0 /* v2 */;
38 }
39 + (void)initialize {
40   NSAssert2([super version] == 2,
41             @"invalid superclass (%@) version %i !",
42             NSStringFromClass([self superclass]), [super version]);
43 }
44
45 - (void)dealloc {
46   [super dealloc];
47 }
48
49 /* accessors */
50
51 - (iCalPortalDatabase *)database {
52   return [(id)[self application] database];
53 }
54
55 - (iCalPortalUser *)user {
56   if (![self hasSession])
57     return nil;
58   
59   return [[self session] objectForKey:@"user"];
60 }
61
62 /* actions */
63
64 - (BOOL)isSessionProtectedPage {
65   return YES;
66 }
67
68 - (id<WOActionResults>)indexPage {
69   NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
70   WOSession *sn;
71   
72   if ((sn = [self existingSession])) {
73     [sn removeObjectForKey:@"user"];
74     [sn terminate];
75   }
76   
77   if ([[ud objectForKey:@"DevMode"] boolValue]) {
78     return [[self pageWithName:@"iCalPortalWelcomePage"] performPage];
79   }
80   else {
81     WOResponse *r;
82     
83     r = [WOResponse responseWithRequest:[[self context] request]];
84     
85     [r setStatus:302];
86     [r setHeader:@"/en/index.xhtml" forKey:@"location"];
87     
88     return r;
89   }
90 }
91
92 - (id)performPage {
93   id result;
94   
95   if ([self isSessionProtectedPage]) {
96     if (![self hasSession]) {
97       [self logWithFormat:
98               @"tried to access login protected page without session !"];
99       return [self indexPage];
100     }
101   }
102   
103   result = [self run];
104   
105   return result;
106 }
107
108 - (id)run {
109   return self;
110 }
111
112 /* labels */
113
114 - (id)label {
115   return [[[iCalLabelDispatcher alloc] initWithComponent:self] autorelease];
116 }
117
118 - (NSString *)stringForKey:(NSString *)_key {
119   NSString *s;
120   NSArray  *langs;
121
122   langs = [self hasSession]
123     ? [[self session] languages]
124     : [[[self context] request] browserLanguages];
125   
126   s = [[self resourceManager] 
127              stringForKey:_key inTableNamed:@"main" withDefaultValue:_key
128              languages:langs];
129   return s;
130 }
131
132 - (NSString *)localizedTitle {
133   return [self stringForKey:[self name]];
134 }
135
136 @end /* iCalPortalPage */
137
138 @implementation iCalLabelDispatcher
139
140 - (id)initWithComponent:(iCalPortalPage *)_comp {
141   self->component = _comp;
142   return self;
143 }
144
145 - (id)valueForKey:(NSString *)_key {
146   if ([_key length] == 0)
147     return _key;
148   
149   return [self->component stringForKey:_key];
150 }
151
152 @end /* iCalLabelDispatcher */