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
|
/**
* Whiteboard session manager
* XML node tracking facility
*
* Authors:
* David Yip <yipdw@rose-hulman.edu>
*
* Copyright (c) 2005 Authors
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#include "sp-object.h"
#include "sp-item-group.h"
#include "document.h"
#include "document-private.h"
#include "xml/node.h"
#include "util/compose.hpp"
#include "jabber_whiteboard/session-manager.h"
#include "jabber_whiteboard/node-tracker.h"
// TODO: remove redundant calls to isTracking(); it's a rather unnecessary
// performance burden.
namespace Inkscape {
namespace Whiteboard {
// Lookup tables
/**
* Keys for special nodes.
*
* A special node is a node that can only appear once in a document.
*/
char const* specialnodekeys[] = {
DOCUMENT_ROOT_NODE,
DOCUMENT_NAMEDVIEW_NODE,
};
/**
* Names of special nodes.
*
* A special node is a node that can only appear once in a document.
*/
char const* specialnodenames[] = {
DOCUMENT_ROOT_NAME,
DOCUMENT_NAMEDVIEW_NAME,
};
XMLNodeTracker::XMLNodeTracker(SessionManager* sm) :
_rootKey(DOCUMENT_ROOT_NODE),
_namedviewKey(DOCUMENT_NAMEDVIEW_NODE)
{
this->_sm = sm;
this->_counter = 0;
// Construct special node maps
this->createSpecialNodeTables();
this->reset();
}
XMLNodeTracker::~XMLNodeTracker()
{
this->_clear();
}
void
XMLNodeTracker::put(std::string key, XML::Node const& node)
{
this->put(key, const_cast< XML::Node& >(node));
}
void
XMLNodeTracker::put(std::string key, XML::Node& node)
{
KeyToTrackerNodeMap::iterator i = this->_keyToNode.find(key);
if (i != this->_keyToNode.end()) {
this->_keyToNode.erase(i);
}
this->_keyToNode.insert(std::make_pair< std::string, XML::Node* >(key, &node));
TrackerNodeToKeyMap::iterator j = this->_nodeToKey.find(&node);
if (j != this->_nodeToKey.end()) {
this->_nodeToKey.erase(j);
}
this->_nodeToKey.insert(std::make_pair< XML::Node*, std::string >(&node, key));
}
void
XMLNodeTracker::put(KeyToNodeMap& newids, NodeToKeyMap& newnodes)
{
// TODO: redo
KeyToNodeMap::iterator i = newids.begin();
for(; i != newids.end(); i++) {
this->put((*i).first, *((*i).second));
}
}
void
XMLNodeTracker::process(KeyToNodeActionList& actions)
{
KeyToNodeActionList::iterator i = actions.begin();
for(; i != actions.end(); i++) {
// Get the action to perform.
SerializedEventNodeAction action = *i;
switch(action.second) {
case NODE_ADD:
this->put(action.first.first, *action.first.second);
break;
case NODE_REMOVE:
// this->remove(const_cast< XML::Node& >(*action.first.second));
break;
default:
break;
}
}
}
XML::Node*
XMLNodeTracker::get(std::string& key)
{
KeyToTrackerNodeMap::iterator i = this->_keyToNode.find(key);
if (i != this->_keyToNode.end()) {
return (*i).second;
} else {
g_warning("Key %s is not being tracked!", key.c_str());
return NULL;
}
}
XML::Node*
XMLNodeTracker::get(std::string const& key)
{
return this->get(const_cast< std::string& >(key));
}
std::string const
XMLNodeTracker::get(XML::Node& node)
{
TrackerNodeToKeyMap::iterator i = this->_nodeToKey.find(&node);
if (i != this->_nodeToKey.end()) {
return (*i).second;
} else {
return "";
}
}
std::string const
XMLNodeTracker::get(XML::Node const& node)
{
return this->get(const_cast< XML::Node& >(node));
}
bool
XMLNodeTracker::isTracking(std::string& key)
{
return (this->_keyToNode.find(key) != this->_keyToNode.end());
}
bool
XMLNodeTracker::isTracking(std::string const& key)
{
return this->isTracking(const_cast< std::string& >(key));
}
bool
XMLNodeTracker::isTracking(XML::Node& node)
{
return (this->_nodeToKey.find(&node) != this->_nodeToKey.end());
}
bool
XMLNodeTracker::isTracking(XML::Node const& node)
{
return this->isTracking(const_cast< XML::Node& >(node));
}
bool
XMLNodeTracker::isRootNode(XML::Node& node)
{
XML::Node* docroot = sp_document_repr_root(this->_sm->document());
return (docroot == &node);
}
void
XMLNodeTracker::remove(std::string& key)
{
if (this->isTracking(key)) {
XML::Node* element = this->get(key);
this->_keyToNode.erase(key);
this->_nodeToKey.erase(element);
}
}
void
XMLNodeTracker::remove(XML::Node& node)
{
if (this->isTracking(node)) {
std::string const element = this->get(node);
this->_nodeToKey.erase(&node);
this->_keyToNode.erase(element);
}
}
bool
XMLNodeTracker::isSpecialNode(char const* name)
{
return (this->_specialnodes.find(name) != this->_specialnodes.end());
}
bool
XMLNodeTracker::isSpecialNode(std::string const& name)
{
return (this->_specialnodes.find(name.data()) != this->_specialnodes.end());
}
std::string const
XMLNodeTracker::getSpecialNodeKeyFromName(Glib::ustring const& name)
{
return this->_specialnodes[name.data()];
}
std::string const
XMLNodeTracker::getSpecialNodeKeyFromName(Glib::ustring const* name)
{
return this->_specialnodes[name->data()];
}
std::string
XMLNodeTracker::generateKey(gchar const* JID)
{
return String::compose("%1;%2", this->_counter++, JID);
}
std::string
XMLNodeTracker::generateKey()
{
SessionData* sd = this->_sm->session_data;
std::bitset< NUM_FLAGS >& status = sd->status;
if (status[IN_CHATROOM]) {
// This is not strictly required for chatrooms: chatrooms will
// function just fine with the user-to-user ID scheme. However,
// the user-to-user scheme can lead to loss of anonymity
// in anonymous chat rooms, since it contains the real JID
// of a user.
return String::compose("%1;%2@%3/%4", this->_counter++, sd->chat_name, sd->chat_server, sd->chat_handle);
} else {
return String::compose("%1;%2", this->_counter++, sd->jid);
}
}
void
XMLNodeTracker::createSpecialNodeTables()
{
int const sz = sizeof(specialnodekeys) / sizeof(char const*);
for(int i = 0; i < sz; i++) {
this->_specialnodes[specialnodenames[i]] = specialnodekeys[i];
}
}
// rather nasty and crufty debugging function
void
XMLNodeTracker::dump()
{
g_log(NULL, G_LOG_LEVEL_DEBUG, "XMLNodeTracker dump for %s", this->_sm->session_data->jid.c_str());
KeyToTrackerNodeMap::iterator i = this->_keyToNode.begin();
TrackerNodeToKeyMap::iterator j = this->_nodeToKey.begin();
std::map< char const*, char const* >::iterator k = this->_specialnodes.begin();
g_log(NULL, G_LOG_LEVEL_DEBUG, "%u entries in keyToNode, %u entries in nodeToKey", this->_keyToNode.size(), this->_nodeToKey.size());
if (this->_keyToNode.size() != this->_nodeToKey.size()) {
g_warning("Map sizes do not match!");
}
g_log(NULL, G_LOG_LEVEL_DEBUG, "XMLNodeTracker keyToNode dump");
while(i != this->_keyToNode.end()) {
if (!((*i).first.empty())) {
if ((*i).second != NULL) {
g_log(NULL, G_LOG_LEVEL_DEBUG, "%s\t->\t%p (%s) (%s)", (*i).first.c_str(), (*i).second, (*i).second->name(), (*i).second->content());
} else {
g_log(NULL, G_LOG_LEVEL_DEBUG, "%s\t->\t(null)", (*i).first.c_str());
}
} else {
g_log(NULL, G_LOG_LEVEL_DEBUG, "(null)\t->\t%p (%s)", (*i).second, (*i).second->name());
}
i++;
}
g_log(NULL, G_LOG_LEVEL_DEBUG, "XMLNodeTracker nodeToKey dump");
while(j != this->_nodeToKey.end()) {
if (!((*j).second.empty())) {
if ((*j).first) {
g_log(NULL, G_LOG_LEVEL_DEBUG, "%p\t->\t%s (parent %p)", (*j).first, (*j).second.c_str(), (*j).first->parent());
} else {
g_log(NULL, G_LOG_LEVEL_DEBUG, "(null)\t->\t%s", (*j).second.c_str());
}
} else {
g_log(NULL, G_LOG_LEVEL_DEBUG, "%p\t->\t(null)", (*j).first);
}
j++;
}
g_log(NULL, G_LOG_LEVEL_DEBUG, "_specialnodes dump");
while(k != this->_specialnodes.end()) {
g_log(NULL, G_LOG_LEVEL_DEBUG, "%s\t->\t%s", (*k).first, (*k).second);
k++;
}
}
void
XMLNodeTracker::reset()
{
this->_clear();
// Find and insert special nodes
// root node
this->put(this->_rootKey, *(sp_document_repr_root(this->_sm->document())));
// namedview node
SPObject* namedview = sp_item_group_get_child_by_name((SPGroup *)this->_sm->document()->root, NULL, DOCUMENT_NAMEDVIEW_NAME);
if (!namedview) {
g_warning("namedview node does not exist; it will be created during synchronization");
} else {
this->put(this->_namedviewKey, *(SP_OBJECT_REPR(namedview)));
}
}
void
XMLNodeTracker::_clear()
{
// Remove all keys in both trackers, and delete each key.
this->_keyToNode.clear();
this->_nodeToKey.clear();
/*
TrackerNodeToKeyMap::iterator j = this->_nodeToKey.begin();
for(; j != this->_nodeToKey.end(); j++) {
this->_nodeToKey.erase(j);
}
*/
}
}
}
/*
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 :
|