From cc873b3f00db4cd0a778bc2ec04f8748d70a92f9 Mon Sep 17 00:00:00 2001
From: Tarun Gupta <tarun1995gupta@gmail.com>
Date: Wed, 22 Mar 2017 14:48:20 +0530
Subject: Add Client State Indication Element, its Parser and Serializer

License:
This patch is BSD-licensed, see Documentation/Licenses/BSD-simplified.txt for details.

Test-Information:
Adds tests for Client State Parser and Serializer, which passes.
Tests performed on Ubuntu 16.04 LTS.

Change-Id: I60c63f63e1c0fdd55600ef42faa95989ca5ab75b

diff --git a/Swiften/Elements/ClientState.h b/Swiften/Elements/ClientState.h
new file mode 100644
index 0000000..868d352
--- /dev/null
+++ b/Swiften/Elements/ClientState.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2017 Tarun Gupta.
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+#pragma once
+
+#include <string>
+
+#include <Swiften/Base/API.h>
+#include <Swiften/Elements/Payload.h>
+
+namespace Swift {
+    class SWIFTEN_API ClientState : public Payload {
+        public:
+            typedef std::shared_ptr<ClientState> ref;
+
+        public:
+            enum class ClientStateType {Active, Inactive};
+            ClientState(ClientStateType state = ClientStateType::Active) : state_(state)  {
+
+            }
+
+            ClientStateType getClientState() const { return state_; }
+            void setClientState(ClientStateType state) {state_ = state;}
+
+        private:
+            ClientStateType state_;
+    };
+}
diff --git a/Swiften/Elements/DiscoInfo.cpp b/Swiften/Elements/DiscoInfo.cpp
index 51a4450..11f0623 100644
--- a/Swiften/Elements/DiscoInfo.cpp
+++ b/Swiften/Elements/DiscoInfo.cpp
@@ -11,6 +11,7 @@
 namespace Swift {
 
 const std::string DiscoInfo::ChatStatesFeature = std::string("http://jabber.org/protocol/chatstates");
+const std::string DiscoInfo::ClientStatesFeature = std::string("urn:xmpp:csi:0");
 const std::string DiscoInfo::SecurityLabelsFeature = std::string("urn:xmpp:sec-label:0");
 const std::string DiscoInfo::SecurityLabelsCatalogFeature = std::string("urn:xmpp:sec-label:catalog:2");
 const std::string DiscoInfo::JabberSearchFeature = std::string("jabber:iq:search");
diff --git a/Swiften/Elements/DiscoInfo.h b/Swiften/Elements/DiscoInfo.h
index ebc598c..c8009ee 100644
--- a/Swiften/Elements/DiscoInfo.h
+++ b/Swiften/Elements/DiscoInfo.h
@@ -22,6 +22,7 @@ namespace Swift {
             typedef std::shared_ptr<DiscoInfo> ref;
 
             static const std::string ChatStatesFeature;
+            static const std::string ClientStatesFeature;
             static const std::string SecurityLabelsFeature;
             static const std::string SecurityLabelsCatalogFeature;
             static const std::string JabberSearchFeature;
diff --git a/Swiften/Parser/PayloadParsers/ClientStateParser.cpp b/Swiften/Parser/PayloadParsers/ClientStateParser.cpp
new file mode 100644
index 0000000..8a6cc9c
--- /dev/null
+++ b/Swiften/Parser/PayloadParsers/ClientStateParser.cpp
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2017 Tarun Gupta.
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+#include <Swiften/Parser/PayloadParsers/ClientStateParser.h>
+
+namespace Swift {
+
+ClientStateParser::ClientStateParser() {
+}
+
+void ClientStateParser::handleStartElement(const std::string& element, const std::string&, const AttributeMap&) {
+    if (level_ == 0) {
+        auto state = ClientState::ClientStateType::Active;
+        if (element == "active") {
+            state = ClientState::ClientStateType::Active;
+        } else if (element == "inactive") {
+            state = ClientState::ClientStateType::Inactive;
+        }
+        getPayloadInternal()->setClientState(state);
+    }
+    ++level_;
+}
+
+void ClientStateParser::handleEndElement(const std::string&, const std::string&) {
+    --level_;
+}
+
+void ClientStateParser::handleCharacterData(const std::string&) {
+
+}
+
+}
diff --git a/Swiften/Parser/PayloadParsers/ClientStateParser.h b/Swiften/Parser/PayloadParsers/ClientStateParser.h
new file mode 100644
index 0000000..039ae37
--- /dev/null
+++ b/Swiften/Parser/PayloadParsers/ClientStateParser.h
@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2017 Tarun Gupta.
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+#pragma once
+
+#include <Swiften/Base/API.h>
+#include <Swiften/Elements/ClientState.h>
+#include <Swiften/Parser/GenericPayloadParser.h>
+
+namespace Swift {
+    class SWIFTEN_API ClientStateParser : public GenericPayloadParser<ClientState> {
+        public:
+            ClientStateParser();
+
+            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_ = 0;
+    };
+}
diff --git a/Swiften/Parser/PayloadParsers/ClientStateParserFactory.h b/Swiften/Parser/PayloadParsers/ClientStateParserFactory.h
new file mode 100644
index 0000000..95617a1
--- /dev/null
+++ b/Swiften/Parser/PayloadParsers/ClientStateParserFactory.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2017 Tarun Gupta.
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+#pragma once
+
+#include <Swiften/Base/API.h>
+#include <Swiften/Parser/PayloadParserFactory.h>
+#include <Swiften/Parser/PayloadParsers/ClientStateParser.h>
+
+namespace Swift {
+    class PayloadParserFactoryCollection;
+
+    class SWIFTEN_API ClientStateParserFactory : public PayloadParserFactory {
+        public:
+            ClientStateParserFactory() {
+            }
+
+            virtual bool canParse(const std::string& element, const std::string& ns, const AttributeMap&) const {
+                return ns == "urn:xmpp:csi:0" &&
+                    (element == "active" || element == "inactive");
+            }
+
+            virtual PayloadParser* createPayloadParser() {
+                return new ClientStateParser();
+            }
+
+    };
+}
diff --git a/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp b/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp
index b67556e..4ad943a 100644
--- a/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp
+++ b/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp
@@ -23,6 +23,7 @@
 #include <Swiften/Parser/PayloadParsers/CarbonsReceivedParser.h>
 #include <Swiften/Parser/PayloadParsers/CarbonsSentParser.h>
 #include <Swiften/Parser/PayloadParsers/ChatStateParserFactory.h>
+#include <Swiften/Parser/PayloadParsers/ClientStateParserFactory.h>
 #include <Swiften/Parser/PayloadParsers/CommandParser.h>
 #include <Swiften/Parser/PayloadParsers/DelayParser.h>
 #include <Swiften/Parser/PayloadParsers/DeliveryReceiptParserFactory.h>
@@ -127,6 +128,7 @@ FullPayloadParserFactoryCollection::FullPayloadParserFactoryCollection() {
     factories_.push_back(std::make_shared<GenericPayloadParserFactory<VCardParser> >("vCard", "vcard-temp"));
     factories_.push_back(std::make_shared<PrivateStorageParserFactory>(this));
     factories_.push_back(std::make_shared<ChatStateParserFactory>());
+    factories_.push_back(std::make_shared<ClientStateParserFactory>());
     factories_.push_back(std::make_shared<MUCUserPayloadParserFactory>(this));
     factories_.push_back(std::make_shared<MUCOwnerPayloadParserFactory>(this));
     factories_.push_back(std::make_shared<GenericPayloadParserFactory<MUCInvitationPayloadParser> >("x", "jabber:x:conference"));
diff --git a/Swiften/Parser/PayloadParsers/UnitTest/ClientStateParserTest.cpp b/Swiften/Parser/PayloadParsers/UnitTest/ClientStateParserTest.cpp
new file mode 100644
index 0000000..ca89040
--- /dev/null
+++ b/Swiften/Parser/PayloadParsers/UnitTest/ClientStateParserTest.cpp
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2017 Tarun Gupta
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+#include <gtest/gtest.h>
+
+#include <Swiften/Elements/ClientState.h>
+#include <Swiften/Parser/PayloadParsers/UnitTest/PayloadsParserTester.h>
+
+using namespace Swift;
+
+TEST(ClientStateParserTest, testParse_Active) {
+    PayloadsParserTester parser;
+    ASSERT_TRUE(parser.parse(
+                "<active xmlns='urn:xmpp:csi:0'/>"
+                ));
+
+    ClientState::ref payload = parser.getPayload<ClientState>();
+    ASSERT_EQ(ClientState::ClientStateType::Active, payload->getClientState());
+}
+
+TEST(ClientStateParserTest, testParse_Inactive) {
+    PayloadsParserTester parser;
+    ASSERT_TRUE(parser.parse(
+                "<inactive xmlns='urn:xmpp:csi:0'/>"
+                ));
+
+    ClientState::ref payload = parser.getPayload<ClientState>();
+    ASSERT_EQ(ClientState::ClientStateType::Inactive, payload->getClientState());
+}
diff --git a/Swiften/Parser/SConscript b/Swiften/Parser/SConscript
index 840cbda..1797de1 100644
--- a/Swiften/Parser/SConscript
+++ b/Swiften/Parser/SConscript
@@ -30,6 +30,7 @@ sources = [
         "PayloadParsers/CarbonsReceivedParser.cpp",
         "PayloadParsers/CarbonsSentParser.cpp",
         "PayloadParsers/ChatStateParser.cpp",
+        "PayloadParsers/ClientStateParser.cpp",
         "PayloadParsers/CapsInfoParser.cpp",
         "PayloadParsers/DiscoInfoParser.cpp",
         "PayloadParsers/DiscoItemsParser.cpp",
diff --git a/Swiften/SConscript b/Swiften/SConscript
index 1103362..2182132 100644
--- a/Swiften/SConscript
+++ b/Swiften/SConscript
@@ -202,6 +202,7 @@ if env["SCONS_STAGE"] == "build" :
             "Serializer/PayloadSerializers/CarbonsSentSerializer.cpp",
             "Serializer/PayloadSerializers/CarbonsReceivedSerializer.cpp",
             "Serializer/PayloadSerializers/ChatStateSerializer.cpp",
+            "Serializer/PayloadSerializers/ClientStateSerializer.cpp",
             "Serializer/PayloadSerializers/DiscoInfoSerializer.cpp",
             "Serializer/PayloadSerializers/DiscoItemsSerializer.cpp",
             "Serializer/PayloadSerializers/ErrorSerializer.cpp",
@@ -414,6 +415,7 @@ if env["SCONS_STAGE"] == "build" :
             File("Network/UnitTest/BOSHConnectionPoolTest.cpp"),
             File("Parser/PayloadParsers/UnitTest/BlockParserTest.cpp"),
             File("Parser/PayloadParsers/UnitTest/BodyParserTest.cpp"),
+            File("Parser/PayloadParsers/UnitTest/ClientStateParserTest.cpp"),
             File("Parser/PayloadParsers/UnitTest/DiscoInfoParserTest.cpp"),
             File("Parser/PayloadParsers/UnitTest/DiscoItemsParserTest.cpp"),
             File("Parser/PayloadParsers/UnitTest/ErrorParserTest.cpp"),
@@ -482,6 +484,7 @@ if env["SCONS_STAGE"] == "build" :
             File("Serializer/PayloadSerializers/UnitTest/CarbonsSerializerTest.cpp"),
             File("Serializer/PayloadSerializers/UnitTest/CapsInfoSerializerTest.cpp"),
             File("Serializer/PayloadSerializers/UnitTest/ChatStateSerializerTest.cpp"),
+            File("Serializer/PayloadSerializers/UnitTest/ClientStateSerializerTest.cpp"),
             File("Serializer/PayloadSerializers/UnitTest/FormSerializerTest.cpp"),
             File("Serializer/PayloadSerializers/UnitTest/DiscoInfoSerializerTest.cpp"),
             File("Serializer/PayloadSerializers/UnitTest/ErrorSerializerTest.cpp"),
diff --git a/Swiften/Serializer/PayloadSerializers/ClientStateSerializer.cpp b/Swiften/Serializer/PayloadSerializers/ClientStateSerializer.cpp
new file mode 100644
index 0000000..1485dcb
--- /dev/null
+++ b/Swiften/Serializer/PayloadSerializers/ClientStateSerializer.cpp
@@ -0,0 +1,24 @@
+/*
+ * Copyright (c) 2017 Tarun Gupta.
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+#include <Swiften/Serializer/PayloadSerializers/ClientStateSerializer.h>
+
+namespace Swift {
+
+ClientStateSerializer::ClientStateSerializer() : GenericPayloadSerializer<ClientState>() {
+}
+
+std::string ClientStateSerializer::serializePayload(std::shared_ptr<ClientState> clientState)  const {
+    std::string result("<");
+    switch (clientState->getClientState()) {
+        case ClientState::ClientStateType::Active: result += "active"; break;
+        case ClientState::ClientStateType::Inactive: result += "inactive"; break;
+    }
+    result += " xmlns=\"urn:xmpp:csi:0\"/>";
+    return result;
+}
+
+}
diff --git a/Swiften/Serializer/PayloadSerializers/ClientStateSerializer.h b/Swiften/Serializer/PayloadSerializers/ClientStateSerializer.h
new file mode 100644
index 0000000..1c4f977
--- /dev/null
+++ b/Swiften/Serializer/PayloadSerializers/ClientStateSerializer.h
@@ -0,0 +1,20 @@
+/*
+ * Copyright (c) 2017 Tarun Gupta.
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+#pragma once
+
+#include <Swiften/Base/API.h>
+#include <Swiften/Elements/ClientState.h>
+#include <Swiften/Serializer/GenericPayloadSerializer.h>
+
+namespace Swift {
+    class SWIFTEN_API ClientStateSerializer : public GenericPayloadSerializer<ClientState> {
+        public:
+            ClientStateSerializer();
+
+            virtual std::string serializePayload(std::shared_ptr<ClientState> error)  const;
+    };
+}
diff --git a/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp b/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp
index cf0b54c..bb3632b 100644
--- a/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp
+++ b/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp
@@ -20,6 +20,7 @@
 #include <Swiften/Serializer/PayloadSerializers/CarbonsReceivedSerializer.h>
 #include <Swiften/Serializer/PayloadSerializers/CarbonsSentSerializer.h>
 #include <Swiften/Serializer/PayloadSerializers/ChatStateSerializer.h>
+#include <Swiften/Serializer/PayloadSerializers/ClientStateSerializer.h>
 #include <Swiften/Serializer/PayloadSerializers/CommandSerializer.h>
 #include <Swiften/Serializer/PayloadSerializers/DelaySerializer.h>
 #include <Swiften/Serializer/PayloadSerializers/DeliveryReceiptRequestSerializer.h>
@@ -90,6 +91,7 @@ FullPayloadSerializerCollection::FullPayloadSerializerCollection() {
     serializers_.push_back(new SubjectSerializer());
     serializers_.push_back(new ThreadSerializer());
     serializers_.push_back(new ChatStateSerializer());
+    serializers_.push_back(new ClientStateSerializer());
     serializers_.push_back(new PrioritySerializer());
     serializers_.push_back(new ErrorSerializer(this));
     serializers_.push_back(new RosterSerializer());
diff --git a/Swiften/Serializer/PayloadSerializers/UnitTest/ClientStateSerializerTest.cpp b/Swiften/Serializer/PayloadSerializers/UnitTest/ClientStateSerializerTest.cpp
new file mode 100644
index 0000000..1622354
--- /dev/null
+++ b/Swiften/Serializer/PayloadSerializers/UnitTest/ClientStateSerializerTest.cpp
@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2017 Tarun Gupta
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+#include <gtest/gtest.h>
+
+#include <Swiften/Serializer/PayloadSerializers/ClientStateSerializer.h>
+
+using namespace Swift;
+
+TEST(ClientStateSerializerTest, testSerialize_ActiveState) {
+    ClientStateSerializer testling;
+    std::shared_ptr<ClientState> priority(new ClientState(ClientState::ClientStateType::Active));
+
+    ASSERT_EQ(std::string("<active xmlns=\"urn:xmpp:csi:0\"/>"), testling.serialize(priority));
+}
+
+TEST(ClientStateSerializerTest, testSerialize_InacativeState) {
+    ClientStateSerializer testling;
+    std::shared_ptr<ClientState> priority(new ClientState(ClientState::ClientStateType::Inactive));
+
+    ASSERT_EQ(std::string("<inactive xmlns=\"urn:xmpp:csi:0\"/>"), testling.serialize(priority));
+}
-- 
cgit v0.10.2-6-g49f6