summaryrefslogtreecommitdiffstats
blob: 2f2623fbdcf7928a0f00a419831e5ae5a3e99472 (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
/*
 * Copyright (c) 2011 Kevin Smith
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#pragma once

#include <Swiften/Serializer/GenericPayloadSerializer.h>
#include <Swiften/Elements/MUCItem.h>
#include <boost/smart_ptr/make_shared.hpp>
#include <Swiften/Serializer/XML/XMLElement.h>
#include <Swiften/Serializer/XML/XMLTextNode.h>

namespace Swift {
	class MUCItemSerializer {
		public:
			static std::string affiliationToString(MUCOccupant::Affiliation affiliation) {
				std::string result;
				switch (affiliation) {
				case MUCOccupant::Owner: result = "owner"; break;
				case MUCOccupant::Admin: result = "admin"; break;
				case MUCOccupant::Member: result = "member"; break;
				case MUCOccupant::Outcast: result = "outcast"; break;
				case MUCOccupant::NoAffiliation: result = "none"; break;
				}
				return result;
			}

			static std::string roleToString(MUCOccupant::Role role) {
				std::string result;
				switch (role) {
				case MUCOccupant::Moderator: result = "moderator"; break;
				case MUCOccupant::NoRole: result = "none"; break;
				case MUCOccupant::Participant: result = "participant"; break;
				case MUCOccupant::Visitor: result = "visitor"; break;
				}
				return result;

			}

			static boost::shared_ptr<XMLElement> itemToElement(const MUCItem& item) {
				boost::shared_ptr<XMLElement> itemElement(new XMLElement("item"));
				if (item.affiliation) {
					itemElement->setAttribute("affiliation", affiliationToString(item.affiliation.get()));
				}
				if (item.role) {
					itemElement->setAttribute("role", roleToString(item.role.get()));
				}
				if (item.realJID) {
					itemElement->setAttribute("jid", item.realJID.get());
				}
				if (item.nick) {
					itemElement->setAttribute("nick", item.nick.get());
				}
				if (item.actor) {
					boost::shared_ptr<XMLElement> actorElement(new XMLElement("actor"));
					actorElement->setAttribute("jid", item.actor->toString());
					itemElement->addNode(actorElement);
				}
				if (item.reason) {
					boost::shared_ptr<XMLElement> reasonElement(new XMLElement("reason"));
					reasonElement->addNode(boost::make_shared<XMLTextNode>(*item.reason));
					itemElement->addNode(reasonElement);
				}
				return itemElement;
			}
	};
}