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 {
106 _to = [self properlySplitAddresses:_to];
107 ASSIGNCOPY(self->to, _to);
113 - (void)setCc:(NSArray *)_cc {
114 _cc = [self properlySplitAddresses:_cc];
115 ASSIGNCOPY(self->cc, _cc);
121 - (void)setBcc:(NSArray *)_bcc {
122 _bcc = [self properlySplitAddresses:_bcc];
123 ASSIGNCOPY(self->bcc, _bcc);
129 - (void)setAddressList:(NSArray *)_addressList {
130 ASSIGN(self->addressList, _addressList);
132 - (NSArray *)addressList {
133 return self->addressList;
136 - (void)setAddress:(id)_address {
137 ASSIGN(self->address, _address);
140 return self->address;
143 - (void)setItem:(id)_item {
144 ASSIGN(self->item, _item);
150 - (NSArray *)addressLists {
153 ma = [NSMutableArray arrayWithCapacity:3];
154 if ([self->to isNotNull]) [ma addObject:self->to];
155 if ([self->cc isNotNull]) [ma addObject:self->cc];
156 if ([self->bcc isNotNull]) [ma addObject:self->bcc];
158 /* ensure that at least one object is available */
159 if ([ma count] == 0) {
160 NSArray *tmp = [NSArray arrayWithObject:@""];
161 ASSIGNCOPY(self->to, tmp);
162 [ma addObject:self->to];
167 - (NSArray *)headers {
171 - (NSString *)currentHeader {
172 if(self->addressList == self->to)
174 else if(self->addressList == self->cc)
181 - (NSString *)currentRowId {
182 unsigned char buf[16];
183 sprintf(buf, "row_%d", self->currentIndex);
184 return [NSString stringWithCString:buf];
187 - (NSString *)currentPopUpId {
188 unsigned char buf[16];
189 sprintf(buf, "popup_%d", self->currentIndex);
190 return [NSString stringWithCString:buf];
193 - (NSString *)currentAddressId {
194 unsigned char buf[16];
195 sprintf(buf, "addr_%d", self->currentIndex);
196 return [NSString stringWithCString:buf];
199 - (NSString *)nextId {
200 self->currentIndex++;
204 /* address handling */
206 - (NSArray *)properlySplitAddresses:(NSArray *)_addresses {
208 NGMailAddressParser *parser;
213 if(!_addresses || [_addresses count] == 0)
216 /* create one huge string, then split it using the parser */
217 addrs = [_addresses componentsJoinedByString:@","];
218 parser = [NGMailAddressParser mailAddressParserWithString:addrs];
219 result = [parser parseAddressList];
221 [self debugWithFormat:@"Couldn't parse given addresses:%@", _addresses];
225 count = [result count];
226 ma = [NSMutableArray arrayWithCapacity:count];
227 for (i = 0; i < count; i++) {
232 s = [[NSMutableString alloc] init];
233 addr = [result objectAtIndex:i];
234 if([addr displayName]) {
235 [s appendString:[addr displayName]];
236 [s appendString:@" "];
240 [s appendString:@"<"];
241 [s appendString:[addr address]];
243 [s appendString:@">"];
249 /* handling requests */
251 - (void)getAddressesFromFormValues:(NSDictionary *)_dict {
252 NSMutableArray *rawTo, *rawCc, *rawBcc;
256 rawTo = [NSMutableArray arrayWithCapacity:4];
257 rawCc = [NSMutableArray arrayWithCapacity:4];
258 rawBcc = [NSMutableArray arrayWithCapacity:2];
260 keys = [_dict allKeys];
261 count = [keys count];
262 for (i = 0; i < count; i++) {
265 key = [keys objectAtIndex:i];
266 if([key hasPrefix:@"addr_"]) {
267 NSString *idx, *addr, *popupKey, *popupValue;
269 addr = [[_dict objectForKey:key] lastObject];
270 idx = [self getIndexFromIdentifier:key];
271 popupKey = [NSString stringWithFormat:@"popup_%@", idx];
272 popupValue = [[_dict objectForKey:popupKey] lastObject];
273 if([popupValue isEqualToString:@"0"])
274 [rawTo addObject:addr];
275 else if([popupValue isEqualToString:@"1"])
276 [rawCc addObject:addr];
278 [rawBcc addObject:addr];
284 [self setBcc:rawBcc];
287 - (NSString *)getIndexFromIdentifier:(NSString *)_identifier {
290 r = [_identifier rangeOfString:@"_"];
291 return [_identifier substringFromIndex:NSMaxRange(r)];
294 - (void)takeValuesFromRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
295 /* OK, we have a special form value processor */
298 if ((d = [_rq formValues]) == nil)
302 [self debugWithFormat:@"Note: will take values ..."];
303 NSLog(@"%s formValues: %@",
307 [self getAddressesFromFormValues:d];
310 - (int)addressCount {
311 return [self->to count] + [self->cc count] + [self->bcc count];
314 - (int)currentIndex {
317 count = [self addressCount];
318 return count > 0 ? count - 1 : 0;
321 @end /* UIxMailToSelection */