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
|
/**
* Inkboard message -> XML::Event* serializer
*
* Authors:
* David Yip <yipdw@rose-hulman.edu>
*
* Copyright (c) 2005 Authors
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#include "xml/attribute-record.h"
#include "jabber_whiteboard/serializer.h"
#include "util/list.h"
#include "util/share.h"
#include "jabber_whiteboard/message-utilities.h"
#include "jabber_whiteboard/message-tags.h"
#include "jabber_whiteboard/typedefs.h"
#include "jabber_whiteboard/node-tracker.h"
#include "jabber_whiteboard/node-utilities.h"
#include "jabber_whiteboard/node-tracker-observer.h"
namespace Inkscape {
namespace Whiteboard {
void
Serializer::notifyChildAdded(XML::Node& node, XML::Node& child, XML::Node* prev)
{
// do not recurse upon initial notification
this->_newObjectEventHelper(node, child, prev, false);
this->_nn.insert(&child);
}
void
Serializer::_newObjectEventHelper(XML::Node& node, XML::Node& child, XML::Node* prev, bool recurse)
{
// 1. Check if we are tracking the parent node,
// and issue it an ID if we are not.
std::string parentid = this->_findOrGenerateNodeID(node);
// 2. Check if the child node is a special node.
// Special nodes are nodes that should appear only once in a document.
// If it is, we do not want to generate a new ID for the child; we will use
// the existing ID. Otherwise, we will generate a new ID for it, since we
// have not yet seen it.
std::string childid;
if (this->_xnt->isSpecialNode(child.name())) {
childid = this->_xnt->get(child);
} else {
// If the child id already exists in the new node buffer, then we've already seen it.
if (!this->actions.tryToTrack(&child, NODE_ADD)) {
return;
} else {
childid = this->_xnt->generateKey();
// childid = this->_findOrGenerateNodeID(child);
}
}
// 3. Find this node's previous node, and, if it has one, retrieve its ID.
std::string previd;
if (prev) {
previd = this->_findOrGenerateNodeID(*prev);
}
// 4. Serialize.
Glib::ustring childmsg = MessageUtilities::makeTagWithContent(MESSAGE_CHILD, childid);
Glib::ustring parentmsg = MessageUtilities::makeTagWithContent(MESSAGE_PARENT, parentid);
Glib::ustring namemsg = MessageUtilities::makeTagWithContent(MESSAGE_NAME, child.name());
Glib::ustring nodetype = MessageUtilities::makeTagWithContent(MESSAGE_NODETYPE, NodeUtilities::nodeTypeToString(child));
Glib::ustring prevmsg;
if (!previd.empty()) {
prevmsg = MessageUtilities::makeTagWithContent(MESSAGE_REF, previd);
}
Glib::ustring buf = MessageUtilities::makeTagWithContent(MESSAGE_NEWOBJ, childmsg + parentmsg + prevmsg + namemsg + nodetype);
this->_events.push_back(buf);
// 5. Add the child node to the new nodes buffers.
this->newnodes.push_back(SerializedEventNodeAction(KeyNodePair(childid, &child), NODE_ADD));
this->newkeys[&child] = childid;
this->_parent_child_map[&child] = &node;
// 6. Scan attributes and content.
Inkscape::Util::List<Inkscape::XML::AttributeRecord const> attrlist = child.attributeList();
for(; attrlist; attrlist++) {
this->notifyAttributeChanged(child, attrlist->key, Util::shared_ptr<char>(), attrlist->value);
}
if (child.content()) {
this->notifyContentChanged(child, Util::shared_ptr<char>(), Util::share_string(child.content()));
}
this->_attributes_scanned.insert(childid);
// 7. Repeat this process for each child of this child.
if (recurse && child.childCount() > 0) {
XML::Node* prev = child.firstChild();
for(XML::Node* ch = child.firstChild(); ch; ch = ch->next()) {
if (ch == child.firstChild()) {
// No prev node in this case.
this->_newObjectEventHelper(child, *ch, NULL, true);
} else {
this->_newObjectEventHelper(child, *ch, prev, true);
prev = ch;
}
}
}
return;
}
void
Serializer::notifyChildRemoved(XML::Node& node, XML::Node& child, XML::Node* prev)
{
// 1. Get the ID of the child.
std::string childid;
_pc_map_type::iterator i = this->_parent_child_map.find(&child);
if (i != this->_parent_child_map.end() && i->second != &node) {
// Don't look in local! Go for the tracker.
childid = this->_xnt->get(child);
} else if (i == this->_parent_child_map.end()) {
childid = this->_findOrGenerateNodeID(child);
} else if (i->second == &node) {
childid = this->_findOrGenerateNodeID(child);
this->_parent_child_map.erase(i);
} else {
childid = this->_findOrGenerateNodeID(child);
}
// 2. Double-deletes don't make any sense. If we've seen this node already and if it's
// marked for deletion, return.
if (!this->actions.tryToTrack(&child, NODE_REMOVE)) {
return;
} else {
// 2a. Although we do not have to remove all child nodes of this subtree,
// we _do_ have to mark each child node as deleted.
this->_recursiveMarkAsRemoved(child);
}
// 2. Mark this node as deleted. We don't want to be faced with the possibility of
// generating a new key for this deleted node, so insert it into both maps.
this->newnodes.push_back(SerializedEventNodeAction(KeyNodePair(childid, &child), NODE_REMOVE));
this->newkeys[&child] = childid;
this->_nn.erase(&child);
std::string parentid = this->_findOrGenerateNodeID(node);
// 4. Serialize the event.
this->_attributes_scanned.erase(childid);
Glib::ustring childidmsg = MessageUtilities::makeTagWithContent(MESSAGE_CHILD, childid);
Glib::ustring parentidmsg = MessageUtilities::makeTagWithContent(MESSAGE_PARENT, parentid);
this->_events.push_back(MessageUtilities::makeTagWithContent(MESSAGE_DELETE, childidmsg + parentidmsg));
}
void
Serializer::notifyChildOrderChanged(XML::Node& node, XML::Node& child, XML::Node* old_prev, XML::Node* new_prev)
{
// 1. Find the ID of the node, or generate it if it does not exist.
std::string nodeid = this->_findOrGenerateNodeID(child);
// 2. Find the ID of the parent of this node, or generate it if it does not exist.
std::string parentid = this->_findOrGenerateNodeID(*(child.parent()));
// 3. Get the ID for the new child reference node, or generate it if it does not exist.
std::string newprevid = this->_findOrGenerateNodeID(*new_prev);
// 4. Get the ID for the old child reference node, or generate it if it does not exist.
std::string oldprevid = this->_findOrGenerateNodeID(*old_prev);
// 5. Serialize the event.
Glib::ustring nodeidmsg = MessageUtilities::makeTagWithContent(MESSAGE_ID, nodeid);
Glib::ustring parentidmsg = MessageUtilities::makeTagWithContent(MESSAGE_PARENT, parentid);
Glib::ustring oldprevidmsg = MessageUtilities::makeTagWithContent(MESSAGE_OLDVAL, oldprevid);
Glib::ustring newprevidmsg = MessageUtilities::makeTagWithContent(MESSAGE_NEWVAL, newprevid);
this->_events.push_back(MessageUtilities::makeTagWithContent(MESSAGE_ORDERCHANGE, nodeidmsg + parentidmsg + oldprevidmsg + newprevidmsg));
}
void
Serializer::notifyContentChanged(XML::Node& node, Util::shared_ptr<char> old_content, Util::shared_ptr<char> new_content)
{
// 1. Find the ID of the node, or generate it if it does not exist.
std::string nodeid = this->_findOrGenerateNodeID(node);
std::string oldvalmsg, newvalmsg;
// 2. If the old and new content are identical, don't send out this change.
// (identical meaning "same string" or "same string content")
if (old_content == new_content) {
return;
}
if (old_content.cString() != NULL && new_content.cString() != NULL) {
if (strcmp(old_content.cString(), new_content.cString()) == 0) {
return;
}
}
// 3. Serialize the event.
if (old_content.cString() != NULL) {
oldvalmsg = MessageUtilities::makeTagWithContent(MESSAGE_OLDVAL, old_content.cString());
}
if (new_content.cString() != NULL) {
newvalmsg = MessageUtilities::makeTagWithContent(MESSAGE_NEWVAL, new_content.cString());
}
Glib::ustring nodeidmsg = MessageUtilities::makeTagWithContent(MESSAGE_ID, nodeid);
this->_events.push_back(MessageUtilities::makeTagWithContent(MESSAGE_NODECONTENT, nodeidmsg + oldvalmsg + newvalmsg));
}
void
Serializer::notifyAttributeChanged(XML::Node& node, GQuark name, Util::shared_ptr<char> old_value, Util::shared_ptr<char> new_value)
{
// 1. Find the ID of the node that has had an attribute modified, or generate it if it
// does not exist.
std::string nodeid = this->_findOrGenerateNodeID(node);
// Proceed with 2-4 if the node has not already been scanned by notifyChildAdded.
if (this->_attributes_scanned.find(nodeid) == this->_attributes_scanned.end()) {
// 2. Convert the key to a string.
Glib::ustring key = g_quark_to_string(name);
// 3. If oldval == newval, don't echo this change.
if (new_value.cString() != NULL && old_value.cString() != NULL) {
if (strcmp(new_value.cString(), old_value.cString()) == 0) {
return;
}
}
// 4. Serialize the event.
Glib::ustring keymsg = MessageUtilities::makeTagWithContent(MESSAGE_KEY, key);
Glib::ustring oldvalmsg, newvalmsg;
if (old_value.cString() != NULL) {
oldvalmsg = MessageUtilities::makeTagWithContent(MESSAGE_OLDVAL, old_value.cString());
}
if (new_value.cString() != NULL) {
newvalmsg = MessageUtilities::makeTagWithContent(MESSAGE_NEWVAL, new_value.cString());
}
Glib::ustring nodeidmsg = MessageUtilities::makeTagWithContent(MESSAGE_ID, nodeid);
this->_events.push_back(MessageUtilities::makeTagWithContent(MESSAGE_CHANGE, nodeidmsg + keymsg + oldvalmsg + newvalmsg));
}
}
void
Serializer::synthesizeChildNodeAddEvents()
{
_New_nodes_type::iterator i = this->_nn.begin();
for(; i != this->_nn.end(); ++i) {
XML::Node* parent = *i;
// The root of the subtree defined by parent has already been considered; now,
// recursively look at the rest of the tree.
XML::Node* prev = parent->firstChild();
for(XML::Node* ch = parent->firstChild(); ch; ch = ch->next()) {
if (ch == parent->firstChild()) {
this->_newObjectEventHelper(*parent, *ch, NULL, true);
} else {
this->_newObjectEventHelper(*parent, *ch, prev, true);
prev = ch;
}
}
}
}
void
Serializer::_recursiveMarkAsRemoved(XML::Node& node)
{
this->actions.tryToTrack(&node, NODE_REMOVE);
for(XML::Node* ch = node.firstChild(); ch; ch = ch->next()) {
this->_recursiveMarkAsRemoved(*ch);
}
}
}
}
/*
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 :
|