]> err.no Git - scalable-opengroupware.org/blob - UI/Common/UIxToolbar.m
initial sync
[scalable-opengroupware.org] / UI / Common / UIxToolbar.m
1 /*
2   Copyright (C) 2004-2005 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 #import <Foundation/NSKeyValueCoding.h>
23 #import <NGExtensions/NGExtensions.h>
24 #import <NGObjWeb/NGObjWeb.h>
25 #import <NGObjWeb/SoObjects.h>
26
27 #import <SOGoUI/UIxComponent.h>
28
29 #import <NGObjWeb/SoComponent.h>
30
31 @class NSArray, NSDictionary;
32
33 @interface UIxToolbar : UIxComponent
34 {
35   NSArray      *toolbarConfig;
36   NSArray      *toolbarGroup;
37   NSString *toolbar;
38   NSDictionary *buttonInfo;
39 }
40
41 - (void) setToolbar: (NSString *) newToolbar;
42 - (NSString *) toolbar;
43
44 @end
45
46 @implementation UIxToolbar
47
48 - (id) init
49 {
50   if ((self = [super init]))
51     {
52       toolbar = nil;
53     }
54
55   return self;
56 }
57
58 - (void)dealloc {
59   [toolbarGroup  release];
60   [toolbarConfig release];
61   [buttonInfo    release];
62   if (toolbar)
63     [toolbar release];
64   [super dealloc];
65 }
66
67 /* notifications */
68
69 - (void)sleep {
70   [toolbarGroup  release]; toolbarGroup  = nil;
71   [toolbarConfig release]; toolbarConfig = nil;
72   [buttonInfo    release]; buttonInfo    = nil;
73   [super sleep];
74 }
75
76 /* accessors */
77
78 - (void)setToolbarGroup:(id)_group {
79   ASSIGN(toolbarGroup, _group);
80 }
81
82 - (id)toolbarGroup {
83   return toolbarGroup;
84 }
85
86 - (void)setButtonInfo:(id)_info {
87   ASSIGN(buttonInfo, _info);
88 }
89
90 - (id)buttonInfo {
91   return buttonInfo;
92 }
93
94 /* toolbar */
95
96 - (WOResourceManager *)pageResourceManager {
97   WOResourceManager *rm;
98   
99   if ((rm = [[[self context] page] resourceManager]) != nil)
100     return rm;
101   
102   return [[self application] resourceManager];
103 }
104
105 - (id) pathToResourceNamed: (NSString *) name
106 {
107   WOResourceManager *rm;
108   NSRange  r;
109   NSString *fw, *rn;
110
111   r = [name rangeOfString: @"/"];
112   if (r.length > 0)
113     {
114       fw = [name substringToIndex: r.location];
115       rn = [name substringFromIndex: (r.location + r.length)];
116     }
117   else
118     {
119       rn = name;
120       fw = nil;
121     }
122   
123   rm = [self pageResourceManager];
124
125   return [rm pathForResourceNamed: rn inFramework: fw 
126              languages: [[self context] resourceLookupLanguages]];
127 }
128
129 - (id)loadToolbarConfigFromResourceNamed:(NSString *)_name {
130   /*
131     Note: we cannot cache by name because we don't know how the resource
132           manager will look up the name.
133           Both, the clientObject and the page might be different.
134           
135           Of course the resourcemanager will cache the resource path and we
136           cache the parsed content for a given path;
137   */
138   static NSMutableDictionary *pathToConfig = nil;
139   NSDictionary *tb;
140   NSString *path;
141
142   path = [self pathToResourceNamed: _name];
143   if (path == nil) {
144     [self errorWithFormat:@"Did not find toolbar resource: %@", _name];
145     return nil;
146   }
147
148   if ((tb = [pathToConfig objectForKey:path]) != nil)
149     return [tb isNotNull] ? tb : nil;
150   
151   if ((tb = [NSArray arrayWithContentsOfFile:path]) == nil)
152     [self errorWithFormat:@"Could not load toolbar resource: %@", _name];
153
154   if (pathToConfig == nil)
155     pathToConfig = [[NSMutableDictionary alloc] initWithCapacity:32];
156   [pathToConfig setObject:(tb ? tb : (id)[NSNull null]) forKey:path];
157
158   return tb;
159 }
160
161 - (id)toolbarConfig {
162   id tb;
163   
164   if (toolbarConfig != nil)
165     return [toolbarConfig isNotNull] ? toolbarConfig : nil;
166   
167   if (toolbar)
168     tb = toolbar;
169   else
170     tb = [[self clientObject] lookupName:@"toolbar" inContext:[self context]
171                               acquire:NO];
172
173   if ([tb isKindOfClass:[NSException class]]) {
174     [self errorWithFormat:
175             @"not toolbar configuration found on SoObject: %@ (%@)",
176             [self clientObject], [[self clientObject] soClass]];
177     toolbarConfig = [[NSNull null] retain];
178     return nil;
179   }
180   
181   if ([tb isKindOfClass:[NSString class]])
182     tb = [self loadToolbarConfigFromResourceNamed:tb];
183   
184   toolbarConfig = [tb retain];
185   return toolbarConfig;
186 }
187
188 /* labels */
189
190 - (NSString *) buttonLabel
191 {
192   NSString          *key;
193   
194   key = [[self buttonInfo] valueForKey: @"label"];
195
196   return [self labelForKey: key];
197 }
198
199 - (id) buttonImage
200 {
201   NSString *image;
202
203   image = [buttonInfo objectForKey: @"image"];
204   if (image && [image length] > 0)
205     image = [self urlForResourceFilename: image];
206
207   return image;
208 }
209
210 /* enable/disable buttons */
211
212 - (BOOL)isButtonEnabled {
213   // TODO: replace 'enabled' with WOAssociation when this gets a dynamic
214   //       element
215   NSString *onOffKey;
216   
217   if ((onOffKey = [[self buttonInfo] valueForKey:@"enabled"]) == nil)
218     return YES;
219
220   return [[[[self context] page] valueForKeyPath:onOffKey] boolValue];
221 }
222
223 - (BOOL) isLastGroup {
224   return ([toolbarConfig indexOfObject: toolbarGroup]
225           == ([toolbarConfig count] - 1));
226 }
227
228 - (BOOL) hasButtons
229 {
230   id tbConfig;
231   unsigned int count, max, amount;
232
233   tbConfig = [self toolbarConfig];
234
235   amount = 0;
236   max = [tbConfig count];
237   for (count = 0; count < max; count++)
238     amount += [[tbConfig objectAtIndex: count] count];
239
240   return (amount > 0);
241 }
242
243 - (void) setToolbar: (NSString *) newToolbar
244 {
245   ASSIGN(toolbar, newToolbar);
246 }
247
248 - (NSString *) toolbar
249 {
250   return toolbar;
251 }
252
253 @end /* UIxToolbar */