summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swiften/Avatars')
-rw-r--r--Swiften/Avatars/AvatarManager.h20
-rw-r--r--Swiften/Avatars/AvatarManagerImpl.cpp62
-rw-r--r--Swiften/Avatars/AvatarManagerImpl.h58
-rw-r--r--Swiften/Avatars/AvatarMemoryStorage.h58
-rw-r--r--Swiften/Avatars/AvatarProvider.h19
-rw-r--r--Swiften/Avatars/AvatarStorage.h27
-rw-r--r--Swiften/Avatars/CombinedAvatarProvider.cpp63
-rw-r--r--Swiften/Avatars/CombinedAvatarProvider.h30
-rw-r--r--Swiften/Avatars/DummyAvatarManager.h36
-rw-r--r--Swiften/Avatars/NullAvatarManager.h18
-rw-r--r--Swiften/Avatars/OfflineAvatarManager.cpp10
-rw-r--r--Swiften/Avatars/OfflineAvatarManager.h20
-rw-r--r--Swiften/Avatars/SConscript18
-rw-r--r--Swiften/Avatars/UnitTest/AvatarManagerImplTest.cpp238
-rw-r--r--Swiften/Avatars/UnitTest/CombinedAvatarProviderTest.cpp708
-rw-r--r--Swiften/Avatars/UnitTest/VCardAvatarManagerTest.cpp307
-rw-r--r--Swiften/Avatars/UnitTest/VCardUpdateAvatarManagerTest.cpp381
-rw-r--r--Swiften/Avatars/VCardAvatarManager.cpp68
-rw-r--r--Swiften/Avatars/VCardAvatarManager.h38
-rw-r--r--Swiften/Avatars/VCardUpdateAvatarManager.cpp122
-rw-r--r--Swiften/Avatars/VCardUpdateAvatarManager.h62
21 files changed, 1184 insertions, 1179 deletions
diff --git a/Swiften/Avatars/AvatarManager.h b/Swiften/Avatars/AvatarManager.h
index b717959..a71d323 100644
--- a/Swiften/Avatars/AvatarManager.h
+++ b/Swiften/Avatars/AvatarManager.h
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2010 Isode Limited.
+ * Copyright (c) 2010-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
@@ -7,21 +7,21 @@
#pragma once
#include <boost/filesystem/path.hpp>
+#include <boost/signals2.hpp>
#include <Swiften/Base/API.h>
-#include <Swiften/Base/boost_bsignals.h>
#include <Swiften/Base/ByteArray.h>
namespace Swift {
- class JID;
+ class JID;
- class SWIFTEN_API AvatarManager {
- public:
- virtual ~AvatarManager();
+ class SWIFTEN_API AvatarManager {
+ public:
+ virtual ~AvatarManager();
- virtual ByteArray getAvatar(const JID&) const = 0;
- virtual boost::filesystem::path getAvatarPath(const JID&) const = 0;
+ virtual ByteArray getAvatar(const JID&) const = 0;
+ virtual boost::filesystem::path getAvatarPath(const JID&) const = 0;
- boost::signal<void (const JID&)> onAvatarChanged;
- };
+ boost::signals2::signal<void (const JID&)> onAvatarChanged;
+ };
}
diff --git a/Swiften/Avatars/AvatarManagerImpl.cpp b/Swiften/Avatars/AvatarManagerImpl.cpp
index fb924c6..6e98b1b 100644
--- a/Swiften/Avatars/AvatarManagerImpl.cpp
+++ b/Swiften/Avatars/AvatarManagerImpl.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2010-2013 Isode Limited.
+ * Copyright (c) 2010-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
@@ -8,59 +8,59 @@
#include <boost/bind.hpp>
-#include <Swiften/Avatars/VCardUpdateAvatarManager.h>
-#include <Swiften/Avatars/VCardAvatarManager.h>
#include <Swiften/Avatars/AvatarStorage.h>
#include <Swiften/Avatars/OfflineAvatarManager.h>
+#include <Swiften/Avatars/VCardAvatarManager.h>
+#include <Swiften/Avatars/VCardUpdateAvatarManager.h>
#include <Swiften/Base/ByteArray.h>
namespace Swift {
AvatarManagerImpl::AvatarManagerImpl(VCardManager* vcardManager, StanzaChannel* stanzaChannel, AvatarStorage* avatarStorage, CryptoProvider* crypto, MUCRegistry* mucRegistry) : avatarStorage(avatarStorage) {
- vcardUpdateAvatarManager = new VCardUpdateAvatarManager(vcardManager, stanzaChannel, avatarStorage, crypto, mucRegistry);
- combinedAvatarProvider.addProvider(vcardUpdateAvatarManager);
+ vcardUpdateAvatarManager = new VCardUpdateAvatarManager(vcardManager, stanzaChannel, avatarStorage, crypto, mucRegistry);
+ combinedAvatarProvider.addProvider(vcardUpdateAvatarManager);
- vcardAvatarManager = new VCardAvatarManager(vcardManager, avatarStorage, crypto, mucRegistry);
- combinedAvatarProvider.addProvider(vcardAvatarManager);
+ vcardAvatarManager = new VCardAvatarManager(vcardManager, avatarStorage, crypto, mucRegistry);
+ combinedAvatarProvider.addProvider(vcardAvatarManager);
- offlineAvatarManager = new OfflineAvatarManager(avatarStorage);
- combinedAvatarProvider.addProvider(offlineAvatarManager);
+ offlineAvatarManager = new OfflineAvatarManager(avatarStorage);
+ combinedAvatarProvider.addProvider(offlineAvatarManager);
- combinedAvatarProvider.onAvatarChanged.connect(boost::bind(&AvatarManagerImpl::handleCombinedAvatarChanged, this, _1));
+ combinedAvatarProvider.onAvatarChanged.connect(boost::bind(&AvatarManagerImpl::handleCombinedAvatarChanged, this, _1));
}
AvatarManagerImpl::~AvatarManagerImpl() {
- combinedAvatarProvider.onAvatarChanged.disconnect(boost::bind(&AvatarManagerImpl::handleCombinedAvatarChanged, this, _1));
+ combinedAvatarProvider.onAvatarChanged.disconnect(boost::bind(&AvatarManagerImpl::handleCombinedAvatarChanged, this, _1));
- combinedAvatarProvider.removeProvider(offlineAvatarManager);
- delete offlineAvatarManager;
- combinedAvatarProvider.removeProvider(vcardAvatarManager);
- delete vcardAvatarManager;
- combinedAvatarProvider.removeProvider(vcardUpdateAvatarManager);
- delete vcardUpdateAvatarManager;
+ combinedAvatarProvider.removeProvider(offlineAvatarManager);
+ delete offlineAvatarManager;
+ combinedAvatarProvider.removeProvider(vcardAvatarManager);
+ delete vcardAvatarManager;
+ combinedAvatarProvider.removeProvider(vcardUpdateAvatarManager);
+ delete vcardUpdateAvatarManager;
}
boost::filesystem::path AvatarManagerImpl::getAvatarPath(const JID& jid) const {
- boost::optional<std::string> hash = combinedAvatarProvider.getAvatarHash(jid);
- if (hash && !hash->empty()) {
- return avatarStorage->getAvatarPath(*hash);
- }
- return boost::filesystem::path();
+ boost::optional<std::string> hash = combinedAvatarProvider.getAvatarHash(jid);
+ if (hash && !hash->empty()) {
+ return avatarStorage->getAvatarPath(*hash);
+ }
+ return boost::filesystem::path();
}
ByteArray AvatarManagerImpl::getAvatar(const JID& jid) const {
- boost::optional<std::string> hash = combinedAvatarProvider.getAvatarHash(jid);
- if (hash && !hash->empty()) {
- return avatarStorage->getAvatar(*hash);
- }
- return ByteArray();
+ boost::optional<std::string> hash = combinedAvatarProvider.getAvatarHash(jid);
+ if (hash && !hash->empty()) {
+ return avatarStorage->getAvatar(*hash);
+ }
+ return ByteArray();
}
void AvatarManagerImpl::handleCombinedAvatarChanged(const JID& jid) {
- boost::optional<std::string> hash = combinedAvatarProvider.getAvatarHash(jid);
- assert(hash);
- offlineAvatarManager->setAvatar(jid, *hash);
- onAvatarChanged(jid);
+ boost::optional<std::string> hash = combinedAvatarProvider.getAvatarHash(jid);
+ assert(hash);
+ offlineAvatarManager->setAvatar(jid, *hash);
+ onAvatarChanged(jid);
}
}
diff --git a/Swiften/Avatars/AvatarManagerImpl.h b/Swiften/Avatars/AvatarManagerImpl.h
index 69af924..d58e09f 100644
--- a/Swiften/Avatars/AvatarManagerImpl.h
+++ b/Swiften/Avatars/AvatarManagerImpl.h
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2010 Isode Limited.
+ * Copyright (c) 2010-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
@@ -10,32 +10,32 @@
#include <Swiften/Avatars/CombinedAvatarProvider.h>
namespace Swift {
- class MUCRegistry;
- class AvatarStorage;
- class StanzaChannel;
- class VCardManager;
- class VCardUpdateAvatarManager;
- class VCardAvatarManager;
- class OfflineAvatarManager;
- class CryptoProvider;
-
- class AvatarManagerImpl : public AvatarManager {
- public:
- AvatarManagerImpl(VCardManager*, StanzaChannel*, AvatarStorage*, CryptoProvider* crypto, MUCRegistry* = NULL);
- virtual ~AvatarManagerImpl();
-
- virtual boost::filesystem::path getAvatarPath(const JID&) const;
- virtual ByteArray getAvatar(const JID&) const;
-
- private:
- void handleCombinedAvatarChanged(const JID& jid);
-
-
- private:
- CombinedAvatarProvider combinedAvatarProvider;
- AvatarStorage* avatarStorage;
- VCardUpdateAvatarManager* vcardUpdateAvatarManager;
- VCardAvatarManager* vcardAvatarManager;
- OfflineAvatarManager* offlineAvatarManager;
- };
+ class MUCRegistry;
+ class AvatarStorage;
+ class StanzaChannel;
+ class VCardManager;
+ class VCardUpdateAvatarManager;
+ class VCardAvatarManager;
+ class OfflineAvatarManager;
+ class CryptoProvider;
+
+ class AvatarManagerImpl : public AvatarManager {
+ public:
+ AvatarManagerImpl(VCardManager*, StanzaChannel*, AvatarStorage*, CryptoProvider* crypto, MUCRegistry* = nullptr);
+ virtual ~AvatarManagerImpl();
+
+ virtual boost::filesystem::path getAvatarPath(const JID&) const;
+ virtual ByteArray getAvatar(const JID&) const;
+
+ private:
+ void handleCombinedAvatarChanged(const JID& jid);
+
+
+ private:
+ CombinedAvatarProvider combinedAvatarProvider;
+ AvatarStorage* avatarStorage;
+ VCardUpdateAvatarManager* vcardUpdateAvatarManager;
+ VCardAvatarManager* vcardAvatarManager;
+ OfflineAvatarManager* offlineAvatarManager;
+ };
}
diff --git a/Swiften/Avatars/AvatarMemoryStorage.h b/Swiften/Avatars/AvatarMemoryStorage.h
index bd67e74..bc40a26 100644
--- a/Swiften/Avatars/AvatarMemoryStorage.h
+++ b/Swiften/Avatars/AvatarMemoryStorage.h
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2010-2015 Isode Limited.
+ * Copyright (c) 2010-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
@@ -9,36 +9,36 @@
#include <map>
#include <string>
-#include <Swiften/Base/ByteArray.h>
#include <Swiften/Avatars/AvatarStorage.h>
-#include <Swiften/JID/JID.h>
#include <Swiften/Base/API.h>
+#include <Swiften/Base/ByteArray.h>
+#include <Swiften/JID/JID.h>
namespace Swift {
- class SWIFTEN_API AvatarMemoryStorage : public AvatarStorage {
- public:
- virtual bool hasAvatar(const std::string& hash) const { return avatars.find(hash) != avatars.end(); }
- virtual void addAvatar(const std::string& hash, const ByteArray& avatar) { avatars[hash] = avatar; }
- virtual ByteArray getAvatar(const std::string& hash) const {
- std::map<std::string, ByteArray>::const_iterator i = avatars.find(hash);
- return i == avatars.end() ? ByteArray() : i->second;
- }
-
- virtual boost::filesystem::path getAvatarPath(const std::string& hash) const {
- return boost::filesystem::path("/avatars") / hash;
- }
-
- virtual void setAvatarForJID(const JID& jid, const std::string& hash) {
- jidAvatars[jid] = hash;
- }
-
- virtual std::string getAvatarForJID(const JID& jid) const {
- std::map<JID, std::string>::const_iterator i = jidAvatars.find(jid);
- return i == jidAvatars.end() ? "" : i->second;
- }
-
- private:
- std::map<std::string, ByteArray> avatars;
- std::map<JID, std::string> jidAvatars;
- };
+ class SWIFTEN_API AvatarMemoryStorage : public AvatarStorage {
+ public:
+ virtual bool hasAvatar(const std::string& hash) const { return avatars.find(hash) != avatars.end(); }
+ virtual void addAvatar(const std::string& hash, const ByteArray& avatar) { avatars[hash] = avatar; }
+ virtual ByteArray getAvatar(const std::string& hash) const {
+ std::map<std::string, ByteArray>::const_iterator i = avatars.find(hash);
+ return i == avatars.end() ? ByteArray() : i->second;
+ }
+
+ virtual boost::filesystem::path getAvatarPath(const std::string& hash) const {
+ return boost::filesystem::path("/avatars") / hash;
+ }
+
+ virtual void setAvatarForJID(const JID& jid, const std::string& hash) {
+ jidAvatars[jid] = hash;
+ }
+
+ virtual std::string getAvatarForJID(const JID& jid) const {
+ std::map<JID, std::string>::const_iterator i = jidAvatars.find(jid);
+ return i == jidAvatars.end() ? "" : i->second;
+ }
+
+ private:
+ std::map<std::string, ByteArray> avatars;
+ std::map<JID, std::string> jidAvatars;
+ };
}
diff --git a/Swiften/Avatars/AvatarProvider.h b/Swiften/Avatars/AvatarProvider.h
index 8289ecd..a92418f 100644
--- a/Swiften/Avatars/AvatarProvider.h
+++ b/Swiften/Avatars/AvatarProvider.h
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2010 Isode Limited.
+ * Copyright (c) 2010-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
@@ -8,18 +8,19 @@
#include <string>
+#include <boost/signals2.hpp>
+
#include <Swiften/Base/API.h>
-#include <Swiften/Base/boost_bsignals.h>
namespace Swift {
- class JID;
+ class JID;
- class SWIFTEN_API AvatarProvider {
- public:
- virtual ~AvatarProvider();
+ class SWIFTEN_API AvatarProvider {
+ public:
+ virtual ~AvatarProvider();
- virtual boost::optional<std::string> getAvatarHash(const JID&) const = 0;
+ virtual boost::optional<std::string> getAvatarHash(const JID&) const = 0;
- boost::signal<void (const JID&)> onAvatarChanged;
- };
+ boost::signals2::signal<void (const JID&)> onAvatarChanged;
+ };
}
diff --git a/Swiften/Avatars/AvatarStorage.h b/Swiften/Avatars/AvatarStorage.h
index 01151c2..dc3eb34 100644
--- a/Swiften/Avatars/AvatarStorage.h
+++ b/Swiften/Avatars/AvatarStorage.h
@@ -1,31 +1,32 @@
/*
- * Copyright (c) 2010 Isode Limited.
+ * Copyright (c) 2010-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
#pragma once
-#include <boost/filesystem/path.hpp>
#include <string>
+#include <boost/filesystem/path.hpp>
+
#include <Swiften/Base/API.h>
#include <Swiften/Base/ByteArray.h>
namespace Swift {
- class JID;
+ class JID;
- class SWIFTEN_API AvatarStorage {
- public:
- virtual ~AvatarStorage();
+ class SWIFTEN_API AvatarStorage {
+ public:
+ virtual ~AvatarStorage();
- virtual bool hasAvatar(const std::string& hash) const = 0;
- virtual void addAvatar(const std::string& hash, const ByteArray& avatar) = 0;
- virtual ByteArray getAvatar(const std::string& hash) const = 0;
- virtual boost::filesystem::path getAvatarPath(const std::string& hash) const = 0;
+ virtual bool hasAvatar(const std::string& hash) const = 0;
+ virtual void addAvatar(const std::string& hash, const ByteArray& avatar) = 0;
+ virtual ByteArray getAvatar(const std::string& hash) const = 0;
+ virtual boost::filesystem::path getAvatarPath(const std::string& hash) const = 0;
- virtual void setAvatarForJID(const JID& jid, const std::string& hash) = 0;
- virtual std::string getAvatarForJID(const JID& jid) const = 0;
- };
+ virtual void setAvatarForJID(const JID& jid, const std::string& hash) = 0;
+ virtual std::string getAvatarForJID(const JID& jid) const = 0;
+ };
}
diff --git a/Swiften/Avatars/CombinedAvatarProvider.cpp b/Swiften/Avatars/CombinedAvatarProvider.cpp
index 4fa4c2b..465512f 100644
--- a/Swiften/Avatars/CombinedAvatarProvider.cpp
+++ b/Swiften/Avatars/CombinedAvatarProvider.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2010 Isode Limited.
+ * Copyright (c) 2010-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
@@ -7,6 +7,7 @@
#include <Swiften/Avatars/CombinedAvatarProvider.h>
#include <algorithm>
+
#include <boost/bind.hpp>
#include <Swiften/Base/Log.h>
@@ -14,48 +15,48 @@
namespace Swift {
boost::optional<std::string> CombinedAvatarProvider::getAvatarHash(const JID& jid) const {
- return getCombinedAvatarAndCache(jid);
+ return getCombinedAvatarAndCache(jid);
}
void CombinedAvatarProvider::addProvider(AvatarProvider* provider) {
- provider->onAvatarChanged.connect(boost::bind(&CombinedAvatarProvider::handleAvatarChanged, this, _1));
- providers.push_back(provider);
+ provider->onAvatarChanged.connect(boost::bind(&CombinedAvatarProvider::handleAvatarChanged, this, _1));
+ providers.push_back(provider);
}
void CombinedAvatarProvider::removeProvider(AvatarProvider* provider) {
- std::vector<AvatarProvider*>::iterator i = std::remove(providers.begin(), providers.end(), provider);
- for(std::vector<AvatarProvider*>::iterator j = i; j < providers.end(); ++j) {
- provider->onAvatarChanged.disconnect(boost::bind(&CombinedAvatarProvider::handleAvatarChanged, this, _1));
- }
- providers.erase(i, providers.end());
+ std::vector<AvatarProvider*>::iterator i = std::remove(providers.begin(), providers.end(), provider);
+ for(std::vector<AvatarProvider*>::iterator j = i; j < providers.end(); ++j) {
+ provider->onAvatarChanged.disconnect(boost::bind(&CombinedAvatarProvider::handleAvatarChanged, this, _1));
+ }
+ providers.erase(i, providers.end());
}
void CombinedAvatarProvider::handleAvatarChanged(const JID& jid) {
- std::string oldHash;
- std::map<JID, std::string>::const_iterator i = avatars.find(jid);
- if (i != avatars.end()) {
- oldHash = i->second;
- }
- boost::optional<std::string> newHash = getCombinedAvatarAndCache(jid);
- if (newHash != oldHash) {
- SWIFT_LOG(debug) << "Avatar changed: " << jid << ": " << oldHash << " -> " << (newHash ? newHash.get() : "NULL") << std::endl;
- onAvatarChanged(jid);
- }
+ std::string oldHash;
+ std::map<JID, std::string>::const_iterator i = avatars.find(jid);
+ if (i != avatars.end()) {
+ oldHash = i->second;
+ }
+ boost::optional<std::string> newHash = getCombinedAvatarAndCache(jid);
+ if (newHash != oldHash) {
+ SWIFT_LOG(debug) << "Avatar changed: " << jid << ": " << oldHash << " -> " << (newHash ? newHash.get() : "NULL") << std::endl;
+ onAvatarChanged(jid);
+ }
}
boost::optional<std::string> CombinedAvatarProvider::getCombinedAvatarAndCache(const JID& jid) const {
- SWIFT_LOG(debug) << "JID: " << jid << std::endl;
- boost::optional<std::string> hash;
- for (size_t i = 0; i < providers.size() && !hash; ++i) {
- hash = providers[i]->getAvatarHash(jid);
- SWIFT_LOG(debug) << "Provider " << providers[i] << ": " << (hash ? hash.get() : "NULL") << std::endl;
- }
- if (hash) {
- avatars[jid] = *hash;
- } else {
- avatars[jid] = "";
- }
- return hash;
+ SWIFT_LOG(debug) << "JID: " << jid << std::endl;
+ boost::optional<std::string> hash;
+ for (size_t i = 0; i < providers.size() && !hash; ++i) {
+ hash = providers[i]->getAvatarHash(jid);
+ SWIFT_LOG(debug) << "Provider " << providers[i] << ": " << (hash ? hash.get() : "NULL") << std::endl;
+ }
+ if (hash) {
+ avatars[jid] = *hash;
+ } else {
+ avatars[jid] = "";
+ }
+ return hash;
}
}
diff --git a/Swiften/Avatars/CombinedAvatarProvider.h b/Swiften/Avatars/CombinedAvatarProvider.h
index d8c045c..f667ad2 100644
--- a/Swiften/Avatars/CombinedAvatarProvider.h
+++ b/Swiften/Avatars/CombinedAvatarProvider.h
@@ -1,32 +1,32 @@
/*
- * Copyright (c) 2010 Isode Limited.
+ * Copyright (c) 2010-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
#pragma once
-#include <vector>
#include <map>
+#include <vector>
-#include <Swiften/Base/API.h>
#include <Swiften/Avatars/AvatarProvider.h>
+#include <Swiften/Base/API.h>
#include <Swiften/JID/JID.h>
namespace Swift {
- class SWIFTEN_API CombinedAvatarProvider : public AvatarProvider {
- public:
- virtual boost::optional<std::string> getAvatarHash(const JID&) const;
+ class SWIFTEN_API CombinedAvatarProvider : public AvatarProvider {
+ public:
+ virtual boost::optional<std::string> getAvatarHash(const JID&) const;
- void addProvider(AvatarProvider*);
- void removeProvider(AvatarProvider*);
+ void addProvider(AvatarProvider*);
+ void removeProvider(AvatarProvider*);
- private:
- void handleAvatarChanged(const JID&);
- boost::optional<std::string> getCombinedAvatarAndCache(const JID&) const;
+ private:
+ void handleAvatarChanged(const JID&);
+ boost::optional<std::string> getCombinedAvatarAndCache(const JID&) const;
- private:
- std::vector<AvatarProvider*> providers;
- mutable std::map<JID, std::string> avatars;
- };
+ private:
+ std::vector<AvatarProvider*> providers;
+ mutable std::map<JID, std::string> avatars;
+ };
}
diff --git a/Swiften/Avatars/DummyAvatarManager.h b/Swiften/Avatars/DummyAvatarManager.h
index f6f075f..f079be1 100644
--- a/Swiften/Avatars/DummyAvatarManager.h
+++ b/Swiften/Avatars/DummyAvatarManager.h
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2010 Isode Limited.
+ * Copyright (c) 2010-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
@@ -8,26 +8,26 @@
#include <map>
-#include <Swiften/JID/JID.h>
#include <Swiften/Avatars/AvatarManager.h>
+#include <Swiften/JID/JID.h>
namespace Swift {
- class DummyAvatarManager : public AvatarManager {
- public:
- virtual boost::filesystem::path getAvatarPath(const JID& j) const {
- return boost::filesystem::path("/avatars") / j.toString();
- }
+ class DummyAvatarManager : public AvatarManager {
+ public:
+ virtual boost::filesystem::path getAvatarPath(const JID& j) const {
+ return boost::filesystem::path("/avatars") / j.toString();
+ }
- 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();
- }
- }
+ 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;
- };
+ std::map<JID, ByteArray> avatars;
+ };
}
diff --git a/Swiften/Avatars/NullAvatarManager.h b/Swiften/Avatars/NullAvatarManager.h
index e9f3028..adc1acc 100644
--- a/Swiften/Avatars/NullAvatarManager.h
+++ b/Swiften/Avatars/NullAvatarManager.h
@@ -9,14 +9,14 @@
#include <Swiften/Avatars/AvatarManager.h>
namespace Swift {
- class NullAvatarManager : public AvatarManager {
- public:
- virtual boost::filesystem::path getAvatarPath(const JID&) const {
- return boost::filesystem::path();
- }
+ class NullAvatarManager : public AvatarManager {
+ public:
+ virtual boost::filesystem::path getAvatarPath(const JID&) const {
+ return boost::filesystem::path();
+ }
- virtual ByteArray getAvatar(const JID&) const {
- return ByteArray();
- }
- };
+ virtual ByteArray getAvatar(const JID&) const {
+ return ByteArray();
+ }
+ };
}
diff --git a/Swiften/Avatars/OfflineAvatarManager.cpp b/Swiften/Avatars/OfflineAvatarManager.cpp
index d462f08..e2468e6 100644
--- a/Swiften/Avatars/OfflineAvatarManager.cpp
+++ b/Swiften/Avatars/OfflineAvatarManager.cpp
@@ -19,14 +19,14 @@ OfflineAvatarManager::~OfflineAvatarManager() {
}
boost::optional<std::string> OfflineAvatarManager::getAvatarHash(const JID& jid) const {
- return avatarStorage->getAvatarForJID(jid);
+ return avatarStorage->getAvatarForJID(jid);
}
void OfflineAvatarManager::setAvatar(const JID& jid, const std::string& hash) {
- if (getAvatarHash(jid) != hash) {
- avatarStorage->setAvatarForJID(jid, hash);
- onAvatarChanged(jid);
- }
+ if (getAvatarHash(jid) != hash) {
+ avatarStorage->setAvatarForJID(jid, hash);
+ onAvatarChanged(jid);
+ }
}
}
diff --git a/Swiften/Avatars/OfflineAvatarManager.h b/Swiften/Avatars/OfflineAvatarManager.h
index 22345ae..11b1d5f 100644
--- a/Swiften/Avatars/OfflineAvatarManager.h
+++ b/Swiften/Avatars/OfflineAvatarManager.h
@@ -10,17 +10,17 @@
#include <Swiften/Base/API.h>
namespace Swift {
- class AvatarStorage;
+ class AvatarStorage;
- class SWIFTEN_API OfflineAvatarManager : public AvatarProvider {
- public:
- OfflineAvatarManager(AvatarStorage*);
- ~OfflineAvatarManager();
+ class SWIFTEN_API OfflineAvatarManager : public AvatarProvider {
+ public:
+ OfflineAvatarManager(AvatarStorage*);
+ ~OfflineAvatarManager();
- virtual boost::optional<std::string> getAvatarHash(const JID&) const;
- void setAvatar(const JID&, const std::string& hash);
+ virtual boost::optional<std::string> getAvatarHash(const JID&) const;
+ void setAvatar(const JID&, const std::string& hash);
- private:
- AvatarStorage* avatarStorage;
- };
+ private:
+ AvatarStorage* avatarStorage;
+ };
}
diff --git a/Swiften/Avatars/SConscript b/Swiften/Avatars/SConscript
index 9c219a4..0cd419d 100644
--- a/Swiften/Avatars/SConscript
+++ b/Swiften/Avatars/SConscript
@@ -1,13 +1,13 @@
Import("swiften_env")
objects = swiften_env.SwiftenObject([
- "VCardUpdateAvatarManager.cpp",
- "VCardAvatarManager.cpp",
- "OfflineAvatarManager.cpp",
- "AvatarManager.cpp",
- "AvatarManagerImpl.cpp",
- "AvatarStorage.cpp",
- "AvatarProvider.cpp",
- "CombinedAvatarProvider.cpp",
- ])
+ "VCardUpdateAvatarManager.cpp",
+ "VCardAvatarManager.cpp",
+ "OfflineAvatarManager.cpp",
+ "AvatarManager.cpp",
+ "AvatarManagerImpl.cpp",
+ "AvatarStorage.cpp",
+ "AvatarProvider.cpp",
+ "CombinedAvatarProvider.cpp",
+ ])
swiften_env.Append(SWIFTEN_OBJECTS = [objects])
diff --git a/Swiften/Avatars/UnitTest/AvatarManagerImplTest.cpp b/Swiften/Avatars/UnitTest/AvatarManagerImplTest.cpp
index 146676a..241f375 100644
--- a/Swiften/Avatars/UnitTest/AvatarManagerImplTest.cpp
+++ b/Swiften/Avatars/UnitTest/AvatarManagerImplTest.cpp
@@ -1,141 +1,143 @@
/*
- * Copyright (c) 2014 Isode Limited.
+ * Copyright (c) 2014-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
+#include <string>
+
+#include <boost/bind.hpp>
+
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
-#include <boost/bind.hpp>
-#include <Swiften/JID/JID.h>
-#include <string>
#include <Swiften/Avatars/AvatarManagerImpl.h>
+#include <Swiften/Avatars/AvatarMemoryStorage.h>
#include <Swiften/Avatars/CombinedAvatarProvider.h>
-#include <Swiften/Avatars/VCardAvatarManager.h>
#include <Swiften/Avatars/OfflineAvatarManager.h>
-#include <Swiften/Elements/VCardUpdate.h>
-#include <Swiften/MUC/MUCRegistry.h>
+#include <Swiften/Avatars/VCardAvatarManager.h>
+#include <Swiften/Avatars/VCardUpdateAvatarManager.h>
#include <Swiften/Client/DummyStanzaChannel.h>
#include <Swiften/Crypto/CryptoProvider.h>
#include <Swiften/Crypto/PlatformCryptoProvider.h>
+#include <Swiften/Elements/VCardUpdate.h>
+#include <Swiften/JID/JID.h>
+#include <Swiften/MUC/MUCRegistry.h>
#include <Swiften/Queries/IQRouter.h>
-#include <Swiften/Avatars/AvatarMemoryStorage.h>
-#include <Swiften/VCards/VCardMemoryStorage.h>
-#include <Swiften/VCards/VCardManager.h>
-#include <Swiften/Avatars/VCardUpdateAvatarManager.h>
#include <Swiften/StringCodecs/Hexify.h>
+#include <Swiften/VCards/VCardManager.h>
+#include <Swiften/VCards/VCardMemoryStorage.h>
using namespace Swift;
class AvatarManagerImplTest : public CppUnit::TestFixture {
- CPPUNIT_TEST_SUITE(AvatarManagerImplTest);
- CPPUNIT_TEST(testGetSetAvatar);
- CPPUNIT_TEST_SUITE_END();
-
- public:
- void setUp() {
- ownerJID = JID("owner@domain.com/theowner");
- stanzaChannel = boost::make_shared<DummyStanzaChannel>();
- iqRouter = boost::make_shared<IQRouter>(stanzaChannel.get());
- crypto = boost::shared_ptr<CryptoProvider>(PlatformCryptoProvider::create());
- vcardStorage = boost::make_shared<VCardMemoryStorage>(crypto.get());
- vcardManager = boost::make_shared<VCardManager>(ownerJID, iqRouter.get(), vcardStorage.get());
- avatarStorage = boost::make_shared<AvatarMemoryStorage>();
- mucRegistry = boost::make_shared<DummyMUCRegistry>();
- avatarManager = boost::make_shared<AvatarManagerImpl>(vcardManager.get(), stanzaChannel.get(), avatarStorage.get(), crypto.get(), mucRegistry.get());
- }
-
- void testGetSetAvatar() {
- /* initially we have no knowledge of the user or their avatar */
- JID personJID("person@domain.com/theperson");
- ByteArray avatar = avatarManager->getAvatar(personJID.toBare());
- CPPUNIT_ASSERT(!avatar.size());
-
- /* notify the 'owner' JID that our avatar has changed */
-
- ByteArray fullAvatar = createByteArray("abcdefg");
- boost::shared_ptr<VCardUpdate> vcardUpdate = boost::make_shared<VCardUpdate>();
- vcardUpdate->setPhotoHash(Hexify::hexify(crypto->getSHA1Hash(fullAvatar)));
- boost::shared_ptr<Presence> presence = boost::make_shared<Presence>();
- presence->setTo(ownerJID);
- presence->setFrom(personJID);
- presence->setType(Presence::Available);
- presence->addPayload(vcardUpdate);
- stanzaChannel->onPresenceReceived(presence);
-
- /* reply to the avatar request with our new avatar */
-
- CPPUNIT_ASSERT_EQUAL(size_t(1), stanzaChannel->sentStanzas.size());
- boost::shared_ptr<IQ> request = boost::dynamic_pointer_cast<IQ>(stanzaChannel->sentStanzas[0]);
- stanzaChannel->sentStanzas.pop_back();
- CPPUNIT_ASSERT(!!request);
- boost::shared_ptr<VCard> vcard = request->getPayload<VCard>();
- CPPUNIT_ASSERT(!!vcard);
-
- boost::shared_ptr<IQ> reply = boost::make_shared<IQ>(IQ::Result);
- reply->setTo(request->getFrom());
- reply->setFrom(request->getTo());
- reply->setID(request->getID());
- vcard->setPhoto(fullAvatar);
- reply->addPayload(vcard);
- stanzaChannel->onIQReceived(reply);
-
- /* check hash through avatarManager that it received the correct photo */
-
- ByteArray reportedAvatar = avatarManager->getAvatar(personJID.toBare());
- CPPUNIT_ASSERT_EQUAL(byteArrayToString(fullAvatar), byteArrayToString(reportedAvatar));
-
- /* send new presence to notify of blank avatar */
-
- vcardUpdate = boost::make_shared<VCardUpdate>();
- presence = boost::make_shared<Presence>();
- presence->setTo(ownerJID);
- presence->setFrom(personJID);
- presence->setType(Presence::Available);
- presence->addPayload(vcardUpdate);
- stanzaChannel->onPresenceReceived(presence);
-
- /* reply to the avatar request with our EMPTY avatar */
-
- CPPUNIT_ASSERT_EQUAL(size_t(1), stanzaChannel->sentStanzas.size());
- request = boost::dynamic_pointer_cast<IQ>(stanzaChannel->sentStanzas[0]);
- stanzaChannel->sentStanzas.pop_back();
- CPPUNIT_ASSERT(!!request);
- vcard = request->getPayload<VCard>();
- CPPUNIT_ASSERT(!!vcard);
-
- ByteArray blankAvatar = createByteArray("");
- reply = boost::make_shared<IQ>(IQ::Result);
- reply->setTo(request->getFrom());
- reply->setFrom(request->getTo());
- reply->setID(request->getID());
- vcard->setPhoto(blankAvatar);
- reply->addPayload(vcard);
- stanzaChannel->onIQReceived(reply);
-
- /* check hash through avatarManager that it received the correct photo */
-
- reportedAvatar = avatarManager->getAvatar(personJID.toBare());
- CPPUNIT_ASSERT_EQUAL(byteArrayToString(blankAvatar), byteArrayToString(reportedAvatar));
- }
-
- struct DummyMUCRegistry : public MUCRegistry {
- bool isMUC(const JID& jid) const { return std::find(mucs_.begin(), mucs_.end(), jid) != mucs_.end(); }
- std::vector<JID> mucs_;
- };
-
- private:
-
- JID ownerJID;
- boost::shared_ptr<DummyStanzaChannel> stanzaChannel;
- boost::shared_ptr<IQRouter> iqRouter;
- boost::shared_ptr<CryptoProvider> crypto;
- boost::shared_ptr<VCardMemoryStorage> vcardStorage;
- boost::shared_ptr<VCardManager> vcardManager;
- boost::shared_ptr<AvatarMemoryStorage> avatarStorage;
- boost::shared_ptr<DummyMUCRegistry> mucRegistry;
- boost::shared_ptr<AvatarManagerImpl> avatarManager;
+ CPPUNIT_TEST_SUITE(AvatarManagerImplTest);
+ CPPUNIT_TEST(testGetSetAvatar);
+ CPPUNIT_TEST_SUITE_END();
+
+ public:
+ void setUp() {
+ ownerJID = JID("owner@domain.com/theowner");
+ stanzaChannel = std::make_shared<DummyStanzaChannel>();
+ iqRouter = std::make_shared<IQRouter>(stanzaChannel.get());
+ crypto = std::shared_ptr<CryptoProvider>(PlatformCryptoProvider::create());
+ vcardStorage = std::make_shared<VCardMemoryStorage>(crypto.get());
+ vcardManager = std::make_shared<VCardManager>(ownerJID, iqRouter.get(), vcardStorage.get());
+ avatarStorage = std::make_shared<AvatarMemoryStorage>();
+ mucRegistry = std::make_shared<DummyMUCRegistry>();
+ avatarManager = std::make_shared<AvatarManagerImpl>(vcardManager.get(), stanzaChannel.get(), avatarStorage.get(), crypto.get(), mucRegistry.get());
+ }
+
+ void testGetSetAvatar() {
+ /* initially we have no knowledge of the user or their avatar */
+ JID personJID("person@domain.com/theperson");
+ ByteArray avatar = avatarManager->getAvatar(personJID.toBare());
+ CPPUNIT_ASSERT(!avatar.size());
+
+ /* notify the 'owner' JID that our avatar has changed */
+
+ ByteArray fullAvatar = createByteArray("abcdefg");
+ std::shared_ptr<VCardUpdate> vcardUpdate = std::make_shared<VCardUpdate>();
+ vcardUpdate->setPhotoHash(Hexify::hexify(crypto->getSHA1Hash(fullAvatar)));
+ std::shared_ptr<Presence> presence = std::make_shared<Presence>();
+ presence->setTo(ownerJID);
+ presence->setFrom(personJID);
+ presence->setType(Presence::Available);
+ presence->addPayload(vcardUpdate);
+ stanzaChannel->onPresenceReceived(presence);
+
+ /* reply to the avatar request with our new avatar */
+
+ CPPUNIT_ASSERT_EQUAL(size_t(1), stanzaChannel->sentStanzas.size());
+ std::shared_ptr<IQ> request = std::dynamic_pointer_cast<IQ>(stanzaChannel->sentStanzas[0]);
+ stanzaChannel->sentStanzas.pop_back();
+ CPPUNIT_ASSERT(!!request);
+ std::shared_ptr<VCard> vcard = request->getPayload<VCard>();
+ CPPUNIT_ASSERT(!!vcard);
+
+ std::shared_ptr<IQ> reply = std::make_shared<IQ>(IQ::Result);
+ reply->setTo(request->getFrom());
+ reply->setFrom(request->getTo());
+ reply->setID(request->getID());
+ vcard->setPhoto(fullAvatar);
+ reply->addPayload(vcard);
+ stanzaChannel->onIQReceived(reply);
+
+ /* check hash through avatarManager that it received the correct photo */
+
+ ByteArray reportedAvatar = avatarManager->getAvatar(personJID.toBare());
+ CPPUNIT_ASSERT_EQUAL(byteArrayToString(fullAvatar), byteArrayToString(reportedAvatar));
+
+ /* send new presence to notify of blank avatar */
+
+ vcardUpdate = std::make_shared<VCardUpdate>();
+ presence = std::make_shared<Presence>();
+ presence->setTo(ownerJID);
+ presence->setFrom(personJID);
+ presence->setType(Presence::Available);
+ presence->addPayload(vcardUpdate);
+ stanzaChannel->onPresenceReceived(presence);
+
+ /* reply to the avatar request with our EMPTY avatar */
+
+ CPPUNIT_ASSERT_EQUAL(size_t(1), stanzaChannel->sentStanzas.size());
+ request = std::dynamic_pointer_cast<IQ>(stanzaChannel->sentStanzas[0]);
+ stanzaChannel->sentStanzas.pop_back();
+ CPPUNIT_ASSERT(!!request);
+ vcard = request->getPayload<VCard>();
+ CPPUNIT_ASSERT(!!vcard);
+
+ ByteArray blankAvatar = createByteArray("");
+ reply = std::make_shared<IQ>(IQ::Result);
+ reply->setTo(request->getFrom());
+ reply->setFrom(request->getTo());
+ reply->setID(request->getID());
+ vcard->setPhoto(blankAvatar);
+ reply->addPayload(vcard);
+ stanzaChannel->onIQReceived(reply);
+
+ /* check hash through avatarManager that it received the correct photo */
+
+ reportedAvatar = avatarManager->getAvatar(personJID.toBare());
+ CPPUNIT_ASSERT_EQUAL(byteArrayToString(blankAvatar), byteArrayToString(reportedAvatar));
+ }
+
+ struct DummyMUCRegistry : public MUCRegistry {
+ bool isMUC(const JID& jid) const { return std::find(mucs_.begin(), mucs_.end(), jid) != mucs_.end(); }
+ std::vector<JID> mucs_;
+ };
+
+ private:
+
+ JID ownerJID;
+ std::shared_ptr<DummyStanzaChannel> stanzaChannel;
+ std::shared_ptr<IQRouter> iqRouter;
+ std::shared_ptr<CryptoProvider> crypto;
+ std::shared_ptr<VCardMemoryStorage> vcardStorage;
+ std::shared_ptr<VCardManager> vcardManager;
+ std::shared_ptr<AvatarMemoryStorage> avatarStorage;
+ std::shared_ptr<DummyMUCRegistry> mucRegistry;
+ std::shared_ptr<AvatarManagerImpl> avatarManager;
};
diff --git a/Swiften/Avatars/UnitTest/CombinedAvatarProviderTest.cpp b/Swiften/Avatars/UnitTest/CombinedAvatarProviderTest.cpp
index 58bf6fd..8aca98e 100644
--- a/Swiften/Avatars/UnitTest/CombinedAvatarProviderTest.cpp
+++ b/Swiften/Avatars/UnitTest/CombinedAvatarProviderTest.cpp
@@ -1,377 +1,375 @@
/*
- * Copyright (c) 2010 Isode Limited.
+ * Copyright (c) 2010-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
+#include <memory>
+#include <string>
+
+#include <boost/bind.hpp>
+
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
-#include <boost/bind.hpp>
-#include <Swiften/JID/JID.h>
-#include <string>
+#include <Swiften/Avatars/AvatarMemoryStorage.h>
#include <Swiften/Avatars/CombinedAvatarProvider.h>
-#include <Swiften/Avatars/VCardAvatarManager.h>
#include <Swiften/Avatars/OfflineAvatarManager.h>
-#include <Swiften/MUC/MUCRegistry.h>
+#include <Swiften/Avatars/VCardAvatarManager.h>
+#include <Swiften/Avatars/VCardUpdateAvatarManager.h>
#include <Swiften/Client/DummyStanzaChannel.h>
#include <Swiften/Crypto/CryptoProvider.h>
#include <Swiften/Crypto/PlatformCryptoProvider.h>
+#include <Swiften/JID/JID.h>
+#include <Swiften/MUC/MUCRegistry.h>
#include <Swiften/Queries/IQRouter.h>
-#include <Swiften/Avatars/AvatarMemoryStorage.h>
-#include <Swiften/VCards/VCardMemoryStorage.h>
-#include <Swiften/VCards/VCardManager.h>
-#include <Swiften/Avatars/VCardUpdateAvatarManager.h>
#include <Swiften/StringCodecs/Hexify.h>
+#include <Swiften/VCards/VCardManager.h>
+#include <Swiften/VCards/VCardMemoryStorage.h>
using namespace Swift;
class CombinedAvatarProviderTest : public CppUnit::TestFixture {
- CPPUNIT_TEST_SUITE(CombinedAvatarProviderTest);
- CPPUNIT_TEST(testGetAvatarWithNoAvatarProviderReturnsEmpty);
- CPPUNIT_TEST(testGetAvatarWithSingleAvatarProvider);
- CPPUNIT_TEST(testGetAvatarWithMultipleAvatarProviderReturnsFirstAvatar);
- CPPUNIT_TEST(testGetAvatarWithMultipleAvatarProviderAndFailingFirstProviderReturnsSecondAvatar);
- CPPUNIT_TEST(testProviderUpdateTriggersChange);
- CPPUNIT_TEST(testProviderUpdateWithoutChangeDoesNotTriggerChange);
- CPPUNIT_TEST(testProviderSecondUpdateTriggersChange);
- CPPUNIT_TEST(testProviderUpdateWithAvatarDisappearingTriggersChange);
- CPPUNIT_TEST(testProviderUpdateAfterAvatarDisappearedTriggersChange);
- CPPUNIT_TEST(testProviderUpdateAfterGetDoesNotTriggerChange);
- CPPUNIT_TEST(testProviderUpdateBareJIDAfterGetFullJID);
- CPPUNIT_TEST(testRemoveProviderDisconnectsUpdates);
- CPPUNIT_TEST(testAddRemoveFallthrough);
- CPPUNIT_TEST_SUITE_END();
-
- public:
- void setUp() {
- avatarProvider1 = new DummyAvatarProvider();
- avatarProvider2 = new DummyAvatarProvider();
- user1 = JID("user1@bar.com/bla");
- user2 = JID("user2@foo.com/baz");
- avatarHash1 = "ABCDEFG";
- avatarHash2 = "XYZU";
- avatarHash3 = "IDGH";
- }
-
- void tearDown() {
- delete avatarProvider1;
- delete avatarProvider2;
- }
-
- void testGetAvatarWithNoAvatarProviderReturnsEmpty() {
- boost::shared_ptr<CombinedAvatarProvider> testling(createProvider());
-
- boost::optional<std::string> hash = testling->getAvatarHash(user1);
- CPPUNIT_ASSERT(!hash);
- }
-
- void testGetAvatarWithSingleAvatarProvider() {
- boost::shared_ptr<CombinedAvatarProvider> testling(createProvider());
- avatarProvider1->avatars[user1] = avatarHash1;
- testling->addProvider(avatarProvider1);
-
- boost::optional<std::string> hash = testling->getAvatarHash(user1);
- CPPUNIT_ASSERT(hash);
- CPPUNIT_ASSERT_EQUAL(avatarHash1, *hash);
- }
-
- void testGetAvatarWithMultipleAvatarProviderReturnsFirstAvatar() {
- boost::shared_ptr<CombinedAvatarProvider> testling(createProvider());
- avatarProvider1->avatars[user1] = avatarHash1;
- avatarProvider2->avatars[user1] = avatarHash2;
- testling->addProvider(avatarProvider1);
- testling->addProvider(avatarProvider2);
-
- boost::optional<std::string> hash = testling->getAvatarHash(user1);
- CPPUNIT_ASSERT(hash);
- CPPUNIT_ASSERT_EQUAL(avatarHash1, *hash);
- }
-
- void testGetAvatarWithMultipleAvatarProviderAndFailingFirstProviderReturnsSecondAvatar() {
- boost::shared_ptr<CombinedAvatarProvider> testling(createProvider());
- avatarProvider2->avatars[user1] = avatarHash2;
- testling->addProvider(avatarProvider1);
- testling->addProvider(avatarProvider2);
-
- boost::optional<std::string> hash = testling->getAvatarHash(user1);
- CPPUNIT_ASSERT(hash);
- CPPUNIT_ASSERT_EQUAL(avatarHash2, *hash);
- }
-
- void testProviderUpdateTriggersChange() {
- boost::shared_ptr<CombinedAvatarProvider> testling(createProvider());
- testling->addProvider(avatarProvider1);
- avatarProvider1->avatars[user1] = avatarHash1;
- avatarProvider1->onAvatarChanged(user1);
-
- CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changes.size()));
- CPPUNIT_ASSERT_EQUAL(user1, changes[0]);
- }
-
- void testProviderUpdateWithoutChangeDoesNotTriggerChange() {
- boost::shared_ptr<CombinedAvatarProvider> testling(createProvider());
- testling->addProvider(avatarProvider1);
- testling->addProvider(avatarProvider2);
- avatarProvider1->avatars[user1] = avatarHash1;
- avatarProvider1->onAvatarChanged(user1);
- changes.clear();
-
- avatarProvider2->avatars[user1] = avatarHash2;
- avatarProvider2->onAvatarChanged(user1);
-
- CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(changes.size()));
- }
-
- void testProviderSecondUpdateTriggersChange() {
- boost::shared_ptr<CombinedAvatarProvider> testling(createProvider());
- testling->addProvider(avatarProvider1);
- avatarProvider1->avatars[user1] = avatarHash1;
- avatarProvider1->onAvatarChanged(user1);
- changes.clear();
- avatarProvider1->avatars[user1] = avatarHash2;
- avatarProvider1->onAvatarChanged(user1);
-
- CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changes.size()));
- CPPUNIT_ASSERT_EQUAL(user1, changes[0]);
- }
-
-
- void testProviderUpdateWithAvatarDisappearingTriggersChange() {
- boost::shared_ptr<CombinedAvatarProvider> testling(createProvider());
- testling->addProvider(avatarProvider1);
- avatarProvider1->avatars[user1] = avatarHash1;
- avatarProvider1->onAvatarChanged(user1);
- changes.clear();
- avatarProvider1->avatars.clear();
- avatarProvider1->onAvatarChanged(user1);
-
- CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changes.size()));
- CPPUNIT_ASSERT_EQUAL(user1, changes[0]);
- }
-
- void testProviderUpdateAfterAvatarDisappearedTriggersChange() {
- boost::shared_ptr<CombinedAvatarProvider> testling(createProvider());
- testling->addProvider(avatarProvider1);
- avatarProvider1->avatars[user1] = avatarHash1;
- avatarProvider1->onAvatarChanged(user1);
- avatarProvider1->avatars.clear();
- avatarProvider1->onAvatarChanged(user1);
- changes.clear();
- avatarProvider1->avatars[user1] = avatarHash1;
- avatarProvider1->onAvatarChanged(user1);
-
- CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changes.size()));
- CPPUNIT_ASSERT_EQUAL(user1, changes[0]);
- }
-
-
- void testProviderUpdateAfterGetDoesNotTriggerChange() {
- boost::shared_ptr<CombinedAvatarProvider> testling(createProvider());
- testling->addProvider(avatarProvider1);
- avatarProvider1->avatars[user1] = avatarHash1;
-
- testling->getAvatarHash(user1);
- avatarProvider1->onAvatarChanged(user1);
-
- CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(changes.size()));
- }
-
- void testRemoveProviderDisconnectsUpdates() {
- boost::shared_ptr<CombinedAvatarProvider> testling(createProvider());
- testling->addProvider(avatarProvider1);
- testling->addProvider(avatarProvider2);
- testling->removeProvider(avatarProvider1);
- avatarProvider1->avatars[user1] = avatarHash1;
- avatarProvider2->avatars[user1] = avatarHash2;
- avatarProvider1->onAvatarChanged(user1);
-
- CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(changes.size()));
- }
-
- void testProviderUpdateBareJIDAfterGetFullJID() {
- boost::shared_ptr<CombinedAvatarProvider> testling(createProvider());
- avatarProvider1->useBare = true;
- testling->addProvider(avatarProvider1);
-
- avatarProvider1->avatars[user1.toBare()] = avatarHash1;
- testling->getAvatarHash(user1);
- avatarProvider1->avatars[user1.toBare()] = avatarHash2;
- avatarProvider1->onAvatarChanged(user1.toBare());
-
- boost::optional<std::string> hash = testling->getAvatarHash(user1);
- CPPUNIT_ASSERT(hash);
- CPPUNIT_ASSERT_EQUAL(avatarHash2, *hash);
- }
-
- void testAddRemoveFallthrough() {
- JID ownJID = JID("user0@own.com/res");
- JID user1 = JID("user1@bar.com/bla");
-
- boost::shared_ptr<CryptoProvider> crypto = boost::shared_ptr<CryptoProvider>(PlatformCryptoProvider::create());
- DummyStanzaChannel* stanzaChannel = new DummyStanzaChannel();
- stanzaChannel->setAvailable(true);
- IQRouter* iqRouter = new IQRouter(stanzaChannel);
- DummyMUCRegistry* mucRegistry = new DummyMUCRegistry();
- AvatarMemoryStorage* avatarStorage = new AvatarMemoryStorage();
- VCardMemoryStorage* vcardStorage = new VCardMemoryStorage(crypto.get());
- VCardManager* vcardManager = new VCardManager(ownJID, iqRouter, vcardStorage);
-
- boost::shared_ptr<VCardUpdateAvatarManager> updateManager(new VCardUpdateAvatarManager(vcardManager, stanzaChannel, avatarStorage, crypto.get(), mucRegistry));
- updateManager->onAvatarChanged.connect(boost::bind(&CombinedAvatarProviderTest::handleAvatarChanged, this, _1));
-
- boost::shared_ptr<VCardAvatarManager> manager(new VCardAvatarManager(vcardManager, avatarStorage, crypto.get(), mucRegistry));
- manager->onAvatarChanged.connect(boost::bind(&CombinedAvatarProviderTest::handleAvatarChanged, this, _1));
-
- boost::shared_ptr<OfflineAvatarManager> offlineManager(new OfflineAvatarManager(avatarStorage));
- offlineManager->onAvatarChanged.connect(boost::bind(&CombinedAvatarProviderTest::handleAvatarChanged, this, _1));
-
- boost::shared_ptr<CombinedAvatarProvider> testling(createProvider());
- avatarProvider1->useBare = true;
- testling->addProvider(updateManager.get());
- testling->addProvider(manager.get());
- testling->addProvider(offlineManager.get());
-
- /* place an avatar in the cache, check that it reads back OK */
-
- CPPUNIT_ASSERT_EQUAL(size_t(0), changes.size());
-
- ByteArray avatar1 = createByteArray("abcdefg");
- std::string avatar1Hash = Hexify::hexify(crypto->getSHA1Hash(avatar1));
- VCard::ref vcard1(new VCard());
- vcard1->setPhoto(avatar1);
-
- vcardStorage->setVCard(user1.toBare(), vcard1);
- boost::optional<std::string> testHash = testling->getAvatarHash(user1.toBare());
- CPPUNIT_ASSERT(testHash);
- CPPUNIT_ASSERT_EQUAL(avatar1Hash, *testHash);
-
- VCard::ref storedVCard = vcardStorage->getVCard(user1.toBare());
- CPPUNIT_ASSERT(!!storedVCard);
- testHash = Hexify::hexify(crypto->getSHA1Hash(storedVCard->getPhoto()));
- CPPUNIT_ASSERT_EQUAL(avatar1Hash, *testHash);
-
- /* change the avatar by sending an VCard IQ */
-
- vcardManager->requestVCard(user1.toBare());
- CPPUNIT_ASSERT_EQUAL(size_t(1), stanzaChannel->sentStanzas.size());
- IQ::ref request = boost::dynamic_pointer_cast<IQ>(stanzaChannel->sentStanzas.back());
- VCard::ref payload = request->getPayload<VCard>();
- CPPUNIT_ASSERT(!!payload);
- stanzaChannel->sentStanzas.pop_back();
-
- ByteArray avatar2 = createByteArray("1234567");
- std::string avatar2Hash = Hexify::hexify(crypto->getSHA1Hash(avatar2));
- VCard::ref vcard2(new VCard());
- vcard2->setPhoto(avatar2);
-
- IQ::ref reply = boost::make_shared<IQ>();
- reply->setTo(request->getFrom());
- reply->setFrom(request->getTo());
- reply->setID(request->getID());
- reply->addPayload(vcard2);
- reply->setType(IQ::Result);
-
- stanzaChannel->onIQReceived(reply);
-
- /* check that we changed the avatar successfully and that we were notified about the changes */
-
- testHash = testling->getAvatarHash(user1.toBare());
- CPPUNIT_ASSERT(testHash);
- CPPUNIT_ASSERT_EQUAL(avatar2Hash, *testHash);
- CPPUNIT_ASSERT_EQUAL(size_t(3), changes.size());
- CPPUNIT_ASSERT_EQUAL(user1.toBare(), changes[0]);
- CPPUNIT_ASSERT_EQUAL(user1.toBare(), changes[1]);
- CPPUNIT_ASSERT_EQUAL(user1.toBare(), changes[2]);
- changes.clear();
- storedVCard = vcardStorage->getVCard(user1.toBare());
- CPPUNIT_ASSERT(!!storedVCard);
- testHash = Hexify::hexify(crypto->getSHA1Hash(storedVCard->getPhoto()));
- CPPUNIT_ASSERT_EQUAL(avatar2Hash, *testHash);
-
- /* change the avatar to the empty avatar */
-
- vcardManager->requestVCard(user1.toBare());
- CPPUNIT_ASSERT_EQUAL(size_t(1), stanzaChannel->sentStanzas.size());
- request = boost::dynamic_pointer_cast<IQ>(stanzaChannel->sentStanzas.back());
- payload = request->getPayload<VCard>();
- CPPUNIT_ASSERT(!!payload);
- stanzaChannel->sentStanzas.pop_back();
-
- VCard::ref vcard3(new VCard());
- reply = boost::make_shared<IQ>();
- reply->setTo(request->getFrom());
- reply->setFrom(request->getTo());
- reply->setID(request->getID());
- reply->addPayload(vcard3);
- reply->setType(IQ::Result);
- stanzaChannel->onIQReceived(reply);
-
- /* check that we changed the avatar successfully */
-
- testHash = testling->getAvatarHash(user1.toBare());
- CPPUNIT_ASSERT(testHash);
- CPPUNIT_ASSERT_EQUAL(std::string(""), *testHash);
- CPPUNIT_ASSERT_EQUAL(size_t(3), changes.size());
- CPPUNIT_ASSERT_EQUAL(user1.toBare(), changes[0]);
- CPPUNIT_ASSERT_EQUAL(user1.toBare(), changes[1]);
- CPPUNIT_ASSERT_EQUAL(user1.toBare(), changes[2]);
- changes.clear();
- storedVCard = vcardStorage->getVCard(user1.toBare());
- CPPUNIT_ASSERT(!!storedVCard);
- CPPUNIT_ASSERT(!storedVCard->getPhoto().size());
-
- delete vcardManager;
- delete vcardStorage;
- delete mucRegistry;
- delete iqRouter;
- delete stanzaChannel;
- }
-
- private:
- boost::shared_ptr<CombinedAvatarProvider> createProvider() {
- boost::shared_ptr<CombinedAvatarProvider> result(new CombinedAvatarProvider());
- result->onAvatarChanged.connect(boost::bind(&CombinedAvatarProviderTest::handleAvatarChanged, this, _1));
- return result;
- }
-
- void handleAvatarChanged(const JID& jid) {
- changes.push_back(jid);
- }
-
- private:
- struct DummyAvatarProvider : public AvatarProvider {
- DummyAvatarProvider() : useBare(false) {
- }
-
- boost::optional<std::string> getAvatarHash(const JID& jid) const {
- JID actualJID = useBare ? jid.toBare() : jid;
- std::map<JID, std::string>::const_iterator i = avatars.find(actualJID);
- if (i != avatars.end()) {
- return i->second;
- }
- else {
- return boost::optional<std::string>();
- }
- }
-
- bool useBare;
- std::map<JID, std::string> avatars;
- };
-
- struct DummyMUCRegistry : public MUCRegistry {
- bool isMUC(const JID& jid) const { return std::find(mucs_.begin(), mucs_.end(), jid) != mucs_.end(); }
- std::vector<JID> mucs_;
- };
-
- DummyAvatarProvider* avatarProvider1;
- DummyAvatarProvider* avatarProvider2;
- JID user1;
- JID user2;
- std::string avatarHash1;
- std::string avatarHash2;
- std::string avatarHash3;
- std::vector<JID> changes;
+ CPPUNIT_TEST_SUITE(CombinedAvatarProviderTest);
+ CPPUNIT_TEST(testGetAvatarWithNoAvatarProviderReturnsEmpty);
+ CPPUNIT_TEST(testGetAvatarWithSingleAvatarProvider);
+ CPPUNIT_TEST(testGetAvatarWithMultipleAvatarProviderReturnsFirstAvatar);
+ CPPUNIT_TEST(testGetAvatarWithMultipleAvatarProviderAndFailingFirstProviderReturnsSecondAvatar);
+ CPPUNIT_TEST(testProviderUpdateTriggersChange);
+ CPPUNIT_TEST(testProviderUpdateWithoutChangeDoesNotTriggerChange);
+ CPPUNIT_TEST(testProviderSecondUpdateTriggersChange);
+ CPPUNIT_TEST(testProviderUpdateWithAvatarDisappearingTriggersChange);
+ CPPUNIT_TEST(testProviderUpdateAfterAvatarDisappearedTriggersChange);
+ CPPUNIT_TEST(testProviderUpdateAfterGetDoesNotTriggerChange);
+ CPPUNIT_TEST(testProviderUpdateBareJIDAfterGetFullJID);
+ CPPUNIT_TEST(testRemoveProviderDisconnectsUpdates);
+ CPPUNIT_TEST(testAddRemoveFallthrough);
+ CPPUNIT_TEST_SUITE_END();
+
+ public:
+ void setUp() {
+ avatarProvider1 = std::unique_ptr<DummyAvatarProvider>(new DummyAvatarProvider());
+ avatarProvider2 = std::unique_ptr<DummyAvatarProvider>(new DummyAvatarProvider());
+ user1 = JID("user1@bar.com/bla");
+ user2 = JID("user2@foo.com/baz");
+ avatarHash1 = "ABCDEFG";
+ avatarHash2 = "XYZU";
+ avatarHash3 = "IDGH";
+ }
+
+ void testGetAvatarWithNoAvatarProviderReturnsEmpty() {
+ std::shared_ptr<CombinedAvatarProvider> testling(createProvider());
+
+ boost::optional<std::string> hash = testling->getAvatarHash(user1);
+ CPPUNIT_ASSERT(!hash);
+ }
+
+ void testGetAvatarWithSingleAvatarProvider() {
+ std::shared_ptr<CombinedAvatarProvider> testling(createProvider());
+ avatarProvider1->avatars[user1] = avatarHash1;
+ testling->addProvider(avatarProvider1.get());
+
+ boost::optional<std::string> hash = testling->getAvatarHash(user1);
+ CPPUNIT_ASSERT(hash);
+ CPPUNIT_ASSERT_EQUAL(avatarHash1, *hash);
+ }
+
+ void testGetAvatarWithMultipleAvatarProviderReturnsFirstAvatar() {
+ std::shared_ptr<CombinedAvatarProvider> testling(createProvider());
+ avatarProvider1->avatars[user1] = avatarHash1;
+ avatarProvider2->avatars[user1] = avatarHash2;
+ testling->addProvider(avatarProvider1.get());
+ testling->addProvider(avatarProvider2.get());
+
+ boost::optional<std::string> hash = testling->getAvatarHash(user1);
+ CPPUNIT_ASSERT(hash);
+ CPPUNIT_ASSERT_EQUAL(avatarHash1, *hash);
+ }
+
+ void testGetAvatarWithMultipleAvatarProviderAndFailingFirstProviderReturnsSecondAvatar() {
+ std::shared_ptr<CombinedAvatarProvider> testling(createProvider());
+ avatarProvider2->avatars[user1] = avatarHash2;
+ testling->addProvider(avatarProvider1.get());
+ testling->addProvider(avatarProvider2.get());
+
+ boost::optional<std::string> hash = testling->getAvatarHash(user1);
+ CPPUNIT_ASSERT(hash);
+ CPPUNIT_ASSERT_EQUAL(avatarHash2, *hash);
+ }
+
+ void testProviderUpdateTriggersChange() {
+ std::shared_ptr<CombinedAvatarProvider> testling(createProvider());
+ testling->addProvider(avatarProvider1.get());
+ avatarProvider1->avatars[user1] = avatarHash1;
+ avatarProvider1->onAvatarChanged(user1);
+
+ CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changes.size()));
+ CPPUNIT_ASSERT_EQUAL(user1, changes[0]);
+ }
+
+ void testProviderUpdateWithoutChangeDoesNotTriggerChange() {
+ std::shared_ptr<CombinedAvatarProvider> testling(createProvider());
+ testling->addProvider(avatarProvider1.get());
+ testling->addProvider(avatarProvider2.get());
+ avatarProvider1->avatars[user1] = avatarHash1;
+ avatarProvider1->onAvatarChanged(user1);
+ changes.clear();
+
+ avatarProvider2->avatars[user1] = avatarHash2;
+ avatarProvider2->onAvatarChanged(user1);
+
+ CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(changes.size()));
+ }
+
+ void testProviderSecondUpdateTriggersChange() {
+ std::shared_ptr<CombinedAvatarProvider> testling(createProvider());
+ testling->addProvider(avatarProvider1.get());
+ avatarProvider1->avatars[user1] = avatarHash1;
+ avatarProvider1->onAvatarChanged(user1);
+ changes.clear();
+ avatarProvider1->avatars[user1] = avatarHash2;
+ avatarProvider1->onAvatarChanged(user1);
+
+ CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changes.size()));
+ CPPUNIT_ASSERT_EQUAL(user1, changes[0]);
+ }
+
+
+ void testProviderUpdateWithAvatarDisappearingTriggersChange() {
+ std::shared_ptr<CombinedAvatarProvider> testling(createProvider());
+ testling->addProvider(avatarProvider1.get());
+ avatarProvider1->avatars[user1] = avatarHash1;
+ avatarProvider1->onAvatarChanged(user1);
+ changes.clear();
+ avatarProvider1->avatars.clear();
+ avatarProvider1->onAvatarChanged(user1);
+
+ CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changes.size()));
+ CPPUNIT_ASSERT_EQUAL(user1, changes[0]);
+ }
+
+ void testProviderUpdateAfterAvatarDisappearedTriggersChange() {
+ std::shared_ptr<CombinedAvatarProvider> testling(createProvider());
+ testling->addProvider(avatarProvider1.get());
+ avatarProvider1->avatars[user1] = avatarHash1;
+ avatarProvider1->onAvatarChanged(user1);
+ avatarProvider1->avatars.clear();
+ avatarProvider1->onAvatarChanged(user1);
+ changes.clear();
+ avatarProvider1->avatars[user1] = avatarHash1;
+ avatarProvider1->onAvatarChanged(user1);
+
+ CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changes.size()));
+ CPPUNIT_ASSERT_EQUAL(user1, changes[0]);
+ }
+
+
+ void testProviderUpdateAfterGetDoesNotTriggerChange() {
+ std::shared_ptr<CombinedAvatarProvider> testling(createProvider());
+ testling->addProvider(avatarProvider1.get());
+ avatarProvider1->avatars[user1] = avatarHash1;
+
+ testling->getAvatarHash(user1);
+ avatarProvider1->onAvatarChanged(user1);
+
+ CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(changes.size()));
+ }
+
+ void testRemoveProviderDisconnectsUpdates() {
+ std::shared_ptr<CombinedAvatarProvider> testling(createProvider());
+ testling->addProvider(avatarProvider1.get());
+ testling->addProvider(avatarProvider2.get());
+ testling->removeProvider(avatarProvider1.get());
+ avatarProvider1->avatars[user1] = avatarHash1;
+ avatarProvider2->avatars[user1] = avatarHash2;
+ avatarProvider1->onAvatarChanged(user1);
+
+ CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(changes.size()));
+ }
+
+ void testProviderUpdateBareJIDAfterGetFullJID() {
+ std::shared_ptr<CombinedAvatarProvider> testling(createProvider());
+ avatarProvider1->useBare = true;
+ testling->addProvider(avatarProvider1.get());
+
+ avatarProvider1->avatars[user1.toBare()] = avatarHash1;
+ testling->getAvatarHash(user1);
+ avatarProvider1->avatars[user1.toBare()] = avatarHash2;
+ avatarProvider1->onAvatarChanged(user1.toBare());
+
+ boost::optional<std::string> hash = testling->getAvatarHash(user1);
+ CPPUNIT_ASSERT(hash);
+ CPPUNIT_ASSERT_EQUAL(avatarHash2, *hash);
+ }
+
+ void testAddRemoveFallthrough() {
+ JID ownJID = JID("user0@own.com/res");
+ JID user1 = JID("user1@bar.com/bla");
+
+ std::shared_ptr<CryptoProvider> crypto = std::shared_ptr<CryptoProvider>(PlatformCryptoProvider::create());
+ DummyStanzaChannel* stanzaChannel = new DummyStanzaChannel();
+ stanzaChannel->setAvailable(true);
+ IQRouter* iqRouter = new IQRouter(stanzaChannel);
+ DummyMUCRegistry* mucRegistry = new DummyMUCRegistry();
+ AvatarMemoryStorage* avatarStorage = new AvatarMemoryStorage();
+ VCardMemoryStorage* vcardStorage = new VCardMemoryStorage(crypto.get());
+ VCardManager* vcardManager = new VCardManager(ownJID, iqRouter, vcardStorage);
+
+ std::shared_ptr<VCardUpdateAvatarManager> updateManager(new VCardUpdateAvatarManager(vcardManager, stanzaChannel, avatarStorage, crypto.get(), mucRegistry));
+ updateManager->onAvatarChanged.connect(boost::bind(&CombinedAvatarProviderTest::handleAvatarChanged, this, _1));
+
+ std::shared_ptr<VCardAvatarManager> manager(new VCardAvatarManager(vcardManager, avatarStorage, crypto.get(), mucRegistry));
+ manager->onAvatarChanged.connect(boost::bind(&CombinedAvatarProviderTest::handleAvatarChanged, this, _1));
+
+ std::shared_ptr<OfflineAvatarManager> offlineManager(new OfflineAvatarManager(avatarStorage));
+ offlineManager->onAvatarChanged.connect(boost::bind(&CombinedAvatarProviderTest::handleAvatarChanged, this, _1));
+
+ std::shared_ptr<CombinedAvatarProvider> testling(createProvider());
+ avatarProvider1->useBare = true;
+ testling->addProvider(updateManager.get());
+ testling->addProvider(manager.get());
+ testling->addProvider(offlineManager.get());
+
+ /* place an avatar in the cache, check that it reads back OK */
+
+ CPPUNIT_ASSERT_EQUAL(size_t(0), changes.size());
+
+ ByteArray avatar1 = createByteArray("abcdefg");
+ std::string avatar1Hash = Hexify::hexify(crypto->getSHA1Hash(avatar1));
+ VCard::ref vcard1(new VCard());
+ vcard1->setPhoto(avatar1);
+
+ vcardStorage->setVCard(user1.toBare(), vcard1);
+ boost::optional<std::string> testHash = testling->getAvatarHash(user1.toBare());
+ CPPUNIT_ASSERT(testHash);
+ CPPUNIT_ASSERT_EQUAL(avatar1Hash, *testHash);
+
+ VCard::ref storedVCard = vcardStorage->getVCard(user1.toBare());
+ CPPUNIT_ASSERT(!!storedVCard);
+ testHash = Hexify::hexify(crypto->getSHA1Hash(storedVCard->getPhoto()));
+ CPPUNIT_ASSERT_EQUAL(avatar1Hash, *testHash);
+
+ /* change the avatar by sending an VCard IQ */
+
+ vcardManager->requestVCard(user1.toBare());
+ CPPUNIT_ASSERT_EQUAL(size_t(1), stanzaChannel->sentStanzas.size());
+ IQ::ref request = std::dynamic_pointer_cast<IQ>(stanzaChannel->sentStanzas.back());
+ VCard::ref payload = request->getPayload<VCard>();
+ CPPUNIT_ASSERT(!!payload);
+ stanzaChannel->sentStanzas.pop_back();
+
+ ByteArray avatar2 = createByteArray("1234567");
+ std::string avatar2Hash = Hexify::hexify(crypto->getSHA1Hash(avatar2));
+ VCard::ref vcard2(new VCard());
+ vcard2->setPhoto(avatar2);
+
+ IQ::ref reply = std::make_shared<IQ>();
+ reply->setTo(request->getFrom());
+ reply->setFrom(request->getTo());
+ reply->setID(request->getID());
+ reply->addPayload(vcard2);
+ reply->setType(IQ::Result);
+
+ stanzaChannel->onIQReceived(reply);
+
+ /* check that we changed the avatar successfully and that we were notified about the changes */
+
+ testHash = testling->getAvatarHash(user1.toBare());
+ CPPUNIT_ASSERT(testHash);
+ CPPUNIT_ASSERT_EQUAL(avatar2Hash, *testHash);
+ CPPUNIT_ASSERT_EQUAL(size_t(3), changes.size());
+ CPPUNIT_ASSERT_EQUAL(user1.toBare(), changes[0]);
+ CPPUNIT_ASSERT_EQUAL(user1.toBare(), changes[1]);
+ CPPUNIT_ASSERT_EQUAL(user1.toBare(), changes[2]);
+ changes.clear();
+ storedVCard = vcardStorage->getVCard(user1.toBare());
+ CPPUNIT_ASSERT(!!storedVCard);
+ testHash = Hexify::hexify(crypto->getSHA1Hash(storedVCard->getPhoto()));
+ CPPUNIT_ASSERT_EQUAL(avatar2Hash, *testHash);
+
+ /* change the avatar to the empty avatar */
+
+ vcardManager->requestVCard(user1.toBare());
+ CPPUNIT_ASSERT_EQUAL(size_t(1), stanzaChannel->sentStanzas.size());
+ request = std::dynamic_pointer_cast<IQ>(stanzaChannel->sentStanzas.back());
+ payload = request->getPayload<VCard>();
+ CPPUNIT_ASSERT(!!payload);
+ stanzaChannel->sentStanzas.pop_back();
+
+ VCard::ref vcard3(new VCard());
+ reply = std::make_shared<IQ>();
+ reply->setTo(request->getFrom());
+ reply->setFrom(request->getTo());
+ reply->setID(request->getID());
+ reply->addPayload(vcard3);
+ reply->setType(IQ::Result);
+ stanzaChannel->onIQReceived(reply);
+
+ /* check that we changed the avatar successfully */
+
+ testHash = testling->getAvatarHash(user1.toBare());
+ CPPUNIT_ASSERT(testHash);
+ CPPUNIT_ASSERT_EQUAL(std::string(""), *testHash);
+ CPPUNIT_ASSERT_EQUAL(size_t(3), changes.size());
+ CPPUNIT_ASSERT_EQUAL(user1.toBare(), changes[0]);
+ CPPUNIT_ASSERT_EQUAL(user1.toBare(), changes[1]);
+ CPPUNIT_ASSERT_EQUAL(user1.toBare(), changes[2]);
+ changes.clear();
+ storedVCard = vcardStorage->getVCard(user1.toBare());
+ CPPUNIT_ASSERT(!!storedVCard);
+ CPPUNIT_ASSERT(!storedVCard->getPhoto().size());
+
+ delete vcardManager;
+ delete vcardStorage;
+ delete mucRegistry;
+ delete iqRouter;
+ delete stanzaChannel;
+ }
+
+ private:
+ std::shared_ptr<CombinedAvatarProvider> createProvider() {
+ std::shared_ptr<CombinedAvatarProvider> result(new CombinedAvatarProvider());
+ result->onAvatarChanged.connect(boost::bind(&CombinedAvatarProviderTest::handleAvatarChanged, this, _1));
+ return result;
+ }
+
+ void handleAvatarChanged(const JID& jid) {
+ changes.push_back(jid);
+ }
+
+ private:
+ struct DummyAvatarProvider : public AvatarProvider {
+ DummyAvatarProvider() : useBare(false) {
+ }
+
+ boost::optional<std::string> getAvatarHash(const JID& jid) const {
+ JID actualJID = useBare ? jid.toBare() : jid;
+ std::map<JID, std::string>::const_iterator i = avatars.find(actualJID);
+ if (i != avatars.end()) {
+ return i->second;
+ }
+ else {
+ return boost::optional<std::string>();
+ }
+ }
+
+ bool useBare;
+ std::map<JID, std::string> avatars;
+ };
+
+ struct DummyMUCRegistry : public MUCRegistry {
+ bool isMUC(const JID& jid) const { return std::find(mucs_.begin(), mucs_.end(), jid) != mucs_.end(); }
+ std::vector<JID> mucs_;
+ };
+
+ std::unique_ptr<DummyAvatarProvider> avatarProvider1;
+ std::unique_ptr<DummyAvatarProvider> avatarProvider2;
+ JID user1;
+ JID user2;
+ std::string avatarHash1;
+ std::string avatarHash2;
+ std::string avatarHash3;
+ std::vector<JID> changes;
};
CPPUNIT_TEST_SUITE_REGISTRATION(CombinedAvatarProviderTest);
diff --git a/Swiften/Avatars/UnitTest/VCardAvatarManagerTest.cpp b/Swiften/Avatars/UnitTest/VCardAvatarManagerTest.cpp
index 0ac0b70..2ca9c1a 100644
--- a/Swiften/Avatars/UnitTest/VCardAvatarManagerTest.cpp
+++ b/Swiften/Avatars/UnitTest/VCardAvatarManagerTest.cpp
@@ -1,175 +1,176 @@
/*
- * Copyright (c) 2010-2013 Isode Limited.
+ * Copyright (c) 2010-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
-#include <Swiften/Base/ByteArray.h>
+#include <boost/bind.hpp>
+
#include <QA/Checker/IO.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
-#include <boost/bind.hpp>
-#include <Swiften/Elements/VCard.h>
-#include <Swiften/Avatars/VCardAvatarManager.h>
-#include <Swiften/VCards/VCardMemoryStorage.h>
#include <Swiften/Avatars/AvatarMemoryStorage.h>
-#include <Swiften/VCards/VCardManager.h>
-#include <Swiften/MUC/MUCRegistry.h>
-#include <Swiften/Queries/IQRouter.h>
+#include <Swiften/Avatars/VCardAvatarManager.h>
+#include <Swiften/Base/ByteArray.h>
#include <Swiften/Client/DummyStanzaChannel.h>
#include <Swiften/Crypto/CryptoProvider.h>
#include <Swiften/Crypto/PlatformCryptoProvider.h>
+#include <Swiften/Elements/VCard.h>
+#include <Swiften/MUC/MUCRegistry.h>
+#include <Swiften/Queries/IQRouter.h>
#include <Swiften/StringCodecs/Hexify.h>
+#include <Swiften/VCards/VCardManager.h>
+#include <Swiften/VCards/VCardMemoryStorage.h>
using namespace Swift;
class VCardAvatarManagerTest : public CppUnit::TestFixture {
- CPPUNIT_TEST_SUITE(VCardAvatarManagerTest);
- CPPUNIT_TEST(testGetAvatarHashKnownAvatar);
- CPPUNIT_TEST(testGetAvatarHashEmptyAvatar);
- CPPUNIT_TEST(testGetAvatarHashUnknownAvatarKnownVCardStoresAvatar);
- CPPUNIT_TEST(testGetAvatarHashUnknownAvatarUnknownVCard);
- CPPUNIT_TEST(testVCardUpdateTriggersUpdate);
- CPPUNIT_TEST(testGetAvatarHashKnownAvatarUnknownVCard);
- CPPUNIT_TEST_SUITE_END();
-
- public:
- void setUp() {
- crypto = boost::shared_ptr<CryptoProvider>(PlatformCryptoProvider::create());
- ownJID = JID("foo@fum.com/bum");
- stanzaChannel = new DummyStanzaChannel();
- stanzaChannel->setAvailable(true);
- iqRouter = new IQRouter(stanzaChannel);
- mucRegistry = new DummyMUCRegistry();
- avatarStorage = new AvatarMemoryStorage();
- vcardStorage = new VCardMemoryStorage(crypto.get());
- vcardManager = new VCardManager(ownJID, iqRouter, vcardStorage);
- avatar1 = createByteArray("abcdefg");
- avatar1Hash = Hexify::hexify(crypto->getSHA1Hash(avatar1));
- user1 = JID("user1@bar.com/bla");
- user2 = JID("user2@foo.com/baz");
- }
-
- void tearDown() {
- delete vcardManager;
- delete vcardStorage;
- delete avatarStorage;
- delete mucRegistry;
- delete iqRouter;
- delete stanzaChannel;
- }
-
- void testGetAvatarHashKnownAvatar() {
- boost::shared_ptr<VCardAvatarManager> testling = createManager();
- storeVCardWithPhoto(user1.toBare(), avatar1);
- avatarStorage->addAvatar(avatar1Hash, avatar1);
-
- boost::optional<std::string> result = testling->getAvatarHash(user1);
-
- CPPUNIT_ASSERT(result);
- CPPUNIT_ASSERT_EQUAL(avatar1Hash, *result);
- }
-
- void testGetAvatarHashEmptyAvatar() {
- boost::shared_ptr<VCardAvatarManager> testling = createManager();
- storeEmptyVCard(user1.toBare());
-
- boost::optional<std::string> result = testling->getAvatarHash(user1);
-
- CPPUNIT_ASSERT(result);
- CPPUNIT_ASSERT_EQUAL(std::string(), *result);
- }
-
- void testGetAvatarHashUnknownAvatarKnownVCardStoresAvatar() {
- boost::shared_ptr<VCardAvatarManager> testling = createManager();
- storeVCardWithPhoto(user1.toBare(), avatar1);
-
- boost::optional<std::string> result = testling->getAvatarHash(user1);
-
- CPPUNIT_ASSERT(result);
- CPPUNIT_ASSERT_EQUAL(avatar1Hash, *result);
- CPPUNIT_ASSERT(avatarStorage->hasAvatar(avatar1Hash));
- CPPUNIT_ASSERT_EQUAL(avatar1, avatarStorage->getAvatar(avatar1Hash));
- }
-
- void testGetAvatarHashUnknownAvatarUnknownVCard() {
- boost::shared_ptr<VCardAvatarManager> testling = createManager();
-
- boost::optional<std::string> result = testling->getAvatarHash(user1);
-
- CPPUNIT_ASSERT(result);
- CPPUNIT_ASSERT_EQUAL(std::string(), *result);
- }
-
- void testGetAvatarHashKnownAvatarUnknownVCard() {
- boost::shared_ptr<VCardAvatarManager> testling = createManager();
-
- avatarStorage->setAvatarForJID(user1, avatar1Hash);
-
- boost::optional<std::string> result = testling->getAvatarHash(user1);
-
- CPPUNIT_ASSERT(result);
- CPPUNIT_ASSERT_EQUAL(std::string(), *result);
- }
-
-
- void testVCardUpdateTriggersUpdate() {
- boost::shared_ptr<VCardAvatarManager> testling = createManager();
- vcardManager->requestVCard(user1);
- sendVCardResult();
-
- CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changes.size()));
- }
-
- private:
- boost::shared_ptr<VCardAvatarManager> createManager() {
- boost::shared_ptr<VCardAvatarManager> result(new VCardAvatarManager(vcardManager, avatarStorage, crypto.get(), mucRegistry));
- result->onAvatarChanged.connect(boost::bind(&VCardAvatarManagerTest::handleAvatarChanged, this, _1));
- return result;
- }
-
- void storeVCardWithPhoto(const JID& jid, const ByteArray& avatar) {
- VCard::ref vcard(new VCard());
- vcard->setPhoto(avatar);
- vcardStorage->setVCard(jid, vcard);
- }
-
- void storeEmptyVCard(const JID& jid) {
- VCard::ref vcard(new VCard());
- vcardStorage->setVCard(jid, vcard);
- }
-
- void handleAvatarChanged(const JID& jid) {
- changes.push_back(jid);
- }
-
- void sendVCardResult() {
- VCard::ref vcard(new VCard());
- vcard->setFullName("Foo Bar");
- stanzaChannel->onIQReceived(IQ::createResult(JID("baz@fum.com/dum"), stanzaChannel->sentStanzas[0]->getTo(), stanzaChannel->sentStanzas[0]->getID(), vcard));
- }
-
- private:
- struct DummyMUCRegistry : public MUCRegistry {
- bool isMUC(const JID& jid) const { return std::find(mucs_.begin(), mucs_.end(), jid) != mucs_.end(); }
- std::vector<JID> mucs_;
- };
-
- JID ownJID;
- DummyStanzaChannel* stanzaChannel;
- IQRouter* iqRouter;
- DummyMUCRegistry* mucRegistry;
- AvatarMemoryStorage* avatarStorage;
- VCardManager* vcardManager;
- VCardMemoryStorage* vcardStorage;
- ByteArray avatar1;
- std::string avatar1Hash;
- std::vector<JID> changes;
- JID user1;
- JID user2;
- boost::shared_ptr<CryptoProvider> crypto;
+ CPPUNIT_TEST_SUITE(VCardAvatarManagerTest);
+ CPPUNIT_TEST(testGetAvatarHashKnownAvatar);
+ CPPUNIT_TEST(testGetAvatarHashEmptyAvatar);
+ CPPUNIT_TEST(testGetAvatarHashUnknownAvatarKnownVCardStoresAvatar);
+ CPPUNIT_TEST(testGetAvatarHashUnknownAvatarUnknownVCard);
+ CPPUNIT_TEST(testVCardUpdateTriggersUpdate);
+ CPPUNIT_TEST(testGetAvatarHashKnownAvatarUnknownVCard);
+ CPPUNIT_TEST_SUITE_END();
+
+ public:
+ void setUp() {
+ crypto = std::shared_ptr<CryptoProvider>(PlatformCryptoProvider::create());
+ ownJID = JID("foo@fum.com/bum");
+ stanzaChannel = new DummyStanzaChannel();
+ stanzaChannel->setAvailable(true);
+ iqRouter = new IQRouter(stanzaChannel);
+ mucRegistry = new DummyMUCRegistry();
+ avatarStorage = new AvatarMemoryStorage();
+ vcardStorage = new VCardMemoryStorage(crypto.get());
+ vcardManager = new VCardManager(ownJID, iqRouter, vcardStorage);
+ avatar1 = createByteArray("abcdefg");
+ avatar1Hash = Hexify::hexify(crypto->getSHA1Hash(avatar1));
+ user1 = JID("user1@bar.com/bla");
+ user2 = JID("user2@foo.com/baz");
+ }
+
+ void tearDown() {
+ delete vcardManager;
+ delete vcardStorage;
+ delete avatarStorage;
+ delete mucRegistry;
+ delete iqRouter;
+ delete stanzaChannel;
+ }
+
+ void testGetAvatarHashKnownAvatar() {
+ std::shared_ptr<VCardAvatarManager> testling = createManager();
+ storeVCardWithPhoto(user1.toBare(), avatar1);
+ avatarStorage->addAvatar(avatar1Hash, avatar1);
+
+ boost::optional<std::string> result = testling->getAvatarHash(user1);
+
+ CPPUNIT_ASSERT(result);
+ CPPUNIT_ASSERT_EQUAL(avatar1Hash, *result);
+ }
+
+ void testGetAvatarHashEmptyAvatar() {
+ std::shared_ptr<VCardAvatarManager> testling = createManager();
+ storeEmptyVCard(user1.toBare());
+
+ boost::optional<std::string> result = testling->getAvatarHash(user1);
+
+ CPPUNIT_ASSERT(result);
+ CPPUNIT_ASSERT_EQUAL(std::string(), *result);
+ }
+
+ void testGetAvatarHashUnknownAvatarKnownVCardStoresAvatar() {
+ std::shared_ptr<VCardAvatarManager> testling = createManager();
+ storeVCardWithPhoto(user1.toBare(), avatar1);
+
+ boost::optional<std::string> result = testling->getAvatarHash(user1);
+
+ CPPUNIT_ASSERT(result);
+ CPPUNIT_ASSERT_EQUAL(avatar1Hash, *result);
+ CPPUNIT_ASSERT(avatarStorage->hasAvatar(avatar1Hash));
+ CPPUNIT_ASSERT_EQUAL(avatar1, avatarStorage->getAvatar(avatar1Hash));
+ }
+
+ void testGetAvatarHashUnknownAvatarUnknownVCard() {
+ std::shared_ptr<VCardAvatarManager> testling = createManager();
+
+ boost::optional<std::string> result = testling->getAvatarHash(user1);
+
+ CPPUNIT_ASSERT(result);
+ CPPUNIT_ASSERT_EQUAL(std::string(), *result);
+ }
+
+ void testGetAvatarHashKnownAvatarUnknownVCard() {
+ std::shared_ptr<VCardAvatarManager> testling = createManager();
+
+ avatarStorage->setAvatarForJID(user1, avatar1Hash);
+
+ boost::optional<std::string> result = testling->getAvatarHash(user1);
+
+ CPPUNIT_ASSERT(result);
+ CPPUNIT_ASSERT_EQUAL(std::string(), *result);
+ }
+
+
+ void testVCardUpdateTriggersUpdate() {
+ std::shared_ptr<VCardAvatarManager> testling = createManager();
+ vcardManager->requestVCard(user1);
+ sendVCardResult();
+
+ CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changes.size()));
+ }
+
+ private:
+ std::shared_ptr<VCardAvatarManager> createManager() {
+ std::shared_ptr<VCardAvatarManager> result(new VCardAvatarManager(vcardManager, avatarStorage, crypto.get(), mucRegistry));
+ result->onAvatarChanged.connect(boost::bind(&VCardAvatarManagerTest::handleAvatarChanged, this, _1));
+ return result;
+ }
+
+ void storeVCardWithPhoto(const JID& jid, const ByteArray& avatar) {
+ VCard::ref vcard(new VCard());
+ vcard->setPhoto(avatar);
+ vcardStorage->setVCard(jid, vcard);
+ }
+
+ void storeEmptyVCard(const JID& jid) {
+ VCard::ref vcard(new VCard());
+ vcardStorage->setVCard(jid, vcard);
+ }
+
+ void handleAvatarChanged(const JID& jid) {
+ changes.push_back(jid);
+ }
+
+ void sendVCardResult() {
+ VCard::ref vcard(new VCard());
+ vcard->setFullName("Foo Bar");
+ stanzaChannel->onIQReceived(IQ::createResult(JID("baz@fum.com/dum"), stanzaChannel->sentStanzas[0]->getTo(), stanzaChannel->sentStanzas[0]->getID(), vcard));
+ }
+
+ private:
+ struct DummyMUCRegistry : public MUCRegistry {
+ bool isMUC(const JID& jid) const { return std::find(mucs_.begin(), mucs_.end(), jid) != mucs_.end(); }
+ std::vector<JID> mucs_;
+ };
+
+ JID ownJID;
+ DummyStanzaChannel* stanzaChannel;
+ IQRouter* iqRouter;
+ DummyMUCRegistry* mucRegistry;
+ AvatarMemoryStorage* avatarStorage;
+ VCardManager* vcardManager;
+ VCardMemoryStorage* vcardStorage;
+ ByteArray avatar1;
+ std::string avatar1Hash;
+ std::vector<JID> changes;
+ JID user1;
+ JID user2;
+ std::shared_ptr<CryptoProvider> crypto;
};
CPPUNIT_TEST_SUITE_REGISTRATION(VCardAvatarManagerTest);
diff --git a/Swiften/Avatars/UnitTest/VCardUpdateAvatarManagerTest.cpp b/Swiften/Avatars/UnitTest/VCardUpdateAvatarManagerTest.cpp
index 80b5718..bfa13cd 100644
--- a/Swiften/Avatars/UnitTest/VCardUpdateAvatarManagerTest.cpp
+++ b/Swiften/Avatars/UnitTest/VCardUpdateAvatarManagerTest.cpp
@@ -1,211 +1,212 @@
/*
- * Copyright (c) 2010-2013 Isode Limited.
+ * Copyright (c) 2010-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
-#include <Swiften/Base/ByteArray.h>
+#include <boost/bind.hpp>
+
#include <QA/Checker/IO.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
-#include <boost/bind.hpp>
-#include <Swiften/Elements/VCardUpdate.h>
-#include <Swiften/Avatars/VCardUpdateAvatarManager.h>
#include <Swiften/Avatars/AvatarMemoryStorage.h>
-#include <Swiften/VCards/VCardMemoryStorage.h>
-#include <Swiften/VCards/VCardManager.h>
-#include <Swiften/MUC/MUCRegistry.h>
-#include <Swiften/Queries/IQRouter.h>
+#include <Swiften/Avatars/VCardUpdateAvatarManager.h>
+#include <Swiften/Base/ByteArray.h>
#include <Swiften/Client/DummyStanzaChannel.h>
-#include <Swiften/StringCodecs/Hexify.h>
#include <Swiften/Crypto/CryptoProvider.h>
#include <Swiften/Crypto/PlatformCryptoProvider.h>
+#include <Swiften/Elements/VCardUpdate.h>
+#include <Swiften/MUC/MUCRegistry.h>
+#include <Swiften/Queries/IQRouter.h>
+#include <Swiften/StringCodecs/Hexify.h>
+#include <Swiften/VCards/VCardManager.h>
+#include <Swiften/VCards/VCardMemoryStorage.h>
using namespace Swift;
class VCardUpdateAvatarManagerTest : public CppUnit::TestFixture {
- CPPUNIT_TEST_SUITE(VCardUpdateAvatarManagerTest);
- CPPUNIT_TEST(testUpdate_NewHashNewVCardRequestsVCard);
- CPPUNIT_TEST(testUpdate_NewHashStoresAvatarAndEmitsNotificationOnVCardReceive);
- CPPUNIT_TEST(testUpdate_KnownHash);
- CPPUNIT_TEST(testUpdate_KnownHashFromDifferentUserDoesNotRequestVCardButTriggersNotification);
- CPPUNIT_TEST(testVCardWithEmptyPhoto);
- CPPUNIT_TEST(testStanzaChannelReset_ClearsHash);
- CPPUNIT_TEST(testStanzaChannelReset_ReceiveHashAfterResetUpdatesHash);
- CPPUNIT_TEST_SUITE_END();
-
- public:
- void setUp() {
- crypto = boost::shared_ptr<CryptoProvider>(PlatformCryptoProvider::create());
- ownJID = JID("foo@fum.com/bum");
- stanzaChannel = new DummyStanzaChannel();
- stanzaChannel->setAvailable(true);
- iqRouter = new IQRouter(stanzaChannel);
- mucRegistry = new DummyMUCRegistry();
- avatarStorage = new AvatarMemoryStorage();
- vcardStorage = new VCardMemoryStorage(crypto.get());
- vcardManager = new VCardManager(ownJID, iqRouter, vcardStorage);
- avatar1 = createByteArray("abcdefg");
- avatar1Hash = Hexify::hexify(crypto->getSHA1Hash(avatar1));
- user1 = JID("user1@bar.com/bla");
- user2 = JID("user2@foo.com/baz");
- }
-
-
- void tearDown() {
- delete vcardManager;
- delete vcardStorage;
- delete avatarStorage;
- delete mucRegistry;
- delete iqRouter;
- delete stanzaChannel;
- }
-
- void testUpdate_NewHashNewVCardRequestsVCard() {
- boost::shared_ptr<VCardUpdateAvatarManager> testling = createManager();
- stanzaChannel->onPresenceReceived(createPresenceWithPhotoHash(user1, avatar1Hash));
-
- CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(stanzaChannel->sentStanzas.size()));
- CPPUNIT_ASSERT(stanzaChannel->isRequestAtIndex<VCard>(0, user1.toBare(), IQ::Get));
- }
-
- void testUpdate_NewHashStoresAvatarAndEmitsNotificationOnVCardReceive() {
- boost::shared_ptr<VCardUpdateAvatarManager> testling = createManager();
- stanzaChannel->onPresenceReceived(createPresenceWithPhotoHash(user1, avatar1Hash));
- stanzaChannel->onIQReceived(createVCardResult(avatar1));
-
- CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changes.size()));
- CPPUNIT_ASSERT_EQUAL(user1.toBare(), changes[0]);
- boost::optional<std::string> hash = testling->getAvatarHash(user1.toBare());
- CPPUNIT_ASSERT(hash);
- CPPUNIT_ASSERT_EQUAL(avatar1Hash, *hash);
- CPPUNIT_ASSERT(avatarStorage->hasAvatar(avatar1Hash));
- CPPUNIT_ASSERT_EQUAL(avatar1, avatarStorage->getAvatar(avatar1Hash));
- }
-
- void testUpdate_KnownHash() {
- boost::shared_ptr<VCardUpdateAvatarManager> testling = createManager();
- stanzaChannel->onPresenceReceived(createPresenceWithPhotoHash(user1, avatar1Hash));
- stanzaChannel->onIQReceived(createVCardResult(avatar1));
- changes.clear();
- stanzaChannel->sentStanzas.clear();
-
- stanzaChannel->onPresenceReceived(createPresenceWithPhotoHash(user1, avatar1Hash));
-
- CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(stanzaChannel->sentStanzas.size()));
- CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(changes.size()));
- }
-
- void testUpdate_KnownHashFromDifferentUserDoesNotRequestVCardButTriggersNotification() {
- boost::shared_ptr<VCardUpdateAvatarManager> testling = createManager();
- stanzaChannel->onPresenceReceived(createPresenceWithPhotoHash(user1, avatar1Hash));
- stanzaChannel->onIQReceived(createVCardResult(avatar1));
- changes.clear();
- stanzaChannel->sentStanzas.clear();
-
- stanzaChannel->onPresenceReceived(createPresenceWithPhotoHash(user2, avatar1Hash));
-
- CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(stanzaChannel->sentStanzas.size()));
- CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changes.size()));
- CPPUNIT_ASSERT_EQUAL(user2.toBare(), changes[0]);
- boost::optional<std::string> hash = testling->getAvatarHash(user2.toBare());
- CPPUNIT_ASSERT(hash);
- CPPUNIT_ASSERT_EQUAL(avatar1Hash, *hash);
- }
-
- void testVCardWithEmptyPhoto() {
- boost::shared_ptr<VCardUpdateAvatarManager> testling = createManager();
- vcardManager->requestVCard(JID("foo@bar.com"));
- stanzaChannel->onIQReceived(createVCardResult(ByteArray()));
-
- CPPUNIT_ASSERT(!avatarStorage->hasAvatar(Hexify::hexify(crypto->getSHA1Hash(ByteArray()))));
- boost::optional<std::string> hash = testling->getAvatarHash(JID("foo@bar.com"));
- CPPUNIT_ASSERT(hash);
- CPPUNIT_ASSERT_EQUAL(std::string(), *hash);
- }
-
- void testStanzaChannelReset_ClearsHash() {
- boost::shared_ptr<VCardUpdateAvatarManager> testling = createManager();
- stanzaChannel->onPresenceReceived(createPresenceWithPhotoHash(user1, avatar1Hash));
- stanzaChannel->onIQReceived(createVCardResult(avatar1));
- changes.clear();
- stanzaChannel->sentStanzas.clear();
-
- stanzaChannel->setAvailable(false);
- stanzaChannel->setAvailable(true);
-
- CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changes.size()));
- CPPUNIT_ASSERT_EQUAL(user1.toBare(), changes[0]);
- boost::optional<std::string> hash = testling->getAvatarHash(user1.toBare());
- CPPUNIT_ASSERT(!hash);
- //CPPUNIT_ASSERT_EQUAL(std::string(""), *hash);
- }
-
- void testStanzaChannelReset_ReceiveHashAfterResetUpdatesHash() {
- boost::shared_ptr<VCardUpdateAvatarManager> testling = createManager();
- stanzaChannel->onPresenceReceived(createPresenceWithPhotoHash(user1, avatar1Hash));
- stanzaChannel->onIQReceived(createVCardResult(avatar1));
- changes.clear();
- stanzaChannel->sentStanzas.clear();
-
- stanzaChannel->setAvailable(false);
- stanzaChannel->setAvailable(true);
- stanzaChannel->onPresenceReceived(createPresenceWithPhotoHash(user1, avatar1Hash));
-
- CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(changes.size()));
- CPPUNIT_ASSERT_EQUAL(user1.toBare(), changes[1]);
- boost::optional<std::string> hash = testling->getAvatarHash(user1.toBare());
- CPPUNIT_ASSERT(hash);
- CPPUNIT_ASSERT_EQUAL(avatar1Hash, *hash);
- }
-
- private:
- boost::shared_ptr<VCardUpdateAvatarManager> createManager() {
- boost::shared_ptr<VCardUpdateAvatarManager> result(new VCardUpdateAvatarManager(vcardManager, stanzaChannel, avatarStorage, crypto.get(), mucRegistry));
- result->onAvatarChanged.connect(boost::bind(&VCardUpdateAvatarManagerTest::handleAvatarChanged, this, _1));
- return result;
- }
-
- boost::shared_ptr<Presence> createPresenceWithPhotoHash(const JID& jid, const std::string& hash) {
- boost::shared_ptr<Presence> presence(new Presence());
- presence->setFrom(jid);
- presence->addPayload(boost::make_shared<VCardUpdate>(hash));
- return presence;
- }
-
- IQ::ref createVCardResult(const ByteArray& avatar) {
- VCard::ref vcard(new VCard());
- if (!avatar.empty()) {
- vcard->setPhoto(avatar);
- }
- return IQ::createResult(JID("baz@fum.com"), stanzaChannel->sentStanzas[0]->getTo(), stanzaChannel->sentStanzas[0]->getID(), vcard);
- }
-
- void handleAvatarChanged(const JID& jid) {
- changes.push_back(jid);
- }
-
- private:
- struct DummyMUCRegistry : public MUCRegistry {
- bool isMUC(const JID& jid) const { return std::find(mucs_.begin(), mucs_.end(), jid) != mucs_.end(); }
- std::vector<JID> mucs_;
- };
-
- JID ownJID;
- DummyStanzaChannel* stanzaChannel;
- IQRouter* iqRouter;
- DummyMUCRegistry* mucRegistry;
- AvatarMemoryStorage* avatarStorage;
- VCardManager* vcardManager;
- VCardMemoryStorage* vcardStorage;
- ByteArray avatar1;
- std::string avatar1Hash;
- std::vector<JID> changes;
- JID user1;
- JID user2;
- boost::shared_ptr<CryptoProvider> crypto;
+ CPPUNIT_TEST_SUITE(VCardUpdateAvatarManagerTest);
+ CPPUNIT_TEST(testUpdate_NewHashNewVCardRequestsVCard);
+ CPPUNIT_TEST(testUpdate_NewHashStoresAvatarAndEmitsNotificationOnVCardReceive);
+ CPPUNIT_TEST(testUpdate_KnownHash);
+ CPPUNIT_TEST(testUpdate_KnownHashFromDifferentUserDoesNotRequestVCardButTriggersNotification);
+ CPPUNIT_TEST(testVCardWithEmptyPhoto);
+ CPPUNIT_TEST(testStanzaChannelReset_ClearsHash);
+ CPPUNIT_TEST(testStanzaChannelReset_ReceiveHashAfterResetUpdatesHash);
+ CPPUNIT_TEST_SUITE_END();
+
+ public:
+ void setUp() {
+ crypto = std::shared_ptr<CryptoProvider>(PlatformCryptoProvider::create());
+ ownJID = JID("foo@fum.com/bum");
+ stanzaChannel = new DummyStanzaChannel();
+ stanzaChannel->setAvailable(true);
+ iqRouter = new IQRouter(stanzaChannel);
+ mucRegistry = new DummyMUCRegistry();
+ avatarStorage = new AvatarMemoryStorage();
+ vcardStorage = new VCardMemoryStorage(crypto.get());
+ vcardManager = new VCardManager(ownJID, iqRouter, vcardStorage);
+ avatar1 = createByteArray("abcdefg");
+ avatar1Hash = Hexify::hexify(crypto->getSHA1Hash(avatar1));
+ user1 = JID("user1@bar.com/bla");
+ user2 = JID("user2@foo.com/baz");
+ }
+
+
+ void tearDown() {
+ delete vcardManager;
+ delete vcardStorage;
+ delete avatarStorage;
+ delete mucRegistry;
+ delete iqRouter;
+ delete stanzaChannel;
+ }
+
+ void testUpdate_NewHashNewVCardRequestsVCard() {
+ std::shared_ptr<VCardUpdateAvatarManager> testling = createManager();
+ stanzaChannel->onPresenceReceived(createPresenceWithPhotoHash(user1, avatar1Hash));
+
+ CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(stanzaChannel->sentStanzas.size()));
+ CPPUNIT_ASSERT(stanzaChannel->isRequestAtIndex<VCard>(0, user1.toBare(), IQ::Get));
+ }
+
+ void testUpdate_NewHashStoresAvatarAndEmitsNotificationOnVCardReceive() {
+ std::shared_ptr<VCardUpdateAvatarManager> testling = createManager();
+ stanzaChannel->onPresenceReceived(createPresenceWithPhotoHash(user1, avatar1Hash));
+ stanzaChannel->onIQReceived(createVCardResult(avatar1));
+
+ CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changes.size()));
+ CPPUNIT_ASSERT_EQUAL(user1.toBare(), changes[0]);
+ boost::optional<std::string> hash = testling->getAvatarHash(user1.toBare());
+ CPPUNIT_ASSERT(hash);
+ CPPUNIT_ASSERT_EQUAL(avatar1Hash, *hash);
+ CPPUNIT_ASSERT(avatarStorage->hasAvatar(avatar1Hash));
+ CPPUNIT_ASSERT_EQUAL(avatar1, avatarStorage->getAvatar(avatar1Hash));
+ }
+
+ void testUpdate_KnownHash() {
+ std::shared_ptr<VCardUpdateAvatarManager> testling = createManager();
+ stanzaChannel->onPresenceReceived(createPresenceWithPhotoHash(user1, avatar1Hash));
+ stanzaChannel->onIQReceived(createVCardResult(avatar1));
+ changes.clear();
+ stanzaChannel->sentStanzas.clear();
+
+ stanzaChannel->onPresenceReceived(createPresenceWithPhotoHash(user1, avatar1Hash));
+
+ CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(stanzaChannel->sentStanzas.size()));
+ CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(changes.size()));
+ }
+
+ void testUpdate_KnownHashFromDifferentUserDoesNotRequestVCardButTriggersNotification() {
+ std::shared_ptr<VCardUpdateAvatarManager> testling = createManager();
+ stanzaChannel->onPresenceReceived(createPresenceWithPhotoHash(user1, avatar1Hash));
+ stanzaChannel->onIQReceived(createVCardResult(avatar1));
+ changes.clear();
+ stanzaChannel->sentStanzas.clear();
+
+ stanzaChannel->onPresenceReceived(createPresenceWithPhotoHash(user2, avatar1Hash));
+
+ CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(stanzaChannel->sentStanzas.size()));
+ CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changes.size()));
+ CPPUNIT_ASSERT_EQUAL(user2.toBare(), changes[0]);
+ boost::optional<std::string> hash = testling->getAvatarHash(user2.toBare());
+ CPPUNIT_ASSERT(hash);
+ CPPUNIT_ASSERT_EQUAL(avatar1Hash, *hash);
+ }
+
+ void testVCardWithEmptyPhoto() {
+ std::shared_ptr<VCardUpdateAvatarManager> testling = createManager();
+ vcardManager->requestVCard(JID("foo@bar.com"));
+ stanzaChannel->onIQReceived(createVCardResult(ByteArray()));
+
+ CPPUNIT_ASSERT(!avatarStorage->hasAvatar(Hexify::hexify(crypto->getSHA1Hash(ByteArray()))));
+ boost::optional<std::string> hash = testling->getAvatarHash(JID("foo@bar.com"));
+ CPPUNIT_ASSERT(hash);
+ CPPUNIT_ASSERT_EQUAL(std::string(), *hash);
+ }
+
+ void testStanzaChannelReset_ClearsHash() {
+ std::shared_ptr<VCardUpdateAvatarManager> testling = createManager();
+ stanzaChannel->onPresenceReceived(createPresenceWithPhotoHash(user1, avatar1Hash));
+ stanzaChannel->onIQReceived(createVCardResult(avatar1));
+ changes.clear();
+ stanzaChannel->sentStanzas.clear();
+
+ stanzaChannel->setAvailable(false);
+ stanzaChannel->setAvailable(true);
+
+ CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changes.size()));
+ CPPUNIT_ASSERT_EQUAL(user1.toBare(), changes[0]);
+ boost::optional<std::string> hash = testling->getAvatarHash(user1.toBare());
+ CPPUNIT_ASSERT(!hash);
+ //CPPUNIT_ASSERT_EQUAL(std::string(""), *hash);
+ }
+
+ void testStanzaChannelReset_ReceiveHashAfterResetUpdatesHash() {
+ std::shared_ptr<VCardUpdateAvatarManager> testling = createManager();
+ stanzaChannel->onPresenceReceived(createPresenceWithPhotoHash(user1, avatar1Hash));
+ stanzaChannel->onIQReceived(createVCardResult(avatar1));
+ changes.clear();
+ stanzaChannel->sentStanzas.clear();
+
+ stanzaChannel->setAvailable(false);
+ stanzaChannel->setAvailable(true);
+ stanzaChannel->onPresenceReceived(createPresenceWithPhotoHash(user1, avatar1Hash));
+
+ CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(changes.size()));
+ CPPUNIT_ASSERT_EQUAL(user1.toBare(), changes[1]);
+ boost::optional<std::string> hash = testling->getAvatarHash(user1.toBare());
+ CPPUNIT_ASSERT(hash);
+ CPPUNIT_ASSERT_EQUAL(avatar1Hash, *hash);
+ }
+
+ private:
+ std::shared_ptr<VCardUpdateAvatarManager> createManager() {
+ std::shared_ptr<VCardUpdateAvatarManager> result(new VCardUpdateAvatarManager(vcardManager, stanzaChannel, avatarStorage, crypto.get(), mucRegistry));
+ result->onAvatarChanged.connect(boost::bind(&VCardUpdateAvatarManagerTest::handleAvatarChanged, this, _1));
+ return result;
+ }
+
+ std::shared_ptr<Presence> createPresenceWithPhotoHash(const JID& jid, const std::string& hash) {
+ std::shared_ptr<Presence> presence(new Presence());
+ presence->setFrom(jid);
+ presence->addPayload(std::make_shared<VCardUpdate>(hash));
+ return presence;
+ }
+
+ IQ::ref createVCardResult(const ByteArray& avatar) {
+ VCard::ref vcard(new VCard());
+ if (!avatar.empty()) {
+ vcard->setPhoto(avatar);
+ }
+ return IQ::createResult(JID("baz@fum.com"), stanzaChannel->sentStanzas[0]->getTo(), stanzaChannel->sentStanzas[0]->getID(), vcard);
+ }
+
+ void handleAvatarChanged(const JID& jid) {
+ changes.push_back(jid);
+ }
+
+ private:
+ struct DummyMUCRegistry : public MUCRegistry {
+ bool isMUC(const JID& jid) const { return std::find(mucs_.begin(), mucs_.end(), jid) != mucs_.end(); }
+ std::vector<JID> mucs_;
+ };
+
+ JID ownJID;
+ DummyStanzaChannel* stanzaChannel;
+ IQRouter* iqRouter;
+ DummyMUCRegistry* mucRegistry;
+ AvatarMemoryStorage* avatarStorage;
+ VCardManager* vcardManager;
+ VCardMemoryStorage* vcardStorage;
+ ByteArray avatar1;
+ std::string avatar1Hash;
+ std::vector<JID> changes;
+ JID user1;
+ JID user2;
+ std::shared_ptr<CryptoProvider> crypto;
};
CPPUNIT_TEST_SUITE_REGISTRATION(VCardUpdateAvatarManagerTest);
diff --git a/Swiften/Avatars/VCardAvatarManager.cpp b/Swiften/Avatars/VCardAvatarManager.cpp
index 4960900..919d833 100644
--- a/Swiften/Avatars/VCardAvatarManager.cpp
+++ b/Swiften/Avatars/VCardAvatarManager.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2010-2013 Isode Limited.
+ * Copyright (c) 2010-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
@@ -8,55 +8,55 @@
#include <boost/bind.hpp>
-#include <Swiften/Elements/VCard.h>
-#include <Swiften/Crypto/CryptoProvider.h>
-#include <Swiften/StringCodecs/Hexify.h>
#include <Swiften/Avatars/AvatarStorage.h>
+#include <Swiften/Base/Log.h>
+#include <Swiften/Crypto/CryptoProvider.h>
+#include <Swiften/Elements/VCard.h>
#include <Swiften/MUC/MUCRegistry.h>
+#include <Swiften/StringCodecs/Hexify.h>
#include <Swiften/VCards/VCardManager.h>
-#include <Swiften/Base/Log.h>
namespace Swift {
VCardAvatarManager::VCardAvatarManager(VCardManager* vcardManager, AvatarStorage* avatarStorage, CryptoProvider* crypto, MUCRegistry* mucRegistry) : vcardManager_(vcardManager), avatarStorage_(avatarStorage), crypto_(crypto), mucRegistry_(mucRegistry) {
- vcardManager_->onVCardChanged.connect(boost::bind(&VCardAvatarManager::handleVCardChanged, this, _1));
+ vcardManager_->onVCardChanged.connect(boost::bind(&VCardAvatarManager::handleVCardChanged, this, _1));
}
void VCardAvatarManager::handleVCardChanged(const JID& from) {
- // We don't check whether the avatar actually changed. Direct use of this
- // manager could cause unnecessary updates, but in practice, this will be
- // caught by the wrapping CombinedAvatarManager anyway.
- onAvatarChanged(from);
+ // We don't check whether the avatar actually changed. Direct use of this
+ // manager could cause unnecessary updates, but in practice, this will be
+ // caught by the wrapping CombinedAvatarManager anyway.
+ onAvatarChanged(from);
}
boost::optional<std::string> VCardAvatarManager::getAvatarHash(const JID& jid) const {
- JID avatarJID = getAvatarJID(jid);
- std::string hash = vcardManager_->getPhotoHash(avatarJID);
- if (!hash.empty()) {
- if (!avatarStorage_->hasAvatar(hash)) {
- VCard::ref vCard = vcardManager_->getVCard(avatarJID);
- if (vCard) {
- std::string newHash = Hexify::hexify(crypto_->getSHA1Hash(vCard->getPhoto()));
- if (newHash != hash) {
- // Shouldn't happen, but sometimes seem to. Might be fixed if we
- // move to a safer backend.
- SWIFT_LOG(warning) << "Inconsistent vCard photo hash cache";
- hash = newHash;
- }
- avatarStorage_->addAvatar(hash, vCard->getPhoto());
- }
- else {
- // Can happen if the cache is inconsistent.
- hash = "";
- }
- }
- }
- return hash;
+ JID avatarJID = getAvatarJID(jid);
+ std::string hash = vcardManager_->getPhotoHash(avatarJID);
+ if (!hash.empty()) {
+ if (!avatarStorage_->hasAvatar(hash)) {
+ VCard::ref vCard = vcardManager_->getVCard(avatarJID);
+ if (vCard) {
+ std::string newHash = Hexify::hexify(crypto_->getSHA1Hash(vCard->getPhoto()));
+ if (newHash != hash) {
+ // Shouldn't happen, but sometimes seem to. Might be fixed if we
+ // move to a safer backend.
+ SWIFT_LOG(warning) << "Inconsistent vCard photo hash cache";
+ hash = newHash;
+ }
+ avatarStorage_->addAvatar(hash, vCard->getPhoto());
+ }
+ else {
+ // Can happen if the cache is inconsistent.
+ hash = "";
+ }
+ }
+ }
+ return hash;
}
JID VCardAvatarManager::getAvatarJID(const JID& jid) const {
- JID bareFrom = jid.toBare();
- return (mucRegistry_ && mucRegistry_->isMUC(bareFrom)) ? jid : bareFrom;
+ JID bareFrom = jid.toBare();
+ return (mucRegistry_ && mucRegistry_->isMUC(bareFrom)) ? jid : bareFrom;
}
}
diff --git a/Swiften/Avatars/VCardAvatarManager.h b/Swiften/Avatars/VCardAvatarManager.h
index 74f36a1..70df6a8 100644
--- a/Swiften/Avatars/VCardAvatarManager.h
+++ b/Swiften/Avatars/VCardAvatarManager.h
@@ -1,35 +1,35 @@
/*
- * Copyright (c) 2010-2013 Isode Limited.
+ * Copyright (c) 2010-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
#pragma once
-#include <Swiften/Base/API.h>
#include <Swiften/Avatars/AvatarProvider.h>
+#include <Swiften/Base/API.h>
#include <Swiften/JID/JID.h>
namespace Swift {
- class MUCRegistry;
- class AvatarStorage;
- class VCardManager;
- class CryptoProvider;
+ class MUCRegistry;
+ class AvatarStorage;
+ class VCardManager;
+ class CryptoProvider;
- class SWIFTEN_API VCardAvatarManager : public AvatarProvider {
- public:
- VCardAvatarManager(VCardManager*, AvatarStorage*, CryptoProvider* crypto, MUCRegistry* = NULL);
+ class SWIFTEN_API VCardAvatarManager : public AvatarProvider {
+ public:
+ VCardAvatarManager(VCardManager*, AvatarStorage*, CryptoProvider* crypto, MUCRegistry* = nullptr);
- boost::optional<std::string> getAvatarHash(const JID&) const;
+ boost::optional<std::string> getAvatarHash(const JID&) const;
- private:
- void handleVCardChanged(const JID& from);
- JID getAvatarJID(const JID& o) const;
+ private:
+ void handleVCardChanged(const JID& from);
+ JID getAvatarJID(const JID& o) const;
- private:
- VCardManager* vcardManager_;
- AvatarStorage* avatarStorage_;
- CryptoProvider* crypto_;
- MUCRegistry* mucRegistry_;
- };
+ private:
+ VCardManager* vcardManager_;
+ AvatarStorage* avatarStorage_;
+ CryptoProvider* crypto_;
+ MUCRegistry* mucRegistry_;
+ };
}
diff --git a/Swiften/Avatars/VCardUpdateAvatarManager.cpp b/Swiften/Avatars/VCardUpdateAvatarManager.cpp
index a21cd41..3e8d87b 100644
--- a/Swiften/Avatars/VCardUpdateAvatarManager.cpp
+++ b/Swiften/Avatars/VCardUpdateAvatarManager.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2010-2014 Isode Limited.
+ * Copyright (c) 2010-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
@@ -8,97 +8,97 @@
#include <boost/bind.hpp>
+#include <Swiften/Avatars/AvatarStorage.h>
+#include <Swiften/Base/Log.h>
#include <Swiften/Client/StanzaChannel.h>
-#include <Swiften/Elements/VCardUpdate.h>
-#include <Swiften/VCards/GetVCardRequest.h>
#include <Swiften/Crypto/CryptoProvider.h>
-#include <Swiften/StringCodecs/Hexify.h>
-#include <Swiften/Avatars/AvatarStorage.h>
+#include <Swiften/Elements/VCardUpdate.h>
#include <Swiften/MUC/MUCRegistry.h>
+#include <Swiften/StringCodecs/Hexify.h>
+#include <Swiften/VCards/GetVCardRequest.h>
#include <Swiften/VCards/VCardManager.h>
-#include <Swiften/Base/Log.h>
namespace Swift {
VCardUpdateAvatarManager::VCardUpdateAvatarManager(VCardManager* vcardManager, StanzaChannel* stanzaChannel, AvatarStorage* avatarStorage, CryptoProvider* crypto, MUCRegistry* mucRegistry) : vcardManager_(vcardManager), avatarStorage_(avatarStorage), crypto_(crypto), mucRegistry_(mucRegistry) {
- stanzaChannel->onPresenceReceived.connect(boost::bind(&VCardUpdateAvatarManager::handlePresenceReceived, this, _1));
- stanzaChannel->onAvailableChanged.connect(boost::bind(&VCardUpdateAvatarManager::handleStanzaChannelAvailableChanged, this, _1));
- vcardManager_->onVCardChanged.connect(boost::bind(&VCardUpdateAvatarManager::handleVCardChanged, this, _1, _2));
+ stanzaChannel->onPresenceReceived.connect(boost::bind(&VCardUpdateAvatarManager::handlePresenceReceived, this, _1));
+ stanzaChannel->onAvailableChanged.connect(boost::bind(&VCardUpdateAvatarManager::handleStanzaChannelAvailableChanged, this, _1));
+ vcardManager_->onVCardChanged.connect(boost::bind(&VCardUpdateAvatarManager::handleVCardChanged, this, _1, _2));
}
-void VCardUpdateAvatarManager::handlePresenceReceived(boost::shared_ptr<Presence> presence) {
- boost::shared_ptr<VCardUpdate> update = presence->getPayload<VCardUpdate>();
- if (!update || presence->getPayload<ErrorPayload>()) {
- return;
- }
- JID from = getAvatarJID(presence->getFrom());
- if (getAvatarHash(from) == update->getPhotoHash()) {
- return;
- }
- SWIFT_LOG(debug) << "Updated hash: " << from << " -> " << update->getPhotoHash() << std::endl;
- if (avatarStorage_->hasAvatar(update->getPhotoHash())) {
- setAvatarHash(from, update->getPhotoHash());
- }
- else {
- vcardManager_->requestVCard(from);
- }
+void VCardUpdateAvatarManager::handlePresenceReceived(std::shared_ptr<Presence> presence) {
+ std::shared_ptr<VCardUpdate> update = presence->getPayload<VCardUpdate>();
+ if (!update || presence->getPayload<ErrorPayload>()) {
+ return;
+ }
+ JID from = getAvatarJID(presence->getFrom());
+ if (getAvatarHash(from) == update->getPhotoHash()) {
+ return;
+ }
+ SWIFT_LOG(debug) << "Updated hash: " << from << " -> " << update->getPhotoHash() << std::endl;
+ if (avatarStorage_->hasAvatar(update->getPhotoHash())) {
+ setAvatarHash(from, update->getPhotoHash());
+ }
+ else {
+ vcardManager_->requestVCard(from);
+ }
}
void VCardUpdateAvatarManager::handleVCardChanged(const JID& from, VCard::ref vCard) {
- if (!vCard) {
- SWIFT_LOG(debug) << "Missing element: " << from << ": null vcard payload" << std::endl;
- return;
- }
+ if (!vCard) {
+ SWIFT_LOG(debug) << "Missing element: " << from << ": null vcard payload" << std::endl;
+ return;
+ }
- if (vCard->getPhoto().empty()) {
- setAvatarHash(from, "");
- }
- else {
- std::string hash = Hexify::hexify(crypto_->getSHA1Hash(vCard->getPhoto()));
- if (!avatarStorage_->hasAvatar(hash)) {
- avatarStorage_->addAvatar(hash, vCard->getPhoto());
- }
- setAvatarHash(from, hash);
- }
+ if (vCard->getPhoto().empty()) {
+ setAvatarHash(from, "");
+ }
+ else {
+ std::string hash = Hexify::hexify(crypto_->getSHA1Hash(vCard->getPhoto()));
+ if (!avatarStorage_->hasAvatar(hash)) {
+ avatarStorage_->addAvatar(hash, vCard->getPhoto());
+ }
+ setAvatarHash(from, hash);
+ }
}
void VCardUpdateAvatarManager::setAvatarHash(const JID& from, const std::string& hash) {
- SWIFT_LOG(debug) << "Updating hash: " << from << " -> " << hash << std::endl;
- avatarHashes_[from] = hash;
- onAvatarChanged(from);
+ SWIFT_LOG(debug) << "Updating hash: " << from << " -> " << hash << std::endl;
+ avatarHashes_[from] = hash;
+ onAvatarChanged(from);
}
/*
void VCardUpdateAvatarManager::setAvatar(const JID& jid, const ByteArray& avatar) {
- std::string hash = Hexify::hexify(SHA1::getHash(avatar));
- avatarStorage_->addAvatar(hash, avatar);
- setAvatarHash(getAvatarJID(jid), hash);
+ std::string hash = Hexify::hexify(SHA1::getHash(avatar));
+ avatarStorage_->addAvatar(hash, avatar);
+ setAvatarHash(getAvatarJID(jid), hash);
}
*/
boost::optional<std::string> VCardUpdateAvatarManager::getAvatarHash(const JID& jid) const {
- std::map<JID, std::string>::const_iterator i = avatarHashes_.find(getAvatarJID(jid));
- if (i != avatarHashes_.end()) {
- return i->second;
- }
- else {
- return boost::optional<std::string>();
- }
+ std::map<JID, std::string>::const_iterator i = avatarHashes_.find(getAvatarJID(jid));
+ if (i != avatarHashes_.end()) {
+ return i->second;
+ }
+ else {
+ return boost::optional<std::string>();
+ }
}
JID VCardUpdateAvatarManager::getAvatarJID(const JID& jid) const {
- JID bareFrom = jid.toBare();
- return (mucRegistry_ && mucRegistry_->isMUC(bareFrom)) ? jid : bareFrom;
+ JID bareFrom = jid.toBare();
+ return (mucRegistry_ && mucRegistry_->isMUC(bareFrom)) ? jid : bareFrom;
}
void VCardUpdateAvatarManager::handleStanzaChannelAvailableChanged(bool available) {
- if (available) {
- std::map<JID, std::string> oldAvatarHashes;
- avatarHashes_.swap(oldAvatarHashes);
- for(std::map<JID, std::string>::const_iterator i = oldAvatarHashes.begin(); i != oldAvatarHashes.end(); ++i) {
- onAvatarChanged(i->first);
- }
- }
+ if (available) {
+ std::map<JID, std::string> oldAvatarHashes;
+ avatarHashes_.swap(oldAvatarHashes);
+ for(std::map<JID, std::string>::const_iterator i = oldAvatarHashes.begin(); i != oldAvatarHashes.end(); ++i) {
+ onAvatarChanged(i->first);
+ }
+ }
}
diff --git a/Swiften/Avatars/VCardUpdateAvatarManager.h b/Swiften/Avatars/VCardUpdateAvatarManager.h
index 3d6d73a..19ed672 100644
--- a/Swiften/Avatars/VCardUpdateAvatarManager.h
+++ b/Swiften/Avatars/VCardUpdateAvatarManager.h
@@ -1,46 +1,46 @@
/*
- * Copyright (c) 2010-2013 Isode Limited.
+ * Copyright (c) 2010-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
#pragma once
-#include <boost/shared_ptr.hpp>
#include <map>
+#include <memory>
-#include <Swiften/Base/API.h>
#include <Swiften/Avatars/AvatarProvider.h>
-#include <Swiften/JID/JID.h>
+#include <Swiften/Base/API.h>
+#include <Swiften/Elements/ErrorPayload.h>
#include <Swiften/Elements/Presence.h>
#include <Swiften/Elements/VCard.h>
-#include <Swiften/Elements/ErrorPayload.h>
+#include <Swiften/JID/JID.h>
namespace Swift {
- class MUCRegistry;
- class AvatarStorage;
- class StanzaChannel;
- class VCardManager;
- class CryptoProvider;
-
- class SWIFTEN_API VCardUpdateAvatarManager : public AvatarProvider, public boost::bsignals::trackable {
- public:
- VCardUpdateAvatarManager(VCardManager*, StanzaChannel*, AvatarStorage*, CryptoProvider* crypto, MUCRegistry* = NULL);
-
- boost::optional<std::string> getAvatarHash(const JID&) const;
-
- private:
- void handlePresenceReceived(boost::shared_ptr<Presence>);
- void handleStanzaChannelAvailableChanged(bool);
- void handleVCardChanged(const JID& from, VCard::ref);
- void setAvatarHash(const JID& from, const std::string& hash);
- JID getAvatarJID(const JID& o) const;
-
- private:
- VCardManager* vcardManager_;
- AvatarStorage* avatarStorage_;
- CryptoProvider* crypto_;
- MUCRegistry* mucRegistry_;
- std::map<JID, std::string> avatarHashes_;
- };
+ class MUCRegistry;
+ class AvatarStorage;
+ class StanzaChannel;
+ class VCardManager;
+ class CryptoProvider;
+
+ class SWIFTEN_API VCardUpdateAvatarManager : public AvatarProvider, public boost::signals2::trackable {
+ public:
+ VCardUpdateAvatarManager(VCardManager*, StanzaChannel*, AvatarStorage*, CryptoProvider* crypto, MUCRegistry* = nullptr);
+
+ boost::optional<std::string> getAvatarHash(const JID&) const;
+
+ private:
+ void handlePresenceReceived(std::shared_ptr<Presence>);
+ void handleStanzaChannelAvailableChanged(bool);
+ void handleVCardChanged(const JID& from, VCard::ref);
+ void setAvatarHash(const JID& from, const std::string& hash);
+ JID getAvatarJID(const JID& o) const;
+
+ private:
+ VCardManager* vcardManager_;
+ AvatarStorage* avatarStorage_;
+ CryptoProvider* crypto_;
+ MUCRegistry* mucRegistry_;
+ std::map<JID, std::string> avatarHashes_;
+ };
}