summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swift/Controllers/Roster')
-rw-r--r--Swift/Controllers/Roster/AppearOffline.h32
-rw-r--r--Swift/Controllers/Roster/ContactRosterItem.cpp116
-rw-r--r--Swift/Controllers/Roster/ContactRosterItem.h54
-rw-r--r--Swift/Controllers/Roster/GroupRosterItem.cpp201
-rw-r--r--Swift/Controllers/Roster/GroupRosterItem.h45
-rw-r--r--Swift/Controllers/Roster/OfflineRosterFilter.h27
-rw-r--r--Swift/Controllers/Roster/Roster.cpp209
-rw-r--r--Swift/Controllers/Roster/Roster.h58
-rw-r--r--Swift/Controllers/Roster/RosterController.cpp298
-rw-r--r--Swift/Controllers/Roster/RosterController.h93
-rw-r--r--Swift/Controllers/Roster/RosterFilter.h21
-rw-r--r--Swift/Controllers/Roster/RosterGroupExpandinessPersister.cpp60
-rw-r--r--Swift/Controllers/Roster/RosterGroupExpandinessPersister.h27
-rw-r--r--Swift/Controllers/Roster/RosterItem.cpp44
-rw-r--r--Swift/Controllers/Roster/RosterItem.h32
-rw-r--r--Swift/Controllers/Roster/RosterItemOperation.h30
-rw-r--r--Swift/Controllers/Roster/SetAvatar.h36
-rw-r--r--Swift/Controllers/Roster/SetName.h37
-rw-r--r--Swift/Controllers/Roster/SetPresence.h36
-rw-r--r--Swift/Controllers/Roster/UnitTest/RosterControllerTest.cpp290
-rw-r--r--Swift/Controllers/Roster/UnitTest/RosterTest.cpp128
21 files changed, 1874 insertions, 0 deletions
diff --git a/Swift/Controllers/Roster/AppearOffline.h b/Swift/Controllers/Roster/AppearOffline.h
new file mode 100644
index 0000000..8bd53d7
--- /dev/null
+++ b/Swift/Controllers/Roster/AppearOffline.h
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include <Swift/Controllers/Roster/RosterItemOperation.h>
+#include <Swift/Controllers/Roster/ContactRosterItem.h>
+
+namespace Swift {
+
+class RosterItem;
+
+class AppearOffline : public RosterItemOperation {
+ public:
+ AppearOffline() {
+ }
+
+ virtual void operator() (RosterItem* item) const {
+ ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(item);
+ if (contact) {
+ contact->clearPresence();
+ }
+ }
+
+};
+
+}
+
+
diff --git a/Swift/Controllers/Roster/ContactRosterItem.cpp b/Swift/Controllers/Roster/ContactRosterItem.cpp
new file mode 100644
index 0000000..df0eb7b
--- /dev/null
+++ b/Swift/Controllers/Roster/ContactRosterItem.cpp
@@ -0,0 +1,116 @@
+/*
+ * Copyright (c) 2010 Kevin Smith
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include "Swift/Controllers/Roster/ContactRosterItem.h"
+#include "Swift/Controllers/Roster/GroupRosterItem.h"
+
+namespace Swift {
+
+
+ContactRosterItem::ContactRosterItem(const JID& jid, const JID& displayJID, const String& name, GroupRosterItem* parent) : RosterItem(name, parent), jid_(jid), displayJID_(displayJID) {
+}
+
+ContactRosterItem::~ContactRosterItem() {
+}
+
+StatusShow::Type ContactRosterItem::getStatusShow() const {
+ return shownPresence_ ? shownPresence_->getShow() : StatusShow::None;
+}
+
+StatusShow::Type ContactRosterItem::getSimplifiedStatusShow() const {
+ switch (shownPresence_ ? shownPresence_->getShow() : StatusShow::None) {
+ case StatusShow::Online: return StatusShow::Online; break;
+ case StatusShow::Away: return StatusShow::Away; break;
+ case StatusShow::XA: return StatusShow::Away; break;
+ case StatusShow::FFC: return StatusShow::Online; break;
+ case StatusShow::DND: return StatusShow::DND; break;
+ case StatusShow::None: return StatusShow::None; break;
+ }
+ assert(false);
+ return StatusShow::None;
+}
+
+String ContactRosterItem::getStatusText() const {
+ return shownPresence_ ? shownPresence_->getStatus() : "";
+}
+
+void ContactRosterItem::setAvatarPath(const String& path) {
+ avatarPath_ = path;
+ onDataChanged();
+}
+const String& ContactRosterItem::getAvatarPath() const {
+ return avatarPath_;
+}
+
+const JID& ContactRosterItem::getJID() const {
+ return jid_;
+}
+
+void ContactRosterItem::setDisplayJID(const JID& jid) {
+ displayJID_ = jid;
+}
+
+const JID& ContactRosterItem::getDisplayJID() const {
+ return displayJID_;
+}
+
+
+typedef std::pair<String, boost::shared_ptr<Presence> > StringPresencePair;
+
+void ContactRosterItem::calculateShownPresence() {
+ shownPresence_ = offlinePresence_;
+ foreach (StringPresencePair presencePair, presences_) {
+ boost::shared_ptr<Presence> presence = presencePair.second;
+ if (!shownPresence_ || presence->getPriority() > shownPresence_->getPriority() || presence->getShow() < shownPresence_->getShow()) {
+ shownPresence_ = presence;
+ }
+ }
+}
+
+void ContactRosterItem::clearPresence() {
+ presences_.clear();
+ calculateShownPresence();
+ onDataChanged();
+}
+
+void ContactRosterItem::applyPresence(const String& resource, boost::shared_ptr<Presence> presence) {
+ if (offlinePresence_) {
+ offlinePresence_ = boost::shared_ptr<Presence>();
+ }
+ if (presence->getType() == Presence::Unavailable) {
+ if (resource.isEmpty()) {
+ /* Unavailable from the bare JID means all resources are offline.*/
+ presences_.clear();
+ } else {
+ if (presences_.find(resource) != presences_.end()) {
+ presences_.erase(resource);
+ }
+ }
+ if (presences_.size() == 0) {
+ offlinePresence_ = presence;
+ }
+ } else {
+ presences_[resource] = presence;
+ }
+ calculateShownPresence();
+ onDataChanged();
+}
+
+const std::vector<String> ContactRosterItem::getGroups() const {
+ return groups_;
+}
+
+/** Only used so a contact can know about the groups it's in*/
+void ContactRosterItem::addGroup(const String& group) {
+ groups_.push_back(group);
+}
+void ContactRosterItem::removeGroup(const String& group) {
+ groups_.erase(std::remove(groups_.begin(), groups_.end(), group), groups_.end());
+}
+
+}
+
+
diff --git a/Swift/Controllers/Roster/ContactRosterItem.h b/Swift/Controllers/Roster/ContactRosterItem.h
new file mode 100644
index 0000000..ca9d727
--- /dev/null
+++ b/Swift/Controllers/Roster/ContactRosterItem.h
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2010 Kevin Smith
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include "Swiften/Base/String.h"
+#include "Swiften/JID/JID.h"
+#include "Swift/Controllers/Roster/RosterItem.h"
+#include "Swiften/Elements/StatusShow.h"
+#include "Swiften/Elements/Presence.h"
+
+#include <map>
+#include <boost/bind.hpp>
+#include "Swiften/Base/boost_bsignals.h"
+#include <boost/shared_ptr.hpp>
+
+namespace Swift {
+
+class GroupRosterItem;
+class ContactRosterItem : public RosterItem {
+ public:
+ ContactRosterItem(const JID& jid, const JID& displayJID, const String& name, GroupRosterItem* parent);
+ virtual ~ContactRosterItem();
+
+ StatusShow::Type getStatusShow() const;
+ StatusShow::Type getSimplifiedStatusShow() const;
+ String getStatusText() const;
+ void setAvatarPath(const String& path);
+ const String& getAvatarPath() const;
+ const JID& getJID() const;
+ void setDisplayJID(const JID& jid);
+ const JID& getDisplayJID() const;
+ void applyPresence(const String& resource, boost::shared_ptr<Presence> presence);
+ void clearPresence();
+ void calculateShownPresence();
+ const std::vector<String> getGroups() const;
+ /** Only used so a contact can know about the groups it's in*/
+ void addGroup(const String& group);
+ void removeGroup(const String& group);
+ private:
+ JID jid_;
+ JID displayJID_;
+ String avatarPath_;
+ std::map<String, boost::shared_ptr<Presence> > presences_;
+ boost::shared_ptr<Presence> offlinePresence_;
+ boost::shared_ptr<Presence> shownPresence_;
+ std::vector<String> groups_;
+};
+
+}
+
diff --git a/Swift/Controllers/Roster/GroupRosterItem.cpp b/Swift/Controllers/Roster/GroupRosterItem.cpp
new file mode 100644
index 0000000..c473ae7
--- /dev/null
+++ b/Swift/Controllers/Roster/GroupRosterItem.cpp
@@ -0,0 +1,201 @@
+/*
+ * Copyright (c) 2010 Kevin Smith
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include "Swift/Controllers/Roster/GroupRosterItem.h"
+
+#include <boost/bind.hpp>
+//#include <boost/algorithm.hpp>
+#include <iostream>
+
+namespace Swift {
+
+GroupRosterItem::GroupRosterItem(const String& name, GroupRosterItem* parent, bool sortByStatus) : RosterItem(name, parent), sortByStatus_(sortByStatus) {
+ expanded_ = true;
+}
+
+GroupRosterItem::~GroupRosterItem() {
+
+}
+
+bool GroupRosterItem::isExpanded() const {
+ return expanded_;
+}
+
+/**
+ This has no effect, and is only used by the UI.
+ If reTransmit is specified, dataChanged will be emitted on a change -
+ This may be undesireable if called from the UI, so you can use reTransmit=false
+ to avoid a loop in this case.
+ */
+void GroupRosterItem::setExpanded(bool expanded) {
+ bool old = expanded_;
+ expanded_ = expanded;
+ if (expanded != old) {
+ onExpandedChanged(expanded);
+ }
+}
+
+const std::vector<RosterItem*>& GroupRosterItem::getChildren() const {
+ return children_;
+}
+
+const std::vector<RosterItem*>& GroupRosterItem::getDisplayedChildren() const {
+ 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();
+}
+
+/**
+ * Does not emit a changed signal.
+ */
+void GroupRosterItem::removeAll() {
+ std::vector<RosterItem*>::iterator it = children_.begin();
+ displayedChildren_.clear();
+ while (it != children_.end()) {
+ ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(*it);
+ if (contact) {
+ delete contact;
+ } else {
+ GroupRosterItem* group = dynamic_cast<GroupRosterItem*>(*it);
+ if (group) {
+ group->removeAll();
+ delete group;
+ }
+ }
+ it++;
+ }
+ children_.clear();
+}
+
+/**
+ * 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(), sortByStatus_? itemLessThanWithStatus : itemLessThanWithoutStatus);
+ return true;
+}
+
+bool GroupRosterItem::itemLessThanWithoutStatus(const RosterItem* left, const RosterItem* right) {
+ return left->getSortableDisplayName() < right->getSortableDisplayName();
+}
+
+bool GroupRosterItem::itemLessThanWithStatus(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;
+ }
+ 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());
+ }
+ onChildrenChanged();
+ onDataChanged();
+}
+
+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()) {
+ onChildrenChanged();
+ onDataChanged();
+ }
+}
+
+
+}
diff --git a/Swift/Controllers/Roster/GroupRosterItem.h b/Swift/Controllers/Roster/GroupRosterItem.h
new file mode 100644
index 0000000..85e8e46
--- /dev/null
+++ b/Swift/Controllers/Roster/GroupRosterItem.h
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2010 Kevin Smith
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include "Swift/Controllers/Roster/RosterItem.h"
+#include "Swiften/Base/String.h"
+#include "Swift/Controllers/Roster/ContactRosterItem.h"
+
+#include <vector>
+
+namespace Swift {
+
+class GroupRosterItem : public RosterItem {
+ public:
+ GroupRosterItem(const String& name, GroupRosterItem* parent, bool sortByStatus);
+ virtual ~GroupRosterItem();
+ const std::vector<RosterItem*>& getChildren() const;
+ const std::vector<RosterItem*>& getDisplayedChildren() const;
+ void addChild(RosterItem* item);
+ ContactRosterItem* removeChild(const JID& jid);
+ void removeAll();
+ void setDisplayed(RosterItem* item, bool displayed);
+ boost::signal<void ()> onChildrenChanged;
+ static bool itemLessThanWithStatus(const RosterItem* left, const RosterItem* right);
+ static bool itemLessThanWithoutStatus(const RosterItem* left, const RosterItem* right);
+ void setExpanded(bool expanded);
+ bool isExpanded() const;
+ boost::signal<void (bool)> onExpandedChanged;
+ private:
+ void handleChildrenChanged(GroupRosterItem* group);
+ void handleDataChanged(RosterItem* item);
+ bool sortDisplayed();
+ String name_;
+ bool expanded_;
+ std::vector<RosterItem*> children_;
+ std::vector<RosterItem*> displayedChildren_;
+ bool sortByStatus_;
+};
+
+}
+
diff --git a/Swift/Controllers/Roster/OfflineRosterFilter.h b/Swift/Controllers/Roster/OfflineRosterFilter.h
new file mode 100644
index 0000000..1af2624
--- /dev/null
+++ b/Swift/Controllers/Roster/OfflineRosterFilter.h
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include "Swift/Controllers/Roster/ContactRosterItem.h"
+#include "Swift/Controllers/Roster/RosterItem.h"
+#include "Swift/Controllers/Roster/RosterFilter.h"
+#include "Swiften/Elements/StatusShow.h"
+
+namespace Swift {
+
+class OfflineRosterFilter : public RosterFilter {
+ public:
+ virtual ~OfflineRosterFilter() {}
+ virtual bool operator() (RosterItem *item) const {
+ ContactRosterItem *contactItem = dynamic_cast<ContactRosterItem*>(item);
+ return contactItem && contactItem->getStatusShow() == StatusShow::None;
+ }
+};
+
+}
+
+
diff --git a/Swift/Controllers/Roster/Roster.cpp b/Swift/Controllers/Roster/Roster.cpp
new file mode 100644
index 0000000..7967a38
--- /dev/null
+++ b/Swift/Controllers/Roster/Roster.cpp
@@ -0,0 +1,209 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include "Swift/Controllers/Roster/Roster.h"
+
+#include "Swiften/Base/foreach.h"
+#include "Swiften/Base/String.h"
+#include "Swiften/JID/JID.h"
+#include "Swift/Controllers/Roster/ContactRosterItem.h"
+#include "Swift/Controllers/Roster/RosterItem.h"
+#include "Swift/Controllers/Roster/GroupRosterItem.h"
+#include "Swift/Controllers/Roster/RosterItemOperation.h"
+
+#include <boost/bind.hpp>
+
+#include <iostream>
+#include <deque>
+
+namespace Swift {
+
+Roster::Roster(bool sortByStatus, bool fullJIDMapping) {
+ sortByStatus_ = sortByStatus;
+ fullJIDMapping_ = fullJIDMapping;
+ root_ = new GroupRosterItem("Dummy-Root", NULL, sortByStatus_);
+ root_->onChildrenChanged.connect(boost::bind(&Roster::handleChildrenChanged, this, root_));
+}
+
+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_, sortByStatus_);
+ root_->addChild(group);
+ 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 JID& displayJID, const String& name, const String& groupName, const String& avatarPath) {
+ GroupRosterItem* group(getGroup(groupName));
+ ContactRosterItem *item = new ContactRosterItem(jid, displayJID, name, group);
+ item->setAvatarPath(avatarPath);
+ group->addChild(item);
+ if (itemMap_[fullJIDMapping_ ? jid : jid.toBare()].size() > 0) {
+ foreach (String existingGroup, itemMap_[fullJIDMapping_ ? jid : jid.toBare()][0]->getGroups()) {
+ item->addGroup(existingGroup);
+ }
+ }
+ itemMap_[fullJIDMapping_ ? jid : jid.toBare()].push_back(item);
+ item->onDataChanged.connect(boost::bind(&Roster::handleDataChanged, this, item));
+ filterContact(item, group);
+
+ foreach (ContactRosterItem* item, itemMap_[fullJIDMapping_ ? jid : jid.toBare()]) {
+ item->addGroup(groupName);
+ }
+}
+
+struct JIDEqualsTo {
+ JIDEqualsTo(const JID& jid) : jid(jid) {}
+ bool operator()(ContactRosterItem* i) const { return jid == i->getJID(); }
+ JID jid;
+};
+
+void Roster::removeAll() {
+ root_->removeAll();
+ itemMap_.clear();
+ onChildrenChanged(root_);
+ onDataChanged(root_);
+}
+
+void Roster::removeContact(const JID& jid) {
+ std::vector<ContactRosterItem*>* items = &itemMap_[fullJIDMapping_ ? jid : jid.toBare()];
+ items->erase(std::remove_if(items->begin(), items->end(), JIDEqualsTo(jid)), items->end());
+ if (items->size() == 0) {
+ itemMap_.erase(fullJIDMapping_ ? jid : 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_[fullJIDMapping_ ? jid : jid.toBare()];
+ items->erase(std::remove(items->begin(), items->end(), deleted), items->end());
+ }
+ it++;
+ }
+ foreach (ContactRosterItem* item, itemMap_[fullJIDMapping_ ? jid : jid.toBare()]) {
+ item->removeGroup(groupName);
+ }
+}
+
+
+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_[fullJIDMapping_ ? jid : jid.toBare()]) {
+ 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();
+ if (oldDisplayedSize == 0 && newDisplayedSize > 0) {
+ 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);
+ }
+ }
+}
+
+}
+
diff --git a/Swift/Controllers/Roster/Roster.h b/Swift/Controllers/Roster/Roster.h
new file mode 100644
index 0000000..70ff0b5
--- /dev/null
+++ b/Swift/Controllers/Roster/Roster.h
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include "Swiften/Base/String.h"
+#include "Swiften/JID/JID.h"
+#include "Swift/Controllers/Roster/RosterItemOperation.h"
+#include "Swift/Controllers/Roster/RosterFilter.h"
+
+#include <vector>
+#include <map>
+#include "Swiften/Base/boost_bsignals.h"
+#include <boost/shared_ptr.hpp>
+
+namespace Swift {
+
+class RosterItem;
+class GroupRosterItem;
+class ContactRosterItem;
+
+class Roster {
+ public:
+ Roster(bool sortByStatus = true, bool fullJIDMapping = false);
+ ~Roster();
+
+ void addContact(const JID& jid, const JID& displayJID, const String& name, const String& group, const String& avatarPath);
+ void removeContact(const JID& jid);
+ void removeContactFromGroup(const JID& jid, const String& group);
+ void removeAll();
+ void applyOnItems(const RosterItemOperation& operation);
+ void applyOnAllItems(const RosterItemOperation& operation);
+ void applyOnItem(const RosterItemOperation& operation, const JID& jid);
+ void addFilter(RosterFilter *filter) {filters_.push_back(filter);filterAll();};
+ void removeFilter(RosterFilter *filter);
+ GroupRosterItem* getRoot();
+ std::vector<RosterFilter*> getFilters() {return filters_;};
+ boost::signal<void (GroupRosterItem*)> onChildrenChanged;
+ boost::signal<void (GroupRosterItem*)> onGroupAdded;
+ boost::signal<void (RosterItem*)> onDataChanged;
+ private:
+ GroupRosterItem* getGroup(const String& groupName);
+ void handleDataChanged(RosterItem* item);
+ void handleChildrenChanged(GroupRosterItem* item);
+ void filterGroup(GroupRosterItem* item);
+ void filterContact(ContactRosterItem* contact, GroupRosterItem* group);
+ void filterAll();
+ GroupRosterItem* root_;
+ std::vector<RosterFilter*> filters_;
+ std::map<JID, std::vector<ContactRosterItem*> > itemMap_;
+ bool fullJIDMapping_;
+ bool sortByStatus_;
+};
+
+}
diff --git a/Swift/Controllers/Roster/RosterController.cpp b/Swift/Controllers/Roster/RosterController.cpp
new file mode 100644
index 0000000..2dddd7d
--- /dev/null
+++ b/Swift/Controllers/Roster/RosterController.cpp
@@ -0,0 +1,298 @@
+/*
+ * Copyright (c) 2010 Kevin Smith
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include "Swift/Controllers/Roster/RosterController.h"
+
+#include <boost/bind.hpp>
+#include <boost/smart_ptr/make_shared.hpp>
+
+#include "Swiften/Base/foreach.h"
+#include "Swift/Controllers/UIInterfaces/MainWindow.h"
+#include "Swift/Controllers/UIInterfaces/MainWindowFactory.h"
+#include "Swiften/Client/NickResolver.h"
+#include "Swiften/Roster/GetRosterRequest.h"
+#include "Swiften/Roster/SetRosterRequest.h"
+#include "Swift/Controllers/XMPPEvents/SubscriptionRequestEvent.h"
+#include "Swift/Controllers/XMPPEvents/ErrorEvent.h"
+#include "Swiften/Presence/PresenceOracle.h"
+#include "Swiften/Presence/SubscriptionManager.h"
+#include "Swift/Controllers/XMPPEvents/EventController.h"
+#include "Swiften/Queries/IQRouter.h"
+#include "Swift/Controllers/Roster/Roster.h"
+#include "Swift/Controllers/Roster/SetPresence.h"
+#include "Swift/Controllers/Roster/AppearOffline.h"
+#include "Swift/Controllers/Roster/SetAvatar.h"
+#include "Swift/Controllers/Roster/SetName.h"
+#include "Swift/Controllers/Roster/OfflineRosterFilter.h"
+#include "Swiften/Roster/XMPPRoster.h"
+#include "Swiften/Roster/XMPPRosterItem.h"
+#include "Swift/Controllers/UIEvents/AddContactUIEvent.h"
+#include "Swift/Controllers/UIEvents/RemoveRosterItemUIEvent.h"
+#include "Swift/Controllers/UIEvents/RenameRosterItemUIEvent.h"
+#include "Swift/Controllers/UIEvents/RenameGroupUIEvent.h"
+#include "Swift/Controllers/UIEvents/ToggleShowOfflineUIEvent.h"
+#include <Swiften/Client/NickManager.h>
+
+namespace Swift {
+
+static const String SHOW_OFFLINE = "showOffline";
+
+/**
+ * The controller does not gain ownership of these parameters.
+ */
+RosterController::RosterController(const JID& jid, XMPPRoster* xmppRoster, AvatarManager* avatarManager, MainWindowFactory* mainWindowFactory, NickManager* nickManager, NickResolver* nickResolver, PresenceOracle* presenceOracle, SubscriptionManager* subscriptionManager, EventController* eventController, UIEventStream* uiEventStream, IQRouter* iqRouter, SettingsProvider* settings)
+ : myJID_(jid), xmppRoster_(xmppRoster), mainWindowFactory_(mainWindowFactory), mainWindow_(mainWindowFactory_->createMainWindow(uiEventStream)), roster_(new Roster()), offlineFilter_(new OfflineRosterFilter()), nickManager_(nickManager), nickResolver_(nickResolver), uiEventStream_(uiEventStream) {
+ iqRouter_ = iqRouter;
+ presenceOracle_ = presenceOracle;
+ subscriptionManager_ = subscriptionManager;
+ eventController_ = eventController;
+ settings_ = settings;
+ expandiness_ = new RosterGroupExpandinessPersister(roster_, settings);
+ roster_->addFilter(offlineFilter_);
+ mainWindow_->setRosterModel(roster_);
+
+ changeStatusConnection_ = mainWindow_->onChangeStatusRequest.connect(boost::bind(&RosterController::handleChangeStatusRequest, this, _1, _2));
+ signOutConnection_ = mainWindow_->onSignOutRequest.connect(boost::bind(boost::ref(onSignOutRequest)));
+ xmppRoster_->onJIDAdded.connect(boost::bind(&RosterController::handleOnJIDAdded, this, _1));
+ xmppRoster_->onJIDUpdated.connect(boost::bind(&RosterController::handleOnJIDUpdated, this, _1, _2, _3));
+ xmppRoster_->onJIDRemoved.connect(boost::bind(&RosterController::handleOnJIDRemoved, this, _1));
+ xmppRoster_->onRosterCleared.connect(boost::bind(&RosterController::handleRosterCleared, this));
+ subscriptionManager_->onPresenceSubscriptionRequest.connect(boost::bind(&RosterController::handleSubscriptionRequest, this, _1, _2));
+ presenceOracle_->onPresenceChange.connect(boost::bind(&RosterController::handleIncomingPresence, this, _1));
+ uiEventConnection_ = uiEventStream->onUIEvent.connect(boost::bind(&RosterController::handleUIEvent, this, _1));
+ avatarManager_ = avatarManager;
+ avatarManager_->onAvatarChanged.connect(boost::bind(&RosterController::handleAvatarChanged, this, _1));
+ mainWindow_->setMyAvatarPath(avatarManager_->getAvatarPath(myJID_).string());
+
+ nickManager_->onOwnNickChanged.connect(boost::bind(&MainWindow::setMyNick, mainWindow_, _1));
+ mainWindow_->setMyJID(jid);
+ mainWindow_->setMyNick(nickManager_->getOwnNick());
+
+ if (settings->getBoolSetting(SHOW_OFFLINE, false)) {
+ uiEventStream->onUIEvent(boost::shared_ptr<UIEvent>(new ToggleShowOfflineUIEvent(true)));
+ }
+}
+
+RosterController::~RosterController() {
+ nickManager_->onOwnNickChanged.disconnect(boost::bind(&MainWindow::setMyNick, mainWindow_, _1));
+
+ delete offlineFilter_;
+ delete expandiness_;
+
+ mainWindow_->setRosterModel(NULL);
+ if (mainWindow_->canDelete()) {
+ delete mainWindow_;
+ }
+ delete roster_;
+}
+
+void RosterController::setEnabled(bool enabled) {
+ if (!enabled) {
+ roster_->applyOnItems(AppearOffline());
+ }
+}
+
+void RosterController::handleShowOfflineToggled(bool state) {
+ if (state != settings_->getBoolSetting(SHOW_OFFLINE, false)) {
+ settings_->storeBool(SHOW_OFFLINE, state);
+ }
+ if (state) {
+ roster_->removeFilter(offlineFilter_);
+ } else {
+ roster_->addFilter(offlineFilter_);
+ }
+}
+
+void RosterController::handleChangeStatusRequest(StatusShow::Type show, const String &statusText) {
+ onChangeStatusRequest(show, statusText);
+}
+
+void RosterController::handleOnJIDAdded(const JID& jid) {
+ std::vector<String> groups = xmppRoster_->getGroupsForJID(jid);
+ String name = nickResolver_->jidToNick(jid);
+ if (!groups.empty()) {
+ foreach(const String& group, groups) {
+ roster_->addContact(jid, jid, name, group, avatarManager_->getAvatarPath(jid).string());
+ }
+ }
+ else {
+ roster_->addContact(jid, jid, name, "Contacts", avatarManager_->getAvatarPath(jid).string());
+ }
+ applyAllPresenceTo(jid);
+}
+
+void RosterController::applyAllPresenceTo(const JID& jid) {
+ foreach (Presence::ref presence, presenceOracle_->getAllPresence(jid)) {
+ roster_->applyOnItems(SetPresence(presence));
+ }
+}
+
+void RosterController::handleRosterCleared() {
+ roster_->removeAll();
+}
+
+void RosterController::handleOnJIDRemoved(const JID& jid) {
+ roster_->removeContact(jid);
+}
+
+void RosterController::handleOnJIDUpdated(const JID& jid, const String& oldName, const std::vector<String> passedOldGroups) {
+ if (oldName != xmppRoster_->getNameForJID(jid)) {
+ roster_->applyOnItems(SetName(nickResolver_->jidToNick(jid), jid));
+ return;
+ }
+ std::vector<String> groups = xmppRoster_->getGroupsForJID(jid);
+ std::vector<String> oldGroups = passedOldGroups;
+ String name = nickResolver_->jidToNick(jid);
+ String contactsGroup = "Contacts";
+ if (oldGroups.empty()) {
+ oldGroups.push_back(contactsGroup);
+ }
+ if (groups.empty()) {
+ groups.push_back(contactsGroup);
+ }
+ foreach(const String& group, groups) {
+ if (std::find(oldGroups.begin(), oldGroups.end(), group) == oldGroups.end()) {
+ roster_->addContact(jid, jid, name, group, avatarManager_->getAvatarPath(jid).string());
+ }
+ }
+ foreach(const String& group, oldGroups) {
+ if (std::find(groups.begin(), groups.end(), group) == groups.end()) {
+ roster_->removeContactFromGroup(jid, group);
+ }
+ }
+ applyAllPresenceTo(jid);
+}
+
+void RosterController::handleUIEvent(boost::shared_ptr<UIEvent> event) {
+ if (boost::shared_ptr<ToggleShowOfflineUIEvent> showOfflineEvent = boost::dynamic_pointer_cast<ToggleShowOfflineUIEvent>(event)) {
+ handleShowOfflineToggled(showOfflineEvent->getShow());
+ }
+ else if (boost::shared_ptr<AddContactUIEvent> addContactEvent = boost::dynamic_pointer_cast<AddContactUIEvent>(event)) {
+ RosterItemPayload item;
+ item.setName(addContactEvent->getName());
+ item.setJID(addContactEvent->getJID());
+ boost::shared_ptr<RosterPayload> roster(new RosterPayload());
+ roster->addItem(item);
+ SetRosterRequest::ref request = SetRosterRequest::create(roster, iqRouter_);
+ request->onResponse.connect(boost::bind(&RosterController::handleRosterSetError, this, _1, roster));
+ request->send();
+ subscriptionManager_->requestSubscription(addContactEvent->getJID());
+ }
+ else if (boost::shared_ptr<RemoveRosterItemUIEvent> removeEvent = boost::dynamic_pointer_cast<RemoveRosterItemUIEvent>(event)) {
+ RosterItemPayload item(removeEvent->getJID(), "", RosterItemPayload::Remove);
+ boost::shared_ptr<RosterPayload> roster(new RosterPayload());
+ roster->addItem(item);
+ SetRosterRequest::ref request = SetRosterRequest::create(roster, iqRouter_);
+ request->onResponse.connect(boost::bind(&RosterController::handleRosterSetError, this, _1, roster));
+ request->send();
+
+ }
+ else if (boost::shared_ptr<RenameRosterItemUIEvent> renameEvent = boost::dynamic_pointer_cast<RenameRosterItemUIEvent>(event)) {
+ JID contact(renameEvent->getJID());
+ RosterItemPayload item(contact, renameEvent->getNewName(), xmppRoster_->getSubscriptionStateForJID(contact));
+ item.setGroups(xmppRoster_->getGroupsForJID(contact));
+ boost::shared_ptr<RosterPayload> roster(new RosterPayload());
+ roster->addItem(item);
+ SetRosterRequest::ref request = SetRosterRequest::create(roster, iqRouter_);
+ request->onResponse.connect(boost::bind(&RosterController::handleRosterSetError, this, _1, roster));
+ request->send();
+ }
+ else if (boost::shared_ptr<RenameGroupUIEvent> renameGroupEvent = boost::dynamic_pointer_cast<RenameGroupUIEvent>(event)) {
+ std::vector<XMPPRosterItem> items = xmppRoster_->getItems();
+ String group = renameGroupEvent->getGroup();
+ // FIXME: We should handle contacts groups specially to avoid clashes
+ if (group == "Contacts") {
+ group = "";
+ }
+ foreach(XMPPRosterItem& item, items) {
+ std::vector<String> groups = item.getGroups();
+ if ( (group.isEmpty() && groups.empty()) || std::find(groups.begin(), groups.end(), group) != groups.end()) {
+ groups.erase(std::remove(groups.begin(), groups.end(), group), groups.end());
+ if (std::find(groups.begin(), groups.end(), renameGroupEvent->getNewName()) == groups.end()) {
+ groups.push_back(renameGroupEvent->getNewName());
+ }
+ item.setGroups(groups);
+ updateItem(item);
+ }
+ }
+ }
+}
+
+void RosterController::setContactGroups(const JID& jid, const std::vector<String>& groups) {
+ updateItem(XMPPRosterItem(jid, xmppRoster_->getNameForJID(jid), groups, xmppRoster_->getSubscriptionStateForJID(jid)));
+}
+
+void RosterController::updateItem(const XMPPRosterItem& item) {
+ RosterItemPayload itemPayload(item.getJID(), item.getName(), item.getSubscription());
+ itemPayload.setGroups(item.getGroups());
+
+ RosterPayload::ref roster = boost::make_shared<RosterPayload>();
+ roster->addItem(itemPayload);
+
+ SetRosterRequest::ref request = SetRosterRequest::create(roster, iqRouter_);
+ request->onResponse.connect(boost::bind(&RosterController::handleRosterSetError, this, _1, roster));
+ request->send();
+}
+
+void RosterController::handleRosterSetError(ErrorPayload::ref error, boost::shared_ptr<RosterPayload> rosterPayload) {
+ if (!error) {
+ return;
+ }
+ String text = "Server " + myJID_.getDomain() + " rejected roster change to item '" + rosterPayload->getItems()[0].getJID() + "'";
+ if (!error->getText().isEmpty()) {
+ text += ": " + error->getText();
+ }
+ boost::shared_ptr<ErrorEvent> errorEvent(new ErrorEvent(JID(myJID_.getDomain()), text));
+ eventController_->handleIncomingEvent(errorEvent);
+}
+
+void RosterController::handleIncomingPresence(Presence::ref newPresence) {
+ if (newPresence->getType() == Presence::Error) {
+ return;
+ }
+ roster_->applyOnItems(SetPresence(newPresence));
+}
+
+void RosterController::handleSubscriptionRequest(const JID& jid, const String& message) {
+ if (xmppRoster_->containsJID(jid) && (xmppRoster_->getSubscriptionStateForJID(jid) == RosterItemPayload::To || xmppRoster_->getSubscriptionStateForJID(jid) == RosterItemPayload::Both)) {
+ subscriptionManager_->confirmSubscription(jid);
+ return;
+ }
+ SubscriptionRequestEvent* eventPointer = new SubscriptionRequestEvent(jid, message);
+ eventPointer->onAccept.connect(boost::bind(&RosterController::handleSubscriptionRequestAccepted, this, eventPointer));
+ eventPointer->onDecline.connect(boost::bind(&RosterController::handleSubscriptionRequestDeclined, this, eventPointer));
+ boost::shared_ptr<StanzaEvent> event(eventPointer);
+ eventController_->handleIncomingEvent(event);
+}
+
+void RosterController::handleSubscriptionRequestAccepted(SubscriptionRequestEvent* event) {
+ subscriptionManager_->confirmSubscription(event->getJID());
+ if (!xmppRoster_->containsJID(event->getJID()) || xmppRoster_->getSubscriptionStateForJID(event->getJID()) == RosterItemPayload::None || xmppRoster_->getSubscriptionStateForJID(event->getJID()) == RosterItemPayload::From) {
+ subscriptionManager_->requestSubscription(event->getJID());
+ }
+}
+
+void RosterController::handleSubscriptionRequestDeclined(SubscriptionRequestEvent* event) {
+ subscriptionManager_->cancelSubscription(event->getJID());
+}
+
+void RosterController::handleAvatarChanged(const JID& jid) {
+ String path = avatarManager_->getAvatarPath(jid).string();
+ roster_->applyOnItems(SetAvatar(jid, path));
+ if (jid.equals(myJID_, JID::WithoutResource)) {
+ mainWindow_->setMyAvatarPath(path);
+ }
+}
+
+boost::optional<XMPPRosterItem> RosterController::getItem(const JID& jid) const {
+ return xmppRoster_->getItem(jid);
+}
+
+std::set<String> RosterController::getGroups() const {
+ return xmppRoster_->getGroups();
+}
+
+}
diff --git a/Swift/Controllers/Roster/RosterController.h b/Swift/Controllers/Roster/RosterController.h
new file mode 100644
index 0000000..b0641c3
--- /dev/null
+++ b/Swift/Controllers/Roster/RosterController.h
@@ -0,0 +1,93 @@
+/*
+ * Copyright (c) 2010 Kevin Smith
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include "Swiften/JID/JID.h"
+#include "Swiften/Base/String.h"
+#include "Swiften/Elements/Presence.h"
+#include "Swiften/Elements/ErrorPayload.h"
+#include "Swiften/Elements/RosterPayload.h"
+#include "Swiften/Avatars/AvatarManager.h"
+#include "Swift/Controllers/UIEvents/UIEvent.h"
+#include "RosterGroupExpandinessPersister.h"
+
+#include "Swiften/Base/boost_bsignals.h"
+#include <boost/shared_ptr.hpp>
+
+namespace Swift {
+ class IQRouter;
+ class Roster;
+ class XMPPRoster;
+ class XMPPRosterItem;
+ class MainWindow;
+ class MainWindowFactory;
+ class OfflineRosterFilter;
+ class NickResolver;
+ class PresenceOracle;
+ class SubscriptionManager;
+ class EventController;
+ class SubscriptionRequestEvent;
+ class UIEventStream;
+ class IQRouter;
+ class SettingsProvider;
+ class NickManager;
+
+ class RosterController {
+ public:
+ RosterController(const JID& jid, XMPPRoster* xmppRoster, AvatarManager* avatarManager, MainWindowFactory* mainWindowFactory, NickManager* nickManager, NickResolver* nickResolver, PresenceOracle* presenceOracle, SubscriptionManager* subscriptionManager, EventController* eventController, UIEventStream* uiEventStream, IQRouter* iqRouter_, SettingsProvider* settings);
+ ~RosterController();
+ void showRosterWindow();
+ MainWindow* getWindow() {return mainWindow_;};
+ boost::signal<void (StatusShow::Type, const String&)> onChangeStatusRequest;
+ boost::signal<void ()> onSignOutRequest;
+ void handleAvatarChanged(const JID& jid);
+ void setEnabled(bool enabled);
+
+ boost::optional<XMPPRosterItem> getItem(const JID&) const;
+ std::set<String> getGroups() const;
+
+ void setContactGroups(const JID& jid, const std::vector<String>& groups);
+ void updateItem(const XMPPRosterItem&);
+
+ private:
+ void handleOnJIDAdded(const JID &jid);
+ void handleRosterCleared();
+ void handleOnJIDRemoved(const JID &jid);
+ void handleOnJIDUpdated(const JID &jid, const String& oldName, const std::vector<String> oldGroups);
+ void handleStartChatRequest(const JID& contact);
+ void handleChangeStatusRequest(StatusShow::Type show, const String &statusText);
+ void handleShowOfflineToggled(bool state);
+ void handleIncomingPresence(boost::shared_ptr<Presence> newPresence);
+ void handleSubscriptionRequest(const JID& jid, const String& message);
+ void handleSubscriptionRequestAccepted(SubscriptionRequestEvent* event);
+ void handleSubscriptionRequestDeclined(SubscriptionRequestEvent* event);
+ void handleUIEvent(boost::shared_ptr<UIEvent> event);
+ void handleRosterSetError(ErrorPayload::ref error, boost::shared_ptr<RosterPayload> rosterPayload);
+ void applyAllPresenceTo(const JID& jid);
+ void handleEditProfileRequest();
+
+ JID myJID_;
+ XMPPRoster* xmppRoster_;
+ MainWindowFactory* mainWindowFactory_;
+ MainWindow* mainWindow_;
+ Roster* roster_;
+ OfflineRosterFilter* offlineFilter_;
+ AvatarManager* avatarManager_;
+ NickManager* nickManager_;
+ NickResolver* nickResolver_;
+ PresenceOracle* presenceOracle_;
+ SubscriptionManager* subscriptionManager_;
+ EventController* eventController_;
+ RosterGroupExpandinessPersister* expandiness_;
+ IQRouter* iqRouter_;
+ SettingsProvider* settings_;
+ UIEventStream* uiEventStream_;
+ boost::bsignals::scoped_connection changeStatusConnection_;
+ boost::bsignals::scoped_connection signOutConnection_;
+ boost::bsignals::scoped_connection uiEventConnection_;
+ };
+}
diff --git a/Swift/Controllers/Roster/RosterFilter.h b/Swift/Controllers/Roster/RosterFilter.h
new file mode 100644
index 0000000..508b9da
--- /dev/null
+++ b/Swift/Controllers/Roster/RosterFilter.h
@@ -0,0 +1,21 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include "Swift/Controllers/Roster/RosterItem.h"
+
+namespace Swift {
+
+class RosterFilter {
+ public:
+ virtual ~RosterFilter() {}
+ virtual bool operator() (RosterItem* item) const = 0;
+};
+
+}
+
+
diff --git a/Swift/Controllers/Roster/RosterGroupExpandinessPersister.cpp b/Swift/Controllers/Roster/RosterGroupExpandinessPersister.cpp
new file mode 100644
index 0000000..64baac9
--- /dev/null
+++ b/Swift/Controllers/Roster/RosterGroupExpandinessPersister.cpp
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2010 Kevin Smith
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include "RosterGroupExpandinessPersister.h"
+
+#include <boost/bind.hpp>
+#include <vector>
+
+#include "Swift/Controllers/Roster/GroupRosterItem.h"
+
+namespace Swift {
+
+RosterGroupExpandinessPersister::RosterGroupExpandinessPersister(Roster* roster, SettingsProvider* settings) : roster_(roster), settings_(settings) {
+ load();
+ roster_->onGroupAdded.connect(boost::bind(&RosterGroupExpandinessPersister::handleGroupAdded, this, _1));
+}
+
+void RosterGroupExpandinessPersister::handleGroupAdded(GroupRosterItem* group) {
+ if (collapsed_.find(group->getDisplayName()) != collapsed_.end()) {
+ group->setExpanded(false);
+ } else {
+ group->setExpanded(true);
+ }
+ group->onExpandedChanged.connect(boost::bind(&RosterGroupExpandinessPersister::handleExpandedChanged, this, group, _1));
+}
+
+void RosterGroupExpandinessPersister::handleExpandedChanged(GroupRosterItem* group, bool expanded) {
+ if (expanded) {
+ String displayName = group->getDisplayName();
+ //collapsed_.erase(std::remove(collapsed_.begin(), collapsed_.end(), displayName), collapsed_.end());
+ collapsed_.erase(displayName);
+ } else {
+ collapsed_.insert(group->getDisplayName());
+ }
+ save();
+}
+
+void RosterGroupExpandinessPersister::save() {
+ String setting;
+ foreach (const String& group, collapsed_) {
+ if (!setting.isEmpty()) {
+ setting += "\n";
+ }
+ setting += group;
+ }
+ settings_->storeString(SettingPath, setting);
+}
+
+void RosterGroupExpandinessPersister::load() {
+ String saved = settings_->getStringSetting(SettingPath);
+ std::vector<String> collapsed = saved.split('\n');
+ collapsed_.insert(collapsed.begin(), collapsed.end());
+}
+
+const String RosterGroupExpandinessPersister::SettingPath = "GroupExpandiness";
+
+}
diff --git a/Swift/Controllers/Roster/RosterGroupExpandinessPersister.h b/Swift/Controllers/Roster/RosterGroupExpandinessPersister.h
new file mode 100644
index 0000000..f73afa8
--- /dev/null
+++ b/Swift/Controllers/Roster/RosterGroupExpandinessPersister.h
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2010 Kevin Smith
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include <set>
+#include "Swift/Controllers/Roster/Roster.h"
+#include "Swift/Controllers/Settings/SettingsProvider.h"
+
+namespace Swift {
+ class RosterGroupExpandinessPersister {
+ public:
+ RosterGroupExpandinessPersister(Roster* roster, SettingsProvider* settings);
+ private:
+ void handleExpandedChanged(GroupRosterItem* group, bool expanded);
+ void handleGroupAdded(GroupRosterItem* group);
+ void load();
+ void save();
+ std::set<String> collapsed_;
+ Roster* roster_;
+ SettingsProvider* settings_;
+ static const String SettingPath;
+ };
+}
diff --git a/Swift/Controllers/Roster/RosterItem.cpp b/Swift/Controllers/Roster/RosterItem.cpp
new file mode 100644
index 0000000..61c5aea
--- /dev/null
+++ b/Swift/Controllers/Roster/RosterItem.cpp
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2010 Kevin Smith
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include "Swift/Controllers/Roster/RosterItem.h"
+
+#include "Swift/Controllers/Roster/GroupRosterItem.h"
+
+namespace Swift {
+
+RosterItem::RosterItem(const String& name, GroupRosterItem* parent) : name_(name), sortableDisplayName_(name_.getLowerCase()), parent_(parent) {
+ /* The following would be good, but because of C++'s inheritance not working in constructors, it's not going to work. */
+ //if (parent) {
+ // parent_->addChild(this);
+ //}
+}
+
+RosterItem::~RosterItem() {
+
+}
+
+GroupRosterItem* RosterItem::getParent() const {
+ return parent_;
+}
+
+void RosterItem::setDisplayName(const String& name) {
+ name_ = name;
+ sortableDisplayName_ = name_.getLowerCase();
+ onDataChanged();
+}
+
+String RosterItem::getDisplayName() const {
+ return name_;
+}
+
+String RosterItem::getSortableDisplayName() const {
+ return sortableDisplayName_;
+}
+
+
+}
+
diff --git a/Swift/Controllers/Roster/RosterItem.h b/Swift/Controllers/Roster/RosterItem.h
new file mode 100644
index 0000000..35dbe73
--- /dev/null
+++ b/Swift/Controllers/Roster/RosterItem.h
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2010 Kevin Smith
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include "Swiften/Base/boost_bsignals.h"
+#include <boost/shared_ptr.hpp>
+
+#include "Swiften/Base/String.h"
+
+namespace Swift {
+class GroupRosterItem;
+class RosterItem {
+ public:
+ RosterItem(const String& name, GroupRosterItem* parent);
+ virtual ~RosterItem();
+ boost::signal<void ()> onDataChanged;
+ GroupRosterItem* getParent() const;
+ void setDisplayName(const String& name);
+ String getDisplayName() const;
+ String getSortableDisplayName() const;
+ private:
+ String name_;
+ String sortableDisplayName_;
+ GroupRosterItem* parent_;
+};
+
+}
+
diff --git a/Swift/Controllers/Roster/RosterItemOperation.h b/Swift/Controllers/Roster/RosterItemOperation.h
new file mode 100644
index 0000000..691c8ef
--- /dev/null
+++ b/Swift/Controllers/Roster/RosterItemOperation.h
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include "Swift/Controllers/Roster/RosterItem.h"
+
+namespace Swift {
+
+class RosterItemOperation {
+ public:
+ RosterItemOperation(bool requiresLookup = false, const JID& lookupJID = JID()) : requiresLookup_(requiresLookup), lookupJID_(lookupJID) {};
+ virtual ~RosterItemOperation() {};
+ bool requiresLookup() const {return requiresLookup_;};
+ const JID& lookupJID() const {return lookupJID_;};
+ /**
+ * This is called when iterating over possible subjects, so must check it's
+ * applying to the right items - even if requiresLookup() is true an item
+ * with the same bare JID but different full JID may be passed.
+ */
+ virtual void operator() (RosterItem*) const = 0;
+ private:
+ bool requiresLookup_;
+ JID lookupJID_;
+};
+
+}
diff --git a/Swift/Controllers/Roster/SetAvatar.h b/Swift/Controllers/Roster/SetAvatar.h
new file mode 100644
index 0000000..2b9cfa8
--- /dev/null
+++ b/Swift/Controllers/Roster/SetAvatar.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include "Swiften/Elements/Presence.h"
+#include "Swiften/JID/JID.h"
+#include "Swift/Controllers/Roster/RosterItemOperation.h"
+#include "Swift/Controllers/Roster/ContactRosterItem.h"
+
+namespace Swift {
+
+class RosterItem;
+
+class SetAvatar : public RosterItemOperation {
+ public:
+ SetAvatar(const JID& jid, const String& path, JID::CompareType compareType = JID::WithoutResource) : RosterItemOperation(true, jid), jid_(jid), path_(path), compareType_(compareType) {
+ }
+
+ virtual void operator() (RosterItem* item) const {
+ ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(item);
+ if (contact && contact->getJID().equals(jid_, compareType_)) {
+ contact->setAvatarPath(path_);
+ }
+ }
+
+ private:
+ JID jid_;
+ String path_;
+ JID::CompareType compareType_;
+};
+
+}
diff --git a/Swift/Controllers/Roster/SetName.h b/Swift/Controllers/Roster/SetName.h
new file mode 100644
index 0000000..4d75392
--- /dev/null
+++ b/Swift/Controllers/Roster/SetName.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2010 Kevin Smith
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include "Swiften/JID/JID.h"
+#include "Swift/Controllers/Roster/RosterItemOperation.h"
+#include "Swift/Controllers/Roster/ContactRosterItem.h"
+
+namespace Swift {
+
+class RosterItem;
+
+class SetName : public RosterItemOperation {
+ public:
+ SetName(const String& name, const JID& jid, JID::CompareType compareType = JID::WithoutResource) : RosterItemOperation(true, jid), name_(name), jid_(jid), compareType_(compareType) {
+ }
+
+ virtual void operator() (RosterItem* item) const {
+ ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(item);
+ if (contact && contact->getJID().equals(jid_, compareType_)) {
+ contact->setDisplayName(name_);
+ }
+ }
+
+ private:
+ String name_;
+ JID jid_;
+ JID::CompareType compareType_;
+};
+
+}
+
+
diff --git a/Swift/Controllers/Roster/SetPresence.h b/Swift/Controllers/Roster/SetPresence.h
new file mode 100644
index 0000000..06adfa4
--- /dev/null
+++ b/Swift/Controllers/Roster/SetPresence.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include "Swiften/Elements/Presence.h"
+#include "Swiften/JID/JID.h"
+#include "Swift/Controllers/Roster/RosterItemOperation.h"
+#include "Swift/Controllers/Roster/ContactRosterItem.h"
+
+namespace Swift {
+
+class RosterItem;
+
+class SetPresence : public RosterItemOperation {
+ public:
+ SetPresence(Presence::ref presence, JID::CompareType compareType = JID::WithoutResource) : RosterItemOperation(true, compareType == JID::WithoutResource ? presence->getFrom().toBare() : presence->getFrom()), presence_(presence), compareType_(compareType) {
+ }
+
+ virtual void operator() (RosterItem* item) const {
+ ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(item);
+ if (contact && contact->getJID().equals(presence_->getFrom(), compareType_)) {
+ contact->applyPresence(presence_->getFrom().getResource(), presence_);
+ }
+ }
+
+ private:
+ Presence::ref presence_;
+ JID::CompareType compareType_;
+};
+
+}
+
diff --git a/Swift/Controllers/Roster/UnitTest/RosterControllerTest.cpp b/Swift/Controllers/Roster/UnitTest/RosterControllerTest.cpp
new file mode 100644
index 0000000..604cda6
--- /dev/null
+++ b/Swift/Controllers/Roster/UnitTest/RosterControllerTest.cpp
@@ -0,0 +1,290 @@
+
+/*
+ * Copyright (c) 2010 Kevin Smith
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include <cppunit/extensions/HelperMacros.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+
+#include "Swift/Controllers/Roster/RosterController.h"
+#include "Swift/Controllers/UnitTest/MockMainWindowFactory.h"
+// #include "Swiften/Elements/Payload.h"
+// #include "Swiften/Elements/RosterItemPayload.h"
+// #include "Swiften/Elements/RosterPayload.h"
+#include "Swiften/Queries/DummyIQChannel.h"
+#include "Swiften/Client/DummyStanzaChannel.h"
+#include "Swiften/Queries/IQRouter.h"
+#include "Swiften/Roster/XMPPRosterImpl.h"
+#include "Swift/Controllers/Roster/Roster.h"
+#include "Swift/Controllers/Roster/GroupRosterItem.h"
+#include "Swift/Controllers/Roster/ContactRosterItem.h"
+#include "Swift/Controllers/Settings/DummySettingsProvider.h"
+#include "Swiften/Avatars/NullAvatarManager.h"
+#include "Swift/Controllers/XMPPEvents/EventController.h"
+#include "Swiften/Presence/PresenceOracle.h"
+#include "Swiften/Presence/SubscriptionManager.h"
+#include "Swiften/Client/NickResolver.h"
+#include "Swift/Controllers/UIEvents/UIEventStream.h"
+#include "Swift/Controllers/UIEvents/RenameRosterItemUIEvent.h"
+#include "Swiften/MUC/MUCRegistry.h"
+#include <Swiften/Client/DummyNickManager.h>
+
+using namespace Swift;
+
+#define CHILDREN mainWindow_->roster->getRoot()->getChildren()
+
+class RosterControllerTest : public CppUnit::TestFixture {
+ CPPUNIT_TEST_SUITE(RosterControllerTest);
+ CPPUNIT_TEST(testAdd);
+ CPPUNIT_TEST(testAddSubscription);
+ CPPUNIT_TEST(testReceiveRename);
+ CPPUNIT_TEST(testSendRename);
+ CPPUNIT_TEST(testPresence);
+ CPPUNIT_TEST(testHighestPresence);
+ CPPUNIT_TEST(testNotHighestPresence);
+ CPPUNIT_TEST(testUnavailablePresence);
+ CPPUNIT_TEST_SUITE_END();
+
+ public:
+ void setUp() {
+ jid_ = JID("testjid@swift.im/swift");
+ xmppRoster_ = new XMPPRosterImpl();
+ avatarManager_ = new NullAvatarManager();
+ mainWindowFactory_ = new MockMainWindowFactory();
+ mucRegistry_ = new MUCRegistry();
+ nickResolver_ = new NickResolver(jid_.toBare(), xmppRoster_, NULL, mucRegistry_);
+ channel_ = new DummyIQChannel();
+ router_ = new IQRouter(channel_);
+ stanzaChannel_ = new DummyStanzaChannel();
+ presenceOracle_ = new PresenceOracle(stanzaChannel_);
+ subscriptionManager_ = new SubscriptionManager(stanzaChannel_);
+ eventController_ = new EventController();
+ uiEventStream_ = new UIEventStream();
+ settings_ = new DummySettingsProvider();
+ nickManager_ = new DummyNickManager();
+ rosterController_ = new RosterController(jid_, xmppRoster_, avatarManager_, mainWindowFactory_, nickManager_, nickResolver_, presenceOracle_, subscriptionManager_, eventController_, uiEventStream_, router_, settings_);
+ mainWindow_ = mainWindowFactory_->last;
+ };
+
+ void tearDown() {
+ delete rosterController_;
+ delete nickManager_;
+ delete nickResolver_;
+ delete mucRegistry_;
+ delete mainWindowFactory_;
+ delete avatarManager_;
+ delete router_;
+ delete channel_;
+ delete eventController_;
+ delete subscriptionManager_;
+ delete presenceOracle_;
+ delete stanzaChannel_;
+ delete uiEventStream_;
+ delete settings_;
+ delete xmppRoster_;
+ };
+
+ GroupRosterItem* groupChild(size_t i) {
+ return dynamic_cast<GroupRosterItem*>(CHILDREN[i]);
+ }
+
+ JID withResource(const JID& jid, const String& resource) {
+ return JID(jid.toBare().toString() + "/" + resource);
+ }
+
+ void testPresence() {
+ std::vector<String> groups;
+ groups.push_back("testGroup1");
+ groups.push_back("testGroup2");
+ JID from("test@testdomain.com");
+ xmppRoster_->addContact(from, "name", groups, RosterItemPayload::Both);
+ Presence::ref presence(new Presence());
+ presence->setFrom(withResource(from, "bob"));
+ presence->setPriority(2);
+ presence->setStatus("So totally here");
+ stanzaChannel_->onPresenceReceived(presence);
+ ContactRosterItem* item = dynamic_cast<ContactRosterItem*>(dynamic_cast<GroupRosterItem*>(CHILDREN[0])->getChildren()[0]);
+ CPPUNIT_ASSERT(item);
+ CPPUNIT_ASSERT_EQUAL(presence->getStatus(), item->getStatusText());
+ ContactRosterItem* item2 = dynamic_cast<ContactRosterItem*>(dynamic_cast<GroupRosterItem*>(CHILDREN[1])->getChildren()[0]);
+ CPPUNIT_ASSERT(item2);
+ CPPUNIT_ASSERT_EQUAL(presence->getStatus(), item2->getStatusText());
+
+ };
+
+ void testHighestPresence() {
+ std::vector<String> groups;
+ groups.push_back("testGroup1");
+ JID from("test@testdomain.com");
+ xmppRoster_->addContact(from, "name", groups, RosterItemPayload::Both);
+ Presence::ref lowPresence(new Presence());
+ lowPresence->setFrom(withResource(from, "bob"));
+ lowPresence->setPriority(2);
+ lowPresence->setStatus("Not here");
+ Presence::ref highPresence(new Presence());
+ highPresence->setFrom(withResource(from, "bert"));
+ highPresence->setPriority(10);
+ highPresence->setStatus("So totally here");
+ stanzaChannel_->onPresenceReceived(lowPresence);
+ stanzaChannel_->onPresenceReceived(highPresence);
+ ContactRosterItem* item = dynamic_cast<ContactRosterItem*>(dynamic_cast<GroupRosterItem*>(CHILDREN[0])->getChildren()[0]);
+ CPPUNIT_ASSERT(item);
+ CPPUNIT_ASSERT_EQUAL(highPresence->getStatus(), item->getStatusText());
+ };
+
+ void testNotHighestPresence() {
+ std::vector<String> groups;
+ groups.push_back("testGroup1");
+ JID from("test@testdomain.com");
+ xmppRoster_->addContact(from, "name", groups, RosterItemPayload::Both);
+ Presence::ref lowPresence(new Presence());
+ lowPresence->setFrom(withResource(from, "bob"));
+ lowPresence->setPriority(2);
+ lowPresence->setStatus("Not here");
+ Presence::ref highPresence(new Presence());
+ highPresence->setFrom(withResource(from, "bert"));
+ highPresence->setPriority(10);
+ highPresence->setStatus("So totally here");
+ stanzaChannel_->onPresenceReceived(highPresence);
+ stanzaChannel_->onPresenceReceived(lowPresence);
+ ContactRosterItem* item = dynamic_cast<ContactRosterItem*>(dynamic_cast<GroupRosterItem*>(CHILDREN[0])->getChildren()[0]);
+ CPPUNIT_ASSERT(item);
+ CPPUNIT_ASSERT_EQUAL(highPresence->getStatus(), item->getStatusText());
+ };
+
+ void testUnavailablePresence() {
+ std::vector<String> groups;
+ groups.push_back("testGroup1");
+ JID from("test@testdomain.com");
+ xmppRoster_->addContact(from, "name", groups, RosterItemPayload::Both);
+ Presence::ref lowPresence(new Presence());
+ lowPresence->setFrom(withResource(from, "bob"));
+ lowPresence->setPriority(2);
+ lowPresence->setStatus("Not here");
+ Presence::ref highPresence(new Presence());
+ highPresence->setFrom(withResource(from, "bert"));
+ highPresence->setPriority(10);
+ highPresence->setStatus("So totally here");
+ Presence::ref highPresenceOffline(new Presence());
+ highPresenceOffline->setFrom(withResource(from, "bert"));
+ highPresenceOffline->setType(Presence::Unavailable);
+ Presence::ref lowPresenceOffline(new Presence());
+ lowPresenceOffline->setFrom(withResource(from, "bob"));
+ lowPresenceOffline->setStatus("Signing out");
+ lowPresenceOffline->setType(Presence::Unavailable);
+ stanzaChannel_->onPresenceReceived(lowPresence);
+ stanzaChannel_->onPresenceReceived(highPresence);
+ stanzaChannel_->onPresenceReceived(highPresenceOffline);
+ ContactRosterItem* item = dynamic_cast<ContactRosterItem*>(dynamic_cast<GroupRosterItem*>(CHILDREN[0])->getChildren()[0]);
+ CPPUNIT_ASSERT(item);
+ /* A verification that if the test fails, it's the RosterController, not the PresenceOracle. */
+ Presence::ref high = presenceOracle_->getHighestPriorityPresence(from);
+ CPPUNIT_ASSERT_EQUAL(Presence::Available, high->getType());
+ CPPUNIT_ASSERT_EQUAL(lowPresence->getStatus(), high->getStatus());
+ CPPUNIT_ASSERT_EQUAL(StatusShow::Online, item->getStatusShow());
+ CPPUNIT_ASSERT_EQUAL(lowPresence->getStatus(), item->getStatusText());
+ stanzaChannel_->onPresenceReceived(lowPresenceOffline);
+ item = dynamic_cast<ContactRosterItem*>(dynamic_cast<GroupRosterItem*>(CHILDREN[0])->getChildren()[0]);
+ CPPUNIT_ASSERT(item);
+ /* A verification that if the test fails, it's the RosterController, not the PresenceOracle. */
+ high = presenceOracle_->getHighestPriorityPresence(from);
+ CPPUNIT_ASSERT_EQUAL(Presence::Unavailable, high->getType());
+ CPPUNIT_ASSERT_EQUAL(lowPresenceOffline->getStatus(), high->getStatus());
+ CPPUNIT_ASSERT_EQUAL(StatusShow::None, item->getStatusShow());
+ CPPUNIT_ASSERT_EQUAL(lowPresenceOffline->getStatus(), item->getStatusText());
+ };
+
+ void testAdd() {
+ std::vector<String> groups;
+ groups.push_back("testGroup1");
+ groups.push_back("testGroup2");
+ xmppRoster_->addContact(JID("test@testdomain.com/bob"), "name", groups, RosterItemPayload::Both);
+
+ CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(CHILDREN.size()));
+ //CPPUNIT_ASSERT_EQUAL(String("Bob"), xmppRoster_->getNameForJID(JID("foo@bar.com")));
+ };
+
+ void testAddSubscription() {
+ std::vector<String> groups;
+ JID jid("test@testdomain.com");
+ xmppRoster_->addContact(jid, "name", groups, RosterItemPayload::None);
+
+ CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(CHILDREN.size()));
+ CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(groupChild(0)->getChildren().size()));
+ xmppRoster_->addContact(jid, "name", groups, RosterItemPayload::To);
+ CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(CHILDREN.size()));
+ CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(groupChild(0)->getChildren().size()));
+
+ xmppRoster_->addContact(jid, "name", groups, RosterItemPayload::Both);
+ CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(CHILDREN.size()));
+ CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(groupChild(0)->getChildren().size()));
+
+ };
+
+ void testReceiveRename() {
+ std::vector<String> groups;
+ JID jid("test@testdomain.com");
+ xmppRoster_->addContact(jid, "name", groups, RosterItemPayload::Both);
+
+ CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(CHILDREN.size()));
+ CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(groupChild(0)->getChildren().size()));
+ CPPUNIT_ASSERT_EQUAL(String("name"), groupChild(0)->getChildren()[0]->getDisplayName());
+ xmppRoster_->addContact(jid, "NewName", groups, RosterItemPayload::Both);
+ CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(CHILDREN.size()));
+ CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(groupChild(0)->getChildren().size()));
+ CPPUNIT_ASSERT_EQUAL(String("NewName"), groupChild(0)->getChildren()[0]->getDisplayName());
+ };
+
+ void testSendRename() {
+ JID jid("testling@wonderland.lit");
+ std::vector<String> groups;
+ groups.push_back("Friends");
+ groups.push_back("Enemies");
+ xmppRoster_->addContact(jid, "Bob", groups, RosterItemPayload::From);
+ CPPUNIT_ASSERT_EQUAL(groups.size(), xmppRoster_->getGroupsForJID(jid).size());
+ uiEventStream_->send(boost::shared_ptr<UIEvent>(new RenameRosterItemUIEvent(jid, "Robert")));
+ CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), channel_->iqs_.size());
+ CPPUNIT_ASSERT_EQUAL(IQ::Set, channel_->iqs_[0]->getType());
+ boost::shared_ptr<RosterPayload> payload = channel_->iqs_[0]->getPayload<RosterPayload>();
+ CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), payload->getItems().size());
+ RosterItemPayload item = payload->getItems()[0];
+ CPPUNIT_ASSERT_EQUAL(jid, item.getJID());
+ CPPUNIT_ASSERT_EQUAL(String("Robert"), item.getName());
+
+ CPPUNIT_ASSERT_EQUAL(groups.size(), item.getGroups().size());
+ assertVectorsEqual(groups, item.getGroups(), __LINE__);
+ }
+
+ void assertVectorsEqual(const std::vector<String>& v1, const std::vector<String>& v2, int line) {
+ foreach (const String& entry, v1) {
+ if (std::find(v2.begin(), v2.end(), entry) == v2.end()) {
+ std::stringstream stream;
+ stream << "Couldn't find " << entry.getUTF8String() << " in v2 (line " << line << ")";
+ CPPUNIT_FAIL(stream.str());
+ }
+ }
+ }
+
+ private:
+ JID jid_;
+ XMPPRosterImpl* xmppRoster_;
+ MUCRegistry* mucRegistry_;
+ AvatarManager* avatarManager_;
+ MockMainWindowFactory* mainWindowFactory_;
+ NickManager* nickManager_;
+ NickResolver* nickResolver_;
+ RosterController* rosterController_;
+ DummyIQChannel* channel_;
+ DummyStanzaChannel* stanzaChannel_;
+ IQRouter* router_;
+ PresenceOracle* presenceOracle_;
+ SubscriptionManager* subscriptionManager_;
+ EventController* eventController_;
+ UIEventStream* uiEventStream_;
+ MockMainWindow* mainWindow_;
+ DummySettingsProvider* settings_;
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION(RosterControllerTest);
diff --git a/Swift/Controllers/Roster/UnitTest/RosterTest.cpp b/Swift/Controllers/Roster/UnitTest/RosterTest.cpp
new file mode 100644
index 0000000..754f3e1
--- /dev/null
+++ b/Swift/Controllers/Roster/UnitTest/RosterTest.cpp
@@ -0,0 +1,128 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include <cppunit/extensions/HelperMacros.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+#include <boost/shared_ptr.hpp>
+
+#include "Swift/Controllers/Roster/Roster.h"
+#include "Swift/Controllers/Roster/GroupRosterItem.h"
+#include "Swift/Controllers/Roster/SetPresence.h"
+
+using namespace Swift;
+
+class RosterTest : public CppUnit::TestFixture {
+ CPPUNIT_TEST_SUITE(RosterTest);
+ CPPUNIT_TEST(testGetGroup);
+ CPPUNIT_TEST(testRemoveContact);
+ CPPUNIT_TEST(testRemoveSecondContact);
+ CPPUNIT_TEST(testRemoveSecondContactSameBare);
+ CPPUNIT_TEST(testApplyPresenceLikeMUC);
+ CPPUNIT_TEST_SUITE_END();
+
+ public:
+ void setUp() {
+ jid1_ = JID("a@b.c");
+ jid2_ = JID("b@c.d");
+ jid3_ = JID("c@d.e");
+ roster_ = new Roster();
+ }
+
+ void tearDown() {
+ delete roster_;
+ }
+
+ void testGetGroup() {
+ roster_->addContact(jid1_, JID(), "Bert", "group1", "");
+ roster_->addContact(jid2_, JID(), "Ernie", "group2", "");
+ roster_->addContact(jid3_, JID(), "Cookie", "group1", "");
+
+ CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(roster_->getRoot()->getChildren().size()));
+ CPPUNIT_ASSERT_EQUAL(String("group1"), roster_->getRoot()->getChildren()[0]->getDisplayName());
+ CPPUNIT_ASSERT_EQUAL(String("group2"), roster_->getRoot()->getChildren()[1]->getDisplayName());
+ CPPUNIT_ASSERT_EQUAL(String("Bert"), static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren()[0]->getDisplayName());
+ CPPUNIT_ASSERT_EQUAL(String("Cookie"), static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren()[1]->getDisplayName());
+ CPPUNIT_ASSERT_EQUAL(String("Ernie"), static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[1])->getChildren()[0]->getDisplayName());
+
+ }
+
+ void testRemoveContact() {
+ roster_->addContact(jid1_, jid1_, "Bert", "group1", "");
+ CPPUNIT_ASSERT_EQUAL(String("Bert"), static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren()[0]->getDisplayName());
+
+ roster_->removeContact(jid1_);
+ CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren().size()));
+ }
+
+ void testRemoveSecondContact() {
+ roster_->addContact(jid1_, jid1_, "Bert", "group1", "");
+ roster_->addContact(jid2_, jid2_, "Cookie", "group1", "");
+ CPPUNIT_ASSERT_EQUAL(String("Cookie"), static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren()[1]->getDisplayName());
+
+ roster_->removeContact(jid2_);
+ CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren().size()));
+ CPPUNIT_ASSERT_EQUAL(String("Bert"), static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren()[0]->getDisplayName());
+ }
+
+ void testRemoveSecondContactSameBare() {
+ JID jid4a("a@b/c");
+ JID jid4b("a@b/d");
+ roster_->addContact(jid4a, JID(), "Bert", "group1", "");
+ roster_->addContact(jid4b, JID(), "Cookie", "group1", "");
+ CPPUNIT_ASSERT_EQUAL(String("Cookie"), static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren()[1]->getDisplayName());
+
+ roster_->removeContact(jid4b);
+ CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren().size()));
+ CPPUNIT_ASSERT_EQUAL(String("Bert"), static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren()[0]->getDisplayName());
+ }
+
+ void testApplyPresenceLikeMUC() {
+ JID jid4a("a@b/c");
+ JID jid4b("a@b/d");
+ JID jid4c("a@b/e");
+ roster_->addContact(jid4a, JID(), "Bird", "group1", "");
+ roster_->addContact(jid4b, JID(), "Cookie", "group1", "");
+ roster_->removeContact(jid4b);
+ roster_->addContact(jid4c, JID(), "Bert", "group1", "");
+ roster_->addContact(jid4b, JID(), "Ernie", "group1", "");
+ boost::shared_ptr<Presence> presence(new Presence());
+ presence->setShow(StatusShow::DND);
+ presence->setFrom(jid4a);
+ roster_->applyOnItems(SetPresence(presence, JID::WithResource));
+ presence->setFrom(jid4b);
+ roster_->applyOnItems(SetPresence(presence, JID::WithResource));
+ presence->setFrom(jid4c);
+ roster_->applyOnItems(SetPresence(presence, JID::WithResource));
+
+ presence = boost::shared_ptr<Presence>(new Presence());
+ presence->setFrom(jid4b);
+ presence->setShow(StatusShow::Online);
+ roster_->applyOnItems(SetPresence(presence, JID::WithResource));
+ std::vector<RosterItem*> children = static_cast<GroupRosterItem*>(roster_->getRoot()->getDisplayedChildren()[0])->getDisplayedChildren();
+ CPPUNIT_ASSERT_EQUAL(3, static_cast<int>(children.size()));
+
+ /* Check order */
+ CPPUNIT_ASSERT_EQUAL(String("Ernie"), children[0]->getDisplayName());
+ CPPUNIT_ASSERT_EQUAL(String("Bert"), children[1]->getDisplayName());
+ CPPUNIT_ASSERT_EQUAL(String("Bird"), children[2]->getDisplayName());
+
+ presence = boost::shared_ptr<Presence>(new Presence());
+ presence->setFrom(jid4c);
+ presence->setType(Presence::Unavailable);
+ roster_->removeContact(jid4c);
+ roster_->applyOnItems(SetPresence(presence, JID::WithResource));
+
+ }
+
+ private:
+ Roster *roster_;
+ JID jid1_;
+ JID jid2_;
+ JID jid3_;
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION(RosterTest);
+