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;
79 static BOOL didInit = NO;
84 headers = [[NSArray alloc] initWithObjects:@"to", @"cc", @"bcc", nil];
90 self->currentIndex = 0;
100 [self->address release];
101 [self->addressList release];
107 - (void)setTo:(NSArray *)_to
109 _to = [self properlySplitAddresses:_to];
110 ASSIGNCOPY(self->to, _to);
117 mailto = [self queryParameterForKey:@"mailto"];
118 if ([mailto length] > 0 && ![to count])
120 to = [NSArray arrayWithObject: mailto];
127 - (void)setCc:(NSArray *)_cc {
128 _cc = [self properlySplitAddresses:_cc];
129 ASSIGNCOPY(self->cc, _cc);
136 - (void)setBcc:(NSArray *)_bcc {
137 _bcc = [self properlySplitAddresses:_bcc];
138 ASSIGNCOPY(self->bcc, _bcc);
144 - (void)setAddressList:(NSArray *)_addressList {
145 ASSIGN(self->addressList, _addressList);
147 - (NSArray *)addressList {
148 return self->addressList;
151 - (void)setAddress:(id)_address {
152 ASSIGN(self->address, _address);
155 return self->address;
158 - (void)setItem:(id)_item {
159 ASSIGN(self->item, _item);
165 - (NSArray *)addressLists {
168 ma = [NSMutableArray arrayWithCapacity:3];
169 if ([self->to isNotNull]) [ma addObject:self->to];
170 if ([self->cc isNotNull]) [ma addObject:self->cc];
171 if ([self->bcc isNotNull]) [ma addObject:self->bcc];
173 /* ensure that at least one object is available */
174 if ([ma count] == 0) {
175 NSArray *tmp = [NSArray arrayWithObject:@""];
176 ASSIGNCOPY(self->to, tmp);
177 [ma addObject:self->to];
182 - (NSArray *)headers {
186 - (NSString *)currentHeader {
187 if(self->addressList == self->to)
189 else if(self->addressList == self->cc)
196 - (NSString *)currentRowId {
198 sprintf(buf, "row_%d", self->currentIndex);
199 return [NSString stringWithCString:buf];
202 - (NSString *)currentPopUpId {
204 sprintf(buf, "popup_%d", self->currentIndex);
205 return [NSString stringWithCString:buf];
208 - (NSString *)currentAddressId {
210 sprintf(buf, "addr_%d", self->currentIndex);
211 return [NSString stringWithCString:buf];
214 - (NSString *)nextId {
215 self->currentIndex++;
219 /* address handling */
221 - (NSArray *)properlySplitAddresses:(NSArray *)_addresses {
223 NGMailAddressParser *parser;
228 if(!_addresses || [_addresses count] == 0)
231 /* create one huge string, then split it using the parser */
232 addrs = [_addresses componentsJoinedByString:@","];
233 parser = [NGMailAddressParser mailAddressParserWithString:addrs];
234 result = [parser parseAddressList];
236 [self debugWithFormat:@"Couldn't parse given addresses:%@", _addresses];
240 count = [result count];
241 ma = [NSMutableArray arrayWithCapacity:count];
242 for (i = 0; i < count; i++) {
247 s = [[NSMutableString alloc] init];
248 addr = [result objectAtIndex:i];
249 if([addr displayName]) {
250 [s appendString:[addr displayName]];
251 [s appendString:@" "];
255 [s appendString:@"<"];
256 [s appendString:[addr address]];
258 [s appendString:@">"];
264 /* handling requests */
266 - (void)getAddressesFromFormValues:(NSDictionary *)_dict {
267 NSMutableArray *rawTo, *rawCc, *rawBcc;
271 rawTo = [NSMutableArray arrayWithCapacity:4];
272 rawCc = [NSMutableArray arrayWithCapacity:4];
273 rawBcc = [NSMutableArray arrayWithCapacity:2];
275 keys = [_dict allKeys];
276 count = [keys count];
277 for (i = 0; i < count; i++) {
280 key = [keys objectAtIndex:i];
281 if([key hasPrefix:@"addr_"]) {
282 NSString *idx, *addr, *popupKey, *popupValue;
284 addr = [[_dict objectForKey:key] lastObject];
285 idx = [self getIndexFromIdentifier:key];
286 popupKey = [NSString stringWithFormat:@"popup_%@", idx];
287 popupValue = [[_dict objectForKey:popupKey] lastObject];
288 if([popupValue isEqualToString:@"0"])
289 [rawTo addObject:addr];
290 else if([popupValue isEqualToString:@"1"])
291 [rawCc addObject:addr];
293 [rawBcc addObject:addr];
299 [self setBcc:rawBcc];
302 - (NSString *)getIndexFromIdentifier:(NSString *)_identifier {
305 r = [_identifier rangeOfString:@"_"];
306 return [_identifier substringFromIndex:NSMaxRange(r)];
309 - (void)takeValuesFromRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
310 /* OK, we have a special form value processor */
313 if ((d = [_rq formValues]) == nil)
317 [self debugWithFormat:@"Note: will take values ..."];
318 NSLog(@"%s formValues: %@",
322 [self getAddressesFromFormValues:d];
325 - (int)addressCount {
326 return [self->to count] + [self->cc count] + [self->bcc count];
329 - (int)currentIndex {
332 count = [self addressCount];
333 return count > 0 ? count - 1 : 0;
336 @end /* UIxMailToSelection */