2 Copyright (C) 2004-2005 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
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
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.
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
22 #import <Foundation/NSArray.h>
24 #import <NGExtensions/NSNull+misc.h>
25 #import <NGExtensions/NSObject+Logs.h>
26 #import <NGExtensions/NSObject+Values.h>
27 #import <NGImap4/NGImap4EnvelopeAddress.h>
29 #import "UIxMailFormatter.h"
31 @implementation UIxEnvelopeAddressFormatter
33 static Class EnvAddrClass = Nil;
34 static Class StrClass = Nil;
37 EnvAddrClass = [NGImap4EnvelopeAddress class];
38 StrClass = [NSString class];
41 - (id)initWithMaxLength:(unsigned int)_max generateFullEMail:(BOOL)_genFull {
42 if ((self = [super init])) {
43 self->maxLength = _max;
44 self->separator = @", ";
46 self->eafFlags.fullEMail = _genFull ? 1 : 0;
51 return [self initWithMaxLength:128 generateFullEMail:NO];
56 - (unsigned)maxLength {
57 return self->maxLength;
59 - (NSString *)separator {
60 return self->separator;
62 - (BOOL)generateFullEMail {
63 return self->eafFlags.fullEMail ? YES : NO;
66 /* formatting envelope addresses */
68 - (NSString *)stringForEnvelopeAddress:(NGImap4EnvelopeAddress *)_address {
71 if ([self generateFullEMail])
72 return [_address email];
74 s = [_address personalName];
75 if ([s isNotNull]) return s;
77 s = [_address baseEMail];
78 if ([s isNotNull]) return s;
80 [self warnWithFormat:@"unexpected envelope address: %@", _address];
81 return [_address stringValue];
84 - (NSString *)stringForArray:(NSArray *)_addresses {
88 if ((count = [_addresses count]) == 0)
92 return [self stringForObjectValue:[_addresses objectAtIndex:0]];
94 ms = [NSMutableString stringWithCapacity:16 * count];
95 for (i = 0; i < count && [ms length] < [self maxLength]; i++) {
98 s = [self stringForObjectValue:[_addresses objectAtIndex:i]];
102 if ([ms length] > 0) [ms appendString:[self separator]];
108 /* formatter entry function */
110 - (NSString *)stringForObjectValue:(id)_address {
111 if (![_address isNotNull])
114 if ([_address isKindOfClass:StrClass]) /* preformatted? */
117 if ([_address isKindOfClass:EnvAddrClass])
118 return [self stringForEnvelopeAddress:_address];
120 if ([_address isKindOfClass:[NSArray class]])
121 return [self stringForArray:_address];
123 [self debugWithFormat:
124 @"NOTE: unexpected object for envelope formatter: %@<%@>",
125 _address, NSStringFromClass([_address class])];
126 return [_address stringValue];
129 @end /* UIxEnvelopeAddressFormatter */