]> err.no Git - scalable-opengroupware.org/blob - UI/Common/UIxToolbar.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1305 d1b88da0-ebda-0310...
[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 {
163   id tb;
164   
165   if (toolbarConfig != nil)
166     return [toolbarConfig isNotNull] ? toolbarConfig : nil;
167   
168   if (toolbar)
169     tb = toolbar;
170   else
171     tb = [[self clientObject] lookupName: @"toolbar" inContext:[self context]
172                               acquire:NO];
173
174   if ([tb isKindOfClass:[NSException class]]) {
175     [self errorWithFormat:
176             @"not toolbar configuration found on SoObject: %@ (%@)",
177             [self clientObject], [[self clientObject] soClass]];
178     toolbarConfig = [[NSNull null] retain];
179     return nil;
180   }
181
182   if ([tb isKindOfClass: [NSString class]])
183     {
184       if ([tb isEqualToString: @"none"])
185         tb = [NSNull null];
186       else
187         tb = [self loadToolbarConfigFromResourceNamed:tb];
188     }
189   
190   toolbarConfig = [tb retain];
191
192   return toolbarConfig;
193 }
194
195 /* labels */
196
197 - (NSString *) buttonLabel
198 {
199   NSString          *key;
200   
201   key = [[self buttonInfo] valueForKey: @"label"];
202
203   return [self labelForKey: key];
204 }
205
206 - (id) buttonImage
207 {
208   NSString *image;
209
210   image = [buttonInfo objectForKey: @"image"];
211   if (image && [image length] > 0)
212     image = [self urlForResourceFilename: image];
213
214   return image;
215 }
216
217 - (NSString *) buttonTooltip
218 {
219   NSString          *key;
220   
221   key = [[self buttonInfo] valueForKey: @"tooltip"];
222
223   return [self labelForKey: key];
224 }
225
226 /* enable/disable buttons */
227
228 - (BOOL)isButtonEnabled {
229   // TODO: replace 'enabled' with WOAssociation when this gets a dynamic
230   //       element
231   NSString *onOffKey;
232   
233   if ((onOffKey = [[self buttonInfo] valueForKey:@"enabled"]) == nil)
234     return YES;
235
236   return [[[[self context] page] valueForKeyPath:onOffKey] boolValue];
237 }
238
239 - (BOOL) isLastGroup {
240   return ([toolbarConfig indexOfObject: toolbarGroup]
241           == ([toolbarConfig count] - 1));
242 }
243
244 - (BOOL) hasButtons
245 {
246   id tbConfig;
247   unsigned int count, max, amount;
248
249   tbConfig = [self toolbarConfig];
250
251   amount = 0;
252   max = [tbConfig count];
253   for (count = 0; count < max; count++)
254     amount += [[tbConfig objectAtIndex: count] count];
255
256   return (amount > 0);
257 }
258
259 - (BOOL) hasMenu
260 {
261   return [[[self buttonInfo] valueForKey:@"hasMenu"] boolValue];
262 }
263
264 - (void) setToolbar: (NSString *) newToolbar
265 {
266   ASSIGN(toolbar, newToolbar);
267 }
268
269 - (NSString *) toolbar
270 {
271   return toolbar;
272 }
273
274 @end /* UIxToolbar */