]> err.no Git - scalable-opengroupware.org/blob - UI/MailerUI/UIxEnvelopeAddressFormatter.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1123 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / MailerUI / UIxEnvelopeAddressFormatter.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 "UIxMailFormatter.h"
23 #include <NGImap4/NGImap4EnvelopeAddress.h>
24 #include "common.h"
25
26 @implementation UIxEnvelopeAddressFormatter
27
28 static Class EnvAddrClass = Nil;
29 static Class StrClass     = Nil;
30
31 + (void)initialize {
32   EnvAddrClass = [NGImap4EnvelopeAddress class];
33   StrClass     = [NSString       class];
34 }
35
36 - (id)initWithMaxLength:(unsigned int)_max generateFullEMail:(BOOL)_genFull {
37   if ((self = [super init])) {
38     self->maxLength = _max;
39     self->separator = @", ";
40     
41     self->eafFlags.fullEMail = _genFull ? 1 : 0;
42   }
43   return self;
44 }
45 - (id)init {
46   return [self initWithMaxLength:128 generateFullEMail:NO];
47 }
48
49 /* configuration */
50
51 - (unsigned)maxLength {
52   return self->maxLength;
53 }
54 - (NSString *)separator {
55   return self->separator;
56 }
57 - (BOOL)generateFullEMail {
58   return self->eafFlags.fullEMail ? YES : NO;
59 }
60
61 /* formatting envelope addresses */
62
63 - (NSString *)stringForEnvelopeAddress:(NGImap4EnvelopeAddress *)_address {
64   NSString *s;
65
66   if ([self generateFullEMail])
67     return [_address email];
68   
69   s = [_address personalName];
70   if ([s isNotNull]) return s;
71   
72   s = [_address baseEMail];
73   if ([s isNotNull]) return s;
74   
75   [self warnWithFormat:@"unexpected envelope address: %@", _address];
76   return [_address stringValue];
77 }
78
79 - (NSString *)stringForArray:(NSArray *)_addresses {
80   NSMutableString *ms;
81   unsigned i, count;
82   
83   if ((count = [_addresses count]) == 0)
84     return nil;
85   
86   if (count == 1)
87     return [self stringForObjectValue:[_addresses objectAtIndex:0]];
88   
89   ms = [NSMutableString stringWithCapacity:16 * count];
90   for (i = 0; i < count && [ms length] < [self maxLength]; i++) {
91     NSString *s;
92     
93     s = [self stringForObjectValue:[_addresses objectAtIndex:i]];
94     if (s == nil)
95       continue;
96     
97     if ([ms length] > 0) [ms appendString:[self separator]];
98     [ms appendString:s];
99   }
100   return ms;
101 }
102
103 /* formatter entry function */
104
105 - (NSString *)stringForObjectValue:(id)_address {
106   if (![_address isNotNull])
107     return nil;
108   
109   if ([_address isKindOfClass:StrClass]) /* preformatted? */
110     return _address;
111   
112   if ([_address isKindOfClass:EnvAddrClass])
113     return [self stringForEnvelopeAddress:_address];
114   
115   if ([_address isKindOfClass:[NSArray class]])
116     return [self stringForArray:_address];
117
118   [self debugWithFormat:
119           @"NOTE: unexpected object for envelope formatter: %@<%@>",
120           _address, NSStringFromClass([_address class])];
121   return [_address stringValue];
122 }
123
124 @end /* UIxEnvelopeAddressFormatter */