summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swiften/Serializer/PayloadSerializers')
-rw-r--r--Swiften/Serializer/PayloadSerializers/UnitTest/VCardSerializerTest.cpp84
-rw-r--r--Swiften/Serializer/PayloadSerializers/VCardSerializer.cpp46
2 files changed, 127 insertions, 3 deletions
diff --git a/Swiften/Serializer/PayloadSerializers/UnitTest/VCardSerializerTest.cpp b/Swiften/Serializer/PayloadSerializers/UnitTest/VCardSerializerTest.cpp
new file mode 100644
index 0000000..11b24dc
--- /dev/null
+++ b/Swiften/Serializer/PayloadSerializers/UnitTest/VCardSerializerTest.cpp
@@ -0,0 +1,84 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include <cppunit/extensions/HelperMacros.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+
+#include "Swiften/Serializer/PayloadSerializers/VCardSerializer.h"
+
+using namespace Swift;
+
+class VCardSerializerTest : public CppUnit::TestFixture
+{
+ CPPUNIT_TEST_SUITE(VCardSerializerTest);
+ CPPUNIT_TEST(testSerialize);
+ CPPUNIT_TEST_SUITE_END();
+
+ public:
+ void testSerialize() {
+ VCardSerializer testling;
+ boost::shared_ptr<VCard> vcard(new VCard());
+ vcard->setVersion("2.0");
+ vcard->setFullName("Alice In Wonderland");
+ vcard->setPrefix("Mrs");
+ vcard->setGivenName("Alice");
+ vcard->setMiddleName("In");
+ vcard->setFamilyName("Wonderland");
+ vcard->setSuffix("PhD");
+ vcard->setNickname("DreamGirl");
+ vcard->setPhoto("abcdef");
+ vcard->setPhotoType("image/png");
+ vcard->addUnknownContent("<BDAY>1234</BDAY><MAILER>mutt</MAILER>");
+
+ VCard::EMailAddress address1;
+ address1.address = "alice@wonderland.lit";
+ address1.isHome = true;
+ address1.isPreferred = true;
+ address1.isInternet = true;
+ vcard->addEMailAddress(address1);
+
+ VCard::EMailAddress address2;
+ address2.address = "alice@teaparty.lit";
+ address2.isWork = true;
+ address2.isX400 = true;
+ vcard->addEMailAddress(address2);
+
+ String expectedResult =
+ "<vCard xmlns=\"vcard-temp\">"
+ "<VERSION>2.0</VERSION>"
+ "<FN>Alice In Wonderland</FN>"
+ "<N>"
+ "<FAMILY>Wonderland</FAMILY>"
+ "<GIVEN>Alice</GIVEN>"
+ "<MIDDLE>In</MIDDLE>"
+ "<PREFIX>Mrs</PREFIX>"
+ "<SUFFIX>PhD</SUFFIX>"
+ "</N>"
+ "<EMAIL>"
+ "<USERID>alice@wonderland.lit</USERID>"
+ "<HOME/>"
+ "<INTERNET/>"
+ "<PREF/>"
+ "</EMAIL>"
+ "<EMAIL>"
+ "<USERID>alice@teaparty.lit</USERID>"
+ "<WORK/>"
+ "<X400/>"
+ "</EMAIL>"
+ "<NICKNAME>DreamGirl</NICKNAME>"
+ "<PHOTO>"
+ "<TYPE>image/png</TYPE>"
+ "<BINVAL>616263646566</BINVAL>"
+ "</PHOTO>"
+ "<BDAY>1234</BDAY>"
+ "<MAILER>mutt</MAILER>"
+ "</vCard>";
+
+ CPPUNIT_ASSERT_EQUAL(expectedResult, testling.serialize(vcard));
+ }
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION(VCardSerializerTest);
diff --git a/Swiften/Serializer/PayloadSerializers/VCardSerializer.cpp b/Swiften/Serializer/PayloadSerializers/VCardSerializer.cpp
index 8975818..5953ef6 100644
--- a/Swiften/Serializer/PayloadSerializers/VCardSerializer.cpp
+++ b/Swiften/Serializer/PayloadSerializers/VCardSerializer.cpp
@@ -10,7 +10,9 @@
#include "Swiften/Serializer/XML/XMLElement.h"
#include "Swiften/Serializer/XML/XMLTextNode.h"
+#include "Swiften/Serializer/XML/XMLRawTextNode.h"
#include "Swiften/StringCodecs/Hexify.h"
+#include "Swiften/Base/foreach.h"
namespace Swift {
@@ -19,12 +21,17 @@ VCardSerializer::VCardSerializer() : GenericPayloadSerializer<VCard>() {
String VCardSerializer::serializePayload(boost::shared_ptr<VCard> vcard) const {
XMLElement queryElement("vCard", "vcard-temp");
+ if (!vcard->getVersion().isEmpty()) {
+ boost::shared_ptr<XMLElement> versionElement(new XMLElement("VERSION"));
+ versionElement->addNode(boost::shared_ptr<XMLTextNode>(new XMLTextNode(vcard->getVersion())));
+ queryElement.addNode(versionElement);
+ }
if (!vcard->getFullName().isEmpty()) {
boost::shared_ptr<XMLElement> fullNameElement(new XMLElement("FN"));
fullNameElement->addNode(boost::shared_ptr<XMLTextNode>(new XMLTextNode(vcard->getFullName())));
queryElement.addNode(fullNameElement);
}
- if (!vcard->getGivenName().isEmpty() || !vcard->getFamilyName().isEmpty()) {
+ if (!vcard->getGivenName().isEmpty() || !vcard->getFamilyName().isEmpty() || !vcard->getMiddleName().isEmpty() || !vcard->getPrefix().isEmpty() || !vcard->getSuffix().isEmpty()) {
boost::shared_ptr<XMLElement> nameElement(new XMLElement("N"));
if (!vcard->getFamilyName().isEmpty()) {
boost::shared_ptr<XMLElement> familyNameElement(new XMLElement("FAMILY"));
@@ -36,13 +43,43 @@ String VCardSerializer::serializePayload(boost::shared_ptr<VCard> vcard) const
givenNameElement->addNode(boost::shared_ptr<XMLTextNode>(new XMLTextNode(vcard->getGivenName())));
nameElement->addNode(givenNameElement);
}
+ if (!vcard->getMiddleName().isEmpty()) {
+ boost::shared_ptr<XMLElement> middleNameElement(new XMLElement("MIDDLE"));
+ middleNameElement->addNode(boost::shared_ptr<XMLTextNode>(new XMLTextNode(vcard->getMiddleName())));
+ nameElement->addNode(middleNameElement);
+ }
+ if (!vcard->getPrefix().isEmpty()) {
+ boost::shared_ptr<XMLElement> prefixElement(new XMLElement("PREFIX"));
+ prefixElement->addNode(boost::shared_ptr<XMLTextNode>(new XMLTextNode(vcard->getPrefix())));
+ nameElement->addNode(prefixElement);
+ }
+ if (!vcard->getSuffix().isEmpty()) {
+ boost::shared_ptr<XMLElement> suffixElement(new XMLElement("SUFFIX"));
+ suffixElement->addNode(boost::shared_ptr<XMLTextNode>(new XMLTextNode(vcard->getSuffix())));
+ nameElement->addNode(suffixElement);
+ }
queryElement.addNode(nameElement);
}
- if (!vcard->getEMail().isEmpty()) {
+ 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(vcard->getEMail())));
+ 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().isEmpty()) {
@@ -64,6 +101,9 @@ String VCardSerializer::serializePayload(boost::shared_ptr<VCard> vcard) const
}
queryElement.addNode(photoElement);
}
+ if (!vcard->getUnknownContent().isEmpty()) {
+ queryElement.addNode(boost::shared_ptr<XMLRawTextNode>(new XMLRawTextNode(vcard->getUnknownContent())));
+ }
return queryElement.serialize();
}