From d37e8afadb4b7638556aa1a5639aaa257541e9d6 Mon Sep 17 00:00:00 2001 From: Jan Kaluza Date: Fri, 13 Nov 2015 07:15:27 +0100 Subject: [PATCH] Use FormUtils in SettingsAdHocCommand --- include/transport/adhoccommand.h | 3 - include/transport/formutils.h | 6 ++ include/transport/settingsadhoccommand.h | 3 + src/adhoccommand.cpp | 4 - src/formutils.cpp | 120 ++++++++++++++++++++--- src/settingsadhoccommand.cpp | 109 ++++++-------------- 6 files changed, 146 insertions(+), 99 deletions(-) diff --git a/include/transport/adhoccommand.h b/include/transport/adhoccommand.h index df38d913..e1563bf6 100644 --- a/include/transport/adhoccommand.h +++ b/include/transport/adhoccommand.h @@ -46,8 +46,6 @@ class AdHocCommand { virtual boost::shared_ptr handleRequest(boost::shared_ptr payload) = 0; - void addFormField(Swift::FormField::ref field); - const std::string &getId() { return m_id; } @@ -66,7 +64,6 @@ class AdHocCommand { StorageBackend *m_storageBackend; Swift::JID m_initiator; Swift::JID m_to; - std::vector m_fields; std::string m_id; private: diff --git a/include/transport/formutils.h b/include/transport/formutils.h index 2af0a702..312de882 100644 --- a/include/transport/formutils.h +++ b/include/transport/formutils.h @@ -28,6 +28,9 @@ #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); @@ -39,6 +42,9 @@ namespace FormUtils { 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); } diff --git a/include/transport/settingsadhoccommand.h b/include/transport/settingsadhoccommand.h index ee0e6083..1c061269 100644 --- a/include/transport/settingsadhoccommand.h +++ b/include/transport/settingsadhoccommand.h @@ -34,6 +34,7 @@ namespace Transport { class Component; class UserManager; class StorageBackend; +class UserInfo; class SettingsAdHocCommand : public AdHocCommand { public: @@ -47,6 +48,8 @@ class SettingsAdHocCommand : public AdHocCommand { virtual boost::shared_ptr handleRequest(boost::shared_ptr payload); private: + void updateUserSetting(Swift::Form::ref form, UserInfo &user, const std::string &name); + boost::shared_ptr getForm(); boost::shared_ptr handleResponse(boost::shared_ptr payload); State m_state; diff --git a/src/adhoccommand.cpp b/src/adhoccommand.cpp index 9810693b..b02bc740 100644 --- a/src/adhoccommand.cpp +++ b/src/adhoccommand.cpp @@ -48,8 +48,4 @@ AdHocCommand::AdHocCommand(Component *component, UserManager *userManager, Stora AdHocCommand::~AdHocCommand() { } -void AdHocCommand::addFormField(Swift::FormField::ref field) { - m_fields.push_back(field); -} - } diff --git a/src/formutils.cpp b/src/formutils.cpp index adef0eef..42346a46 100644 --- a/src/formutils.cpp +++ b/src/formutils.cpp @@ -19,6 +19,7 @@ */ #include "transport/formutils.h" +#include "transport/adhoccommand.h" #if HAVE_SWIFTEN_3 #include @@ -29,7 +30,13 @@ 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::HiddenType, value); #else @@ -37,10 +44,16 @@ void addHiddenField(Form::ref form, const std::string &name, const std::string & 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::TextSingleType, value); #else @@ -50,10 +63,16 @@ void addTextSingleField(Swift::Form::ref form, const std::string &name, const st 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::TextPrivateType); #else @@ -62,10 +81,16 @@ void addTextPrivateField(Swift::Form::ref form, const std::string &name, const s 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::ListSingleType); #else @@ -79,11 +104,16 @@ void addListSingleField(Swift::Form::ref form, const std::string &name, Swift::F #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::BooleanType, value); #else @@ -93,7 +123,75 @@ void addBooleanField(Swift::Form::ref form, const std::string &name, const std:: 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::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(*it); + if (textSingle) { + return textSingle->getValue(); + } + + TextPrivateFormField::ref textPrivate = boost::dynamic_pointer_cast(*it); + if (textPrivate) { + return textPrivate->getValue(); + } + + ListSingleFormField::ref listSingle = boost::dynamic_pointer_cast(*it); + if (listSingle) { + return listSingle->getValue(); + } + + BooleanFormField::ref boolean = boost::dynamic_pointer_cast(*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) { diff --git a/src/settingsadhoccommand.cpp b/src/settingsadhoccommand.cpp index 82dcfa4d..d888a3b3 100644 --- a/src/settingsadhoccommand.cpp +++ b/src/settingsadhoccommand.cpp @@ -26,6 +26,7 @@ #include "transport/user.h" #include "transport/logging.h" #include "transport/storagebackend.h" +#include "transport/formutils.h" namespace Transport { @@ -34,32 +35,6 @@ 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::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::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::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() { @@ -69,11 +44,7 @@ boost::shared_ptr SettingsAdHocCommand::getForm() { if (!m_storageBackend) { boost::shared_ptr response(new Swift::Command("settings", m_id, Swift::Command::Completed)); boost::shared_ptr form(new Swift::Form()); -#if HAVE_SWIFTEN_3 - form->addField(boost::make_shared(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; } @@ -82,11 +53,7 @@ boost::shared_ptr SettingsAdHocCommand::getForm() { if (m_storageBackend->getUser(m_initiator.toBare().toString(), user) == false) { boost::shared_ptr response(new Swift::Command("settings", m_id, Swift::Command::Completed)); boost::shared_ptr form(new Swift::Form()); -#if HAVE_SWIFTEN_3 - form->addField(boost::make_shared(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; } @@ -94,62 +61,42 @@ boost::shared_ptr SettingsAdHocCommand::getForm() { boost::shared_ptr response(new Swift::Command("settings", m_id, Swift::Command::Executing)); boost::shared_ptr 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"; - int type = (int) TYPE_BOOLEAN; - m_storageBackend->getUserSetting(user.id, field->getName(), type, value); - field->setBoolValue(value == "1"); - } -#else - if (boost::dynamic_pointer_cast(field)) { - Swift::BooleanFormField::ref f(boost::dynamic_pointer_cast(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 + std::string value; + int type = (int) TYPE_BOOLEAN; - 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 SettingsAdHocCommand::handleResponse(boost::shared_ptr 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(received)) { - Swift::BooleanFormField::ref f(boost::dynamic_pointer_cast(received)); - std::string value = f->getValue() ? "1" : "0"; - m_storageBackend->updateUserSetting(user.id, f->getName(), value); - } - else if (boost::dynamic_pointer_cast(received)) { - Swift::TextSingleFormField::ref f(boost::dynamic_pointer_cast(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 response(new Swift::Command("settings", m_id, Swift::Command::Completed));