From 1dc6b7f7a3d96df848edad85c0d07d99340e3a3e Mon Sep 17 00:00:00 2001 From: Mateusz Piekos Date: Fri, 15 Jun 2012 12:48:54 +0200 Subject: Moved serialization of elements to WhiteboardSerializer diff --git a/Swift/QtUI/Whiteboard/QtWhiteboardWindow.cpp b/Swift/QtUI/Whiteboard/QtWhiteboardWindow.cpp index 3ea09ef..80c4447 100644 --- a/Swift/QtUI/Whiteboard/QtWhiteboardWindow.cpp +++ b/Swift/QtUI/Whiteboard/QtWhiteboardWindow.cpp @@ -9,9 +9,11 @@ #include #include +#include #include #include +#include #include using namespace std; @@ -245,10 +247,13 @@ namespace Swift { QGraphicsLineItem* lineItem = qgraphicsitem_cast(item); if (lineItem != 0) { QLine line = lineItem->line().toLine(); - std::stringstream stream; +/* std::stringstream stream; stream << "L"; stream << line.x1() << "," << line.y1() << "," << line.x2() << "," << line.y2(); - stream >> serialized; + stream >> serialized;*/ + WhiteboardLineElement::ref element = boost::make_shared(line.x1(), line.y1(), line.x2(), line.y2()); + whiteboardSession_->sendElement(element); + } FreehandLineItem* freehandLineItem = qgraphicsitem_cast(item); if (freehandLineItem != 0) { diff --git a/Swiften/Elements/WhiteboardPayload.h b/Swiften/Elements/WhiteboardPayload.h index 3ad1706..c61e4a5 100644 --- a/Swiften/Elements/WhiteboardPayload.h +++ b/Swiften/Elements/WhiteboardPayload.h @@ -9,12 +9,14 @@ #include #include +#include namespace Swift { class WhiteboardPayload : public Payload { public: typedef boost::shared_ptr ref; + public: enum Type {Data, SessionRequest, SessionAccept, SessionTerminate}; WhiteboardPayload(Type type = WhiteboardPayload::Data) : type_(type) { @@ -36,8 +38,17 @@ namespace Swift { type_ = type; } + WhiteboardElement::ref getElement() const { + return element_; + } + + void setElement(WhiteboardElement::ref element) { + element_ = element; + } + private: std::string data_; Type type_; + WhiteboardElement::ref element_; }; } diff --git a/Swiften/Serializer/PayloadSerializers/WhiteboardSerializer.cpp b/Swiften/Serializer/PayloadSerializers/WhiteboardSerializer.cpp index 578b0ab..def21b1 100644 --- a/Swiften/Serializer/PayloadSerializers/WhiteboardSerializer.cpp +++ b/Swiften/Serializer/PayloadSerializers/WhiteboardSerializer.cpp @@ -5,13 +5,36 @@ */ #include -#include + +#include #include namespace Swift { + void WhiteboardElementSerializingVisitor::visit(const WhiteboardLineElement* line) { + element = boost::make_shared("line"); + element->setAttribute("x1", intToStr(line->x1())); + element->setAttribute("y1", intToStr(line->y1())); + element->setAttribute("x2", intToStr(line->x2())); + element->setAttribute("y2", intToStr(line->y2())); + } + + std::string WhiteboardElementSerializingVisitor::intToStr(const int t) { + std::stringstream ss; + ss << t; + return ss.str(); + } + + XMLElement::ref WhiteboardElementSerializingVisitor::getResult() const { + return element; + } + std::string WhiteboardSerializer::serializePayload(boost::shared_ptr payload) const { XMLElement element("wb"); - element.addNode(XMLTextNode::ref(new XMLTextNode(payload->getData()))); + if (payload->getType() == WhiteboardPayload::Data) { + WhiteboardElementSerializingVisitor visitor; + payload->getElement()->accept(visitor); + element.addNode(visitor.getResult()); + } element.setAttribute("type", typeToString(payload->getType())); return element.serialize(); } diff --git a/Swiften/Serializer/PayloadSerializers/WhiteboardSerializer.h b/Swiften/Serializer/PayloadSerializers/WhiteboardSerializer.h index 975d537..219ebcc 100644 --- a/Swiften/Serializer/PayloadSerializers/WhiteboardSerializer.h +++ b/Swiften/Serializer/PayloadSerializers/WhiteboardSerializer.h @@ -7,9 +7,23 @@ #pragma once #include +#include +#include #include +#include namespace Swift { + class WhiteboardElementSerializingVisitor : public WhiteboardElementVisitor { + public: + void visit(const WhiteboardLineElement* line); + XMLElement::ref getResult() const; + + private: + std::string intToStr(const int t); + + XMLElement::ref element; + }; + class WhiteboardSerializer : public GenericPayloadSerializer { public: std::string serializePayload(boost::shared_ptr payload) const; diff --git a/Swiften/Whiteboard/Elements/WhiteboardElement.h b/Swiften/Whiteboard/Elements/WhiteboardElement.h new file mode 100644 index 0000000..5095506 --- /dev/null +++ b/Swiften/Whiteboard/Elements/WhiteboardElement.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2012 Mateusz Piękos + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#pragma once + +#include +#include + +namespace Swift { + class WhiteboardElement { + public: + typedef boost::shared_ptr ref; + + public: + virtual ~WhiteboardElement() {} + virtual void accept(WhiteboardElementVisitor& visitor) = 0; + }; +} diff --git a/Swiften/Whiteboard/Elements/WhiteboardElementVisitor.h b/Swiften/Whiteboard/Elements/WhiteboardElementVisitor.h new file mode 100644 index 0000000..b5fd546 --- /dev/null +++ b/Swiften/Whiteboard/Elements/WhiteboardElementVisitor.h @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2012 Mateusz Piękos + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#pragma once + +namespace Swift { + class WhiteboardLineElement; + + class WhiteboardElementVisitor { + public: + virtual ~WhiteboardElementVisitor() {} + virtual void visit(const WhiteboardLineElement* /*element*/) = 0; + }; +} diff --git a/Swiften/Whiteboard/Elements/WhiteboardLineElement.h b/Swiften/Whiteboard/Elements/WhiteboardLineElement.h new file mode 100644 index 0000000..b64e397 --- /dev/null +++ b/Swiften/Whiteboard/Elements/WhiteboardLineElement.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2012 Mateusz Piękos + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#pragma once + +#include + +namespace Swift { + class WhiteboardLineElement : public WhiteboardElement { + public: + typedef boost::shared_ptr ref; + public: + WhiteboardLineElement(int x1, int y1, int x2, int y2) { + x1_ = x1; + y1_ = y1; + x2_ = x2; + y2_ = y2; + } + + int x1() const { + return x1_; + } + + int y1() const { + return y1_; + } + + int x2() const { + return x2_; + } + + int y2() const { + return y2_; + } + + void accept(WhiteboardElementVisitor& visitor) { + visitor.visit(this); + } + + private: + int x1_, y1_, x2_, y2_; + std::string id; + }; +} diff --git a/Swiften/Whiteboard/IncomingWhiteboardSession.h b/Swiften/Whiteboard/IncomingWhiteboardSession.h index 3b15a49..a0d0b49 100644 --- a/Swiften/Whiteboard/IncomingWhiteboardSession.h +++ b/Swiften/Whiteboard/IncomingWhiteboardSession.h @@ -4,12 +4,12 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ +#pragma once + #include #include -#pragma once - namespace Swift { class IncomingWhiteboardSession : public WhiteboardSession { public: diff --git a/Swiften/Whiteboard/WhiteboardSession.cpp b/Swiften/Whiteboard/WhiteboardSession.cpp index bfef7c3..9e4fd9f 100644 --- a/Swiften/Whiteboard/WhiteboardSession.cpp +++ b/Swiften/Whiteboard/WhiteboardSession.cpp @@ -42,6 +42,13 @@ namespace Swift { request->send(); } + void WhiteboardSession::sendElement(const WhiteboardElement::ref element) { + boost::shared_ptr payload = boost::make_shared(); + payload->setElement(element); + boost::shared_ptr > request = boost::make_shared >(IQ::Set, toJID_, payload, router_); + request->send(); + } + void WhiteboardSession::cancel() { boost::shared_ptr payload = boost::make_shared(WhiteboardPayload::SessionTerminate); boost::shared_ptr > request = boost::make_shared >(IQ::Set, toJID_, payload, router_); diff --git a/Swiften/Whiteboard/WhiteboardSession.h b/Swiften/Whiteboard/WhiteboardSession.h index 7aa67ca..fb4305c 100644 --- a/Swiften/Whiteboard/WhiteboardSession.h +++ b/Swiften/Whiteboard/WhiteboardSession.h @@ -11,6 +11,7 @@ #include #include #include +#include namespace Swift { class IQRouter; @@ -26,6 +27,7 @@ namespace Swift { virtual ~WhiteboardSession(); void handleIncomingAction(boost::shared_ptr payload); void sendData(const std::string& data); + void sendElement(const WhiteboardElement::ref element); void cancel(); const JID& getTo() const; -- cgit v0.10.2-6-g49f6