diff options
author | Remko Tronçon <git@el-tramo.be> | 2010-10-24 12:10:44 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2010-10-24 12:15:24 (GMT) |
commit | b107bdd08df9ae3a978ad8f966a26eb8d551bd13 (patch) | |
tree | ad1c4d6550f323a19dffd699c98513aa68acddaf /Swiften/Roster | |
parent | cfa314bd0b6f6ed9285c7167948899184f6a8c12 (diff) | |
download | swift-contrib-b107bdd08df9ae3a978ad8f966a26eb8d551bd13.zip swift-contrib-b107bdd08df9ae3a978ad8f966a26eb8d551bd13.tar.bz2 |
Creating abstract XMPPRoster base class.
Diffstat (limited to 'Swiften/Roster')
-rw-r--r-- | Swiften/Roster/UnitTest/XMPPRosterControllerTest.cpp | 6 | ||||
-rw-r--r-- | Swiften/Roster/UnitTest/XMPPRosterImplTest.cpp (renamed from Swiften/Roster/UnitTest/XMPPRosterTest.cpp) | 15 | ||||
-rw-r--r-- | Swiften/Roster/XMPPRoster.cpp | 51 | ||||
-rw-r--r-- | Swiften/Roster/XMPPRoster.h | 61 | ||||
-rw-r--r-- | Swiften/Roster/XMPPRosterController.cpp | 4 | ||||
-rw-r--r-- | Swiften/Roster/XMPPRosterController.h | 6 | ||||
-rw-r--r-- | Swiften/Roster/XMPPRosterImpl.cpp | 68 | ||||
-rw-r--r-- | Swiften/Roster/XMPPRosterImpl.h | 36 |
8 files changed, 164 insertions, 83 deletions
diff --git a/Swiften/Roster/UnitTest/XMPPRosterControllerTest.cpp b/Swiften/Roster/UnitTest/XMPPRosterControllerTest.cpp index 33fc04a..17c5527 100644 --- a/Swiften/Roster/UnitTest/XMPPRosterControllerTest.cpp +++ b/Swiften/Roster/UnitTest/XMPPRosterControllerTest.cpp @@ -13,7 +13,7 @@ #include "Swiften/Elements/RosterPayload.h" #include "Swiften/Queries/DummyIQChannel.h" #include "Swiften/Queries/IQRouter.h" -#include "Swiften/Roster/XMPPRoster.h" +#include "Swiften/Roster/XMPPRosterImpl.h" using namespace Swift; @@ -31,7 +31,7 @@ class XMPPRosterControllerTest : public CppUnit::TestFixture void setUp() { channel_ = new DummyIQChannel(); router_ = new IQRouter(channel_); - xmppRoster_ = new XMPPRoster(); + xmppRoster_ = new XMPPRosterImpl(); } void tearDown() { @@ -79,7 +79,7 @@ class XMPPRosterControllerTest : public CppUnit::TestFixture private: DummyIQChannel* channel_; IQRouter* router_; - XMPPRoster* xmppRoster_; + XMPPRosterImpl* xmppRoster_; }; CPPUNIT_TEST_SUITE_REGISTRATION(XMPPRosterControllerTest); diff --git a/Swiften/Roster/UnitTest/XMPPRosterTest.cpp b/Swiften/Roster/UnitTest/XMPPRosterImplTest.cpp index 7e64d8c..fbc3430 100644 --- a/Swiften/Roster/UnitTest/XMPPRosterTest.cpp +++ b/Swiften/Roster/UnitTest/XMPPRosterImplTest.cpp @@ -11,7 +11,7 @@ #include <vector> -#include "Swiften/Roster/XMPPRoster.h" +#include "Swiften/Roster/XMPPRosterImpl.h" using namespace Swift; @@ -73,16 +73,16 @@ private: }; -class XMPPRosterTest : public CppUnit::TestFixture +class XMPPRosterImplTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(XMPPRosterTest); + CPPUNIT_TEST_SUITE(XMPPRosterImplTest); CPPUNIT_TEST(testJIDAdded); CPPUNIT_TEST(testJIDRemoved); CPPUNIT_TEST(testJIDUpdated); CPPUNIT_TEST_SUITE_END(); private: - XMPPRoster* roster_; + XMPPRosterImpl* roster_; XMPPRosterSignalHandler* handler_; JID jid1_; JID jid2_; @@ -92,11 +92,10 @@ class XMPPRosterTest : public CppUnit::TestFixture public: - - XMPPRosterTest() : jid1_(JID("a@b.c")), jid2_(JID("b@c.d")), jid3_(JID("c@d.e")) {} + XMPPRosterImplTest() : jid1_(JID("a@b.c")), jid2_(JID("b@c.d")), jid3_(JID("c@d.e")) {} void setUp() { - roster_ = new XMPPRoster(); + roster_ = new XMPPRosterImpl(); handler_ = new XMPPRosterSignalHandler(roster_); groups1_.push_back("bobs"); groups1_.push_back("berts"); @@ -167,5 +166,5 @@ class XMPPRosterTest : public CppUnit::TestFixture } }; -CPPUNIT_TEST_SUITE_REGISTRATION(XMPPRosterTest); +CPPUNIT_TEST_SUITE_REGISTRATION(XMPPRosterImplTest); diff --git a/Swiften/Roster/XMPPRoster.cpp b/Swiften/Roster/XMPPRoster.cpp index 56616c2..61630b4 100644 --- a/Swiften/Roster/XMPPRoster.cpp +++ b/Swiften/Roster/XMPPRoster.cpp @@ -11,57 +11,8 @@ 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); - String oldName = getNameForJID(bareJID); - std::vector<String> oldGroups = entries_[bareJID].groups; - if (exists) { - entries_.erase(bareJID); - } - XMPPRosterItem item; - item.groups = groups; - item.name = name; - item.jid = jid; - item.subscription = subscription; - entries_[bareJID] = item; - if (exists) { - onJIDUpdated(bareJID, oldName, oldGroups); - } else { - onJIDAdded(bareJID); - } -} - -void XMPPRoster::removeContact(const JID& jid) { - entries_.erase(JID(jid.toBare())); - onJIDRemoved(jid); -} - -void XMPPRoster::clear() { - entries_.clear(); - onRosterCleared(); -} - -bool XMPPRoster::containsJID(const JID& jid) { - return entries_.find(JID(jid.toBare())) != entries_.end(); -} - -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) { - return entries_[JID(jid.toBare())].groups; -} -RosterItemPayload::Subscription XMPPRoster::getSubscriptionStateForJID(const JID& jid) { - return entries_[JID(jid.toBare())].subscription; +XMPPRoster::~XMPPRoster() { } } diff --git a/Swiften/Roster/XMPPRoster.h b/Swiften/Roster/XMPPRoster.h index abafdfe..b88148d 100644 --- a/Swiften/Roster/XMPPRoster.h +++ b/Swiften/Roster/XMPPRoster.h @@ -10,36 +10,63 @@ #include "Swiften/JID/JID.h" #include "Swiften/Elements/RosterItemPayload.h" -#include <map> #include <vector> #include "Swiften/Base/boost_bsignals.h" namespace Swift { + /** + * This class represents the roster of an account, as stored on the XMPP server. + * + * Changes to the roster (either due to subscription requests or by going online/offline) are + * emitted through signals. + */ class XMPPRoster { public: XMPPRoster(); + virtual ~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(); + /** + * Checks whether the bare jid of the given jid is in the roster. + */ + virtual bool containsJID(const JID& jid) = 0; - 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); + /** + * Retrieves the subscription state for the given jid. + */ + virtual RosterItemPayload::Subscription getSubscriptionStateForJID(const JID& jid) = 0; + /** + * Retrieves the stored roster name for the given jid. + */ + virtual String getNameForJID(const JID& jid) const = 0; + + /** + * Returns the list of groups for the given JID. + */ + virtual const std::vector<String>& getGroupsForJID(const JID& jid) = 0; + + public: + /** + * Emitted when the given JID is added to the roster. + */ boost::signal<void (const JID&)> onJIDAdded; + + /** + * Emitted when the given JID is removed from the roster. + */ boost::signal<void (const JID&)> onJIDRemoved; + + /** + * Emitted when the name or the groups of the roster item with the + * given JID changes. + */ 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_; + /** + * Emitted when the roster is reset (e.g. due to logging in/logging out). + * After this signal is emitted, the roster is empty. It will be repopulated through + * onJIDAdded and onJIDRemoved events. + */ + boost::signal<void ()> onRosterCleared; }; } diff --git a/Swiften/Roster/XMPPRosterController.cpp b/Swiften/Roster/XMPPRosterController.cpp index 024cb1d..75ab494 100644 --- a/Swiften/Roster/XMPPRosterController.cpp +++ b/Swiften/Roster/XMPPRosterController.cpp @@ -16,14 +16,14 @@ #include "Swiften/Roster/Roster.h" #include "Swiften/Roster/SetPresence.h" #include "Swiften/Roster/OfflineRosterFilter.h" -#include "Swiften/Roster/XMPPRoster.h" +#include "Swiften/Roster/XMPPRosterImpl.h" namespace Swift { /** * The controller does not gain ownership of these parameters. */ -XMPPRosterController::XMPPRosterController(IQRouter* iqRouter, XMPPRoster* xmppRoster) : iqRouter_(iqRouter), rosterPushResponder_(iqRouter), xmppRoster_(xmppRoster) { +XMPPRosterController::XMPPRosterController(IQRouter* iqRouter, XMPPRosterImpl* xmppRoster) : iqRouter_(iqRouter), rosterPushResponder_(iqRouter), xmppRoster_(xmppRoster) { rosterPushResponder_.onRosterReceived.connect(boost::bind(&XMPPRosterController::handleRosterReceived, this, _1)); rosterPushResponder_.start(); } diff --git a/Swiften/Roster/XMPPRosterController.h b/Swiften/Roster/XMPPRosterController.h index d846b86..dedf090 100644 --- a/Swiften/Roster/XMPPRosterController.h +++ b/Swiften/Roster/XMPPRosterController.h @@ -17,11 +17,11 @@ namespace Swift { class IQRouter; - class XMPPRoster; + class XMPPRosterImpl; class XMPPRosterController { public: - XMPPRosterController(IQRouter *iqRouter, XMPPRoster* xmppRoster); + XMPPRosterController(IQRouter *iqRouter, XMPPRosterImpl* xmppRoster); ~XMPPRosterController(); void requestRoster(); @@ -31,7 +31,7 @@ namespace Swift { private: IQRouter* iqRouter_; RosterPushResponder rosterPushResponder_; - XMPPRoster* xmppRoster_; + XMPPRosterImpl* xmppRoster_; }; } diff --git a/Swiften/Roster/XMPPRosterImpl.cpp b/Swiften/Roster/XMPPRosterImpl.cpp new file mode 100644 index 0000000..762ae29 --- /dev/null +++ b/Swiften/Roster/XMPPRosterImpl.cpp @@ -0,0 +1,68 @@ +/* + * 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/XMPPRosterImpl.h" + +namespace Swift { + +XMPPRosterImpl::XMPPRosterImpl() { +} + +void XMPPRosterImpl::addContact(const JID& jid, const String& name, const std::vector<String>& groups, RosterItemPayload::Subscription subscription) { + JID bareJID(jid.toBare()); + bool exists = containsJID(bareJID); + String oldName = getNameForJID(bareJID); + std::vector<String> oldGroups = entries_[bareJID].groups; + if (exists) { + entries_.erase(bareJID); + } + XMPPRosterItem item; + item.groups = groups; + item.name = name; + item.jid = jid; + item.subscription = subscription; + entries_[bareJID] = item; + if (exists) { + onJIDUpdated(bareJID, oldName, oldGroups); + } else { + onJIDAdded(bareJID); + } +} + +void XMPPRosterImpl::removeContact(const JID& jid) { + entries_.erase(JID(jid.toBare())); + onJIDRemoved(jid); +} + +void XMPPRosterImpl::clear() { + entries_.clear(); + onRosterCleared(); +} + +bool XMPPRosterImpl::containsJID(const JID& jid) { + return entries_.find(JID(jid.toBare())) != entries_.end(); +} + +String XMPPRosterImpl::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>& XMPPRosterImpl::getGroupsForJID(const JID& jid) { + return entries_[JID(jid.toBare())].groups; +} + +RosterItemPayload::Subscription XMPPRosterImpl::getSubscriptionStateForJID(const JID& jid) { + return entries_[JID(jid.toBare())].subscription; +} + +} + diff --git a/Swiften/Roster/XMPPRosterImpl.h b/Swiften/Roster/XMPPRosterImpl.h new file mode 100644 index 0000000..c2d2458 --- /dev/null +++ b/Swiften/Roster/XMPPRosterImpl.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 <map> + +#include "Swiften/Roster/XMPPRoster.h" + +namespace Swift { + class XMPPRosterImpl : public XMPPRoster { + public: + XMPPRosterImpl(); + + 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); + + private: + struct XMPPRosterItem { + JID jid; + String name; + std::vector<String> groups; + RosterItemPayload::Subscription subscription; + }; + std::map<JID, XMPPRosterItem> entries_; + }; +} |