File: UIxMailToSelection.m

package info (click to toggle)
sogo 2.2.9%2Bgit20141017-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 43,812 kB
  • sloc: objc: 115,592; python: 5,696; sh: 1,380; perl: 861; makefile: 240; xml: 114; sql: 53; php: 43
file content (336 lines) | stat: -rw-r--r-- 6,593 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
 Copyright (C) 2004-2005 SKYRIX Software AG
 
 This file is part of OpenGroupware.org.
 
 OGo is free software; you can redistribute it and/or modify it under
 the terms of the GNU Lesser General Public License as published by the
 Free Software Foundation; either version 2, or (at your option) any
 later version.
 
 OGo is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or
 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 License for more details.
 
 You should have received a copy of the GNU Lesser General Public
 License along with OGo; see the file COPYING.  If not, write to the
 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 02111-1307, USA.
*/

#import <Foundation/NSDictionary.h>
#import <Foundation/NSEnumerator.h>

#import <NGObjWeb/WORequest.h>
#import <NGExtensions/NSNull+misc.h>
#import <NGMail/NGMailAddress.h>
#import <NGMail/NGMailAddressParser.h>

#import <SOGoUI/UIxComponent.h>

/*
  UIxMailToSelection
  
  Select a set of address headers for composing an email.
  
  Bindings:
  to   - array of strings suitable for placement in a To: header
  cc   - array of strings suitable for placement in a Cc: header
  bcc  - array of strings suitable for placement in a Bcc: header
  
  Sample:
  <var:component className="UIxMailToSelection"
  to="to"
  cc="cc"
  bcc="bcc"
  />
*/

@class NSArray;

@interface UIxMailToSelection : UIxComponent
{
  NSArray *to;
  NSArray *cc;
  NSArray *bcc;
  id      item;
  id      address;
  NSArray *addressList;
  int     currentIndex;
}

- (void) setTo: (NSArray *) _to;
- (NSArray *) to;
- (void) setCc: (NSArray *) _cc;
- (NSArray *) cc;
- (void) setBcc: (NSArray *) _bcc;
- (NSArray *) bcc;

- (void) getAddressesFromFormValues: (NSDictionary *) _dict;
- (NSString *) getIndexFromIdentifier: (NSString *) _identifier;

@end

@implementation UIxMailToSelection

static NSArray *headers = nil;

+ (void) initialize
{
  static BOOL didInit = NO;
  if (!didInit)
    {
      didInit = YES;
      headers = [[NSArray alloc] initWithObjects: @"to", @"cc", @"bcc", nil];
    }
}

- (id) init
{
  if ((self = [super init]))
    currentIndex = -1;

  return self;
}

- (void) dealloc
{
  [to          release];
  [cc          release];
  [bcc         release];
  [item        release];
  [address     release];
  [addressList release];
  [super dealloc];
}

/* accessors */

- (void) setTo: (NSArray *) _to
{
  ASSIGN (to, _to);
}

- (NSArray *) to
{
  NSString *mailto;
 
  mailto = [self queryParameterForKey: @"mailto"];
  if ([mailto length] > 0 && ![to count])
    {
      to = [NSArray arrayWithObject: mailto];
      [to retain];
    }

  return to;
}

- (void) setCc: (NSArray *) _cc
{
  ASSIGN (cc, _cc);
}

- (NSArray *) cc
{
  return cc;
}

- (void) setBcc: (NSArray *) _bcc
{
  ASSIGN (bcc, _bcc);
}

- (NSArray *) bcc
{
  return bcc;
}

- (void) setAddressList: (NSArray *) _addressList
{
  ASSIGN (addressList, _addressList);
}

- (NSArray *) addressList
{
  return addressList;
}

- (void) setAddress: (id) _address
{
  ASSIGN (address, _address);
}

- (id) address
{
  return address;
}

- (void) setItem: (id) _item
{
  ASSIGN (item, _item);
}

- (id) item
{
  return item;
}

- (NSArray *) addressLists
{
  NSMutableArray *ma;
  NSArray *tmp;

  ma = [NSMutableArray arrayWithCapacity:3];
  if ([to isNotNull] && [to count] > 0)
    [ma addObject: to];
  if ([cc isNotNull])
    [ma addObject: cc];
  if ([bcc isNotNull])
    [ma addObject: bcc];

  /* ensure that at least one object is available */
  if ([ma count] == 0)
    {
      tmp = [NSArray arrayWithObject: @""];
      ASSIGN (to, tmp);
      [ma addObject: to];
    }

  return ma;
}

- (NSArray *) headers
{
  return headers;
}

- (NSString *) currentHeader
{
  if (addressList == to)
    return @"to";
  else if (addressList == cc)
    return @"cc";

  return @"bcc";
}

/* identifiers */

- (NSString *) nextId
{
  currentIndex++;

  return @"";
}

- (NSString *) currentRowId
{
  [self nextId];
  
  return [NSString stringWithFormat: @"row_%d", currentIndex];
}

- (NSString *) currentPopUpId
{
  
  return [NSString stringWithFormat: @"popup_%d", currentIndex];
}

- (NSString *) currentAddressId
{
  return [NSString stringWithFormat: @"addr_%d", currentIndex];
}

/* handling requests */

- (void) _fillAddresses: (NSMutableArray *) addresses
	     withObject: (id) object
{
  NSEnumerator *list;
  NSString *currentAddress;

  if ([object isKindOfClass: [NSString class]])
    [addresses addObject: object];
  else if ([object isKindOfClass: [NSArray class]])
    {
      list = [object objectEnumerator];
      while ((currentAddress
	      = [[list nextObject] stringByTrimmingSpaces]))
	if ([currentAddress length])
	  [addresses addObject: currentAddress];
    }
}

- (void) getAddressesFromFormValues: (NSDictionary *) _dict
{
  NSMutableArray *rawTo, *rawCc, *rawBcc;
  NSString *idx, *popupKey, *popupValue;
  NSArray *keys;
  unsigned i, count;
  id addr;

  rawTo  = [NSMutableArray arrayWithCapacity:4];
  rawCc  = [NSMutableArray arrayWithCapacity:4];
  rawBcc = [NSMutableArray arrayWithCapacity:2];
  
  keys  = [_dict allKeys];
  count = [keys count];
  for (i = 0; i < count; i++)
    {
      NSString *key;
    
      key = [keys objectAtIndex:i];
      if ([key hasPrefix:@"addr_"])
	{
	  addr = [_dict objectForKey:key];
	  idx  = [self getIndexFromIdentifier:key];
	  popupKey = [NSString stringWithFormat:@"popup_%@", idx];
	  popupValue = [[_dict objectForKey:popupKey] lastObject];
	  if([popupValue isEqualToString:@"0"])
	    [self _fillAddresses: rawTo withObject: addr];
	  else if([popupValue isEqualToString:@"1"])
	    [self _fillAddresses: rawCc withObject: addr];
	  else
	    [self _fillAddresses: rawBcc withObject: addr];
	}
    }
  
  [self setTo: rawTo];
  [self setCc: rawCc];
  [self setBcc: rawBcc];
}

- (NSString *) getIndexFromIdentifier: (NSString *) _identifier
{
  NSRange r;
  
  r = [_identifier rangeOfString: @"_"];

  return [_identifier substringFromIndex: NSMaxRange(r)];
}

- (void) takeValuesFromRequest: (WORequest *) _rq
		     inContext: (WOContext *) _ctx
{
  /* OK, we have a special form value processor */
  NSDictionary *d;

  if ((d = [_rq formValues]) == nil)
    return;

#if 0
  [self debugWithFormat:@"Note: will take values ..."];
  NSLog(@"%s formValues: %@",
        __PRETTY_FUNCTION__,
        d);
#endif
  [self getAddressesFromFormValues: d];
}

- (int) addressCount
{
  return [to count] + [cc count] + [bcc count];
}

@end /* UIxMailToSelection */