]> err.no Git - sope/blob - sope-mime/NGMime/NGMimeContentDispositionHeaderFieldGenerator.m
Xcode updates and updates for SOPE:X 2.x
[sope] / sope-mime / NGMime / NGMimeContentDispositionHeaderFieldGenerator.m
1 /*
2   Copyright (C) 2000-2004 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
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: NGMimeContentDispositionHeaderFieldGenerator.m 1 2004-08-20 10:08:27Z znek $
22
23 #include "NGMimeHeaderFieldGenerator.h"
24 #include "NGMimeHeaderFields.h"
25 #include "common.h"
26
27 @implementation NGMimeContentDispositionHeaderFieldGenerator
28
29 + (int)version {
30   return 2;
31 }
32
33 - (NSData *)generateDataForHeaderFieldNamed:(NSString *)_headerField
34   value:(id)_value
35 {
36   NGMimeContentDispositionHeaderField *field;
37   NSString      *tmp;
38   NSMutableData *data;
39   
40   field = _value;
41   if (field == nil) {
42     [self logWithFormat:@"WARNING(%s): Content-Disposition field is empty",
43           __PRETTY_FUNCTION__];
44     return [NSData data];
45   }
46   
47   if ([_value isKindOfClass:[NSString class]])
48     return [_value dataUsingEncoding:NSUTF8StringEncoding];
49   
50   // TODO: move the stuff below to some NSString or NSData category?
51   
52   data = [NSMutableData dataWithCapacity:64];
53   tmp  = [field type];
54   [data appendBytes:[tmp cString] length:[tmp length]];
55   tmp = [field filename];
56   if (tmp != nil) {
57     [data appendBytes:"; " length:2];
58     [data appendBytes:"filename=\"" length:10];
59     {
60       unsigned char *ctmp;
61       int  cnt, len;
62       BOOL doEnc;
63       
64       // TODO: unicode?
65       len  = [tmp cStringLength];
66       ctmp = malloc(len + 3);
67       [tmp getCString:ctmp]; ctmp[len] = '\0';
68       cnt  = 0;
69       doEnc = NO;
70       while (cnt < len) {
71         if ((unsigned char)ctmp[cnt] > 127) {
72           doEnc = YES;
73           break;
74         }
75         cnt++;
76       }
77       if (doEnc) {
78         char        iso[]     = "=?iso-8859-15?q?";
79         unsigned    isoLen    = 16;
80         char        isoEnd[]  = "?=";
81         unsigned    isoEndLen = 2;
82         unsigned    desLen;
83         char        *des;
84       
85         if (ctmp) free(ctmp);
86         {
87           NSData *data;
88
89 #if APPLE_Foundation_LIBRARY || NeXT_Foundation_LIBRARY
90           data = [tmp dataUsingEncoding:NSISOLatin1StringEncoding];
91 #else
92           data = [tmp dataUsingEncoding:NSISOLatin9StringEncoding];
93 #endif
94
95           len  = [data length];
96           ctmp =  malloc(len+1);
97           [data getBytes:ctmp];  ctmp[len] = '\0';
98         }
99           
100         desLen = len * 3 + 20;
101         des    = calloc(desLen + 10, sizeof(char));
102       
103         memcpy(des, ctmp, cnt);
104         memcpy(des + cnt, iso, isoLen);
105         desLen =
106              NGEncodeQuotedPrintableMime(ctmp + cnt, len - cnt,
107                                          des + cnt + isoLen,
108                                          desLen - cnt - isoLen);
109         if ((int)desLen != -1) {
110           memcpy(des + cnt + isoLen + desLen, isoEnd, isoEndLen);
111           [data appendBytes:des length:(cnt + isoLen + desLen + isoEndLen)];
112         }
113         else {
114           [self logWithFormat:@"WARNING(%s:%i): An error occour during "
115                 @"quoted-printable decoding",
116                 __PRETTY_FUNCTION__, __LINE__];
117         }
118         if (des) free(des);
119       }
120       else {
121         [data appendBytes:ctmp length:len];
122       }
123     }
124       //      [data appendBytes:[tmp cString] length:[tmp length]];
125       [data appendBytes:"\"" length:1];
126   }
127   return data;
128 }
129   
130 @end /* NGMimeContentDispositionHeaderFieldGenerator */