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