typo found by Andrey Cherepanov
[kdepim.git] / mimelib / attach.cpp
blob27b35dfdf71687c92a6fe238fd9b307093424ddd
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 //
7 // Copyright (c) 1996, 1997 Douglas W. Sauder
8 // All rights reserved.
9 //
10 // IN NO EVENT SHALL DOUGLAS W. SAUDER BE LIABLE TO ANY PARTY FOR DIRECT,
11 // INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
12 // THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF DOUGLAS W. SAUDER
13 // HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 // DOUGLAS W. SAUDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT
16 // NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
17 // PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
18 // BASIS, AND DOUGLAS W. SAUDER HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
19 // SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
21 //=============================================================================
23 #include <stdio.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <mimelib/string.h>
27 #include <mimelib/utility.h>
28 #include "attach.h"
31 MessageWithAttachments::MessageWithAttachments()
36 MessageWithAttachments::~MessageWithAttachments()
41 void MessageWithAttachments::SetText(const DwString& aStr)
43 // Create a body part and set the necessary fields
45 MultipartBodyPart part;
46 part.SetType(DwMime::kTypeText);
47 part.SetSubtype(DwMime::kSubtypePlain);
48 part.SetCte(DwMime::kCte7bit);
50 // Set the string as the body of the body part
52 part.SetBody(aStr);
54 // Set this body part as the first one
56 SetBodyPart(0, part);
60 int MessageWithAttachments::NumberOfAttachments() const
62 int n = NumberOfParts() - 1;
63 return (n >= 0) ? n : 0;
67 void MessageWithAttachments::Attach7bitFile(const char* aFilename,
68 int aType, int aSubtype)
70 // Get the file contents
72 DwString str;
73 PutFileInString(aFilename, str);
75 // Create a body part and set the necessary fields
77 MultipartBodyPart part;
78 part.SetType(aType);
79 part.SetSubtype(aSubtype);
80 part.SetCte(DwMime::kCte7bit);
82 // Set content-disposition to attachment, with filename parameter
83 // (see RFC-1806 for information on this *experimental* header field)
85 DwString contDisp = "attachment; filename=";
86 contDisp += '\"';
87 contDisp += aFilename;
88 contDisp += '\"';
89 part.SetContentDisposition(contDisp);
91 // Set the file contents as the body of the body part
93 part.SetBody(str);
95 // Make sure this is not the first part, since that is reserved for
96 // the text
98 if (NumberOfParts() == 0) {
99 SetBodyPart(1, part);
101 else {
102 AddBodyPart(part);
107 void MessageWithAttachments::Attach8bitFile(const char* aFilename,
108 int aType, int aSubtype)
110 // Get the file contents
112 DwString str;
113 PutFileInString(aFilename, str);
115 // Encode using quoted-printable encoding
117 DwString encStr;
118 DwEncodeQuotedPrintable(str, encStr);
120 // Create a body part and set the necessary fields
122 MultipartBodyPart part;
123 part.SetType(aType);
124 part.SetSubtype(aSubtype);
125 part.SetCte(DwMime::kCteQuotedPrintable);
127 // Set content-disposition to attachment, with filename parameter
128 // (see RFC-1806 for information on this *experimental* header field)
130 DwString contDisp = "attachment; filename=";
131 contDisp += '\"';
132 contDisp += aFilename;
133 contDisp += '\"';
134 part.SetContentDisposition(contDisp);
136 // Set the encoded file contents as the body of the body part
138 part.SetBody(encStr);
140 // Make sure this is not the first part, since that is reserved for
141 // the text
143 if (NumberOfParts() == 0) {
144 SetBodyPart(1, part);
146 else {
147 AddBodyPart(part);
152 void MessageWithAttachments::AttachBinaryFile(const char* aFilename,
153 int aType, int aSubtype)
155 // Get the file contents
157 DwString str;
158 PutFileInString(aFilename, str);
160 // Encode using base64 encoding
162 DwString encStr;
163 DwEncodeBase64(str, encStr);
165 // Create a body part and set the necessary fields
167 MultipartBodyPart part;
168 part.SetType(aType);
169 part.SetSubtype(aSubtype);
170 part.SetCte(DwMime::kCteBase64);
172 // Set content-disposition to attachment, with filename parameter
173 // (see RFC-1806 for information on this *experimental* header field)
175 DwString contDisp = "attachment; filename=";
176 contDisp += '\"';
177 contDisp += aFilename;
178 contDisp += '\"';
179 part.SetContentDisposition(contDisp);
181 // Set the encoded file contents as the body of the body part
183 part.SetBody(encStr);
185 // Make sure this is not the first part, since that is reserved for
186 // the text
188 if (NumberOfParts() == 0) {
189 SetBodyPart(1, part);
191 else {
192 AddBodyPart(part);
197 int MessageWithAttachments::PutFileInString(const char* aFilename,
198 DwString& str)
200 // Get the file size
201 struct stat statBuf;
202 int k = stat(aFilename, &statBuf);
203 if (k < 0) {
204 str = "";
205 return -1;
207 int fileSize = (int) statBuf.st_size;
209 // Allocate a buffer
211 int bufSize = fileSize + 8; // a little elbow room added
212 char* buf = new char[bufSize];
214 // Read the file into the buffer
216 FILE* fp = fopen(aFilename, "rb");
217 if (fp == 0) {
218 delete[] buf;
219 str = "";
220 return -1;
222 int len = 0;
223 while (1) {
224 int ch = getc(fp);
225 if (feof(fp) || len == fileSize) {
226 break;
228 buf[len++] = ch;
230 buf[len] = 0;
231 fclose(fp);
233 // Place the buffer in the string
235 str.TakeBuffer(buf, bufSize, 0, len);
236 return 0;