summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2013-08-22 21:07:51 (GMT)
committerRemko Tronçon <git@el-tramo.be>2013-08-23 08:02:12 (GMT)
commit00284e5f4445a7bbab482196901c5927ea7d5488 (patch)
treef2ca6e0c51632a8aa5a7a884f3b312bacc3a2fb0 /Swiften/Elements/FormField.h
parent54da0a4c0cceae947afbde586a97d1cc60c50ed8 (diff)
downloadswift-00284e5f4445a7bbab482196901c5927ea7d5488.zip
swift-00284e5f4445a7bbab482196901c5927ea7d5488.tar.bz2
FormField element refactoring.
This should make FormField easier to use. Change-Id: Ia5eeedcdb673e2fe5e38fd23d5ab00970178bc44
Diffstat (limited to 'Swiften/Elements/FormField.h')
-rw-r--r--Swiften/Elements/FormField.h126
1 files changed, 73 insertions, 53 deletions
diff --git a/Swiften/Elements/FormField.h b/Swiften/Elements/FormField.h
index fbd1ebe..26e70bb 100644
--- a/Swiften/Elements/FormField.h
+++ b/Swiften/Elements/FormField.h
@@ -1,12 +1,9 @@
/*
- * 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.
*/
-// FIXME: We currently keep 2 values: the raw values, and the actual value.
-// We should only store the raw values, and deduce the actual values from this
-
#pragma once
#include <vector>
@@ -20,7 +17,25 @@ namespace Swift {
public:
typedef boost::shared_ptr<FormField> ref;
- virtual ~FormField() {}
+ enum Type {
+ UnknownType,
+ BooleanType,
+ FixedType,
+ HiddenType,
+ ListSingleType,
+ TextMultiType,
+ TextPrivateType,
+ TextSingleType,
+ JIDSingleType,
+ JIDMultiType,
+ ListMultiType
+ };
+
+ FormField(Type type = UnknownType) : type(type), required(false) {}
+ FormField(Type type, const std::string& value) : type(type), required(false) {
+ addValue(value);
+ }
+ virtual ~FormField();
struct Option {
Option(const std::string& label, const std::string& value) : label(label), value(value) {}
@@ -48,67 +63,72 @@ namespace Swift {
return options;
}
- const std::vector<std::string>& getRawValues() const {
- return rawValues;
+ const std::vector<std::string>& getValues() const {
+ return values;
}
- void addRawValue(const std::string& value) {
- rawValues.push_back(value);
+ void addValue(const std::string& value) {
+ values.push_back(value);
}
- protected:
- FormField() : required(false) {}
+ Type getType() const {
+ return type;
+ }
- private:
- std::string name;
- std::string label;
- std::string description;
- bool required;
- std::vector<Option> options;
- std::vector<std::string> rawValues;
- };
+ void setType(Type type) {
+ this->type = type;
+ }
- template<typename T> class GenericFormField : public FormField {
- public:
- const T& getValue() const {
- return value;
+ // Type specific
+
+ bool getBoolValue() const {
+ assert(type == BooleanType || type == UnknownType);
+ if (values.empty()) {
+ return false;
+ }
+ return values[0] == "true" || values[0] == "1";
}
- void setValue(const T& value) {
- this->value = value;
+ void setBoolValue(bool b);
+
+ JID getJIDSingleValue() const {
+ assert(type == JIDSingleType || type == UnknownType);
+ return values.empty() ? JID() : JID(values[0]);
}
+ JID getJIDMultiValue(size_t index) const {
+ assert(type == JIDMultiType || type == UnknownType);
+ return values.empty() ? JID() : JID(values[index]);
+ }
+
+ std::string getTextPrivateValue() const {
+ assert(type == TextPrivateType || type == UnknownType);
+ return values.empty() ? "" : values[0];
+ }
+
+ std::string getFixedValue() const {
+ assert(type == FixedType || type == UnknownType);
+ return values.empty() ? "" : values[0];
+ }
+
+ std::string getTextSingleValue() const {
+ assert(type == TextSingleType || type == UnknownType);
+ return values.empty() ? "" : values[0];
+ }
+
+ std::string getTextMultiValue() const;
+ void setTextMultiValue(const std::string& value);
+
protected:
- GenericFormField() : value() {}
- GenericFormField(const T& value) : value(value) {}
private:
- T value;
- };
-
-#define SWIFTEN_DECLARE_FORM_FIELD(name, valueType) \
- class name##FormField : public GenericFormField< valueType > { \
- public: \
- typedef boost::shared_ptr<name##FormField> ref; \
- static ref create(const valueType& value) { \
- return ref(new name##FormField(value)); \
- } \
- static ref create() { \
- return ref(new name##FormField()); \
- } \
- private: \
- name##FormField(valueType value) : GenericFormField< valueType >(value) {} \
- name##FormField() : GenericFormField< valueType >() {} \
+ Type type;
+ std::string name;
+ std::string label;
+ std::string description;
+ bool required;
+ std::vector<Option> options;
+ std::vector<std::string> values;
};
- SWIFTEN_DECLARE_FORM_FIELD(Boolean, bool)
- SWIFTEN_DECLARE_FORM_FIELD(Fixed, std::string)
- SWIFTEN_DECLARE_FORM_FIELD(Hidden, std::string)
- SWIFTEN_DECLARE_FORM_FIELD(ListSingle, std::string)
- SWIFTEN_DECLARE_FORM_FIELD(TextMulti, std::string)
- SWIFTEN_DECLARE_FORM_FIELD(TextPrivate, std::string)
- SWIFTEN_DECLARE_FORM_FIELD(TextSingle, std::string)
- SWIFTEN_DECLARE_FORM_FIELD(JIDSingle, JID)
- SWIFTEN_DECLARE_FORM_FIELD(JIDMulti, std::vector<JID>)
- SWIFTEN_DECLARE_FORM_FIELD(ListMulti, std::vector<std::string>)
}