mime.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007 Tommi Maekitalo
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #include <iosfwd>
30 #include <string>
31 #include <vector>
32 #include <map>
33 
34 namespace cxxtools
35 {
36  class Mimepart
37  {
38  friend std::ostream& operator<< (std::ostream& out, const Mimepart& mimePart);
39 
40  public:
44  };
45 
46  private:
47  typedef std::map<std::string, std::string> HeadersType;
48  HeadersType headers;
49  ContentTransferEncoding contentTransferEncoding;
50  std::string body;
51 
52  public:
53  explicit Mimepart(const std::string& contentType_ = "text/plain, charset=UTF-8",
54  ContentTransferEncoding contentTransferEncoding_ = quotedPrintable);
55 
56  void setData(const std::string& data) { body = data; }
57  void addData(const std::string& data) { body += data; }
58  void addData(std::istream& in);
59  const std::string& getBody() const { return body; }
60  void setHeader(const std::string& key, const std::string& value)
61  { headers[key] = value; }
62  };
63 
64  class Mime
65  {
66  friend std::ostream& operator<< (std::ostream& out, const Mime& mime);
67 
68  public:
70 
71  private:
72  typedef std::map<std::string, std::string> HeadersType;
73  HeadersType headers;
74 
75  typedef std::vector<Mimepart> PartsType;
76  PartsType parts;
77 
78  public:
80  void setHeader(const std::string& key, const std::string& value)
81  { headers[key] = value; }
82 
84  Mimepart& addPart(const Mimepart& part)
85  { parts.push_back(part); return parts.back(); }
86 
88  Mimepart& addPart(const std::string& data, const std::string& contentType = "text/plain",
89  ContentTransferEncoding contentTransferEncoding = Mimepart::quotedPrintable);
90 
92  Mimepart& addPart(std::istream& in, const std::string& contentType = "text/plain",
93  ContentTransferEncoding contentTransferEncoding = Mimepart::quotedPrintable);
94 
96  Mimepart& addTextFile(const std::string& contentType, const std::string& filename, const std::string& data)
97  {
98  Mimepart& part = addPart(data, contentType, Mimepart::quotedPrintable);
99  part.setHeader("Content-Disposition", "attachment; filename=" + filename);
100  return part;
101  }
102 
104  Mimepart& addTextFile(const std::string& contentType, const std::string& filename, std::istream& in)
105  {
106  Mimepart& part = addPart(in, contentType, Mimepart::quotedPrintable);
107  part.setHeader("Content-Disposition", "attachment; filename=" + filename);
108  return part;
109  }
110 
112  Mimepart& addTextFile(const std::string& contentType, const std::string& filename);
113 
115  Mimepart& addBinaryFile(const std::string& contentType, const std::string& filename, const std::string& data)
116  {
117  Mimepart& part = addPart(data, contentType, Mimepart::base64);
118  part.setHeader("Content-Disposition", "attachment; filename=" + filename);
119  return part;
120  }
121 
123  Mimepart& addBinaryFile(const std::string& contentType, const std::string& filename, std::istream& in)
124  {
125  Mimepart& part = addPart(in, contentType, Mimepart::base64);
126  part.setHeader("Content-Disposition", "attachment; filename=" + filename);
127  return part;
128  }
129 
131  Mimepart& addBinaryFile(const std::string& contentType, const std::string& filename);
132 
133  };
134 
135  std::ostream& operator<< (std::ostream& out, const Mimepart& mimePart);
136  std::ostream& operator<< (std::ostream& out, const Mime& mime);
137 }