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 #include <SOGoUI/UIxComponent.h>
27 Select a set of address headers for composing an email.
30 to - array of strings suitable for placement in a To: header
31 cc - array of strings suitable for placement in a Cc: header
32 bcc - array of strings suitable for placement in a Bcc: header
35 <var:component className="UIxMailToSelection"
44 @interface UIxMailToSelection : UIxComponent
55 - (void)setTo:(NSArray *)_to;
57 - (void)setCc:(NSArray *)_cc;
59 - (void)setBcc:(NSArray *)_bcc;
62 - (NSArray *)properlySplitAddresses:(NSArray *)_addresses;
64 - (void)getAddressesFromFormValues:(NSDictionary *)_dict;
65 - (NSString *)getIndexFromIdentifier:(NSString *)_identifier;
70 #include <NGMail/NGMail.h>
72 @implementation UIxMailToSelection
74 static NSArray *headers = nil;
77 static BOOL didInit = NO;
82 headers = [[NSArray alloc] initWithObjects:@"to", @"cc", @"bcc", nil];
88 self->currentIndex = 0;
98 [self->address release];
99 [self->addressList release];
105 - (void)setTo:(NSArray *)_to
107 _to = [self properlySplitAddresses:_to];
108 ASSIGNCOPY(self->to, _to);
115 mailto = [self queryParameterForKey:@"mailto"];
116 if ([mailto length] > 0 && ![to count])
118 to = [NSArray arrayWithObject: mailto];
125 - (void)setCc:(NSArray *)_cc {
126 _cc = [self properlySplitAddresses:_cc];
127 ASSIGNCOPY(self->cc, _cc);
134 - (void)setBcc:(NSArray *)_bcc {
135 _bcc = [self properlySplitAddresses:_bcc];
136 ASSIGNCOPY(self->bcc, _bcc);
142 - (void)setAddressList:(NSArray *)_addressList {
143 ASSIGN(self->addressList, _addressList);
145 - (NSArray *)addressList {
146 return self->addressList;
149 - (void)setAddress:(id)_address {
150 ASSIGN(self->address, _address);
153 return self->address;
156 - (void)setItem:(id)_item {
157 ASSIGN(self->item, _item);
163 - (NSArray *)addressLists {
166 ma = [NSMutableArray arrayWithCapacity:3];
167 if ([self->to isNotNull]) [ma addObject:self->to];
168 if ([self->cc isNotNull]) [ma addObject:self->cc];
169 if ([self->bcc isNotNull]) [ma addObject:self->bcc];
171 /* ensure that at least one object is available */
172 if ([ma count] == 0) {
173 NSArray *tmp = [NSArray arrayWithObject:@""];
174 ASSIGNCOPY(self->to, tmp);
175 [ma addObject:self->to];
180 - (NSArray *)headers {
184 - (NSString *)currentHeader {
185 if(self->addressList == self->to)
187 else if(self->addressList == self->cc)
194 - (NSString *)currentRowId {
196 sprintf(buf, "row_%d", self->currentIndex);
197 return [NSString stringWithCString:buf];
200 - (NSString *)currentPopUpId {
202 sprintf(buf, "popup_%d", self->currentIndex);
203 return [NSString stringWithCString:buf];
206 - (NSString *)currentAddressId {
208 sprintf(buf, "addr_%d", self->currentIndex);
209 return [NSString stringWithCString:buf];
212 - (NSString *)nextId {
213 self->currentIndex++;
217 /* address handling */
219 - (NSArray *)properlySplitAddresses:(NSArray *)_addresses {
221 NGMailAddressParser *parser;
226 if(!_addresses || [_addresses count] == 0)
229 /* create one huge string, then split it using the parser */
230 addrs = [_addresses componentsJoinedByString:@","];
231 parser = [NGMailAddressParser mailAddressParserWithString:addrs];
232 result = [parser parseAddressList];
234 [self debugWithFormat:@"Couldn't parse given addresses:%@", _addresses];
238 count = [result count];
239 ma = [NSMutableArray arrayWithCapacity:count];
240 for (i = 0; i < count; i++) {
245 s = [[NSMutableString alloc] init];
246 addr = [result objectAtIndex:i];
247 if([addr displayName]) {
248 [s appendString:[addr displayName]];
249 [s appendString:@" "];
253 [s appendString:@"<"];
254 [s appendString:[addr address]];
256 [s appendString:@">"];
262 /* handling requests */
264 - (void)getAddressesFromFormValues:(NSDictionary *)_dict {
265 NSMutableArray *rawTo, *rawCc, *rawBcc;
269 rawTo = [NSMutableArray arrayWithCapacity:4];
270 rawCc = [NSMutableArray arrayWithCapacity:4];
271 rawBcc = [NSMutableArray arrayWithCapacity:2];
273 keys = [_dict allKeys];
274 count = [keys count];
275 for (i = 0; i < count; i++) {
278 key = [keys objectAtIndex:i];
279 if([key hasPrefix:@"addr_"]) {
280 NSString *idx, *addr, *popupKey, *popupValue;
282 addr = [[_dict objectForKey:key] lastObject];
283 idx = [self getIndexFromIdentifier:key];
284 popupKey = [NSString stringWithFormat:@"popup_%@", idx];
285 popupValue = [[_dict objectForKey:popupKey] lastObject];
286 if([popupValue isEqualToString:@"0"])
287 [rawTo addObject:addr];
288 else if([popupValue isEqualToString:@"1"])
289 [rawCc addObject:addr];
291 [rawBcc addObject:addr];
297 [self setBcc:rawBcc];
300 - (NSString *)getIndexFromIdentifier:(NSString *)_identifier {
303 r = [_identifier rangeOfString:@"_"];
304 return [_identifier substringFromIndex:NSMaxRange(r)];
307 - (void)takeValuesFromRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
308 /* OK, we have a special form value processor */
311 if ((d = [_rq formValues]) == nil)
315 [self debugWithFormat:@"Note: will take values ..."];
316 NSLog(@"%s formValues: %@",
320 [self getAddressesFromFormValues:d];
323 - (int)addressCount {
324 return [self->to count] + [self->cc count] + [self->bcc count];
327 - (int)currentIndex {
330 count = [self addressCount];
331 return count > 0 ? count - 1 : 0;
334 @end /* UIxMailToSelection */