From 8f75cf3d9892a3ce2c58537f29c851392231e29c Mon Sep 17 00:00:00 2001 From: Tobias Markmann Date: Mon, 8 Oct 2012 23:59:42 +0200 Subject: Update XEP-0234 implementation to version 0.15 of the standard. diff --git a/Swiften/Elements/JingleFileTransferDescription.h b/Swiften/Elements/JingleFileTransferDescription.h index 04f3f1f..bdd5aae 100644 --- a/Swiften/Elements/JingleFileTransferDescription.h +++ b/Swiften/Elements/JingleFileTransferDescription.h @@ -10,32 +10,32 @@ #include #include -#include +#include namespace Swift { class JingleFileTransferDescription : public JingleDescription { public: typedef boost::shared_ptr ref; - void addOffer(const StreamInitiationFileInfo& offer) { + void addOffer(const JingleFileTransferFileInfo& offer) { offers.push_back(offer); } - const std::vector& getOffers() const { + const std::vector& getOffers() const { return offers; } - void addRequest(const StreamInitiationFileInfo& request) { + void addRequest(const JingleFileTransferFileInfo& request) { reqeusts.push_back(request); } - const std::vector& getRequests() const { + const std::vector& getRequests() const { return reqeusts; } private: - std::vector offers; - std::vector reqeusts; + std::vector offers; + std::vector reqeusts; }; } diff --git a/Swiften/Elements/JingleFileTransferFileInfo.h b/Swiften/Elements/JingleFileTransferFileInfo.h new file mode 100644 index 0000000..a174df0 --- /dev/null +++ b/Swiften/Elements/JingleFileTransferFileInfo.h @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2012 Tobias Markmann + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#pragma once + +#include +#include +#include +#include + +#include + +namespace Swift { + +class JingleFileTransferFileInfo : public Payload { +public: + typedef boost::shared_ptr ref; + +public: + JingleFileTransferFileInfo(const std::string& name = "", const std::string& description = "", int size = 0, + const std::string& hash = "", const boost::posix_time::ptime &date = boost::posix_time::ptime(), const std::string& algo="md5") : + name(name), description(description), size(size), hash(hash), date(date), algo(algo), supportsRangeRequests(false), rangeOffset(0) {} + JingleFileTransferFileInfo(const StreamInitiationFileInfo& siFileInfo) { + this->name = siFileInfo.getName(); + this->description = siFileInfo.getDescription(); + this->size = siFileInfo.getSize(); + this->hash = siFileInfo.getHash(); + this->date = siFileInfo.getDate(); + this->algo = siFileInfo.getAlgo(); + this->supportsRangeRequests = siFileInfo.getSupportsRangeRequests(); + this->rangeOffset = siFileInfo.getRangeOffset(); + } + + void setName(const std::string& name) { + this->name = name;; + } + + const std::string& getName() const { + return this->name; + } + + void setDescription(const std::string& description) { + this->description = description; + } + + const std::string& getDescription() const { + return this->description; + } + + void setSize(const boost::uintmax_t size) { + this->size = size; + } + + boost::uintmax_t getSize() const { + return this->size; + } + + void setHash(const std::string& hash) { + this->hash = hash; + } + + const std::string& getHash() const { + return this->hash; + } + + void setDate(const boost::posix_time::ptime& date) { + this->date = date; + } + + const boost::posix_time::ptime& getDate() const { + return this->date; + } + + void setAlgo(const std::string& algo) { + this->algo = algo; + } + + const std::string& getAlgo() const { + return this->algo; + } + + void setSupportsRangeRequests(const bool supportsIt) { + supportsRangeRequests = supportsIt; + } + + bool getSupportsRangeRequests() const { + return supportsRangeRequests; + } + + void setRangeOffset(const int offset) { + supportsRangeRequests = offset >= 0 ? true : false; + rangeOffset = offset; + } + + boost::uintmax_t getRangeOffset() const { + return rangeOffset; + } + +private: + std::string name; + std::string description; + boost::uintmax_t size; + std::string hash; + boost::posix_time::ptime date; + std::string algo; + bool supportsRangeRequests; + boost::uintmax_t rangeOffset; +}; + +} diff --git a/Swiften/Elements/JingleFileTransferReceived.h b/Swiften/Elements/JingleFileTransferReceived.h index 75c95d9..0a65088 100644 --- a/Swiften/Elements/JingleFileTransferReceived.h +++ b/Swiften/Elements/JingleFileTransferReceived.h @@ -9,7 +9,7 @@ #include #include -#include +#include #include namespace Swift { @@ -18,15 +18,15 @@ class JingleFileTransferReceived : public Payload { public: typedef boost::shared_ptr ref; - void setFileInfo(const StreamInitiationFileInfo& fileInfo) { + void setFileInfo(const JingleFileTransferFileInfo& fileInfo) { this->fileInfo = fileInfo; } - const StreamInitiationFileInfo& getFileInfo() const { + const JingleFileTransferFileInfo& getFileInfo() const { return this->fileInfo; } private: - StreamInitiationFileInfo fileInfo; + JingleFileTransferFileInfo fileInfo; }; diff --git a/Swiften/FileTransfer/IncomingJingleFileTransfer.cpp b/Swiften/FileTransfer/IncomingJingleFileTransfer.cpp index 808ff58..dd7cde4 100644 --- a/Swiften/FileTransfer/IncomingJingleFileTransfer.cpp +++ b/Swiften/FileTransfer/IncomingJingleFileTransfer.cpp @@ -64,7 +64,7 @@ IncomingJingleFileTransfer::IncomingJingleFileTransfer( description = initialContent->getDescription(); assert(description); assert(description->getOffers().size() == 1); - StreamInitiationFileInfo fileInfo = description->getOffers().front(); + JingleFileTransferFileInfo fileInfo = description->getOffers().front(); fileSizeInBytes = fileInfo.getSize(); filename = fileInfo.getName(); hash = fileInfo.getHash(); diff --git a/Swiften/FileTransfer/OutgoingJingleFileTransfer.h b/Swiften/FileTransfer/OutgoingJingleFileTransfer.h index e18b5c3..2dbc89c 100644 --- a/Swiften/FileTransfer/OutgoingJingleFileTransfer.h +++ b/Swiften/FileTransfer/OutgoingJingleFileTransfer.h @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -93,7 +94,7 @@ private: JID fromJID; JID toJID; boost::shared_ptr readStream; - StreamInitiationFileInfo fileInfo; + JingleFileTransferFileInfo fileInfo; IncrementalBytestreamHashCalculator *hashCalculator; boost::shared_ptr ibbSession; diff --git a/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp b/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp index a40e8f6..0a0fc39 100644 --- a/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp +++ b/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp @@ -59,6 +59,7 @@ #include #include #include +#include #include #include #include @@ -122,6 +123,7 @@ FullPayloadParserFactoryCollection::FullPayloadParserFactoryCollection() { factories_.push_back(boost::make_shared >("transport", "urn:xmpp:jingle:transports:s5b:1")); factories_.push_back(boost::make_shared(this)); factories_.push_back(boost::make_shared >("file", "http://jabber.org/protocol/si/profile/file-transfer")); + factories_.push_back(boost::make_shared >("file", "urn:xmpp:jingle:apps:file-transfer:3")); factories_.push_back(boost::make_shared >("received", "urn:xmpp:jingle:apps:file-transfer:3")); factories_.push_back(boost::make_shared >("checksum")); factories_.push_back(boost::make_shared >("query", "http://jabber.org/protocol/bytestreams")); diff --git a/Swiften/Parser/PayloadParsers/JingleFileTransferDescriptionParser.cpp b/Swiften/Parser/PayloadParsers/JingleFileTransferDescriptionParser.cpp index b394115..fa6c6ae 100644 --- a/Swiften/Parser/PayloadParsers/JingleFileTransferDescriptionParser.cpp +++ b/Swiften/Parser/PayloadParsers/JingleFileTransferDescriptionParser.cpp @@ -54,7 +54,7 @@ void JingleFileTransferDescriptionParser::handleEndElement(const std::string& el } if (level == 2) { - boost::shared_ptr info = boost::dynamic_pointer_cast(currentPayloadParser->getPayload()); + boost::shared_ptr info = boost::dynamic_pointer_cast(currentPayloadParser->getPayload()); if (info) { if (currentElement == OfferElement) { getPayloadInternal()->addOffer(*info); diff --git a/Swiften/Parser/PayloadParsers/JingleFileTransferFileInfoParser.cpp b/Swiften/Parser/PayloadParsers/JingleFileTransferFileInfoParser.cpp new file mode 100644 index 0000000..769fc84 --- /dev/null +++ b/Swiften/Parser/PayloadParsers/JingleFileTransferFileInfoParser.cpp @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2012 Tobias Markmann + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#include "JingleFileTransferFileInfoParser.h" + +#include +#include + +#include +#include + +#include + +namespace Swift { + +JingleFileTransferFileInfoParser::JingleFileTransferFileInfoParser() : level(0) { + +} + +void JingleFileTransferFileInfoParser::handleStartElement(const std::string& element, const std::string&, const AttributeMap& attributes) { + if (level == 0) { + + } else if (level == 1) { + if (element == "date" || element == "desc" || element == "name" || element == "size") { + cdataBuffer.clear(); + } else if (element == "hash") { + cdataBuffer.clear(); + getPayloadInternal()->setAlgo(attributes.getAttributeValue("algo").get_value_or("md5")); + } else { + if (element == "range") { + int offset = 0; + try { + offset = boost::lexical_cast(attributes.getAttributeValue("offset").get_value_or("0")); + } catch (boost::bad_lexical_cast &) { + offset = 0; + } + if (offset == 0) { + getPayloadInternal()->setSupportsRangeRequests(true); + } else { + getPayloadInternal()->setRangeOffset(offset); + } + } + } + } + ++level; +} + +void JingleFileTransferFileInfoParser::handleEndElement(const std::string& element, const std::string&) { + --level; + if (element == "date") { + getPayloadInternal()->setDate(stringToDateTime(cdataBuffer)); + } else if (element == "desc") { + getPayloadInternal()->setDescription(cdataBuffer); + } else if (element == "name") { + getPayloadInternal()->setName(cdataBuffer); + } else if (element == "size") { + try { + getPayloadInternal()->setSize(boost::lexical_cast(cdataBuffer)); + } catch (boost::bad_lexical_cast &) { + getPayloadInternal()->setSize(0); + } + } else if (element == "hash") { + getPayloadInternal()->setHash(cdataBuffer); + } +} + +void JingleFileTransferFileInfoParser::handleCharacterData(const std::string& data) { + cdataBuffer += data; +} + +} diff --git a/Swiften/Parser/PayloadParsers/JingleFileTransferFileInfoParser.h b/Swiften/Parser/PayloadParsers/JingleFileTransferFileInfoParser.h new file mode 100644 index 0000000..5b44f9a --- /dev/null +++ b/Swiften/Parser/PayloadParsers/JingleFileTransferFileInfoParser.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2012 Tobias Markmann + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#pragma once + +#include +#include + +namespace Swift { + +class JingleFileTransferFileInfoParser : public GenericPayloadParser { + public: + JingleFileTransferFileInfoParser(); + + virtual void handleStartElement(const std::string& element, const std::string&, const AttributeMap& attributes); + virtual void handleEndElement(const std::string& element, const std::string&); + virtual void handleCharacterData(const std::string& data); + + private: + int level; + std::string cdataBuffer; +}; + +} diff --git a/Swiften/Parser/PayloadParsers/JingleFileTransferReceivedParser.cpp b/Swiften/Parser/PayloadParsers/JingleFileTransferReceivedParser.cpp index ae56981..afe1da1 100644 --- a/Swiften/Parser/PayloadParsers/JingleFileTransferReceivedParser.cpp +++ b/Swiften/Parser/PayloadParsers/JingleFileTransferReceivedParser.cpp @@ -7,8 +7,7 @@ #include #include -#include -#include +#include #include #include @@ -19,7 +18,7 @@ JingleFileTransferReceivedParser::JingleFileTransferReceivedParser() : level(0) void JingleFileTransferReceivedParser::handleStartElement(const std::string& element, const std::string& ns, const AttributeMap& attributes) { if (level == 1 && element == "file") { - PayloadParserFactory* payloadParserFactory = new GenericPayloadParserFactory("file", "http://jabber.org/protocol/si/profile/file-transfer"); + PayloadParserFactory* payloadParserFactory = new GenericPayloadParserFactory("file"); if (payloadParserFactory) { currentPayloadParser.reset(payloadParserFactory->createPayloadParser()); } @@ -32,18 +31,22 @@ void JingleFileTransferReceivedParser::handleStartElement(const std::string& ele ++level; } -void JingleFileTransferReceivedParser::handleEndElement(const std::string& element, const std::string& ) { +void JingleFileTransferReceivedParser::handleEndElement(const std::string& element, const std::string& ns) { --level; if (element == "file") { - boost::shared_ptr fileInfo = boost::dynamic_pointer_cast(currentPayloadParser->getPayload()); + boost::shared_ptr fileInfo = boost::dynamic_pointer_cast(currentPayloadParser->getPayload()); if (fileInfo) { getPayloadInternal()->setFileInfo(*fileInfo); } + } else { + currentPayloadParser->handleEndElement(element, ns); } } -void JingleFileTransferReceivedParser::handleCharacterData(const std::string& ) { - +void JingleFileTransferReceivedParser::handleCharacterData(const std::string &data) { + if (currentPayloadParser && level >= 1) { + currentPayloadParser->handleCharacterData(data); + } } } diff --git a/Swiften/Parser/PayloadParsers/JingleFileTransferReceivedParser.h b/Swiften/Parser/PayloadParsers/JingleFileTransferReceivedParser.h index 824b06d..d5ed138 100644 --- a/Swiften/Parser/PayloadParsers/JingleFileTransferReceivedParser.h +++ b/Swiften/Parser/PayloadParsers/JingleFileTransferReceivedParser.h @@ -16,7 +16,7 @@ public: JingleFileTransferReceivedParser(); virtual void handleStartElement(const std::string& element, const std::string&, const AttributeMap& attributes); - virtual void handleEndElement(const std::string& element, const std::string&); + virtual void handleEndElement(const std::string& element, const std::string& ns); virtual void handleCharacterData(const std::string& data); private: @@ -24,4 +24,4 @@ private: int level; }; -} \ No newline at end of file +} diff --git a/Swiften/Parser/PayloadParsers/UnitTest/JingleParserTest.cpp b/Swiften/Parser/PayloadParsers/UnitTest/JingleParserTest.cpp index 8719a5d..95423c1 100644 --- a/Swiften/Parser/PayloadParsers/UnitTest/JingleParserTest.cpp +++ b/Swiften/Parser/PayloadParsers/UnitTest/JingleParserTest.cpp @@ -35,9 +35,9 @@ class JingleParserTest : public CppUnit::TestFixture { CPPUNIT_TEST(testParse_Xep0234_Example3); CPPUNIT_TEST(testParse_Xep0234_Example5); CPPUNIT_TEST(testParse_Xep0234_Example8); - CPPUNIT_TEST(testParse_Xep0234_Example10); - CPPUNIT_TEST(testParse_Xep0234_Example11); CPPUNIT_TEST(testParse_Xep0234_Example12); + CPPUNIT_TEST(testParse_Xep0234_Example13); + CPPUNIT_TEST(testParse_Xep0234_Example14); CPPUNIT_TEST(testParse_Xep0260_Example1); CPPUNIT_TEST(testParse_Xep0260_Example3); @@ -229,13 +229,13 @@ class JingleParserTest : public CppUnit::TestFixture { " \n" " \n" " \n" - " \n" + " \n" + " 1969-07-21T02:56:15Z\n" " This is a test. If this were a real file...\n" + "............test.txt\n" " \n" + " 1022\n" + " 552da749930852c69ae5d2141d3766b1\n" " \n" " \n" " \n" @@ -271,7 +271,7 @@ class JingleParserTest : public CppUnit::TestFixture { JingleFileTransferDescription::ref description = contents[0]->getDescription(); - std::vector offers = description->getOffers(); + std::vector offers = description->getOffers(); CPPUNIT_ASSERT_EQUAL(static_cast(1), offers.size()); CPPUNIT_ASSERT_EQUAL(std::string("test.txt"), offers[0].getName()); CPPUNIT_ASSERT_EQUAL(std::string("552da749930852c69ae5d2141d3766b1"), offers[0].getHash()); @@ -279,7 +279,7 @@ class JingleParserTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(std::string("This is a test. If this were a real file..."), offers[0].getDescription()); CPPUNIT_ASSERT_EQUAL(true, offers[0].getSupportsRangeRequests()); CPPUNIT_ASSERT(stringToDateTime("1969-07-21T02:56:15Z") == offers[0].getDate()); - CPPUNIT_ASSERT_EQUAL(std::string("md5"), offers[0].getAlgo()); + CPPUNIT_ASSERT_EQUAL(std::string("sha-1"), offers[0].getAlgo()); } // http://xmpp.org/extensions/xep-0234.html#example-3 @@ -293,14 +293,14 @@ class JingleParserTest : public CppUnit::TestFixture { " \n" " \n" " \n" - " \n" - " This is a test. If this were a real file...\n" - " \n" - " \n" + " \n" + " 1969-07-21T02:56:15Z\n" + " This is a test. If this were a real file...\n" + "............test.txt\n" + " \n" + " 1022\n" + " 552da749930852c69ae5d2141d3766b1\n" + " \n" " \n" " \n" " getDescription(); - std::vector offers = description->getOffers(); + std::vector offers = description->getOffers(); CPPUNIT_ASSERT_EQUAL(static_cast(1), offers.size()); CPPUNIT_ASSERT_EQUAL(std::string("test.txt"), offers[0].getName()); CPPUNIT_ASSERT_EQUAL(std::string("552da749930852c69ae5d2141d3766b1"), offers[0].getHash()); @@ -349,7 +349,7 @@ class JingleParserTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(std::string("This is a test. If this were a real file..."), offers[0].getDescription()); CPPUNIT_ASSERT_EQUAL(true, offers[0].getSupportsRangeRequests()); CPPUNIT_ASSERT(stringToDateTime("1969-07-21T02:56:15Z") == offers[0].getDate()); - CPPUNIT_ASSERT_EQUAL(std::string("md5"), offers[0].getAlgo()); + CPPUNIT_ASSERT_EQUAL(std::string("sha-1"), offers[0].getAlgo()); } // http://xmpp.org/extensions/xep-0234.html#example-5 @@ -395,9 +395,7 @@ class JingleParserTest : public CppUnit::TestFixture { " sid='a73sjjvkla37jfea'>\n" " \n" " \n" - " \n" - " 552da749930852c69ae5d2141d3766b1\n" - " \n" + " 552da749930852c69ae5d2141d3766b1\n" " \n" " \n" "\n" @@ -414,8 +412,8 @@ class JingleParserTest : public CppUnit::TestFixture { } - // http://xmpp.org/extensions/xep-0234.html#example-10 - void testParse_Xep0234_Example10() { + // http://xmpp.org/extensions/xep-0234.html#example-12 + void testParse_Xep0234_Example12() { PayloadsParserTester parser; CPPUNIT_ASSERT(parser.parse( "\n" " \n" " \n" - " \n" + " \n" " \n" + " 552da749930852c69ae5d2141d3766b1\n" " \n" " \n" " \n" @@ -466,14 +464,14 @@ class JingleParserTest : public CppUnit::TestFixture { JingleContentPayload::ref content = jingle->getPayload(); CPPUNIT_ASSERT(content); - StreamInitiationFileInfo file = content->getDescription()->getRequests()[0]; + JingleFileTransferFileInfo file = content->getDescription()->getRequests()[0]; CPPUNIT_ASSERT_EQUAL(std::string("552da749930852c69ae5d2141d3766b1"), file.getHash()); CPPUNIT_ASSERT_EQUAL(static_cast(270336), file.getRangeOffset()); CPPUNIT_ASSERT_EQUAL(true, file.getSupportsRangeRequests()); } - // http://xmpp.org/extensions/xep-0234.html#example-11 - void testParse_Xep0234_Example11() { + // http://xmpp.org/extensions/xep-0234.html#example-13 + void testParse_Xep0234_Example13() { PayloadsParserTester parser; CPPUNIT_ASSERT(parser.parse( "\n" " \n" " \n" - " \n" - " \n" - " \n" + " \n" + " 2011-06-01T15:58:15Z\n" + " somefile.txt\n" + " 1234\n" + " a749930852c69ae5d2141d3766b1552d\n" + " \n" + " \n" + " 2011-06-01T15:58:15Z\n" + " somefile.txt\n" + " 2345\n" + " a749930852c69ae5d2141d3766b1552d\n" + " \n" + " \n" + " 2011-06-01T15:58:15Z\n" + " yetanotherfile.txt\n" + " 3456\n" + " 52c69ae5d2141d3766b1552da7499308\n" + " \n" " \n" " \n" " getCreator()); CPPUNIT_ASSERT_EQUAL(std::string("a-file-offer"), content->getName()); - std::vector offers = content->getDescription()->getOffers(); + std::vector offers = content->getDescription()->getOffers(); CPPUNIT_ASSERT_EQUAL(static_cast(3), offers.size()); } - // http://xmpp.org/extensions/xep-0234.html#example-12 - void testParse_Xep0234_Example12() { + // http://xmpp.org/extensions/xep-0234.html#example-14 + void testParse_Xep0234_Example14() { PayloadsParserTester parser; CPPUNIT_ASSERT(parser.parse( "\n" " \n" - " \n" + " \n" + " a749930852c69ae5d2141d3766b1552d\n" + "...." " \n" "\n" )); diff --git a/Swiften/Parser/SConscript b/Swiften/Parser/SConscript index 64e9eb9..ce3b1b7 100644 --- a/Swiften/Parser/SConscript +++ b/Swiften/Parser/SConscript @@ -38,6 +38,7 @@ sources = [ "PayloadParsers/JingleFileTransferDescriptionParser.cpp", "PayloadParsers/JingleFileTransferReceivedParser.cpp", "PayloadParsers/JingleFileTransferHashParser.cpp", + "PayloadParsers/JingleFileTransferFileInfoParser.cpp", "PayloadParsers/StreamInitiationFileInfoParser.cpp", "PayloadParsers/CommandParser.cpp", "PayloadParsers/InBandRegistrationPayloadParser.cpp", diff --git a/Swiften/SConscript b/Swiften/SConscript index db18cc3..b7a2af7 100644 --- a/Swiften/SConscript +++ b/Swiften/SConscript @@ -200,6 +200,7 @@ if env["SCONS_STAGE"] == "build" : "Serializer/PayloadSerializers/JingleFileTransferReceivedSerializer.cpp", "Serializer/PayloadSerializers/JingleIBBTransportPayloadSerializer.cpp", "Serializer/PayloadSerializers/JingleS5BTransportPayloadSerializer.cpp", + "Serializer/PayloadSerializers/JingleFileTransferFileInfoSerializer.cpp", "Serializer/PayloadSerializers/StreamInitiationFileInfoSerializer.cpp", "Serializer/PayloadSerializers/DeliveryReceiptSerializer.cpp", "Serializer/PayloadSerializers/DeliveryReceiptRequestSerializer.cpp", diff --git a/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp b/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp index b4822cd..8946360 100644 --- a/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp +++ b/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp @@ -53,6 +53,7 @@ #include #include +#include #include #include #include @@ -112,6 +113,7 @@ FullPayloadSerializerCollection::FullPayloadSerializerCollection() { serializers_.push_back(new WhiteboardSerializer()); serializers_.push_back(new StreamInitiationFileInfoSerializer()); + serializers_.push_back(new JingleFileTransferFileInfoSerializer()); serializers_.push_back(new JingleContentPayloadSerializer()); serializers_.push_back(new JingleFileTransferDescriptionSerializer()); serializers_.push_back(new JingleFileTransferHashSerializer()); diff --git a/Swiften/Serializer/PayloadSerializers/JingleFileTransferDescriptionSerializer.cpp b/Swiften/Serializer/PayloadSerializers/JingleFileTransferDescriptionSerializer.cpp index 16337ff..1162a32 100644 --- a/Swiften/Serializer/PayloadSerializers/JingleFileTransferDescriptionSerializer.cpp +++ b/Swiften/Serializer/PayloadSerializers/JingleFileTransferDescriptionSerializer.cpp @@ -14,7 +14,7 @@ #include #include -#include +#include namespace Swift { @@ -23,19 +23,19 @@ JingleFileTransferDescriptionSerializer::JingleFileTransferDescriptionSerializer std::string JingleFileTransferDescriptionSerializer::serializePayload(boost::shared_ptr payload) const { XMLElement description("description", "urn:xmpp:jingle:apps:file-transfer:3"); - StreamInitiationFileInfoSerializer fileInfoSerializer; + JingleFileTransferFileInfoSerializer fileInfoSerializer; if (!payload->getOffers().empty()) { boost::shared_ptr offers = boost::make_shared("offer"); - foreach(const StreamInitiationFileInfo &fileInfo, payload->getOffers()) { - boost::shared_ptr fileInfoXML = boost::make_shared(fileInfoSerializer.serialize(boost::make_shared(fileInfo))); + foreach(const JingleFileTransferFileInfo &fileInfo, payload->getOffers()) { + boost::shared_ptr fileInfoXML = boost::make_shared(fileInfoSerializer.serialize(boost::make_shared(fileInfo))); offers->addNode(fileInfoXML); } description.addNode(offers); } if (!payload->getRequests().empty()) { boost::shared_ptr requests = boost::make_shared("request"); - foreach(const StreamInitiationFileInfo &fileInfo, payload->getRequests()) { - boost::shared_ptr fileInfoXML = boost::make_shared(fileInfoSerializer.serialize(boost::make_shared(fileInfo))); + foreach(const JingleFileTransferFileInfo &fileInfo, payload->getRequests()) { + boost::shared_ptr fileInfoXML = boost::make_shared(fileInfoSerializer.serialize(boost::make_shared(fileInfo))); requests->addNode(fileInfoXML); } description.addNode(requests); diff --git a/Swiften/Serializer/PayloadSerializers/JingleFileTransferFileInfoSerializer.cpp b/Swiften/Serializer/PayloadSerializers/JingleFileTransferFileInfoSerializer.cpp new file mode 100644 index 0000000..a13a40a --- /dev/null +++ b/Swiften/Serializer/PayloadSerializers/JingleFileTransferFileInfoSerializer.cpp @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2012 Tobias Markmann + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + + + +namespace Swift { + +JingleFileTransferFileInfoSerializer::JingleFileTransferFileInfoSerializer() { +} + +std::string JingleFileTransferFileInfoSerializer::serializePayload(boost::shared_ptr fileInfo) const { + XMLElement fileElement("file"); + + if (fileInfo->getDate() != stringToDateTime("")) { + boost::shared_ptr date = boost::make_shared("date", "", dateTimeToString(fileInfo->getDate())); + fileElement.addNode(date); + } + if (!fileInfo->getDescription().empty()) { + boost::shared_ptr desc = boost::make_shared("desc", "", fileInfo->getDescription()); + fileElement.addNode(desc); + } + if (!fileInfo->getName().empty()) { + boost::shared_ptr name = boost::make_shared("name", "", fileInfo->getName()); + fileElement.addNode(name); + } + if (fileInfo->getSupportsRangeRequests()) { + boost::shared_ptr range = boost::make_shared("range"); + if (fileInfo->getRangeOffset() != 0) { + range->setAttribute("offset", boost::lexical_cast(fileInfo->getRangeOffset())); + } + fileElement.addNode(range); + } + if (fileInfo->getSize() != 0) { + boost::shared_ptr size = boost::make_shared("size", "", boost::lexical_cast(fileInfo->getSize())); + fileElement.addNode(size); + } + if (!fileInfo->getHash().empty()) { + boost::shared_ptr hash = boost::make_shared("hash", "urn:xmpp:hashes:1", fileInfo->getHash()); + if (!fileInfo->getAlgo().empty()) { + hash->setAttribute("algo", fileInfo->getAlgo()); + } + fileElement.addNode(hash); + } + + return fileElement.serialize(); +} + +} diff --git a/Swiften/Serializer/PayloadSerializers/JingleFileTransferFileInfoSerializer.h b/Swiften/Serializer/PayloadSerializers/JingleFileTransferFileInfoSerializer.h new file mode 100644 index 0000000..6f468d7 --- /dev/null +++ b/Swiften/Serializer/PayloadSerializers/JingleFileTransferFileInfoSerializer.h @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2012 Tobias Markmann + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#pragma once + +#include +#include +#include + +#include + +namespace Swift { + class PayloadSerializerCollection; + + class SWIFTEN_API JingleFileTransferFileInfoSerializer : public GenericPayloadSerializer { + public: + JingleFileTransferFileInfoSerializer(); + + virtual std::string serializePayload(boost::shared_ptr) const; + }; +} diff --git a/Swiften/Serializer/PayloadSerializers/UnitTest/JingleSerializersTest.cpp b/Swiften/Serializer/PayloadSerializers/UnitTest/JingleSerializersTest.cpp index e3ec8fc..10aa7e9 100644 --- a/Swiften/Serializer/PayloadSerializers/UnitTest/JingleSerializersTest.cpp +++ b/Swiften/Serializer/PayloadSerializers/UnitTest/JingleSerializersTest.cpp @@ -181,14 +181,13 @@ class JingleSerializersTest : public CppUnit::TestFixture { void testSerialize_Xep0234_Example1() { std::string expected = "" "" - "" + "" + "1969-07-21T02:56:15Z" "This is a test. If this were a real file..." + "test.txt" "" + "1022" + "552da749930852c69ae5d2141d3766b1" "" "" ""; @@ -197,6 +196,7 @@ class JingleSerializersTest : public CppUnit::TestFixture { fileInfo.setDate(stringToDateTime("1969-07-21T02:56:15Z")); fileInfo.setHash("552da749930852c69ae5d2141d3766b1"); + fileInfo.setAlgo("sha-1"); fileInfo.setSize(1022); fileInfo.setName("test.txt"); fileInfo.setDescription("This is a test. If this were a real file..."); @@ -217,14 +217,13 @@ class JingleSerializersTest : public CppUnit::TestFixture { "" "" "" - "" + "" + "1969-07-21T02:56:15Z" "This is a test. If this were a real file..." + "test.txt" "" + "1022" + "552da749930852c69ae5d2141d3766b1" "" "" "" @@ -266,6 +265,7 @@ class JingleSerializersTest : public CppUnit::TestFixture { StreamInitiationFileInfo fileInfo; fileInfo.setName("test.txt"); fileInfo.setSize(1022); + fileInfo.setAlgo("sha-1"); fileInfo.setHash("552da749930852c69ae5d2141d3766b1"); fileInfo.setDate(stringToDateTime("1969-07-21T02:56:15Z")); fileInfo.setDescription("This is a test. If this were a real file..."); @@ -350,10 +350,9 @@ class JingleSerializersTest : public CppUnit::TestFixture { "" "" - "" + "" "" + "552da749930852c69ae5d2141d3766b1" "" "" "" @@ -393,6 +392,7 @@ class JingleSerializersTest : public CppUnit::TestFixture { StreamInitiationFileInfo fileInfo; fileInfo.setHash("552da749930852c69ae5d2141d3766b1"); + fileInfo.setAlgo("sha-1"); fileInfo.setRangeOffset(270336); JingleFileTransferDescription::ref desc = boost::make_shared(); -- cgit v0.10.2-6-g49f6