summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2010-09-12 18:29:39 (GMT)
committerRemko Tronçon <git@el-tramo.be>2010-09-13 19:19:52 (GMT)
commitef1052bbdb315aaa1c6254098ea05638d9a25b2f (patch)
tree482277f649f6c0fc00027514ea8986861fe33437 /Swiften
parent3ae8cccfe9c6bfed5dda5f024a5cb046ccfc9793 (diff)
downloadswift-contrib-ef1052bbdb315aaa1c6254098ea05638d9a25b2f.zip
swift-contrib-ef1052bbdb315aaa1c6254098ea05638d9a25b2f.tar.bz2
Added presence notifier.
Diffstat (limited to 'Swiften')
-rw-r--r--Swiften/Avatars/AvatarManager.h2
-rw-r--r--Swiften/Avatars/AvatarManagerImpl.cpp8
-rw-r--r--Swiften/Avatars/AvatarManagerImpl.h1
-rw-r--r--Swiften/Avatars/DummyAvatarManager.h32
-rw-r--r--Swiften/Avatars/NullAvatarManager.h4
-rw-r--r--Swiften/Chat/ChatStateTracker.cpp2
-rw-r--r--Swiften/Chat/ChatStateTracker.h2
-rw-r--r--Swiften/Network/DummyTimerFactory.cpp30
-rw-r--r--Swiften/Presence/PresenceOracle.cpp59
-rw-r--r--Swiften/Presence/PresenceOracle.h42
-rw-r--r--Swiften/Presence/PresenceSender.cpp22
-rw-r--r--Swiften/Presence/PresenceSender.h4
-rw-r--r--Swiften/Presence/UnitTest/PresenceOracleTest.cpp143
-rw-r--r--Swiften/Roster/XMPPRoster.cpp13
-rw-r--r--Swiften/Roster/XMPPRoster.h63
15 files changed, 243 insertions, 184 deletions
diff --git a/Swiften/Avatars/AvatarManager.h b/Swiften/Avatars/AvatarManager.h
index 74e58f7..d40c3c0 100644
--- a/Swiften/Avatars/AvatarManager.h
+++ b/Swiften/Avatars/AvatarManager.h
@@ -9,6 +9,7 @@
#include <boost/filesystem.hpp>
#include "Swiften/Base/boost_bsignals.h"
+#include "Swiften/Base/ByteArray.h"
namespace Swift {
class JID;
@@ -17,6 +18,7 @@ namespace Swift {
public:
virtual ~AvatarManager();
+ virtual ByteArray getAvatar(const JID&) const = 0;
virtual boost::filesystem::path getAvatarPath(const JID&) const = 0;
boost::signal<void (const JID&)> onAvatarChanged;
diff --git a/Swiften/Avatars/AvatarManagerImpl.cpp b/Swiften/Avatars/AvatarManagerImpl.cpp
index 384994b..9813aed 100644
--- a/Swiften/Avatars/AvatarManagerImpl.cpp
+++ b/Swiften/Avatars/AvatarManagerImpl.cpp
@@ -11,6 +11,7 @@
#include "Swiften/Avatars/VCardUpdateAvatarManager.h"
#include "Swiften/Avatars/VCardAvatarManager.h"
#include "Swiften/Avatars/AvatarStorage.h"
+#include "Swiften/Base/ByteArray.h"
namespace Swift {
@@ -39,5 +40,12 @@ boost::filesystem::path AvatarManagerImpl::getAvatarPath(const JID& jid) const {
return boost::filesystem::path();
}
+ByteArray AvatarManagerImpl::getAvatar(const JID& jid) const {
+ String hash = combinedAvatarProvider.getAvatarHash(jid);
+ if (!hash.isEmpty()) {
+ return avatarStorage->getAvatar(hash);
+ }
+ return ByteArray();
+}
}
diff --git a/Swiften/Avatars/AvatarManagerImpl.h b/Swiften/Avatars/AvatarManagerImpl.h
index f533160..a28d490 100644
--- a/Swiften/Avatars/AvatarManagerImpl.h
+++ b/Swiften/Avatars/AvatarManagerImpl.h
@@ -33,6 +33,7 @@ namespace Swift {
virtual ~AvatarManagerImpl();
virtual boost::filesystem::path getAvatarPath(const JID&) const;
+ virtual ByteArray getAvatar(const JID&) const;
private:
CombinedAvatarProvider combinedAvatarProvider;
diff --git a/Swiften/Avatars/DummyAvatarManager.h b/Swiften/Avatars/DummyAvatarManager.h
new file mode 100644
index 0000000..db63b05
--- /dev/null
+++ b/Swiften/Avatars/DummyAvatarManager.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 <map>
+
+#include "Swiften/Avatars/AvatarManager.h"
+
+namespace Swift {
+ class DummyAvatarManager : public AvatarManager {
+ public:
+ virtual boost::filesystem::path getAvatarPath(const JID&) const {
+ return boost::filesystem::path();
+ }
+
+ virtual ByteArray getAvatar(const JID& jid) const {
+ std::map<JID, ByteArray>::const_iterator i = avatars.find(jid);
+ if (i != avatars.end()) {
+ return i->second;
+ }
+ else {
+ return ByteArray();
+ }
+ }
+
+ std::map<JID, ByteArray> avatars;
+ };
+}
diff --git a/Swiften/Avatars/NullAvatarManager.h b/Swiften/Avatars/NullAvatarManager.h
index 7f3c646..e96ed7a 100644
--- a/Swiften/Avatars/NullAvatarManager.h
+++ b/Swiften/Avatars/NullAvatarManager.h
@@ -14,5 +14,9 @@ namespace Swift {
virtual boost::filesystem::path getAvatarPath(const JID&) const {
return boost::filesystem::path();
}
+
+ virtual ByteArray getAvatar(const JID&) const {
+ return ByteArray();
+ }
};
}
diff --git a/Swiften/Chat/ChatStateTracker.cpp b/Swiften/Chat/ChatStateTracker.cpp
index b8d76da..985f04a 100644
--- a/Swiften/Chat/ChatStateTracker.cpp
+++ b/Swiften/Chat/ChatStateTracker.cpp
@@ -18,7 +18,7 @@ void ChatStateTracker::handleMessageReceived(boost::shared_ptr<Message> message)
}
}
-void ChatStateTracker::handlePresenceChange(boost::shared_ptr<Presence> newPresence, boost::shared_ptr<Presence>) {
+void ChatStateTracker::handlePresenceChange(boost::shared_ptr<Presence> newPresence) {
if (newPresence->getType() == Presence::Unavailable) {
onChatStateChange(ChatState::Gone);
}
diff --git a/Swiften/Chat/ChatStateTracker.h b/Swiften/Chat/ChatStateTracker.h
index c8e8cb5..343d828 100644
--- a/Swiften/Chat/ChatStateTracker.h
+++ b/Swiften/Chat/ChatStateTracker.h
@@ -18,7 +18,7 @@ namespace Swift {
public:
ChatStateTracker();
void handleMessageReceived(boost::shared_ptr<Message> message);
- void handlePresenceChange(boost::shared_ptr<Presence> newPresence, boost::shared_ptr<Presence> oldPresence);
+ void handlePresenceChange(boost::shared_ptr<Presence> newPresence);
boost::signal<void (ChatState::ChatStateType)> onChatStateChange;
private:
void changeState(ChatState::ChatStateType state);
diff --git a/Swiften/Network/DummyTimerFactory.cpp b/Swiften/Network/DummyTimerFactory.cpp
index 466dd38..105b103 100644
--- a/Swiften/Network/DummyTimerFactory.cpp
+++ b/Swiften/Network/DummyTimerFactory.cpp
@@ -15,19 +15,26 @@ namespace Swift {
class DummyTimerFactory::DummyTimer : public Timer {
public:
- DummyTimer(int timeout) : timeout(timeout), isRunning(false) {
+ DummyTimer(int timeout, DummyTimerFactory* factory) : timeout(timeout), factory(factory), isRunning(false), startTime(~0) {
}
virtual void start() {
isRunning = true;
+ startTime = factory->currentTime;
}
virtual void stop() {
isRunning = false;
}
+
+ int getAlarmTime() const {
+ return startTime + timeout;
+ }
int timeout;
+ DummyTimerFactory* factory;
bool isRunning;
+ int startTime;
};
@@ -35,31 +42,18 @@ DummyTimerFactory::DummyTimerFactory() : currentTime(0) {
}
boost::shared_ptr<Timer> DummyTimerFactory::createTimer(int milliseconds) {
- boost::shared_ptr<DummyTimer> timer(new DummyTimer(milliseconds));
+ boost::shared_ptr<DummyTimer> timer(new DummyTimer(milliseconds, this));
timers.push_back(timer);
return timer;
}
-static bool hasZeroTimeout(boost::shared_ptr<DummyTimerFactory::DummyTimer> timer) {
- return timer->timeout == 0;
-}
-
void DummyTimerFactory::setTime(int time) {
assert(time > currentTime);
- int increment = time - currentTime;
- std::vector< boost::shared_ptr<DummyTimer> > notifyTimers(timers.begin(), timers.end());
- foreach(boost::shared_ptr<DummyTimer> timer, notifyTimers) {
- if (increment >= timer->timeout) {
- if (timer->isRunning) {
- timer->onTick();
- }
- timer->timeout = 0;
- }
- else {
- timer->timeout -= increment;
+ foreach(boost::shared_ptr<DummyTimer> timer, timers) {
+ if (timer->getAlarmTime() > currentTime && timer->getAlarmTime() <= time && timer->isRunning) {
+ timer->onTick();
}
}
- timers.erase(std::remove_if(timers.begin(), timers.end(), hasZeroTimeout), timers.end());
currentTime = time;
}
diff --git a/Swiften/Presence/PresenceOracle.cpp b/Swiften/Presence/PresenceOracle.cpp
index 44934e6..758ae7c 100644
--- a/Swiften/Presence/PresenceOracle.cpp
+++ b/Swiften/Presence/PresenceOracle.cpp
@@ -7,37 +7,26 @@
#include "PresenceOracle.h"
#include <boost/bind.hpp>
+
#include "Swiften/Client/StanzaChannel.h"
-namespace Swift {
-typedef std::pair<JID, std::map<JID, boost::shared_ptr<Presence> > > JIDMapPair;
-typedef std::pair<JID, boost::shared_ptr<Presence> > JIDPresencePair;
+namespace Swift {
PresenceOracle::PresenceOracle(StanzaChannel* stanzaChannel) {
stanzaChannel_ = stanzaChannel;
stanzaChannel_->onPresenceReceived.connect(boost::bind(&PresenceOracle::handleIncomingPresence, this, _1));
+ stanzaChannel_->onAvailableChanged.connect(boost::bind(&PresenceOracle::handleStanzaChannelAvailableChanged, this, _1));
}
-void PresenceOracle::cancelSubscription(const JID& jid) {
- boost::shared_ptr<Presence> stanza(new Presence());
- stanza->setType(Presence::Unsubscribed);
- stanza->setTo(jid);
- stanzaChannel_->sendPresence(stanza);
+PresenceOracle::~PresenceOracle() {
+ stanzaChannel_->onPresenceReceived.disconnect(boost::bind(&PresenceOracle::handleIncomingPresence, this, _1));
+ stanzaChannel_->onAvailableChanged.disconnect(boost::bind(&PresenceOracle::handleStanzaChannelAvailableChanged, this, _1));
}
-void PresenceOracle::confirmSubscription(const JID& jid) {
- boost::shared_ptr<Presence> stanza(new Presence());
- stanza->setType(Presence::Subscribed);
- stanza->setTo(jid);
- stanzaChannel_->sendPresence(stanza);
-}
-
-
-void PresenceOracle::requestSubscription(const JID& jid) {
- boost::shared_ptr<Presence> stanza(new Presence());
- stanza->setType(Presence::Subscribe);
- stanza->setTo(jid);
- stanzaChannel_->sendPresence(stanza);
+void PresenceOracle::handleStanzaChannelAvailableChanged(bool available) {
+ if (available) {
+ entries_.clear();
+ }
}
@@ -46,19 +35,29 @@ void PresenceOracle::handleIncomingPresence(boost::shared_ptr<Presence> presence
if (presence->getType() == Presence::Subscribe) {
onPresenceSubscriptionRequest(bareJID, presence->getStatus());
- } else {
+ }
+ else {
std::map<JID, boost::shared_ptr<Presence> > jidMap = entries_[bareJID];
- boost::shared_ptr<Presence> last;
- foreach(JIDPresencePair pair, jidMap) {
- if (pair.first == presence->getFrom()) {
- last = pair.second;
- break;
- }
- }
jidMap[presence->getFrom()] = presence;
entries_[bareJID] = jidMap;
- onPresenceChange(presence, last);
+ onPresenceChange(presence);
}
}
+Presence::ref PresenceOracle::getLastPresence(const JID& jid) const {
+ PresencesMap::const_iterator i = entries_.find(jid.toBare());
+ if (i == entries_.end()) {
+ return Presence::ref();
+ }
+ PresenceMap presenceMap = i->second;
+ PresenceMap::const_iterator j = presenceMap.find(jid);
+ if (j != presenceMap.end()) {
+ return j->second;
+ }
+ else {
+ return Presence::ref();
+ }
+}
+
+
}
diff --git a/Swiften/Presence/PresenceOracle.h b/Swiften/Presence/PresenceOracle.h
index 9f64000..e5f0372 100644
--- a/Swiften/Presence/PresenceOracle.h
+++ b/Swiften/Presence/PresenceOracle.h
@@ -6,31 +6,35 @@
#pragma once
+#include <map>
+
#include "Swiften/Base/String.h"
#include "Swiften/Elements/Presence.h"
-#include <map>
#include "Swiften/Base/boost_bsignals.h"
namespace Swift {
class StanzaChannel;
-
-class PresenceOracle {
- public:
- PresenceOracle(StanzaChannel* stanzaChannel);
- ~PresenceOracle() {};
-
- void cancelSubscription(const JID& jid);
- void confirmSubscription(const JID& jid);
- void requestSubscription(const JID& jid);
-
- boost::signal<void (boost::shared_ptr<Presence>, boost::shared_ptr<Presence>)> onPresenceChange;
- boost::signal<void (const JID&, const String&)> onPresenceSubscriptionRequest;
-
- private:
- void handleIncomingPresence(boost::shared_ptr<Presence> presence);
- std::map<JID, std::map<JID, boost::shared_ptr<Presence> > > entries_;
- StanzaChannel* stanzaChannel_;
-};
+ class PresenceOracle {
+ public:
+ PresenceOracle(StanzaChannel* stanzaChannel);
+ ~PresenceOracle();
+
+ Presence::ref getLastPresence(const JID&) const;
+
+ public:
+ boost::signal<void (boost::shared_ptr<Presence>)> onPresenceChange;
+ boost::signal<void (const JID&, const String&)> onPresenceSubscriptionRequest;
+
+ private:
+ void handleIncomingPresence(boost::shared_ptr<Presence> presence);
+ void handleStanzaChannelAvailableChanged(bool);
+
+ private:
+ typedef std::map<JID, Presence::ref> PresenceMap;
+ typedef std::map<JID, PresenceMap> PresencesMap;
+ PresencesMap entries_;
+ StanzaChannel* stanzaChannel_;
+ };
}
diff --git a/Swiften/Presence/PresenceSender.cpp b/Swiften/Presence/PresenceSender.cpp
index e4562a9..082c841 100644
--- a/Swiften/Presence/PresenceSender.cpp
+++ b/Swiften/Presence/PresenceSender.cpp
@@ -52,4 +52,26 @@ void PresenceSender::removeDirectedPresenceReceiver(const JID& jid) {
}
}
+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);
+}
+
}
diff --git a/Swiften/Presence/PresenceSender.h b/Swiften/Presence/PresenceSender.h
index da4bc70..87be753 100644
--- a/Swiften/Presence/PresenceSender.h
+++ b/Swiften/Presence/PresenceSender.h
@@ -22,6 +22,10 @@ namespace Swift {
void sendPresence(boost::shared_ptr<Presence>);
+ void cancelSubscription(const JID& jid);
+ void confirmSubscription(const JID& jid);
+ void requestSubscription(const JID& jid);
+
private:
boost::shared_ptr<Presence> lastSentUndirectedPresence;
StanzaChannel* channel;
diff --git a/Swiften/Presence/UnitTest/PresenceOracleTest.cpp b/Swiften/Presence/UnitTest/PresenceOracleTest.cpp
index 691beb7..e96d8a4 100644
--- a/Swiften/Presence/UnitTest/PresenceOracleTest.cpp
+++ b/Swiften/Presence/UnitTest/PresenceOracleTest.cpp
@@ -8,58 +8,29 @@
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
-#include <boost/optional.hpp>
#include "Swiften/Presence/PresenceOracle.h"
#include "Swiften/Client/DummyStanzaChannel.h"
using namespace Swift;
-class PresencePointerPair {
- public:
- boost::shared_ptr<Presence> one;
- boost::shared_ptr<Presence> two;
-};
-
-class SubscriptionRequestInfo {
- public:
- boost::optional<JID> jid;
- boost::optional<String> reason;
-};
-
-class PresenceOracleTest : public CppUnit::TestFixture
-{
+class PresenceOracleTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(PresenceOracleTest);
- CPPUNIT_TEST(testFirstPresence);
- CPPUNIT_TEST(testSecondPresence);
+ CPPUNIT_TEST(testReceivePresence);
+ CPPUNIT_TEST(testReceivePresenceFromDifferentResources);
CPPUNIT_TEST(testSubscriptionRequest);
+ CPPUNIT_TEST(testReconnectResetsPresences);
CPPUNIT_TEST_SUITE_END();
- private:
- PresenceOracle* oracle_;
- DummyStanzaChannel* stanzaChannel_;
-
public:
-
- void handlePresenceChange(boost::shared_ptr<Presence> newPresence, boost::shared_ptr<Presence> lastPresence, PresencePointerPair* out) {
- CPPUNIT_ASSERT(out->one.get() == NULL);
- CPPUNIT_ASSERT(out->two.get() == NULL);
- out->one = newPresence;
- out->two = lastPresence;
- CPPUNIT_ASSERT(newPresence.get());
- CPPUNIT_ASSERT_EQUAL(newPresence, out->one);
- }
-
- void handlePresenceSubscriptionRequest(const JID& jid, const String& reason, SubscriptionRequestInfo* info) {
- CPPUNIT_ASSERT(!info->jid);
- CPPUNIT_ASSERT(!info->reason);
- info->jid = jid;
- info->reason = reason;
- }
-
void setUp() {
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));
+ user1 = JID("user1@foo.com/Foo");
+ user1alt = JID("user1@foo.com/Bar");
+ user2 = JID("user2@bar.com/Bar");
}
void tearDown() {
@@ -67,49 +38,27 @@ class PresenceOracleTest : public CppUnit::TestFixture
delete stanzaChannel_;
}
- void testFirstPresence() {
- PresencePointerPair out;
- oracle_->onPresenceChange.connect(boost::bind(&PresenceOracleTest::handlePresenceChange, this, _1, _2, &out));
-
- SubscriptionRequestInfo info;
- oracle_->onPresenceSubscriptionRequest.connect(boost::bind(&PresenceOracleTest::handlePresenceSubscriptionRequest, this, _1, _2, &info));
-
- boost::shared_ptr<Presence> sentPresence(new Presence("blarb"));
+ void testReceivePresence() {
+ boost::shared_ptr<Presence> sentPresence(createPresence(user1));
stanzaChannel_->onPresenceReceived(sentPresence);
- CPPUNIT_ASSERT(!info.jid);
- CPPUNIT_ASSERT(!info.reason);
- CPPUNIT_ASSERT(out.two.get() == NULL);
- CPPUNIT_ASSERT_EQUAL(sentPresence, out.one);
+ CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changes.size()));
+ CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(subscriptionRequests.size()));
+ CPPUNIT_ASSERT_EQUAL(sentPresence, changes[0]);
+ CPPUNIT_ASSERT_EQUAL(sentPresence, oracle_->getLastPresence(user1));
}
-
- void testSecondPresence() {
- PresencePointerPair out;
- oracle_->onPresenceChange.connect(boost::bind(&PresenceOracleTest::handlePresenceChange, this, _1, _2, &out));
- boost::shared_ptr<Presence> sentPresence1(new Presence("blarb"));
+ void testReceivePresenceFromDifferentResources() {
+ boost::shared_ptr<Presence> sentPresence1(createPresence(user1));
+ boost::shared_ptr<Presence> sentPresence2(createPresence(user1alt));
stanzaChannel_->onPresenceReceived(sentPresence1);
- CPPUNIT_ASSERT_EQUAL(sentPresence1, out.one);
- out.one = boost::shared_ptr<Presence>();
-
- SubscriptionRequestInfo info;
- oracle_->onPresenceSubscriptionRequest.connect(boost::bind(&PresenceOracleTest::handlePresenceSubscriptionRequest, this, _1, _2, &info));
-
- boost::shared_ptr<Presence> sentPresence2(new Presence("test2"));
stanzaChannel_->onPresenceReceived(sentPresence2);
- CPPUNIT_ASSERT(!info.jid);
- CPPUNIT_ASSERT(!info.reason);
- CPPUNIT_ASSERT_EQUAL(sentPresence1, out.two);
- CPPUNIT_ASSERT_EQUAL(sentPresence2, out.one);
+ CPPUNIT_ASSERT_EQUAL(sentPresence1, oracle_->getLastPresence(user1));
+ CPPUNIT_ASSERT_EQUAL(sentPresence2, oracle_->getLastPresence(user1alt));
}
-
+
void testSubscriptionRequest() {
- PresencePointerPair out;
- oracle_->onPresenceChange.connect(boost::bind(&PresenceOracleTest::handlePresenceChange, this, _1, _2, &out));
- SubscriptionRequestInfo info;
- oracle_->onPresenceSubscriptionRequest.connect(boost::bind(&PresenceOracleTest::handlePresenceSubscriptionRequest, this, _1, _2, &info));
-
String reasonText = "Because I want to";
JID sentJID = JID("me@example.com");
@@ -119,14 +68,52 @@ class PresenceOracleTest : public CppUnit::TestFixture
sentPresence->setStatus(reasonText);
stanzaChannel_->onPresenceReceived(sentPresence);
- CPPUNIT_ASSERT(info.jid);
- CPPUNIT_ASSERT(info.reason);
- CPPUNIT_ASSERT_EQUAL(sentJID, info.jid.get());
- CPPUNIT_ASSERT_EQUAL(reasonText, info.reason.get());
+ CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(changes.size()));
+ CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(subscriptionRequests.size()));
+ CPPUNIT_ASSERT_EQUAL(sentJID, subscriptionRequests[0].jid);
+ CPPUNIT_ASSERT_EQUAL(reasonText, subscriptionRequests[0].reason);
+ }
+
+ void testReconnectResetsPresences() {
+ boost::shared_ptr<Presence> sentPresence(createPresence(user1));
+ stanzaChannel_->onPresenceReceived(sentPresence);
+ stanzaChannel_->setAvailable(false);
+ stanzaChannel_->setAvailable(true);
- CPPUNIT_ASSERT(!out.two);
- CPPUNIT_ASSERT(!out.one);
+ CPPUNIT_ASSERT(!oracle_->getLastPresence(user1));
}
+
+ private:
+ void handlePresenceChange(boost::shared_ptr<Presence> newPresence) {
+ changes.push_back(newPresence);
+ }
+
+ void handlePresenceSubscriptionRequest(const JID& jid, const String& reason) {
+ SubscriptionRequestInfo subscriptionRequest;
+ subscriptionRequest.jid = jid;
+ subscriptionRequest.reason = reason;
+ subscriptionRequests.push_back(subscriptionRequest);
+ }
+
+ boost::shared_ptr<Presence> createPresence(const JID& jid) {
+ boost::shared_ptr<Presence> sentPresence(new Presence("blarb"));
+ sentPresence->setFrom(jid);
+ return sentPresence;
+ }
+
+ private:
+ struct SubscriptionRequestInfo {
+ JID jid;
+ String reason;
+ };
+ PresenceOracle* oracle_;
+ DummyStanzaChannel* stanzaChannel_;
+ std::vector<Presence::ref> changes;
+ std::vector<SubscriptionRequestInfo> subscriptionRequests;
+ JID user1;
+ JID user1alt;
+ JID user2;
};
+
CPPUNIT_TEST_SUITE_REGISTRATION(PresenceOracleTest);
diff --git a/Swiften/Roster/XMPPRoster.cpp b/Swiften/Roster/XMPPRoster.cpp
index 28b04c6..56616c2 100644
--- a/Swiften/Roster/XMPPRoster.cpp
+++ b/Swiften/Roster/XMPPRoster.cpp
@@ -8,6 +8,9 @@
namespace Swift {
+XMPPRoster::XMPPRoster() {
+}
+
void XMPPRoster::addContact(const JID& jid, const String& name, const std::vector<String>& groups, RosterItemPayload::Subscription subscription) {
JID bareJID(jid.toBare());
bool exists = containsJID(bareJID);
@@ -43,8 +46,14 @@ bool XMPPRoster::containsJID(const JID& jid) {
return entries_.find(JID(jid.toBare())) != entries_.end();
}
-const String& XMPPRoster::getNameForJID(const JID& jid) {
- return entries_[JID(jid.toBare())].name;
+String XMPPRoster::getNameForJID(const JID& jid) const {
+ std::map<JID, XMPPRosterItem>::const_iterator i = entries_.find(jid.toBare());
+ if (i != entries_.end()) {
+ return i->second.name;
+ }
+ else {
+ return "";
+ }
}
const std::vector<String>& XMPPRoster::getGroupsForJID(const JID& jid) {
diff --git a/Swiften/Roster/XMPPRoster.h b/Swiften/Roster/XMPPRoster.h
index e449d28..abafdfe 100644
--- a/Swiften/Roster/XMPPRoster.h
+++ b/Swiften/Roster/XMPPRoster.h
@@ -4,8 +4,7 @@
* See Documentation/Licenses/GPLv3.txt for more information.
*/
-#ifndef SWIFTEN_XMPPRoster_H
-#define SWIFTEN_XMPPRoster_H
+#pragma once
#include "Swiften/Base/String.h"
#include "Swiften/JID/JID.h"
@@ -16,37 +15,31 @@
#include "Swiften/Base/boost_bsignals.h"
namespace Swift {
-
-struct XMPPRosterItem {
- JID jid;
- String name;
- std::vector<String> groups;
- RosterItemPayload::Subscription subscription;
-};
-
-class XMPPRoster {
- public:
- XMPPRoster() {};
- ~XMPPRoster() {};
-
- void addContact(const JID& jid, const String& name, const std::vector<String>& groups, const RosterItemPayload::Subscription subscription);
- bool containsJID(const JID& jid);
- void removeContact(const JID& jid);
- void clear();
- RosterItemPayload::Subscription getSubscriptionStateForJID(const JID& jid);
- const String& getNameForJID(const JID& jid);
- const std::vector<String>& getGroupsForJID(const JID& jid);
-
- boost::signal<void (const JID&)> onJIDAdded;
- boost::signal<void (const JID&)> onJIDRemoved;
- boost::signal<void (const JID&, const String&, const std::vector<String>&)> onJIDUpdated;
- boost::signal<void ()> onRosterCleared;
-
- private:
- //std::map<JID, std::pair<String, std::vector<String> > > entries_;
- std::map<JID, XMPPRosterItem> entries_;
-};
+ class XMPPRoster {
+ public:
+ XMPPRoster();
+
+ void addContact(const JID& jid, const String& name, const std::vector<String>& groups, const RosterItemPayload::Subscription subscription);
+ void removeContact(const JID& jid);
+ void clear();
+
+ bool containsJID(const JID& jid);
+ RosterItemPayload::Subscription getSubscriptionStateForJID(const JID& jid);
+ String getNameForJID(const JID& jid) const;
+ const std::vector<String>& getGroupsForJID(const JID& jid);
+
+ boost::signal<void (const JID&)> onJIDAdded;
+ boost::signal<void (const JID&)> onJIDRemoved;
+ boost::signal<void (const JID&, const String&, const std::vector<String>&)> onJIDUpdated;
+ boost::signal<void ()> onRosterCleared;
+
+ private:
+ struct XMPPRosterItem {
+ JID jid;
+ String name;
+ std::vector<String> groups;
+ RosterItemPayload::Subscription subscription;
+ };
+ std::map<JID, XMPPRosterItem> entries_;
+ };
}
-
-#endif
-