summaryrefslogtreecommitdiffstats
path: root/src/jabber_whiteboard/deserializer.cpp
blob: 40047051e250f5e63b4e6c8bd3818671ea2bc7e7 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/**
 * Inkboard message -> XML::Event* deserializer
 *
 * Authors:
 * David Yip <yipdw@rose-hulman.edu>
 *
 * Copyright (c) 2005 Authors
 *
 * Released under GNU GPL, read the file 'COPYING' for more information
 */

#include "xml/log-builder.h"
#include "xml/event.h"
#include "xml/node.h"
#include "xml/repr.h"

#include "util/shared-c-string-ptr.h"

#include "gc-anchored.h"

#include "jabber_whiteboard/message-utilities.h"
#include "jabber_whiteboard/typedefs.h"
#include "jabber_whiteboard/node-utilities.h"
#include "jabber_whiteboard/node-tracker.h"
#include "jabber_whiteboard/deserializer.h"
#include <glibmm.h>

namespace Inkscape {

namespace Whiteboard {

void
Deserializer::deserializeEventAdd(Glib::ustring const& msg)
{
	// 1.  Extract required attributes: parent, child, node name, and node type.
	// If any of these cannot be found, return.
	std::string parent, child, prev;
	Glib::ustring name, type;

	struct Node buf;

	buf.tag = MESSAGE_PARENT;
	if (!MessageUtilities::findTag(buf, msg)) {
		return;
	}
	parent = buf.data.c_str();

	buf.tag = MESSAGE_CHILD;
	if (!MessageUtilities::findTag(buf, msg)) {
		return;
	}
	child = buf.data.c_str();

	KeyToNodeMap::iterator i = this->_newnodes.find(child);
	if (i != this->_newnodes.end()) {
		if (this->_node_action_tracker.getAction(i->second) == NODE_ADD) {
			return;
		}
	}

	buf.tag = MESSAGE_NAME;
	if (!MessageUtilities::findTag(buf, msg)) {
		return;
	} else {
		name = buf.data;
	}

	buf.tag = MESSAGE_NODETYPE;
	if (!MessageUtilities::findTag(buf, msg)) {
		return;
	} else {
		type = buf.data;
	}

	// 2.  Extract optional attributes: the node previous to the new child node.
	buf.tag = MESSAGE_REF;
	if (MessageUtilities::findTag(buf, msg)) {
		prev = buf.data.c_str();
	}

	// 3. Check if the received child node is a special node.  If it is, and we are already 
	// tracking it, then we should not add the node.
	if (this->_xnt->isSpecialNode(name)) {
		if (this->_xnt->isTracking(this->_xnt->getSpecialNodeKeyFromName(name))) {
			return;
		}
	}

	// 4.  Look up the parent node.  If we cannot find it, return.
	XML::Node* parentRepr = this->_getNodeByID(parent);
	if (parentRepr == NULL) {
		g_warning("Cannot find parent node identified by %s", parent.c_str());
		return; 
	}

	// 5.  Look up the node previous to the child, if it exists.
	// If we cannot find it, we may be in a change conflict situation.
	// In that case, just append the node.
	XML::Node* prevRepr = NULL;
	if (!prev.empty()) {
		prevRepr = this->_getNodeByID(prev);
		if (prevRepr == NULL) {
			g_warning("Prev node %s could not be found; appending incoming node.  Document may not be synchronized.", prev.c_str());
			prevRepr = parentRepr->lastChild();
		}
	}

	if (prevRepr) {
		if (prevRepr->parent() != parentRepr) {
			if (this->_parent_child_map[prevRepr] != parentRepr) {
				g_warning("ref mismatch on node %s: child=%s ref=%p parent=%p ref->parent()=%p", prev.c_str(), child.c_str(), prevRepr, parentRepr, prevRepr->parent());
				g_warning("parent_child_map[%p (%s)] = %p (%s)", prevRepr, this->_xnt->get(*prevRepr).c_str(), this->_parent_child_map[prevRepr], this->_xnt->get(*(this->_parent_child_map[prevRepr])).c_str());
				return;
			}
		}
	}

	XML::Node* childRepr = NULL;
	
	// 6.  Create the child node.

	switch (NodeUtilities::stringToNodeType(type)) {
		case XML::TEXT_NODE:
			buf.tag = MESSAGE_CONTENT;
			if (!MessageUtilities::findTag(buf, msg)) {
				childRepr = sp_repr_new_text("");
			} else {
				childRepr = sp_repr_new_text(buf.data.c_str());
			}
			break;
		case XML::DOCUMENT_NODE:
			// TODO
		case XML::COMMENT_NODE:
			buf.tag = MESSAGE_CONTENT;
			if (!MessageUtilities::findTag(buf, msg)) {
				childRepr = sp_repr_new_comment("");
			} else {
				childRepr = sp_repr_new_comment(buf.data.c_str()); 
			}
			break;
		case XML::ELEMENT_NODE: 
		default:
			childRepr = sp_repr_new(name.data());
			break;
	}


	this->_actions.push_back(SerializedEventNodeAction(KeyNodePair(child, childRepr), NODE_ADD));
	this->_newnodes[child] = childRepr;
	this->_newkeys[childRepr] = child;


	// 8.  Deserialize the event.
	this->_builder.addChild(*parentRepr, *childRepr, prevRepr);
	this->_parent_child_map.erase(childRepr);
	this->_parent_child_map[childRepr] = parentRepr;
	this->_addOneEvent(this->_builder.detach());
	Inkscape::GC::release(childRepr);
}

void
Deserializer::deserializeEventDel(Glib::ustring const& msg)
{
	// 1.  Extract required attributes: parent, child.  If we do not know these,
	// return.
	std::string parent, child, prev;

	struct Node buf;

	buf.tag = MESSAGE_PARENT;
	if (!MessageUtilities::findTag(buf, msg)) {
		return;
	}
	parent = buf.data.c_str();

	buf.tag = MESSAGE_CHILD;
	if (!MessageUtilities::findTag(buf, msg)) {
		return;
	}
	child = buf.data.c_str();

	KeyToNodeMap::iterator i = this->_newnodes.find(child);
	if (i != this->_newnodes.end()) {
		if (this->_node_action_tracker.getAction(i->second) == NODE_REMOVE) {
			return;
		}
	}

	// 2.  Extract optional attributes: previous node.
	buf.tag = MESSAGE_REF;
	if (MessageUtilities::findTag(buf, msg)) {
		prev = buf.data.c_str(); 
	}

	// 3.  Retrieve nodes.  If we cannot find all nodes involved, return.
	XML::Node* parentRepr = this->_getNodeByID(parent);
	XML::Node* childRepr = this->_getNodeByID(child);
	XML::Node* prevRepr = NULL;
	if (!prev.empty()) {
		prevRepr = this->_getNodeByID(prev);
		if (prevRepr == NULL) {
			return;
		}
	}

	// 4.  Deserialize the event.
	if (parentRepr && childRepr) {
		if (childRepr->parent() == parentRepr || this->_parent_child_map[childRepr] == parentRepr) {
//			this->_actions.push_back(SerializedEventNodeAction(KeyNodePair(child, childRepr), NODE_REMOVE));
			this->_builder.removeChild(*parentRepr, *childRepr, prevRepr);
			this->_parent_child_map.erase(childRepr);
			this->_addOneEvent(this->_builder.detach());

			// 5.  Mark the removed node and all its children for removal from the tracker.
			this->_recursiveMarkForRemoval(childRepr);
		} else {
			g_warning("child->parent() == parent mismatch on child=%s (%p), parent=%s: parent=%p child->parent()=%p", child.c_str(), childRepr, parent.c_str(), parentRepr, childRepr->parent());
			g_warning("parent_child_map[%p] = %p", childRepr, this->_parent_child_map[childRepr]);
		}
	} else {
		g_warning("Missing parentRepr and childRepr: parent=%p, child=%p", parentRepr, childRepr);
	}
}

void
Deserializer::deserializeEventChgOrder(Glib::ustring const& msg)
{
	// 1.  Extract required attributes: node ID, parent ID, new previous node ID.
	// If we do not know these, return.
	std::string id, parentid, oldprevid, newprevid;
	Node buf;

	buf.tag = MESSAGE_ID;
	if (MessageUtilities::findTag(buf, msg)) {
		id = buf.data.raw();
	} else {
		return;
	}

	buf.tag = MESSAGE_PARENT;
	if (MessageUtilities::findTag(buf, msg)) {
		parentid = buf.data.raw();
	} else {
		return;
	}

	// 2.  Extract optional attributes: old previous node, new previous node.
	buf.tag = MESSAGE_OLDVAL;
	if (MessageUtilities::findTag(buf, msg)) {
		oldprevid = buf.data.raw();
	}

	buf.tag = MESSAGE_NEWVAL;
	if (MessageUtilities::findTag(buf, msg)) {
		newprevid = buf.data.raw();
	} else {
		return;
	}

	// 3.  Find the identified nodes.  If we do not know about the parent and child, return.
	XML::Node* node = this->_getNodeByID(id);
	XML::Node* parent = this->_getNodeByID(parentid);
	XML::Node* oldprev = NULL;
	XML::Node* newprev = NULL;
	if (!oldprevid.empty()) {
		oldprev = this->_getNodeByID(oldprevid);
	}

	if (!newprevid.empty()) {
		newprev = this->_getNodeByID(newprevid);
	}

	if (parent && node) {
		// 4.  Deserialize the event.
		this->_builder.setChildOrder(*parent, *node, oldprev, newprev);
		this->_addOneEvent(this->_builder.detach());
	} else {
		return;
	}
}

void
Deserializer::deserializeEventChgContent(Glib::ustring const& msg)
{
	// 1.  Extract required attributes: node ID.  If we do not know these, return.
	std::string id;
	Util::SharedCStringPtr oldval, newval;
	Node buf;

	buf.tag = MESSAGE_ID;
	if (!MessageUtilities::findTag(buf, msg)) {
		return;
	} else {
		id = buf.data.raw();
	}

	// 2.  Extract optional attributes: old value, new value.
	buf.tag = MESSAGE_OLDVAL;
	if (MessageUtilities::findTag(buf, msg)) {
		oldval = Util::SharedCStringPtr::copy(buf.data.c_str());
	} else {
		oldval = Util::SharedCStringPtr::copy("");
	}

	buf.tag = MESSAGE_NEWVAL;
	if (MessageUtilities::findTag(buf, msg)) {
		newval = Util::SharedCStringPtr::copy(buf.data.c_str());
	} else {
		newval = Util::SharedCStringPtr::copy("");
	}

	// 3.  Find the node identified by the ID.  If we cannot find it, return.
	XML::Node* node = this->_getNodeByID(id);
	if (node == NULL) {
		return;
	}

	// 4.  Deserialize the event.
	this->_builder.setContent(*node, oldval, newval);
	this->_addOneEvent(this->_builder.detach());
}

void
Deserializer::deserializeEventChgAttr(Glib::ustring const& msg)
{
	// 1.  Extract required attributes: node ID, attribute key.  If we do not know these,
	// return.

	struct Node buf;
	buf.tag = MESSAGE_ID;
	if (!MessageUtilities::findTag(buf, msg)) {
		return;
	}

	std::string id = buf.data.data();

	buf.tag = MESSAGE_KEY;
	if (!MessageUtilities::findTag(buf, msg)) {
		return;
	}

	Glib::ustring key = buf.data;

	// 2.  Extract optional attributes: new value.  If we do not find it in the message,
	// assume there is no new value.
	buf.tag = MESSAGE_NEWVAL;
	Util::SharedCStringPtr newval;
	if (MessageUtilities::findTag(buf, msg)) {
		newval = Util::SharedCStringPtr::copy(buf.data.c_str());
	} else {
		newval = Util::SharedCStringPtr::copy("");
	}

	// 3.  Extract optional attributes: old value.  If we do not find it in the message,
	// assume that there is no old value.
	buf.tag = MESSAGE_OLDVAL;
	Util::SharedCStringPtr oldval;
	if (MessageUtilities::findTag(buf, msg)) {
		oldval = Util::SharedCStringPtr::copy(buf.data.c_str());
	} else {
		oldval = Util::SharedCStringPtr::copy("");
	}

	// 4.  Look up this node in the local node database and external tracker.
	// If it cannot be found, return.
	XML::Node* node = this->_getNodeByID(id);
	if (node == NULL) {
		g_warning("Could not find node %s on which to change attribute", id.c_str());
		return;
	}

	// 5.  If this node is in the actions queue and is marked as "new", we need to apply
	// _all_ received attributes to it _before_ adding it to the document tree.
	if (this->_newnodes.find(id) != this->_newnodes.end()) {
		node->setAttribute(key.c_str(), newval.cString());	
	}

	// 6.  Deserialize the event.
	this->_builder.setAttribute(*node, g_quark_from_string(key.c_str()), oldval, newval);
	this->_updated.insert(node);
	this->_addOneEvent(this->_builder.detach());
}

void 
Deserializer::_recursiveMarkForRemoval(XML::Node* node)
{
	if (node != NULL) {
		NodeToKeyMap::iterator i = this->_newkeys.find(node);
		if (i == this->_newkeys.end()) {
			std::string id = this->_xnt->get(*node);
			if (!id.empty()) {
				this->_actions.push_back(SerializedEventNodeAction(KeyNodePair(id, node), NODE_REMOVE));
			}
		} else {
			this->_actions.push_back(SerializedEventNodeAction(KeyNodePair((*i).second, node), NODE_REMOVE));
		}

		for (XML::Node* child = node->firstChild(); child; child = child->next()) {
			this->_recursiveMarkForRemoval(child);
		}
	}
}

}

}

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :