From a8f57139d663a176af87d7bc71dd85c62f6274cf Mon Sep 17 00:00:00 2001
From: Mateusz Piekos <mateuszpiekos@gmail.com>
Date: Fri, 29 Jun 2012 09:32:51 +0200
Subject: Improved handling of IDs

Added ID parsing to every element and added function to get prefixes from
session to be able to distinguish elements between clients

diff --git a/Swift/QtUI/Whiteboard/GView.cpp b/Swift/QtUI/Whiteboard/GView.cpp
index 588f369..fe0e5f9 100644
--- a/Swift/QtUI/Whiteboard/GView.cpp
+++ b/Swift/QtUI/Whiteboard/GView.cpp
@@ -55,6 +55,14 @@ namespace Swift {
 		items_.insert(id, item);
 	}
 
+	void GView::setIDPrefix(QString prefix) {
+		idPrefix = prefix;
+	}
+
+	QString GView::getNewID() {
+		return idPrefix + P2QSTRING(idGenerator.generateID());
+	}
+
 	void GView::mouseMoveEvent(QMouseEvent* event)
 	{
 		if (!mousePressed) {
@@ -168,20 +176,20 @@ namespace Swift {
 		if (mode == Line) {
 			QPointF point = this->mapToScene(event->pos());
 			QGraphicsItem* item = scene()->addLine(point.x(), point.y(), point.x(), point.y(), pen);
-			QString id = P2QSTRING(idGenerator.generateID());
+			QString id = getNewID();
 			items_.insert(id, item);
 			item->setZValue(zValue++);
-			item->setData(0, id);
+			item->setData(100, id);
 			lastItem = item;
 		}
 		else if (mode == Rect) {
 			QPointF point = this->mapToScene(event->pos());
 			QGraphicsRectItem* item = scene()->addRect(point.x(), point.y(), 0, 0, pen, brush);
-			QString id = P2QSTRING(idGenerator.generateID());
+			QString id = getNewID();
 			items_.insert(id, item);
 			item->setZValue(zValue++);
 			item->setData(0, point);
-			item->setData(1, id);
+			item->setData(100, id);
 			lastItem = item;
 		}
 		else if (mode == Rubber) {
@@ -199,22 +207,22 @@ namespace Swift {
 		else if (mode == Circle) {
 			QPointF point = this->mapToScene(event->pos());
 			QGraphicsEllipseItem* item = scene()->addEllipse(point.x(), point.y(), 0, 0, pen, brush);
-			QString id = P2QSTRING(idGenerator.generateID());
+			QString id = getNewID();
 			items_.insert(id, item);
 			item->setZValue(zValue++);
 			item->setData(0, point);
-			item->setData(1, id);
+			item->setData(100, id);
 			lastItem = item;
 		}
 		else if (mode == HandLine) {
 			QPointF point = this->mapToScene(event->pos());
 			FreehandLineItem* item = new FreehandLineItem;
-			QString id = P2QSTRING(idGenerator.generateID());
+			QString id = getNewID();
 			items_.insert(id, item);
 			item->setPen(pen);
 			item->setStartPoint(point);
 			item->setZValue(zValue++);
-			item->setData(0, id);
+			item->setData(100, id);
 			scene()->addItem(item);
 			lastItem = item;
 		}
@@ -222,20 +230,20 @@ namespace Swift {
 			QPointF point = this->mapToScene(event->pos());
 			QPainterPath path;
 			QGraphicsPathItem* item = scene()->addPath(path, pen, brush);
-			QString id = P2QSTRING(idGenerator.generateID());
+			QString id = getNewID();
 			items_.insert(id, item);
 			item->setZValue(zValue++);
 			item->setData(0, point);
-			item->setData(1, id);
+			item->setData(100, id);
 			lastItem = item;
 		}
 		else if (mode == Text) {
 			QPointF point = this->mapToScene(event->pos());
 			QGraphicsTextItem* item = scene()->addText("");
-			QString id = P2QSTRING(idGenerator.generateID());
+			QString id = getNewID();
 			items_.insert(id, item);
 			item->setZValue(zValue++);
-			item->setData(0, id);
+			item->setData(100, id);
 			item->setDefaultTextColor(pen.color());
 			textDialog = new TextDialog(item, this);
 			connect(textDialog, SIGNAL(accepted(QGraphicsItem*)), this, SIGNAL(lastItemChanged(QGraphicsItem*)));
@@ -252,10 +260,10 @@ namespace Swift {
 				polygon.append(point);
 				polygon.append(point);
 				item = scene()->addPolygon(polygon, pen, brush);
-				QString id = P2QSTRING(idGenerator.generateID());
+				QString id = getNewID();
 				items_.insert(id, item);
 				item->setZValue(zValue++);
-				item->setData(0, id);
+				item->setData(100, id);
 				lastItem = item;
 			}
 			else {
diff --git a/Swift/QtUI/Whiteboard/GView.h b/Swift/QtUI/Whiteboard/GView.h
index 8b416a1..4830381 100644
--- a/Swift/QtUI/Whiteboard/GView.h
+++ b/Swift/QtUI/Whiteboard/GView.h
@@ -33,12 +33,15 @@ namespace Swift {
 		void mouseReleaseEvent(QMouseEvent* event);
 		QGraphicsItem* getItem(const QString id) const;
 		void addItem(QGraphicsItem* item, QString id);
+		void setIDPrefix(QString prefix);
 
 	public slots:
 		void moveUpSelectedItem();
 		void moveDownSelectedItem();
 
 	private:
+		QString getNewID();
+
 		int zValue;
 		bool mousePressed;
 		QPen pen;
@@ -47,8 +50,9 @@ namespace Swift {
 		QGraphicsItem* lastItem;
 		QGraphicsRectItem* selectionRect;
 		TextDialog* textDialog;
-		QHash<QString, QGraphicsItem*> items_;
+		QMap<QString, QGraphicsItem*> items_;
 		SimpleIDGenerator idGenerator;
+		QString idPrefix;
 
 	signals:
 		void lastItemChanged(QGraphicsItem* item);
diff --git a/Swift/QtUI/Whiteboard/QtWhiteboardWindow.cpp b/Swift/QtUI/Whiteboard/QtWhiteboardWindow.cpp
index 6e259a2..9aa6eb0 100644
--- a/Swift/QtUI/Whiteboard/QtWhiteboardWindow.cpp
+++ b/Swift/QtUI/Whiteboard/QtWhiteboardWindow.cpp
@@ -205,6 +205,7 @@ namespace Swift {
 
 	void QtWhiteboardWindow::setSession(WhiteboardSession::ref session) {
 		whiteboardSession_ = session;
+		graphicsView->setIDPrefix(P2QSTRING(whiteboardSession_->getClientID()));
 		whiteboardSession_->onElementReceived.connect(boost::bind(&QtWhiteboardWindow::handleWhiteboardElementReceive, this, _1));
 		whiteboardSession_->onRequestAccepted.connect(boost::bind(&QWidget::show, this));
 		whiteboardSession_->onSessionTerminateReceived.connect(boost::bind(&QtWhiteboardWindow::handleSessionTerminate, this));
@@ -224,7 +225,7 @@ namespace Swift {
 			element->setColor(Color(color.red(), color.green(), color.blue(), color.alpha()));
 			element->setPenWidth(lineItem->pen().width());
 
-			element->setID(lineItem->data(0).toString().toStdString());
+			element->setID(lineItem->data(100).toString().toStdString());
 			whiteboardSession_->sendElement(element);
 
 		}
@@ -242,7 +243,7 @@ namespace Swift {
 			element->setPenWidth(freehandLineItem->pen().width());
 			element->setPoints(points);
 
-			element->setID(freehandLineItem->data(0).toString().toStdString());
+			element->setID(freehandLineItem->data(100).toString().toStdString());
 			whiteboardSession_->sendElement(element);
 		}
 
@@ -257,7 +258,7 @@ namespace Swift {
 			element->setPenColor(Color(penColor.red(), penColor.green(), penColor.blue(), penColor.alpha()));
 			element->setPenWidth(rectItem->pen().width());
 
-			element->setID(rectItem->data(1).toString().toStdString());
+			element->setID(rectItem->data(100).toString().toStdString());
 			whiteboardSession_->sendElement(element);
 		}
 
@@ -270,7 +271,7 @@ namespace Swift {
 			QColor color = textItem->defaultTextColor();
 			element->setColor(Color(color.red(), color.green(), color.blue(), color.alpha()));
 
-			element->setID(rectItem->data(0).toString().toStdString());
+			element->setID(rectItem->data(100).toString().toStdString());
 			whiteboardSession_->sendElement(element);
 		}
 
@@ -292,7 +293,7 @@ namespace Swift {
 			element->setBrushColor(Color(brushColor.red(), brushColor.green(), brushColor.blue(), brushColor.alpha()));
 			element->setPenWidth(polygonItem->pen().width());
 
-			element->setID(polygonItem->data(0).toString().toStdString());
+			element->setID(polygonItem->data(100).toString().toStdString());
 			whiteboardSession_->sendElement(element);
 		}
 
@@ -311,7 +312,7 @@ namespace Swift {
 			element->setBrushColor(Color(brushColor.red(), brushColor.green(), brushColor.blue(), brushColor.alpha()));
 			element->setPenWidth(ellipseItem->pen().width());
 
-			element->setID(ellipseItem->data(1).toString().toStdString());
+			element->setID(ellipseItem->data(100).toString().toStdString());
 			whiteboardSession_->sendElement(element);
 		}
 	}
diff --git a/Swift/QtUI/Whiteboard/WhiteboardElementDrawingVisitor.h b/Swift/QtUI/Whiteboard/WhiteboardElementDrawingVisitor.h
index f3d2526..38ed676 100644
--- a/Swift/QtUI/Whiteboard/WhiteboardElementDrawingVisitor.h
+++ b/Swift/QtUI/Whiteboard/WhiteboardElementDrawingVisitor.h
@@ -27,7 +27,9 @@ namespace Swift {
 			pen.setColor(QColor(color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha()));
 			pen.setWidth(element.getPenWidth());
 			item->setPen(pen);
-			graphicsView_->scene()->addItem(item);
+			QString id = P2QSTRING(element.getID());
+			item->setData(100, id);
+			graphicsView_->addItem(item, id);
 		}
 
 		void visit(WhiteboardFreehandPathElement& element) {
@@ -44,7 +46,9 @@ namespace Swift {
 				item->lineTo(QPointF(it->first, it->second));
 			}
 
-			graphicsView_->scene()->addItem(item);
+			QString id = P2QSTRING(element.getID());
+			item->setData(100, id);
+			graphicsView_->addItem(item, id);
 		}
 
 		void visit(WhiteboardRectElement& element) {
@@ -58,7 +62,9 @@ namespace Swift {
 			brush.setColor(QColor(brushColor.getRed(), brushColor.getGreen(), brushColor.getBlue(), brushColor.getAlpha()));
 			item->setPen(pen);
 			item->setBrush(brush);
-			graphicsView_->scene()->addItem(item);
+			QString id = P2QSTRING(element.getID());
+			item->setData(100, id);
+			graphicsView_->addItem(item, id);
 		}
 
 		void visit(WhiteboardPolygonElement& element) {
@@ -74,7 +80,9 @@ namespace Swift {
 				brush.setColor(QColor(brushColor.getRed(), brushColor.getGreen(), brushColor.getBlue(), brushColor.getAlpha()));
 				item->setPen(pen);
 				item->setBrush(brush);
-				graphicsView_->addItem(item, P2QSTRING(element.getID()));
+				QString id = P2QSTRING(element.getID());
+				item->setData(100, id);
+				graphicsView_->addItem(item, id);
 			}
 			QPolygonF polygon;
 			std::vector<std::pair<int, int> >::const_iterator it = element.getPoints().begin();
@@ -92,7 +100,10 @@ namespace Swift {
 			item->setFont(font);
 			Color color = element.getColor();
 			item->setDefaultTextColor(QColor(color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha()));
-			graphicsView_->scene()->addItem(item);
+
+			QString id = P2QSTRING(element.getID());
+			item->setData(100, id);
+			graphicsView_->addItem(item, id);
 		}
 
 		void visit(WhiteboardEllipseElement& element) {
@@ -111,7 +122,9 @@ namespace Swift {
 			brush.setColor(QColor(brushColor.getRed(), brushColor.getGreen(), brushColor.getBlue(), brushColor.getAlpha()));
 			item->setPen(pen);
 			item->setBrush(brush);
-			graphicsView_->scene()->addItem(item);
+			QString id = P2QSTRING(element.getID());
+			item->setData(100, id);
+			graphicsView_->addItem(item, id);
 		}
 
 	private:
diff --git a/Swiften/Parser/PayloadParsers/WhiteboardParser.cpp b/Swiften/Parser/PayloadParsers/WhiteboardParser.cpp
index 0477988..0f2734c 100644
--- a/Swiften/Parser/PayloadParsers/WhiteboardParser.cpp
+++ b/Swiften/Parser/PayloadParsers/WhiteboardParser.cpp
@@ -48,7 +48,7 @@ namespace Swift {
 				} catch (boost::bad_lexical_cast&) {
 				}
 				whiteboardElement->setPenWidth(penWidth);
-
+				whiteboardElement->setID(attributes.getAttributeValue("id").get_value_or(""));
 				getPayloadInternal()->setElement(whiteboardElement);
 			} else if (element == "path") {
 				WhiteboardFreehandPathElement::ref whiteboardElement = boost::make_shared<WhiteboardFreehandPathElement>();
@@ -96,7 +96,7 @@ namespace Swift {
 				Color color(attributes.getAttributeValue("stroke").get_value_or("#000000"));
 				color.setAlpha(opacityToAlpha(attributes.getAttributeValue("opacity").get_value_or("1")));
 				whiteboardElement->setColor(color);
-
+				whiteboardElement->setID(attributes.getAttributeValue("id").get_value_or(""));
 				getPayloadInternal()->setElement(whiteboardElement);
 			} else if (element == "rect") {
 				int x = 0;
@@ -126,7 +126,7 @@ namespace Swift {
 				brushColor.setAlpha(opacityToAlpha(attributes.getAttributeValue("fill-opacity").get_value_or("1")));
 				whiteboardElement->setPenColor(penColor);
 				whiteboardElement->setBrushColor(brushColor);
-
+				whiteboardElement->setID(attributes.getAttributeValue("id").get_value_or(""));
 				getPayloadInternal()->setElement(whiteboardElement);
 			} else if (element == "polygon") {
 				WhiteboardPolygonElement::ref whiteboardElement = boost::make_shared<WhiteboardPolygonElement>();
@@ -188,7 +188,7 @@ namespace Swift {
 				} catch (boost::bad_lexical_cast&) {
 				}
 				whiteboardElement->setSize(fontSize);
-
+				whiteboardElement->setID(attributes.getAttributeValue("id").get_value_or(""));
 				getPayloadInternal()->setElement(whiteboardElement);
 			} else if (element == "ellipse") {
 				int cx = 0;
@@ -218,7 +218,7 @@ namespace Swift {
 				brushColor.setAlpha(opacityToAlpha(attributes.getAttributeValue("fill-opacity").get_value_or("1")));
 				whiteboardElement->setPenColor(penColor);
 				whiteboardElement->setBrushColor(brushColor);
-
+				whiteboardElement->setID(attributes.getAttributeValue("id").get_value_or(""));
 				getPayloadInternal()->setElement(whiteboardElement);
 			}
 		}
diff --git a/Swiften/Whiteboard/IncomingWhiteboardSession.cpp b/Swiften/Whiteboard/IncomingWhiteboardSession.cpp
index 92a86a1..5cdba1a 100644
--- a/Swiften/Whiteboard/IncomingWhiteboardSession.cpp
+++ b/Swiften/Whiteboard/IncomingWhiteboardSession.cpp
@@ -19,4 +19,8 @@ namespace Swift {
 		boost::shared_ptr<GenericRequest<WhiteboardPayload> > request = boost::make_shared<GenericRequest<WhiteboardPayload> >(IQ::Set, toJID_, payload, router_);
 		request->send();
 	}
+
+	std::string IncomingWhiteboardSession::getClientID() const {
+		return "b";
+	}
 }
diff --git a/Swiften/Whiteboard/IncomingWhiteboardSession.h b/Swiften/Whiteboard/IncomingWhiteboardSession.h
index a0d0b49..9971d75 100644
--- a/Swiften/Whiteboard/IncomingWhiteboardSession.h
+++ b/Swiften/Whiteboard/IncomingWhiteboardSession.h
@@ -20,5 +20,6 @@ namespace Swift {
 		~IncomingWhiteboardSession();
 
 		void accept();
+		std::string getClientID() const;
 	};
 }
diff --git a/Swiften/Whiteboard/OutgoingWhiteboardSession.cpp b/Swiften/Whiteboard/OutgoingWhiteboardSession.cpp
index 2693fcd..585a689 100644
--- a/Swiften/Whiteboard/OutgoingWhiteboardSession.cpp
+++ b/Swiften/Whiteboard/OutgoingWhiteboardSession.cpp
@@ -21,4 +21,8 @@ namespace Swift {
 		boost::shared_ptr<GenericRequest<WhiteboardPayload> > request = boost::make_shared<GenericRequest<WhiteboardPayload> >(IQ::Set, toJID_, payload, router_);
 		request->send();
 	}
+
+	std::string OutgoingWhiteboardSession::getClientID() const {
+		return "a";
+	}
 }
diff --git a/Swiften/Whiteboard/OutgoingWhiteboardSession.h b/Swiften/Whiteboard/OutgoingWhiteboardSession.h
index dfbd200..3d0dd1a 100644
--- a/Swiften/Whiteboard/OutgoingWhiteboardSession.h
+++ b/Swiften/Whiteboard/OutgoingWhiteboardSession.h
@@ -20,5 +20,6 @@ namespace Swift {
 		OutgoingWhiteboardSession(const JID& jid, IQRouter* router);
 		virtual ~OutgoingWhiteboardSession();
 		void startSession();
+		std::string getClientID() const;
 	};
 }
diff --git a/Swiften/Whiteboard/WhiteboardSession.cpp b/Swiften/Whiteboard/WhiteboardSession.cpp
index 18dea52..d9dc592 100644
--- a/Swiften/Whiteboard/WhiteboardSession.cpp
+++ b/Swiften/Whiteboard/WhiteboardSession.cpp
@@ -35,13 +35,6 @@ namespace Swift {
 		}
 	}
 
-	void WhiteboardSession::sendData(const std::string& data) {
-		boost::shared_ptr<WhiteboardPayload> payload = boost::make_shared<WhiteboardPayload>();
-		payload->setData(data);
-		boost::shared_ptr<GenericRequest<WhiteboardPayload> > request = boost::make_shared<GenericRequest<WhiteboardPayload> >(IQ::Set, toJID_, payload, router_);
-		request->send();
-	}
-
 	void WhiteboardSession::sendElement(const WhiteboardElement::ref element) {
 		boost::shared_ptr<WhiteboardPayload> payload = boost::make_shared<WhiteboardPayload>();
 		payload->setElement(element);
diff --git a/Swiften/Whiteboard/WhiteboardSession.h b/Swiften/Whiteboard/WhiteboardSession.h
index e7deb85..d3e435a 100644
--- a/Swiften/Whiteboard/WhiteboardSession.h
+++ b/Swiften/Whiteboard/WhiteboardSession.h
@@ -26,10 +26,10 @@ namespace Swift {
 		WhiteboardSession(const JID& jid, IQRouter* router);
 		virtual ~WhiteboardSession();
 		void handleIncomingAction(boost::shared_ptr<WhiteboardPayload> payload);
-		void sendData(const std::string& data);
 		void sendElement(const WhiteboardElement::ref element);
 		void cancel();
 		const JID& getTo() const;
+		virtual std::string getClientID() const = 0;
 
 	public:
 		boost::signal< void(const WhiteboardElement::ref element)> onElementReceived;
-- 
cgit v0.10.2-6-g49f6