summaryrefslogtreecommitdiffstats
blob: e6640e200c0acf0cbfd2f5ec5b90a65a537d7c0c (plain)
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
/*
 * 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 "Swiften/Roster/TreeWidget.h"
#include "Swiften/Roster/TreeWidgetFactory.h"

#include <boost/bind.hpp>

#include <deque>

namespace Swift {

Roster::Roster(TreeWidget *treeWidget, TreeWidgetFactory *widgetFactory) : treeWidget_(treeWidget), widgetFactory_(widgetFactory) {
}

Roster::~Roster() {
	foreach (RosterItem* item, items_) {
		delete item;
	}
	delete treeWidget_;
}

TreeWidget* Roster::getWidget() {
	return treeWidget_;
}

GroupRosterItem* Roster::getGroup(const String& groupName) {
	foreach (RosterItem *item, children_) {
		GroupRosterItem *group = dynamic_cast<GroupRosterItem*>(item);
		if (group && group->getName() == groupName) {
			return group;
		}
	}
	GroupRosterItem* group = new GroupRosterItem(groupName, treeWidget_, widgetFactory_);
	children_.push_back(group);
	items_.push_back(group);
	return group;
}

void Roster::handleUserAction(boost::shared_ptr<UserRosterAction> action) {
	onUserAction(action);
}

void Roster::addContact(const JID& jid, const String& name, const String& group) {
	ContactRosterItem *item = new ContactRosterItem(jid, name, getGroup(group), widgetFactory_);
	items_.push_back(item);
	item->onUserAction.connect(boost::bind(&Roster::handleUserAction, this, _1));
	filterItem(item);

}

void Roster::removeContact(const JID& jid) {
	std::vector<RosterItem*>::iterator it = children_.begin();
	while (it != children_.end()) {
		ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(*it);
		if (contact && contact->getJID() == jid) {
			delete contact;
			it = children_.erase(it);
			continue;
		} 
		GroupRosterItem* group = dynamic_cast<GroupRosterItem*>(*it);
		if (group) {
			group->removeChild(jid);
		}
		it++;
	}
}

void Roster::removeContactFromGroup(const JID& jid, const String& groupName) {
	std::vector<RosterItem*>::iterator it = children_.begin();
	while (it != children_.end()) {
		GroupRosterItem* group = dynamic_cast<GroupRosterItem*>(*it);
		if (group && group->getName() == groupName) {
			group->removeChild(jid);
		}
		it++;
	}
}


void Roster::applyOnItems(const RosterItemOperation& operation) {
	std::deque<RosterItem*> queue(children_.begin(), children_.end());
	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::filterItem(RosterItem* rosterItem) {
	ContactRosterItem *item = dynamic_cast<ContactRosterItem*>(rosterItem);
	if (!item) {
		return;
	}
	bool hide = true;
	foreach (RosterFilter *filter, filters_) {
		hide &= (*filter)(item);
	}
	filters_.size() > 0 && hide ? item->hide() : item->show();
}

void Roster::filterAll() {
	std::deque<RosterItem*> queue(children_.begin(), children_.end());
	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());
		} else {
			filterItem(item);
		}
	}
}

}