summaryrefslogtreecommitdiffstats
blob: fbd1ebef187f5bcf39f973058ee6941c430bbafe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * Copyright (c) 2010 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>
#include <boost/shared_ptr.hpp>
#include <string>

#include <Swiften/JID/JID.h>

namespace Swift {
	class FormField {
		public:
			typedef boost::shared_ptr<FormField> ref;

			virtual ~FormField() {}

			struct Option {
				Option(const std::string& label, const std::string& value) : label(label), value(value) {}
				std::string label;
				std::string value;
			};

			void setName(const std::string& name) { this->name = name; }
			const std::string& getName() const { return name; }

			void setLabel(const std::string& label) { this->label = label; }
			const std::string& getLabel() const { return label; }

			void setDescription(const std::string& description) { this->description = description; }
			const std::string& getDescription() const { return description; }

			void setRequired(bool required) { this->required = required; }
			bool getRequired() const { return required; }

			void addOption(const Option& option) {
				options.push_back(option);
			}

			const std::vector<Option>& getOptions() const {
				return options;
			}

			const std::vector<std::string>& getRawValues() const {
				return rawValues;
			}

			void addRawValue(const std::string& value) {
				rawValues.push_back(value);
			}

		protected:
			FormField() : required(false) {}

		private:
			std::string name;
			std::string label;
			std::string description;
			bool required;
			std::vector<Option> options;
			std::vector<std::string> rawValues;
	};

	template<typename T> class GenericFormField : public FormField {
		public:
			const T& getValue() const {
				return value;
			}

			void setValue(const T& value) {
				this->value = 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 >() {} \
	};

	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>)
}