]> err.no Git - sope/blob - sope-mime/NGMime/NGMimeHeaderFields.m
minor fixes
[sope] / sope-mime / NGMime / NGMimeHeaderFields.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 "NGMimeHeaderFields.h"
24 #include "NGMimeUtilities.h"
25 #include "common.h"
26
27 NGMime_DECLARE NSString *NGMimeContentDispositionInlineType     = @"inline";
28 NGMime_DECLARE NSString *NGMimeContentDispositionAttachmentType = @"attachment";
29 NGMime_DECLARE NSString *NGMimeContentDispositionFormType       = @"form-data";
30
31 @implementation NGMimeContentDispositionHeaderField
32
33 static int MimeLogEnabled = -1;
34
35 + (int)version {
36   return 2;
37 }
38
39 + (void)initialize {
40   NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
41   
42   MimeLogEnabled = [ud boolForKey:@"MimeLogEnabled"] ? 1 : 0;
43 }
44
45 - (id)initWithString:(NSString *)_value {
46   unsigned len = [_value length];
47   unichar  buf[len+1];
48   
49   if (len == 0) {
50     [self logWithFormat:
51             @"WARNING(%s): no value for disposition header value!",
52             __PRETTY_FUNCTION__];
53     self = [self autorelease];
54     return nil;
55   }
56   
57   [_value getCharacters:buf];
58   buf[len] = '\0';
59   
60   if ((self = [super init])) {
61     unsigned cnt, start;
62
63     cnt = 0;
64     // skip leading spaces
65     
66     while (isRfc822_LWSP(buf[cnt])) cnt++;
67     
68     if (buf[cnt] == '\0') {
69       if (MimeLogEnabled) 
70         [self logWithFormat:@"WARNING(%s): no value for disposition header"
71               @" value !", __PRETTY_FUNCTION__];
72       self = [self autorelease];
73       return nil;
74     }
75     start = cnt;
76     
77     while ((buf[cnt] != ';') && (buf[cnt] != '\0') && !isRfc822_LWSP(buf[cnt]))
78       cnt++;
79
80     if (cnt <= start) {
81       if (MimeLogEnabled) 
82         [self logWithFormat:@"WARNING(%s): found no type in disposition "
83               @"header value (%@) !", __PRETTY_FUNCTION__, _value];
84       self = [self autorelease];
85       return nil;
86     }
87     
88     self->type = [[[NSString alloc]
89                              initWithCharacters:buf+start length:(cnt - start)]
90                              autorelease];
91     start      = 0;
92     self->type = [self->type lowercaseString];
93     self->type = [self->type retain];
94     
95     if (self->type == nil) {
96       if (MimeLogEnabled)
97         [self logWithFormat:@"WARNING(%s): found no type in disposition header "
98               @"value (%@) !", __PRETTY_FUNCTION__, _value];
99       self = [self autorelease];
100       return nil;
101     }
102     self->parameters = [parseParameters(self, _value, buf+cnt) retain];
103   }
104   return self;
105 }
106
107 - (id)init {
108   return [self initWithString:nil];
109 }
110
111 - (void)dealloc {
112   [self->type       release];
113   [self->parameters release];
114   [super dealloc];
115 }
116
117 /* accessors */
118
119 - (NSString *)type {
120   return self->type;
121 }
122
123 /* parameters */
124
125 - (NSString *)name {
126   return [self->parameters objectForKey:@"name"];
127 }
128 - (NSString *)filename {
129   NSString *fn;
130
131   fn = [self->parameters objectForKey:@"filename"];
132
133   if (![fn isNotNull])
134     fn = nil;
135   
136   if (![fn length]) {
137     fn = [self name];
138   }
139   return fn;
140 }
141
142 - (NSString *)valueOfParameterWithName:(NSString *)_name {
143   return [self->parameters objectForKey:_name];
144 }
145
146 - (BOOL)valueNeedsQuotes:(NSString *)_parameterValue {
147   int     len = [_parameterValue length];
148   unichar cstr[len + 1];
149   int     cnt;
150
151   [_parameterValue getCharacters:cstr];
152
153   for (cnt = 0; cnt < len; cnt++) {
154
155     if (isMime_SpecialByte(cstr[cnt]))
156       return YES;
157
158     if (cstr[cnt] == 32)
159       return YES;
160   }
161   return NO;
162 }
163
164 - (NSString *)parametersAsString {
165   NSEnumerator *names;
166
167   if ((names = [self->parameters keyEnumerator])) {
168     NSMutableString *result = [NSMutableString stringWithCapacity:64];
169     NSString *name;
170
171     while ((name = [names nextObject])) {
172       id value = [[parameters objectForKey:name] stringValue];
173       [result appendString:@"; "];
174       [result appendString:name];
175       [result appendString:@"="];
176       if ([self valueNeedsQuotes:value]) {
177         [result appendString:@"\""];
178         [result appendString:value];
179         [result appendString:@"\""];
180       }
181       else
182         [result appendString:value];
183     }
184     return result;
185   }
186   else
187     return nil;
188 }
189
190 - (NSString *)stringValue {
191   NSMutableString *str;
192
193   str = [NSMutableString stringWithCapacity:20];
194   [str appendString:type];
195   {
196     NSString *paras = [self parametersAsString];
197     if (paras) [str appendString:paras];
198   }
199   return str;
200 }
201
202 /* description */
203
204 - (NSString *)description {
205   NSMutableString *d;
206
207   d = [[NSMutableString alloc] init];
208
209   [d appendFormat:@"<%@[0x%08X]: type=%@",
210        NSStringFromClass([self class]), self, self->type];
211
212   if (self->parameters)
213     [d appendFormat:@" parameters=%@", self->parameters];
214
215   [d appendString:@">"];
216   return [d autorelease];
217 }
218
219 @end /* NGMimeContentDispositionHeaderField */