diff options
| -rw-r--r-- | Swift/Controllers/Chat/ChatController.cpp | 22 | ||||
| -rw-r--r-- | Swift/Controllers/Chat/ChatsManager.cpp | 4 | ||||
| -rw-r--r-- | Swift/Controllers/Chat/UserSearchController.cpp | 2 | ||||
| -rw-r--r-- | Swift/Controllers/ContactsFromXMPPRoster.cpp | 7 | ||||
| -rw-r--r-- | Swift/Controllers/Roster/ContactRosterItem.cpp | 52 | ||||
| -rw-r--r-- | Swift/Controllers/Roster/ContactRosterItem.h | 13 | ||||
| -rw-r--r-- | Swift/Controllers/Roster/RosterController.cpp | 7 | ||||
| -rw-r--r-- | Swift/Controllers/Roster/UnitTest/RosterControllerTest.cpp | 37 | ||||
| -rw-r--r-- | Swiften/Parser/PayloadParsers/UnitTest/JingleParserTest.cpp | 12 | ||||
| -rw-r--r-- | Swiften/Presence/PresenceOracle.cpp | 89 | ||||
| -rw-r--r-- | Swiften/Presence/PresenceOracle.h | 33 | ||||
| -rw-r--r-- | Swiften/Presence/UnitTest/PresenceOracleTest.cpp | 63 |
12 files changed, 245 insertions, 96 deletions
diff --git a/Swift/Controllers/Chat/ChatController.cpp b/Swift/Controllers/Chat/ChatController.cpp index bedc427..51c7349 100644 --- a/Swift/Controllers/Chat/ChatController.cpp +++ b/Swift/Controllers/Chat/ChatController.cpp @@ -65,19 +65,19 @@ ChatController::ChatController(const JID& self, StanzaChannel* stanzaChannel, IQ std::string nick = nickResolver_->jidToNick(toJID_); chatWindow_->setName(nick); std::string startMessage; Presence::ref theirPresence; if (isInMUC) { startMessage = str(format(QT_TRANSLATE_NOOP("", "Starting chat with %1% in chatroom %2%")) % nick % contact.toBare().toString()); theirPresence = presenceOracle->getLastPresence(contact); } else { startMessage = str(format(QT_TRANSLATE_NOOP("", "Starting chat with %1% - %2%")) % nick % contact.toBare().toString()); - theirPresence = contact.isBare() ? presenceOracle->getHighestPriorityPresence(contact.toBare()) : presenceOracle->getLastPresence(contact); + theirPresence = contact.isBare() ? presenceOracle->getAccountPresence(contact) : presenceOracle->getLastPresence(contact); } Idle::ref idle; if (theirPresence && (idle = theirPresence->getPayload<Idle>())) { startMessage += str(format(QT_TRANSLATE_NOOP("", ", who has been idle since %1%")) % dateTimeToLocalString(idle->getSince())); } startMessage += ": " + statusShowTypeToFriendlyName(theirPresence ? theirPresence->getShow() : StatusShow::None); if (theirPresence && !theirPresence->getStatus().empty()) { startMessage += " (" + theirPresence->getStatus() + ")"; } @@ -137,19 +137,19 @@ void ChatController::handleBareJIDCapsChanged(const JID& /*jid*/) { } void ChatController::setToJID(const JID& jid) { chatStateNotifier_->setContact(jid); ChatControllerBase::setToJID(jid); Presence::ref presence; if (isInMUC_) { presence = presenceOracle_->getLastPresence(jid); } else { - presence = jid.isBare() ? presenceOracle_->getHighestPriorityPresence(jid.toBare()) : presenceOracle_->getLastPresence(jid); + presence = jid.isBare() ? presenceOracle_->getAccountPresence(jid.toBare()) : presenceOracle_->getLastPresence(jid); } chatStateNotifier_->setContactIsOnline(presence && presence->getType() == Presence::Available); handleBareJIDCapsChanged(toJID_); } void ChatController::setAvailableServerFeatures(boost::shared_ptr<DiscoInfo> info) { ChatControllerBase::setAvailableServerFeatures(info); if (iqRouter_->isAvailable() && info->hasFeature(DiscoInfo::BlockingCommandFeature)) { boost::shared_ptr<BlockList> blockList = clientBlockListManager_->getBlockList(); @@ -458,30 +458,30 @@ std::string ChatController::getStatusChangeString(boost::shared_ptr<Presence> pr } if (!presence->getStatus().empty()) { response += " (" + presence->getStatus() + ")"; } return response + "."; } void ChatController::handlePresenceChange(boost::shared_ptr<Presence> newPresence) { - bool me = false; - if (toJID_.isBare()) { - newPresence = presenceOracle_->getHighestPriorityPresence(toJID_); - if ((newPresence ? newPresence->getShow() : StatusShow::None) != lastShownStatus_) { - me = true; - } - } else if (toJID_.equals(newPresence->getFrom(), JID::WithResource)) { - me = true; + bool relevantPresence = false; + + if (toJID_.equals(newPresence->getFrom(), JID::WithoutResource)) { + // Presence matches ChatController JID. + newPresence = presenceOracle_->getAccountPresence(toJID_); + relevantPresence = true; } - if (!me) { + + if (!relevantPresence) { return; } + if (!newPresence) { newPresence = boost::make_shared<Presence>(); newPresence->setType(Presence::Unavailable); } lastShownStatus_ = newPresence->getShow(); chatStateTracker_->handlePresenceChange(newPresence); chatStateNotifier_->setContactIsOnline(newPresence->getType() == Presence::Available); std::string newStatusChangeString = getStatusChangeString(newPresence); diff --git a/Swift/Controllers/Chat/ChatsManager.cpp b/Swift/Controllers/Chat/ChatsManager.cpp index 343f490..8a06333 100644 --- a/Swift/Controllers/Chat/ChatsManager.cpp +++ b/Swift/Controllers/Chat/ChatsManager.cpp @@ -281,19 +281,19 @@ ChatListWindow::Chat ChatsManager::updateChatStatusAndAvatarHelper(const ChatLis ChatListWindow::Chat fixedChat = chat; if (fixedChat.isMUC) { if (mucControllers_.find(fixedChat.jid.toBare()) != mucControllers_.end()) { fixedChat.statusType = StatusShow::Online; } } else { if (avatarManager_) { fixedChat.avatarPath = avatarManager_->getAvatarPath(fixedChat.jid); } - Presence::ref presence = presenceOracle_->getHighestPriorityPresence(fixedChat.jid.toBare()); + Presence::ref presence = presenceOracle_->getAccountPresence(fixedChat.jid.toBare()); fixedChat.statusType = presence ? presence->getShow() : StatusShow::None; } return fixedChat; } void ChatsManager::loadRecents() { std::string recentsString(profileSettings_->getStringSetting(RECENT_CHATS)); if (recentsString.find("\t") != std::string::npos) { // old format @@ -403,19 +403,19 @@ ChatListWindow::Chat ChatsManager::createChatListChatItem(const JID& jid, const } } return ChatListWindow::Chat(jid, jid.toString(), activity, unreadCount, type, boost::filesystem::path(), true, privateMessage, nick, password); } else { ChatController* controller = getChatControllerIfExists(jid, false); if (controller) { unreadCount = controller->getUnreadCount(); } JID bareishJID = mucRegistry_->isMUC(jid.toBare()) ? jid : jid.toBare(); - Presence::ref presence = presenceOracle_->getHighestPriorityPresence(bareishJID); + Presence::ref presence = presenceOracle_->getAccountPresence(bareishJID); StatusShow::Type type = presence ? presence->getShow() : StatusShow::None; boost::filesystem::path avatarPath = avatarManager_ ? avatarManager_->getAvatarPath(bareishJID) : boost::filesystem::path(); return ChatListWindow::Chat(bareishJID, nickResolver_->jidToNick(bareishJID), activity, unreadCount, type, avatarPath, false, privateMessage); } } void ChatsManager::handleChatActivity(const JID& jid, const std::string& activity, bool isMUC) { const bool privateMessage = mucRegistry_->isMUC(jid.toBare()) && !isMUC; ChatListWindow::Chat chat = createChatListChatItem(jid, activity, privateMessage); diff --git a/Swift/Controllers/Chat/UserSearchController.cpp b/Swift/Controllers/Chat/UserSearchController.cpp index 454a110..291472f 100644 --- a/Swift/Controllers/Chat/UserSearchController.cpp +++ b/Swift/Controllers/Chat/UserSearchController.cpp @@ -299,19 +299,19 @@ Contact::ref UserSearchController::convertJIDtoContact(const JID& jid) { VCard::ref vcard = vcardManager_->getVCard(jid); if (vcard && !vcard->getFullName().empty()) { contact->name = vcard->getFullName(); } else { contact->name = jid.toString(); } } // presence lookup - Presence::ref presence = presenceOracle_->getHighestPriorityPresence(jid); + Presence::ref presence = presenceOracle_->getAccountPresence(jid); if (presence) { contact->statusType = presence->getShow(); } else { contact->statusType = StatusShow::None; } // avatar lookup contact->avatarPath = avatarManager_->getAvatarPath(jid); return contact; diff --git a/Swift/Controllers/ContactsFromXMPPRoster.cpp b/Swift/Controllers/ContactsFromXMPPRoster.cpp index d3d7364..1cab9b1 100644 --- a/Swift/Controllers/ContactsFromXMPPRoster.cpp +++ b/Swift/Controllers/ContactsFromXMPPRoster.cpp @@ -1,42 +1,41 @@ /* * Copyright (c) 2013 Tobias Markmann * Licensed under the simplified BSD license. * See Documentation/Licenses/BSD-simplified.txt for more information. */ /* - * Copyright (c) 2014 Isode Limited. + * Copyright (c) 2014-2015 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Swift/Controllers/ContactsFromXMPPRoster.h> -#include <Swiften/Base/foreach.h> - #include <Swiften/Avatars/AvatarManager.h> +#include <Swiften/Base/foreach.h> #include <Swiften/Presence/PresenceOracle.h> #include <Swiften/Roster/XMPPRoster.h> #include <Swiften/Roster/XMPPRosterItem.h> namespace Swift { ContactsFromXMPPRoster::ContactsFromXMPPRoster(XMPPRoster* roster, AvatarManager* avatarManager, PresenceOracle* presenceOracle) : roster_(roster), avatarManager_(avatarManager), presenceOracle_(presenceOracle) { } ContactsFromXMPPRoster::~ContactsFromXMPPRoster() { } std::vector<Contact::ref> ContactsFromXMPPRoster::getContacts(bool /*withMUCNicks*/) { std::vector<Contact::ref> results; std::vector<XMPPRosterItem> rosterItems = roster_->getItems(); foreach(const XMPPRosterItem& rosterItem, rosterItems) { Contact::ref contact = boost::make_shared<Contact>(rosterItem.getName().empty() ? rosterItem.getJID().toString() : rosterItem.getName(), rosterItem.getJID(), StatusShow::None,""); - contact->statusType = presenceOracle_->getHighestPriorityPresence(contact->jid) ? presenceOracle_->getHighestPriorityPresence(contact->jid)->getShow() : StatusShow::None; + contact->statusType = presenceOracle_->getAccountPresence(contact->jid) ? presenceOracle_->getAccountPresence(contact->jid)->getShow() : StatusShow::None; contact->avatarPath = avatarManager_->getAvatarPath(contact->jid); results.push_back(contact); } return results; } } diff --git a/Swift/Controllers/Roster/ContactRosterItem.cpp b/Swift/Controllers/Roster/ContactRosterItem.cpp index fd6d1cd..ae05aee 100644 --- a/Swift/Controllers/Roster/ContactRosterItem.cpp +++ b/Swift/Controllers/Roster/ContactRosterItem.cpp @@ -1,70 +1,71 @@ /* - * Copyright (c) 2010 Isode Limited. + * Copyright (c) 2010-2015 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Swift/Controllers/Roster/ContactRosterItem.h> #include <boost/date_time/posix_time/posix_time.hpp> -#include <Swiften/Base/foreach.h> #include <Swiften/Base/DateTime.h> +#include <Swiften/Base/foreach.h> #include <Swiften/Elements/Idle.h> + #include <Swift/Controllers/Intl.h> #include <Swift/Controllers/Roster/GroupRosterItem.h> namespace Swift { ContactRosterItem::ContactRosterItem(const JID& jid, const JID& displayJID, const std::string& name, GroupRosterItem* parent) : RosterItem(name, parent), jid_(jid), displayJID_(displayJID.toBare()), mucRole_(MUCOccupant::NoRole), mucAffiliation_(MUCOccupant::NoAffiliation), blockState_(BlockingNotSupported) { } ContactRosterItem::~ContactRosterItem() { } StatusShow::Type ContactRosterItem::getStatusShow() const { - return shownPresence_ ? shownPresence_->getShow() : StatusShow::None; + return presence_ ? presence_->getShow() : StatusShow::None; } StatusShow::Type ContactRosterItem::getSimplifiedStatusShow() const { - switch (shownPresence_ ? shownPresence_->getShow() : StatusShow::None) { + switch (presence_ ? presence_->getShow() : StatusShow::None) { case StatusShow::Online: return StatusShow::Online; case StatusShow::Away: return StatusShow::Away; case StatusShow::XA: return StatusShow::Away; case StatusShow::FFC: return StatusShow::Online; case StatusShow::DND: return StatusShow::DND; case StatusShow::None: return StatusShow::None; } assert(false); return StatusShow::None; } std::string ContactRosterItem::getStatusText() const { - return shownPresence_ ? shownPresence_->getStatus() : ""; + return presence_ ? presence_->getStatus() : ""; } std::string ContactRosterItem::getIdleText() const { - Idle::ref idle = shownPresence_ ? shownPresence_->getPayload<Idle>() : Idle::ref(); + Idle::ref idle = presence_ ? presence_->getPayload<Idle>() : Idle::ref(); if (!idle || idle->getSince().is_not_a_date_time()) { return ""; } else { return dateTimeToLocalString(idle->getSince()); } } std::string ContactRosterItem::getOfflineSinceText() const { - if (offlinePresence_) { - boost::optional<boost::posix_time::ptime> delay = offlinePresence_->getTimestamp(); - if (offlinePresence_->getType() == Presence::Unavailable && delay) { + if (presence_ && presence_->getType() == Presence::Unavailable) { + boost::optional<boost::posix_time::ptime> delay = presence_->getTimestamp(); + if (delay) { return dateTimeToLocalString(*delay); } } return ""; } void ContactRosterItem::setAvatarPath(const boost::filesystem::path& path) { avatarPath_ = path; onDataChanged(); @@ -82,54 +83,25 @@ void ContactRosterItem::setDisplayJID(const JID& jid) { } const JID& ContactRosterItem::getDisplayJID() const { return displayJID_; } typedef std::pair<std::string, boost::shared_ptr<Presence> > StringPresencePair; -void ContactRosterItem::calculateShownPresence() { - shownPresence_ = offlinePresence_; - foreach (StringPresencePair presencePair, presences_) { - boost::shared_ptr<Presence> presence = presencePair.second; - if (!shownPresence_ || presence->getPriority() > shownPresence_->getPriority() || presence->getShow() < shownPresence_->getShow()) { - shownPresence_ = presence; - } - } -} - void ContactRosterItem::clearPresence() { - presences_.clear(); - calculateShownPresence(); + presence_.reset(); onDataChanged(); } void ContactRosterItem::applyPresence(const std::string& resource, boost::shared_ptr<Presence> presence) { - if (offlinePresence_) { - offlinePresence_ = boost::shared_ptr<Presence>(); - } - if (presence->getType() == Presence::Unavailable) { - if (resource.empty()) { - /* Unavailable from the bare JID means all resources are offline.*/ - presences_.clear(); - } else { - if (presences_.find(resource) != presences_.end()) { - presences_.erase(resource); - } - } - if (presences_.empty()) { - offlinePresence_ = presence; - } - } else { - presences_[resource] = presence; - } - calculateShownPresence(); + presence_ = presence; onDataChanged(); } const std::vector<std::string>& ContactRosterItem::getGroups() const { return groups_; } /** Only used so a contact can know about the groups it's in*/ void ContactRosterItem::addGroup(const std::string& group) { diff --git a/Swift/Controllers/Roster/ContactRosterItem.h b/Swift/Controllers/Roster/ContactRosterItem.h index 54a6b60..ec04a8c 100644 --- a/Swift/Controllers/Roster/ContactRosterItem.h +++ b/Swift/Controllers/Roster/ContactRosterItem.h @@ -1,30 +1,30 @@ /* - * Copyright (c) 2010-2013 Isode Limited. + * Copyright (c) 2010-2015 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #pragma once -#include <map> #include <set> #include <string> +#include <vector> #include <boost/bind.hpp> #include <boost/date_time/posix_time/ptime.hpp> #include <boost/filesystem/path.hpp> #include <boost/shared_ptr.hpp> #include <Swiften/Base/boost_bsignals.h> +#include <Swiften/Elements/MUCOccupant.h> #include <Swiften/Elements/Presence.h> #include <Swiften/Elements/StatusShow.h> -#include <Swiften/Elements/MUCOccupant.h> #include <Swiften/Elements/VCard.h> #include <Swiften/JID/JID.h> #include <Swift/Controllers/Roster/RosterItem.h> namespace Swift { class GroupRosterItem; class ContactRosterItem : public RosterItem { @@ -50,24 +50,23 @@ class ContactRosterItem : public RosterItem { std::string getStatusText() const; std::string getIdleText() const; std::string getOfflineSinceText() const; void setAvatarPath(const boost::filesystem::path& path); const boost::filesystem::path& getAvatarPath() const; const JID& getJID() const; void setDisplayJID(const JID& jid); const JID& getDisplayJID() const; void applyPresence(const std::string& resource, boost::shared_ptr<Presence> presence); - void clearPresence(); - void calculateShownPresence(); const std::vector<std::string>& getGroups() const; /** Only used so a contact can know about the groups it's in*/ void addGroup(const std::string& group); void removeGroup(const std::string& group); + void clearPresence(); MUCOccupant::Role getMUCRole() const; void setMUCRole(const MUCOccupant::Role& role); MUCOccupant::Affiliation getMUCAffiliation() const; void setMUCAffiliation(const MUCOccupant::Affiliation& affiliation); std::string getMUCAffiliationText() const; void setSupportedFeatures(const std::set<Feature>& features); bool supportsFeature(Feature feature) const; @@ -79,21 +78,19 @@ class ContactRosterItem : public RosterItem { void setVCard(VCard::ref vcard); boost::signal<void ()> onVCardRequested; private: JID jid_; JID displayJID_; boost::posix_time::ptime lastAvailableTime_; boost::filesystem::path avatarPath_; - std::map<std::string, boost::shared_ptr<Presence> > presences_; - boost::shared_ptr<Presence> offlinePresence_; - boost::shared_ptr<Presence> shownPresence_; + boost::shared_ptr<Presence> presence_; std::vector<std::string> groups_; MUCOccupant::Role mucRole_; MUCOccupant::Affiliation mucAffiliation_; std::set<Feature> features_; BlockState blockState_; VCard::ref vcard_; }; } diff --git a/Swift/Controllers/Roster/RosterController.cpp b/Swift/Controllers/Roster/RosterController.cpp index 73efa43..751ca32 100644 --- a/Swift/Controllers/Roster/RosterController.cpp +++ b/Swift/Controllers/Roster/RosterController.cpp @@ -73,19 +73,18 @@ RosterController::RosterController(const JID& jid, XMPPRoster* xmppRoster, Avata rosterVCardProvider_ = new RosterVCardProvider(roster_, vcardManager, JID::WithoutResource); 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)); vcardManager_->onOwnVCardChanged.connect(boost::bind(&RosterController::handleOwnVCardChanged, this, _1)); avatarManager_->onAvatarChanged.connect(boost::bind(&RosterController::handleAvatarChanged, this, _1)); presenceOracle_->onPresenceChange.connect(boost::bind(&RosterController::handlePresenceChanged, this, _1)); mainWindow_->setMyAvatarPath(pathToString(avatarManager_->getAvatarPath(myJID_.toBare()))); nickManager_->onOwnNickChanged.connect(boost::bind(&MainWindow::setMyNick, mainWindow_, _1)); mainWindow_->setMyJID(jid); @@ -328,19 +327,20 @@ void RosterController::handleRosterSetError(ErrorPayload::ref error, boost::shar } boost::shared_ptr<ErrorEvent> errorEvent(new ErrorEvent(JID(myJID_.getDomain()), text)); eventController_->handleIncomingEvent(errorEvent); } void RosterController::handleIncomingPresence(Presence::ref newPresence) { if (newPresence->getType() == Presence::Error) { return; } - roster_->applyOnItems(SetPresence(newPresence)); + Presence::ref accountPresence = presenceOracle_->getAccountPresence(newPresence->getFrom().toBare()); + roster_->applyOnItems(SetPresence(accountPresence)); } void RosterController::handleSubscriptionRequest(const JID& jid, const std::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)); @@ -374,18 +374,21 @@ void RosterController::handleAvatarChanged(const JID& jid) { mainWindow_->setMyContactRosterItem(ownContact_); } } void RosterController::handlePresenceChanged(Presence::ref presence) { if (presence->getFrom().equals(myJID_, JID::WithResource)) { ownContact_->applyPresence(std::string(), presence); mainWindow_->setMyContactRosterItem(ownContact_); } + else { + handleIncomingPresence(presence); + } } boost::optional<XMPPRosterItem> RosterController::getItem(const JID& jid) const { return xmppRoster_->getItem(jid); } std::set<std::string> RosterController::getGroups() const { return xmppRoster_->getGroups(); } diff --git a/Swift/Controllers/Roster/UnitTest/RosterControllerTest.cpp b/Swift/Controllers/Roster/UnitTest/RosterControllerTest.cpp index 9320af1..d774e6d 100644 --- a/Swift/Controllers/Roster/UnitTest/RosterControllerTest.cpp +++ b/Swift/Controllers/Roster/UnitTest/RosterControllerTest.cpp @@ -188,51 +188,64 @@ class RosterControllerTest : public CppUnit::TestFixture { CPPUNIT_ASSERT(item); CPPUNIT_ASSERT_EQUAL(highPresence->getStatus(), item->getStatusText()); } void testUnavailablePresence() { std::vector<std::string> groups; groups.push_back("testGroup1"); JID from("test@testdomain.com"); xmppRoster_->addContact(from, "name", groups, RosterItemPayload::Both); + Presence::ref lowPresence(new Presence()); lowPresence->setFrom(withResource(from, "bob")); lowPresence->setPriority(2); + lowPresence->setShow(StatusShow::Away); lowPresence->setStatus("Not here"); + Presence::ref lowPresenceOffline(new Presence()); + lowPresenceOffline->setFrom(withResource(from, "bob")); + lowPresenceOffline->setStatus("Signing out"); + lowPresenceOffline->setType(Presence::Unavailable); + 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); + Presence::ref accountPresence = presenceOracle_->getAccountPresence(from); + CPPUNIT_ASSERT_EQUAL(StatusShow::Away, accountPresence->getShow()); + stanzaChannel_->onPresenceReceived(highPresence); + accountPresence = presenceOracle_->getAccountPresence(from); + CPPUNIT_ASSERT_EQUAL(StatusShow::Online, accountPresence->getShow()); + stanzaChannel_->onPresenceReceived(highPresenceOffline); + + // After this, the roster should show the low presence. ContactRosterItem* item = dynamic_cast<ContactRosterItem*>(dynamic_cast<GroupRosterItem*>(CHILDREN[0])->getChildren()[0]); CPPUNIT_ASSERT(item); - /* A verification that if the test fails, it's the RosterController, not the PresenceOracle. */ - Presence::ref high = presenceOracle_->getHighestPriorityPresence(from); - CPPUNIT_ASSERT_EQUAL(Presence::Available, high->getType()); - CPPUNIT_ASSERT_EQUAL(lowPresence->getStatus(), high->getStatus()); - CPPUNIT_ASSERT_EQUAL(StatusShow::Online, item->getStatusShow()); + + Presence::ref low = presenceOracle_->getAccountPresence(from); + + CPPUNIT_ASSERT_EQUAL(Presence::Available, low->getType()); + CPPUNIT_ASSERT_EQUAL(lowPresence->getStatus(), low->getStatus()); + CPPUNIT_ASSERT_EQUAL(lowPresence->getShow(), item->getStatusShow()); CPPUNIT_ASSERT_EQUAL(lowPresence->getStatus(), item->getStatusText()); stanzaChannel_->onPresenceReceived(lowPresenceOffline); item = dynamic_cast<ContactRosterItem*>(dynamic_cast<GroupRosterItem*>(CHILDREN[0])->getChildren()[0]); CPPUNIT_ASSERT(item); /* A verification that if the test fails, it's the RosterController, not the PresenceOracle. */ - high = presenceOracle_->getHighestPriorityPresence(from); - CPPUNIT_ASSERT_EQUAL(Presence::Unavailable, high->getType()); - CPPUNIT_ASSERT_EQUAL(lowPresenceOffline->getStatus(), high->getStatus()); + low = presenceOracle_->getHighestPriorityPresence(from); + CPPUNIT_ASSERT_EQUAL(Presence::Unavailable, low->getType()); + CPPUNIT_ASSERT_EQUAL(lowPresenceOffline->getStatus(), low->getStatus()); CPPUNIT_ASSERT_EQUAL(StatusShow::None, item->getStatusShow()); CPPUNIT_ASSERT_EQUAL(lowPresenceOffline->getStatus(), item->getStatusText()); } void testAdd() { std::vector<std::string> groups; groups.push_back("testGroup1"); groups.push_back("testGroup2"); xmppRoster_->addContact(JID("test@testdomain.com/bob"), "name", groups, RosterItemPayload::Both); diff --git a/Swiften/Parser/PayloadParsers/UnitTest/JingleParserTest.cpp b/Swiften/Parser/PayloadParsers/UnitTest/JingleParserTest.cpp index 05aaedf..56143ef 100644 --- a/Swiften/Parser/PayloadParsers/UnitTest/JingleParserTest.cpp +++ b/Swiften/Parser/PayloadParsers/UnitTest/JingleParserTest.cpp @@ -7,28 +7,27 @@ /* * Copyright (c) 2015 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <cppunit/extensions/HelperMacros.h> #include <cppunit/extensions/TestFactoryRegistry.h> -#include <Swiften/Parser/PayloadParsers/UnitTest/PayloadsParserTester.h> -#include <Swiften/Elements/JinglePayload.h> +#include <Swiften/Base/DateTime.h> +#include <Swiften/Elements/JingleFileTransferDescription.h> +#include <Swiften/Elements/JingleFileTransferHash.h> #include <Swiften/Elements/JingleIBBTransportPayload.h> +#include <Swiften/Elements/JinglePayload.h> #include <Swiften/Elements/JingleS5BTransportPayload.h> -#include <Swiften/Elements/JingleFileTransferDescription.h> #include <Swiften/Elements/StreamInitiationFileInfo.h> -#include <Swiften/Elements/JingleFileTransferHash.h> -#include <Swiften/Base/DateTime.h> +#include <Swiften/Parser/PayloadParsers/UnitTest/PayloadsParserTester.h> #include <Swiften/StringCodecs/Base64.h> -#include <Swiften/Base/Log.h> using namespace Swift; class JingleParserTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(JingleParserTest); CPPUNIT_TEST(testParse_Xep0166_Example3); CPPUNIT_TEST(testParse_Xep0166_Example8); CPPUNIT_TEST(testParse_Xep0261_Example1); @@ -407,19 +406,18 @@ class JingleParserTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(std::string("a73sjjvkla37jfea"), jingle->getSessionID()); JingleFileTransferHash::ref hash = jingle->getPayload<JingleFileTransferHash>(); CPPUNIT_ASSERT(hash); CPPUNIT_ASSERT_EQUAL(std::string("VS2nSZMIUsaa5dIUHTdmsQ=="), Base64::encode(hash->getFileInfo().getHash("sha-1").get())); } // http://xmpp.org/extensions/xep-0234.html#example-10 void testParse_Xep0234_Example10() { - Log::setLogLevel(Log::debug); PayloadsParserTester parser; CPPUNIT_ASSERT(parser.parse( "<jingle xmlns='urn:xmpp:jingle:1'\n" " action='session-initiate'\n" " initiator='romeo@montague.lit/orchard'\n" " sid='uj3b2'>\n" " <content creator='initiator' name='a-file-request'>\n" " <description xmlns='urn:xmpp:jingle:apps:file-transfer:4'>\n" " <file>\n" diff --git a/Swiften/Presence/PresenceOracle.cpp b/Swiften/Presence/PresenceOracle.cpp index e4129fb..8623529 100644 --- a/Swiften/Presence/PresenceOracle.cpp +++ b/Swiften/Presence/PresenceOracle.cpp @@ -1,20 +1,24 @@ /* * Copyright (c) 2010-2015 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Swiften/Presence/PresenceOracle.h> +#include <queue> + #include <boost/bind.hpp> +#include <Swiften/Base/foreach.h> #include <Swiften/Client/StanzaChannel.h> +#include <Swiften/Elements/StatusShow.h> #include <Swiften/Roster/XMPPRoster.h> namespace Swift { PresenceOracle::PresenceOracle(StanzaChannel* stanzaChannel, XMPPRoster* roster) : stanzaChannel_(stanzaChannel), xmppRoster_(roster) { stanzaChannel_->onPresenceReceived.connect(boost::bind(&PresenceOracle::handleIncomingPresence, this, _1)); stanzaChannel_->onAvailableChanged.connect(boost::bind(&PresenceOracle::handleStanzaChannelAvailableChanged, this, _1)); xmppRoster_->onJIDRemoved.connect(boost::bind(&PresenceOracle::handleJIDRemoved, this, _1)); } @@ -100,18 +104,103 @@ std::vector<Presence::ref> PresenceOracle::getAllPresence(const JID& bareJID) co PresenceMap presenceMap = i->second; PresenceMap::const_iterator j = presenceMap.begin(); for (; j != presenceMap.end(); ++j) { Presence::ref current = j->second; results.push_back(current); } return results; } +struct PresenceAccountCmp { + static int preferenceFromStatusShow(StatusShow::Type showType) { + switch (showType) { + case StatusShow::FFC: + return 5; + case StatusShow::Online: + return 4; + case StatusShow::DND: + return 3; + case StatusShow::Away: + return 2; + case StatusShow::XA: + return 1; + case StatusShow::None: + return 0; + } + assert(false); + return -1; + } + + bool operator()(const Presence::ref& a, const Presence::ref& b) { + int aPreference = preferenceFromStatusShow(a->getShow()); + int bPreference = preferenceFromStatusShow(b->getShow()); + + if (aPreference != bPreference) { + return aPreference < bPreference; + } + if (a->getPriority() != b->getPriority()) { + return a->getPriority() < b->getPriority(); + } + return a->getFrom().getResource() < b->getFrom().getResource(); + } +}; + +typedef std::priority_queue<Presence::ref, std::vector<Presence::ref>, PresenceAccountCmp> PresenceAccountPriorityQueue; + +Presence::ref PresenceOracle::getActivePresence(const std::vector<Presence::ref> presences) { + Presence::ref accountPresence; + + PresenceAccountPriorityQueue online; + PresenceAccountPriorityQueue away; + PresenceAccountPriorityQueue offline; + + foreach(Presence::ref presence, presences) { + switch (presence->getShow()) { + case StatusShow::Online: + online.push(presence); + break; + case StatusShow::Away: + away.push(presence); + break; + case StatusShow::FFC: + online.push(presence); + break; + case StatusShow::XA: + away.push(presence); + break; + case StatusShow::DND: + away.push(presence); + break; + case StatusShow::None: + offline.push(presence); + break; + } + } + + if (!online.empty()) { + accountPresence = online.top(); + } + else if (!away.empty()) { + accountPresence = away.top(); + } + else if (!offline.empty()) { + accountPresence = offline.top(); + } + return accountPresence; +} + +Presence::ref PresenceOracle::getAccountPresence(const JID& jid) const { + Presence::ref accountPresence; + std::vector<Presence::ref> allPresences = getAllPresence(jid.toBare()); + accountPresence = getActivePresence(allPresences); + return accountPresence; +} + Presence::ref PresenceOracle::getHighestPriorityPresence(const JID& bareJID) const { PresencesMap::const_iterator i = entries_.find(bareJID); if (i == entries_.end()) { return Presence::ref(); } PresenceMap presenceMap = i->second; PresenceMap::const_iterator j = presenceMap.begin(); Presence::ref highest; for (; j != presenceMap.end(); ++j) { diff --git a/Swiften/Presence/PresenceOracle.h b/Swiften/Presence/PresenceOracle.h index f312506..a010e8e 100644 --- a/Swiften/Presence/PresenceOracle.h +++ b/Swiften/Presence/PresenceOracle.h @@ -11,27 +11,60 @@ #include <Swiften/Base/API.h> #include <Swiften/Base/boost_bsignals.h> #include <Swiften/Elements/Presence.h> namespace Swift { class StanzaChannel; class XMPPRoster; + /** + * The PresenceOracle class observes all received presence stanzas for + * the \ref StanzaChannel class passed in the constructor and maintains a + * cache. + */ class SWIFTEN_API PresenceOracle { public: PresenceOracle(StanzaChannel* stanzaChannel, XMPPRoster* roster); ~PresenceOracle(); Presence::ref getLastPresence(const JID&) const; Presence::ref getHighestPriorityPresence(const JID& bareJID) const; std::vector<Presence::ref> getAllPresence(const JID& bareJID) const; + /** + * \brief Returns the relevant presence for a list of resource presences. + * + * It only takes the presence show type into account. Priorities are + * ignored as various clients set them to arbitrary values unrelated + * to actual end point availability. + * + * The presences of the resources are group by availablilty and sorted + * by show type in the following order: + * + * -# Online + * -# Free for Chat + * -# Available + * -# Away + * -# DND + * -# Extended Away + * -# Away + * -# Offline + * -# Unavailable + */ + static Presence::ref getActivePresence(const std::vector<Presence::ref> presences); + + /** + * \brief This considers all online resources of a bare JID and returns + * the value returned by \ref getActivePresence when passing this list. + */ + Presence::ref getAccountPresence(const JID& jid) const; + public: boost::signal<void (Presence::ref)> onPresenceChange; private: void handleIncomingPresence(Presence::ref presence); void handleStanzaChannelAvailableChanged(bool); void handleJIDRemoved(const JID& removedJID); private: diff --git a/Swiften/Presence/UnitTest/PresenceOracleTest.cpp b/Swiften/Presence/UnitTest/PresenceOracleTest.cpp index 85dcca9..6d16d80 100644 --- a/Swiften/Presence/UnitTest/PresenceOracleTest.cpp +++ b/Swiften/Presence/UnitTest/PresenceOracleTest.cpp @@ -22,18 +22,19 @@ class PresenceOracleTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(PresenceOracleTest); CPPUNIT_TEST(testReceivePresence); CPPUNIT_TEST(testReceivePresenceFromDifferentResources); CPPUNIT_TEST(testSubscriptionRequest); CPPUNIT_TEST(testReconnectResetsPresences); CPPUNIT_TEST(testHighestPresenceSingle); CPPUNIT_TEST(testHighestPresenceMultiple); CPPUNIT_TEST(testHighestPresenceGlobal); CPPUNIT_TEST(testHighestPresenceChangePriority); + CPPUNIT_TEST(testGetActivePresence); CPPUNIT_TEST_SUITE_END(); public: void setUp() { stanzaChannel_ = new DummyStanzaChannel(); xmppRoster_ = new XMPPRosterImpl(); oracle_ = new PresenceOracle(stanzaChannel_, xmppRoster_); oracle_->onPresenceChange.connect(boost::bind(&PresenceOracleTest::handlePresenceChange, this, _1)); @@ -46,27 +47,27 @@ class PresenceOracleTest : public CppUnit::TestFixture { void tearDown() { delete subscriptionManager_; delete oracle_; delete xmppRoster_; delete stanzaChannel_; } void testHighestPresenceSingle() { - JID bareJID("alice@wonderland.lit"); - Presence::ref fiveOn = makeOnline("blah", 5); - Presence::ref fiveOff = makeOffline("/blah"); - CPPUNIT_ASSERT_EQUAL(Presence::ref(), oracle_->getHighestPriorityPresence(bareJID)); - stanzaChannel_->onPresenceReceived(fiveOn); - CPPUNIT_ASSERT_EQUAL(fiveOn, oracle_->getHighestPriorityPresence(bareJID)); - stanzaChannel_->onPresenceReceived(fiveOff); - CPPUNIT_ASSERT_EQUAL(fiveOff, oracle_->getHighestPriorityPresence(bareJID)); - } + JID bareJID("alice@wonderland.lit"); + Presence::ref fiveOn = makeOnline("blah", 5); + Presence::ref fiveOff = makeOffline("/blah"); + CPPUNIT_ASSERT_EQUAL(Presence::ref(), oracle_->getHighestPriorityPresence(bareJID)); + stanzaChannel_->onPresenceReceived(fiveOn); + CPPUNIT_ASSERT_EQUAL(fiveOn, oracle_->getHighestPriorityPresence(bareJID)); + stanzaChannel_->onPresenceReceived(fiveOff); + CPPUNIT_ASSERT_EQUAL(fiveOff, oracle_->getHighestPriorityPresence(bareJID)); + } void testHighestPresenceMultiple() { JID bareJID("alice@wonderland.lit"); Presence::ref fiveOn = makeOnline("blah", 5); Presence::ref fiveOff = makeOffline("/blah"); Presence::ref tenOn = makeOnline("bert", 10); Presence::ref tenOff = makeOffline("/bert"); stanzaChannel_->onPresenceReceived(fiveOn); stanzaChannel_->onPresenceReceived(tenOn); @@ -145,20 +146,64 @@ class PresenceOracleTest : public CppUnit::TestFixture { void testReconnectResetsPresences() { boost::shared_ptr<Presence> sentPresence(createPresence(user1)); stanzaChannel_->onPresenceReceived(sentPresence); stanzaChannel_->setAvailable(false); stanzaChannel_->setAvailable(true); CPPUNIT_ASSERT(!oracle_->getLastPresence(user1)); } + + void testGetActivePresence() { + { + std::vector<Presence::ref> presenceList; + presenceList.push_back(createPresence("alice@wonderland.lit/resourceA", 10, Presence::Available, StatusShow::Away)); + presenceList.push_back(createPresence("alice@wonderland.lit/resourceB", 5, Presence::Available, StatusShow::Online)); + + CPPUNIT_ASSERT_EQUAL(StatusShow::Online, PresenceOracle::getActivePresence(presenceList)->getShow()); + } + + { + std::vector<Presence::ref> presenceList; + presenceList.push_back(createPresence("alice@wonderland.lit/resourceA", 10, Presence::Available, StatusShow::Away)); + presenceList.push_back(createPresence("alice@wonderland.lit/resourceB", 5, Presence::Available, StatusShow::DND)); + + CPPUNIT_ASSERT_EQUAL(StatusShow::DND, PresenceOracle::getActivePresence(presenceList)->getShow()); + } + + { + std::vector<Presence::ref> presenceList; + presenceList.push_back(createPresence("alice@wonderland.lit/resourceA", 0, Presence::Available, StatusShow::Online)); + presenceList.push_back(createPresence("alice@wonderland.lit/resourceB", 0, Presence::Available, StatusShow::DND)); + + CPPUNIT_ASSERT_EQUAL(StatusShow::Online, PresenceOracle::getActivePresence(presenceList)->getShow()); + } + + { + std::vector<Presence::ref> presenceList; + presenceList.push_back(createPresence("alice@wonderland.lit/resourceA", 1, Presence::Available, StatusShow::Online)); + presenceList.push_back(createPresence("alice@wonderland.lit/resourceB", 0, Presence::Available, StatusShow::Online)); + + CPPUNIT_ASSERT_EQUAL(JID("alice@wonderland.lit/resourceA"), PresenceOracle::getActivePresence(presenceList)->getFrom()); + } + } private: + Presence::ref createPresence(const JID &jid, int priority, Presence::Type type, const StatusShow::Type& statusShow) { + Presence::ref presence = boost::make_shared<Presence>(); + presence->setFrom(jid); + presence->setPriority(priority); + presence->setType(type); + presence->setShow(statusShow); + return presence; + } + + Presence::ref makeOnline(const std::string& resource, int priority) { Presence::ref presence(new Presence()); presence->setPriority(priority); presence->setFrom(JID("alice@wonderland.lit/" + resource)); return presence; } Presence::ref makeOffline(const std::string& resource) { Presence::ref presence(new Presence()); |
Swift