2 Copyright (C) 2004-2005 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
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
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.
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
22 #import <NGObjWeb/WORequest.h>
23 #import <NGExtensions/NSNull+misc.h>
24 #import <NGMail/NGMailAddress.h>
25 #import <NGMail/NGMailAddressParser.h>
27 #import <SOGoUI/UIxComponent.h>
32 Select a set of address headers for composing an email.
35 to - array of strings suitable for placement in a To: header
36 cc - array of strings suitable for placement in a Cc: header
37 bcc - array of strings suitable for placement in a Bcc: header
40 <var:component className="UIxMailToSelection"
49 @interface UIxMailToSelection : UIxComponent
60 - (void) setTo: (NSArray *) _to;
62 - (void) setCc: (NSArray *) _cc;
64 - (void) setBcc: (NSArray *) _bcc;
67 - (void) getAddressesFromFormValues: (NSDictionary *) _dict;
68 - (NSString *) getIndexFromIdentifier: (NSString *) _identifier;
72 @implementation UIxMailToSelection
74 static NSArray *headers = nil;
78 static BOOL didInit = NO;
82 headers = [[NSArray alloc] initWithObjects: @"to", @"cc", @"bcc", nil];
88 if ((self = [super init]))
101 [addressList release];
107 - (void) setTo: (NSArray *) _to
116 mailto = [self queryParameterForKey: @"mailto"];
117 if ([mailto length] > 0 && ![to count])
119 to = [NSArray arrayWithObject: mailto];
126 - (void) setCc: (NSArray *) _cc
136 - (void) setBcc: (NSArray *) _bcc
146 - (void) setAddressList: (NSArray *) _addressList
148 ASSIGN (addressList, _addressList);
151 - (NSArray *) addressList
156 - (void) setAddress: (id) _address
158 ASSIGN (address, _address);
166 - (void) setItem: (id) _item
168 ASSIGN (item, _item);
176 - (NSArray *) addressLists
180 ma = [NSMutableArray arrayWithCapacity:3];
181 if ([to isNotNull] && [to count] > 0)
188 /* ensure that at least one object is available */
191 NSArray *tmp = [NSArray arrayWithObject:@""];
199 - (NSArray *) headers
204 - (NSString *) currentHeader
206 if (addressList == to)
208 else if (addressList == cc)
216 - (NSString *) currentRowId
218 return [NSString stringWithFormat: @"row_%d", currentIndex];
221 - (NSString *) currentPopUpId
223 return [NSString stringWithFormat: @"popup_%d", currentIndex];
226 - (NSString *) currentAddressId
228 return [NSString stringWithFormat: @"addr_%d", currentIndex];
231 - (NSString *) nextId
238 /* handling requests */
240 - (void) _fillAddresses: (NSMutableArray *) addresses
241 withObject: (id) object
244 NSString *currentAddress;
246 if ([object isKindOfClass: [NSString class]])
247 [addresses addObject: object];
248 else if ([object isKindOfClass: [NSArray class]])
250 list = [object objectEnumerator];
251 while ((currentAddress
252 = [[list nextObject] stringByTrimmingSpaces]))
253 if ([currentAddress length])
254 [addresses addObject: currentAddress];
258 - (void) getAddressesFromFormValues: (NSDictionary *) _dict
260 NSMutableArray *rawTo, *rawCc, *rawBcc;
261 NSString *idx, *popupKey, *popupValue;
266 rawTo = [NSMutableArray arrayWithCapacity:4];
267 rawCc = [NSMutableArray arrayWithCapacity:4];
268 rawBcc = [NSMutableArray arrayWithCapacity:2];
270 keys = [_dict allKeys];
271 count = [keys count];
272 for (i = 0; i < count; i++)
276 key = [keys objectAtIndex:i];
277 if ([key hasPrefix:@"addr_"])
279 addr = [_dict objectForKey:key];
280 idx = [self getIndexFromIdentifier:key];
281 popupKey = [NSString stringWithFormat:@"popup_%@", idx];
282 popupValue = [[_dict objectForKey:popupKey] lastObject];
283 if([popupValue isEqualToString:@"0"])
284 [self _fillAddresses: rawTo withObject: addr];
285 else if([popupValue isEqualToString:@"1"])
286 [self _fillAddresses: rawCc withObject: addr];
288 [self _fillAddresses: rawBcc withObject: addr];
294 [self setBcc: rawBcc];
297 - (NSString *) getIndexFromIdentifier: (NSString *) _identifier
301 r = [_identifier rangeOfString: @"_"];
303 return [_identifier substringFromIndex: NSMaxRange(r)];
306 - (void) takeValuesFromRequest: (WORequest *) _rq
307 inContext: (WOContext *) _ctx
309 /* OK, we have a special form value processor */
312 if ((d = [_rq formValues]) == nil)
316 [self debugWithFormat:@"Note: will take values ..."];
317 NSLog(@"%s formValues: %@",
321 [self getAddressesFromFormValues: d];
326 return [to count] + [cc count] + [bcc count];
333 count = [self addressCount];
335 return count > 0 ? count - 1 : 0;
338 @end /* UIxMailToSelection */