diff options
Diffstat (limited to 'Swiften/Presence')
-rw-r--r-- | Swiften/Presence/DirectedPresenceSender.cpp | 66 | ||||
-rw-r--r-- | Swiften/Presence/DirectedPresenceSender.h | 33 | ||||
-rw-r--r-- | Swiften/Presence/PresenceOracle.cpp | 5 | ||||
-rw-r--r-- | Swiften/Presence/PresenceOracle.h | 2 | ||||
-rw-r--r-- | Swiften/Presence/PresenceSender.cpp | 73 | ||||
-rw-r--r-- | Swiften/Presence/PresenceSender.h | 22 | ||||
-rw-r--r-- | Swiften/Presence/StanzaChannelPresenceSender.cpp | 23 | ||||
-rw-r--r-- | Swiften/Presence/StanzaChannelPresenceSender.h | 25 | ||||
-rw-r--r-- | Swiften/Presence/SubscriptionManager.cpp | 57 | ||||
-rw-r--r-- | Swiften/Presence/SubscriptionManager.h | 41 | ||||
-rw-r--r-- | Swiften/Presence/UnitTest/DirectedPresenceSenderTest.cpp (renamed from Swiften/Presence/UnitTest/PresenceSenderTest.cpp) | 28 | ||||
-rw-r--r-- | Swiften/Presence/UnitTest/PresenceOracleTest.cpp | 6 |
12 files changed, 272 insertions, 109 deletions
diff --git a/Swiften/Presence/DirectedPresenceSender.cpp b/Swiften/Presence/DirectedPresenceSender.cpp new file mode 100644 index 0000000..aade6cf --- /dev/null +++ b/Swiften/Presence/DirectedPresenceSender.cpp @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2010 Remko Tronçon + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#include "Swiften/Presence/DirectedPresenceSender.h" +#include "Swiften/Base/foreach.h" + +namespace Swift { + +DirectedPresenceSender::DirectedPresenceSender(PresenceSender* sender) : sender(sender) { +} + +void DirectedPresenceSender::sendPresence(boost::shared_ptr<Presence> presence) { + if (!sender->isAvailable()) { + return; + } + + sender->sendPresence(presence); + + if (!presence->getTo().isValid()) { + boost::shared_ptr<Presence> presenceCopy(new Presence(*presence)); + foreach(const JID& jid, directedPresenceReceivers) { + presenceCopy->setTo(jid); + sender->sendPresence(presenceCopy); + } + + lastSentUndirectedPresence = presence; + } +} + +/** + * Gets either the last broadcast presence, or an empty stanza if none has been sent. + */ +boost::shared_ptr<Presence> DirectedPresenceSender::getLastSentUndirectedPresence() { + boost::shared_ptr<Presence> presenceCopy(lastSentUndirectedPresence ? new Presence(*lastSentUndirectedPresence) : new Presence()); + return presenceCopy; +} + +void DirectedPresenceSender::addDirectedPresenceReceiver(const JID& jid) { + directedPresenceReceivers.insert(jid); + if (sender->isAvailable()) { + if (lastSentUndirectedPresence && lastSentUndirectedPresence->getType() == Presence::Available) { + boost::shared_ptr<Presence> presenceCopy(new Presence(*lastSentUndirectedPresence)); + presenceCopy->setTo(jid); + sender->sendPresence(presenceCopy); + } + } +} + +void DirectedPresenceSender::removeDirectedPresenceReceiver(const JID& jid) { + directedPresenceReceivers.erase(jid); + if (sender->isAvailable()) { + boost::shared_ptr<Presence> presence(new Presence()); + presence->setType(Presence::Unavailable); + presence->setTo(jid); + sender->sendPresence(presence); + } +} + +bool DirectedPresenceSender::isAvailable() const { + return sender->isAvailable(); +} + +} diff --git a/Swiften/Presence/DirectedPresenceSender.h b/Swiften/Presence/DirectedPresenceSender.h new file mode 100644 index 0000000..b63a50e --- /dev/null +++ b/Swiften/Presence/DirectedPresenceSender.h @@ -0,0 +1,33 @@ +/* + * 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 <set> + +#include "Swiften/Elements/Presence.h" +#include "Swiften/Presence/PresenceSender.h" + +namespace Swift { + class DirectedPresenceSender : public PresenceSender { + public: + DirectedPresenceSender(PresenceSender*); + + void addDirectedPresenceReceiver(const JID&); + void removeDirectedPresenceReceiver(const JID&); + + void sendPresence(Presence::ref); + + Presence::ref getLastSentUndirectedPresence(); + + bool isAvailable() const; + + private: + Presence::ref lastSentUndirectedPresence; + PresenceSender* sender; + std::set<JID> directedPresenceReceivers; + }; +} diff --git a/Swiften/Presence/PresenceOracle.cpp b/Swiften/Presence/PresenceOracle.cpp index fb4be3f..387ad42 100644 --- a/Swiften/Presence/PresenceOracle.cpp +++ b/Swiften/Presence/PresenceOracle.cpp @@ -33,12 +33,11 @@ void PresenceOracle::handleStanzaChannelAvailableChanged(bool available) { void PresenceOracle::handleIncomingPresence(Presence::ref presence) { JID bareJID(presence->getFrom().toBare()); if (presence->getType() == Presence::Subscribe) { - onPresenceSubscriptionRequest(bareJID, presence->getStatus()); - } else { + } + else { Presence::ref passedPresence = presence; if (presence->getType() == Presence::Unsubscribe) { /* 3921bis says that we don't follow up with an unavailable, so simulate this ourselves */ - onPresenceSubscriptionRevoked(bareJID, presence->getStatus()); passedPresence = Presence::ref(new Presence()); passedPresence->setType(Presence::Unavailable); passedPresence->setFrom(bareJID); diff --git a/Swiften/Presence/PresenceOracle.h b/Swiften/Presence/PresenceOracle.h index f30d05d..e846984 100644 --- a/Swiften/Presence/PresenceOracle.h +++ b/Swiften/Presence/PresenceOracle.h @@ -25,8 +25,6 @@ class StanzaChannel; public: boost::signal<void (Presence::ref)> onPresenceChange; - boost::signal<void (const JID&, const String&)> onPresenceSubscriptionRequest; - boost::signal<void (const JID&, const String&)> onPresenceSubscriptionRevoked; private: void handleIncomingPresence(Presence::ref presence); diff --git a/Swiften/Presence/PresenceSender.cpp b/Swiften/Presence/PresenceSender.cpp index 6df02b8..50d75eb 100644 --- a/Swiften/Presence/PresenceSender.cpp +++ b/Swiften/Presence/PresenceSender.cpp @@ -5,81 +5,10 @@ */ #include "Swiften/Presence/PresenceSender.h" -#include "Swiften/Base/foreach.h" -#include "Swiften/Client/StanzaChannel.h" namespace Swift { -PresenceSender::PresenceSender(StanzaChannel* channel) : channel(channel) { -} - -void PresenceSender::sendPresence(boost::shared_ptr<Presence> presence) { - if (!channel->isAvailable()) { - return; - } - - channel->sendPresence(presence); - - if (!presence->getTo().isValid()) { - boost::shared_ptr<Presence> presenceCopy(new Presence(*presence)); - foreach(const JID& jid, directedPresenceReceivers) { - presenceCopy->setTo(jid); - channel->sendPresence(presenceCopy); - } - - lastSentUndirectedPresence = presence; - } -} - -/** - * Gets either the last broadcast presence, or an empty stanza if none has been sent. - */ -boost::shared_ptr<Presence> PresenceSender::getLastSentUndirectedPresence() { - boost::shared_ptr<Presence> presenceCopy(lastSentUndirectedPresence ? new Presence(*lastSentUndirectedPresence) : new Presence()); - return presenceCopy; -} - -void PresenceSender::addDirectedPresenceReceiver(const JID& jid) { - directedPresenceReceivers.insert(jid); - if (channel->isAvailable()) { - if (lastSentUndirectedPresence && lastSentUndirectedPresence->getType() == Presence::Available) { - boost::shared_ptr<Presence> presenceCopy(new Presence(*lastSentUndirectedPresence)); - presenceCopy->setTo(jid); - channel->sendPresence(presenceCopy); - } - } -} - -void PresenceSender::removeDirectedPresenceReceiver(const JID& jid) { - directedPresenceReceivers.erase(jid); - if (channel->isAvailable()) { - boost::shared_ptr<Presence> presence(new Presence()); - presence->setType(Presence::Unavailable); - presence->setTo(jid); - channel->sendPresence(presence); - } -} - -void PresenceSender::cancelSubscription(const JID& jid) { - boost::shared_ptr<Presence> stanza(new Presence()); - stanza->setType(Presence::Unsubscribed); - stanza->setTo(jid); - channel->sendPresence(stanza); -} - -void PresenceSender::confirmSubscription(const JID& jid) { - boost::shared_ptr<Presence> stanza(new Presence()); - stanza->setType(Presence::Subscribed); - stanza->setTo(jid); - channel->sendPresence(stanza); -} - - -void PresenceSender::requestSubscription(const JID& jid) { - boost::shared_ptr<Presence> stanza(new Presence()); - stanza->setType(Presence::Subscribe); - stanza->setTo(jid); - channel->sendPresence(stanza); +PresenceSender::~PresenceSender() { } } diff --git a/Swiften/Presence/PresenceSender.h b/Swiften/Presence/PresenceSender.h index 3336523..5abf2f3 100644 --- a/Swiften/Presence/PresenceSender.h +++ b/Swiften/Presence/PresenceSender.h @@ -6,31 +6,15 @@ #pragma once -#include <set> - #include "Swiften/Elements/Presence.h" namespace Swift { - class StanzaChannel; - class PresenceSender { public: - PresenceSender(StanzaChannel*); - - void addDirectedPresenceReceiver(const JID&); - void removeDirectedPresenceReceiver(const JID&); - - void sendPresence(boost::shared_ptr<Presence>); - - void cancelSubscription(const JID& jid); - void confirmSubscription(const JID& jid); - void requestSubscription(const JID& jid); + virtual ~PresenceSender(); - boost::shared_ptr<Presence> getLastSentUndirectedPresence(); + virtual void sendPresence(Presence::ref) = 0; - private: - boost::shared_ptr<Presence> lastSentUndirectedPresence; - StanzaChannel* channel; - std::set<JID> directedPresenceReceivers; + virtual bool isAvailable() const = 0; }; } diff --git a/Swiften/Presence/StanzaChannelPresenceSender.cpp b/Swiften/Presence/StanzaChannelPresenceSender.cpp new file mode 100644 index 0000000..654b7e7 --- /dev/null +++ b/Swiften/Presence/StanzaChannelPresenceSender.cpp @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2010 Remko Tronçon + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#include "Swiften/Presence/StanzaChannelPresenceSender.h" +#include "Swiften/Client/StanzaChannel.h" + +namespace Swift { + +StanzaChannelPresenceSender::StanzaChannelPresenceSender(StanzaChannel* channel) : channel(channel) { +} + +void StanzaChannelPresenceSender::sendPresence(Presence::ref presence) { + channel->sendPresence(presence); +} + +bool StanzaChannelPresenceSender::isAvailable() const { + return channel->isAvailable(); +} + +} diff --git a/Swiften/Presence/StanzaChannelPresenceSender.h b/Swiften/Presence/StanzaChannelPresenceSender.h new file mode 100644 index 0000000..23230ab --- /dev/null +++ b/Swiften/Presence/StanzaChannelPresenceSender.h @@ -0,0 +1,25 @@ +/* + * 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/Presence/PresenceSender.h" + +namespace Swift { + class StanzaChannel; + + class StanzaChannelPresenceSender : public PresenceSender { + public: + StanzaChannelPresenceSender(StanzaChannel*); + + void sendPresence(Presence::ref); + + bool isAvailable() const; + + private: + StanzaChannel* channel; + }; +} diff --git a/Swiften/Presence/SubscriptionManager.cpp b/Swiften/Presence/SubscriptionManager.cpp new file mode 100644 index 0000000..12534dc --- /dev/null +++ b/Swiften/Presence/SubscriptionManager.cpp @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2010 Remko Tronçon + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#include "Swiften/Presence/SubscriptionManager.h" + +#include <boost/bind.hpp> + +#include "Swiften/Base/foreach.h" +#include "Swiften/Client/StanzaChannel.h" + +namespace Swift { + +SubscriptionManager::SubscriptionManager(StanzaChannel* channel) : stanzaChannel(channel) { + stanzaChannel->onPresenceReceived.connect(boost::bind(&SubscriptionManager::handleIncomingPresence, this, _1)); +} + +SubscriptionManager::~SubscriptionManager() { + stanzaChannel->onPresenceReceived.disconnect(boost::bind(&SubscriptionManager::handleIncomingPresence, this, _1)); +} + +void SubscriptionManager::cancelSubscription(const JID& jid) { + Presence::ref stanza(new Presence()); + stanza->setType(Presence::Unsubscribed); + stanza->setTo(jid); + stanzaChannel->sendPresence(stanza); +} + +void SubscriptionManager::confirmSubscription(const JID& jid) { + Presence::ref stanza(new Presence()); + stanza->setType(Presence::Subscribed); + stanza->setTo(jid); + stanzaChannel->sendPresence(stanza); +} + + +void SubscriptionManager::requestSubscription(const JID& jid) { + Presence::ref stanza(new Presence()); + stanza->setType(Presence::Subscribe); + stanza->setTo(jid); + stanzaChannel->sendPresence(stanza); +} + +void SubscriptionManager::handleIncomingPresence(Presence::ref presence) { + JID bareJID(presence->getFrom().toBare()); + if (presence->getType() == Presence::Subscribe) { + onPresenceSubscriptionRequest(bareJID, presence->getStatus()); + } + else if (presence->getType() == Presence::Unsubscribe) { + onPresenceSubscriptionRevoked(bareJID, presence->getStatus()); + } +} + + +} diff --git a/Swiften/Presence/SubscriptionManager.h b/Swiften/Presence/SubscriptionManager.h new file mode 100644 index 0000000..477a2fd --- /dev/null +++ b/Swiften/Presence/SubscriptionManager.h @@ -0,0 +1,41 @@ +/* + * 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 <map> + +#include "Swiften/Base/String.h" +#include "Swiften/JID/JID.h" +#include "Swiften/Base/boost_bsignals.h" +#include "Swiften/Elements/Presence.h" + +namespace Swift { + class StanzaChannel; + + class SubscriptionManager { + public: + SubscriptionManager(StanzaChannel* stanzaChannel); + ~SubscriptionManager(); + + void cancelSubscription(const JID& jid); + void confirmSubscription(const JID& jid); + void requestSubscription(const JID& jid); + + /** + * This signal is emitted when a presence subscription request is received. + */ + boost::signal<void (const JID&, const String&)> onPresenceSubscriptionRequest; + + boost::signal<void (const JID&, const String&)> onPresenceSubscriptionRevoked; + + private: + void handleIncomingPresence(Presence::ref presence); + + private: + StanzaChannel* stanzaChannel; + }; +} diff --git a/Swiften/Presence/UnitTest/PresenceSenderTest.cpp b/Swiften/Presence/UnitTest/DirectedPresenceSenderTest.cpp index 737f317..a60c429 100644 --- a/Swiften/Presence/UnitTest/PresenceSenderTest.cpp +++ b/Swiften/Presence/UnitTest/DirectedPresenceSenderTest.cpp @@ -8,12 +8,13 @@ #include <cppunit/extensions/TestFactoryRegistry.h> #include "Swiften/Client/DummyStanzaChannel.h" -#include "Swiften/Presence/PresenceSender.h" +#include "Swiften/Presence/DirectedPresenceSender.h" +#include "Swiften/Presence/StanzaChannelPresenceSender.h" using namespace Swift; -class PresenceSenderTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(PresenceSenderTest); +class DirectedPresenceSenderTest : public CppUnit::TestFixture { + CPPUNIT_TEST_SUITE(DirectedPresenceSenderTest); CPPUNIT_TEST(testSendPresence); CPPUNIT_TEST(testSendPresence_UndirectedPresenceWithDirectedPresenceReceivers); CPPUNIT_TEST(testSendPresence_DirectedPresenceWithDirectedPresenceReceivers); @@ -29,14 +30,16 @@ class PresenceSenderTest : public CppUnit::TestFixture { testPresence->setStatus("Foo"); secondTestPresence = boost::shared_ptr<Presence>(new Presence()); secondTestPresence->setStatus("Bar"); + stanzaChannelPresenceSender = new StanzaChannelPresenceSender(channel); } void tearDown() { + delete stanzaChannelPresenceSender; delete channel; } void testSendPresence() { - std::auto_ptr<PresenceSender> testling(createPresenceSender()); + std::auto_ptr<DirectedPresenceSender> testling(createPresenceSender()); testling->sendPresence(testPresence); CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel->sentStanzas.size())); @@ -45,7 +48,7 @@ class PresenceSenderTest : public CppUnit::TestFixture { } void testSendPresence_UndirectedPresenceWithDirectedPresenceReceivers() { - std::auto_ptr<PresenceSender> testling(createPresenceSender()); + std::auto_ptr<DirectedPresenceSender> testling(createPresenceSender()); testling->addDirectedPresenceReceiver(JID("alice@wonderland.lit/teaparty")); testling->sendPresence(testPresence); @@ -59,7 +62,7 @@ class PresenceSenderTest : public CppUnit::TestFixture { } void testSendPresence_DirectedPresenceWithDirectedPresenceReceivers() { - std::auto_ptr<PresenceSender> testling(createPresenceSender()); + std::auto_ptr<DirectedPresenceSender> testling(createPresenceSender()); testling->addDirectedPresenceReceiver(JID("alice@wonderland.lit/teaparty")); channel->sentStanzas.clear(); @@ -72,7 +75,7 @@ class PresenceSenderTest : public CppUnit::TestFixture { } void testAddDirectedPresenceReceiver() { - std::auto_ptr<PresenceSender> testling(createPresenceSender()); + std::auto_ptr<DirectedPresenceSender> testling(createPresenceSender()); testling->sendPresence(testPresence); channel->sentStanzas.clear(); @@ -85,7 +88,7 @@ class PresenceSenderTest : public CppUnit::TestFixture { } void testAddDirectedPresenceReceiver_AfterSendingDirectedPresence() { - std::auto_ptr<PresenceSender> testling(createPresenceSender()); + std::auto_ptr<DirectedPresenceSender> testling(createPresenceSender()); testling->sendPresence(testPresence); secondTestPresence->setTo(JID("foo@bar.com")); testling->sendPresence(secondTestPresence); @@ -100,7 +103,7 @@ class PresenceSenderTest : public CppUnit::TestFixture { } void testRemoveDirectedPresenceReceiver() { - std::auto_ptr<PresenceSender> testling(createPresenceSender()); + std::auto_ptr<DirectedPresenceSender> testling(createPresenceSender()); testling->addDirectedPresenceReceiver(JID("alice@wonderland.lit/teaparty")); channel->sentStanzas.clear(); @@ -113,14 +116,15 @@ class PresenceSenderTest : public CppUnit::TestFixture { } private: - PresenceSender* createPresenceSender() { - return new PresenceSender(channel); + DirectedPresenceSender* createPresenceSender() { + return new DirectedPresenceSender(stanzaChannelPresenceSender); } private: DummyStanzaChannel* channel; + StanzaChannelPresenceSender* stanzaChannelPresenceSender; boost::shared_ptr<Presence> testPresence; boost::shared_ptr<Presence> secondTestPresence; }; -CPPUNIT_TEST_SUITE_REGISTRATION(PresenceSenderTest); +CPPUNIT_TEST_SUITE_REGISTRATION(DirectedPresenceSenderTest); diff --git a/Swiften/Presence/UnitTest/PresenceOracleTest.cpp b/Swiften/Presence/UnitTest/PresenceOracleTest.cpp index d3b4b20..aa450a2 100644 --- a/Swiften/Presence/UnitTest/PresenceOracleTest.cpp +++ b/Swiften/Presence/UnitTest/PresenceOracleTest.cpp @@ -11,6 +11,7 @@ #include "Swiften/Presence/PresenceOracle.h" #include "Swiften/Client/DummyStanzaChannel.h" +#include "Swiften/Presence/SubscriptionManager.h" using namespace Swift; @@ -31,13 +32,15 @@ class PresenceOracleTest : public CppUnit::TestFixture { stanzaChannel_ = new DummyStanzaChannel(); oracle_ = new PresenceOracle(stanzaChannel_); oracle_->onPresenceChange.connect(boost::bind(&PresenceOracleTest::handlePresenceChange, this, _1)); - oracle_->onPresenceSubscriptionRequest.connect(boost::bind(&PresenceOracleTest::handlePresenceSubscriptionRequest, this, _1, _2)); + subscriptionManager_ = new SubscriptionManager(stanzaChannel_); + subscriptionManager_->onPresenceSubscriptionRequest.connect(boost::bind(&PresenceOracleTest::handlePresenceSubscriptionRequest, this, _1, _2)); user1 = JID("user1@foo.com/Foo"); user1alt = JID("user1@foo.com/Bar"); user2 = JID("user2@bar.com/Bar"); } void tearDown() { + delete subscriptionManager_; delete oracle_; delete stanzaChannel_; } @@ -181,6 +184,7 @@ class PresenceOracleTest : public CppUnit::TestFixture { String reason; }; PresenceOracle* oracle_; + SubscriptionManager* subscriptionManager_; DummyStanzaChannel* stanzaChannel_; std::vector<Presence::ref> changes; std::vector<SubscriptionRequestInfo> subscriptionRequests; |