Changeset - d37e8afadb4b
[Not reviewed]
0 6 0
Jan Kaluza - 10 years ago 2015-11-13 07:15:27
jkaluza@redhat.com
Use FormUtils in SettingsAdHocCommand
6 files changed with 145 insertions and 98 deletions:
0 comments (0 inline, 0 general)
include/transport/adhoccommand.h
Show inline comments
 
@@ -37,43 +37,40 @@ class StorageBackend;
 
class AdHocCommand {
 
	public:
 
		/// Creates new AdHocManager.
 

	
 
		/// \param component Transport instance associated with this AdHocManager.
 
		AdHocCommand(Component *component, UserManager *userManager, StorageBackend *storageBackend, const Swift::JID &initiator, const Swift::JID &to);
 

	
 
		/// Destructor.
 
		virtual ~AdHocCommand();
 

	
 
		virtual boost::shared_ptr<Swift::Command> handleRequest(boost::shared_ptr<Swift::Command> payload) = 0;
 

	
 
		void addFormField(Swift::FormField::ref field);
 

	
 
		const std::string &getId() {
 
			return m_id;
 
		}
 

	
 
		void refreshLastActivity() {
 
			m_lastActivity = time(NULL);
 
		}
 

	
 
		time_t getLastActivity() {
 
			return m_lastActivity;
 
		}
 

	
 
	protected:
 
		Component *m_component;
 
		UserManager *m_userManager;
 
		StorageBackend *m_storageBackend;
 
		Swift::JID m_initiator;
 
		Swift::JID m_to;
 
		std::vector<Swift::FormField::ref> m_fields;
 
		std::string m_id;
 

	
 
	private:
 
		// This is used to remove AdHocCommand after long inactivity to prevent memory leaks
 
		// caused by users which disconnect before they finish the command.
 
		// AdHocManager uses this to garbage collect old AdHocCommands.
 
		time_t m_lastActivity;
 
};
 

	
 
}
include/transport/formutils.h
Show inline comments
 
@@ -19,27 +19,33 @@
 
 */
 

	
 
#pragma once
 

	
 
#include "Swiften/Queries/Responder.h"
 
#include "Swiften/Elements/InBandRegistrationPayload.h"
 
#include "Swiften/Elements/RosterPayload.h"
 
#include <boost/signal.hpp>
 
#include <Swiften/Version.h>
 
#define HAVE_SWIFTEN_3  (SWIFTEN_VERSION >= 0x030000)
 

	
 
namespace Transport {
 

	
 
	class AdHocCommand;
 

	
 
namespace FormUtils {
 

	
 
	void addHiddenField(Swift::Form::ref form, const std::string &name, const std::string &value);
 
	void addTextSingleField(Swift::Form::ref form, const std::string &name, const std::string &value,
 
							const std::string &label, bool required = false);
 
	void addTextPrivateField(Swift::Form::ref form, const std::string &name, const std::string &label,
 
							 bool required = false);
 
	void addListSingleField(Swift::Form::ref form, const std::string &name, Swift::FormField::Option value,
 
							const std::string &label, const std::string &def, bool required = false);
 
	void addBooleanField(Swift::Form::ref form, const std::string &name, const std::string &value,
 
							const std::string &label, bool required = false);
 
	void addTextFixedField(Swift::Form::ref form, const std::string &value);
 

	
 
	std::string fieldValue(Swift::FormField::ref);
 

	
 
	std::string fieldValue(Swift::Form::ref, const std::string &key, const std::string &def);
 
}
 
}
 
\ No newline at end of file
include/transport/settingsadhoccommand.h
Show inline comments
 
@@ -25,37 +25,40 @@
 
#include <map>
 
#include "transport/adhoccommand.h"
 
#include "transport/adhoccommandfactory.h"
 
#include <Swiften/Version.h>
 
#define HAVE_SWIFTEN_3  (SWIFTEN_VERSION >= 0x030000)
 

	
 

	
 
namespace Transport {
 

	
 
class Component;
 
class UserManager;
 
class StorageBackend;
 
class UserInfo;
 

	
 
class SettingsAdHocCommand : public AdHocCommand {
 
	public:
 
		typedef enum { Init, WaitingForResponse } State;
 

	
 
		SettingsAdHocCommand(Component *component, UserManager *userManager, StorageBackend *storageBackend, const Swift::JID &initiator, const Swift::JID &to);
 

	
 
		/// Destructor.
 
		virtual ~SettingsAdHocCommand();
 

	
 
		virtual boost::shared_ptr<Swift::Command> handleRequest(boost::shared_ptr<Swift::Command> payload);
 

	
 
	private:
 
		void updateUserSetting(Swift::Form::ref form, UserInfo &user, const std::string &name);
 

	
 
		boost::shared_ptr<Swift::Command> getForm();
 
		boost::shared_ptr<Swift::Command> handleResponse(boost::shared_ptr<Swift::Command> payload);
 
		State m_state;
 
};
 

	
 
class SettingsAdHocCommandFactory : public AdHocCommandFactory {
 
	public:
 
		SettingsAdHocCommandFactory() {
 
			m_userSettings["send_headlines"] = "0";
 
			m_userSettings["stay_connected"] = "0";
 
		}
 

	
src/adhoccommand.cpp
Show inline comments
 
@@ -39,17 +39,13 @@ AdHocCommand::AdHocCommand(Component *component, UserManager *userManager, Stora
 
	m_initiator = initiator;
 
	m_to = to;
 

	
 
	std::string bucket = "abcdefghijklmnopqrstuvwxyz";
 
	for (int i = 0; i < 32; i++) {
 
		m_id += bucket[rand() % bucket.size()];
 
	}
 
}
 

	
 
AdHocCommand::~AdHocCommand() {
 
}
 

	
 
void AdHocCommand::addFormField(Swift::FormField::ref field) {
 
	m_fields.push_back(field);
 
}
 

	
 
}
src/formutils.cpp
Show inline comments
 
@@ -10,99 +10,197 @@
 
 *
 
 * This program is distributed in the hope that it will be useful,
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 * GNU General Public License for more details.
 
 *
 
 * You should have received a copy of the GNU General Public License
 
 * along with this program; if not, write to the Free Software
 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 
 */
 

	
 
#include "transport/formutils.h"
 
#include "transport/adhoccommand.h"
 

	
 
#if HAVE_SWIFTEN_3
 
#include <Swiften/Elements/Form.h>
 
#endif
 

	
 
using namespace Swift;
 

	
 
namespace Transport {
 
namespace FormUtils {
 

	
 
void addHiddenField(Form::ref form, const std::string &name, const std::string &value) {
 
static 
 
#if HAVE_SWIFTEN_3
 
FormField::ref
 
#else
 
HiddenFormField::ref
 
#endif
 
createHiddenField(const std::string &name, const std::string &value) {
 
#if HAVE_SWIFTEN_3
 
	FormField::ref field = boost::make_shared<FormField>(FormField::HiddenType, value);	
 
#else
 
	HiddenFormField::ref field = HiddenFormField::create();
 
	field->setValue(value);
 
#endif
 
	field->setName(name);
 
	form->addField(field);
 
	return field;
 
}
 

	
 
void addTextSingleField(Swift::Form::ref form, const std::string &name, const std::string &value, const std::string &label, bool required) {
 
static 
 
#if HAVE_SWIFTEN_3
 
FormField::ref
 
#else
 
TextSingleFormField::ref
 
#endif
 
createTextSingleField(const std::string &name, const std::string &value, const std::string &label, bool required) {
 
#if HAVE_SWIFTEN_3
 
	FormField::ref field = boost::make_shared<FormField>(FormField::TextSingleType, value);
 
#else
 
	TextSingleFormField::ref field = TextSingleFormField::create();
 
	field->setValue(value);
 
#endif
 
	field->setName(name);
 
	field->setLabel(label);
 
	field->setRequired(required);
 
	form->addField(field);
 
	return field;
 
}
 

	
 
void addTextPrivateField(Swift::Form::ref form, const std::string &name, const std::string &label, bool required) {
 
static 
 
#if HAVE_SWIFTEN_3
 
FormField::ref
 
#else
 
TextPrivateFormField::ref
 
#endif
 
createTextPrivateField(const std::string &name, const std::string &label, bool required) {
 
#if HAVE_SWIFTEN_3
 
	FormField::ref field = boost::make_shared<FormField>(FormField::TextPrivateType);
 
#else
 
	TextPrivateFormField::ref field = TextPrivateFormField::create();
 
#endif
 
	field->setName(name);
 
	field->setLabel(label);
 
	field->setRequired(required);
 
	form->addField(field);
 
	return field;
 
}
 

	
 
void addListSingleField(Swift::Form::ref form, const std::string &name, Swift::FormField::Option value, const std::string &label, const std::string &def, bool required) {
 
static 
 
#if HAVE_SWIFTEN_3
 
FormField::ref
 
#else
 
ListSingleFormField::ref
 
#endif
 
createListSingleField(const std::string &name, Swift::FormField::Option value, const std::string &label, const std::string &def, bool required) {
 
#if HAVE_SWIFTEN_3
 
	FormField::ref field = boost::make_shared<FormField>(FormField::ListSingleType);
 
#else
 
	ListSingleFormField::ref field = ListSingleFormField::create();
 
#endif
 
	field->setName(name);
 
	field->setLabel(label);
 
	field->addOption(value);
 
#if HAVE_SWIFTEN_3
 
	field->addValue(def);
 
#else
 
	field->setValue(def);
 
#endif
 
	form->addField(field);
 
	return field;
 
}
 

	
 

	
 
void addBooleanField(Swift::Form::ref form, const std::string &name, const std::string &value, const std::string &label, bool required) {
 
static 
 
#if HAVE_SWIFTEN_3
 
FormField::ref
 
#else
 
BooleanFormField::ref
 
#endif
 
createBooleanField(const std::string &name, const std::string &value, const std::string &label, bool required) {
 
#if HAVE_SWIFTEN_3
 
	FormField::ref field = boost::make_shared<FormField>(FormField::BooleanType, value);
 
#else
 
	BooleanFormField::ref field = BooleanFormField::create();
 
	field->setValue(value == "0");
 
#endif
 
	field->setName(name);
 
	field->setLabel(label);
 
	field->setRequired(required);
 
	form->addField(field);
 
	return field;
 
}
 

	
 
static 
 
#if HAVE_SWIFTEN_3
 
FormField::ref
 
#else
 
FixedFormField::ref
 
#endif
 
createTextFixedField(const std::string &value) {
 
#if HAVE_SWIFTEN_3
 
	FormField::ref field = boost::make_shared<FormField>(FormField::FixedType, value);
 
#else
 
	FixedFormField::ref field = FixedFormField::create(value)
 
#endif
 
	return field;
 
}
 

	
 
void addHiddenField(Form::ref form, const std::string &name, const std::string &value) {
 
	form->addField(createHiddenField(name, value));
 
}
 

	
 
void addTextSingleField(Swift::Form::ref form, const std::string &name, const std::string &value, const std::string &label, bool required) {
 
	form->addField(createTextSingleField(name, value, label, required));
 
}
 

	
 
void addTextPrivateField(Swift::Form::ref form, const std::string &name, const std::string &label, bool required) {
 
	form->addField(createTextPrivateField(name, label, required));
 
}
 

	
 
void addListSingleField(Swift::Form::ref form, const std::string &name, Swift::FormField::Option value, const std::string &label, const std::string &def, bool required) {
 
	form->addField(createListSingleField(name, value, label, def, required));
 
}
 

	
 
void addBooleanField(Swift::Form::ref form, const std::string &name, const std::string &value, const std::string &label, bool required) {
 
	form->addField(createBooleanField(name, value, label, required));
 
}
 

	
 
void addTextFixedField(Swift::Form::ref form, const std::string &value) {
 
	form->addField(createTextFixedField(value));
 
}
 

	
 

	
 
std::string fieldValue(Swift::FormField::ref field) {
 
#if HAVE_SWIFTEN_3
 
	return field->getValues()[0];
 
#else
 
	TextSingleFormField::ref textSingle = boost::dynamic_pointer_cast<TextSingleFormField>(*it);
 
	if (textSingle) {
 
		return textSingle->getValue();
 
	}
 

	
 
	TextPrivateFormField::ref textPrivate = boost::dynamic_pointer_cast<TextPrivateFormField>(*it);
 
	if (textPrivate) {
 
		return textPrivate->getValue();
 
	}
 

	
 
	ListSingleFormField::ref listSingle = boost::dynamic_pointer_cast<ListSingleFormField>(*it);
 
	if (listSingle) {
 
		return listSingle->getValue();
 
	}
 

	
 
	BooleanFormField::ref boolean = boost::dynamic_pointer_cast<BooleanFormField>(*it);
 
	if (boolean) {
 
		return boolen->getValue() ? "1" : "0";
 
	}
 
	
 
	return "";
 
#endif
 
}
 

	
 
std::string fieldValue(Swift::Form::ref form, const std::string &key, const std::string &def) {
 
	const std::vector<FormField::ref> fields = form->getFields();
 
	for (std::vector<FormField::ref>::const_iterator it = fields.begin(); it != fields.end(); it++) {
 
#if HAVE_SWIFTEN_3
 
		FormField::ref field = *it;
 
		if (field->getName() == key) {
 
			return field->getValues()[0];
 
		}
 
#else
 
		TextSingleFormField::ref textSingle = boost::dynamic_pointer_cast<TextSingleFormField>(*it);
src/settingsadhoccommand.cpp
Show inline comments
 
@@ -17,148 +17,95 @@
 
 * along with this program; if not, write to the Free Software
 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 
 */
 

	
 
#include "transport/settingsadhoccommand.h"
 
#include "transport/conversation.h"
 
#include "transport/usermanager.h"
 
#include "transport/buddy.h"
 
#include "transport/factory.h"
 
#include "transport/user.h"
 
#include "transport/logging.h"
 
#include "transport/storagebackend.h"
 
#include "transport/formutils.h"
 

	
 

	
 
namespace Transport {
 

	
 
DEFINE_LOGGER(logger, "SettingsAdHocCommand");
 

	
 
SettingsAdHocCommand::SettingsAdHocCommand(Component *component, UserManager *userManager, StorageBackend *storageBackend, const Swift::JID &initiator, const Swift::JID &to) : AdHocCommand(component, userManager, storageBackend, initiator, to) {
 
	m_state = Init;
 
#if HAVE_SWIFTEN_3
 
	Swift::FormField::ref field = boost::make_shared<Swift::FormField>(Swift::FormField::BooleanType, "1");
 
#else
 
	Swift::BooleanFormField::ref field;
 

	
 
	field = Swift::BooleanFormField::create(true);
 
#endif
 
	field->setName("enable_transport");
 
	field->setLabel("Enable transport");
 
	addFormField(field);
 
#if HAVE_SWIFTEN_3
 
	field = boost::make_shared<Swift::FormField>(Swift::FormField::BooleanType, CONFIG_STRING_DEFAULTED(component->getConfig(), "settings.send_headlines", "0"));
 
#else
 
	field = Swift::BooleanFormField::create(CONFIG_STRING_DEFAULTED(component->getConfig(), "settings.send_headlines", "0") == "1");
 
#endif
 
	field->setName("send_headlines");
 
	field->setLabel("Allow sending messages as headlines");
 
	addFormField(field);
 
#if HAVE_SWIFTEN_3
 
	field = boost::make_shared<Swift::FormField>(Swift::FormField::BooleanType, CONFIG_STRING_DEFAULTED(component->getConfig(), "settings.stay_connected", "0"));
 
#else
 
	field = Swift::BooleanFormField::create(CONFIG_STRING_DEFAULTED(component->getConfig(), "settings.stay_connected", "0") == "1");
 
#endif
 
	field->setName("stay_connected");
 
	field->setLabel("Stay connected to legacy network when offline on XMPP");
 
	addFormField(field);
 
}
 

	
 
SettingsAdHocCommand::~SettingsAdHocCommand() {
 
}
 

	
 
boost::shared_ptr<Swift::Command> SettingsAdHocCommand::getForm() {
 
	if (!m_storageBackend) {
 
		boost::shared_ptr<Swift::Command> response(new Swift::Command("settings", m_id, Swift::Command::Completed));
 
		boost::shared_ptr<Swift::Form> form(new Swift::Form());
 
#if HAVE_SWIFTEN_3
 
		form->addField(boost::make_shared<Swift::FormField>(Swift::FormField::FixedType, "This server does not support transport settings. There is no storage backend configured"));
 
#else
 
		form->addField(Swift::FixedFormField::create("This server does not support transport settings. There is no storage backend configured"));
 
#endif
 
		FormUtils::addTextFixedField(form, "This server does not support transport settings. There is no storage backend configured");
 
		response->setForm(form);
 
		return response;
 
	}
 

	
 
	UserInfo user;
 
	if (m_storageBackend->getUser(m_initiator.toBare().toString(), user) == false) {
 
		boost::shared_ptr<Swift::Command> response(new Swift::Command("settings", m_id, Swift::Command::Completed));
 
		boost::shared_ptr<Swift::Form> form(new Swift::Form());
 
#if HAVE_SWIFTEN_3
 
		form->addField(boost::make_shared<Swift::FormField>(Swift::FormField::FixedType, "You are not registered."));
 
#else
 
		form->addField(Swift::FixedFormField::create("You are not registered."));
 
#endif
 
		FormUtils::addTextFixedField(form, "You are not registered.");
 
		response->setForm(form);
 
		return response;
 
	}
 

	
 
	boost::shared_ptr<Swift::Command> response(new Swift::Command("settings", m_id, Swift::Command::Executing));
 
	boost::shared_ptr<Swift::Form> form(new Swift::Form());
 

	
 
	BOOST_FOREACH(Swift::FormField::ref field, m_fields) {
 
		// FIXME: Support for more types than boolean
 
#if HAVE_SWIFTEN_3
 
		if (field->getType() == Swift::FormField::BooleanType) {
 
			std::string value = field->getBoolValue() ? "1" : "0";
 
	std::string value;
 
	int type = (int) TYPE_BOOLEAN;
 
			m_storageBackend->getUserSetting(user.id, field->getName(), type, value);
 
			field->setBoolValue(value == "1");
 
		}
 
#else
 
		if (boost::dynamic_pointer_cast<Swift::BooleanFormField>(field)) {
 
			Swift::BooleanFormField::ref f(boost::dynamic_pointer_cast<Swift::BooleanFormField>(field));
 
			std::string value = f->getValue() ? "1" : "0";
 
			int type = (int)TYPE_BOOLEAN;
 
			m_storageBackend->getUserSetting(user.id, f->getName(), type, value);
 
			f->setValue(value == "1");
 
		}
 
#endif			
 

	
 
		form->addField(field);
 
	}
 
	value = "1";
 
	m_storageBackend->getUserSetting(user.id, "enable_transport", type, value);
 
	FormUtils::addBooleanField(form, "enable_transport", value, "Enable transport");
 

	
 
	value = CONFIG_STRING_DEFAULTED(m_component->getConfig(), "settings.send_headlines", "0");
 
	m_storageBackend->getUserSetting(user.id, "send_headlines", type, value);
 
	FormUtils::addBooleanField(form, "send_headlines", value, "Allow sending messages as headlines");
 

	
 
	value = CONFIG_STRING_DEFAULTED(m_component->getConfig(), "settings.stay_connected", "0");
 
	m_storageBackend->getUserSetting(user.id, "stay_connected", type, value);
 
	FormUtils::addBooleanField(form, "stay_connected", value, "Stay connected to legacy network when offline on XMPP");
 

	
 
	response->setForm(form);
 
	return response;
 
}
 

	
 
void SettingsAdHocCommand::updateUserSetting(Swift::Form::ref form, UserInfo &user, const std::string &name) {
 
	std::string value = FormUtils::fieldValue(form, name, "");
 
	if (value.empty()) {
 
		return;
 
	}
 

	
 
	m_storageBackend->updateUserSetting(user.id, name, value);
 
}
 

	
 
boost::shared_ptr<Swift::Command> SettingsAdHocCommand::handleResponse(boost::shared_ptr<Swift::Command> payload) {
 
	UserInfo user;
 
	bool registered = m_storageBackend->getUser(m_initiator.toBare().toString(), user);
 

	
 
	if (registered && payload->getForm()) {
 
		BOOST_FOREACH(Swift::FormField::ref field, m_fields) {
 
			Swift::FormField::ref received = payload->getForm()->getField(field->getName());
 
			if (!received) {
 
				continue;
 
			}
 
#if HAVE_SWIFTEN_3
 
			if (received->getType() == Swift::FormField::BooleanType) {
 
				std::string value = received->getBoolValue() ? "1" : "0";
 
				m_storageBackend->updateUserSetting(user.id, received->getName(), value);
 
			} else if (received->getType() == Swift::FormField::TextSingleType) {
 
				m_storageBackend->updateUserSetting(user.id, received->getName(), received->getTextSingleValue());
 
			}
 
#else
 
			// FIXME: Support for more types than boolean
 
			if (boost::dynamic_pointer_cast<Swift::BooleanFormField>(received)) {
 
				Swift::BooleanFormField::ref f(boost::dynamic_pointer_cast<Swift::BooleanFormField>(received));
 
				std::string value = f->getValue() ? "1" : "0";
 
				m_storageBackend->updateUserSetting(user.id, f->getName(), value);
 
			}
 
			else if (boost::dynamic_pointer_cast<Swift::TextSingleFormField>(received)) {
 
				Swift::TextSingleFormField::ref f(boost::dynamic_pointer_cast<Swift::TextSingleFormField>(received));
 
				m_storageBackend->updateUserSetting(user.id, f->getName(), f->getValue());
 
			}
 
#endif
 
		}
 
		updateUserSetting(payload->getForm(), user, "enable_transport");
 
		updateUserSetting(payload->getForm(), user, "send_headlines");
 
		updateUserSetting(payload->getForm(), user, "stay_connected");
 
	}
 

	
 
	boost::shared_ptr<Swift::Command> response(new Swift::Command("settings", m_id, Swift::Command::Completed));
 
	return response;
 
}
 

	
 
boost::shared_ptr<Swift::Command> SettingsAdHocCommand::handleRequest(boost::shared_ptr<Swift::Command> payload) {
 
	boost::shared_ptr<Swift::Command> response;
 
	if (payload->getAction() == Swift::Command::Cancel) {
 
		response = boost::shared_ptr<Swift::Command>(new Swift::Command("settings", m_id, Swift::Command::Canceled));
 
		return response;
 
	}
0 comments (0 inline, 0 general)