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 - (NSArray *) properlySplitAddresses: (NSArray *) _addresses;
69 - (void) getAddressesFromFormValues: (NSDictionary *) _dict;
70 - (NSString *) getIndexFromIdentifier: (NSString *) _identifier;
74 @implementation UIxMailToSelection
76 static NSArray *headers = nil;
80 static BOOL didInit = NO;
84 headers = [[NSArray alloc] initWithObjects: @"to", @"cc", @"bcc", nil];
90 if ((self = [super init]))
103 [addressList release];
109 - (void) setTo: (NSArray *) _to
111 ASSIGN (to, [self properlySplitAddresses: _to]);
118 mailto = [self queryParameterForKey: @"mailto"];
119 if ([mailto length] > 0 && ![to count])
121 to = [NSArray arrayWithObject: mailto];
128 - (void) setCc: (NSArray *) _cc
130 ASSIGN (cc, [self properlySplitAddresses: _cc]);
138 - (void) setBcc: (NSArray *) _bcc
140 ASSIGN (bcc, [self properlySplitAddresses: _bcc]);
148 - (void) setAddressList: (NSArray *) _addressList
150 ASSIGN (addressList, _addressList);
153 - (NSArray *) addressList
158 - (void) setAddress: (id) _address
160 ASSIGN (address, _address);
168 - (void) setItem: (id) _item
170 ASSIGN (item, _item);
178 - (NSArray *) addressLists
182 ma = [NSMutableArray arrayWithCapacity:3];
190 /* ensure that at least one object is available */
193 NSArray *tmp = [NSArray arrayWithObject:@""];
201 - (NSArray *) headers
206 - (NSString *) currentHeader
208 if (addressList == to)
210 else if (addressList == cc)
218 - (NSString *) currentRowId
220 return [NSString stringWithFormat: @"row_%d", currentIndex];
223 - (NSString *) currentPopUpId
225 return [NSString stringWithFormat: @"popup_%d", currentIndex];
228 - (NSString *) currentAddressId
230 return [NSString stringWithFormat: @"addr_%d", currentIndex];
233 - (NSString *) nextId
240 /* address handling */
242 - (NSArray *) properlySplitAddresses: (NSArray *) _addresses
245 NGMailAddressParser *parser;
250 if (!_addresses || [_addresses count] == 0)
253 /* create one huge string, then split it using the parser */
254 addrs = [_addresses componentsJoinedByString:@","];
255 parser = [NGMailAddressParser mailAddressParserWithString:addrs];
256 result = [parser parseAddressList];
258 [self debugWithFormat:@"Couldn't parse given addresses:%@", _addresses];
262 count = [result count];
263 ma = [NSMutableArray arrayWithCapacity:count];
264 for (i = 0; i < count; i++) {
269 s = [[NSMutableString alloc] init];
270 addr = [result objectAtIndex:i];
271 if([addr displayName]) {
272 [s appendString:[addr displayName]];
273 [s appendString:@" "];
277 [s appendString:@"<"];
278 [s appendString:[addr address]];
280 [s appendString:@">"];
286 /* handling requests */
288 - (void) getAddressesFromFormValues: (NSDictionary *) _dict
290 NSMutableArray *rawTo, *rawCc, *rawBcc;
294 rawTo = [NSMutableArray arrayWithCapacity:4];
295 rawCc = [NSMutableArray arrayWithCapacity:4];
296 rawBcc = [NSMutableArray arrayWithCapacity:2];
298 keys = [_dict allKeys];
299 count = [keys count];
300 for (i = 0; i < count; i++)
304 key = [keys objectAtIndex:i];
305 if ([key hasPrefix:@"addr_"])
307 NSString *idx, *addr, *popupKey, *popupValue;
309 addr = [[_dict objectForKey:key] lastObject];
310 idx = [self getIndexFromIdentifier:key];
311 popupKey = [NSString stringWithFormat:@"popup_%@", idx];
312 popupValue = [[_dict objectForKey:popupKey] lastObject];
313 if([popupValue isEqualToString:@"0"])
314 [rawTo addObject:addr];
315 else if([popupValue isEqualToString:@"1"])
316 [rawCc addObject:addr];
318 [rawBcc addObject:addr];
324 [self setBcc: rawBcc];
327 - (NSString *) getIndexFromIdentifier: (NSString *) _identifier
331 r = [_identifier rangeOfString: @"_"];
333 return [_identifier substringFromIndex: NSMaxRange(r)];
336 - (void) takeValuesFromRequest: (WORequest *) _rq
337 inContext: (WOContext *) _ctx
339 /* OK, we have a special form value processor */
342 if ((d = [_rq formValues]) == nil)
346 [self debugWithFormat:@"Note: will take values ..."];
347 NSLog(@"%s formValues: %@",
351 [self getAddressesFromFormValues: d];
356 return [to count] + [cc count] + [bcc count];
363 count = [self addressCount];
365 return count > 0 ? count - 1 : 0;
368 @end /* UIxMailToSelection */