CVS_SILENT Removing not needed accelerators (<property name="accel">) from ui files...
[kdepim.git] / mimelib / attach.cpp
blob9c4100d10888ab654887956eff66feb2e6b99a13
1 //=============================================================================
2 // File: attach.cpp
3 // Contents: Definitions for MessageWithAttachments
4 // Maintainer: Doug Sauder <dwsauder@fwb.gulf.net>
5 // WWW: http://www.fwb.gulf.net/~dwsauder/mimepp.html
6 // $Revision$
7 // $Date$
8 //
9 // Copyright (c) 1996, 1997 Douglas W. Sauder
10 // All rights reserved.
11 //
12 // IN NO EVENT SHALL DOUGLAS W. SAUDER BE LIABLE TO ANY PARTY FOR DIRECT,
13 // INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
14 // THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF DOUGLAS W. SAUDER
15 // HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17 // DOUGLAS W. SAUDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT
18 // NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
19 // PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
20 // BASIS, AND DOUGLAS W. SAUDER HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
21 // SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23 //=============================================================================
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <mimelib/string.h>
29 #include <mimelib/utility.h>
30 #include "attach.h"
33 MessageWithAttachments::MessageWithAttachments()
38 MessageWithAttachments::~MessageWithAttachments()
43 void MessageWithAttachments::SetText(const DwString& aStr)
45 // Create a body part and set the necessary fields
47 MultipartBodyPart part;
48 part.SetType(DwMime::kTypeText);
49 part.SetSubtype(DwMime::kSubtypePlain);
50 part.SetCte(DwMime::kCte7bit);
52 // Set the string as the body of the body part
54 part.SetBody(aStr);
56 // Set this body part as the first one
58 SetBodyPart(0, part);
62 int MessageWithAttachments::NumberOfAttachments() const
64 int n = NumberOfParts() - 1;
65 return (n >= 0) ? n : 0;
69 void MessageWithAttachments::Attach7bitFile(const char* aFilename,
70 int aType, int aSubtype)
72 // Get the file contents
74 DwString str;
75 PutFileInString(aFilename, str);
77 // Create a body part and set the necessary fields
79 MultipartBodyPart part;
80 part.SetType(aType);
81 part.SetSubtype(aSubtype);
82 part.SetCte(DwMime::kCte7bit);
84 // Set content-disposition to attachment, with filename parameter
85 // (see RFC-1806 for information on this *experimental* header field)
87 DwString contDisp = "attachment; filename=";
88 contDisp += '\"';
89 contDisp += aFilename;
90 contDisp += '\"';
91 part.SetContentDisposition(contDisp);
93 // Set the file contents as the body of the body part
95 part.SetBody(str);
97 // Make sure this is not the first part, since that is reserved for
98 // the text
100 if (NumberOfParts() == 0) {
101 SetBodyPart(1, part);
103 else {
104 AddBodyPart(part);
109 void MessageWithAttachments::Attach8bitFile(const char* aFilename,
110 int aType, int aSubtype)
112 // Get the file contents
114 DwString str;
115 PutFileInString(aFilename, str);
117 // Encode using quoted-printable encoding
119 DwString encStr;
120 DwEncodeQuotedPrintable(str, encStr);
122 // Create a body part and set the necessary fields
124 MultipartBodyPart part;
125 part.SetType(aType);
126 part.SetSubtype(aSubtype);
127 part.SetCte(DwMime::kCteQuotedPrintable);
129 // Set content-disposition to attachment, with filename parameter
130 // (see RFC-1806 for information on this *experimental* header field)
132 DwString contDisp = "attachment; filename=";
133 contDisp += '\"';
134 contDisp += aFilename;
135 contDisp += '\"';
136 part.SetContentDisposition(contDisp);
138 // Set the encoded file contents as the body of the body part
140 part.SetBody(encStr);
142 // Make sure this is not the first part, since that is reserved for
143 // the text
145 if (NumberOfParts() == 0) {
146 SetBodyPart(1, part);
148 else {
149 AddBodyPart(part);
154 void MessageWithAttachments::AttachBinaryFile(const char* aFilename,
155 int aType, int aSubtype)
157 // Get the file contents
159 DwString str;
160 PutFileInString(aFilename, str);
162 // Encode using base64 encoding
164 DwString encStr;
165 DwEncodeBase64(str, encStr);
167 // Create a body part and set the necessary fields
169 MultipartBodyPart part;
170 part.SetType(aType);
171 part.SetSubtype(aSubtype);
172 part.SetCte(DwMime::kCteBase64);
174 // Set content-disposition to attachment, with filename parameter
175 // (see RFC-1806 for information on this *experimental* header field)
177 DwString contDisp = "attachment; filename=";
178 contDisp += '\"';
179 contDisp += aFilename;
180 contDisp += '\"';
181 part.SetContentDisposition(contDisp);
183 // Set the encoded file contents as the body of the body part
185 part.SetBody(encStr);
187 // Make sure this is not the first part, since that is reserved for
188 // the text
190 if (NumberOfParts() == 0) {
191 SetBodyPart(1, part);
193 else {
194 AddBodyPart(part);
199 int MessageWithAttachments::PutFileInString(const char* aFilename,
200 DwString& str)
202 // Get the file size
203 struct stat statBuf;
204 int k = stat(aFilename, &statBuf);
205 if (k < 0) {
206 str = "";
207 return -1;
209 int fileSize = (int) statBuf.st_size;
211 // Allocate a buffer
213 int bufSize = fileSize + 8; // a little elbow room added
214 char* buf = new char[bufSize];
216 // Read the file into the buffer
218 FILE* fp = fopen(aFilename, "rb");
219 if (fp == 0) {
220 delete buf;
221 str = "";
222 return -1;
224 int len = 0;
225 while (1) {
226 int ch = getc(fp);
227 if (feof(fp) || len == fileSize) {
228 break;
230 buf[len++] = ch;
232 buf[len] = 0;
233 fclose(fp);
235 // Place the buffer in the string
237 str.TakeBuffer(buf, bufSize, 0, len);
238 return 0;