summaryrefslogtreecommitdiffstats
blob: d0fa9f98d003f436e7122a06e0aca06cc898c529 (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
/*
 * Copyright (c) 2012 Mateusz Piękos
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

#include <Swiften/Parser/PayloadParsers/WhiteboardParser.h>
#include <Swiften/Whiteboard/Elements/WhiteboardLineElement.h>
#include <Swiften/Whiteboard/Elements/Color.h>
#include <boost/optional.hpp>
#include <boost/smart_ptr/make_shared.hpp>

namespace Swift {
	WhiteboardParser::WhiteboardParser() : level_(0) {
	}

	void WhiteboardParser::handleStartElement(const std::string& element, const std::string&, const AttributeMap& attributes) {
		if (level_ == 0) {
			getPayloadInternal()->setType(stringToType(attributes.getAttributeValue("type").get_value_or("")));
		} else if (level_ == 1) {
			if (element == "line") {
				int x1 = std::atoi(attributes.getAttributeValue("x1").get_value_or("0").c_str());
				int y1 = std::atoi(attributes.getAttributeValue("y1").get_value_or("0").c_str());
				int x2 = std::atoi(attributes.getAttributeValue("x2").get_value_or("0").c_str());
				int y2 = std::atoi(attributes.getAttributeValue("y2").get_value_or("0").c_str());
				WhiteboardLineElement::ref whiteboardElement = boost::make_shared<WhiteboardLineElement>(x1, y1, x2, y2);
				Color color(attributes.getAttributeValue("stroke").get_value_or("#00000"));

				std::string opacity = attributes.getAttributeValue("opacity").get_value_or("1");
				if (opacity.find('.') != std::string::npos) {
					opacity = opacity.substr(opacity.find('.')+1, 2);
					color.setAlpha(std::atoi(opacity.c_str())*255/100);
				} 

				whiteboardElement->setColor(color);

				int penWidth = std::atoi(attributes.getAttributeValue("stroke-width").get_value_or("1").c_str());
				whiteboardElement->setPenWidth(penWidth);

				getPayloadInternal()->setElement(whiteboardElement);
			}
		}
		++level_;
	}

	void WhiteboardParser::handleEndElement(const std::string& element, const std::string&) {
		--level_;
		if (level_ == 0) {
			getPayloadInternal()->setData(data_);
		}
	}

	void WhiteboardParser::handleCharacterData(const std::string& data) {
		data_ += data;
	}

	WhiteboardPayload::Type WhiteboardParser::stringToType(const std::string& type) const {
		if (type.empty()) {
			return WhiteboardPayload::Data;
		} else if (type == "session-request") {
			return WhiteboardPayload::SessionRequest;
		} else if (type == "session-accept") {
			return WhiteboardPayload::SessionAccept;
		} else if (type == "session-terminate") {
			return WhiteboardPayload::SessionTerminate;
		}
	}
}