]> err.no Git - sope/blob - sope-mime/NGMime/NGMimeBodyPart.m
minor fixes
[sope] / sope-mime / NGMime / NGMimeBodyPart.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 "NGMimeBodyPart.h"
24 #include "NGMimeType.h"
25 #include "common.h"
26 #include "NGMimeFileData.h"
27 #include <NGMime/NGMimePartParser.h>
28
29 @implementation NGMimeBodyPart
30
31 + (int)version {
32   return 2;
33 }
34
35 static NGMimeType *defaultType = nil;
36
37 + (void)initialize {
38   static BOOL isInitialized = NO;
39   if (!isInitialized) {
40     isInitialized = YES;
41     
42     defaultType =
43       [[NGMimeType mimeType:@"text/plain; charset=us-ascii"] retain];
44   }
45 }
46   
47 + (id)bodyPartWithHeader:(NGHashMap *)_header {
48   return [[[self alloc] initWithHeader:_header] autorelease];
49 }
50
51 - (id)initWithHeader:(NGHashMap *)_header {
52   if ((self = [super init])) {
53     self->header = [_header retain];
54     self->body   = nil;
55   }
56   return self;
57 }
58 - (id)init {
59   return [self initWithHeader:nil];
60 }
61
62 - (void)dealloc {
63   [self->header release];
64   [self->body   release];
65   [super dealloc];
66 }
67
68 /* NGPart */
69
70 - (NSEnumerator *)valuesOfHeaderFieldWithName:(NSString *)_name {
71   return [self->header objectEnumeratorForKey:_name];
72 }
73 - (NSEnumerator *)headerFieldNames {
74   return [self->header keyEnumerator];
75 }
76
77 - (void)setBody:(id)_body {
78   ASSIGN(self->body, _body);
79 }
80 - (id)body {
81   return self->body;
82 }
83
84 /* NGMimePart */
85
86 - (NGMimeType *)contentType {
87   id type;
88   static NGMimeHeaderNames *Fields = NULL;
89
90   if (!Fields)
91     Fields = (NGMimeHeaderNames *)[NGMimePartParser headerFieldNames];
92   
93   
94   type = [self->header objectForKey:Fields->contentType];
95   
96   if (![type isKindOfClass:[NGMimeType class]])
97     type = [NGMimeType mimeType:[type stringValue]];
98   
99   return (type != nil ? type : defaultType);
100 }
101
102 - (NSString *)contentId {
103   return [[self->header objectForKey:@"content-id"] stringValue];
104 }
105
106 - (NSArray *)contentLanguage {
107   id value;
108   
109   value = [self->header objectForKey:@"content-language"];
110   if (![value isKindOfClass:[NSArray class]])
111     value = [value componentsSeparatedByString:@","];
112
113   return value;
114 }
115
116 - (NSString *)contentMd5 {
117   return [[self->header objectForKey:@"content-md5"] stringValue];
118 }
119
120 - (NSString *)encoding {
121   return [[self->header objectForKey:@"content-transfer-encoding"]
122                         stringValue];
123 }
124
125 - (NSString *)contentDescription {
126   return [[self->header objectForKey:@"content-description"] stringValue];
127 }
128
129 /* description */
130
131 - (NSString *)description {
132   NSMutableString *d = [[NSMutableString alloc] init];
133   id b = [self body];
134
135   [d appendFormat:@"<%@[0x%08X]: header=%@",
136        NSStringFromClass([self class]), self, self->header];
137
138   if (b) [d appendFormat:@" bodyClass=%@", NSStringFromClass([b class])];
139
140   if ([b isKindOfClass:[NGMimeFileData class]]) {
141     [d appendFormat:@" body=%@", b];
142   }
143   else if ([b isKindOfClass:[NSString class]] ||
144            [b isKindOfClass:[NSData class]]) {
145     if ([b length] < 512) {
146       [d appendFormat:@" bodyLen=%i body=%@", [b length], b];
147     }
148     else
149       [d appendFormat:@" body[len=%i]", [b length]];
150   }
151   else
152     [d appendFormat:@" body=%@", b];
153   
154
155   [d appendString:@">"];
156
157   return AUTORELEASE(d);
158 }
159
160 @end /* NGMimeBodyPart */