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