1 /**
2 * Inkscape::Whiteboard::KeyNodeTable - structure for lookup of values from keys
3 * and vice versa
4 *
5 * Authors:
6 * Bob Jamison
7 *
8 * Copyright (c) 2005 Authors
9 */
10 #include "keynode.h"
11 #include "util/ucompose.hpp"
13 namespace Inkscape
14 {
15 namespace Whiteboard
16 {
20 void KeyNodeTable::clear()
21 {
22 items.clear();
23 }
25 void KeyNodeTable::append(const KeyNodeTable &other)
26 {
27 for (unsigned int i = 0; i<other.size() ; i++)
28 {
29 KeyNodePair pair = other.item(i);
30 put(pair);
31 }
32 }
34 void KeyNodeTable::put(const KeyNodePair &pair)
35 {
36 put(pair.key, pair.node);
37 }
39 void KeyNodeTable::put(const Glib::ustring &key, const XML::Node *node)
40 {
41 //delete existing
42 std::vector<KeyNodePair>::iterator iter;
43 for (iter = items.begin() ; iter != items.end() ; )
44 {
45 if (key == iter->key || node == iter->node)
46 iter = items.erase(iter);
47 else
48 iter++;
49 }
51 //add new
52 KeyNodePair pair(key, node);
53 items.push_back(pair);
54 }
56 XML::Node * KeyNodeTable::get(const Glib::ustring &key) const
57 {
58 std::vector<KeyNodePair>::const_iterator iter;
59 for (iter = items.begin() ; iter != items.end() ; iter++)
60 {
61 if (key == iter->key)
62 return iter->node;
63 }
64 return NULL;
65 }
68 void KeyNodeTable::remove(const Glib::ustring &key)
69 {
70 std::vector<KeyNodePair>::iterator iter;
71 for (iter = items.begin() ; iter != items.end() ; )
72 {
73 if (key == iter->key)
74 iter = items.erase(iter);
75 else
76 iter++;
77 }
78 }
81 Glib::ustring KeyNodeTable::get(XML::Node *node) const
82 {
83 std::vector<KeyNodePair>::const_iterator iter;
84 for (iter = items.begin() ; iter != items.end() ; iter++)
85 {
86 if (node == iter->node)
87 return iter->key;
88 }
89 return "";
90 }
92 unsigned int KeyNodeTable::incrementVersion(XML::Node *node)
93 {
94 std::vector<KeyNodePair>::iterator iter;
95 for (iter = items.begin() ; iter != items.end() ; iter++)
96 {
97 if (node == iter->node)
98 break;
99 }
100 return ++iter->version;
101 }
103 unsigned int KeyNodeTable::getVersion(XML::Node *node)
104 {
105 std::vector<KeyNodePair>::iterator iter;
106 for (iter = items.begin() ; iter != items.end() ; iter++)
107 {
108 if (node == iter->node)
109 break;
110 }
111 return iter->version;
112 }
114 void KeyNodeTable::addHistory(XML::Node *node, Glib::ustring attribute, Glib::ustring value)
115 {
116 std::vector<KeyNodePair>::iterator iter;
117 for (iter = items.begin() ; iter != items.end() ; iter++)
118 {
119 if (node == iter->node)
120 {
121 Configure pair(attribute, value);
122 iter->history.push_back(pair);
123 }
124 }
125 }
127 Glib::ustring KeyNodeTable::getLastHistory(XML::Node *node, Glib::ustring att)
128 {
129 std::list<Configure> hist;
131 std::vector<KeyNodePair>::iterator iter;
132 for (iter = items.begin() ; iter != items.end() ; iter++)
133 {
134 if (node == iter->node)
135 hist = iter->history;
136 }
138 std::list<Configure>::iterator it;
139 for(it = hist.end() ; it != hist.begin() ; it--)
140 {
141 if(it->first == att)
142 {
143 //g_warning("hist %s %s",it->first,it->second);
144 return it->second;
145 }
146 }
147 return "";
148 }
150 void KeyNodeTable::remove(XML::Node *node)
151 {
152 std::vector<KeyNodePair>::iterator iter;
153 for (iter = items.begin() ; iter != items.end() ; )
154 {
155 if (node == iter->node)
156 iter = items.erase(iter);
157 else
158 iter++;
159 }
160 }
162 unsigned int KeyNodeTable::size() const
163 {
164 return items.size();
165 }
168 KeyNodePair KeyNodeTable::item(unsigned int index) const
169 {
170 if (index>=items.size())
171 {
172 KeyNodePair pair("", NULL);
173 return pair;
174 }
175 return items[index];
176 }
178 Glib::ustring
179 KeyNodeTable::generateKey(Glib::ustring jid)
180 {
181 return String::ucompose("%1/%2",this->counter++,jid);
182 }
185 } // namespace Whiteboard
187 } // namespace Inkscape
188 //#########################################################################
189 //# E N D O F F I L E
190 //#########################################################################