• Main Page
  • Classes
  • Files
  • File List

Swiften/Elements/FormField.h

00001 /*
00002  * Copyright (c) 2010 Remko Tronçon
00003  * Licensed under the GNU General Public License v3.
00004  * See Documentation/Licenses/GPLv3.txt for more information.
00005  */
00006 
00007 // FIXME: We currently keep 2 values: the raw values, and the actual value.
00008 // We should only store the raw values, and deduce the actual values from this
00009 
00010 #pragma once
00011 
00012 #include <vector>
00013 #include <boost/shared_ptr.hpp>
00014 #include <string>
00015 
00016 #include <Swiften/JID/JID.h>
00017 
00018 namespace Swift {
00019   class FormField {
00020     public:
00021       typedef boost::shared_ptr<FormField> ref;
00022 
00023       virtual ~FormField() {}
00024 
00025       struct Option {
00026         Option(const std::string& label, const std::string& value) : label(label), value(value) {}
00027         std::string label;
00028         std::string value;
00029       };
00030 
00031       void setName(const std::string& name) { this->name = name; }
00032       const std::string& getName() const { return name; }
00033 
00034       void setLabel(const std::string& label) { this->label = label; }
00035       const std::string& getLabel() const { return label; }
00036 
00037       void setDescription(const std::string& description) { this->description = description; }
00038       const std::string& getDescription() const { return description; }
00039 
00040       void setRequired(bool required) { this->required = required; }
00041       bool getRequired() const { return required; }
00042 
00043       void addOption(const Option& option) {
00044         options.push_back(option);
00045       }
00046 
00047       const std::vector<Option>& getOptions() const {
00048         return options;
00049       }
00050 
00051       const std::vector<std::string>& getRawValues() const {
00052         return rawValues;
00053       }
00054 
00055       void addRawValue(const std::string& value) {
00056         rawValues.push_back(value);
00057       }
00058 
00059     protected:
00060       FormField() : required(false) {}
00061 
00062     private:
00063       std::string name;
00064       std::string label;
00065       std::string description;
00066       bool required;
00067       std::vector<Option> options;
00068       std::vector<std::string> rawValues;
00069   };
00070 
00071   template<typename T> class GenericFormField : public FormField {
00072     public:
00073       const T& getValue() const {
00074         return value;
00075       }
00076 
00077       void setValue(const T& value) {
00078         this->value = value;
00079       }
00080 
00081     protected:
00082       GenericFormField() : value() {}
00083       GenericFormField(const T& value) : value(value) {}
00084 
00085     private:
00086       T value;
00087   };
00088 
00089 #define SWIFTEN_DECLARE_FORM_FIELD(name, valueType) \
00090   class name##FormField : public GenericFormField< valueType > { \
00091     public: \
00092       typedef boost::shared_ptr<name##FormField> ref; \
00093       static ref create(const valueType& value) { \
00094         return ref(new name##FormField(value)); \
00095       } \
00096       static ref create() { \
00097         return ref(new name##FormField()); \
00098       } \
00099     private: \
00100       name##FormField(valueType value) : GenericFormField< valueType >(value) {} \
00101       name##FormField() : GenericFormField< valueType >() {} \
00102   };
00103 
00104   SWIFTEN_DECLARE_FORM_FIELD(Boolean, bool);
00105   SWIFTEN_DECLARE_FORM_FIELD(Fixed, std::string);
00106   SWIFTEN_DECLARE_FORM_FIELD(Hidden, std::string);
00107   SWIFTEN_DECLARE_FORM_FIELD(ListSingle, std::string);
00108   SWIFTEN_DECLARE_FORM_FIELD(TextMulti, std::string);
00109   SWIFTEN_DECLARE_FORM_FIELD(TextPrivate, std::string);
00110   SWIFTEN_DECLARE_FORM_FIELD(TextSingle, std::string);
00111   SWIFTEN_DECLARE_FORM_FIELD(JIDSingle, JID);
00112   SWIFTEN_DECLARE_FORM_FIELD(JIDMulti, std::vector<JID>);
00113   SWIFTEN_DECLARE_FORM_FIELD(ListMulti, std::vector<std::string>);
00114 }

Generated on Fri Oct 12 2012 21:00:19 for Swiften by  doxygen 1.7.1