summaryrefslogtreecommitdiffstats
blob: 79e543e5edae8ccb4e3bfa641e3537375d18e490 (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
/*
 * Copyright (c) 2010 Remko Tronçon
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#include "Swiften/Serializer/PayloadSerializers/VCardSerializer.h"

#include <boost/shared_ptr.hpp>

#include "Swiften/Serializer/XML/XMLElement.h"
#include "Swiften/Serializer/XML/XMLTextNode.h"
#include "Swiften/Serializer/XML/XMLRawTextNode.h"
#include "Swiften/StringCodecs/Base64.h"
#include "Swiften/Base/foreach.h"

namespace Swift {

VCardSerializer::VCardSerializer() : GenericPayloadSerializer<VCard>() {
}

std::string VCardSerializer::serializePayload(boost::shared_ptr<VCard> vcard)  const {
	XMLElement queryElement("vCard", "vcard-temp");
	if (!vcard->getVersion().empty()) {
		boost::shared_ptr<XMLElement> versionElement(new XMLElement("VERSION"));
		versionElement->addNode(boost::shared_ptr<XMLTextNode>(new XMLTextNode(vcard->getVersion())));
		queryElement.addNode(versionElement);
	}
	if (!vcard->getFullName().empty()) {
		boost::shared_ptr<XMLElement> fullNameElement(new XMLElement("FN"));
		fullNameElement->addNode(boost::shared_ptr<XMLTextNode>(new XMLTextNode(vcard->getFullName())));
		queryElement.addNode(fullNameElement);
	}
	if (!vcard->getGivenName().empty() || !vcard->getFamilyName().empty() || !vcard->getMiddleName().empty() || !vcard->getPrefix().empty() || !vcard->getSuffix().empty()) {
		boost::shared_ptr<XMLElement> nameElement(new XMLElement("N"));
		if (!vcard->getFamilyName().empty()) {
			boost::shared_ptr<XMLElement> familyNameElement(new XMLElement("FAMILY"));
			familyNameElement->addNode(boost::shared_ptr<XMLTextNode>(new XMLTextNode(vcard->getFamilyName())));
			nameElement->addNode(familyNameElement);
		}
		if (!vcard->getGivenName().empty()) {
			boost::shared_ptr<XMLElement> givenNameElement(new XMLElement("GIVEN"));
			givenNameElement->addNode(boost::shared_ptr<XMLTextNode>(new XMLTextNode(vcard->getGivenName())));
			nameElement->addNode(givenNameElement);
		}
		if (!vcard->getMiddleName().empty()) {
			boost::shared_ptr<XMLElement> middleNameElement(new XMLElement("MIDDLE"));
			middleNameElement->addNode(boost::shared_ptr<XMLTextNode>(new XMLTextNode(vcard->getMiddleName())));
			nameElement->addNode(middleNameElement);
		}
		if (!vcard->getPrefix().empty()) {
			boost::shared_ptr<XMLElement> prefixElement(new XMLElement("PREFIX"));
			prefixElement->addNode(boost::shared_ptr<XMLTextNode>(new XMLTextNode(vcard->getPrefix())));
			nameElement->addNode(prefixElement);
		}
		if (!vcard->getSuffix().empty()) {
			boost::shared_ptr<XMLElement> suffixElement(new XMLElement("SUFFIX"));
			suffixElement->addNode(boost::shared_ptr<XMLTextNode>(new XMLTextNode(vcard->getSuffix())));
			nameElement->addNode(suffixElement);
		}
		queryElement.addNode(nameElement);
	}
	foreach(const VCard::EMailAddress& emailAddress, vcard->getEMailAddresses()) {
		boost::shared_ptr<XMLElement> emailElement(new XMLElement("EMAIL"));
		boost::shared_ptr<XMLElement> userIDElement(new XMLElement("USERID"));
		userIDElement->addNode(boost::shared_ptr<XMLTextNode>(new XMLTextNode(emailAddress.address)));
		emailElement->addNode(userIDElement);
		if (emailAddress.isHome) {
			emailElement->addNode(boost::shared_ptr<XMLElement>(new XMLElement("HOME")));
		}
		if (emailAddress.isWork) {
			emailElement->addNode(boost::shared_ptr<XMLElement>(new XMLElement("WORK")));
		}
		if (emailAddress.isInternet) {
			emailElement->addNode(boost::shared_ptr<XMLElement>(new XMLElement("INTERNET")));
		}
		if (emailAddress.isPreferred) {
			emailElement->addNode(boost::shared_ptr<XMLElement>(new XMLElement("PREF")));
		}
		if (emailAddress.isX400) {
			emailElement->addNode(boost::shared_ptr<XMLElement>(new XMLElement("X400")));
		}
		queryElement.addNode(emailElement);
	}
	if (!vcard->getNickname().empty()) {
		boost::shared_ptr<XMLElement> nickElement(new XMLElement("NICKNAME"));
		nickElement->addNode(boost::shared_ptr<XMLTextNode>(new XMLTextNode(vcard->getNickname())));
		queryElement.addNode(nickElement);
	}
	if (!vcard->getPhoto().isEmpty() || !vcard->getPhotoType().empty()) {
		XMLElement::ref photoElement(new XMLElement("PHOTO"));
		if (!vcard->getPhotoType().empty()) {
			XMLElement::ref typeElement(new XMLElement("TYPE"));
			typeElement->addNode(XMLTextNode::ref(new XMLTextNode(vcard->getPhotoType())));
			photoElement->addNode(typeElement);
		}
		if (!vcard->getPhoto().isEmpty()) {
			XMLElement::ref binvalElement(new XMLElement("BINVAL"));
			binvalElement->addNode(XMLTextNode::ref(new XMLTextNode(Base64::encode(vcard->getPhoto()))));
			photoElement->addNode(binvalElement);
		}
		queryElement.addNode(photoElement);
	}
	if (!vcard->getUnknownContent().empty()) {
		queryElement.addNode(boost::shared_ptr<XMLRawTextNode>(new XMLRawTextNode(vcard->getUnknownContent())));
	}
	return queryElement.serialize();
}

}