2 Copyright (C) 2000-2007 SKYRIX Software AG
3 Copyright (C) 2007 Helge Hess
5 This file is part of SOPE.
7 SOPE is free software; you can redistribute it and/or modify it under
8 the terms of the GNU Lesser General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with SOPE; see the file COPYING. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include "NGMimeBodyGenerator.h"
24 #include "NGMimePartGenerator.h"
25 #include <NGExtensions/NSString+Encoding.h>
28 @implementation NGMimeTextBodyGenerator
34 NSAssert2([super version] == 2,
35 @"invalid superclass (%@) version %i !",
36 NSStringFromClass([self superclass]), [super version]);
39 - (NSStringEncoding)_encodingFromContentType:(NGMimeType *)_type {
40 NSStringEncoding encoding;
43 encoding = [NSString defaultCStringEncoding];
45 charset = [_type valueOfParameter: @"charset"];
46 if ([charset length] > 0) {
47 if ([charset isEqualToString: @"utf-8"])
48 encoding = NSUTF8StringEncoding;
49 else if ([charset isEqualToString: @"iso-8859-1"])
50 encoding = NSISOLatin1StringEncoding;
51 /* more should be handled here */
58 - (NSData *)generateBodyOfPart:(id<NGMimePart>)_part
59 additionalHeaders:(NGMutableHashMap *)_addHeaders
60 delegate:(id)_delegate
68 if ([body isKindOfClass:[NSString class]]) {
69 NSString *charset = [[_part contentType] valueOfParameter:@"charset"];
70 if ([charset isNotEmpty])
71 data = [body dataUsingEncodingNamed:charset];
73 if (data == nil) /* either no charset given or the charset failed */
74 data = [body dataUsingEncoding:[NSString defaultCStringEncoding]];
76 else if ([body respondsToSelector:@selector(bytes)]) {
77 /* an NSData, but why not check the class?!, we can't just cast it */
80 else if ([body respondsToSelector:@selector(dataUsingEncoding:)]) {
81 /* hm, whats that?, NSString is covered before */
82 NSStringEncoding encoding = 0;
83 NSString *charset = [[_part contentType] valueOfParameter:@"charset"];
85 if ([charset isNotEmpty]) {
86 #if NeXT_Foundation_LIBRARY || COCOA_Foundation_LIBRARY
87 encoding = [NSString stringEncodingForEncodingNamed:charset];
89 encoding = [self _encodingFromContentType:[_part contentType]];
93 data = [body dataUsingEncoding:
94 (encoding != 0 ? encoding : NSISOLatin1StringEncoding)];
96 else if (body != nil) {
97 [self errorWithFormat:@"unexpected part body %@: %@", [body class], body];
102 [self warnWithFormat:@"%s: generate empty body", __PRETTY_FUNCTION__];
103 data = [NSData data];
105 return [self encodeData:data forPart:_part additionalHeaders:_addHeaders];
108 @end /* NGMimeTextBodyGenerator */