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

#include <Swiften/Whiteboard/WhiteboardServer.h>
#include <Swiften/Whiteboard/WhiteboardTransformer.h>

namespace Swift {
	void WhiteboardServer::handleLocalOperationReceived(WhiteboardOperation::ref operation) {
		operations_.push_back(operation);
	}

	WhiteboardOperation::ref WhiteboardServer::handleClientOperationReceived(WhiteboardInsertOperation::ref newOperation) {
		std::list<WhiteboardOperation::ref>::reverse_iterator it;
		if (operations_.size() == 0 || newOperation->getParentID() == operations_.back()->getID()) {
			operations_.push_back(newOperation);
			return newOperation;
		}
		for (it = operations_.rbegin(); it != operations_.rend(); ++it) {
			WhiteboardOperation::ref operation = *it;
			while (newOperation->getParentID() == operation->getParentID()) {
				WhiteboardInsertOperation::ref insertOperation = boost::dynamic_pointer_cast<WhiteboardInsertOperation>(operation);
				if (insertOperation) {
					std::pair<WhiteboardInsertOperation::ref, WhiteboardInsertOperation::ref> tResult = WhiteboardTransformer::transform(newOperation, insertOperation);

					if (it == operations_.rbegin()) {
						operations_.push_back(tResult.second);
						return tResult.second;
					} else {
						newOperation = tResult.second;
						--it;
						operation = *it;
					}
				} else {
					operations_.push_back(operation);
					return *it;
				}
			}
		}
		return WhiteboardOperation::ref();
	}
}