From fe460ca46bff4f9e735db86ca072eafb3a5c8f2d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Wed, 22 Jan 2014 21:20:55 +0100
Subject: Sluift: Add SecurityLabel convertor

Change-Id: I59357bda2cbfcb044b5c27b41fe0c91c194e7381

diff --git a/Sluift/ElementConvertors/ElementConvertors.ipp b/Sluift/ElementConvertors/ElementConvertors.ipp
index da25eb6..a30967a 100644
--- a/Sluift/ElementConvertors/ElementConvertors.ipp
+++ b/Sluift/ElementConvertors/ElementConvertors.ipp
@@ -40,6 +40,7 @@
 #include <Sluift/ElementConvertors/PubSubSubscriptionsConvertor.h>
 #include <Sluift/ElementConvertors/PubSubEventAssociateConvertor.h>
 #include <Sluift/ElementConvertors/PubSubSubscriptionConvertor.h>
+#include <Sluift/ElementConvertors/SecurityLabelConvertor.h>
 #include <Sluift/ElementConvertors/PubSubEventConfigurationConvertor.h>
 
 void LuaElementConvertors::registerConvertors() {
@@ -79,5 +80,6 @@ void LuaElementConvertors::registerConvertors() {
 	convertors.push_back(boost::make_shared<PubSubSubscriptionsConvertor>(this));
 	convertors.push_back(boost::make_shared<PubSubEventAssociateConvertor>(this));
 	convertors.push_back(boost::make_shared<PubSubSubscriptionConvertor>(this));
+	convertors.push_back(boost::make_shared<SecurityLabelConvertor>(this));
 	convertors.push_back(boost::make_shared<PubSubEventConfigurationConvertor>(this));
 }
diff --git a/Sluift/ElementConvertors/SConscript b/Sluift/ElementConvertors/SConscript
index 921e325..a2de5a0 100644
--- a/Sluift/ElementConvertors/SConscript
+++ b/Sluift/ElementConvertors/SConscript
@@ -38,6 +38,7 @@ convertors = [
 	env.File("PubSubSubscriptionsConvertor.cpp"),
 	env.File("PubSubEventAssociateConvertor.cpp"),
 	env.File("PubSubSubscriptionConvertor.cpp"),
+	env.File("SecurityLabelConvertor.cpp"),
 	env.File("PubSubEventConfigurationConvertor.cpp")
 ]
 Return('convertors')
diff --git a/Sluift/ElementConvertors/SecurityLabelConvertor.cpp b/Sluift/ElementConvertors/SecurityLabelConvertor.cpp
new file mode 100644
index 0000000..0bebac1
--- /dev/null
+++ b/Sluift/ElementConvertors/SecurityLabelConvertor.cpp
@@ -0,0 +1,101 @@
+/*
+ * Copyright (c) 2010-2014 Remko Tronçon
+ * Licensed under the GNU General Public License.
+ * See the COPYING file for more information.
+ */
+
+#include <Sluift/ElementConvertors/SecurityLabelConvertor.h>
+
+#include <lua.hpp>
+#include <boost/smart_ptr/make_shared.hpp>
+#include <boost/numeric/conversion/cast.hpp>
+
+#include <Swiften/Base/foreach.h>
+
+#pragma clang diagnostic ignored "-Wunused-private-field"
+
+using namespace Swift;
+
+SecurityLabelConvertor::SecurityLabelConvertor(LuaElementConvertors* convertors) : 
+		GenericLuaElementConvertor<SecurityLabel>("security_label"),
+		convertors(convertors) {
+}
+
+SecurityLabelConvertor::~SecurityLabelConvertor() {
+}
+
+boost::shared_ptr<SecurityLabel> SecurityLabelConvertor::doConvertFromLua(lua_State* L) {
+	boost::shared_ptr<SecurityLabel> result = boost::make_shared<SecurityLabel>();
+	lua_getfield(L, -1, "equivalent_labels");
+	if (lua_type(L, -1) == LUA_TTABLE) {
+		std::vector< std::string > items;
+		for(size_t i = 0; i < lua_objlen(L, -1); ++i) {
+			lua_pushnumber(L, i + 1);
+			lua_gettable(L, -2);
+			if (lua_isstring(L, -1)) {
+				items.push_back(std::string(lua_tostring(L, -1)));
+			}
+			lua_pop(L, 1);
+		}
+
+		result->setEquivalentLabels(items);
+	}
+	lua_pop(L, 1);
+	lua_getfield(L, -1, "foreground_color");
+	if (lua_isstring(L, -1)) {
+		result->setForegroundColor(std::string(lua_tostring(L, -1)));
+	}
+	lua_pop(L, 1);
+	lua_getfield(L, -1, "display_marking");
+	if (lua_isstring(L, -1)) {
+		result->setDisplayMarking(std::string(lua_tostring(L, -1)));
+	}
+	lua_pop(L, 1);
+	lua_getfield(L, -1, "background_color");
+	if (lua_isstring(L, -1)) {
+		result->setBackgroundColor(std::string(lua_tostring(L, -1)));
+	}
+	lua_pop(L, 1);
+	lua_getfield(L, -1, "label");
+	if (lua_isstring(L, -1)) {
+		result->setLabel(std::string(lua_tostring(L, -1)));
+	}
+	lua_pop(L, 1);
+	return result;
+}
+
+void SecurityLabelConvertor::doConvertToLua(lua_State* L, boost::shared_ptr<SecurityLabel> payload) {
+	lua_createtable(L, 0, 0);
+	if (!payload->getEquivalentLabels().empty()) {
+		lua_createtable(L, boost::numeric_cast<int>(payload->getEquivalentLabels().size()), 0);
+		{
+			int i = 0;
+			foreach(const std::string& item, payload->getEquivalentLabels()) {
+				lua_pushstring(L, item.c_str());
+				lua_rawseti(L, -2, boost::numeric_cast<int>(i+1));
+				++i;
+			}
+		}
+		lua_setfield(L, -2, "equivalent_labels");
+	}
+	lua_pushstring(L, payload->getForegroundColor().c_str());
+	lua_setfield(L, -2, "foreground_color");
+	lua_pushstring(L, payload->getDisplayMarking().c_str());
+	lua_setfield(L, -2, "display_marking");
+	lua_pushstring(L, payload->getBackgroundColor().c_str());
+	lua_setfield(L, -2, "background_color");
+	lua_pushstring(L, payload->getLabel().c_str());
+	lua_setfield(L, -2, "label");
+}
+
+boost::optional<LuaElementConvertor::Documentation> SecurityLabelConvertor::getDocumentation() const {
+	return Documentation(
+		"SecurityLabel",
+		"This table has the following fields:\n\n"
+		"- `equivalent_labels`: array<string>\n"
+		"- `foreground_color`: string\n"
+		"- `display_marking`: string\n"
+		"- `background_color`: string\n"
+		"- `label`: string\n"
+	);
+}
diff --git a/Sluift/ElementConvertors/SecurityLabelConvertor.h b/Sluift/ElementConvertors/SecurityLabelConvertor.h
new file mode 100644
index 0000000..ac95429
--- /dev/null
+++ b/Sluift/ElementConvertors/SecurityLabelConvertor.h
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2010-2014 Remko Tronçon
+ * Licensed under the GNU General Public License.
+ * See the COPYING file for more information.
+ */
+
+#pragma once
+
+#include <Swiften/Base/Override.h>
+
+#include <Sluift/GenericLuaElementConvertor.h>
+#include <Swiften/Elements/SecurityLabel.h>
+
+namespace Swift {
+	class LuaElementConvertors;
+
+	class SecurityLabelConvertor : public GenericLuaElementConvertor<SecurityLabel> {
+		public:
+			SecurityLabelConvertor(LuaElementConvertors* convertors);
+			virtual ~SecurityLabelConvertor();
+
+			virtual boost::shared_ptr<SecurityLabel> doConvertFromLua(lua_State*) SWIFTEN_OVERRIDE;
+			virtual void doConvertToLua(lua_State*, boost::shared_ptr<SecurityLabel>) SWIFTEN_OVERRIDE;
+			virtual boost::optional<Documentation> getDocumentation() const SWIFTEN_OVERRIDE;
+
+		private:
+			LuaElementConvertors* convertors;
+	};
+}
diff --git a/Swift/Controllers/Chat/UnitTest/MUCControllerTest.cpp b/Swift/Controllers/Chat/UnitTest/MUCControllerTest.cpp
index 3652e86..7268878 100644
--- a/Swift/Controllers/Chat/UnitTest/MUCControllerTest.cpp
+++ b/Swift/Controllers/Chat/UnitTest/MUCControllerTest.cpp
@@ -210,7 +210,7 @@ public:
 	}
 
 	void testMessageWithLabelItem() {
-		SecurityLabel::ref label = boost::make_shared<SecurityLabel>();
+		boost::shared_ptr<SecurityLabel> label = boost::make_shared<SecurityLabel>();
 		label->setLabel("a");
 		SecurityLabelsCatalog::Item labelItem;
 		labelItem.setSelector("Bob");
@@ -237,12 +237,12 @@ public:
 	}
 
 	void testCorrectMessageWithLabelItem() {
-		SecurityLabel::ref label = boost::make_shared<SecurityLabel>();
+		boost::shared_ptr<SecurityLabel> label = boost::make_shared<SecurityLabel>();
 		label->setLabel("a");
 		SecurityLabelsCatalog::Item labelItem;
 		labelItem.setSelector("Bob");
 		labelItem.setLabel(label);
-		SecurityLabel::ref label2 = boost::make_shared<SecurityLabel>();
+		boost::shared_ptr<SecurityLabel> label2 = boost::make_shared<SecurityLabel>();
 		label->setLabel("b");
 		SecurityLabelsCatalog::Item labelItem2;
 		labelItem2.setSelector("Charlie");
diff --git a/Swift/QtUI/QtChatWindow.h b/Swift/QtUI/QtChatWindow.h
index df1e619..eeb8093 100644
--- a/Swift/QtUI/QtChatWindow.h
+++ b/Swift/QtUI/QtChatWindow.h
@@ -54,7 +54,7 @@ namespace Swift {
 				if (!index.isValid()) {
 					return QVariant();
 				}
-				SecurityLabel::ref label = availableLabels_[index.row()].getLabel();
+				boost::shared_ptr<SecurityLabel> label = availableLabels_[index.row()].getLabel();
 				if (label && role == Qt::TextColorRole) {
 					return P2QSTRING(label->getForegroundColor());
 				}
diff --git a/Swiften/Elements/SecurityLabel.cpp b/Swiften/Elements/SecurityLabel.cpp
new file mode 100644
index 0000000..2e315c5
--- /dev/null
+++ b/Swiften/Elements/SecurityLabel.cpp
@@ -0,0 +1,15 @@
+/*
+ * Copyright (c) 2010-2014 Remko Tronçon
+ * Licensed under the GNU General Public License.
+ * See the COPYING file for more information.
+ */
+
+#include <Swiften/Elements/SecurityLabel.h>
+
+using namespace Swift;
+
+SecurityLabel::SecurityLabel() {
+}
+
+SecurityLabel::~SecurityLabel() {
+}
diff --git a/Swiften/Elements/SecurityLabel.h b/Swiften/Elements/SecurityLabel.h
index a1714c8..24f891d 100644
--- a/Swiften/Elements/SecurityLabel.h
+++ b/Swiften/Elements/SecurityLabel.h
@@ -1,61 +1,77 @@
 /*
- * Copyright (c) 2010 Remko Tronçon
- * Licensed under the GNU General Public License v3.
- * See Documentation/Licenses/GPLv3.txt for more information.
+ * Copyright (c) 2010-2014 Remko Tronçon
+ * Licensed under the GNU General Public License.
+ * See the COPYING file for more information.
  */
 
 #pragma once
 
+#include <Swiften/Base/Override.h>
+#include <Swiften/Base/API.h>
+#include <Swiften/Elements/Payload.h>
 #include <vector>
-
 #include <string>
-#include <Swiften/Elements/Payload.h>
+
+
 
 namespace Swift {
-	class SecurityLabel : public Payload {
+	class SWIFTEN_API SecurityLabel : public Payload {
 		public:
-			typedef boost::shared_ptr<SecurityLabel> ref;
-			SecurityLabel() {}
+			
+			SecurityLabel();
+			
+			virtual ~SecurityLabel();
 
-			const std::string& getDisplayMarking() const { return displayMarking_; }
+			const std::vector< std::string >& getEquivalentLabels() const {
+				return equivalentLabels;
+			}
 
-			void setDisplayMarking(const std::string& displayMarking) { 
-				displayMarking_ = displayMarking;
+			void setEquivalentLabels(const std::vector< std::string >& value) {
+				this->equivalentLabels = value ;
 			}
 
-			const std::string& getForegroundColor() const { 
-				return foregroundColor_; 
+			void addEquivalentLabel(const std::string& value) {
+				this->equivalentLabels.push_back(value);
 			}
 
-			void setForegroundColor(const std::string& foregroundColor) { 
-				foregroundColor_ = foregroundColor;
+			const std::string& getForegroundColor() const {
+				return foregroundColor;
 			}
 
-			const std::string& getBackgroundColor() const { 
-				return backgroundColor_; 
+			void setForegroundColor(const std::string& value) {
+				this->foregroundColor = value ;
 			}
 
-			void setBackgroundColor(const std::string& backgroundColor) { 
-				backgroundColor_ = backgroundColor;
+			const std::string& getDisplayMarking() const {
+				return displayMarking;
 			}
 
-			const std::string& getLabel() const { return label_; }
+			void setDisplayMarking(const std::string& value) {
+				this->displayMarking = value ;
+			}
 
-			void setLabel(const std::string& label) {
-				label_ = label;
+			const std::string& getBackgroundColor() const {
+				return backgroundColor;
 			}
 
-			const std::vector<std::string>& getEquivalentLabels() const { return equivalentLabels_; }
+			void setBackgroundColor(const std::string& value) {
+				this->backgroundColor = value ;
+			}
 
-			void addEquivalentLabel(const std::string& label) {
-				equivalentLabels_.push_back(label);
+			const std::string& getLabel() const {
+				return label;
 			}
 
+			void setLabel(const std::string& value) {
+				this->label = value ;
+			}
+
+
 		private:
-			std::string displayMarking_;
-			std::string foregroundColor_;
-			std::string backgroundColor_;
-			std::string label_;
-			std::vector<std::string> equivalentLabels_;
+			std::vector< std::string > equivalentLabels;
+			std::string foregroundColor;
+			std::string displayMarking;
+			std::string backgroundColor;
+			std::string label;
 	};
 }
diff --git a/Swiften/Elements/SecurityLabelsCatalog.h b/Swiften/Elements/SecurityLabelsCatalog.h
index 420cf6f..000240e 100644
--- a/Swiften/Elements/SecurityLabelsCatalog.h
+++ b/Swiften/Elements/SecurityLabelsCatalog.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010 Remko Tronçon
+ * Copyright (c) 2010-2014 Remko Tronçon
  * Licensed under the GNU General Public License v3.
  * See Documentation/Licenses/GPLv3.txt for more information.
  */
@@ -22,11 +22,11 @@ namespace Swift {
 			class Item {
 				public:
 					Item() : default_(false) {}
-					SecurityLabel::ref getLabel() const {
+					boost::shared_ptr<SecurityLabel> getLabel() const {
 						return label_;
 					}
 
-					void setLabel(SecurityLabel::ref label) {
+					void setLabel(boost::shared_ptr<SecurityLabel> label) {
 						label_ = label;
 					}
 
@@ -42,7 +42,7 @@ namespace Swift {
 						default_ = isDefault;
 					}
 				private:
-					SecurityLabel::ref label_;
+					boost::shared_ptr<SecurityLabel> label_;
 					std::string selector_;
 					bool default_;
 			};
diff --git a/Swiften/SConscript b/Swiften/SConscript
index b5e13a9..80db7da 100644
--- a/Swiften/SConscript
+++ b/Swiften/SConscript
@@ -130,6 +130,7 @@ if env["SCONS_STAGE"] == "build" :
 			"Elements/PubSubEventPayload.cpp",
 			"Elements/RosterItemExchangePayload.cpp",
 			"Elements/RosterPayload.cpp",
+			"Elements/SecurityLabel.cpp",
 			"Elements/Stanza.cpp",
 			"Elements/StanzaAck.cpp",
 			"Elements/StatusShow.cpp",
-- 
cgit v0.10.2-6-g49f6