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
|
/*
* Copyright (c) 2010 Kevin Smith
* Licensed under the GNU General Public License v3.
* See Documentation/Licenses/GPLv3.txt for more information.
*/
#include "Swiften/Roster/GroupRosterItem.h"
#include <boost/bind.hpp>
//#include <boost/algorithm.hpp>
#include <iostream>
namespace Swift {
GroupRosterItem::GroupRosterItem(const String& name, GroupRosterItem* parent) : RosterItem(name, parent) {
}
GroupRosterItem::~GroupRosterItem() {
}
const std::vector<RosterItem*>& GroupRosterItem::getChildren() const {
return children_;
}
const std::vector<RosterItem*>& GroupRosterItem::getDisplayedChildren() const {
// std::cout << "Fetching displayed children for " << getDisplayName() << " and found " << displayedChildren_.size() << std::endl;
return displayedChildren_;
}
void GroupRosterItem::addChild(RosterItem* item) {
children_.push_back(item);
GroupRosterItem* group = dynamic_cast<GroupRosterItem*>(item);
if (group) {
group->onChildrenChanged.connect(boost::bind(&GroupRosterItem::handleChildrenChanged, this, group));
} else {
item->onDataChanged.connect(boost::bind(&GroupRosterItem::handleDataChanged, this, item));
}
onChildrenChanged();
onDataChanged();
}
/**
* Returns the removed item - but only if it's the only one, otherwise
* the return result is undefined.
*/
ContactRosterItem* GroupRosterItem::removeChild(const JID& jid) {
std::vector<RosterItem*>::iterator it = children_.begin();
ContactRosterItem* removed = NULL;
while (it != children_.end()) {
ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(*it);
if (contact && contact->getJID() == jid) {
displayedChildren_.erase(std::remove(displayedChildren_.begin(), displayedChildren_.end(), contact), displayedChildren_.end());
removed = contact;
delete contact;
it = children_.erase(it);
continue;
}
GroupRosterItem* group = dynamic_cast<GroupRosterItem*>(*it);
if (group) {
ContactRosterItem* groupRemoved = group->removeChild(jid);
if (groupRemoved) {
removed = groupRemoved;
}
}
it++;
}
onChildrenChanged();
onDataChanged();
return removed;
}
/**
* Returns false if the list didn't need a resort
*/
bool GroupRosterItem::sortDisplayed() {
/* Not doing this until we import boost::algorithm*/
// if (boost::is_sorted(displayedChildren_begin(), displayedChildren_.end(), itemLessThan)) {
// return false;
// }
//Sholudn't need stable_sort here
std::sort(displayedChildren_.begin(), displayedChildren_.end(), itemLessThan);
return true;
}
bool GroupRosterItem::itemLessThan(const RosterItem* left, const RosterItem* right) {
const ContactRosterItem* leftContact = dynamic_cast<const ContactRosterItem*>(left);
const ContactRosterItem* rightContact = dynamic_cast<const ContactRosterItem*>(right);
if (leftContact) {
if (!rightContact) {
return false;
}
StatusShow::Type leftType = leftContact->getSimplifiedStatusShow();
StatusShow::Type rightType = rightContact->getSimplifiedStatusShow();
if (leftType == rightType) {
return left->getSortableDisplayName() < right->getSortableDisplayName();
} else {
return leftType < rightType;
}
} else {
if (rightContact) {
return true;
}
// std::cout << "Comparing groups " << left->getSortableDisplayName() << " and " << right->getSortableDisplayName() << std::endl;
return left->getSortableDisplayName() < right->getSortableDisplayName();
}
}
void GroupRosterItem::setDisplayed(RosterItem* item, bool displayed) {
bool found = false;
for (size_t i = 0; i < displayedChildren_.size(); i++) {
if (displayedChildren_[i] == item) {
found = true;
}
}
if (found == displayed) {
return;
}
if (displayed) {
displayedChildren_.push_back(item);
sortDisplayed();
} else {
displayedChildren_.erase(std::remove(displayedChildren_.begin(), displayedChildren_.end(), item), displayedChildren_.end());
}
onDataChanged();
onChildrenChanged();
}
void GroupRosterItem::handleDataChanged(RosterItem* /*item*/) {
if (sortDisplayed()) {
onChildrenChanged();
}
}
void GroupRosterItem::handleChildrenChanged(GroupRosterItem* group) {
size_t oldSize = getDisplayedChildren().size();
if (group->getDisplayedChildren().size() > 0) {
bool found = false;
for (size_t i = 0; i < displayedChildren_.size(); i++) {
if (displayedChildren_[i] == group) {
found = true;
}
}
if (!found) {
displayedChildren_.push_back(group);
sortDisplayed();
}
} else {
displayedChildren_.erase(std::remove(displayedChildren_.begin(), displayedChildren_.end(), group), displayedChildren_.end());
}
if (oldSize != getDisplayedChildren().size()) {
onDataChanged();
onChildrenChanged();
}
}
}
|