summaryrefslogtreecommitdiffstats
blob: 02c1c18d409b6b8d236114a7e3abe30b5a6f0c01 (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
/*
 * Copyright (c) 2011 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swiften/Client/BlockListImpl.h>

#include <Swiften/Base/foreach.h>

#include <algorithm>

using namespace Swift;

BlockListImpl::BlockListImpl() : state(Init) {

}

void BlockListImpl::setItems(const std::vector<JID>& items) {
	foreach (const JID& jid, this->items) {
		if (std::find(items.begin(), items.end(), jid) != items.end()) {
			onItemRemoved(jid);
		}
	}

	foreach (const JID& jid, items) {
		if (std::find(this->items.begin(), this->items.end(), jid) != this->items.end()) {
			onItemAdded(jid);
		}
	}
	this->items = items;
}

void BlockListImpl::addItem(const JID& item) {
	if (std::find(items.begin(), items.end(), item) == items.end()) {
		items.push_back(item);
		onItemAdded(item);
	}
}

void BlockListImpl::removeItem(const JID& item) {
	size_t oldSize = items.size();
	items.erase(std::remove(items.begin(), items.end(), item), items.end());
	if (items.size() != oldSize) {
		onItemRemoved(item);
	}
}

void BlockListImpl::setState(State state) {
	if (this->state != state) {
		this->state = state;
		onStateChanged();
	}
}

void BlockListImpl::addItems(const std::vector<JID>& items) {
	foreach (const JID& item, items) {
		addItem(item);
	}
}

void BlockListImpl::removeItems(const std::vector<JID>& items) {
	std::vector<JID> itemsToRemove = items;
	foreach (const JID& item, itemsToRemove) {
		removeItem(item);
	}
}

void BlockListImpl::removeAllItems() {
	removeItems(items);
}