]> err.no Git - sope/blob - sope-mime/NGMail/NGMailBase64Encoding.m
synced with latest additions and bumped framework versions
[sope] / sope-mime / NGMail / NGMailBase64Encoding.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 "NGMimeMessageGenerator.h"
23 #include "NGMimeMessage.h"
24 #include <NGMime/NGMimeFileData.h>
25 #include "common.h"
26
27 /* Defaults
28   Mail_Use_8bit_Encoding_For_Text[BOOL] --
29     Use 8bit content-transfer-encoding for
30     text messages
31 */
32
33 NSData *
34 _base64Encoding(NGMimeBodyGenerator *self,
35                 NSData *_data_,
36                 id<NGMimePart>_part,
37                 NGMutableHashMap *_addHeaders)
38 {
39   NSString   *transEnc = nil;  
40   const char *bytes    = NULL;
41   unsigned   length    = 0;
42   
43   /* kinda hack, treat NGMimeFileData objects as already encoded */
44   
45   if ([_data_ isKindOfClass:[NGMimeFileData class]])
46     return _data_;
47
48   /* encoding */
49   
50   bytes  = [_data_ bytes];
51   length = [_data_ length];
52
53   while (length > 0) {
54     if ((unsigned char)*bytes > 127) {
55       break;
56     }
57     bytes++;
58     length--;
59   }
60   if (length > 0) { // should be encoded
61     NGMimeType *type;
62
63     type = [_part contentType];
64     
65     if ([[type type] isEqualToString:@"text"]) {
66       NSUserDefaults *ud;
67       BOOL use8bit;
68
69       ud      = [NSUserDefaults standardUserDefaults];
70       use8bit = [ud boolForKey:@"Mail_Use_8bit_Encoding_For_Text"];
71       
72       if (use8bit)
73         transEnc = @"8bit";
74       else {
75         _data_   = [_data_ dataByEncodingQuotedPrintable];
76         transEnc = @"quoted-printable";
77       }
78     }
79     else {
80       NGMimeType *appOctet;
81       
82       _data_   = [_data_ dataByEncodingBase64];
83       transEnc = @"base64";
84
85       appOctet = [NGMimeType mimeType:@"application" subType:@"octet-stream"];
86       if (type == nil)
87         [_addHeaders setObject:appOctet forKey:@"content-type"];
88     }
89   }
90   else /* no encoding */
91     transEnc = @"7bit";
92   
93   [_addHeaders setObject:transEnc forKey:@"content-transfer-encoding"];
94   [_addHeaders setObject:[NSNumber numberWithInt:[_data_ length]]
95                       forKey:@"content-length"];
96   return _data_;
97 }