aboutsummaryrefslogtreecommitdiffstats
path: root/src/htmlTextPart.hpp
blob: eeb7b6182762b95dc1def136a27c7b4b29c8e93a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//
// VMime library (http://vmime.sourceforge.net)
// Copyright (C) 2002-2004 Vincent Richard <[email protected]>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//

#ifndef VMIME_HTMLTEXTPART_HPP_INCLUDED
#define VMIME_HTMLTEXTPART_HPP_INCLUDED


#include "textPart.hpp"
#include "messageId.hpp"
#include "encoding.hpp"

#include "contentHandler.hpp"


namespace vmime
{


class htmlTextPart : public textPart
{
protected:

	~htmlTextPart();

public:

	const mediaType type() const;

	const vmime::charset& charset() const { return (m_charset); }
	vmime::charset& charset() { return (m_charset); }

	const contentHandler& plainText() const { return (m_plainText); }
	contentHandler& plainText() { return (m_plainText); }

	const contentHandler& text() const { return (m_text); }
	contentHandler& text() { return (m_text); }

	// Embedded object (eg. image for <IMG> tag)
	class embeddedObject
	{
	public:

		embeddedObject(const contentHandler& data, const vmime::encoding& enc,
		               const string& id, const mediaType& type)
			: m_data(data), m_encoding(enc), m_id(id), m_type(type)
		{
		}

	public:

		const contentHandler& data() const { return (m_data); }
		const vmime::encoding& encoding() const { return (m_encoding); }
		const string& id() const { return (m_id); }
		const mediaType& type() const { return (m_type); }

	private:

		contentHandler m_data;
		vmime::encoding m_encoding;
		string m_id;
		mediaType m_type;
	};

	// Embedded objects container
	class embeddedObjectsContainer
	{
		friend class htmlTextPart;

	protected:

		~embeddedObjectsContainer();

	public:

		// Test the existence/get an embedded object given its identifier.
		const bool has(const string& id) const;
		const embeddedObject& find(const string& id) const;

		// Embed an object and returns a string which identifies it.
		const string add(const string& data, const mediaType& type);
		const string add(const contentHandler& data, const mediaType& type);
		const string add(const contentHandler& data, const encoding& enc, const mediaType& type);

		// Embedded objects enumerator
		class const_iterator
		{
		public:

			typedef std::vector <embeddedObject*>::const_iterator::difference_type difference_type;

			const_iterator(std::vector <embeddedObject*>::const_iterator it) : m_iterator(it) { }
			const_iterator(const const_iterator& it) : m_iterator(it.m_iterator) { }

			const_iterator& operator=(const const_iterator& it) { m_iterator = it.m_iterator; return (*this); }

			const embeddedObject& operator*() const { return (**m_iterator); }
			const embeddedObject* operator->() const { return (*m_iterator); }

			const_iterator& operator++() { ++m_iterator; return (*this); }
			const_iterator operator++(int) { const_iterator i(*this); ++m_iterator; return (i); }

			const_iterator& operator--() { --m_iterator; return (*this); }
			const_iterator operator--(int) { const_iterator i(*this); --m_iterator; return (i); }

			const_iterator& operator+=(difference_type n) { m_iterator += n; return (*this); }
			const_iterator& operator-=(difference_type n) { m_iterator -= n; return (*this); }

			const_iterator operator-(difference_type x) const { return const_iterator(m_iterator - x); }

			const embeddedObject& operator[](difference_type n) const { return *(m_iterator[n]); }

			const bool operator==(const const_iterator& it) const { return (it.m_iterator == m_iterator); }
			const bool operator!=(const const_iterator& it) const { return (!(*this == it)); }

		protected:

			std::vector <embeddedObject*>::const_iterator m_iterator;
		};

	public:

		const_iterator begin() const { return (const_iterator(m_list.begin())); }
		const_iterator end() const { return (const_iterator(m_list.end())); }

		// Object count
		const std::vector <embeddedObject*>::size_type count() const { return (m_list.size()); }
		const std::vector <embeddedObject*>::size_type size() const { return (m_list.size()); }
		const bool empty() const { return (m_list.empty()); }

		embeddedObject& front() { return (*m_list.front()); }
		const embeddedObject& front() const { return (*m_list.front()); }
		embeddedObject& back() { return (*m_list.back()); }
		const embeddedObject& back() const { return (*m_list.back()); }

	protected:

		std::vector <embeddedObject*> m_list;

	} embeddedObjects;

	typedef embeddedObjectsContainer::const_iterator const_iterator;

protected:

	contentHandler m_plainText;
	contentHandler m_text;
	vmime::charset m_charset;

	void findEmbeddedParts(const bodyPart& part, std::vector <const bodyPart*>& cidParts, std::vector <const bodyPart*>& locParts);
	void addEmbeddedObject(const bodyPart& part, const string& id);

	bool findPlainTextPart(const bodyPart& part, const bodyPart& parent, const bodyPart& textPart);

	const int getPartCount() const;

	void generateIn(bodyPart& message, bodyPart& parent) const;
	virtual void parse(const bodyPart& message, const bodyPart& parent, const bodyPart& textPart);
};


} // vmime


#endif // VMIME_HTMLTEXTPART_HPP_INCLUDED