~mc.../inkscape/inkscape

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

#ifndef __WHITEBOARD_XML_NODE_TRACKER_H__
#define __WHITEBOARD_XML_NODE_TRACKER_H__

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

#include <bitset>
#include <cstring>
#include <map>
#include <glibmm.h>

namespace Inkscape {

namespace Whiteboard { 

class SessionManager;

/**
 * std::less-like functor for C-style strings.
 */
struct strcmpless : public std::binary_function< char const*, char const*, bool >
{
	bool operator()(char const* _x, char const* _y) const
	{
		return (strcmp(_x, _y) < 0);
	}
};


// TODO: This is a pretty heinous mess of methods that accept
// both pointers and references -- a lot of it has to do with
// XML::Node& in the node observer and XML::Node* elsewhere,
// although some of it (like Glib::ustring const& vs. 
// Glib::ustring const*) is completely mea culpa. When possible
// it'd be good to thin this class out.

/**
 * XMLNodeTracker generates and watches unique IDs for XML::Nodes for use in 
 * document event serialization and deserialization.
 *
 * More specifically, it has three tasks:
 * <ol>
 * 	<li>Association XML::Nodes with string IDs, and vice versa.</li>
 *  <li>Facilitation of lookup of a string ID or XML::Node given the other key.</li>
 * 	<li>Generation of new string IDs for XML::Nodes.</li>
 * </ol>
 *
 * XML::Nodes are assigned an ID that follows one of two forms:
 * <ol>
 * 	<li>unsigned integer;user JID</li>
 * 	<li>unsigned integer;chatroom@conference server/handle</li>
 * </ol>
 * 
 * Form 1 is used in user-to-user sessions; form 2 is used in chatroom sessions.
 */
class XMLNodeTracker  {
public:
	/**
	 * Constructor.
	 *
	 * \param sm The SessionManager with which an XMLNodeTracker instance is to be associated with.
	 */
	XMLNodeTracker(SessionManager* sm);
	~XMLNodeTracker();

	/** 
	 * Insert a (key,node) pair into the tracker.
	 *
	 * \param key The key to associate with the node.
	 * \param node The node to associate with the key.
	 */
	void put(std::string key, XML::Node& node);

	/** 
	 * Insert a (key,node) pair into the tracker.
	 *
	 * \param key The key to associate with the node.
	 * \param node The node to associate with the key.
	 */
	void put(std::string key, XML::Node const& node);

	/** 
	 * Insert a range of (key,node) pairs into the tracker.
	 *
	 * The size of the two maps must be the same.
	 * \param newids The keys to associate with the nodes.
	 * \param newnodes The nodes to associate with the keys.
	 */
	void put(KeyToNodeMap& newids, NodeToKeyMap& newnodes);

	/**
	 * Process a list of node actions to add and remove nodes from the tracker.
	 *
	 * \param actions The action list to process.
	 */
	void process(KeyToNodeActionList& actions);

	/**
	 * Retrieve an XML::Node given a key.
	 *
	 * \param key Reference to a string key.
	 * \return Pointer to an XML::Node, or NULL if no associated node could be found.
	 */
	XML::Node* get(std::string& key);

	/**
	 * Retrieve an XML::Node given a key.
	 *
	 * \param key Reference to a const string key.
	 * \return Pointer to an XML::Node, or NULL if no associated node could be found.
	 */
	XML::Node* get(std::string const& key);

	/**
	 * Retrieve a string key given a reference to an XML::Node.
	 *
	 * \param node Reference to an XML::Node.
	 * \return The associated string key, or an empty string if no associated key could be found.
	 */
	std::string const get(XML::Node& node);

	/**
	 * Retrieve a string key given a reference to an XML::Node.
	 *
	 * \param node Reference to a const XML::Node.
	 * \return The associated string key, or an empty string if no associated key could be found.
	 */
	std::string const get(XML::Node const& node);

	/**
	 * Remove an entry from the tracker based on key.
	 *
	 * \param The key of the entry to remove.
	 */
	void remove(std::string& key);

	/**
	 * Remove an entry from the tracker based on XML::Node.
	 *
	 * \param A reference to the XML::Node associated with the entry to remove.
	 */
	void remove(XML::Node& node);

	/**
	 * Return whether or not a (key,node) pair is being tracked, given a string key.
	 *
	 * \param The key associated with the pair to check.
	 * \return Whether or not the pair is being tracked.
	 */
	bool isTracking(std::string& key);

	/**
	 * Return whether or not a (key,node) pair is being tracked, given a string key.
	 *
	 * \param The key associated with the pair to check.
	 * \return Whether or not the pair is being tracked.
	 */
	bool isTracking(std::string const& key);

	/**
	 * Return whether or not a (key,node) pair is being tracked, given a node.
	 *
	 * \param The node associated with the pair to check.
	 * \return Whether or not the pair is being tracked.
	 */
	bool isTracking(XML::Node& node);

	/**
	 * Return whether or not a (key,node) pair is being tracked, given a node.
	 *
	 * \param The node associated with the pair to check.
	 * \return Whether or not the pair is being tracked.
	 */
	bool isTracking(XML::Node const& node);

	/**
	 * Return whether or not a node identified by a given name is a special node.
	 *
	 * \see Inkscape::Whiteboard::specialnodekeys
	 * \see Inkscape::Whiteboard::specialnodenames
	 *
	 * \param The name associated with the node.
	 * \return Whether or not the node is a special node.
	 */
	bool isSpecialNode(char const* name);

	/**
	 * Return whether or not a node identified by a given name is a special node.
	 *
	 * \see Inkscape::Whiteboard::specialnodekeys
	 * \see Inkscape::Whiteboard::specialnodenames
	 *
	 * \param The name associated with the node.
	 * \return Whether or not the node is a special node.
	 */
	bool isSpecialNode(std::string const& name);

	/**
	 * Retrieve the key of a special node given the name of a special node.
	 *
	 * \see Inkscape::Whiteboard::specialnodekeys
	 * \see Inkscape::Whiteboard::specialnodenames
	 *
	 * \param The name associated with the node.
	 * \return The key of the special node.
	 */
	std::string const getSpecialNodeKeyFromName(Glib::ustring const& name);

	/**
	 * Retrieve the key of a special node given the name of a special node.
	 *
	 * \see Inkscape::Whiteboard::specialnodekeys
	 * \see Inkscape::Whiteboard::specialnodenames
	 *
	 * \param The name associated with the node.
	 * \return The key of the special node.
	 */
	std::string const getSpecialNodeKeyFromName(Glib::ustring const* name);

	/**
	 * Returns whether or not the given node is the root node of the SPDocument associated
	 * with an XMLNodeTracker's SessionManager.
	 *
	 * \param Reference to an XML::Node to test.
	 * \return Whether or not the given node is the document root node.
	 */
	bool isRootNode(XML::Node& node);

	/** 
	 * Generate a node key given a JID.
	 *
	 * \param The JID to use in the key.
	 * \return A node string key.
	 */
	std::string generateKey(gchar const* JID);

	/** 
	 * Generate a node key given the JID specified in the SessionData structure associated
	 * with an XMLNodeTracker's SessionManager.
	 *
	 * \return A node string key.
	 */
	std::string generateKey();

	// TODO: remove debugging function
	void dump();
	void reset();

private:
	void createSpecialNodeTables();
	void _clear();
	
	unsigned int _counter;
	SessionManager* _sm;

	// defined in typedefs.h
	KeyToTrackerNodeMap _keyToNode;
	TrackerNodeToKeyMap _nodeToKey;

	std::map< char const*, char const*, strcmpless > _specialnodes;

	// Keys for special nodes
	std::string _rootKey;
	std::string _defsKey;
	std::string _namedviewKey;
	std::string _metadataKey;

	// noncopyable, nonassignable
	XMLNodeTracker(XMLNodeTracker const&);
	XMLNodeTracker& operator=(XMLNodeTracker const&);
};

}

}


#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 :