cpim-chat-message-modifier.cpp 3.35 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * cpim-chat-message-modifier.cpp
 * Copyright (C) 2017  Belledonne Communications SARL
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

19 20
#include "cpim-chat-message-modifier.h"

Ronan's avatar
Ronan committed
21 22 23
#include "chat/cpim/cpim.h"
#include "content/content-type.h"
#include "content/content.h"
24 25
#include "address/address.h"
#include "chat/chat-message-p.h"
26

Ronan's avatar
Ronan committed
27
// =============================================================================
28

Ronan's avatar
Ronan committed
29 30 31 32
using namespace std;

LINPHONE_BEGIN_NAMESPACE

33
int CpimChatMessageModifier::encode (ChatMessagePrivate *messagePrivate) {
Ronan's avatar
Ronan committed
34 35
	Cpim::Message message;
	Cpim::GenericHeader cpimContentTypeHeader;
36
	cpimContentTypeHeader.setName("Content-Type");
Ronan's avatar
Ronan committed
37 38
	cpimContentTypeHeader.setValue("Message/CPIM");
	message.addCpimHeader(cpimContentTypeHeader);
39

40 41
	Content content;
	if (!messagePrivate->internalContent.isEmpty()) {
Ronan's avatar
Ronan committed
42
		// Another ChatMessageModifier was called before this one, we apply our changes on the private content
43
		content = messagePrivate->internalContent;
Ronan's avatar
Ronan committed
44 45 46 47
	} else {
		// We're the first ChatMessageModifier to be called, we'll create the private content from the public one
		// We take the first one because if there is more of them, the multipart modifier should have been called first
		// So we should not be in this block
48
		content = messagePrivate->contents.front();
Ronan's avatar
Ronan committed
49
	}
50

51 52
	string contentType = content.getContentType().asString();
	const vector<char> body = content.getBody();
Ronan's avatar
Ronan committed
53 54 55
	string contentBody(body.begin(), body.end());

	Cpim::GenericHeader contentTypeHeader;
56
	contentTypeHeader.setName("Content-Type");
Ronan's avatar
Ronan committed
57 58 59 60
	contentTypeHeader.setValue(contentType);
	message.addContentHeader(contentTypeHeader);

	message.setContent(contentBody);
61

Ronan's avatar
Ronan committed
62 63 64
	if (!message.isValid()) {
		//TODO
	} else {
65
		Content newContent;
Ronan's avatar
Ronan committed
66
		ContentType newContentType("Message/CPIM");
67 68
		newContent.setContentType(newContentType);
		newContent.setBody(message.asString());
69
		messagePrivate->internalContent = newContent;
Ronan's avatar
Ronan committed
70
	}
71
	return 0;
Ronan's avatar
Ronan committed
72
}
73

74
int CpimChatMessageModifier::decode (ChatMessagePrivate *messagePrivate) {
75 76
	Content content;
	if (!messagePrivate->internalContent.isEmpty()) {
77
		content = messagePrivate->internalContent;
Ronan's avatar
Ronan committed
78
	} else {
79
		content = messagePrivate->contents.front();
Ronan's avatar
Ronan committed
80
	}
81

82
	ContentType contentType = content.getContentType();
Ronan's avatar
Ronan committed
83
	if (contentType.asString() == "Message/CPIM") {
84
		const vector<char> body = content.getBody();
Ronan's avatar
Ronan committed
85 86 87
		string contentBody(body.begin(), body.end());
		shared_ptr<const Cpim::Message> message = Cpim::Message::createFromString(contentBody);
		if (message && message->isValid()) {
88
			Content newContent;
Ronan's avatar
Ronan committed
89
			ContentType newContentType(message->getContentHeaders()->front()->getValue());
90 91
			newContent.setContentType(newContentType);
			newContent.setBody(message->getContent());
Sylvain Berfini's avatar
Sylvain Berfini committed
92
			messagePrivate->internalContent = newContent;
Ronan's avatar
Ronan committed
93 94 95 96 97 98
		} else {
			//TODO
		}
	} else {
		//TODO
	}
99
	return 0;
Ronan's avatar
Ronan committed
100
}
101

Ronan's avatar
Ronan committed
102
LINPHONE_END_NAMESPACE