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