]> err.no Git - sope/blob - sope-appserver/NGObjWeb/WOComponentFault.m
Improved WORepetition's implementation to be more convenient in regards to the 'list...
[sope] / sope-appserver / NGObjWeb / WOComponentFault.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 "WOComponentFault.h"
23 #include "WOComponent+private.h"
24 #include <NGObjWeb/WOResourceManager.h>
25 #include <NGObjWeb/WOContext.h>
26 #include <NGObjWeb/WOApplication.h>
27 #include "common.h"
28
29 @implementation WOComponentFault
30
31 + (int)version {
32   return 2;
33 }
34
35 - (id)initWithResourceManager:(WOResourceManager *)_rm
36   pageName:(NSString *)_name
37   languages:(NSArray *)_langs
38   bindings:(NSDictionary *)_bindings
39 {
40   NSZone *z = [self zone];
41   self->resourceManager = RETAIN(_rm);
42   self->pageName        = [_name     copyWithZone:z];
43   self->languages       = [_langs    copyWithZone:z];
44   self->bindings        = [_bindings copyWithZone:z];
45   
46   [[NSNotificationCenter defaultCenter]
47                          addObserver:self
48                          selector:@selector(_contextWillDealloc:)
49                          name:@"WOContextWillDeallocate" object:nil];
50   
51   return self;
52 }
53
54 - (void)dealloc {
55   [[NSNotificationCenter defaultCenter] removeObserver:self];
56   RELEASE(self->bindings);
57   RELEASE(self->resourceManager);
58   RELEASE(self->pageName);
59   RELEASE(self->languages);
60   [super dealloc];
61 }
62
63 /* ctx dealloc */
64
65 - (void)_contextWillDealloc:(NSNotification *)_notification {
66 #if DEBUG
67   NSAssert(_notification, @"missing valid notification arg ...");
68 #endif
69   
70   if (self->ctx == nil)
71     /* component isn't interested in context anyway ... */
72     return;
73   if (![[self->ctx contextID] isEqualToString:[_notification object]])
74     /* not the component's context ... */
75     return;
76   
77   self->ctx = nil;
78 }
79
80 /* cached awake & sleep */
81
82 - (void)ensureAwakeInContext:(WOContext *)_ctx {
83   self->ctx = _ctx;
84 }
85 - (void)_awakeWithContext:(WOContext *)_ctx {
86   [self ensureAwakeInContext:_ctx];
87 }
88 - (void)_sleepWithContext:(WOContext *)_ctx {
89   self->ctx = nil;
90 }
91
92 /* resolve */
93
94 - (WOComponent *)resolveWithParent:(WOComponent *)_parent {
95   WOComponent *c;
96   WOResourceManager *rm;
97   
98 #if DEBUG && 0
99   [self logWithFormat:@"resolving fault for component %@", self->pageName];
100 #endif
101
102   if ((rm = self->resourceManager))
103     ;
104   else if ((rm = [_parent resourceManager]))
105     ;
106   else
107     rm = [[WOApplication application] resourceManager];
108   
109   c = [rm pageWithName:self->pageName languages:self->languages];
110   //[self logWithFormat:@"  rm:   %@", rm];
111   //[self logWithFormat:@"  c:    %@", c];
112   
113   [c setBindings:self->bindings];
114   [c setParent:_parent];
115   if (self->ctx) [c _awakeWithContext:self->ctx];
116   
117   if (c == NULL) {
118     [self logWithFormat:@"could not resolve fault for component: %@",
119             self->pageName];
120     [self logWithFormat:@"  resource-manager: %@", rm];
121     [self logWithFormat:@"  parent:           %@", _parent];
122   }
123   
124   return c;
125 }
126
127 - (void)setParent:(id)_parent {
128   /*
129     Not attached to a parent, this is called by WOComponent -dealloc on
130     each child (which can be a fault).
131   */
132 }
133
134 - (BOOL)isComponentFault {
135   return YES;
136 }
137
138 /* NSCoding */
139
140 - (void)encodeWithCoder:(NSCoder *)_coder {
141   [_coder encodeObject:self->pageName];
142   [_coder encodeObject:self->languages];
143   [_coder encodeObject:self->bindings];
144 }
145 - (id)initWithCoder:(NSCoder *)_decoder {
146   if ((self = [super init])) {
147     self->pageName  = [[_decoder decodeObject] copy];
148     self->languages = [[_decoder decodeObject] retain];
149     self->bindings  = [[_decoder decodeObject] retain];
150   }
151   return self;
152 }
153
154 /* NSCopying */
155
156 - (id)copyWithZone:(NSZone *)_zone {
157   return [self retain];
158 }
159
160 @end /* WOComponentFault */
161
162 @implementation WOComponent(WOComponentFault)
163
164 - (BOOL)isComponentFault {
165   return NO;
166 }
167
168 @end /* WOComponent(WOComponentFault) */