]> err.no Git - scalable-opengroupware.org/blob - UI/MailerUI/UIxMailToSelection.m
initial sync
[scalable-opengroupware.org] / UI / MailerUI / UIxMailToSelection.m
1 /*
2  Copyright (C) 2004-2005 SKYRIX Software AG
3  
4  This file is part of OpenGroupware.org.
5  
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
9  later version.
10  
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.
15  
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
19  02111-1307, USA.
20 */
21
22 #include <SOGoUI/UIxComponent.h>
23
24 /*
25   UIxMailToSelection
26   
27   Select a set of address headers for composing an email.
28   
29   Bindings:
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
33   
34   Sample:
35   <var:component className="UIxMailToSelection"
36   to="to"
37   cc="cc"
38   bcc="bcc"
39   />
40 */
41
42 @class NSArray;
43
44 @interface UIxMailToSelection : UIxComponent
45 {
46   NSArray *to;
47   NSArray *cc;
48   NSArray *bcc;
49   id      item;
50   id      address;
51   NSArray *addressList;
52   int     currentIndex;
53 }
54
55 - (void)setTo:(NSArray *)_to;
56 - (NSArray *)to;
57 - (void)setCc:(NSArray *)_cc;
58 - (NSArray *)cc;
59 - (void)setBcc:(NSArray *)_bcc;
60 - (NSArray *)bcc;
61
62 - (NSArray *)properlySplitAddresses:(NSArray *)_addresses;
63
64 - (void)getAddressesFromFormValues:(NSDictionary *)_dict;
65 - (NSString *)getIndexFromIdentifier:(NSString *)_identifier;
66
67 @end
68
69 #include "common.h"
70 #include <NGMail/NGMail.h>
71
72 @implementation UIxMailToSelection
73
74 static NSArray *headers = nil;
75
76 + (void)initialize {
77   static BOOL didInit = NO;
78   if (didInit)
79     return;
80   
81   didInit = YES;
82   headers = [[NSArray alloc] initWithObjects:@"to", @"cc", @"bcc", nil];
83 }
84
85 - (id)init {
86   self = [super init];
87   if(self) {
88     self->currentIndex = 0;
89   }
90   return self;
91 }
92
93 - (void)dealloc {
94   [self->to          release];
95   [self->cc          release];
96   [self->bcc         release];
97   [self->item        release];
98   [self->address     release];
99   [self->addressList release];
100   [super dealloc];
101 }
102
103 /* accessors */
104
105 - (void)setTo:(NSArray *)_to
106 {
107   _to = [self properlySplitAddresses:_to];
108   ASSIGNCOPY(self->to, _to);
109 }
110
111 - (NSArray *) to
112 {
113   NSString *mailto;
114  
115   mailto = [self queryParameterForKey:@"mailto"];
116   if ([mailto length] > 0 && ![to count])
117     {
118       to = [NSArray arrayWithObject: mailto];
119       [to retain];
120     }
121
122   return self->to;
123 }
124
125 - (void)setCc:(NSArray *)_cc {
126   _cc = [self properlySplitAddresses:_cc];
127   ASSIGNCOPY(self->cc, _cc);
128 }
129
130 - (NSArray *)cc {
131   return self->cc;
132 }
133
134 - (void)setBcc:(NSArray *)_bcc {
135   _bcc = [self properlySplitAddresses:_bcc];
136   ASSIGNCOPY(self->bcc, _bcc);
137 }
138 - (NSArray *)bcc {
139   return self->bcc;
140 }
141
142 - (void)setAddressList:(NSArray *)_addressList {
143   ASSIGN(self->addressList, _addressList);
144 }
145 - (NSArray *)addressList {
146   return self->addressList;
147 }
148
149 - (void)setAddress:(id)_address {
150   ASSIGN(self->address, _address);
151 }
152 - (id)address {
153   return self->address;
154 }
155
156 - (void)setItem:(id)_item {
157   ASSIGN(self->item, _item);
158 }
159 - (id)item {
160   return self->item;
161 }
162
163 - (NSArray *)addressLists {
164   NSMutableArray *ma;
165   
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];
170   
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];
176   }
177   return ma;
178 }
179
180 - (NSArray *)headers {
181   return headers;
182 }
183
184 - (NSString *)currentHeader {
185   if(self->addressList == self->to)
186     return @"to";
187   else if(self->addressList == self->cc)
188     return @"cc";
189   return @"bcc";
190 }
191
192 /* identifiers */
193
194 - (NSString *)currentRowId {
195   char buf[16];
196   sprintf(buf, "row_%d", self->currentIndex);
197   return [NSString stringWithCString:buf];
198 }
199
200 - (NSString *)currentPopUpId {
201   char buf[16];
202   sprintf(buf, "popup_%d", self->currentIndex);
203   return [NSString stringWithCString:buf];
204 }
205
206 - (NSString *)currentAddressId {
207   char buf[16];
208   sprintf(buf, "addr_%d", self->currentIndex);
209   return [NSString stringWithCString:buf];
210 }
211
212 - (NSString *)nextId {
213   self->currentIndex++;
214   return @"";
215 }
216
217 /* address handling */
218
219 - (NSArray *)properlySplitAddresses:(NSArray *)_addresses {
220   NSString            *addrs;
221   NGMailAddressParser *parser;
222   NSArray             *result;
223   NSMutableArray      *ma;
224   unsigned            i, count;
225
226   if(!_addresses || [_addresses count] == 0)
227     return nil;
228
229   /* create one huge string, then split it using the parser */
230   addrs = [_addresses componentsJoinedByString:@","];
231   parser = [NGMailAddressParser mailAddressParserWithString:addrs];
232   result = [parser parseAddressList];
233   if(result == nil) {
234     [self debugWithFormat:@"Couldn't parse given addresses:%@", _addresses];
235     return _addresses;
236   }
237
238   count = [result count];
239   ma = [NSMutableArray arrayWithCapacity:count];
240   for (i = 0; i < count; i++) {
241     NGMailAddress   *addr;
242     NSMutableString *s;
243     BOOL hasName = NO;
244
245     s = [[NSMutableString alloc] init];
246     addr = [result objectAtIndex:i];
247     if([addr displayName]) {
248       [s appendString:[addr displayName]];
249       [s appendString:@" "];
250       hasName = YES;
251     }
252     if(hasName)
253       [s appendString:@"<"];
254     [s appendString:[addr address]];
255     if(hasName)
256       [s appendString:@">"];
257     [ma addObject:s];
258   }
259   return ma;
260 }
261
262 /* handling requests */
263
264 - (void)getAddressesFromFormValues:(NSDictionary *)_dict {
265   NSMutableArray *rawTo, *rawCc, *rawBcc;
266   NSArray *keys;
267   unsigned i, count;
268
269   rawTo  = [NSMutableArray arrayWithCapacity:4];
270   rawCc  = [NSMutableArray arrayWithCapacity:4];
271   rawBcc = [NSMutableArray arrayWithCapacity:2];
272   
273   keys  = [_dict allKeys];
274   count = [keys count];
275   for (i = 0; i < count; i++) {
276     NSString *key;
277     
278     key = [keys objectAtIndex:i];
279     if([key hasPrefix:@"addr_"]) {
280       NSString *idx, *addr, *popupKey, *popupValue;
281       
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];
290       else
291         [rawBcc addObject:addr];
292     }
293   }
294   
295   [self setTo:rawTo];
296   [self setCc:rawCc];
297   [self setBcc:rawBcc];
298 }
299
300 - (NSString *)getIndexFromIdentifier:(NSString *)_identifier {
301   NSRange r;
302   
303   r = [_identifier rangeOfString:@"_"];
304   return [_identifier substringFromIndex:NSMaxRange(r)];
305 }
306
307 - (void)takeValuesFromRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
308   /* OK, we have a special form value processor */
309   NSDictionary *d;
310
311   if ((d = [_rq formValues]) == nil)
312     return;
313
314 #if 0
315   [self debugWithFormat:@"Note: will take values ..."];
316   NSLog(@"%s formValues: %@",
317         __PRETTY_FUNCTION__,
318         d);
319 #endif
320   [self getAddressesFromFormValues:d];
321 }
322
323 - (int)addressCount {
324   return [self->to count] + [self->cc count] + [self->bcc count];
325 }
326
327 - (int)currentIndex {
328   int count;
329
330   count = [self addressCount];
331   return count > 0 ? count - 1 : 0;
332 }
333
334 @end /* UIxMailToSelection */