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