]> err.no Git - sope/blob - sope-appserver/NGObjWeb/SoObjects/SoApplication.m
fixed various warnings
[sope] / sope-appserver / NGObjWeb / SoObjects / SoApplication.m
1 /*
2   Copyright (C) 2002-2004 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 "SoApplication.h"
23 #include "SoClassRegistry.h"
24 #include "SoControlPanel.h"
25 #include "SoObject.h"
26 #include "SoObjectRequestHandler.h"
27 #include "SoProductRegistry.h"
28 #include "SoSecurityManager.h"
29 #include "SoApplication.h"
30 #include "SoObject+SoDAV.h"
31 #include <NGObjWeb/WORequest.h>
32 #include "common.h"
33
34 @implementation SoApplication
35
36 static BOOL debugLookup = NO;
37
38 - (BOOL)loadProducts:(id)_spec  {
39   if (_spec != nil) {
40     // TODO: only load specified products
41     [self logWithFormat:@"load products (not implemented): %@", _spec];
42     return NO;
43   }
44   else
45     [self->productRegistry loadAllProducts];
46   
47   return YES;
48 }
49
50 - (id)init {
51   if ((self = [super init])) {
52     NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
53     id tmp;
54     
55     debugLookup = [ud boolForKey:@"SoDebugKeyLookup"];
56     
57     /* setup global objects */
58     
59     self->securityManager = [[SoSecurityManager sharedSecurityManager] retain];
60     self->classRegistry   = [[SoClassRegistry sharedClassRegistry] retain];
61     self->productRegistry = [[SoProductRegistry sharedProductRegistry] retain];
62     
63     /* Object Publishing */
64     tmp = [[SoObjectRequestHandler alloc] init];
65     [self registerRequestHandler:tmp forKey:@"so"];
66     [self registerRequestHandler:tmp forKey:@"dav"];
67     [self registerRequestHandler:tmp forKey:@"RPC2"];
68     [self setDefaultRequestHandler:tmp];
69     [tmp release];
70     
71     /* load products (all if SoApplicationLoadProducts is not set) */
72     if (![self loadProducts:[ud objectForKey:@"SoApplicationLoadProducts"]]) {
73       [self logWithFormat:@"failed to load the products ..."];
74       [self release];
75       return nil;
76     }
77     
78 #if LIB_FOUNDATION_LIBRARY
79     /* debugging */
80     if ([[ud objectForKey:@"EnableDoubleReleaseCheck"] boolValue])
81       [NSAutoreleasePool enableDoubleReleaseCheck:YES];
82 #endif
83   }
84   return self;
85 }
86
87 - (void)dealloc {
88   [self->securityManager release];
89   [self->classRegistry   release];
90   [self->productRegistry release];
91   [super dealloc];
92 }
93
94 /* accessors */
95
96 - (SoProductRegistry *)productRegistry {
97   return self->productRegistry;
98 }
99 - (SoClassRegistry *)classRegistry {
100   return self->classRegistry;
101 }
102 - (SoSecurityManager *)securityManager {
103   return self->securityManager;
104 }
105
106 /* application as the SoObject root */
107
108 - (id)rootObjectInContext:(id)_ctx {
109   return nil;
110 }
111
112 - (NSException *)validateName:(NSString *)_key inContext:(id)_ctx {
113   id root;
114   
115   if ([self hasName:_key inContext:_ctx])
116     return [super validateName:_key inContext:_ctx];
117   
118   root = [self rootObjectInContext:_ctx];
119   return (root != self) 
120     ? [root validateName:_key inContext:_ctx] 
121     : [super validateName:_key inContext:_ctx];
122 }
123
124 - (BOOL)hasName:(NSString *)_key inContext:(id)_ctx {
125   id root;
126   
127   if ([_key isEqualToString:@"ControlPanel"])
128     return YES;
129   
130   if ([[self registeredRequestHandlerKeys] containsObject:_key])
131     return YES;
132   
133   if ([super hasName:_key inContext:_ctx])
134     return YES;
135   
136   root = [self rootObjectInContext:_ctx];
137   if (root != self)
138     return [root hasName:_key inContext:_ctx];
139   
140   return NO;
141 }
142
143 - (id)controlPanel:(NSString *)_name inContext:(id)_ctx {
144   return [[[SoControlPanel alloc] init] autorelease];
145 }
146
147 - (BOOL)isApplicationNameLookup:(NSString *)_name inContext:(id)_ctx {
148   static NSString *WOApplicationSuffix = nil;
149   NSString *appName;
150
151   appName = [[(WOContext *)_ctx request] applicationName];
152   if ([_name isEqual:appName]) {
153     if (debugLookup) [self logWithFormat:@"  matched appname: %@", appName];
154     return YES;
155   }
156
157   if (WOApplicationSuffix == nil) {
158     NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
159     WOApplicationSuffix = [[ud stringForKey:@"WOApplicationSuffix"] copy];
160   }
161   appName = [appName stringByAppendingString:WOApplicationSuffix];
162   if ([_name isEqual:appName]) {
163     if (debugLookup) [self logWithFormat:@"  matched appname: %@", appName];
164     return YES;
165   }
166   
167   return NO;
168 }
169
170 - (id)lookupName:(NSString *)_name inContext:(id)_ctx acquire:(BOOL)_flag {
171   id v;
172   
173   if ([_name isEqualToString:@"ControlPanel"])
174     return [self controlPanel:_name inContext:_ctx];
175   
176   if ([[self registeredRequestHandlerKeys] containsObject:_name]) {
177     /* 
178       need to check registeredRequestHandlerKeys because requestHandlerForKey:
179       returns the default handler if the key could not be found ...
180     */
181     if ((v = [super requestHandlerForKey:_name]))
182       return v;
183   }
184   
185   if (debugLookup) [self logWithFormat:@"lookup name: %@", _name];
186   
187   if ((v = [super lookupName:_name inContext:_ctx acquire:NO]) == nil) {
188     id root;
189     
190     root = [self rootObjectInContext:_ctx];
191     if (debugLookup) [self logWithFormat:@"  lookup in root object: %@", v];
192     
193     if (root != self)
194       v = [root lookupName:_name inContext:_ctx acquire:_flag];
195     else if (debugLookup)
196       [self logWithFormat:@"  root is application object"];
197   }
198   
199   if (debugLookup) [self logWithFormat:@"  GOT: %@", v];
200   
201   /* 
202      hack to allow "/myapp/folder/", it is a hack because it also allows
203      /myapp/myapp/myapp/.../folder/ ...
204   */
205   if (v == nil && [self isApplicationNameLookup:_name inContext:_ctx]) {
206     v = self;
207     if (debugLookup) [self logWithFormat:@"  => rewrote value: %@", self];
208   }
209   
210   return v;
211 }
212
213 - (NSArray *)toOneRelationshipKeys {
214   NSMutableSet *ma;
215   id root;
216   
217   ma = ((root = [super toOneRelationshipKeys]))
218     ? [[NSMutableSet alloc] initWithArray:root]
219     : [[NSMutableSet alloc] init];
220   
221   [ma addObjectsFromArray:[self registeredRequestHandlerKeys]];
222   [ma addObject:@"ControlPanel"];
223   
224   root = [self rootObjectInContext:[self context]];
225   if (root != nil && (root != self)) 
226     [ma addObjectsFromArray:[root toOneRelationshipKeys]];
227   
228   root = [ma allObjects];
229   [ma release];
230   return root;
231 }
232
233 /* WebDAV support for root objects */
234
235 - (id)davCreateObject:(NSString *)_name
236   properties:(NSDictionary *)_props
237   inContext:(id)_ctx
238 {
239   id root;
240
241   if ((root = [self rootObjectInContext:_ctx]) == nil)
242     return [super davCreateObject:_name properties:_props inContext:_ctx];
243   
244   return [root davCreateObject:_name properties:_props inContext:_ctx];
245 }
246
247 - (NSException *)davCreateCollection:(NSString *)_name inContext:(id)_ctx {
248   id root;
249
250   if ((root = [self rootObjectInContext:_ctx]) == nil)
251     return [super davCreateCollection:_name inContext:_ctx];
252   
253   //[self debugWithFormat:@"let root '%@' create collection '%@'", root,_name];
254   return [root davCreateCollection:_name inContext:_ctx];
255 }
256
257 @end /* SoApplication */