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
|
/*
* Copyright (c) 2010 Remko Tronçon
* Licensed under the GNU General Public License v3.
* See Documentation/Licenses/GPLv3.txt for more information.
*/
#include "Swiften/Roster/Roster.h"
#include "Swiften/Base/foreach.h"
#include "Swiften/Base/String.h"
#include "Swiften/JID/JID.h"
#include "Swiften/Roster/ContactRosterItem.h"
#include "Swiften/Roster/RosterItem.h"
#include "Swiften/Roster/GroupRosterItem.h"
#include "Swiften/Roster/RosterItemOperation.h"
#include <boost/bind.hpp>
#include <iostream>
#include <deque>
namespace Swift {
Roster::Roster() {
root_ = new GroupRosterItem("Dummy-Root", NULL);
}
Roster::~Roster() {
std::deque<RosterItem*> queue;
queue.push_back(root_);
while (!queue.empty()) {
RosterItem* item = *queue.begin();
queue.pop_front();
GroupRosterItem* group = dynamic_cast<GroupRosterItem*>(item);
if (group) {
queue.insert(queue.begin(), group->getChildren().begin(), group->getChildren().end());
}
delete item;
}
}
GroupRosterItem* Roster::getRoot() {
return root_;
}
GroupRosterItem* Roster::getGroup(const String& groupName) {
foreach (RosterItem *item, root_->getChildren()) {
GroupRosterItem *group = dynamic_cast<GroupRosterItem*>(item);
if (group && group->getDisplayName() == groupName) {
return group;
}
}
GroupRosterItem* group = new GroupRosterItem(groupName, root_);
root_->addChild(group);
// std::cout << "Added " << groupName << " to root" << std::endl;
group->onChildrenChanged.connect(boost::bind(&Roster::handleChildrenChanged, this, group));
group->onDataChanged.connect(boost::bind(&Roster::handleDataChanged, this, group));
return group;
}
void Roster::handleDataChanged(RosterItem* item) {
onDataChanged(item);
}
void Roster::handleChildrenChanged(GroupRosterItem* item) {
onChildrenChanged(item);
}
void Roster::addContact(const JID& jid, const String& name, const String& groupName) {
GroupRosterItem* group(getGroup(groupName));
ContactRosterItem *item = new ContactRosterItem(jid, name, group);
group->addChild(item);
itemMap_[jid.toBare()].push_back(item);
item->onDataChanged.connect(boost::bind(&Roster::handleDataChanged, this, item));
filterContact(item, group);
}
struct JIDEqualsTo {
JIDEqualsTo(const JID& jid) : jid(jid) {}
bool operator()(ContactRosterItem* i) const { return jid == i->getJID(); }
JID jid;
};
void Roster::removeContact(const JID& jid) {
std::vector<ContactRosterItem*>* items = &itemMap_[jid.toBare()];
items->erase(std::remove_if(items->begin(), items->end(), JIDEqualsTo(jid)), items->end());
if (items->size() == 0) {
itemMap_.erase(jid.toBare());
}
//Causes the delete
root_->removeChild(jid);
}
void Roster::removeContactFromGroup(const JID& jid, const String& groupName) {
std::vector<RosterItem*> children = root_->getChildren();
std::vector<RosterItem*>::iterator it = children.begin();
while (it != children.end()) {
GroupRosterItem* group = dynamic_cast<GroupRosterItem*>(*it);
if (group && group->getDisplayName() == groupName) {
ContactRosterItem* deleted = group->removeChild(jid);
std::vector<ContactRosterItem*>* items = &itemMap_[jid.toBare()];
items->erase(std::remove(items->begin(), items->end(), deleted), items->end());
}
it++;
}
}
void Roster::applyOnItems(const RosterItemOperation& operation) {
if (operation.requiresLookup()) {
applyOnItem(operation, operation.lookupJID());
} else {
applyOnAllItems(operation);
}
}
void Roster::applyOnItem(const RosterItemOperation& operation, const JID& jid) {
foreach (ContactRosterItem* item, itemMap_[jid.toBare()]) {
//std::cout << "Applying on item " << item << " : " << item->getDisplayName() << std::endl;
operation(item);
filterContact(item, item->getParent());
}
}
void Roster::applyOnAllItems(const RosterItemOperation& operation) {
std::deque<RosterItem*> queue;
queue.push_back(root_);
while (!queue.empty()) {
RosterItem* item = *queue.begin();
queue.pop_front();
operation(item);
GroupRosterItem* group = dynamic_cast<GroupRosterItem*>(item);
if (group) {
queue.insert(queue.begin(), group->getChildren().begin(), group->getChildren().end());
}
}
filterAll();
}
void Roster::removeFilter(RosterFilter *filter) {
for (unsigned int i = 0; i < filters_.size(); i++) {
if (filters_[i] == filter) {
filters_.erase(filters_.begin() + i);
break;
}
}
filterAll();
}
void Roster::filterContact(ContactRosterItem* contact, GroupRosterItem* group) {
int oldDisplayedSize = group->getDisplayedChildren().size();
bool hide = true;
foreach (RosterFilter *filter, filters_) {
hide &= (*filter)(contact);
}
group->setDisplayed(contact, filters_.size() == 0 || !hide);
int newDisplayedSize = group->getDisplayedChildren().size();
// std::cout << ", new size = " << newDisplayedSize << std::endl;
if (oldDisplayedSize == 0 && newDisplayedSize > 0) {
// std::cout << "Newly created" << std::endl;
onGroupAdded(group);
}
}
void Roster::filterGroup(GroupRosterItem* group) {
foreach (RosterItem* child, group->getChildren()) {
ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(child);
if (contact) {
filterContact(contact, group);
}
}
}
void Roster::filterAll() {
std::deque<RosterItem*> queue;
queue.push_back(root_);
while (!queue.empty()) {
RosterItem *item = *queue.begin();
queue.pop_front();
GroupRosterItem* group = dynamic_cast<GroupRosterItem*>(item);
if (group) {
queue.insert(queue.begin(), group->getChildren().begin(), group->getChildren().end());
filterGroup(group);
}
}
}
}
|