summaryrefslogtreecommitdiffstats
blob: 2ab59ead5771dcb6ebc34aa8ba628b652ff7cc42 (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
#ifndef SWIFTEN_GroupRosterItem_H
#define SWIFTEN_GroupRosterItem_H

#include "Swiften/Roster/RosterItem.h"
#include "Swiften/Base/String.h"
#include "Swiften/Roster/TreeWidget.h"
#include "Swiften/Roster/TreeWidgetFactory.h"
#include "Swiften/Roster/TreeWidgetItem.h"
#include "Swiften/Roster/ContactRosterItem.h"

#include <list>

namespace Swift {

class GroupRosterItem : public RosterItem {
	public:
		GroupRosterItem(const String& name, TreeWidget* tree, TreeWidgetFactory* factory) : name_(name) {
			widget_ = factory->createTreeWidgetItem(tree);
			widget_->setExpanded(true);
			widget_->setText(name);
		}

		~GroupRosterItem() {
			delete widget_;
		}

		const String& getName() const {
			return name_;
		}

		TreeWidgetItem* getWidget() const {
			return widget_;
		}

		const std::list<RosterItem*>& getChildren() const {
			return children_;
		}

		void addChild(RosterItem* item) {
			children_.push_back(item);
		}

		void removeChild(const JID& jid) {
			std::list<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++;
			}
		}

	private:
		String name_;
		TreeWidgetItem* widget_;
		std::list<RosterItem*> children_;
};

}
#endif