2 Copyright (C) 2004-2005 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
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
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.
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
22 #include <NGObjWeb/SoApplication.h>
24 @interface SOGo : SoApplication
26 NSMutableDictionary *localeLUT;
29 - (NSDictionary *)currentLocaleConsideringLanguages:(NSArray *)_langs;
30 - (NSDictionary *)localeForLanguageNamed:(NSString *)_name;
34 #include "SOGoAuthenticator.h"
35 #include "SOGoProductLoader.h"
36 #include <SOGo/SOGoUserFolder.h>
41 static unsigned int vMemSizeLimit = 0;
42 static BOOL doCrashOnSessionCreate = NO;
45 NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
48 doCrashOnSessionCreate = [ud boolForKey:@"SOGoCrashOnSessionCreate"];
50 /* vMem size check - default is 200MB */
52 tmp = [ud objectForKey:@"SxVMemLimit"];
53 vMemSizeLimit = (tmp != nil)
56 if (vMemSizeLimit > 0) {
57 NSLog(@"Note: vmem size check enabled: shutting down app when "
58 @"vmem > %d MB", vMemSizeLimit);
63 if ((self = [super init])) {
64 self->localeLUT = [[NSMutableDictionary alloc] initWithCapacity:2];
65 [[SOGoProductLoader productLoader] loadProducts];
67 #if 0 /* enable, if we added the value class */
68 /* setup some WebDAV type mappings required for Evolution */
69 [EOQualifier registerValueClass:NSClassFromString(@"dateTime")
70 forTypeName:@"dateTime"];
71 [EOQualifier registerValueClass:NSClassFromString(@"dateTime")
72 forTypeName:@"dateTime.tz"];
80 [self->localeLUT release];
86 - (id)authenticatorInContext:(WOContext *)_ctx {
87 return [SOGoAuthenticator sharedSOGoAuthenticator];
92 - (BOOL)isUserName:(NSString *)_key inContext:(id)_ctx {
93 if ([_key length] < 1)
96 if (isdigit([_key characterAtIndex:0]))
102 - (id)lookupUser:(NSString *)_key inContext:(id)_ctx {
103 return [[[SOGoUserFolder alloc] initWithName:_key inContainer:self]
107 - (void)_setupLocaleInContext:(WOContext *)_ctx {
109 NSDictionary *locale;
111 if ([[_ctx valueForKey:@"locale"] isNotNull])
114 langs = [[(WOContext *)_ctx request] browserLanguages];
115 locale = [self currentLocaleConsideringLanguages:langs];
116 [_ctx takeValue:locale forKey:@"locale"];
119 - (id)lookupName:(NSString *)_key inContext:(id)_ctx acquire:(BOOL)_flag {
122 /* put locale info into the context in case it's not there */
123 [self _setupLocaleInContext:_ctx];
125 /* first check attributes directly bound to the application */
126 if ((obj = [super lookupName:_key inContext:_ctx acquire:_flag]))
130 The problem is, that at this point we still get request for resources,
133 Addition: we also get queries for various other methods, like "GET" if
134 no method was provided in the query path.
137 if ([_key isEqualToString:@"favicon.ico"])
140 if ([self isUserName:_key inContext:_ctx])
141 return [self lookupUser:_key inContext:_ctx];
148 - (NSString *)davDisplayName {
149 /* this is used in the UI, eg in the navigation */
153 /* exception handling */
155 - (WOResponse *)handleException:(NSException *)_exc
156 inContext:(WOContext *)_ctx
158 printf("EXCEPTION: %s\n", [[_exc description] cString]);
162 /* runtime maintenance */
164 - (void)checkIfDaemonHasToBeShutdown {
165 unsigned int limit, vmem;
167 if ((limit = vMemSizeLimit) == 0)
170 vmem = [[NSProcessInfo processInfo] virtualMemorySize]/1048576;
174 @"terminating app, vMem size limit (%d MB) has been reached"
175 @" (currently %d MB)",
181 - (WOResponse *)dispatchRequest:(WORequest *)_request {
182 static NSArray *runLoopModes = nil;
185 resp = [super dispatchRequest:_request];
187 if ([self isTerminating])
190 if (runLoopModes == nil)
191 runLoopModes = [[NSArray alloc] initWithObjects:NSDefaultRunLoopMode, nil];
193 // TODO: a bit complicated? (-perform:afterDelay: doesn't work?)
194 [[NSRunLoop currentRunLoop] performSelector:
195 @selector(checkIfDaemonHasToBeShutdown)
196 target:self argument:nil
197 order:1 modes:runLoopModes];
201 /* session management */
203 - (WOSession *)createSessionForRequest:(WORequest *)_request {
204 [self warnWithFormat:@"session creation requested!"];
205 if (doCrashOnSessionCreate)
207 return [super createSessionForRequest:_request];
212 - (NSDictionary *)currentLocaleConsideringLanguages:(NSArray *)_langs {
215 /* assume _langs is ordered by priority */
216 count = [_langs count];
217 for (i = 0; i < count; i++) {
219 NSDictionary *locale;
221 lname = [_langs objectAtIndex:i];
222 locale = [self localeForLanguageNamed:lname];
226 /* no appropriate language, fallback to default */
227 return [self localeForLanguageNamed:@"English"];
230 - (NSDictionary *)localeForLanguageNamed:(NSString *)_name {
233 NSDictionary *locale;
235 if (![_name isNotNull]) {
236 [self logWithFormat:@"ERROR(%s): name parameter must not be nil!",
237 __PRETTY_FUNCTION__];
241 if ((locale = [self->localeLUT objectForKey:_name]) != nil)
244 lpath = [[self resourceManager]
245 pathForResourceNamed:@"Locale"
247 languages:[NSArray arrayWithObject:_name]];
248 if (![lpath isNotNull]) {
249 [self logWithFormat:@"ERROR: did not find Locale for language: %@", _name];
253 if ((data = [NSData dataWithContentsOfFile:lpath]) == nil) {
254 [self logWithFormat:@"%s didn't find locale with name:%@",
259 data = [[[NSString alloc] initWithData:data
260 encoding:NSUTF8StringEncoding] autorelease];
261 locale = [data propertyList];
263 [self logWithFormat:@"%s couldn't load locale with name:%@",
268 [self->localeLUT setObject:locale forKey:_name];