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