From e3ff5f241bf6e41829d36502e75f585d9620737e Mon Sep 17 00:00:00 2001
From: Roger Planas <roger.planas@isode.com>
Date: Thu, 3 Jul 2014 14:09:06 +0100
Subject: Sluift: Add 'show' parameter option to send_presence methods

Change-Id: I9fbd4514f6bc0d46738154abce495519e43ad46b

diff --git a/Sluift/ElementConvertors/StatusShowConvertor.cpp b/Sluift/ElementConvertors/StatusShowConvertor.cpp
index c8a5bd6..d8e24ab 100644
--- a/Sluift/ElementConvertors/StatusShowConvertor.cpp
+++ b/Sluift/ElementConvertors/StatusShowConvertor.cpp
@@ -10,6 +10,7 @@
 #include <boost/smart_ptr/make_shared.hpp>
 #include <boost/numeric/conversion/cast.hpp>
 #include <Sluift/Lua/Check.h>
+#include <Sluift/Lua/Exception.h>
 
 using namespace Swift;
 
@@ -23,21 +24,7 @@ boost::shared_ptr<StatusShow> StatusShowConvertor::doConvertFromLua(lua_State* L
 	boost::shared_ptr<StatusShow> result = boost::make_shared<StatusShow>();
 	lua_getfield(L, -1, "type");
 	if (lua_isstring(L, -1)) {
-		if (std::string(lua_tostring(L, -1)) == "online") {
-			result->setType(StatusShow::Online);
-		}
-		if (std::string(lua_tostring(L, -1)) == "ffc") {
-			result->setType(StatusShow::FFC);
-		}
-		if (std::string(lua_tostring(L, -1)) == "away") {
-			result->setType(StatusShow::Away);
-		}
-		if (std::string(lua_tostring(L, -1)) == "xa") {
-			result->setType(StatusShow::XA);
-		}
-		if (std::string(lua_tostring(L, -1)) == "dnd") {
-			result->setType(StatusShow::DND);
-		}
+		result->setType(convertStatusShowTypeFromString(lua_tostring(L, -1)));
 	}
 	lua_pop(L, 1);
 	return result;
@@ -45,15 +32,43 @@ boost::shared_ptr<StatusShow> StatusShowConvertor::doConvertFromLua(lua_State* L
 
 void StatusShowConvertor::doConvertToLua(lua_State* L, boost::shared_ptr<StatusShow> payload) {
 	lua_createtable(L, 0, 0);
-	if (payload->getType() != StatusShow::None) {
-		switch (payload->getType()) {
-			case StatusShow::Online: lua_pushstring(L, "online"); break;
-			case StatusShow::FFC: lua_pushstring(L, "ffc"); break;
-			case StatusShow::Away: lua_pushstring(L, "away"); break;
-			case StatusShow::XA: lua_pushstring(L, "xa"); break;
-			case StatusShow::DND: lua_pushstring(L, "dnd"); break;
-			case StatusShow::None: assert(false); break;
-		}
+   	const std::string show = convertStatusShowTypeToString(payload->getType());
+	if (!show.empty()) {
+		lua_pushstring(L, show.c_str());
 		lua_setfield(L, -2, "type");
 	}
 }
+
+std::string StatusShowConvertor::convertStatusShowTypeToString(const StatusShow::Type &show) {
+	switch (show) {
+		case StatusShow::Online: return "online"; break;
+		case StatusShow::FFC: return "ffc"; break;
+		case StatusShow::Away: return "away"; break;
+		case StatusShow::XA: return "xa"; break;
+		case StatusShow::DND: return "dnd"; break;
+		case StatusShow::None: return ""; break;
+	}
+	assert(false);
+	return "";
+}
+
+StatusShow::Type StatusShowConvertor::convertStatusShowTypeFromString(const std::string& show) {
+	if (show == "online") {
+		return StatusShow::Online;
+	}
+	else if (show == "ffc") {
+		return StatusShow::FFC;
+	}
+	else if (show == "away") {
+		return StatusShow::Away;
+	}
+	else if (show == "xa") {
+		return StatusShow::XA;
+	}
+	else if (show == "dnd") {
+		return StatusShow::DND;
+	}
+	else {
+		throw Lua::Exception("Illegal status show: '" + show + "'");
+	}
+}
diff --git a/Sluift/ElementConvertors/StatusShowConvertor.h b/Sluift/ElementConvertors/StatusShowConvertor.h
index 32253ec..ef8ed48 100644
--- a/Sluift/ElementConvertors/StatusShowConvertor.h
+++ b/Sluift/ElementConvertors/StatusShowConvertor.h
@@ -19,5 +19,8 @@ namespace Swift {
 
 			virtual boost::shared_ptr<StatusShow> doConvertFromLua(lua_State*) SWIFTEN_OVERRIDE;
 			virtual void doConvertToLua(lua_State*, boost::shared_ptr<StatusShow>) SWIFTEN_OVERRIDE;
+
+			static std::string convertStatusShowTypeToString(const StatusShow::Type &show);
+			static StatusShow::Type convertStatusShowTypeFromString(const std::string& show);
 	};
 }
diff --git a/Sluift/client.cpp b/Sluift/client.cpp
index 4b065ab..e8e6a1a 100644
--- a/Sluift/client.cpp
+++ b/Sluift/client.cpp
@@ -40,6 +40,7 @@
 #include <Sluift/ElementConvertors/IQConvertor.h>
 #include <Sluift/ElementConvertors/PresenceConvertor.h>
 #include <Sluift/ElementConvertors/MessageConvertor.h>
+#include <Sluift/ElementConvertors/StatusShowConvertor.h>
 
 using namespace Swift;
 namespace lambda = boost::lambda;
@@ -282,6 +283,7 @@ SLUIFT_LUA_FUNCTION_WITH_HELP(
 
 		"to  the JID to send the message to\n"
 		"status  the text of the presence\n"
+		"show  the availability of the presence (`online`, `ffc`, `away`, `xa`, `dnd`)\n"
 		"priority  the priority of the presence\n"
 		"type  the type of message to send (`available`, `error`, `probe`, `subscribe`, `subscribed`, `unavailable`, `unsubscribe`, `unsubscribed`)\n"
 		"payloads  payloads to add to the presence\n"
@@ -307,6 +309,9 @@ SLUIFT_LUA_FUNCTION_WITH_HELP(
 		if (boost::optional<std::string> value = Lua::getStringField(L, index, "type")) {
 			presence->setType(PresenceConvertor::convertPresenceTypeFromString(*value));
 		}
+		if (boost::optional<std::string> value = Lua::getStringField(L, index, "show")) {
+			presence->setShow(StatusShowConvertor::convertStatusShowTypeFromString(*value));
+		}
 		std::vector< boost::shared_ptr<Payload> > payloads = getPayloadsFromTable(L, index);
 		presence->addPayloads(payloads.begin(), payloads.end());
 	}
diff --git a/Sluift/component.cpp b/Sluift/component.cpp
index 0c400b3..6ae78ec 100644
--- a/Sluift/component.cpp
+++ b/Sluift/component.cpp
@@ -39,6 +39,7 @@
 #include <Sluift/ElementConvertors/IQConvertor.h>
 #include <Sluift/ElementConvertors/PresenceConvertor.h>
 #include <Sluift/ElementConvertors/MessageConvertor.h>
+#include <Sluift/ElementConvertors/StatusShowConvertor.h>
 
 using namespace Swift;
 namespace lambda = boost::lambda;
@@ -259,6 +260,7 @@ SLUIFT_LUA_FUNCTION_WITH_HELP(
 		"to  the JID to send the message to\n"
 		"from  the JID to send the message from\n"
 		"status  the text of the presence\n"
+		"show  the availability of the presence (`online`, `ffc`, `away`, `xa`, `dnd`)\n"
 		"priority  the priority of the presence\n"
 		"type  the type of message to send (`available`, `error`, `probe`, `subscribe`, `subscribed`, `unavailable`, `unsubscribe`, `unsubscribed`)\n"
 		"payloads  payloads to add to the presence\n"
@@ -287,6 +289,9 @@ SLUIFT_LUA_FUNCTION_WITH_HELP(
 		if (boost::optional<std::string> value = Lua::getStringField(L, index, "type")) {
 			presence->setType(PresenceConvertor::convertPresenceTypeFromString(*value));
 		}
+		if (boost::optional<std::string> value = Lua::getStringField(L, index, "show")) {
+			presence->setShow(StatusShowConvertor::convertStatusShowTypeFromString(*value));
+		}
 		std::vector< boost::shared_ptr<Payload> > payloads = getPayloadsFromTable(L, index);
 		presence->addPayloads(payloads.begin(), payloads.end());
 	}
-- 
cgit v0.10.2-6-g49f6