]> err.no Git - sope/blob - sope-appserver/WEExtensions/WERichString.m
added strict OSX bundle dependencies
[sope] / sope-appserver / WEExtensions / WERichString.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 <NGObjWeb/WODynamicElement.h>
23
24 @interface WERichString : WODynamicElement
25 {
26   WOAssociation *isBold;
27   WOAssociation *isItalic;
28   WOAssociation *isUnderlined;
29   WOAssociation *isSmall;
30   WOAssociation *color;
31   WOAssociation *face;
32   WOAssociation *size;
33   WOAssociation *insertBR;
34
35   WOAssociation *condition;
36   WOAssociation *negate;
37
38   WOAssociation *formatter;
39   
40   WOAssociation *value;
41   
42   WOElement     *template;
43 }
44
45 @end
46
47 #include <NGObjWeb/NGObjWeb.h>
48 #include "common.h"
49
50 @implementation WERichString
51
52 - (id)initWithName:(NSString *)_name
53   associations:(NSDictionary *)_config
54   template:(WOElement *)_t
55 {
56   if ((self = [super initWithName:_name associations:_config template:_t])) {
57     self->value        = OWGetProperty(_config, @"value");
58     
59     self->isBold       = OWGetProperty(_config, @"isBold");
60     self->isItalic     = OWGetProperty(_config, @"isItalic");
61     self->isUnderlined = OWGetProperty(_config, @"isUnderlined");
62     self->isSmall      = OWGetProperty(_config, @"isSmall");
63     self->color        = OWGetProperty(_config, @"color");
64     self->face         = OWGetProperty(_config, @"face");
65     self->size         = OWGetProperty(_config, @"size");
66     self->insertBR     = OWGetProperty(_config, @"insertBR");
67
68     self->condition    = OWGetProperty(_config, @"condition");
69     self->negate       = OWGetProperty(_config, @"negate");
70
71     self->formatter    = OWGetProperty(_config, @"formatter");
72
73     ASSIGN(self->template, _t);
74   }
75   return self;
76 }
77
78 - (void)dealloc {
79   RELEASE(self->value);
80   RELEASE(self->isBold);
81   RELEASE(self->isItalic);
82   RELEASE(self->isUnderlined);
83   RELEASE(self->isSmall);
84   RELEASE(self->color);
85   RELEASE(self->face);
86   RELEASE(self->size);
87   RELEASE(self->insertBR);
88   RELEASE(self->formatter);
89
90   RELEASE(self->condition);
91   RELEASE(self->negate);
92
93   RELEASE(self->template);
94   
95   [super dealloc];
96 }
97
98 static inline BOOL _doShow(WERichString *self, WOContext *_ctx) {
99   BOOL doShow   = YES;
100   BOOL doNegate = NO;
101
102   if (self->condition != nil) {
103     doShow   = [self->condition boolValueInComponent:[_ctx component]];
104     doNegate = [self->negate boolValueInComponent:[_ctx component]];
105   }
106   
107   return (doNegate) ? !doShow : doShow;
108 }
109
110 - (void)takeValuesFromRequest:(WORequest *)_request
111   inContext:(WOContext *)_ctx
112 {
113   if (_doShow(self, _ctx)) {
114     [_ctx appendElementIDComponent:@"1"];
115     [self->template takeValuesFromRequest:_request inContext:_ctx];
116     [_ctx deleteLastElementIDComponent];
117   }
118 }
119
120 - (id)invokeActionForRequest:(WORequest *)_request inContext:(WOContext *)_ctx {
121   NSString *state;
122
123   state = [[_ctx currentElementID] stringValue];
124   
125   if (state) {
126     [_ctx consumeElementID]; // consume state-id (on or off)
127
128     if ([state isEqualToString:@"1"]) {
129       id result;
130       
131       [_ctx appendElementIDComponent:state];
132       result = [self->template invokeActionForRequest:_request inContext:_ctx];
133       [_ctx deleteLastElementIDComponent];
134
135       return result;
136     }
137   }
138   return nil;
139 }
140
141 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
142   WOComponent *comp      = nil;
143   BOOL     doBold        = NO;
144   BOOL     doItalic      = NO;
145   BOOL     doUnderlined  = NO;
146   BOOL     doSmall       = NO;
147   NSString *color_       = nil;
148   NSString *face_        = nil;
149   NSString *size_        = nil;
150   NSFormatter *fmt       = nil;
151   id       v             = nil;
152
153
154   if (!_doShow(self, _ctx)) return;
155   
156   comp = [_ctx component];
157   
158   doBold       = [self->isBold       boolValueInComponent:comp];
159   doItalic     = [self->isItalic     boolValueInComponent:comp];
160   doUnderlined = [self->isUnderlined boolValueInComponent:comp];
161   doSmall      = [self->isSmall      boolValueInComponent:comp];
162   face_        = [self->face       stringValueInComponent:comp];
163   color_       = [self->color      stringValueInComponent:comp];
164   size_        = [self->size       stringValueInComponent:comp];
165   v            = [self->value            valueInComponent:comp];
166   fmt          = [self->formatter        valueInComponent:comp];
167
168   if (doSmall)
169     [_response appendContentString:@"<small>"];
170   if (doBold)
171     [_response appendContentString:@"<b>"];
172   if (doItalic)
173     [_response appendContentString:@"<i>"];
174   if (doUnderlined)
175     [_response appendContentString:@"<u>"];
176
177   [_response appendContentString:@"<font"];
178   if (color_ != nil) {
179     [_response appendContentString:@" color='"];
180     [_response appendContentHTMLString:color_];
181     [_response appendContentCharacter:'\''];
182   }
183   if (face_ != nil) {
184     [_response appendContentString:@" face='"];
185     [_response appendContentHTMLString:face_];
186     [_response appendContentCharacter:'\''];
187   }
188   if (size_ != nil) {
189     [_response appendContentString:@" size='"];
190     [_response appendContentHTMLString:size_];
191     [_response appendContentCharacter:'\''];
192   }
193   [_response appendContentCharacter:'>'];
194
195   [_ctx appendElementIDComponent:@"1"];
196   [self->template appendToResponse:_response inContext:_ctx];
197   [_ctx deleteLastElementIDComponent];
198
199   v = (fmt)
200     ? [fmt stringForObjectValue:v]
201     : [v stringValue];
202
203   if (v && [self->insertBR boolValueInComponent:comp]) {
204     NSArray *lines;
205     unsigned i, count;
206       
207     lines = [v componentsSeparatedByString:@"\n"];
208     count = [lines count];
209     for (i = 0; i < count; i++) {
210       NSString *line = [lines objectAtIndex:i];
211
212       if (i != 0) {
213         [_response appendContentString:
214                      (_ctx->wcFlags.xmlStyleEmptyElements)?@"<br />":@"<br>"];
215       }
216
217       [_response appendContentHTMLString:line];
218     }
219   }
220   else
221     [_response appendContentHTMLString:v];
222
223   [_response appendContentString:@"</font>"];
224   if (doUnderlined)
225     [_response appendContentString:@"</u>"];
226   if (doItalic)
227     [_response appendContentString:@"</i>"];
228   if (doBold)
229     [_response appendContentString:@"</b>"];
230   if (doSmall)
231     [_response appendContentString:@"</small>"];
232
233 }
234 @end