]> err.no Git - scalable-opengroupware.org/blob - UI/MailerUI/UIxMailToSelection.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1238 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 {
80   static BOOL didInit = NO;
81   if (!didInit)
82     {
83       didInit = YES;
84       headers = [[NSArray alloc] initWithObjects: @"to", @"cc", @"bcc", nil];
85     }
86 }
87
88 - (id) init
89 {
90   if ((self = [super init]))
91     currentIndex = 0;
92
93   return self;
94 }
95
96 - (void) dealloc
97 {
98   [to          release];
99   [cc          release];
100   [bcc         release];
101   [item        release];
102   [address     release];
103   [addressList release];
104   [super dealloc];
105 }
106
107 /* accessors */
108
109 - (void) setTo: (NSArray *) _to
110 {
111   ASSIGN (to, [self properlySplitAddresses: _to]);
112 }
113
114 - (NSArray *) to
115 {
116   NSString *mailto;
117  
118   mailto = [self queryParameterForKey: @"mailto"];
119   if ([mailto length] > 0 && ![to count])
120     {
121       to = [NSArray arrayWithObject: mailto];
122       [to retain];
123     }
124
125   return to;
126 }
127
128 - (void) setCc: (NSArray *) _cc
129 {
130   ASSIGN (cc, [self properlySplitAddresses: _cc]);
131 }
132
133 - (NSArray *) cc
134 {
135   return cc;
136 }
137
138 - (void) setBcc: (NSArray *) _bcc
139 {
140   ASSIGN (bcc, [self properlySplitAddresses: _bcc]);
141 }
142
143 - (NSArray *) bcc
144 {
145   return bcc;
146 }
147
148 - (void) setAddressList: (NSArray *) _addressList
149 {
150   ASSIGN (addressList, _addressList);
151 }
152
153 - (NSArray *) addressList
154 {
155   return addressList;
156 }
157
158 - (void) setAddress: (id) _address
159 {
160   ASSIGN (address, _address);
161 }
162
163 - (id) address
164 {
165   return address;
166 }
167
168 - (void) setItem: (id) _item
169 {
170   ASSIGN (item, _item);
171 }
172
173 - (id) item
174 {
175   return item;
176 }
177
178 - (NSArray *) addressLists
179 {
180   NSMutableArray *ma;
181   
182   ma = [NSMutableArray arrayWithCapacity:3];
183   if ([to isNotNull])
184     [ma addObject: to];
185   if ([cc isNotNull])
186     [ma addObject: cc];
187   if ([bcc isNotNull])
188     [ma addObject: bcc];
189
190   /* ensure that at least one object is available */
191   if ([ma count] == 0)
192     {
193       NSArray *tmp = [NSArray arrayWithObject:@""];
194       ASSIGN (to, tmp);
195       [ma addObject:to];
196     }
197
198   return ma;
199 }
200
201 - (NSArray *) headers
202 {
203   return headers;
204 }
205
206 - (NSString *) currentHeader
207 {
208   if (addressList == to)
209     return @"to";
210   else if (addressList == cc)
211     return @"cc";
212
213   return @"bcc";
214 }
215
216 /* identifiers */
217
218 - (NSString *) currentRowId
219 {
220   return [NSString stringWithFormat: @"row_%d", currentIndex];
221 }
222
223 - (NSString *) currentPopUpId
224 {
225   return [NSString stringWithFormat: @"popup_%d", currentIndex];
226 }
227
228 - (NSString *) currentAddressId
229 {
230   return [NSString stringWithFormat: @"addr_%d", currentIndex];
231 }
232
233 - (NSString *) nextId
234 {
235   currentIndex++;
236
237   return @"";
238 }
239
240 /* address handling */
241
242 - (NSArray *) properlySplitAddresses: (NSArray *) _addresses
243 {
244   NSString            *addrs;
245   NGMailAddressParser *parser;
246   NSArray             *result;
247   NSMutableArray      *ma;
248   unsigned            i, count;
249
250   if (!_addresses || [_addresses count] == 0)
251     return nil;
252
253   /* create one huge string, then split it using the parser */
254   addrs = [_addresses componentsJoinedByString:@","];
255   parser = [NGMailAddressParser mailAddressParserWithString:addrs];
256   result = [parser parseAddressList];
257   if(result == nil) {
258     [self debugWithFormat:@"Couldn't parse given addresses:%@", _addresses];
259     return _addresses;
260   }
261
262   count = [result count];
263   ma = [NSMutableArray arrayWithCapacity:count];
264   for (i = 0; i < count; i++) {
265     NGMailAddress   *addr;
266     NSMutableString *s;
267     BOOL hasName = NO;
268
269     s = [[NSMutableString alloc] init];
270     addr = [result objectAtIndex:i];
271     if([addr displayName]) {
272       [s appendString:[addr displayName]];
273       [s appendString:@" "];
274       hasName = YES;
275     }
276     if(hasName)
277       [s appendString:@"<"];
278     [s appendString:[addr address]];
279     if(hasName)
280       [s appendString:@">"];
281     [ma addObject:s];
282   }
283   return ma;
284 }
285
286 /* handling requests */
287
288 - (void) getAddressesFromFormValues: (NSDictionary *) _dict
289 {
290   NSMutableArray *rawTo, *rawCc, *rawBcc;
291   NSArray *keys;
292   unsigned i, count;
293
294   rawTo  = [NSMutableArray arrayWithCapacity:4];
295   rawCc  = [NSMutableArray arrayWithCapacity:4];
296   rawBcc = [NSMutableArray arrayWithCapacity:2];
297   
298   keys  = [_dict allKeys];
299   count = [keys count];
300   for (i = 0; i < count; i++)
301     {
302       NSString *key;
303     
304       key = [keys objectAtIndex:i];
305       if ([key hasPrefix:@"addr_"])
306         {
307           NSString *idx, *addr, *popupKey, *popupValue;
308           
309           addr = [[_dict objectForKey:key] lastObject];
310           idx  = [self getIndexFromIdentifier:key];
311           popupKey = [NSString stringWithFormat:@"popup_%@", idx];
312           popupValue = [[_dict objectForKey:popupKey] lastObject];
313           if([popupValue isEqualToString:@"0"])
314             [rawTo addObject:addr];
315           else if([popupValue isEqualToString:@"1"])
316             [rawCc addObject:addr];
317           else
318             [rawBcc addObject:addr];
319         }
320     }
321   
322   [self setTo: rawTo];
323   [self setCc: rawCc];
324   [self setBcc: rawBcc];
325 }
326
327 - (NSString *) getIndexFromIdentifier: (NSString *) _identifier
328 {
329   NSRange r;
330   
331   r = [_identifier rangeOfString: @"_"];
332
333   return [_identifier substringFromIndex: NSMaxRange(r)];
334 }
335
336 - (void) takeValuesFromRequest: (WORequest *) _rq
337                      inContext: (WOContext *) _ctx
338 {
339   /* OK, we have a special form value processor */
340   NSDictionary *d;
341
342   if ((d = [_rq formValues]) == nil)
343     return;
344
345 #if 0
346   [self debugWithFormat:@"Note: will take values ..."];
347   NSLog(@"%s formValues: %@",
348         __PRETTY_FUNCTION__,
349         d);
350 #endif
351   [self getAddressesFromFormValues: d];
352 }
353
354 - (int) addressCount
355 {
356   return [to count] + [cc count] + [bcc count];
357 }
358
359 - (int) currentIndex
360 {
361   int count;
362
363   count = [self addressCount];
364
365   return count > 0 ? count - 1 : 0;
366 }
367
368 @end /* UIxMailToSelection */