summaryrefslogtreecommitdiffstats
blob: 2d71ac7ac1b1ca3750ad1cca84aa368ab653f13d (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 * Copyright (c) 2010-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#pragma once

#include <memory>
#include <string>
#include <vector>

#include <Swiften/Base/API.h>
#include <Swiften/JID/JID.h>

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

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

            void clearOptions() {
                options.clear();
            }

            const std::vector<std::string>& getValues() const {
                return values;
            }

            void addValue(const std::string& value) {
                values.push_back(value);
            }

            Type getType() const {
                return type;
            }

            void setType(Type type) {
                this->type = type;
            }

            // Type specific

            bool getBoolValue() const {
                assert(type == BooleanType || type == UnknownType);
                if (values.empty()) {
                    return false;
                }
                return values[0] == "true" || values[0] == "1";
            }

            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:

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

}