summaryrefslogtreecommitdiffstats
path: root/src/jabber_whiteboard/deserializer.h
blob: 715ca4c66ae529b85535a0afaeae81bb70c465a3 (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
/**
 * 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
 */

#ifndef __WHITEBOARD_MESSAGE_DESERIALIZER_H__
#define __WHITEBOARD_MESSAGE_DESERIALIZER_H__

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

#include "jabber_whiteboard/node-tracker-event-tracker.h"
#include "jabber_whiteboard/node-tracker.h"
#include "jabber_whiteboard/typedefs.h"

#include <functional>
#include <algorithm>
#include <glibmm.h>

namespace Inkscape {

namespace Whiteboard {

/**
 * A stateful XML::Event deserializer.
 *
 * The Deserializer class is meant to deserialize XML::Events serialized by 
 * Inkscape::Whiteboard::Serializer or a serializer that serializes
 * XML::Events into the same format.
 *
 * Usage is as follows:
 * <ol>
 * 	<li>For each serialized event called, call the appropriate deserialization method.</li>
 * 	<li>Detach the deserialized event.</li>
 * </ol>
 *
 * The deserializer does not actually modify any aspect of the document or node-tracking systems.
 * Methods are provided to provide the information necessary to perform the modifications outside
 * of the deserializer.
 */
class Deserializer {
public:
	/**
	 * Constructor.
	 *
	 * \param xnt The XMLNodeTracker that a Deserializer should use for retrieving 
	 * XML::Nodes based on string keys.
	 */
	Deserializer(XMLNodeTracker* xnt) : _xnt(xnt)
	{
		this->clearEventLog();
	}

	~Deserializer() { }

	/**
	 * Deserialize a node add event.
	 *
	 * \see XML::EventAdd
	 * \param msg The message that describes the event.
	 */
	void deserializeEventAdd(Glib::ustring const& msg);

	/**
	 * Deserialize a node remove event.
	 *
	 * \see XML::EventDel
	 * \param msg The message that describes the event.
	 */
	void deserializeEventDel(Glib::ustring const& msg);

	/**
	 * Deserialize a node order change event.
	 *
	 * \see XML::EventChgOrder
	 * \param msg The message that describes the event.
	 */
	void deserializeEventChgOrder(Glib::ustring const& msg);

	/**
	 * Deserialize a node content change event.
	 *
	 * \see XML::EventChgContent
	 * \param msg The message that describes the event.
	 */
	void deserializeEventChgContent(Glib::ustring const& msg);

	/**
	 * Deserialize a node attribute change event.
	 *
	 * \see XML::EventChgAttr
	 * \param msg The message that describes the event.
	 */
	void deserializeEventChgAttr(Glib::ustring const& msg);

	/**
	 * Retrieve the deserialized event log.
	 * This method does <b>not</b> clear the internal event log kept by the deserializer.
	 * To do that, use detachEventLog.
	 *
	 * \return The deserialized event log.
	 */
	XML::Event* getEventLog()
	{
		return this->_log;
	}

	/**
	 * Retrieve the deserialized event log and clear the internal event log kept by the deserializer.
	 *
	 * \return The deserialized event log.
	 */
	XML::Event* detachEventLog()
	{
		XML::Event* ret = this->_log;
		this->clearEventLog();
		return ret;
	}

	/**
	 * Clear the internal event log.
	 */
	void clearEventLog()
	{
		g_log(NULL, G_LOG_LEVEL_DEBUG, "Clearing event log");
		this->_log = NULL;
	}

	/**
	 * Retrieve a list of node entry actions (add node entry, remove node entry)
	 * that need to be performed on the XMLNodeTracker.
	 *
	 * Because this method returns a reference to a list, it is not safe for use 
	 * across multiple invocations of this Deserializer.
	 *
	 * \return A reference to a list of node entry actions generated while deserializing.
	 */
	KeyToNodeActionList& getNodeTrackerActions()
	{
		return this->_actions;
	}

	/**
	 * Retrieve a list of node entry actions (add node entry, remove node entry)
	 * that need to be performed on the XMLNodeTracker.
	 *
	 * \return A list of node entry actions generated while deserializing.
	 */
	KeyToNodeActionList getNodeTrackerActionsCopy()
	{
		return this->_actions;
	}

	/**
	 * Retrieve a set of nodes for which an EventChgAttr was deserialized.
	 *
	 * For some actions (i.e. text tool) it is necessary to call updateRepr() on
	 * the updated nodes.  This method provides the information required to perform
	 * that action.
	 *
	 * Because this method returns a reference to a set, it is not safe for use 
	 * across multiple invocations of this Deserializer.
	 *
	 * \return A reference to a set of nodes for which an EventChgAttr was deserialized.
	 */
	AttributesUpdatedSet& getUpdatedAttributeNodeSet()
	{
		return this->_updated;
	}

	/**
	 * Retrieve a set of nodes for which an EventChgAttr was deserialized.
	 *
	 * For some actions (i.e. text tool) it is necessary to call updateRepr() on
	 * the updated nodes.  This method provides the information required to perform
	 * that action.
	 *
	 * \return A set of nodes for which an EventChgAttr was deserialized.
	 */
	AttributesUpdatedSet getUpdatedAttributeNodeSetCopy()
	{
		return this->_updated;
	}

	/**
	 * Clear all internal node buffers.
	 */
	void clearNodeBuffers()
	{
		g_log(NULL, G_LOG_LEVEL_DEBUG, "Clearing deserializer node buffers");
		this->_newnodes.clear();
		this->_actions.clear();
		this->_newkeys.clear();
		this->_parent_child_map.clear();
		this->_updated.clear();
	}

	/**
	 * Clear all internal state.
	 */
	void reset() 
	{
		this->clearEventLog();
		this->clearNodeBuffers();
	}

private:
	XML::Node* _getNodeByID(std::string const& id)
	{
		KeyToNodeMap::iterator i = this->_newnodes.find(id);
		if (i != this->_newnodes.end()) {
			return const_cast< XML::Node* >(i->second);
		} else {
			if (this->_xnt->isTracking(id)) {
				return this->_xnt->get(id);
			} else {
				return NULL;
			}
		}
	}

	void _addOneEvent(XML::Event* event)
	{
		if (this->_log == NULL) {
			this->_log = event;
		} else {
			event->next = this->_log;
			this->_log = event;
		}
	}

	void _recursiveMarkForRemoval(XML::Node* node);

	// internal states with accessors:
	
	// node tracker actions (add node, remove node)
	KeyToNodeActionList _actions;

	// nodes that have had their attributes updated
	AttributesUpdatedSet _updated;

	// the deserialized event log
	XML::Event* _log;


	// for internal use:
	
	// These maps only store information on a single node.  That's fine, though;
	// all we care about is the ability to do key <-> node association.  The NodeTrackerEventTracker
	// and KeyToNodeActionList keep track of the actual actions we need to perform 
	// on the node tracker.
	NodeToKeyMap _newkeys;
	KeyToNodeMap _newnodes;
	NodeTrackerEventTracker _node_action_tracker;

	typedef std::map< XML::Node*, XML::Node* > _pc_map_type;
	_pc_map_type _parent_child_map;

	XMLNodeTracker* _xnt;

	XML::LogBuilder _builder;
};

}

}

#endif

/*
  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 :