diff options
author | Remko Tronçon <git@el-tramo.be> | 2013-08-22 21:07:51 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2013-08-23 08:02:12 (GMT) |
commit | 00284e5f4445a7bbab482196901c5927ea7d5488 (patch) | |
tree | f2ca6e0c51632a8aa5a7a884f3b312bacc3a2fb0 /Swiften/Serializer | |
parent | 54da0a4c0cceae947afbde586a97d1cc60c50ed8 (diff) | |
download | swift-00284e5f4445a7bbab482196901c5927ea7d5488.zip swift-00284e5f4445a7bbab482196901c5927ea7d5488.tar.bz2 |
FormField element refactoring.
This should make FormField easier to use.
Change-Id: Ia5eeedcdb673e2fe5e38fd23d5ab00970178bc44
Diffstat (limited to 'Swiften/Serializer')
6 files changed, 74 insertions, 105 deletions
diff --git a/Swiften/Serializer/PayloadSerializers/FormSerializer.cpp b/Swiften/Serializer/PayloadSerializers/FormSerializer.cpp index ba658e3..cf0f4ea 100644 --- a/Swiften/Serializer/PayloadSerializers/FormSerializer.cpp +++ b/Swiften/Serializer/PayloadSerializers/FormSerializer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010 Kevin Smith + * Copyright (c) 2010-2013 Kevin Smith * Licensed under the GNU General Public License v3. * See Documentation/Licenses/GPLv3.txt for more information. */ @@ -37,6 +37,10 @@ FormSerializer::FormSerializer() : GenericPayloadSerializer<Form>() { } std::string FormSerializer::serializePayload(boost::shared_ptr<Form> form) const { + if (!form) { + return ""; + } + boost::shared_ptr<XMLElement> formElement(new XMLElement("x", "jabber:x:data")); std::string type; switch (form->getType()) { @@ -92,66 +96,27 @@ boost::shared_ptr<XMLElement> FormSerializer::fieldToXML(boost::shared_ptr<FormF // Set the value and type std::string fieldType; - if (boost::dynamic_pointer_cast<BooleanFormField>(field)) { - fieldType = "boolean"; - boost::shared_ptr<XMLElement> valueElement(new XMLElement("value")); - valueElement->addNode(XMLTextNode::create(boost::dynamic_pointer_cast<BooleanFormField>(field)->getValue() ? "1" : "0")); - fieldElement->addNode(valueElement); - } - else if (boost::dynamic_pointer_cast<FixedFormField>(field)) { - fieldType = "fixed"; - serializeValueAsString<FixedFormField>(field, fieldElement); - } - else if (boost::dynamic_pointer_cast<HiddenFormField>(field)) { - fieldType = "hidden"; - serializeValueAsString<HiddenFormField>(field, fieldElement); - } - else if (boost::dynamic_pointer_cast<ListSingleFormField>(field)) { - fieldType = "list-single"; - serializeValueAsString<ListSingleFormField>(field, fieldElement); - } - else if (boost::dynamic_pointer_cast<TextPrivateFormField>(field)) { - fieldType = "text-private"; - serializeValueAsString<TextPrivateFormField>(field, fieldElement); - } - else if (boost::dynamic_pointer_cast<TextSingleFormField>(field)) { - fieldType = "text-single"; - serializeValueAsString<TextSingleFormField>(field, fieldElement); - } - else if (boost::dynamic_pointer_cast<JIDMultiFormField>(field)) { - fieldType = "jid-multi"; - std::vector<JID> jids = boost::dynamic_pointer_cast<JIDMultiFormField>(field)->getValue(); - foreach(const JID& jid, jids) { - boost::shared_ptr<XMLElement> valueElement(new XMLElement("value")); - valueElement->addNode(XMLTextNode::create(jid.toString())); - fieldElement->addNode(valueElement); - } - } - else if (boost::dynamic_pointer_cast<JIDSingleFormField>(field)) { - fieldType = "jid-single"; - boost::shared_ptr<XMLElement> valueElement(new XMLElement("value")); - valueElement->addNode(XMLTextNode::create(boost::dynamic_pointer_cast<JIDSingleFormField>(field)->getValue().toString())); - if ( boost::dynamic_pointer_cast<JIDSingleFormField>(field)->getValue().isValid()) fieldElement->addNode(valueElement); - } - else if (boost::dynamic_pointer_cast<ListMultiFormField>(field)) { - fieldType = "list-multi"; - std::vector<std::string> lines = boost::dynamic_pointer_cast<ListMultiFormField>(field)->getValue(); - foreach(const std::string& line, lines) { - boost::shared_ptr<XMLElement> valueElement(new XMLElement("value")); - valueElement->addNode(XMLTextNode::create(line)); - fieldElement->addNode(valueElement); - } - } - else if (boost::dynamic_pointer_cast<TextMultiFormField>(field)) { - fieldType = "text-multi"; - multiLineify(boost::dynamic_pointer_cast<TextMultiFormField>(field)->getValue(), "value", fieldElement); - } - else { - assert(false); + switch (field->getType()) { + case FormField::UnknownType: fieldType = ""; break; + case FormField::BooleanType: fieldType = "boolean"; break; + case FormField::FixedType: fieldType = "fixed"; break; + case FormField::HiddenType: fieldType = "hidden"; break; + case FormField::ListSingleType: fieldType = "list-single"; break; + case FormField::TextMultiType: fieldType = "text-multi"; break; + case FormField::TextPrivateType: fieldType = "text-private"; break; + case FormField::TextSingleType: fieldType = "text-single"; break; + case FormField::JIDSingleType: fieldType = "jid-single"; break; + case FormField::JIDMultiType: fieldType = "jid-multi"; break; + case FormField::ListMultiType: fieldType = "list-multi"; break; } if (!fieldType.empty() && withTypeAttribute) { fieldElement->setAttribute("type", fieldType); } + foreach (const std::string& value, field->getValues()) { + boost::shared_ptr<XMLElement> valueElement = boost::make_shared<XMLElement>("value"); + valueElement->addNode(boost::make_shared<XMLTextNode>(value)); + fieldElement->addNode(valueElement); + } foreach (const FormField::Option& option, field->getOptions()) { boost::shared_ptr<XMLElement> optionElement(new XMLElement("option")); diff --git a/Swiften/Serializer/PayloadSerializers/FormSerializer.h b/Swiften/Serializer/PayloadSerializers/FormSerializer.h index bb31c97..f0f365c 100644 --- a/Swiften/Serializer/PayloadSerializers/FormSerializer.h +++ b/Swiften/Serializer/PayloadSerializers/FormSerializer.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010 Kevin Smith + * Copyright (c) 2010-2013 Kevin Smith * Licensed under the GNU General Public License v3. * See Documentation/Licenses/GPLv3.txt for more information. */ diff --git a/Swiften/Serializer/PayloadSerializers/StreamInitiationSerializer.cpp b/Swiften/Serializer/PayloadSerializers/StreamInitiationSerializer.cpp index 030b024..973ced9 100644 --- a/Swiften/Serializer/PayloadSerializers/StreamInitiationSerializer.cpp +++ b/Swiften/Serializer/PayloadSerializers/StreamInitiationSerializer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010 Remko Tronçon + * Copyright (c) 2010-2013 Remko Tronçon * Licensed under the GNU General Public License v3. * See Documentation/Licenses/GPLv3.txt for more information. */ @@ -52,7 +52,7 @@ std::string StreamInitiationSerializer::serializePayload(boost::shared_ptr<Strea boost::shared_ptr<XMLElement> featureElement(new XMLElement("feature", FEATURE_NEG_NS)); if (streamInitiation->getProvidedMethods().size() > 0) { Form::ref form(new Form(Form::FormType)); - ListSingleFormField::ref field = ListSingleFormField::create(); + FormField::ref field = boost::make_shared<FormField>(FormField::ListSingleType); field->setName("stream-method"); foreach(const std::string& method, streamInitiation->getProvidedMethods()) { field->addOption(FormField::Option("", method)); @@ -62,7 +62,8 @@ std::string StreamInitiationSerializer::serializePayload(boost::shared_ptr<Strea } else if (!streamInitiation->getRequestedMethod().empty()) { Form::ref form(new Form(Form::SubmitType)); - ListSingleFormField::ref field = ListSingleFormField::create(streamInitiation->getRequestedMethod()); + FormField::ref field = boost::make_shared<FormField>(FormField::ListSingleType); + field->addValue(streamInitiation->getRequestedMethod()); field->setName("stream-method"); form->addField(field); featureElement->addNode(boost::make_shared<XMLRawTextNode>(FormSerializer().serialize(form))); diff --git a/Swiften/Serializer/PayloadSerializers/UnitTest/FormSerializerTest.cpp b/Swiften/Serializer/PayloadSerializers/UnitTest/FormSerializerTest.cpp index 4608522..e4ce2c8 100644 --- a/Swiften/Serializer/PayloadSerializers/UnitTest/FormSerializerTest.cpp +++ b/Swiften/Serializer/PayloadSerializers/UnitTest/FormSerializerTest.cpp @@ -1,11 +1,12 @@ /* - * Copyright (c) 2010 Remko Tronçon + * Copyright (c) 2010-2013 Remko Tronçon * Licensed under the GNU General Public License v3. * See Documentation/Licenses/GPLv3.txt for more information. */ #include <cppunit/extensions/HelperMacros.h> #include <cppunit/extensions/TestFactoryRegistry.h> +#include <boost/smart_ptr/make_shared.hpp> #include <Swiften/Serializer/PayloadSerializers/FormSerializer.h> using namespace Swift; @@ -36,37 +37,37 @@ class FormSerializerTest : public CppUnit::TestFixture { FormSerializer testling; boost::shared_ptr<Form> form(new Form(Form::FormType)); - FormField::ref field = HiddenFormField::create("jabber:bot"); + FormField::ref field = boost::make_shared<FormField>(FormField::HiddenType, "jabber:bot"); field->setName("FORM_TYPE"); form->addField(field); - form->addField(FixedFormField::create("Section 1: Bot Info")); + form->addField(boost::make_shared<FormField>(FormField::FixedType, "Section 1: Bot Info")); - field = TextSingleFormField::create(); + field = boost::make_shared<FormField>(FormField::TextSingleType); field->setName("botname"); field->setLabel("The name of your bot"); form->addField(field); - field = TextMultiFormField::create("This is a bot.\nA quite good one actually"); + field = boost::make_shared<FormField>(FormField::TextMultiType); + field->setTextMultiValue("This is a bot.\nA quite good one actually"); field->setName("description"); field->setLabel("Helpful description of your bot"); form->addField(field); - field = BooleanFormField::create(true); + field = boost::make_shared<FormField>(FormField::BooleanType, "1"); field->setName("public"); field->setLabel("Public bot?"); field->setRequired(true); form->addField(field); - field = TextPrivateFormField::create(); + field = boost::make_shared<FormField>(FormField::TextPrivateType); field->setName("password"); field->setLabel("Password for special access"); form->addField(field); - std::vector<std::string> values; - values.push_back("news"); - values.push_back("search"); - field = ListMultiFormField::create(values); + field = boost::make_shared<FormField>(FormField::ListMultiType); + field->addValue("news"); + field->addValue("search"); field->setName("features"); field->setLabel("What features will the bot support?"); field->addOption(FormField::Option("Contests", "contests")); @@ -76,7 +77,7 @@ class FormSerializerTest : public CppUnit::TestFixture { field->addOption(FormField::Option("Search", "search")); form->addField(field); - field = ListSingleFormField::create("20"); + field = boost::make_shared<FormField>(FormField::ListSingleType, "20"); field->setName("maxsubs"); field->setLabel("Maximum number of subscribers"); field->addOption(FormField::Option("10", "10")); @@ -88,9 +89,9 @@ class FormSerializerTest : public CppUnit::TestFixture { form->addField(field); std::vector<JID> jids; - jids.push_back(JID("foo@bar.com")); - jids.push_back(JID("baz@fum.org")); - field = JIDMultiFormField::create(jids); + field = boost::make_shared<FormField>(FormField::JIDMultiType); + field->addValue("foo@bar.com"); + field->addValue("baz@fum.org"); field->setName("invitelist"); field->setLabel("People to invite"); field->setDescription("Tell all your friends about your new bot!"); @@ -140,62 +141,62 @@ class FormSerializerTest : public CppUnit::TestFixture { boost::shared_ptr<Form> form(new Form(Form::ResultType)); - FormField::ref field = HiddenFormField::create("jabber:iq:search"); + FormField::ref field = boost::make_shared<FormField>(FormField::HiddenType, "jabber:iq:search"); field->setName("FORM_TYPE"); form->addField(field); // reported fields - field = TextSingleFormField::create(); + field = boost::make_shared<FormField>(FormField::TextSingleType); field->setName("first"); field->setLabel("Given Name"); form->addReportedField(field); - field = TextSingleFormField::create(); + field = boost::make_shared<FormField>(FormField::TextSingleType); field->setName("last"); field->setLabel("Family Name"); form->addReportedField(field); - field = JIDSingleFormField::create(); + field = boost::make_shared<FormField>(FormField::JIDSingleType); field->setName("jid"); field->setLabel("Jabber ID"); form->addReportedField(field); - field = ListSingleFormField::create(); + field = boost::make_shared<FormField>(FormField::ListSingleType); field->setName("x-gender"); field->setLabel("Gender"); form->addReportedField(field); Form::FormItem firstItem; - field = TextSingleFormField::create("Benvolio"); + field = boost::make_shared<FormField>(FormField::TextSingleType, "Benvolio"); field->setName("first"); firstItem.push_back(field); - field = TextSingleFormField::create("Montague"); + field = boost::make_shared<FormField>(FormField::TextSingleType, "Montague"); field->setName("last"); firstItem.push_back(field); - field = JIDSingleFormField::create(JID("benvolio@montague.net")); + field = boost::make_shared<FormField>(FormField::JIDSingleType, JID("benvolio@montague.net")); field->setName("jid"); firstItem.push_back(field); - field = ListSingleFormField::create("male"); + field = boost::make_shared<FormField>(FormField::ListSingleType, "male"); field->setName("x-gender"); firstItem.push_back(field); Form::FormItem secondItem; - field = TextSingleFormField::create("Romeo"); + field = boost::make_shared<FormField>(FormField::TextSingleType, "Romeo"); field->setName("first"); secondItem.push_back(field); - field = TextSingleFormField::create("Montague"); + field = boost::make_shared<FormField>(FormField::TextSingleType, "Montague"); field->setName("last"); secondItem.push_back(field); - field = JIDSingleFormField::create(JID("romeo@montague.net")); + field = boost::make_shared<FormField>(FormField::JIDSingleType, JID("romeo@montague.net")); field->setName("jid"); secondItem.push_back(field); - field = ListSingleFormField::create("male"); + field = boost::make_shared<FormField>(FormField::ListSingleType, "male"); field->setName("x-gender"); secondItem.push_back(field); diff --git a/Swiften/Serializer/PayloadSerializers/UnitTest/InBandRegistrationPayloadSerializerTest.cpp b/Swiften/Serializer/PayloadSerializers/UnitTest/InBandRegistrationPayloadSerializerTest.cpp index df43e69..7cbce35 100644 --- a/Swiften/Serializer/PayloadSerializers/UnitTest/InBandRegistrationPayloadSerializerTest.cpp +++ b/Swiften/Serializer/PayloadSerializers/UnitTest/InBandRegistrationPayloadSerializerTest.cpp @@ -1,11 +1,12 @@ /* - * Copyright (c) 2010 Remko Tronçon + * Copyright (c) 2010-2013 Remko Tronçon * Licensed under the GNU General Public License v3. * See Documentation/Licenses/GPLv3.txt for more information. */ #include <cppunit/extensions/HelperMacros.h> #include <cppunit/extensions/TestFactoryRegistry.h> +#include <boost/smart_ptr/make_shared.hpp> #include <Swiften/Serializer/PayloadSerializers/InBandRegistrationPayloadSerializer.h> @@ -38,7 +39,7 @@ class InBandRegistrationPayloadSerializerTest : public CppUnit::TestFixture { boost::shared_ptr<Form> form(new Form(Form::FormType)); form->setTitle("Contest Registration"); - FormField::ref field = HiddenFormField::create("jabber:iq:register"); + FormField::ref field = boost::make_shared<FormField>(FormField::HiddenType, "jabber:iq:register"); field->setName("FORM_TYPE"); form->addField(field); registration->setForm(form); diff --git a/Swiften/Serializer/PayloadSerializers/UnitTest/SearchPayloadSerializerTest.cpp b/Swiften/Serializer/PayloadSerializers/UnitTest/SearchPayloadSerializerTest.cpp index 42bff72..29e20c9 100644 --- a/Swiften/Serializer/PayloadSerializers/UnitTest/SearchPayloadSerializerTest.cpp +++ b/Swiften/Serializer/PayloadSerializers/UnitTest/SearchPayloadSerializerTest.cpp @@ -1,11 +1,12 @@ /* - * Copyright (c) 2010 Remko Tronçon + * Copyright (c) 2010-2013 Remko Tronçon * Licensed under the GNU General Public License v3. * See Documentation/Licenses/GPLv3.txt for more information. */ #include <cppunit/extensions/HelperMacros.h> #include <cppunit/extensions/TestFactoryRegistry.h> +#include <boost/smart_ptr/make_shared.hpp> #include <Swiften/Serializer/PayloadSerializers/SearchPayloadSerializer.h> @@ -78,62 +79,62 @@ class SearchPayloadSerializerTest : public CppUnit::TestFixture { SearchPayload::ref payload(new SearchPayload()); boost::shared_ptr<Form> form(new Form(Form::ResultType)); - FormField::ref field = HiddenFormField::create("jabber:iq:search"); + FormField::ref field = boost::make_shared<FormField>(FormField::HiddenType, "jabber:iq:search"); field->setName("FORM_TYPE"); form->addField(field); // reported fields - field = TextSingleFormField::create(); + field = boost::make_shared<FormField>(FormField::TextSingleType); field->setName("first"); field->setLabel("Given Name"); form->addReportedField(field); - field = TextSingleFormField::create(); + field = boost::make_shared<FormField>(FormField::TextSingleType); field->setName("last"); field->setLabel("Family Name"); form->addReportedField(field); - field = JIDSingleFormField::create(); + field = boost::make_shared<FormField>(FormField::JIDSingleType); field->setName("jid"); field->setLabel("Jabber ID"); form->addReportedField(field); - field = ListSingleFormField::create(); + field = boost::make_shared<FormField>(FormField::ListSingleType); field->setName("x-gender"); field->setLabel("Gender"); form->addReportedField(field); Form::FormItem firstItem; - field = TextSingleFormField::create("Benvolio"); + field = boost::make_shared<FormField>(FormField::TextSingleType, "Benvolio"); field->setName("first"); firstItem.push_back(field); - field = TextSingleFormField::create("Montague"); + field = boost::make_shared<FormField>(FormField::TextSingleType, "Montague"); field->setName("last"); firstItem.push_back(field); - field = TextSingleFormField::create("benvolio@montague.net"); + field = boost::make_shared<FormField>(FormField::TextSingleType, "benvolio@montague.net"); field->setName("jid"); firstItem.push_back(field); - field = ListSingleFormField::create("male"); + field = boost::make_shared<FormField>(FormField::ListSingleType, "male"); field->setName("x-gender"); firstItem.push_back(field); Form::FormItem secondItem; - field = TextSingleFormField::create("Romeo"); + field = boost::make_shared<FormField>(FormField::TextSingleType, "Romeo"); field->setName("first"); secondItem.push_back(field); - field = TextSingleFormField::create("Montague"); + field = boost::make_shared<FormField>(FormField::TextSingleType, "Montague"); field->setName("last"); secondItem.push_back(field); - field = TextSingleFormField::create("romeo@montague.net"); + field = boost::make_shared<FormField>(FormField::TextSingleType, "romeo@montague.net"); field->setName("jid"); secondItem.push_back(field); - field = ListSingleFormField::create("male"); + field = boost::make_shared<FormField>(FormField::ListSingleType, "male"); field->setName("x-gender"); secondItem.push_back(field); |