From 7585bf84c6d6405cfb97ae2509d43eb77cac94de Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Sun, 13 Sep 2009 00:10:43 +0200
Subject: Added storage serializer.


diff --git a/Swiften/SConscript b/Swiften/SConscript
index 6a1dde1..ece2c0e 100644
--- a/Swiften/SConscript
+++ b/Swiften/SConscript
@@ -135,6 +135,7 @@ sources = [
 		"Serializer/PayloadSerializers/SoftwareVersionSerializer.cpp",
 		"Serializer/PayloadSerializers/VCardSerializer.cpp",
 		"Serializer/PayloadSerializers/VCardUpdateSerializer.cpp",
+		"Serializer/PayloadSerializers/StorageSerializer.cpp",
 		"Serializer/PresenceSerializer.cpp",
 		"Serializer/StanzaSerializer.cpp",
 		"Serializer/StreamFeaturesSerializer.cpp",
@@ -258,6 +259,7 @@ env.Append(UNITTEST_SOURCES = [
 		File("Serializer/PayloadSerializers/UnitTest/StatusSerializerTest.cpp"),
 		File("Serializer/PayloadSerializers/UnitTest/StatusShowSerializerTest.cpp"),
 		File("Serializer/PayloadSerializers/UnitTest/VCardUpdateSerializerTest.cpp"),
+		File("Serializer/PayloadSerializers/UnitTest/StorageSerializerTest.cpp"),
 		File("Serializer/UnitTest/StreamFeaturesSerializerTest.cpp"),
 		File("Serializer/XML/UnitTest/XMLElementTest.cpp"),
 		File("Server/UnitTest/ServerStanzaRouterTest.cpp"),
diff --git a/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp b/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp
index 6a24008..998dce8 100644
--- a/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp
+++ b/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp
@@ -18,6 +18,7 @@
 #include "Swiften/Serializer/PayloadSerializers/VCardSerializer.h"
 #include "Swiften/Serializer/PayloadSerializers/VCardUpdateSerializer.h"
 #include "Swiften/Serializer/PayloadSerializers/RawXMLPayloadSerializer.h"
+#include "Swiften/Serializer/PayloadSerializers/StorageSerializer.h"
 
 namespace Swift {
 
@@ -39,6 +40,7 @@ FullPayloadSerializerCollection::FullPayloadSerializerCollection() {
 	serializers_.push_back(new VCardSerializer());
 	serializers_.push_back(new VCardUpdateSerializer());
 	serializers_.push_back(new RawXMLPayloadSerializer());
+	serializers_.push_back(new StorageSerializer());
 	foreach(PayloadSerializer* serializer, serializers_) {
 		addSerializer(serializer);
 	}
diff --git a/Swiften/Serializer/PayloadSerializers/StorageSerializer.cpp b/Swiften/Serializer/PayloadSerializers/StorageSerializer.cpp
new file mode 100644
index 0000000..4268381
--- /dev/null
+++ b/Swiften/Serializer/PayloadSerializers/StorageSerializer.cpp
@@ -0,0 +1,36 @@
+#include "Swiften/Serializer/PayloadSerializers/StorageSerializer.h"
+
+#include <boost/shared_ptr.hpp>
+
+#include "Swiften/Base/foreach.h"
+#include "Swiften/Serializer/XML/XMLElement.h"
+#include "Swiften/Serializer/XML/XMLTextNode.h"
+
+namespace Swift {
+
+StorageSerializer::StorageSerializer() : GenericPayloadSerializer<Storage>() {
+}
+
+String StorageSerializer::serializePayload(boost::shared_ptr<Storage> storage)	const {
+	XMLElement storageElement("storage", "storage:bookmarks");
+	foreach(const Storage::Conference& conference, storage->getConferences()) {
+		boost::shared_ptr<XMLElement> conferenceElement(new XMLElement("conference"));
+		conferenceElement->setAttribute("name", conference.name);
+		conferenceElement->setAttribute("jid", conference.jid);
+		conferenceElement->setAttribute("autojoin", conference.autoJoin ? "1" : "0");
+		if (!conference.nick.isEmpty()) {
+			boost::shared_ptr<XMLElement> nickElement(new XMLElement("nick"));
+			nickElement->addNode(boost::shared_ptr<XMLTextNode>(new XMLTextNode(conference.nick)));
+			conferenceElement->addNode(nickElement);
+		}
+		if (!conference.password.isEmpty()) {
+			boost::shared_ptr<XMLElement> passwordElement(new XMLElement("password"));
+			passwordElement->addNode(boost::shared_ptr<XMLTextNode>(new XMLTextNode(conference.password)));
+			conferenceElement->addNode(passwordElement);
+		}
+		storageElement.addNode(conferenceElement);
+	}
+	return storageElement.serialize();
+}
+
+}
diff --git a/Swiften/Serializer/PayloadSerializers/StorageSerializer.h b/Swiften/Serializer/PayloadSerializers/StorageSerializer.h
new file mode 100644
index 0000000..e657376
--- /dev/null
+++ b/Swiften/Serializer/PayloadSerializers/StorageSerializer.h
@@ -0,0 +1,13 @@
+#pragma once
+
+#include "Swiften/Serializer/GenericPayloadSerializer.h"
+#include "Swiften/Elements/Storage.h"
+
+namespace Swift {
+	class StorageSerializer : public GenericPayloadSerializer<Storage> {
+		public:
+			StorageSerializer();
+
+			virtual String serializePayload(boost::shared_ptr<Storage>)  const;
+	};
+}
diff --git a/Swiften/Serializer/PayloadSerializers/UnitTest/StorageSerializerTest.cpp b/Swiften/Serializer/PayloadSerializers/UnitTest/StorageSerializerTest.cpp
new file mode 100644
index 0000000..158e7f1
--- /dev/null
+++ b/Swiften/Serializer/PayloadSerializers/UnitTest/StorageSerializerTest.cpp
@@ -0,0 +1,59 @@
+#include <cppunit/extensions/HelperMacros.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+
+#include "Swiften/Serializer/PayloadSerializers/StorageSerializer.h"
+
+using namespace Swift;
+
+class StorageSerializerTest : public CppUnit::TestFixture {
+		CPPUNIT_TEST_SUITE(StorageSerializerTest);
+		CPPUNIT_TEST(testSerialize);
+		CPPUNIT_TEST(testSerialize_NoNickOrPassword);
+		CPPUNIT_TEST_SUITE_END();
+
+	public:
+		StorageSerializerTest() {}
+
+		void testSerialize() {
+			StorageSerializer testling;
+			boost::shared_ptr<Storage> storage(new Storage());
+			Storage::Conference conference;
+			conference.name = "Council of Oberon";
+			conference.autoJoin = true;
+			conference.jid = JID("council@conference.underhill.org");
+			conference.nick = "Puck";
+			conference.password = "MyPass";
+			storage->addConference(conference);
+
+			CPPUNIT_ASSERT_EQUAL(String(
+				"<storage xmlns=\"storage:bookmarks\">"
+					"<conference "
+							"autojoin=\"1\" "
+							"jid=\"council@conference.underhill.org\" "
+							"name=\"Council of Oberon\">"
+						"<nick>Puck</nick>"
+						"<password>MyPass</password>"
+					"</conference>"
+				"</storage>"), testling.serialize(storage));
+		}
+
+		void testSerialize_NoNickOrPassword() {
+			StorageSerializer testling;
+			boost::shared_ptr<Storage> storage(new Storage());
+			Storage::Conference conference;
+			conference.name = "Council of Oberon";
+			conference.autoJoin = true;
+			conference.jid = JID("council@conference.underhill.org");
+			storage->addConference(conference);
+
+			CPPUNIT_ASSERT_EQUAL(String(
+				"<storage xmlns=\"storage:bookmarks\">"
+					"<conference "
+							"autojoin=\"1\" "
+							"jid=\"council@conference.underhill.org\" "
+							"name=\"Council of Oberon\"/>"
+				"</storage>"), testling.serialize(storage));
+		}
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION(StorageSerializerTest);
-- 
cgit v0.10.2-6-g49f6