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 "SOGoProductLoader.h"
35 #include <SOGo/SOGoAuthenticator.h>
36 #include <WEExtensions/WEResourceManager.h>
37 #include <SOGo/SOGoUserFolder.h>
42 static unsigned int vMemSizeLimit = 0;
43 static BOOL doCrashOnSessionCreate = NO;
46 NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
49 doCrashOnSessionCreate = [ud boolForKey:@"SOGoCrashOnSessionCreate"];
51 /* vMem size check - default is 200MB */
53 tmp = [ud objectForKey:@"SxVMemLimit"];
54 vMemSizeLimit = (tmp != nil)
57 if (vMemSizeLimit > 0) {
58 NSLog(@"Note: vmem size check enabled: shutting down app when "
59 @"vmem > %d MB", vMemSizeLimit);
61 #if LIB_FOUNDATION_LIBRARY
62 if ([ud boolForKey:@"SOGoEnableDoubleReleaseCheck"])
63 [NSAutoreleasePool enableDoubleReleaseCheck:YES];
68 if ((self = [super init])) {
69 WOResourceManager *rm;
71 self->localeLUT = [[NSMutableDictionary alloc] initWithCapacity:2];
72 [[SOGoProductLoader productLoader] loadProducts];
74 rm = [[WEResourceManager alloc] init];
75 [self setResourceManager:rm];
81 [self->localeLUT release];
87 - (id)authenticatorInContext:(WOContext *)_ctx {
88 return [SOGoAuthenticator sharedSOGoAuthenticator];
93 - (BOOL)isUserName:(NSString *)_key inContext:(id)_ctx {
94 if ([_key length] < 1)
97 if (isdigit([_key characterAtIndex:0]))
103 - (id)lookupUser:(NSString *)_key inContext:(id)_ctx {
104 return [[[NSClassFromString(@"SOGoUserFolder") alloc]
105 initWithName:_key inContainer:self] autorelease];
108 - (void)_setupLocaleInContext:(WOContext *)_ctx {
110 NSDictionary *locale;
112 if ([[_ctx valueForKey:@"locale"] isNotNull])
115 langs = [[(WOContext *)_ctx request] browserLanguages];
116 locale = [self currentLocaleConsideringLanguages:langs];
117 [_ctx takeValue:locale forKey:@"locale"];
120 - (id)lookupName:(NSString *)_key inContext:(id)_ctx acquire:(BOOL)_flag {
123 /* put locale info into the context in case it's not there */
124 [self _setupLocaleInContext:_ctx];
126 /* first check attributes directly bound to the application */
127 if ((obj = [super lookupName:_key inContext:_ctx acquire:_flag]))
131 The problem is, that at this point we still get request for resources,
134 Addition: we also get queries for various other methods, like "GET" if
135 no method was provided in the query path.
138 if ([_key isEqualToString:@"favicon.ico"])
141 if ([self isUserName:_key inContext:_ctx])
142 return [self lookupUser:_key inContext:_ctx];
149 - (NSString *)davDisplayName {
150 /* this is used in the UI, eg in the navigation */
154 /* exception handling */
156 - (WOResponse *)handleException:(NSException *)_exc
157 inContext:(WOContext *)_ctx
159 printf("EXCEPTION: %s\n", [[_exc description] cString]);
163 /* runtime maintenance */
165 - (void)checkIfDaemonHasToBeShutdown {
166 unsigned int limit, vmem;
168 if ((limit = vMemSizeLimit) == 0)
171 vmem = [[NSProcessInfo processInfo] virtualMemorySize]/1048576;
175 @"terminating app, vMem size limit (%d MB) has been reached"
176 @" (currently %d MB)",
182 - (WOResponse *)dispatchRequest:(WORequest *)_request {
183 static NSArray *runLoopModes = nil;
186 resp = [super dispatchRequest:_request];
188 if ([self isTerminating])
191 if (runLoopModes == nil)
192 runLoopModes = [[NSArray alloc] initWithObjects:NSDefaultRunLoopMode, nil];
194 // TODO: a bit complicated? (-perform:afterDelay: doesn't work?)
195 [[NSRunLoop currentRunLoop] performSelector:
196 @selector(checkIfDaemonHasToBeShutdown)
197 target:self argument:nil
198 order:1 modes:runLoopModes];
202 /* session management */
204 - (WOSession *)createSessionForRequest:(WORequest *)_request {
205 [self warnWithFormat:@"session creation requested!"];
206 if (doCrashOnSessionCreate)
208 return [super createSessionForRequest:_request];
213 - (NSDictionary *)currentLocaleConsideringLanguages:(NSArray *)_langs {
216 /* assume _langs is ordered by priority */
217 count = [_langs count];
218 for (i = 0; i < count; i++) {
220 NSDictionary *locale;
222 lname = [_langs objectAtIndex:i];
223 locale = [self localeForLanguageNamed:lname];
227 /* no appropriate language, fallback to default */
228 return [self localeForLanguageNamed:@"English"];
231 - (NSString *)pathToLocaleForLanguageNamed:(NSString *)_name {
232 static Class MainProduct = Nil;
235 lpath = [[self resourceManager] pathForResourceNamed:@"Locale"
237 languages:[NSArray arrayWithObject:_name]];
238 if ([lpath isNotNull])
241 if (MainProduct == Nil) {
242 if ((MainProduct = NSClassFromString(@"MainUIProduct")) == Nil)
243 [self errorWithFormat:@"did not find MainUIProduct class!"];
246 lpath = [(id)MainProduct pathToLocaleForLanguageNamed:_name];
247 if ([lpath isNotNull])
253 - (NSDictionary *)localeForLanguageNamed:(NSString *)_name {
256 NSDictionary *locale;
258 if (![_name isNotNull]) {
259 [self errorWithFormat:@"%s: name parameter must not be nil!",
260 __PRETTY_FUNCTION__];
264 if ((locale = [self->localeLUT objectForKey:_name]) != nil)
267 if ((lpath = [self pathToLocaleForLanguageNamed:_name]) == nil) {
268 [self errorWithFormat:@"did not find Locale for language: %@", _name];
272 if ((data = [NSData dataWithContentsOfFile:lpath]) == nil) {
273 [self logWithFormat:@"%s didn't find locale with name:%@",
278 data = [[[NSString alloc] initWithData:data
279 encoding:NSUTF8StringEncoding] autorelease];
280 locale = [data propertyList];
282 [self logWithFormat:@"%s couldn't load locale with name:%@",
287 [self->localeLUT setObject:locale forKey:_name];
291 /* name (used by the WEResourceManager) */