]> err.no Git - sope/blob - sope-mime/NGMime/NGMimeBodyParser.m
synced with latest additions and bumped framework versions
[sope] / sope-mime / NGMime / NGMimeBodyParser.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
6   SOPE 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   SOPE 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 SOPE; 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 "NGMimeBodyParser.h"
23 #include "NGMimeBodyPartParser.h"
24 #include "NGMimeMultipartBody.h"
25 #include "common.h"
26
27 @implementation NGMimeBodyParser
28
29 + (int)version {
30   return 2;
31 }
32
33 - (id)parseBodyOfPart:(id<NGMimePart>)_part
34   data:(NSData *)_data
35   delegate:(id)_d
36 {
37   return _data;
38 }
39
40 @end /* NGMimeBodyParser */
41
42 @implementation NGMimeTextBodyParser
43
44 static int UseFoundationStringEncodingForMimeText = -1;
45
46 + (int)version {
47   return 2;
48 }
49 + (void)initialize {
50   NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
51   
52   NSAssert2([super version] == 2,
53             @"invalid superclass (%@) version %i !",
54             NSStringFromClass([self superclass]), [super version]);
55   if (UseFoundationStringEncodingForMimeText == -1) {
56     UseFoundationStringEncodingForMimeText =
57       [ud boolForKey:@"UseFoundationStringEncodingForMimeText"]?1:0;
58   }
59 }
60
61 - (id)parseBodyOfPart:(id<NGMimePart>)_part data:(NSData *)_data
62   delegate:(id)_d
63 {
64   NSString *charset;
65   id       ctype, body;
66
67   if (_data == nil) return nil;
68   
69   ctype = [_part contentType];
70   
71   if (![ctype isKindOfClass:[NGMimeType class]])
72     ctype = [NGMimeType mimeType:[ctype stringValue]];
73   
74   charset = [[ctype valueOfParameter:NGMimeParameterTextCharset]
75                     lowercaseString];
76   body     = nil;
77   
78   if (!UseFoundationStringEncodingForMimeText) {
79     if (![_data length])
80       return @"";
81
82     if (![[charset lowercaseString] isEqualToString:@"us-ascii"] &&
83         [charset length]) {
84       body = [NSString stringWithData:_data usingEncodingNamed:charset];
85     }
86   }
87   if (!body) {
88     NSStringEncoding encoding;
89     
90     encoding = [NGMimeType stringEncodingForCharset:charset];
91   
92     body = [[[NSString alloc]
93                        initWithData:_data
94                        encoding:encoding] autorelease];
95   }
96   return body;
97 }
98
99 @end /* NGMimeTextBodyParser */
100