]> err.no Git - scalable-opengroupware.org/blob - UI/MailerUI/UIxMailToSelection.m
fixed compilation in new hierarchy
[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   _to = [self properlySplitAddresses:_to];
107   ASSIGNCOPY(self->to, _to);
108 }
109 - (NSArray *)to {
110   return self->to;
111 }
112
113 - (void)setCc:(NSArray *)_cc {
114   _cc = [self properlySplitAddresses:_cc];
115   ASSIGNCOPY(self->cc, _cc);
116 }
117 - (NSArray *)cc {
118   return self->cc;
119 }
120
121 - (void)setBcc:(NSArray *)_bcc {
122   _bcc = [self properlySplitAddresses:_bcc];
123   ASSIGNCOPY(self->bcc, _bcc);
124 }
125 - (NSArray *)bcc {
126   return self->bcc;
127 }
128
129 - (void)setAddressList:(NSArray *)_addressList {
130   ASSIGN(self->addressList, _addressList);
131 }
132 - (NSArray *)addressList {
133   return self->addressList;
134 }
135
136 - (void)setAddress:(id)_address {
137   ASSIGN(self->address, _address);
138 }
139 - (id)address {
140   return self->address;
141 }
142
143 - (void)setItem:(id)_item {
144   ASSIGN(self->item, _item);
145 }
146 - (id)item {
147   return self->item;
148 }
149
150 - (NSArray *)addressLists {
151   NSMutableArray *ma;
152   
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];
157   
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];
163   }
164   return ma;
165 }
166
167 - (NSArray *)headers {
168   return headers;
169 }
170
171 - (NSString *)currentHeader {
172   if(self->addressList == self->to)
173     return @"to";
174   else if(self->addressList == self->cc)
175     return @"cc";
176   return @"bcc";
177 }
178
179 /* identifiers */
180
181 - (NSString *)currentRowId {
182   char buf[16];
183   sprintf(buf, "row_%d", self->currentIndex);
184   return [NSString stringWithCString:buf];
185 }
186
187 - (NSString *)currentPopUpId {
188   char buf[16];
189   sprintf(buf, "popup_%d", self->currentIndex);
190   return [NSString stringWithCString:buf];
191 }
192
193 - (NSString *)currentAddressId {
194   char buf[16];
195   sprintf(buf, "addr_%d", self->currentIndex);
196   return [NSString stringWithCString:buf];
197 }
198
199 - (NSString *)nextId {
200   self->currentIndex++;
201   return @"";
202 }
203
204 /* address handling */
205
206 - (NSArray *)properlySplitAddresses:(NSArray *)_addresses {
207   NSString            *addrs;
208   NGMailAddressParser *parser;
209   NSArray             *result;
210   NSMutableArray      *ma;
211   unsigned            i, count;
212
213   if(!_addresses || [_addresses count] == 0)
214     return nil;
215
216   /* create one huge string, then split it using the parser */
217   addrs = [_addresses componentsJoinedByString:@","];
218   parser = [NGMailAddressParser mailAddressParserWithString:addrs];
219   result = [parser parseAddressList];
220   if(result == nil) {
221     [self debugWithFormat:@"Couldn't parse given addresses:%@", _addresses];
222     return _addresses;
223   }
224
225   count = [result count];
226   ma = [NSMutableArray arrayWithCapacity:count];
227   for (i = 0; i < count; i++) {
228     NGMailAddress   *addr;
229     NSMutableString *s;
230     BOOL hasName = NO;
231
232     s = [[NSMutableString alloc] init];
233     addr = [result objectAtIndex:i];
234     if([addr displayName]) {
235       [s appendString:[addr displayName]];
236       [s appendString:@" "];
237       hasName = YES;
238     }
239     if(hasName)
240       [s appendString:@"<"];
241     [s appendString:[addr address]];
242     if(hasName)
243       [s appendString:@">"];
244     [ma addObject:s];
245   }
246   return ma;
247 }
248
249 /* handling requests */
250
251 - (void)getAddressesFromFormValues:(NSDictionary *)_dict {
252   NSMutableArray *rawTo, *rawCc, *rawBcc;
253   NSArray *keys;
254   unsigned i, count;
255
256   rawTo  = [NSMutableArray arrayWithCapacity:4];
257   rawCc  = [NSMutableArray arrayWithCapacity:4];
258   rawBcc = [NSMutableArray arrayWithCapacity:2];
259   
260   keys  = [_dict allKeys];
261   count = [keys count];
262   for (i = 0; i < count; i++) {
263     NSString *key;
264     
265     key = [keys objectAtIndex:i];
266     if([key hasPrefix:@"addr_"]) {
267       NSString *idx, *addr, *popupKey, *popupValue;
268       
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];
277       else
278         [rawBcc addObject:addr];
279     }
280   }
281   
282   [self setTo:rawTo];
283   [self setCc:rawCc];
284   [self setBcc:rawBcc];
285 }
286
287 - (NSString *)getIndexFromIdentifier:(NSString *)_identifier {
288   NSRange r;
289   
290   r = [_identifier rangeOfString:@"_"];
291   return [_identifier substringFromIndex:NSMaxRange(r)];
292 }
293
294 - (void)takeValuesFromRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
295   /* OK, we have a special form value processor */
296   NSDictionary *d;
297
298   if ((d = [_rq formValues]) == nil)
299     return;
300
301 #if 0
302   [self debugWithFormat:@"Note: will take values ..."];
303   NSLog(@"%s formValues: %@",
304         __PRETTY_FUNCTION__,
305         d);
306 #endif
307   [self getAddressesFromFormValues:d];
308 }
309
310 - (int)addressCount {
311   return [self->to count] + [self->cc count] + [self->bcc count];
312 }
313
314 - (int)currentIndex {
315   int count;
316
317   count = [self addressCount];
318   return count > 0 ? count - 1 : 0;
319 }
320
321 @end /* UIxMailToSelection */