summaryrefslogtreecommitdiffstats
blob: cf0f4ead0697c3fcf196186061c9d272e4a814dd (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
140
141
142
143
144
145
146
147
148
/*
 * Copyright (c) 2010-2013 Kevin Smith
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#include <Swiften/Serializer/PayloadSerializers/FormSerializer.h>

#include <boost/shared_ptr.hpp>
#include <boost/smart_ptr/make_shared.hpp>
#include <iostream>
#include <string>

#include <Swiften/Base/String.h>
#include <Swiften/Base/Algorithm.h>
#include <Swiften/Base/foreach.h>
#include <Swiften/Serializer/XML/XMLTextNode.h>
#include <Swiften/Serializer/XML/XMLRawTextNode.h>

using namespace Swift;

namespace {
	template<typename T> void serializeValueAsString(boost::shared_ptr<FormField> field, boost::shared_ptr<XMLElement> parent) {
		std::string value = boost::dynamic_pointer_cast<T>(field)->getValue();
		if (!value.empty()) {
			boost::shared_ptr<XMLElement> valueElement(new XMLElement("value"));
			valueElement->addNode(XMLTextNode::create(value));
			parent->addNode(valueElement);
		}
	}
}


namespace Swift {

FormSerializer::FormSerializer() : GenericPayloadSerializer<Form>() {
}

std::string FormSerializer::serializePayload(boost::shared_ptr<Form> form)  const {
	if (!form) {
		return "";
	}

	boost::shared_ptr<XMLElement> formElement(new XMLElement("x", "jabber:x:data"));
	std::string type;
	switch (form->getType()) {
		case Form::FormType: type = "form"; break;
		case Form::SubmitType: type = "submit"; break;
		case Form::CancelType: type = "cancel"; break;
		case Form::ResultType: type = "result"; break;
	}
	formElement->setAttribute("type", type);
	if (!form->getTitle().empty()) {
		multiLineify(form->getTitle(), "title", formElement);
	}
	if (!form->getInstructions().empty()) {
		multiLineify(form->getInstructions(), "instructions", formElement);
	}
	foreach(boost::shared_ptr<FormField> field, form->getFields()) {
		formElement->addNode(fieldToXML(field, true));
	}
	if (!form->getReportedFields().empty()) {
		boost::shared_ptr<XMLElement> reportedElement(new XMLElement("reported"));
		foreach(FormField::ref field, form->getReportedFields()) {
			reportedElement->addNode(fieldToXML(field, true));
		}
		formElement->addNode(reportedElement);
	}
	foreach(Form::FormItem item, form->getItems()) {
		boost::shared_ptr<XMLElement> itemElement(new XMLElement("item"));
		foreach(FormField::ref field, item) {
			itemElement->addNode(fieldToXML(field, false));
		}
		formElement->addNode(itemElement);
	}

	return formElement->serialize();
}

boost::shared_ptr<XMLElement> FormSerializer::fieldToXML(boost::shared_ptr<FormField> field, bool withTypeAttribute) const {
	boost::shared_ptr<XMLElement> fieldElement(new XMLElement("field"));
	if (!field->getName().empty()) {
		fieldElement->setAttribute("var", field->getName());
	}
	if (!field->getLabel().empty()) {
		fieldElement->setAttribute("label", field->getLabel());
	}
	if (field->getRequired()) {
		fieldElement->addNode(boost::make_shared<XMLElement>("required"));
	}
	if (!field->getDescription().empty()) {
		boost::shared_ptr<XMLElement> descriptionElement(new XMLElement("desc"));
		descriptionElement->addNode(boost::make_shared<XMLTextNode>(field->getDescription()));
		fieldElement->addNode(descriptionElement);
	}

	// Set the value and type
	std::string fieldType;
	switch (field->getType()) {
		case FormField::UnknownType: fieldType = ""; break;
		case FormField::BooleanType: fieldType = "boolean"; break;
		case FormField::FixedType: fieldType = "fixed"; break;
		case FormField::HiddenType: fieldType = "hidden"; break;
		case FormField::ListSingleType: fieldType = "list-single"; break;
		case FormField::TextMultiType: fieldType = "text-multi"; break;
		case FormField::TextPrivateType: fieldType = "text-private"; break;
		case FormField::TextSingleType: fieldType = "text-single"; break;
		case FormField::JIDSingleType: fieldType = "jid-single"; break;
		case FormField::JIDMultiType: fieldType = "jid-multi"; break;
		case FormField::ListMultiType: fieldType = "list-multi"; break;
	}
	if (!fieldType.empty() && withTypeAttribute) {
		fieldElement->setAttribute("type", fieldType);
	}
	foreach (const std::string& value, field->getValues()) {
		boost::shared_ptr<XMLElement> valueElement = boost::make_shared<XMLElement>("value");
		valueElement->addNode(boost::make_shared<XMLTextNode>(value));
		fieldElement->addNode(valueElement);
	}

	foreach (const FormField::Option& option, field->getOptions()) {
		boost::shared_ptr<XMLElement> optionElement(new XMLElement("option"));
		if (!option.label.empty()) {
			optionElement->setAttribute("label", option.label);
		}

		boost::shared_ptr<XMLElement> valueElement(new XMLElement("value"));
		valueElement->addNode(XMLTextNode::create(option.value));
		optionElement->addNode(valueElement);

		fieldElement->addNode(optionElement);
	}

	return fieldElement;
}

void FormSerializer::multiLineify(const std::string& text, const std::string& elementName, boost::shared_ptr<XMLElement> element) const {
	std::string unRdText(text);
	erase(unRdText, '\r');
	std::vector<std::string> lines = String::split(unRdText, '\n');
	foreach (std::string line, lines) {
		boost::shared_ptr<XMLElement> lineElement(new XMLElement(elementName));
		lineElement->addNode(boost::make_shared<XMLTextNode>(line));
		element->addNode(lineElement);
	}
}

}