diff options
Diffstat (limited to 'Swiften/VCards')
-rw-r--r-- | Swiften/VCards/SetVCardRequest.h | 26 | ||||
-rw-r--r-- | Swiften/VCards/UnitTest/VCardManagerTest.cpp | 45 | ||||
-rw-r--r-- | Swiften/VCards/VCardManager.cpp | 22 | ||||
-rw-r--r-- | Swiften/VCards/VCardManager.h | 17 |
4 files changed, 108 insertions, 2 deletions
diff --git a/Swiften/VCards/SetVCardRequest.h b/Swiften/VCards/SetVCardRequest.h new file mode 100644 index 0000000..8dfda5d --- /dev/null +++ b/Swiften/VCards/SetVCardRequest.h @@ -0,0 +1,26 @@ +/* + * 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/Queries/GenericRequest.h> +#include <Swiften/Elements/VCard.h> + + +namespace Swift { + class SetVCardRequest : public GenericRequest<VCard> { + public: + typedef boost::shared_ptr<SetVCardRequest> ref; + + static ref create(VCard::ref vcard, IQRouter* router) { + return ref(new SetVCardRequest(vcard, router)); + } + + private: + SetVCardRequest(VCard::ref vcard, IQRouter* router) : GenericRequest<VCard>(IQ::Set, JID(), vcard, router) { + } + }; +} diff --git a/Swiften/VCards/UnitTest/VCardManagerTest.cpp b/Swiften/VCards/UnitTest/VCardManagerTest.cpp index 56bbfa1..1f81f8e 100644 --- a/Swiften/VCards/UnitTest/VCardManagerTest.cpp +++ b/Swiften/VCards/UnitTest/VCardManagerTest.cpp @@ -10,6 +10,7 @@ #include <cppunit/extensions/TestFactoryRegistry.h> #include <vector> #include <boost/bind.hpp> +#include <boost/smart_ptr/make_shared.hpp> #include "Swiften/VCards/VCardManager.h" #include "Swiften/VCards/VCardMemoryStorage.h" @@ -28,6 +29,8 @@ class VCardManagerTest : public CppUnit::TestFixture { CPPUNIT_TEST(testRequest_VCardAlreadyRequested); CPPUNIT_TEST(testRequest_AfterPreviousRequest); CPPUNIT_TEST(testRequestOwnVCard); + CPPUNIT_TEST(testCreateSetVCardRequest); + CPPUNIT_TEST(testCreateSetVCardRequest_Error); CPPUNIT_TEST_SUITE_END(); public: @@ -82,6 +85,8 @@ class VCardManagerTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(JID("foo@bar.com/baz"), changes[0].first); CPPUNIT_ASSERT_EQUAL(String("Foo Bar"), changes[0].second->getFullName()); CPPUNIT_ASSERT_EQUAL(String("Foo Bar"), vcardStorage->getVCard(JID("foo@bar.com/baz"))->getFullName()); + + CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(ownChanges.size())); } void testRequest_Error() { @@ -125,12 +130,42 @@ class VCardManagerTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(ownJID.toBare(), changes[0].first); CPPUNIT_ASSERT_EQUAL(String("Myself"), changes[0].second->getFullName()); CPPUNIT_ASSERT_EQUAL(String("Myself"), vcardStorage->getVCard(ownJID.toBare())->getFullName()); + + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(ownChanges.size())); + CPPUNIT_ASSERT_EQUAL(String("Myself"), ownChanges[0]->getFullName()); + } + + void testCreateSetVCardRequest() { + std::auto_ptr<VCardManager> testling = createManager(); + VCard::ref vcard = boost::make_shared<VCard>(); + vcard->setFullName("New Name"); + SetVCardRequest::ref request = testling->createSetVCardRequest(vcard); + request->send(); + + stanzaChannel->onIQReceived(createSetVCardResult()); + + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changes.size())); + CPPUNIT_ASSERT_EQUAL(ownJID.toBare(), changes[0].first); + CPPUNIT_ASSERT_EQUAL(String("New Name"), changes[0].second->getFullName()); + } + + void testCreateSetVCardRequest_Error() { + std::auto_ptr<VCardManager> testling = createManager(); + VCard::ref vcard = boost::make_shared<VCard>(); + vcard->setFullName("New Name"); + SetVCardRequest::ref request = testling->createSetVCardRequest(vcard); + request->send(); + + stanzaChannel->onIQReceived(IQ::createError(JID("baz@fum.com/foo"), stanzaChannel->sentStanzas[0]->getID())); + + CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(changes.size())); } private: std::auto_ptr<VCardManager> createManager() { std::auto_ptr<VCardManager> manager(new VCardManager(ownJID, iqRouter, vcardStorage)); manager->onVCardChanged.connect(boost::bind(&VCardManagerTest::handleVCardChanged, this, _1, _2)); + manager->onOwnVCardChanged.connect(boost::bind(&VCardManagerTest::handleOwnVCardChanged, this, _1)); return manager; } @@ -138,6 +173,10 @@ class VCardManagerTest : public CppUnit::TestFixture { changes.push_back(std::pair<JID, VCard::ref>(jid, vcard)); } + void handleOwnVCardChanged(VCard::ref vcard) { + ownChanges.push_back(vcard); + } + IQ::ref createVCardResult() { VCard::ref vcard(new VCard()); vcard->setFullName("Foo Bar"); @@ -150,12 +189,18 @@ class VCardManagerTest : public CppUnit::TestFixture { return IQ::createResult(JID(), stanzaChannel->sentStanzas[0]->getID(), vcard); } + IQ::ref createSetVCardResult() { + return IQ::createResult(JID("baz@fum.com/dum"), stanzaChannel->sentStanzas[0]->getID(), VCard::ref()); + } + + private: JID ownJID; DummyStanzaChannel* stanzaChannel; IQRouter* iqRouter; VCardMemoryStorage* vcardStorage; std::vector< std::pair<JID, VCard::ref> > changes; + std::vector<VCard::ref> ownChanges; }; CPPUNIT_TEST_SUITE_REGISTRATION(VCardManagerTest); diff --git a/Swiften/VCards/VCardManager.cpp b/Swiften/VCards/VCardManager.cpp index 8d695d4..de53238 100644 --- a/Swiften/VCards/VCardManager.cpp +++ b/Swiften/VCards/VCardManager.cpp @@ -17,6 +17,9 @@ namespace Swift { VCardManager::VCardManager(const JID& ownJID, IQRouter* iqRouter, VCardStorage* vcardStorage) : ownJID(ownJID), iqRouter(iqRouter), storage(vcardStorage) { } +VCardManager::~VCardManager() { +} + VCard::ref VCardManager::getVCard(const JID& jid) const { return storage->getVCard(jid); } @@ -51,8 +54,27 @@ void VCardManager::handleVCardReceived(const JID& actualJID, VCard::ref vcard, E } requestedVCards.erase(actualJID); JID jid = actualJID.isValid() ? actualJID : ownJID.toBare(); + setVCard(jid, vcard); +} + +SetVCardRequest::ref VCardManager::createSetVCardRequest(VCard::ref vcard) { + SetVCardRequest::ref request = SetVCardRequest::create(vcard, iqRouter); + request->onResponse.connect(boost::bind(&VCardManager::handleSetVCardResponse, this, vcard, _2)); + return request; +} + +void VCardManager::handleSetVCardResponse(VCard::ref vcard, ErrorPayload::ref error) { + if (!error) { + setVCard(ownJID.toBare(), vcard); + } +} + +void VCardManager::setVCard(const JID& jid, VCard::ref vcard) { storage->setVCard(jid, vcard); onVCardChanged(jid, vcard); + if (jid.compare(ownJID, JID::WithoutResource) == 0) { + onOwnVCardChanged(vcard); + } } } diff --git a/Swiften/VCards/VCardManager.h b/Swiften/VCards/VCardManager.h index e1ed44a..5cdf82e 100644 --- a/Swiften/VCards/VCardManager.h +++ b/Swiften/VCards/VCardManager.h @@ -6,35 +6,48 @@ #pragma once -#include <boost/signals.hpp> #include <set> #include "Swiften/JID/JID.h" #include "Swiften/Elements/VCard.h" #include "Swiften/Elements/ErrorPayload.h" +#include <Swiften/VCards/SetVCardRequest.h> +#include <Swiften/Base/boost_bsignals.h> namespace Swift { class JID; class VCardStorage; class IQRouter; - class VCardManager { + class VCardManager : public boost::bsignals::trackable { public: VCardManager(const JID& ownJID, IQRouter* iqRouter, VCardStorage* vcardStorage); + ~VCardManager(); VCard::ref getVCard(const JID& jid) const; VCard::ref getVCardAndRequestWhenNeeded(const JID& jid); void requestVCard(const JID& jid); void requestOwnVCard(); + SetVCardRequest::ref createSetVCardRequest(VCard::ref); + public: /** * The JID will always be bare. */ boost::signal<void (const JID&, VCard::ref)> onVCardChanged; + /** + * Emitted when our own vcard changes. + * + * onVCardChanged will also be emitted. + */ + boost::signal<void (VCard::ref)> onOwnVCardChanged; + private: void handleVCardReceived(const JID& from, VCard::ref, ErrorPayload::ref); + void handleSetVCardResponse(VCard::ref, ErrorPayload::ref); + void setVCard(const JID& jid, VCard::ref vcard); private: JID ownJID; |