diff options
author | Tobias Markmann <tm@ayena.de> | 2012-02-26 14:32:39 (GMT) |
---|---|---|
committer | Tobias Markmann <tm@ayena.de> | 2012-11-11 16:38:00 (GMT) |
commit | 684559ff499077e33d3d5e4b9ecb39afa240d067 (patch) | |
tree | 7c0f03ddb4c73e1d2d2ddd54fe3f3b9640291e75 /Swiften/Serializer | |
parent | be6fd0b4b580d81bfe33975c28ee7a939d6c6723 (diff) | |
download | swift-contrib-684559ff499077e33d3d5e4b9ecb39afa240d067.zip swift-contrib-684559ff499077e33d3d5e4b9ecb39afa240d067.tar.bz2 |
Adding basic vCard edit/show support.
License: This patch is BSD-licensed, see Documentation/Licenses/BSD-simplified.txt for details.
Diffstat (limited to 'Swiften/Serializer')
-rw-r--r-- | Swiften/Serializer/PayloadSerializers/UnitTest/VCardSerializerTest.cpp | 80 | ||||
-rw-r--r-- | Swiften/Serializer/PayloadSerializers/VCardSerializer.cpp | 172 |
2 files changed, 244 insertions, 8 deletions
diff --git a/Swiften/Serializer/PayloadSerializers/UnitTest/VCardSerializerTest.cpp b/Swiften/Serializer/PayloadSerializers/UnitTest/VCardSerializerTest.cpp index 3ac1d77..01c8e77 100644 --- a/Swiften/Serializer/PayloadSerializers/UnitTest/VCardSerializerTest.cpp +++ b/Swiften/Serializer/PayloadSerializers/UnitTest/VCardSerializerTest.cpp @@ -31,14 +31,15 @@ class VCardSerializerTest : public CppUnit::TestFixture vcard->setNickname("DreamGirl"); vcard->setPhoto(createByteArray("abcdef")); vcard->setPhotoType("image/png"); - vcard->addUnknownContent("<BDAY>1234</BDAY><MAILER>mutt</MAILER>"); + vcard->setBirthday(boost::posix_time::ptime(boost::gregorian::date(1865, 5, 4))); + vcard->addUnknownContent("<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 emailAddress1; + emailAddress1.address = "alice@wonderland.lit"; + emailAddress1.isHome = true; + emailAddress1.isPreferred = true; + emailAddress1.isInternet = true; + vcard->addEMailAddress(emailAddress1); VCard::EMailAddress address2; address2.address = "alice@teaparty.lit"; @@ -46,6 +47,41 @@ class VCardSerializerTest : public CppUnit::TestFixture address2.isX400 = true; vcard->addEMailAddress(address2); + VCard::Telephone telephone1; + telephone1.number = "555-6273"; + telephone1.isHome = true; + telephone1.isVoice = true; + vcard->addTelephone(telephone1); + + VCard::Address address1; + address1.locality = "Any Town"; + address1.street = "Fake Street 123"; + address1.postalCode = "12345"; + address1.country = "USA"; + address1.isHome = true; + vcard->addAddress(address1); + + VCard::AddressLabel label1; + label1.lines.push_back("Fake Street 123"); + label1.lines.push_back("12345 Any Town"); + label1.lines.push_back("USA"); + label1.isHome = true; + vcard->addAddressLabel(label1); + + vcard->addJID(JID("alice@teaparty.lit")); + vcard->addJID(JID("alice@wonderland.lit")); + + vcard->setDescription("I once fell down a rabbit hole."); + + VCard::Organization org1; + org1.name = "Alice In Wonderland Inc."; + vcard->addOrganization(org1); + + vcard->addTitle("Some Title"); + vcard->addRole("Main Character"); + vcard->addURL("http://wonderland.lit/~alice"); + vcard->addURL("http://teaparty.lit/~alice2"); + std::string expectedResult = "<vCard xmlns=\"vcard-temp\">" "<VERSION>2.0</VERSION>" @@ -73,7 +109,35 @@ class VCardSerializerTest : public CppUnit::TestFixture "<TYPE>image/png</TYPE>" "<BINVAL>YWJjZGVm</BINVAL>" "</PHOTO>" - "<BDAY>1234</BDAY>" + "<BDAY>1865-05-04T00:00:00Z</BDAY>" + "<TEL>" + "<NUMBER>555-6273</NUMBER>" + "<HOME/>" + "<VOICE/>" + "</TEL>" + "<ADR>" + "<STREET>Fake Street 123</STREET>" + "<LOCALITY>Any Town</LOCALITY>" + "<PCODE>12345</PCODE>" + "<CTRY>USA</CTRY>" + "<HOME/>" + "</ADR>" + "<LABEL>" + "<LINE>Fake Street 123</LINE>" + "<LINE>12345 Any Town</LINE>" + "<LINE>USA</LINE>" + "<HOME/>" + "</LABEL>" + "<JID>alice@teaparty.lit</JID>" + "<JID>alice@wonderland.lit</JID>" + "<DESC>I once fell down a rabbit hole.</DESC>" + "<ORG>" + "<ORGNAME>Alice In Wonderland Inc.</ORGNAME>" + "</ORG>" + "<TITLE>Some Title</TITLE>" + "<ROLE>Main Character</ROLE>" + "<URL>http://wonderland.lit/~alice</URL>" + "<URL>http://teaparty.lit/~alice2</URL>" "<MAILER>mutt</MAILER>" "</vCard>"; diff --git a/Swiften/Serializer/PayloadSerializers/VCardSerializer.cpp b/Swiften/Serializer/PayloadSerializers/VCardSerializer.cpp index 1512c6c..2676a02 100644 --- a/Swiften/Serializer/PayloadSerializers/VCardSerializer.cpp +++ b/Swiften/Serializer/PayloadSerializers/VCardSerializer.cpp @@ -13,10 +13,17 @@ #include <Swiften/Serializer/XML/XMLTextNode.h> #include <Swiften/Serializer/XML/XMLRawTextNode.h> #include <Swiften/StringCodecs/Base64.h> +#include <Swiften/Base/DateTime.h> #include <Swiften/Base/foreach.h> namespace Swift { +boost::shared_ptr<XMLElement> createTextElement(const std::string& elementName, const std::string& text) { + boost::shared_ptr<XMLElement> element = boost::make_shared<XMLElement>(elementName); + element->addNode(boost::make_shared<XMLTextNode>(text)); + return element; +} + VCardSerializer::VCardSerializer() : GenericPayloadSerializer<VCard>() { } @@ -102,6 +109,171 @@ std::string VCardSerializer::serializePayload(boost::shared_ptr<VCard> vcard) c } queryElement.addNode(photoElement); } + if (!vcard->getBirthday().is_not_a_date_time()) { + XMLElement::ref bdayElement(new XMLElement("BDAY")); + bdayElement->addNode(boost::shared_ptr<XMLTextNode>(new XMLTextNode(dateTimeToString(vcard->getBirthday())))); + queryElement.addNode(bdayElement); + } + + foreach(const VCard::Telephone& telephone, vcard->getTelephones()) { + boost::shared_ptr<XMLElement> telElement(new XMLElement("TEL")); + boost::shared_ptr<XMLElement> numberElement(new XMLElement("NUMBER")); + numberElement->addNode(boost::shared_ptr<XMLTextNode>(new XMLTextNode(telephone.number))); + telElement->addNode(numberElement); + if (telephone.isHome) { + telElement->addNode(boost::shared_ptr<XMLElement>(new XMLElement("HOME"))); + } + if (telephone.isWork) { + telElement->addNode(boost::shared_ptr<XMLElement>(new XMLElement("WORK"))); + } + if (telephone.isVoice) { + telElement->addNode(boost::shared_ptr<XMLElement>(new XMLElement("VOICE"))); + } + if (telephone.isFax) { + telElement->addNode(boost::shared_ptr<XMLElement>(new XMLElement("FAX"))); + } + if (telephone.isPager) { + telElement->addNode(boost::shared_ptr<XMLElement>(new XMLElement("PAGER"))); + } + if (telephone.isMSG) { + telElement->addNode(boost::shared_ptr<XMLElement>(new XMLElement("MSG"))); + } + if (telephone.isCell) { + telElement->addNode(boost::shared_ptr<XMLElement>(new XMLElement("CELL"))); + } + if (telephone.isVideo) { + telElement->addNode(boost::shared_ptr<XMLElement>(new XMLElement("VIDEO"))); + } + if (telephone.isBBS) { + telElement->addNode(boost::shared_ptr<XMLElement>(new XMLElement("BBS"))); + } + if (telephone.isModem) { + telElement->addNode(boost::shared_ptr<XMLElement>(new XMLElement("MODEM"))); + } + if (telephone.isISDN) { + telElement->addNode(boost::shared_ptr<XMLElement>(new XMLElement("ISDN"))); + } + if (telephone.isPCS) { + telElement->addNode(boost::shared_ptr<XMLElement>(new XMLElement("PCS"))); + } + if (telephone.isPreferred) { + telElement->addNode(boost::shared_ptr<XMLElement>(new XMLElement("PREF"))); + } + queryElement.addNode(telElement); + } + + foreach(const VCard::Address& address, vcard->getAddresses()) { + boost::shared_ptr<XMLElement> adrElement(new XMLElement("ADR")); + if (!address.POBox.empty()) { + adrElement->addNode(createTextElement("POBOX", address.POBox)); + } + if (!address.addressExtension.empty()) { + adrElement->addNode(createTextElement("EXTADD", address.addressExtension)); + } + if (!address.street.empty()) { + adrElement->addNode(createTextElement("STREET", address.street)); + } + if (!address.locality.empty()) { + adrElement->addNode(createTextElement("LOCALITY", address.locality)); + } + if (!address.region.empty()) { + adrElement->addNode(createTextElement("REGION", address.region)); + } + if (!address.postalCode.empty()) { + adrElement->addNode(createTextElement("PCODE", address.postalCode)); + } + if (!address.country.empty()) { + adrElement->addNode(createTextElement("CTRY", address.country)); + } + + if (address.isHome) { + adrElement->addNode(boost::make_shared<XMLElement>("HOME")); + } + if (address.isWork) { + adrElement->addNode(boost::make_shared<XMLElement>("WORK")); + } + if (address.isPostal) { + adrElement->addNode(boost::make_shared<XMLElement>("POSTAL")); + } + if (address.isParcel) { + adrElement->addNode(boost::make_shared<XMLElement>("PARCEL")); + } + if (address.deliveryType == VCard::DomesticDelivery) { + adrElement->addNode(boost::make_shared<XMLElement>("DOM")); + } + if (address.deliveryType == VCard::InternationalDelivery) { + adrElement->addNode(boost::make_shared<XMLElement>("INTL")); + } + if (address.isPreferred) { + adrElement->addNode(boost::make_shared<XMLElement>("PREF")); + } + queryElement.addNode(adrElement); + } + + foreach(const VCard::AddressLabel& addressLabel, vcard->getAddressLabels()) { + boost::shared_ptr<XMLElement> labelElement(new XMLElement("LABEL")); + + foreach(const std::string& line, addressLabel.lines) { + labelElement->addNode(createTextElement("LINE", line)); + } + + if (addressLabel.isHome) { + labelElement->addNode(boost::make_shared<XMLElement>("HOME")); + } + if (addressLabel.isWork) { + labelElement->addNode(boost::make_shared<XMLElement>("WORK")); + } + if (addressLabel.isPostal) { + labelElement->addNode(boost::make_shared<XMLElement>("POSTAL")); + } + if (addressLabel.isParcel) { + labelElement->addNode(boost::make_shared<XMLElement>("PARCEL")); + } + if (addressLabel.deliveryType == VCard::DomesticDelivery) { + labelElement->addNode(boost::make_shared<XMLElement>("DOM")); + } + if (addressLabel.deliveryType == VCard::InternationalDelivery) { + labelElement->addNode(boost::make_shared<XMLElement>("INTL")); + } + if (addressLabel.isPreferred) { + labelElement->addNode(boost::make_shared<XMLElement>("PREF")); + } + queryElement.addNode(labelElement); + } + + foreach(const JID& jid, vcard->getJIDs()) { + queryElement.addNode(createTextElement("JID", jid.toString())); + } + + if (!vcard->getDescription().empty()) { + queryElement.addNode(createTextElement("DESC", vcard->getDescription())); + } + + foreach(const VCard::Organization& org, vcard->getOrganizations()) { + boost::shared_ptr<XMLElement> orgElement(new XMLElement("ORG")); + if (!org.name.empty()) { + orgElement->addNode(createTextElement("ORGNAME", org.name)); + } + if (!org.units.empty()) { + foreach(const std::string& unit, org.units) { + orgElement->addNode(createTextElement("ORGUNIT", unit)); + } + } + queryElement.addNode(orgElement); + } + + foreach(const std::string& title, vcard->getTitles()) { + queryElement.addNode(createTextElement("TITLE", title)); + } + + foreach(const std::string& role, vcard->getRoles()) { + queryElement.addNode(createTextElement("ROLE", role)); + } + + foreach(const std::string& url, vcard->getURLs()) { + queryElement.addNode(createTextElement("URL", url)); + } + if (!vcard->getUnknownContent().empty()) { queryElement.addNode(boost::make_shared<XMLRawTextNode>(vcard->getUnknownContent())); } |