summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2010-08-08 18:41:53 (GMT)
committerRemko Tronçon <git@el-tramo.be>2010-08-08 18:41:53 (GMT)
commit19df82042f44c201e5a2821b4fa35465e33a1c90 (patch)
tree1746bddfa31ce2a6488ef5186036a049a255c9da /Swiften/Elements
parent4a5a0977f661bf5c7c34ee7aa48b35073a682203 (diff)
downloadswift-contrib-19df82042f44c201e5a2821b4fa35465e33a1c90.zip
swift-contrib-19df82042f44c201e5a2821b4fa35465e33a1c90.tar.bz2
Added XEP-0004 data forms parsing & serializing.
Diffstat (limited to 'Swiften/Elements')
-rw-r--r--Swiften/Elements/Command.h66
-rw-r--r--Swiften/Elements/Form.h46
-rw-r--r--Swiften/Elements/FormField.h102
3 files changed, 214 insertions, 0 deletions
diff --git a/Swiften/Elements/Command.h b/Swiften/Elements/Command.h
new file mode 100644
index 0000000..c802035
--- /dev/null
+++ b/Swiften/Elements/Command.h
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2010 Kevin Smith
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include "Swiften/Base/String.h"
+#include "Swiften/Elements/Payload.h"
+
+namespace Swift {
+ /**
+ * Ad-Hoc Command (XEP-0050).
+ */
+ class Command : public Payload {
+ public:
+ enum Status {Executing, Completed, Canceled, NoStatus};
+ enum Action {Cancel, Execute, Complete, Prev, Next, NoAction};
+
+ struct Note {
+ enum Type {Info, Warn, Error};
+
+ Note(String note, Type type) : note(note), type(type) {};
+
+ String note;
+ Type type;
+ };
+
+ public:
+ Command(const String& node, const String& sessionID, Status status) { constructor(node, sessionID, NoAction, status);}
+ Command(const String& node, const String& sessionID = "", Action action = Execute) { constructor(node, sessionID, action, NoStatus); }
+
+ const String& getNode() const { return node_; }
+ const String& getSessionID() const { return sessionID_; }
+ Action getPerformedAction() const { return performedAction_; }
+ void setExecuteAction(Action action) { executeAction_ = action; }
+ Action getExecuteAction() const { return executeAction_; }
+ Status getStatus() const { return status_; }
+ void addAvailableAction(Action action) { availableActions_.push_back(action); }
+ const std::vector<Action>& getAvailableActions() const { return availableActions_; }
+ void addNote(const Note& note) { notes_.push_back(note); }
+ const std::vector<Note>& getNotes() const { return notes_; }
+ boost::shared_ptr<Payload> getPayload() const { return payload_; }
+ void setPayload(boost::shared_ptr<Payload> payload) { payload_ = payload; }
+
+ private:
+ void constructor(const String& node, const String& sessionID, Action action, Status status) {
+ node_ = node;
+ sessionID_ = sessionID;
+ performedAction_ = action;
+ status_ = status;
+ executeAction_ = NoAction;
+ }
+
+ private:
+ String node_;
+ String sessionID_;
+ Action performedAction_;
+ Status status_;
+ Action executeAction_;
+ std::vector<Action> availableActions_;
+ std::vector<Note> notes_;
+ boost::shared_ptr<Payload> payload_;
+ };
+}
diff --git a/Swiften/Elements/Form.h b/Swiften/Elements/Form.h
new file mode 100644
index 0000000..ed77d2b
--- /dev/null
+++ b/Swiften/Elements/Form.h
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2010 Kevin Smith
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include <vector>
+
+#include "Swiften/Elements/Payload.h"
+#include "Swiften/Elements/FormField.h"
+#include "Swiften/Base/String.h"
+#include "Swiften/JID/JID.h"
+
+namespace Swift {
+ /**
+ * XEP-0004 Data form.
+ * For the relevant Fields, the parsers and serialisers protect the API user against
+ * the strange multi-value instead of newline thing by transforming them.
+ */
+ class Form : public Payload {
+ public:
+ enum Type { FormType, SubmitType, CancelType, ResultType };
+
+ public:
+ Form(Type type = FormType) : type_(type) {}
+
+ void addField(boost::shared_ptr<FormField> field) { fields_.push_back(field); }
+ const std::vector<boost::shared_ptr<FormField> >& getFields() const { return fields_; }
+ void setTitle(const String& title) { title_ = title; }
+ const String& getTitle() { return title_; }
+
+ void setInstructions(const String& instructions) { instructions_ = instructions; }
+ const String& getInstructions() { return instructions_; }
+
+ Type getType() { return type_; }
+ void setType(Type type) { type_ = type; }
+
+ private:
+ std::vector<boost::shared_ptr<FormField> > fields_;
+ String title_;
+ String instructions_;
+ Type type_;
+ };
+}
diff --git a/Swiften/Elements/FormField.h b/Swiften/Elements/FormField.h
new file mode 100644
index 0000000..732203a
--- /dev/null
+++ b/Swiften/Elements/FormField.h
@@ -0,0 +1,102 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include <vector>
+#include <boost/shared_ptr.hpp>
+
+#include "Swiften/Base/String.h"
+#include "Swiften/JID/JID.h"
+
+namespace Swift {
+ class FormField {
+ public:
+ typedef boost::shared_ptr<FormField> ref;
+
+ virtual ~FormField() {}
+
+ struct Option {
+ Option(const String& label, const String& value) : label(label), value(value) {}
+ String label;
+ String value;
+ };
+
+ void setName(const String& name) { this->name = name; }
+ const String& getName() const { return name; }
+
+ void setLabel(const String& label) { this->label = label; }
+ const String& getLabel() const { return label; }
+
+ void setDescription(const String& description) { this->description = description; }
+ const 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;
+ }
+
+ protected:
+ FormField() : required(false) {}
+
+ private:
+ String name;
+ String label;
+ String description;
+ bool required;
+ std::vector<Option> options;
+ };
+
+ template<typename T> class GenericFormField : public FormField {
+ public:
+ const T& getValue() const {
+ return value;
+ }
+
+ void setValue(const T& value) {
+ this->value = value;
+ }
+
+ protected:
+ GenericFormField() {}
+ 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, String);
+ SWIFTEN_DECLARE_FORM_FIELD(Hidden, String);
+ SWIFTEN_DECLARE_FORM_FIELD(ListSingle, String);
+ SWIFTEN_DECLARE_FORM_FIELD(TextMulti, String);
+ SWIFTEN_DECLARE_FORM_FIELD(TextPrivate, String);
+ SWIFTEN_DECLARE_FORM_FIELD(TextSingle, String);
+ SWIFTEN_DECLARE_FORM_FIELD(JIDSingle, JID);
+ SWIFTEN_DECLARE_FORM_FIELD(JIDMulti, std::vector<JID>);
+ SWIFTEN_DECLARE_FORM_FIELD(ListMulti, std::vector<String>);
+}