Use FormUtils in SettingsAdHocCommand
This commit is contained in:
parent
d04f7e19a7
commit
d37e8afadb
6 changed files with 146 additions and 99 deletions
|
@ -46,8 +46,6 @@ class 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;
|
||||
}
|
||||
|
@ -66,7 +64,6 @@ class AdHocCommand {
|
|||
StorageBackend *m_storageBackend;
|
||||
Swift::JID m_initiator;
|
||||
Swift::JID m_to;
|
||||
std::vector<Swift::FormField::ref> m_fields;
|
||||
std::string m_id;
|
||||
|
||||
private:
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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<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;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
*/
|
||||
|
||||
#include "transport/formutils.h"
|
||||
#include "transport/adhoccommand.h"
|
||||
|
||||
#if HAVE_SWIFTEN_3
|
||||
#include <Swiften/Elements/Form.h>
|
||||
|
@ -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>(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>(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>(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>(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>(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>(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) {
|
||||
|
|
|
@ -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>(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() {
|
||||
|
@ -69,11 +44,7 @@ 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;
|
||||
}
|
||||
|
@ -82,11 +53,7 @@ boost::shared_ptr<Swift::Command> SettingsAdHocCommand::getForm() {
|
|||
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;
|
||||
}
|
||||
|
@ -94,62 +61,42 @@ boost::shared_ptr<Swift::Command> SettingsAdHocCommand::getForm() {
|
|||
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";
|
||||
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
|
||||
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<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));
|
||||
|
|
Loading…
Add table
Reference in a new issue