]> err.no Git - sope/blob - sope-mime/NGMime/NGMimeTextBodyGenerator.m
fixed yet another issue with compilation on Cocoa ;-)
[sope] / sope-mime / NGMime / NGMimeTextBodyGenerator.m
1 /*
2   Copyright (C) 2000-2007 SKYRIX Software AG
3   Copyright (C) 2007      Helge Hess
4
5   This file is part of SOPE.
6
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
10   later version.
11
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.
16
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
20   02111-1307, USA.
21 */
22
23 #include "NGMimeBodyGenerator.h"
24 #include "NGMimePartGenerator.h"
25 #include <NGExtensions/NSString+Encoding.h>
26 #include "common.h"
27
28 @implementation NGMimeTextBodyGenerator
29
30 + (int)version {
31   return 2;
32 }
33 + (void)initialize {
34   NSAssert2([super version] == 2,
35             @"invalid superclass (%@) version %i !",
36             NSStringFromClass([self superclass]), [super version]);
37 }
38
39 - (NSStringEncoding)_encodingFromContentType:(NGMimeType *)_type {
40   NSStringEncoding encoding;
41   NSString *charset;
42
43   encoding = [NSString defaultCStringEncoding];
44   if (_type != nil) {
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 */
52     }
53   }
54     
55   return encoding;
56 }
57
58 - (NSData *)generateBodyOfPart:(id<NGMimePart>)_part
59   additionalHeaders:(NGMutableHashMap *)_addHeaders
60   delegate:(id)_delegate
61 {
62   NSData *data;
63   id     body;
64   
65   body = [_part body];
66   data = nil;
67   
68   if ([body isKindOfClass:[NSString class]]) {
69     NSString *charset = [[_part contentType] valueOfParameter:@"charset"];
70     if ([charset isNotEmpty])
71       data = [body dataUsingEncodingNamed:charset];
72     
73     if (data == nil) /* either no charset given or the charset failed */
74       data = [body dataUsingEncoding:[NSString defaultCStringEncoding]];
75   }
76   else if ([body respondsToSelector:@selector(bytes)]) {
77     /* an NSData, but why not check the class?!, we can't just cast it */
78     data = body;
79   }
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"];
84     
85     if ([charset isNotEmpty]) {
86 #if NeXT_Foundation_LIBRARY || COCOA_Foundation_LIBRARY
87       encoding = [NSString stringEncodingForEncodingNamed:charset];
88 #else
89       encoding = [self _encodingFromContentType:[_part contentType]];
90 #endif
91     }
92     
93     data = [body dataUsingEncoding:
94                    (encoding != 0 ? encoding : NSISOLatin1StringEncoding)];
95   }
96   else if (body != nil) {
97     [self errorWithFormat:@"unexpected part body %@: %@", [body class], body];
98     return nil;
99   }
100   
101   if (data == nil) {
102     [self warnWithFormat:@"%s: generate empty body", __PRETTY_FUNCTION__];
103     data = [NSData data];
104   }
105   return [self encodeData:data forPart:_part additionalHeaders:_addHeaders];
106 }
107
108 @end /* NGMimeTextBodyGenerator */