From d1837d06c5d655de759bbfcc66711a71c92296ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Remko=20Tron=C3=A7on?= Date: Mon, 7 Feb 2011 19:18:59 +0100 Subject: Moved Swift-specific roster code out of Swiften. diff --git a/Swift/Controllers/Chat/MUCController.cpp b/Swift/Controllers/Chat/MUCController.cpp index a447f60..001251e 100644 --- a/Swift/Controllers/Chat/MUCController.cpp +++ b/Swift/Controllers/Chat/MUCController.cpp @@ -23,9 +23,9 @@ #include "Swiften/Elements/Delay.h" #include "Swiften/MUC/MUC.h" #include "Swiften/Client/StanzaChannel.h" -#include "Swiften/Roster/Roster.h" -#include "Swiften/Roster/SetAvatar.h" -#include "Swiften/Roster/SetPresence.h" +#include "Swift/Controllers/Roster/Roster.h" +#include "Swift/Controllers/Roster/SetAvatar.h" +#include "Swift/Controllers/Roster/SetPresence.h" #define MUC_JOIN_WARNING_TIMEOUT_MILLISECONDS 60000 diff --git a/Swift/Controllers/ContactEditController.cpp b/Swift/Controllers/ContactEditController.cpp index 286fdeb..a8f7811 100644 --- a/Swift/Controllers/ContactEditController.cpp +++ b/Swift/Controllers/ContactEditController.cpp @@ -13,7 +13,7 @@ #include #include #include -#include +#include namespace Swift { diff --git a/Swift/Controllers/MainController.cpp b/Swift/Controllers/MainController.cpp index 3e1b366..98599bb 100644 --- a/Swift/Controllers/MainController.cpp +++ b/Swift/Controllers/MainController.cpp @@ -28,7 +28,7 @@ #include "Swift/Controllers/UIInterfaces/MainWindow.h" #include "Swift/Controllers/Chat/MUCController.h" #include "Swiften/Client/NickResolver.h" -#include "Swift/Controllers/RosterController.h" +#include "Swift/Controllers/Roster/RosterController.h" #include "Swift/Controllers/SoundEventController.h" #include "Swift/Controllers/SoundPlayer.h" #include "Swift/Controllers/StatusTracker.h" 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 +#include + +namespace Swift { + +class RosterItem; + +class AppearOffline : public RosterItemOperation { + public: + AppearOffline() { + } + + virtual void operator() (RosterItem* item) const { + ContactRosterItem* contact = dynamic_cast(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 > StringPresencePair; + +void ContactRosterItem::calculateShownPresence() { + shownPresence_ = offlinePresence_; + foreach (StringPresencePair presencePair, presences_) { + boost::shared_ptr 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) { + if (offlinePresence_) { + offlinePresence_ = boost::shared_ptr(); + } + 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 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 +#include +#include "Swiften/Base/boost_bsignals.h" +#include + +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); + void clearPresence(); + void calculateShownPresence(); + const std::vector 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 > presences_; + boost::shared_ptr offlinePresence_; + boost::shared_ptr shownPresence_; + std::vector 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 +//#include +#include + +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& GroupRosterItem::getChildren() const { + return children_; +} + +const std::vector& GroupRosterItem::getDisplayedChildren() const { + return displayedChildren_; +} + +void GroupRosterItem::addChild(RosterItem* item) { + children_.push_back(item); + GroupRosterItem* group = dynamic_cast(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::iterator it = children_.begin(); + displayedChildren_.clear(); + while (it != children_.end()) { + ContactRosterItem* contact = dynamic_cast(*it); + if (contact) { + delete contact; + } else { + GroupRosterItem* group = dynamic_cast(*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::iterator it = children_.begin(); + ContactRosterItem* removed = NULL; + while (it != children_.end()) { + ContactRosterItem* contact = dynamic_cast(*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(*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(left); + const ContactRosterItem* rightContact = dynamic_cast(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 + +namespace Swift { + +class GroupRosterItem : public RosterItem { + public: + GroupRosterItem(const String& name, GroupRosterItem* parent, bool sortByStatus); + virtual ~GroupRosterItem(); + const std::vector& getChildren() const; + const std::vector& getDisplayedChildren() const; + void addChild(RosterItem* item); + ContactRosterItem* removeChild(const JID& jid); + void removeAll(); + void setDisplayed(RosterItem* item, bool displayed); + boost::signal 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 onExpandedChanged; + private: + void handleChildrenChanged(GroupRosterItem* group); + void handleDataChanged(RosterItem* item); + bool sortDisplayed(); + String name_; + bool expanded_; + std::vector children_; + std::vector 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(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 + +#include +#include + +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 queue; + queue.push_back(root_); + while (!queue.empty()) { + RosterItem* item = *queue.begin(); + queue.pop_front(); + GroupRosterItem* group = dynamic_cast(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(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* 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 children = root_->getChildren(); + std::vector::iterator it = children.begin(); + while (it != children.end()) { + GroupRosterItem* group = dynamic_cast(*it); + if (group && group->getDisplayName() == groupName) { + ContactRosterItem* deleted = group->removeChild(jid); + std::vector* 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 queue; + queue.push_back(root_); + while (!queue.empty()) { + RosterItem* item = *queue.begin(); + queue.pop_front(); + operation(item); + GroupRosterItem* group = dynamic_cast(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(child); + if (contact) { + filterContact(contact, group); + } + } +} + +void Roster::filterAll() { + std::deque queue; + queue.push_back(root_); + while (!queue.empty()) { + RosterItem *item = *queue.begin(); + queue.pop_front(); + GroupRosterItem* group = dynamic_cast(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 +#include +#include "Swiften/Base/boost_bsignals.h" +#include + +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 getFilters() {return filters_;}; + boost::signal onChildrenChanged; + boost::signal onGroupAdded; + boost::signal 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 filters_; + std::map > 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 +#include + +#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 + +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(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 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 passedOldGroups) { + if (oldName != xmppRoster_->getNameForJID(jid)) { + roster_->applyOnItems(SetName(nickResolver_->jidToNick(jid), jid)); + return; + } + std::vector groups = xmppRoster_->getGroupsForJID(jid); + std::vector 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 event) { + if (boost::shared_ptr showOfflineEvent = boost::dynamic_pointer_cast(event)) { + handleShowOfflineToggled(showOfflineEvent->getShow()); + } + else if (boost::shared_ptr addContactEvent = boost::dynamic_pointer_cast(event)) { + RosterItemPayload item; + item.setName(addContactEvent->getName()); + item.setJID(addContactEvent->getJID()); + boost::shared_ptr 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 removeEvent = boost::dynamic_pointer_cast(event)) { + RosterItemPayload item(removeEvent->getJID(), "", RosterItemPayload::Remove); + boost::shared_ptr 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 renameEvent = boost::dynamic_pointer_cast(event)) { + JID contact(renameEvent->getJID()); + RosterItemPayload item(contact, renameEvent->getNewName(), xmppRoster_->getSubscriptionStateForJID(contact)); + item.setGroups(xmppRoster_->getGroupsForJID(contact)); + boost::shared_ptr 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 renameGroupEvent = boost::dynamic_pointer_cast(event)) { + std::vector 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 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& 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(); + 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) { + 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(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 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 RosterController::getItem(const JID& jid) const { + return xmppRoster_->getItem(jid); +} + +std::set 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 + +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 onChangeStatusRequest; + boost::signal onSignOutRequest; + void handleAvatarChanged(const JID& jid); + void setEnabled(bool enabled); + + boost::optional getItem(const JID&) const; + std::set getGroups() const; + + void setContactGroups(const JID& jid, const std::vector& 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 oldGroups); + void handleStartChatRequest(const JID& contact); + void handleChangeStatusRequest(StatusShow::Type show, const String &statusText); + void handleShowOfflineToggled(bool state); + void handleIncomingPresence(boost::shared_ptr newPresence); + void handleSubscriptionRequest(const JID& jid, const String& message); + void handleSubscriptionRequestAccepted(SubscriptionRequestEvent* event); + void handleSubscriptionRequestDeclined(SubscriptionRequestEvent* event); + void handleUIEvent(boost::shared_ptr event); + void handleRosterSetError(ErrorPayload::ref error, boost::shared_ptr 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 +#include + +#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 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 +#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 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 + +#include "Swiften/Base/String.h" + +namespace Swift { +class GroupRosterItem; +class RosterItem { + public: + RosterItem(const String& name, GroupRosterItem* parent); + virtual ~RosterItem(); + boost::signal 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(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(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(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 +#include + +#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 + +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(CHILDREN[i]); + } + + JID withResource(const JID& jid, const String& resource) { + return JID(jid.toBare().toString() + "/" + resource); + } + + void testPresence() { + std::vector 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(dynamic_cast(CHILDREN[0])->getChildren()[0]); + CPPUNIT_ASSERT(item); + CPPUNIT_ASSERT_EQUAL(presence->getStatus(), item->getStatusText()); + ContactRosterItem* item2 = dynamic_cast(dynamic_cast(CHILDREN[1])->getChildren()[0]); + CPPUNIT_ASSERT(item2); + CPPUNIT_ASSERT_EQUAL(presence->getStatus(), item2->getStatusText()); + + }; + + void testHighestPresence() { + std::vector 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(dynamic_cast(CHILDREN[0])->getChildren()[0]); + CPPUNIT_ASSERT(item); + CPPUNIT_ASSERT_EQUAL(highPresence->getStatus(), item->getStatusText()); + }; + + void testNotHighestPresence() { + std::vector 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(dynamic_cast(CHILDREN[0])->getChildren()[0]); + CPPUNIT_ASSERT(item); + CPPUNIT_ASSERT_EQUAL(highPresence->getStatus(), item->getStatusText()); + }; + + void testUnavailablePresence() { + std::vector 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(dynamic_cast(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(dynamic_cast(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 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(CHILDREN.size())); + //CPPUNIT_ASSERT_EQUAL(String("Bob"), xmppRoster_->getNameForJID(JID("foo@bar.com"))); + }; + + void testAddSubscription() { + std::vector groups; + JID jid("test@testdomain.com"); + xmppRoster_->addContact(jid, "name", groups, RosterItemPayload::None); + + CPPUNIT_ASSERT_EQUAL(1, static_cast(CHILDREN.size())); + CPPUNIT_ASSERT_EQUAL(1, static_cast(groupChild(0)->getChildren().size())); + xmppRoster_->addContact(jid, "name", groups, RosterItemPayload::To); + CPPUNIT_ASSERT_EQUAL(1, static_cast(CHILDREN.size())); + CPPUNIT_ASSERT_EQUAL(1, static_cast(groupChild(0)->getChildren().size())); + + xmppRoster_->addContact(jid, "name", groups, RosterItemPayload::Both); + CPPUNIT_ASSERT_EQUAL(1, static_cast(CHILDREN.size())); + CPPUNIT_ASSERT_EQUAL(1, static_cast(groupChild(0)->getChildren().size())); + + }; + + void testReceiveRename() { + std::vector groups; + JID jid("test@testdomain.com"); + xmppRoster_->addContact(jid, "name", groups, RosterItemPayload::Both); + + CPPUNIT_ASSERT_EQUAL(1, static_cast(CHILDREN.size())); + CPPUNIT_ASSERT_EQUAL(1, static_cast(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(CHILDREN.size())); + CPPUNIT_ASSERT_EQUAL(1, static_cast(groupChild(0)->getChildren().size())); + CPPUNIT_ASSERT_EQUAL(String("NewName"), groupChild(0)->getChildren()[0]->getDisplayName()); + }; + + void testSendRename() { + JID jid("testling@wonderland.lit"); + std::vector 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(new RenameRosterItemUIEvent(jid, "Robert"))); + CPPUNIT_ASSERT_EQUAL(static_cast(1), channel_->iqs_.size()); + CPPUNIT_ASSERT_EQUAL(IQ::Set, channel_->iqs_[0]->getType()); + boost::shared_ptr payload = channel_->iqs_[0]->getPayload(); + CPPUNIT_ASSERT_EQUAL(static_cast(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& v1, const std::vector& 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 +#include +#include + +#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(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(roster_->getRoot()->getChildren()[0])->getChildren()[0]->getDisplayName()); + CPPUNIT_ASSERT_EQUAL(String("Cookie"), static_cast(roster_->getRoot()->getChildren()[0])->getChildren()[1]->getDisplayName()); + CPPUNIT_ASSERT_EQUAL(String("Ernie"), static_cast(roster_->getRoot()->getChildren()[1])->getChildren()[0]->getDisplayName()); + + } + + void testRemoveContact() { + roster_->addContact(jid1_, jid1_, "Bert", "group1", ""); + CPPUNIT_ASSERT_EQUAL(String("Bert"), static_cast(roster_->getRoot()->getChildren()[0])->getChildren()[0]->getDisplayName()); + + roster_->removeContact(jid1_); + CPPUNIT_ASSERT_EQUAL(0, static_cast(static_cast(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(roster_->getRoot()->getChildren()[0])->getChildren()[1]->getDisplayName()); + + roster_->removeContact(jid2_); + CPPUNIT_ASSERT_EQUAL(1, static_cast(static_cast(roster_->getRoot()->getChildren()[0])->getChildren().size())); + CPPUNIT_ASSERT_EQUAL(String("Bert"), static_cast(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(roster_->getRoot()->getChildren()[0])->getChildren()[1]->getDisplayName()); + + roster_->removeContact(jid4b); + CPPUNIT_ASSERT_EQUAL(1, static_cast(static_cast(roster_->getRoot()->getChildren()[0])->getChildren().size())); + CPPUNIT_ASSERT_EQUAL(String("Bert"), static_cast(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(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(new Presence()); + presence->setFrom(jid4b); + presence->setShow(StatusShow::Online); + roster_->applyOnItems(SetPresence(presence, JID::WithResource)); + std::vector children = static_cast(roster_->getRoot()->getDisplayedChildren()[0])->getDisplayedChildren(); + CPPUNIT_ASSERT_EQUAL(3, static_cast(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(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); + diff --git a/Swift/Controllers/RosterController.cpp b/Swift/Controllers/RosterController.cpp deleted file mode 100644 index be735cf..0000000 --- a/Swift/Controllers/RosterController.cpp +++ /dev/null @@ -1,298 +0,0 @@ -/* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. - */ - -#include "Swift/Controllers/RosterController.h" - -#include -#include - -#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 "Swiften/Roster/Roster.h" -#include "Swiften/Roster/SetPresence.h" -#include "Swiften/Roster/AppearOffline.h" -#include "Swiften/Roster/SetAvatar.h" -#include "Swiften/Roster/SetName.h" -#include "Swiften/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 - -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(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 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 passedOldGroups) { - if (oldName != xmppRoster_->getNameForJID(jid)) { - roster_->applyOnItems(SetName(nickResolver_->jidToNick(jid), jid)); - return; - } - std::vector groups = xmppRoster_->getGroupsForJID(jid); - std::vector 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 event) { - if (boost::shared_ptr showOfflineEvent = boost::dynamic_pointer_cast(event)) { - handleShowOfflineToggled(showOfflineEvent->getShow()); - } - else if (boost::shared_ptr addContactEvent = boost::dynamic_pointer_cast(event)) { - RosterItemPayload item; - item.setName(addContactEvent->getName()); - item.setJID(addContactEvent->getJID()); - boost::shared_ptr 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 removeEvent = boost::dynamic_pointer_cast(event)) { - RosterItemPayload item(removeEvent->getJID(), "", RosterItemPayload::Remove); - boost::shared_ptr 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 renameEvent = boost::dynamic_pointer_cast(event)) { - JID contact(renameEvent->getJID()); - RosterItemPayload item(contact, renameEvent->getNewName(), xmppRoster_->getSubscriptionStateForJID(contact)); - item.setGroups(xmppRoster_->getGroupsForJID(contact)); - boost::shared_ptr 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 renameGroupEvent = boost::dynamic_pointer_cast(event)) { - std::vector 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 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& 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(); - 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) { - 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(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 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 RosterController::getItem(const JID& jid) const { - return xmppRoster_->getItem(jid); -} - -std::set RosterController::getGroups() const { - return xmppRoster_->getGroups(); -} - -} diff --git a/Swift/Controllers/RosterController.h b/Swift/Controllers/RosterController.h deleted file mode 100644 index d8c2487..0000000 --- a/Swift/Controllers/RosterController.h +++ /dev/null @@ -1,93 +0,0 @@ -/* - * 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 "Swift/Controllers/RosterGroupExpandinessPersister.h" - -#include "Swiften/Base/boost_bsignals.h" -#include - -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 onChangeStatusRequest; - boost::signal onSignOutRequest; - void handleAvatarChanged(const JID& jid); - void setEnabled(bool enabled); - - boost::optional getItem(const JID&) const; - std::set getGroups() const; - - void setContactGroups(const JID& jid, const std::vector& 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 oldGroups); - void handleStartChatRequest(const JID& contact); - void handleChangeStatusRequest(StatusShow::Type show, const String &statusText); - void handleShowOfflineToggled(bool state); - void handleIncomingPresence(boost::shared_ptr newPresence); - void handleSubscriptionRequest(const JID& jid, const String& message); - void handleSubscriptionRequestAccepted(SubscriptionRequestEvent* event); - void handleSubscriptionRequestDeclined(SubscriptionRequestEvent* event); - void handleUIEvent(boost::shared_ptr event); - void handleRosterSetError(ErrorPayload::ref error, boost::shared_ptr 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/RosterGroupExpandinessPersister.cpp b/Swift/Controllers/RosterGroupExpandinessPersister.cpp deleted file mode 100644 index d532fcb..0000000 --- a/Swift/Controllers/RosterGroupExpandinessPersister.cpp +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. - */ - -#include "Swift/Controllers/RosterGroupExpandinessPersister.h" - -#include -#include - -#include "Swiften/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 collapsed = saved.split('\n'); - collapsed_.insert(collapsed.begin(), collapsed.end()); -} - -const String RosterGroupExpandinessPersister::SettingPath = "GroupExpandiness"; - -} diff --git a/Swift/Controllers/RosterGroupExpandinessPersister.h b/Swift/Controllers/RosterGroupExpandinessPersister.h deleted file mode 100644 index 0b88a48..0000000 --- a/Swift/Controllers/RosterGroupExpandinessPersister.h +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. - */ - -#pragma once - -#include -#include "Swiften/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 collapsed_; - Roster* roster_; - SettingsProvider* settings_; - static const String SettingPath; - }; -} diff --git a/Swift/Controllers/SConscript b/Swift/Controllers/SConscript index c8314de..c563c7b 100644 --- a/Swift/Controllers/SConscript +++ b/Swift/Controllers/SConscript @@ -31,8 +31,12 @@ if env["SCONS_STAGE"] == "build" : "MainController.cpp", "ProfileController.cpp", "ContactEditController.cpp", - "RosterController.cpp", - "RosterGroupExpandinessPersister.cpp", + "Roster/RosterController.cpp", + "Roster/RosterGroupExpandinessPersister.cpp", + "Roster/ContactRosterItem.cpp", + "Roster/GroupRosterItem.cpp", + "Roster/RosterItem.cpp", + "Roster/Roster.cpp", "EventWindowController.cpp", "SoundEventController.cpp", "SystemTrayController.cpp", @@ -51,7 +55,8 @@ if env["SCONS_STAGE"] == "build" : ]) env.Append(UNITTEST_SOURCES = [ - File("UnitTest/RosterControllerTest.cpp"), + File("Roster/UnitTest/RosterControllerTest.cpp"), + File("Roster/UnitTest/RosterTest.cpp"), File("UnitTest/PreviousStatusStoreTest.cpp"), File("UnitTest/PresenceNotifierTest.cpp"), File("Chat/UnitTest/ChatsManagerTest.cpp"), diff --git a/Swift/Controllers/UnitTest/RosterControllerTest.cpp b/Swift/Controllers/UnitTest/RosterControllerTest.cpp deleted file mode 100644 index 2f6f0da..0000000 --- a/Swift/Controllers/UnitTest/RosterControllerTest.cpp +++ /dev/null @@ -1,290 +0,0 @@ - -/* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. - */ - -#include -#include - -#include "Swift/Controllers/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 "Swiften/Roster/Roster.h" -#include "Swiften/Roster/GroupRosterItem.h" -#include "Swiften/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 - -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(CHILDREN[i]); - } - - JID withResource(const JID& jid, const String& resource) { - return JID(jid.toBare().toString() + "/" + resource); - } - - void testPresence() { - std::vector 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(dynamic_cast(CHILDREN[0])->getChildren()[0]); - CPPUNIT_ASSERT(item); - CPPUNIT_ASSERT_EQUAL(presence->getStatus(), item->getStatusText()); - ContactRosterItem* item2 = dynamic_cast(dynamic_cast(CHILDREN[1])->getChildren()[0]); - CPPUNIT_ASSERT(item2); - CPPUNIT_ASSERT_EQUAL(presence->getStatus(), item2->getStatusText()); - - }; - - void testHighestPresence() { - std::vector 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(dynamic_cast(CHILDREN[0])->getChildren()[0]); - CPPUNIT_ASSERT(item); - CPPUNIT_ASSERT_EQUAL(highPresence->getStatus(), item->getStatusText()); - }; - - void testNotHighestPresence() { - std::vector 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(dynamic_cast(CHILDREN[0])->getChildren()[0]); - CPPUNIT_ASSERT(item); - CPPUNIT_ASSERT_EQUAL(highPresence->getStatus(), item->getStatusText()); - }; - - void testUnavailablePresence() { - std::vector 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(dynamic_cast(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(dynamic_cast(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 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(CHILDREN.size())); - //CPPUNIT_ASSERT_EQUAL(String("Bob"), xmppRoster_->getNameForJID(JID("foo@bar.com"))); - }; - - void testAddSubscription() { - std::vector groups; - JID jid("test@testdomain.com"); - xmppRoster_->addContact(jid, "name", groups, RosterItemPayload::None); - - CPPUNIT_ASSERT_EQUAL(1, static_cast(CHILDREN.size())); - CPPUNIT_ASSERT_EQUAL(1, static_cast(groupChild(0)->getChildren().size())); - xmppRoster_->addContact(jid, "name", groups, RosterItemPayload::To); - CPPUNIT_ASSERT_EQUAL(1, static_cast(CHILDREN.size())); - CPPUNIT_ASSERT_EQUAL(1, static_cast(groupChild(0)->getChildren().size())); - - xmppRoster_->addContact(jid, "name", groups, RosterItemPayload::Both); - CPPUNIT_ASSERT_EQUAL(1, static_cast(CHILDREN.size())); - CPPUNIT_ASSERT_EQUAL(1, static_cast(groupChild(0)->getChildren().size())); - - }; - - void testReceiveRename() { - std::vector groups; - JID jid("test@testdomain.com"); - xmppRoster_->addContact(jid, "name", groups, RosterItemPayload::Both); - - CPPUNIT_ASSERT_EQUAL(1, static_cast(CHILDREN.size())); - CPPUNIT_ASSERT_EQUAL(1, static_cast(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(CHILDREN.size())); - CPPUNIT_ASSERT_EQUAL(1, static_cast(groupChild(0)->getChildren().size())); - CPPUNIT_ASSERT_EQUAL(String("NewName"), groupChild(0)->getChildren()[0]->getDisplayName()); - }; - - void testSendRename() { - JID jid("testling@wonderland.lit"); - std::vector 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(new RenameRosterItemUIEvent(jid, "Robert"))); - CPPUNIT_ASSERT_EQUAL(static_cast(1), channel_->iqs_.size()); - CPPUNIT_ASSERT_EQUAL(IQ::Set, channel_->iqs_[0]->getType()); - boost::shared_ptr payload = channel_->iqs_[0]->getPayload(); - CPPUNIT_ASSERT_EQUAL(static_cast(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& v1, const std::vector& 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/QtUI/QtChatWindow.cpp b/Swift/QtUI/QtChatWindow.cpp index ef31727..c8341dc 100644 --- a/Swift/QtUI/QtChatWindow.cpp +++ b/Swift/QtUI/QtChatWindow.cpp @@ -6,7 +6,7 @@ #include "QtChatWindow.h" #include "QtSwiftUtil.h" -#include "Swiften/Roster/Roster.h" +#include "Swift/Controllers/Roster/Roster.h" #include "Roster/QtTreeWidget.h" #include "SwifTools/Linkify.h" #include "QtChatView.h" diff --git a/Swift/QtUI/Roster/QtTreeWidget.cpp b/Swift/QtUI/Roster/QtTreeWidget.cpp index 3c0b303..90084c4 100644 --- a/Swift/QtUI/Roster/QtTreeWidget.cpp +++ b/Swift/QtUI/Roster/QtTreeWidget.cpp @@ -12,8 +12,8 @@ #include #include "Swiften/Base/Platform.h" -#include "Swiften/Roster/ContactRosterItem.h" -#include "Swiften/Roster/GroupRosterItem.h" +#include "Swift/Controllers/Roster/ContactRosterItem.h" +#include "Swift/Controllers/Roster/GroupRosterItem.h" #include "Swift/Controllers/UIEvents/UIEventStream.h" #include "Swift/Controllers/UIEvents/RequestChatUIEvent.h" #include "Swift/Controllers/UIEvents/RequestContactEditorUIEvent.h" diff --git a/Swift/QtUI/Roster/RosterDelegate.cpp b/Swift/QtUI/Roster/RosterDelegate.cpp index f5dcbd6..7dbf3cb 100644 --- a/Swift/QtUI/Roster/RosterDelegate.cpp +++ b/Swift/QtUI/Roster/RosterDelegate.cpp @@ -15,8 +15,8 @@ #include #include -#include "Swiften/Roster/ContactRosterItem.h" -#include "Swiften/Roster/GroupRosterItem.h" +#include "Swift/Controllers/Roster/ContactRosterItem.h" +#include "Swift/Controllers/Roster/GroupRosterItem.h" #include "QtTreeWidget.h" #include "RosterModel.h" diff --git a/Swift/QtUI/Roster/RosterModel.cpp b/Swift/QtUI/Roster/RosterModel.cpp index a6f40bb..95452c8 100644 --- a/Swift/QtUI/Roster/RosterModel.cpp +++ b/Swift/QtUI/Roster/RosterModel.cpp @@ -13,8 +13,8 @@ #include #include "Swiften/Elements/StatusShow.h" -#include "Swiften/Roster/ContactRosterItem.h" -#include "Swiften/Roster/GroupRosterItem.h" +#include "Swift/Controllers/Roster/ContactRosterItem.h" +#include "Swift/Controllers/Roster/GroupRosterItem.h" #include "QtSwiftUtil.h" #include "Swift/QtUI/Roster/QtTreeWidget.h" diff --git a/Swift/QtUI/Roster/RosterModel.h b/Swift/QtUI/Roster/RosterModel.h index 4d9e03c..964f680 100644 --- a/Swift/QtUI/Roster/RosterModel.h +++ b/Swift/QtUI/Roster/RosterModel.h @@ -6,7 +6,7 @@ #pragma once -#include "Swiften/Roster/Roster.h" +#include "Swift/Controllers/Roster/Roster.h" #include #include diff --git a/Swiften/Roster/AppearOffline.h b/Swiften/Roster/AppearOffline.h deleted file mode 100644 index 8e14190..0000000 --- a/Swiften/Roster/AppearOffline.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * 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/Roster/RosterItemOperation.h" -#include "Swiften/Roster/ContactRosterItem.h" - -namespace Swift { - -class RosterItem; - -class AppearOffline : public RosterItemOperation { - public: - AppearOffline() { - } - - virtual void operator() (RosterItem* item) const { - ContactRosterItem* contact = dynamic_cast(item); - if (contact) { - contact->clearPresence(); - } - } - -}; - -} - - diff --git a/Swiften/Roster/ContactRosterItem.cpp b/Swiften/Roster/ContactRosterItem.cpp deleted file mode 100644 index 9c7af32..0000000 --- a/Swiften/Roster/ContactRosterItem.cpp +++ /dev/null @@ -1,116 +0,0 @@ -/* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. - */ - -#include "Swiften/Roster/ContactRosterItem.h" -#include "Swiften/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 > StringPresencePair; - -void ContactRosterItem::calculateShownPresence() { - shownPresence_ = offlinePresence_; - foreach (StringPresencePair presencePair, presences_) { - boost::shared_ptr 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) { - if (offlinePresence_) { - offlinePresence_ = boost::shared_ptr(); - } - 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 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/Swiften/Roster/ContactRosterItem.h b/Swiften/Roster/ContactRosterItem.h deleted file mode 100644 index cb30989..0000000 --- a/Swiften/Roster/ContactRosterItem.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * 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 "Swiften/Roster/RosterItem.h" -#include "Swiften/Elements/StatusShow.h" -#include "Swiften/Elements/Presence.h" - -#include -#include -#include "Swiften/Base/boost_bsignals.h" -#include - -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); - void clearPresence(); - void calculateShownPresence(); - const std::vector 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 > presences_; - boost::shared_ptr offlinePresence_; - boost::shared_ptr shownPresence_; - std::vector groups_; -}; - -} - diff --git a/Swiften/Roster/GroupRosterItem.cpp b/Swiften/Roster/GroupRosterItem.cpp deleted file mode 100644 index 325c890..0000000 --- a/Swiften/Roster/GroupRosterItem.cpp +++ /dev/null @@ -1,201 +0,0 @@ -/* - * 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 -//#include -#include - -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& GroupRosterItem::getChildren() const { - return children_; -} - -const std::vector& GroupRosterItem::getDisplayedChildren() const { - return displayedChildren_; -} - -void GroupRosterItem::addChild(RosterItem* item) { - children_.push_back(item); - GroupRosterItem* group = dynamic_cast(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::iterator it = children_.begin(); - displayedChildren_.clear(); - while (it != children_.end()) { - ContactRosterItem* contact = dynamic_cast(*it); - if (contact) { - delete contact; - } else { - GroupRosterItem* group = dynamic_cast(*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::iterator it = children_.begin(); - ContactRosterItem* removed = NULL; - while (it != children_.end()) { - ContactRosterItem* contact = dynamic_cast(*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(*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(left); - const ContactRosterItem* rightContact = dynamic_cast(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/Swiften/Roster/GroupRosterItem.h b/Swiften/Roster/GroupRosterItem.h deleted file mode 100644 index b306b59..0000000 --- a/Swiften/Roster/GroupRosterItem.h +++ /dev/null @@ -1,45 +0,0 @@ -/* - * 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/Roster/RosterItem.h" -#include "Swiften/Base/String.h" -#include "Swiften/Roster/ContactRosterItem.h" - -#include - -namespace Swift { - -class GroupRosterItem : public RosterItem { - public: - GroupRosterItem(const String& name, GroupRosterItem* parent, bool sortByStatus); - virtual ~GroupRosterItem(); - const std::vector& getChildren() const; - const std::vector& getDisplayedChildren() const; - void addChild(RosterItem* item); - ContactRosterItem* removeChild(const JID& jid); - void removeAll(); - void setDisplayed(RosterItem* item, bool displayed); - boost::signal 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 onExpandedChanged; - private: - void handleChildrenChanged(GroupRosterItem* group); - void handleDataChanged(RosterItem* item); - bool sortDisplayed(); - String name_; - bool expanded_; - std::vector children_; - std::vector displayedChildren_; - bool sortByStatus_; -}; - -} - diff --git a/Swiften/Roster/OfflineRosterFilter.h b/Swiften/Roster/OfflineRosterFilter.h deleted file mode 100644 index 2a71900..0000000 --- a/Swiften/Roster/OfflineRosterFilter.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. - */ - -#ifndef SWIFTEN_OfflineRosterFilter_H -#define SWIFTEN_OfflineRosterFilter_H - -#include "Swiften/Roster/ContactRosterItem.h" -#include "Swiften/Roster/RosterItem.h" -#include "Swiften/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(item); - return contactItem && contactItem->getStatusShow() == StatusShow::None; - } -}; - -} -#endif - - - diff --git a/Swiften/Roster/Roster.cpp b/Swiften/Roster/Roster.cpp deleted file mode 100644 index 68bac53..0000000 --- a/Swiften/Roster/Roster.cpp +++ /dev/null @@ -1,209 +0,0 @@ -/* - * 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 - -#include -#include - -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 queue; - queue.push_back(root_); - while (!queue.empty()) { - RosterItem* item = *queue.begin(); - queue.pop_front(); - GroupRosterItem* group = dynamic_cast(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(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* 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 children = root_->getChildren(); - std::vector::iterator it = children.begin(); - while (it != children.end()) { - GroupRosterItem* group = dynamic_cast(*it); - if (group && group->getDisplayName() == groupName) { - ContactRosterItem* deleted = group->removeChild(jid); - std::vector* 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 queue; - queue.push_back(root_); - while (!queue.empty()) { - RosterItem* item = *queue.begin(); - queue.pop_front(); - operation(item); - GroupRosterItem* group = dynamic_cast(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(child); - if (contact) { - filterContact(contact, group); - } - } -} - -void Roster::filterAll() { - std::deque queue; - queue.push_back(root_); - while (!queue.empty()) { - RosterItem *item = *queue.begin(); - queue.pop_front(); - GroupRosterItem* group = dynamic_cast(item); - if (group) { - queue.insert(queue.begin(), group->getChildren().begin(), group->getChildren().end()); - filterGroup(group); - } - } -} - -} - diff --git a/Swiften/Roster/Roster.h b/Swiften/Roster/Roster.h deleted file mode 100644 index 2f12b57..0000000 --- a/Swiften/Roster/Roster.h +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. - */ - -#ifndef SWIFTEN_Roster_H -#define SWIFTEN_Roster_H - -#include "Swiften/Base/String.h" -#include "Swiften/JID/JID.h" -#include "Swiften/Roster/RosterItemOperation.h" -#include "Swiften/Roster/RosterFilter.h" - -#include -#include -#include "Swiften/Base/boost_bsignals.h" -#include - -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 getFilters() {return filters_;}; - boost::signal onChildrenChanged; - boost::signal onGroupAdded; - boost::signal 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 filters_; - std::map > itemMap_; - bool fullJIDMapping_; - bool sortByStatus_; -}; -} - -#endif diff --git a/Swiften/Roster/RosterFilter.h b/Swiften/Roster/RosterFilter.h deleted file mode 100644 index 02e788c..0000000 --- a/Swiften/Roster/RosterFilter.h +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. - */ - -#ifndef SWIFTEN_RosterFilter_H -#define SWIFTEN_RosterFilter_H - -#include "Swiften/Roster/RosterItem.h" - -namespace Swift { - -class RosterFilter { - public: - virtual ~RosterFilter() {} - virtual bool operator() (RosterItem* item) const = 0; -}; - -} -#endif - - diff --git a/Swiften/Roster/RosterItem.cpp b/Swiften/Roster/RosterItem.cpp deleted file mode 100644 index 3ce13b4..0000000 --- a/Swiften/Roster/RosterItem.cpp +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. - */ - -#include "Swiften/Roster/RosterItem.h" - -#include "Swiften/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/Swiften/Roster/RosterItem.h b/Swiften/Roster/RosterItem.h deleted file mode 100644 index 35dbe73..0000000 --- a/Swiften/Roster/RosterItem.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * 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 - -#include "Swiften/Base/String.h" - -namespace Swift { -class GroupRosterItem; -class RosterItem { - public: - RosterItem(const String& name, GroupRosterItem* parent); - virtual ~RosterItem(); - boost::signal 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/Swiften/Roster/RosterItemOperation.h b/Swiften/Roster/RosterItemOperation.h deleted file mode 100644 index e27e68b..0000000 --- a/Swiften/Roster/RosterItemOperation.h +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. - */ - -#ifndef SWIFTEN_RosterItemOperation_H -#define SWIFTEN_RosterItemOperation_H - -#include "Swiften/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_; -}; - -} -#endif - diff --git a/Swiften/Roster/SetAvatar.h b/Swiften/Roster/SetAvatar.h deleted file mode 100644 index 2b22188..0000000 --- a/Swiften/Roster/SetAvatar.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. - */ - -#ifndef SWIFTEN_SetAvatar_H -#define SWIFTEN_SetAvatar_H - -#include "Swiften/Elements/Presence.h" -#include "Swiften/JID/JID.h" -#include "Swiften/Roster/RosterItemOperation.h" -#include "Swiften/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(item); - if (contact && contact->getJID().equals(jid_, compareType_)) { - contact->setAvatarPath(path_); - } - } - - private: - JID jid_; - String path_; - JID::CompareType compareType_; -}; - -} -#endif - diff --git a/Swiften/Roster/SetName.h b/Swiften/Roster/SetName.h deleted file mode 100644 index d3f7749..0000000 --- a/Swiften/Roster/SetName.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * 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/Roster/RosterItemOperation.h" -#include "Swiften/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(item); - if (contact && contact->getJID().equals(jid_, compareType_)) { - contact->setDisplayName(name_); - } - } - - private: - String name_; - JID jid_; - JID::CompareType compareType_; -}; - -} - - diff --git a/Swiften/Roster/SetPresence.h b/Swiften/Roster/SetPresence.h deleted file mode 100644 index e710931..0000000 --- a/Swiften/Roster/SetPresence.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * 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 "Swiften/Roster/RosterItemOperation.h" -#include "Swiften/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(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/Swiften/Roster/UnitTest/RosterTest.cpp b/Swiften/Roster/UnitTest/RosterTest.cpp deleted file mode 100644 index 0300188..0000000 --- a/Swiften/Roster/UnitTest/RosterTest.cpp +++ /dev/null @@ -1,128 +0,0 @@ -/* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. - */ - -#include -#include -#include - -#include "Swiften/Roster/Roster.h" -#include "Swiften/Roster/GroupRosterItem.h" -#include "Swiften/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(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(roster_->getRoot()->getChildren()[0])->getChildren()[0]->getDisplayName()); - CPPUNIT_ASSERT_EQUAL(String("Cookie"), static_cast(roster_->getRoot()->getChildren()[0])->getChildren()[1]->getDisplayName()); - CPPUNIT_ASSERT_EQUAL(String("Ernie"), static_cast(roster_->getRoot()->getChildren()[1])->getChildren()[0]->getDisplayName()); - - } - - void testRemoveContact() { - roster_->addContact(jid1_, jid1_, "Bert", "group1", ""); - CPPUNIT_ASSERT_EQUAL(String("Bert"), static_cast(roster_->getRoot()->getChildren()[0])->getChildren()[0]->getDisplayName()); - - roster_->removeContact(jid1_); - CPPUNIT_ASSERT_EQUAL(0, static_cast(static_cast(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(roster_->getRoot()->getChildren()[0])->getChildren()[1]->getDisplayName()); - - roster_->removeContact(jid2_); - CPPUNIT_ASSERT_EQUAL(1, static_cast(static_cast(roster_->getRoot()->getChildren()[0])->getChildren().size())); - CPPUNIT_ASSERT_EQUAL(String("Bert"), static_cast(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(roster_->getRoot()->getChildren()[0])->getChildren()[1]->getDisplayName()); - - roster_->removeContact(jid4b); - CPPUNIT_ASSERT_EQUAL(1, static_cast(static_cast(roster_->getRoot()->getChildren()[0])->getChildren().size())); - CPPUNIT_ASSERT_EQUAL(String("Bert"), static_cast(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(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(new Presence()); - presence->setFrom(jid4b); - presence->setShow(StatusShow::Online); - roster_->applyOnItems(SetPresence(presence, JID::WithResource)); - std::vector children = static_cast(roster_->getRoot()->getDisplayedChildren()[0])->getDisplayedChildren(); - CPPUNIT_ASSERT_EQUAL(3, static_cast(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(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); - diff --git a/Swiften/Roster/XMPPRosterController.cpp b/Swiften/Roster/XMPPRosterController.cpp index 909ef8e..3a1d11f 100644 --- a/Swiften/Roster/XMPPRosterController.cpp +++ b/Swiften/Roster/XMPPRosterController.cpp @@ -12,9 +12,6 @@ #include "Swiften/Elements/RosterItemPayload.h" #include "Swiften/Queries/IQRouter.h" #include "Swiften/Roster/GetRosterRequest.h" -#include "Swiften/Roster/Roster.h" -#include "Swiften/Roster/SetPresence.h" -#include "Swiften/Roster/OfflineRosterFilter.h" #include "Swiften/Roster/XMPPRosterImpl.h" namespace Swift { diff --git a/Swiften/SConscript b/Swiften/SConscript index 36ebd0c..8eeab37 100644 --- a/Swiften/SConscript +++ b/Swiften/SConscript @@ -75,10 +75,6 @@ if env["SCONS_STAGE"] == "build" : "Queries/Requests/GetInBandRegistrationFormRequest.cpp", "Queries/Requests/SubmitInBandRegistrationFormRequest.cpp", "Queries/Responders/SoftwareVersionResponder.cpp", - "Roster/ContactRosterItem.cpp", - "Roster/GroupRosterItem.cpp", - "Roster/RosterItem.cpp", - "Roster/Roster.cpp", "Roster/XMPPRoster.cpp", "Roster/XMPPRosterImpl.cpp", "Roster/XMPPRosterController.cpp", @@ -249,7 +245,6 @@ if env["SCONS_STAGE"] == "build" : File("Queries/UnitTest/IQRouterTest.cpp"), File("Queries/UnitTest/RequestTest.cpp"), File("Queries/UnitTest/ResponderTest.cpp"), - File("Roster/UnitTest/RosterTest.cpp"), File("Roster/UnitTest/XMPPRosterImplTest.cpp"), File("Roster/UnitTest/XMPPRosterControllerTest.cpp"), File("Serializer/PayloadSerializers/UnitTest/PayloadsSerializer.cpp"), -- cgit v0.10.2-6-g49f6