~ubuntu-branches/ubuntu/wily/cxxtools/wily-proposed

« back to all changes in this revision

Viewing changes to include/cxxtools/mime.h

  • Committer: Bazaar Package Importer
  • Author(s): Kari Pahula
  • Date: 2008-06-16 12:24:28 UTC
  • mfrom: (3.1.3 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080616122428-7bllgyt1358u779r
Tags: 1.4.8-2
Made libcxxtools-dev depend on libcxxtools6, not libcxxtools5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2007 Tommi Maekitalo
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU General Public License as
 
6
 * published by the Free Software Foundation; either version 2 of the
 
7
 * License, or (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful, but
 
10
 * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
 
11
 * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
 
12
 * NON-INFRINGEMENT.  See the GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 
17
 *
 
18
 */
 
19
 
 
20
#include <iosfwd>
 
21
#include <string>
 
22
#include <vector>
 
23
#include <map>
 
24
 
 
25
namespace cxxtools
 
26
{
 
27
  class Mimepart
 
28
  {
 
29
      friend std::ostream& operator<< (std::ostream& out, const Mimepart& mimePart);
 
30
 
 
31
    public:
 
32
      enum ContentTransferEncoding {
 
33
        quotedPrintable,
 
34
        base64
 
35
      };
 
36
 
 
37
    private:
 
38
      typedef std::map<std::string, std::string> HeadersType;
 
39
      HeadersType headers;
 
40
      ContentTransferEncoding contentTransferEncoding;
 
41
      std::string body;
 
42
 
 
43
    public:
 
44
      explicit Mimepart(const std::string& contentType_ = "text/plain, charset=UTF-8",
 
45
                        ContentTransferEncoding contentTransferEncoding_ = quotedPrintable);
 
46
 
 
47
      void setData(const std::string& data)   { body = data; }
 
48
      void addData(const std::string& data)   { body += data; }
 
49
      void addData(std::istream& in);
 
50
      const std::string& getBody() const       { return body; }
 
51
      void setHeader(const std::string& key, const std::string& value)
 
52
        { headers[key] = value; }
 
53
  };
 
54
 
 
55
  class Mime
 
56
  {
 
57
      friend std::ostream& operator<< (std::ostream& out, const Mime& mime);
 
58
 
 
59
    public:
 
60
      typedef Mimepart::ContentTransferEncoding ContentTransferEncoding;
 
61
 
 
62
    private:
 
63
      typedef std::map<std::string, std::string> HeadersType;
 
64
      HeadersType headers;
 
65
 
 
66
      typedef std::vector<Mimepart> PartsType;
 
67
      PartsType parts;
 
68
 
 
69
    public:
 
70
      /// Adds a header-line to the mime-object.
 
71
      void setHeader(const std::string& key, const std::string& value)
 
72
         { headers[key] = value; }
 
73
 
 
74
      /// Adds a part to the mime-object.
 
75
      Mimepart& addPart(const Mimepart& part)
 
76
        { parts.push_back(part); return parts.back(); }
 
77
 
 
78
      /// Adds a part to the mime-object. The data is passed as a std::string.
 
79
      Mimepart& addPart(const std::string& data, const std::string& contentType = "text/plain",
 
80
        ContentTransferEncoding contentTransferEncoding = Mimepart::quotedPrintable);
 
81
 
 
82
      /// Adds a part to the mime-object. The data is read from a input stream.
 
83
      Mimepart& addPart(std::istream& in, const std::string& contentType = "text/plain",
 
84
        ContentTransferEncoding contentTransferEncoding = Mimepart::quotedPrintable);
 
85
 
 
86
      /// Adds a text file. The data is passed as a std::string.
 
87
      Mimepart& addTextFile(const std::string& contentType, const std::string& filename, const std::string& data)
 
88
      {
 
89
        Mimepart& part = addPart(data, contentType, Mimepart::quotedPrintable);
 
90
        part.setHeader("Content-Disposition", "attachment; filename=" + filename);
 
91
        return part;
 
92
      }
 
93
 
 
94
      /// Adds a text file. The data is read from a istream
 
95
      Mimepart& addTextFile(const std::string& contentType, const std::string& filename, std::istream& in)
 
96
      {
 
97
        Mimepart& part = addPart(in, contentType, Mimepart::quotedPrintable);
 
98
        part.setHeader("Content-Disposition", "attachment; filename=" + filename);
 
99
        return part;
 
100
      }
 
101
 
 
102
      /// Adds a text file. The data is read from a file.
 
103
      Mimepart& addTextFile(const std::string& contentType, const std::string& filename);
 
104
 
 
105
      /// Adds a binary file. The data is passed as a std::string.
 
106
      Mimepart& addBinaryFile(const std::string& contentType, const std::string& filename, const std::string& data)
 
107
      {
 
108
        Mimepart& part = addPart(data, contentType, Mimepart::base64);
 
109
        part.setHeader("Content-Disposition", "attachment; filename=" + filename);
 
110
        return part;
 
111
      }
 
112
 
 
113
      /// Adds a binary file. The data is read from a istream
 
114
      Mimepart& addBinaryFile(const std::string& contentType, const std::string& filename, std::istream& in)
 
115
      {
 
116
        Mimepart& part = addPart(in, contentType, Mimepart::base64);
 
117
        part.setHeader("Content-Disposition", "attachment; filename=" + filename);
 
118
        return part;
 
119
      }
 
120
 
 
121
      /// Adds a binary file. The data is read from a file.
 
122
      Mimepart& addBinaryFile(const std::string& contentType, const std::string& filename);
 
123
 
 
124
  };
 
125
 
 
126
  std::ostream& operator<< (std::ostream& out, const Mimepart& mimePart);
 
127
  std::ostream& operator<< (std::ostream& out, const Mime& mime);
 
128
}