2 Copyright (C) 2004 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
23 #include <NGObjWeb/SoApplication.h>
25 @interface SOGo : SoApplication
27 NSMutableDictionary *localeLUT;
30 - (NSDictionary *)currentLocaleConsideringLanguages:(NSArray *)_langs;
31 - (NSDictionary *)localeForLanguageNamed:(NSString *)_name;
35 #include "SOGoAuthenticator.h"
36 #include "SOGoProductLoader.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);
64 if ((self = [super init])) {
65 self->localeLUT = [[NSMutableDictionary alloc] initWithCapacity:2];
66 [[SOGoProductLoader productLoader] loadProducts];
68 #if 0 /* enable, if we added the value class */
69 /* setup some WebDAV type mappings required for Evolution */
70 [EOQualifier registerValueClass:NSClassFromString(@"dateTime")
71 forTypeName:@"dateTime"];
72 [EOQualifier registerValueClass:NSClassFromString(@"dateTime")
73 forTypeName:@"dateTime.tz"];
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 [[[SOGoUserFolder alloc] initWithName:_key inContainer:self]
108 - (id)lookupName:(NSString *)_key inContext:(id)_ctx acquire:(BOOL)_flag {
111 /* put locale info into the context in case it's not there */
112 if (![_ctx valueForKey:@"locale"]) {
114 NSDictionary *locale;
116 langs = [[(WOContext *)_ctx request] browserLanguages];
117 locale = [self currentLocaleConsideringLanguages:langs];
118 [_ctx takeValue:locale forKey:@"locale"];
121 #if 0 // znek: explain why you did that?
123 if ([_ctx valueForKey:@"SoRootURL"] == nil) {
124 [_ctx takeValue:@"/sogod.woa/so/" forKey:@"SoRootURL"];
128 /* first check attributes directly bound to the application */
129 if ((obj = [super lookupName:_key inContext:_ctx acquire:_flag]))
133 The problem is, that at this point we still get request for resources,
135 The hack here is to check for a dot in the key, but we should find a way
136 to catch that in a more sensible way.
138 One way to check for a valid key would be to check whether the key is a
139 valid hostname, but I would like to avoid that for performance reasons.
141 Addition: we also get queries for various other methods, like "GET" if
142 no method was provided in the query path.
144 if ([self isUserName:_key inContext:_ctx])
145 return [self lookupUser:_key inContext:_ctx];
152 - (NSString *)davDisplayName {
153 /* this is used in the UI, eg in the navigation */
157 /* exception handling */
159 - (WOResponse *)handleException:(NSException *)_exc
160 inContext:(WOContext *)_ctx
162 printf("EXCEPTION: %s\n", [[_exc description] cString]);
166 /* runtime maintenance */
168 - (void)checkIfDaemonHasToBeShutdown {
169 unsigned int limit, vmem;
171 if ((limit = vMemSizeLimit) == 0)
174 vmem = [[NSProcessInfo processInfo] virtualMemorySize]/1048576;
178 @"terminating app, vMem size limit (%d MB) has been reached"
179 @" (currently %d MB)",
185 - (WOResponse *)dispatchRequest:(WORequest *)_request {
186 static NSArray *runLoopModes = nil;
189 resp = [super dispatchRequest:_request];
191 if ([self isTerminating])
194 if (runLoopModes == nil)
195 runLoopModes = [[NSArray alloc] initWithObjects:NSDefaultRunLoopMode, nil];
197 // TODO: a bit complicated? (-perform:afterDelay: doesn't work?)
198 [[NSRunLoop currentRunLoop] performSelector:
199 @selector(checkIfDaemonHasToBeShutdown)
200 target:self argument:nil
201 order:1 modes:runLoopModes];
205 /* session management */
207 - (WOSession *)createSessionForRequest:(WORequest *)_request {
208 [self logWithFormat:@"WARNING: session creation requested!"];
209 if (doCrashOnSessionCreate)
211 return [super createSessionForRequest:_request];
216 - (NSDictionary *)currentLocaleConsideringLanguages:(NSArray *)_langs {
219 /* assume _langs is ordered by priority */
220 count = [_langs count];
221 for (i = 0; i < count; i++) {
223 NSDictionary *locale;
225 lname = [_langs objectAtIndex:i];
226 locale = [self localeForLanguageNamed:lname];
230 /* no appropriate language, fallback to default */
231 return [self localeForLanguageNamed:@"English"];
234 - (NSDictionary *)localeForLanguageNamed:(NSString *)_name {
237 NSDictionary *locale;
239 NSAssert(_name != nil, @"_name MUST NOT be nil!");
240 if((locale = [self->localeLUT objectForKey:_name]) != nil)
242 lpath = [[self resourceManager]
243 pathForResourceNamed:@"Locale"
245 languages:[NSArray arrayWithObject:_name]];
247 data = [NSData dataWithContentsOfFile:lpath];
249 NSLog(@"%s didn't find locale with name:%@",
254 data = [[[NSString alloc] initWithData:data
255 encoding:NSUTF8StringEncoding] autorelease];
256 locale = [data propertyList];
258 NSLog(@"%s couldn't load locale with name:%@",
263 [self->localeLUT setObject:locale forKey:_name];
270 int main(int argc, char **argv, char **env) {
271 NSAutoreleasePool *pool;
273 pool = [[NSAutoreleasePool alloc] init];
274 #if LIB_FOUNDATION_LIBRARY
275 [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
277 [NGBundleManager defaultBundleManager];
279 WOWatchDogApplicationMain(@"SOGo", argc, (void*)argv);