content.cpp 3.91 KB
Newer Older
Ronan's avatar
Ronan committed
1 2
/*
 * content.cpp
Ghislain MARY's avatar
Ghislain MARY committed
3
 * Copyright (C) 2010-2017 Belledonne Communications SARL
Ronan's avatar
Ronan committed
4
 *
Ghislain MARY's avatar
Ghislain MARY committed
5 6 7 8
 * 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.
Ronan's avatar
Ronan committed
9 10 11 12 13 14 15
 *
 * 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
Ghislain MARY's avatar
Ghislain MARY committed
16 17
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Ronan's avatar
Ronan committed
18 19
 */

Ronan's avatar
Ronan committed
20 21
#include "content-type.h"
#include "object/clonable-object-p.h"
Ronan's avatar
Ronan committed
22 23 24 25 26

#include "content.h"

// =============================================================================

Ronan's avatar
Ronan committed
27 28
using namespace std;

Ronan's avatar
Ronan committed
29 30
LINPHONE_BEGIN_NAMESPACE

Ronan's avatar
Ronan committed
31
class ContentPrivate : public ClonableObjectPrivate {
Ronan's avatar
Ronan committed
32
public:
Ronan's avatar
Ronan committed
33 34
	vector<char> body;
	ContentType contentType;
35
	string contentDisposition;
Ronan's avatar
Ronan committed
36 37
};

38 39
const Content Content::Empty;

Ronan's avatar
Ronan committed
40 41
// -----------------------------------------------------------------------------

Ronan's avatar
Ronan committed
42
Content::Content () : ClonableObject(*new ContentPrivate) {}
Ronan's avatar
Ronan committed
43

44
Content::Content (const Content &src) : ClonableObject(*new ContentPrivate), AppDataContainer(src) {
45
	L_D();
Ronan's avatar
Ronan committed
46 47
	d->body = src.getBody();
	d->contentType = src.getContentType();
48
	d->contentDisposition = src.getContentDisposition();
Ronan's avatar
Ronan committed
49 50
}

Ronan's avatar
Ronan committed
51
Content::Content (Content &&src) : ClonableObject(*new ContentPrivate), AppDataContainer(move(src)) {
52
	L_D();
Ronan's avatar
Ronan committed
53 54
	d->body = move(src.getPrivate()->body);
	d->contentType = move(src.getPrivate()->contentType);
55
	d->contentDisposition = move(src.getPrivate()->contentDisposition);
Ronan's avatar
Ronan committed
56 57
}

Ronan's avatar
Ronan committed
58
Content &Content::operator= (const Content &src) {
59
	L_D();
Ronan's avatar
Ronan committed
60 61 62
	if (this != &src) {
		d->body = src.getBody();
		d->contentType = src.getContentType();
63
		d->contentDisposition = src.getContentDisposition();
64
		AppDataContainer::operator=(src);
Ronan's avatar
Ronan committed
65
	}
Ronan's avatar
Ronan committed
66

Ronan's avatar
Ronan committed
67
	return *this;
Ronan's avatar
Ronan committed
68 69
}

Ronan's avatar
Ronan committed
70
Content &Content::operator= (Content &&src) {
71
	L_D();
72 73
	d->body = move(src.getPrivate()->body);
	d->contentType = move(src.getPrivate()->contentType);
74
	d->contentDisposition = move(src.getPrivate()->contentDisposition);
75
	AppDataContainer::operator=(move(src));
Ronan's avatar
Ronan committed
76
	return *this;
Ronan's avatar
Ronan committed
77 78
}

79
bool Content::operator== (const Content &content) const {
80
	L_D();
81 82 83
	return d->contentType == content.getContentType() &&
		d->body == content.getBody() &&
		d->contentDisposition == content.getContentDisposition();
84 85
}

Ronan's avatar
Ronan committed
86
const ContentType &Content::getContentType () const {
87
	L_D();
Ronan's avatar
Ronan committed
88
	return d->contentType;
Ronan's avatar
Ronan committed
89 90
}

Ronan's avatar
Ronan committed
91
void Content::setContentType (const ContentType &contentType) {
92
	L_D();
Ronan's avatar
Ronan committed
93
	d->contentType = contentType;
Ronan's avatar
Ronan committed
94 95
}

96
void Content::setContentType (const string &contentType) {
97
	L_D();
98 99 100 101
	d->contentType = ContentType(contentType);
}

const string &Content::getContentDisposition () const {
102
	L_D();
103 104 105 106
	return d->contentDisposition;
}

void Content::setContentDisposition (const string &contentDisposition) {
107
	L_D();
108 109 110
	d->contentDisposition = contentDisposition;
}

Ronan's avatar
Ronan committed
111
const vector<char> &Content::getBody () const {
112
	L_D();
Ronan's avatar
Ronan committed
113
	return d->body;
Ronan's avatar
Ronan committed
114 115
}

116
string Content::getBodyAsString () const {
117
	L_D();
118 119 120
	return string(d->body.begin(), d->body.end());
}

Ronan's avatar
Ronan committed
121
void Content::setBody (const vector<char> &body) {
122
	L_D();
Ronan's avatar
Ronan committed
123
	d->body = body;
Ronan's avatar
Ronan committed
124 125
}

126
void Content::setBody (std::vector<char> &&body) {
127
	L_D();
128
	d->body = move(body);
129 130
}

Ronan's avatar
Ronan committed
131
void Content::setBody (const string &body) {
132
	L_D();
Ronan's avatar
Ronan committed
133
	d->body = vector<char>(body.cbegin(), body.cend());
Ronan's avatar
Ronan committed
134 135
}

Ronan's avatar
Ronan committed
136
void Content::setBody (const void *buffer, size_t size) {
137
	L_D();
Ronan's avatar
Ronan committed
138 139
	const char *start = static_cast<const char *>(buffer);
	d->body = vector<char>(start, start + size);
Ronan's avatar
Ronan committed
140 141
}

Ronan's avatar
Ronan committed
142
size_t Content::getSize () const {
143
	L_D();
Ronan's avatar
Ronan committed
144
	return d->body.size();
Ronan's avatar
Ronan committed
145
}
Ronan's avatar
Ronan committed
146

Ronan's avatar
Ronan committed
147 148 149 150
bool Content::isEmpty () const {
	return getSize() == 0;
}

151 152 153 154 155
bool Content::isValid() const {
	L_D();
	return d->contentType.isValid() || d->body.empty();
}

Ronan's avatar
Ronan committed
156
LINPHONE_END_NAMESPACE