From 10d8ba50a9e09517cfe4a6d4c3d51a768e989125 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Thu, 9 Sep 2010 13:04:52 +0200
Subject: Refactoring VCardUpdateAvatarManager.


diff --git a/Swift/Controllers/Chat/MUCController.cpp b/Swift/Controllers/Chat/MUCController.cpp
index 3b37179..3031efe 100644
--- a/Swift/Controllers/Chat/MUCController.cpp
+++ b/Swift/Controllers/Chat/MUCController.cpp
@@ -75,7 +75,7 @@ MUCController::MUCController (
 	chatWindow_->addSystemMessage("Trying to join room " + toJID_.toString());
 	rejoin();
 	if (avatarManager_ != NULL) {
-		avatarChangedConnection_ = (avatarManager_->onAvatarChanged.connect(boost::bind(&MUCController::handleAvatarChanged, this, _1, _2)));
+		avatarChangedConnection_ = (avatarManager_->onAvatarChanged.connect(boost::bind(&MUCController::handleAvatarChanged, this, _1)));
 	} 
 }
 
@@ -148,7 +148,7 @@ void MUCController::handleJoinComplete(const String& nick) {
 	setEnabled(true);
 }
 
-void MUCController::handleAvatarChanged(const JID& jid, const String&) {
+void MUCController::handleAvatarChanged(const JID& jid) {
 	if (parting_ || !jid.equals(toJID_, JID::WithoutResource)) {
 		return;
 	}
@@ -192,7 +192,7 @@ void MUCController::handleOccupantJoined(const MUCOccupant& occupant) {
 		}
 	}
 	if (avatarManager_ != NULL) {
-		handleAvatarChanged(jid, "dummy");
+		handleAvatarChanged(jid);
 	}
 }
 
diff --git a/Swift/Controllers/Chat/MUCController.h b/Swift/Controllers/Chat/MUCController.h
index 4601386..c922e83 100644
--- a/Swift/Controllers/Chat/MUCController.h
+++ b/Swift/Controllers/Chat/MUCController.h
@@ -60,7 +60,7 @@ namespace Swift {
 			void clearPresenceQueue();
 			void addPresenceMessage(const String& message);
 			void handleWindowClosed();
-			void handleAvatarChanged(const JID& jid, const String&);
+			void handleAvatarChanged(const JID& jid);
 			void handleOccupantJoined(const MUCOccupant& occupant);
 			void handleOccupantLeft(const MUCOccupant& occupant, MUC::LeavingType type, const String& reason);
 			void handleOccupantPresenceChange(boost::shared_ptr<Presence> presence);
diff --git a/Swift/Controllers/RosterController.cpp b/Swift/Controllers/RosterController.cpp
index 87b0b74..b4626fb 100644
--- a/Swift/Controllers/RosterController.cpp
+++ b/Swift/Controllers/RosterController.cpp
@@ -83,7 +83,7 @@ void RosterController::setAvatarManager(AvatarManager* avatarManager) {
 	}
 	avatarManager_ = avatarManager;
 	if (avatarManager != NULL) {
-		avatarManager_->onAvatarChanged.connect(boost::bind(&RosterController::handleAvatarChanged, this, _1, _2));
+		avatarManager_->onAvatarChanged.connect(boost::bind(&RosterController::handleAvatarChanged, this, _1));
 		mainWindow_->setMyAvatarPath(avatarManager_->getAvatarPath(myJID_).string());
 	}
 }
@@ -221,7 +221,7 @@ void RosterController::handleSubscriptionRequestDeclined(SubscriptionRequestEven
 	presenceOracle_->cancelSubscription(event->getJID());
 }
 
-void RosterController::handleAvatarChanged(const JID& jid, const String&) {
+void RosterController::handleAvatarChanged(const JID& jid) {
 	String path = avatarManager_->getAvatarPath(jid).string();
 	roster_->applyOnItems(SetAvatar(jid, path));
 	if (jid.equals(myJID_, JID::WithoutResource)) {
diff --git a/Swift/Controllers/RosterController.h b/Swift/Controllers/RosterController.h
index 7e2b3da..dea1f0f 100644
--- a/Swift/Controllers/RosterController.h
+++ b/Swift/Controllers/RosterController.h
@@ -41,7 +41,7 @@ namespace Swift {
 			void setNickResolver(NickResolver* nickResolver);
 			boost::signal<void (StatusShow::Type, const String&)> onChangeStatusRequest;
 			boost::signal<void ()> onSignOutRequest;
-			void handleAvatarChanged(const JID& jid, const String& hash);
+			void handleAvatarChanged(const JID& jid);
 			void setEnabled(bool enabled);
 		private:
 			void handleOnJIDAdded(const JID &jid);
diff --git a/Swiften/Avatars/AvatarManager.cpp b/Swiften/Avatars/AvatarManager.cpp
index cd691c0..9c3255d 100644
--- a/Swiften/Avatars/AvatarManager.cpp
+++ b/Swiften/Avatars/AvatarManager.cpp
@@ -9,10 +9,11 @@
 #include <boost/bind.hpp>
 
 #include "Swiften/Avatars/VCardUpdateAvatarManager.h"
+#include "Swiften/Avatars/AvatarStorage.h"
 
 namespace Swift {
 
-AvatarManager::AvatarManager(VCardManager* vcardManager, StanzaChannel* stanzaChannel, AvatarStorage* avatarStorage, MUCRegistry* mucRegistry) {
+AvatarManager::AvatarManager(VCardManager* vcardManager, StanzaChannel* stanzaChannel, AvatarStorage* avatarStorage, MUCRegistry* mucRegistry) : avatarStorage(avatarStorage) {
 	vcardUpdateAvatarManager = new VCardUpdateAvatarManager(vcardManager, stanzaChannel, avatarStorage, mucRegistry);
 	vcardUpdateAvatarManager->onAvatarChanged.connect(boost::ref(onAvatarChanged));
 }
@@ -22,7 +23,11 @@ AvatarManager::~AvatarManager() {
 }
 
 boost::filesystem::path AvatarManager::getAvatarPath(const JID& jid) const {
-	return vcardUpdateAvatarManager->getAvatarPath(jid);
+	String hash = vcardUpdateAvatarManager->getAvatarHash(jid);
+	if (!hash.isEmpty()) {
+		return avatarStorage->getAvatarPath(hash);
+	}
+	return boost::filesystem::path();
 }
 
 
diff --git a/Swiften/Avatars/AvatarManager.h b/Swiften/Avatars/AvatarManager.h
index 90f0076..d3cdbae 100644
--- a/Swiften/Avatars/AvatarManager.h
+++ b/Swiften/Avatars/AvatarManager.h
@@ -32,9 +32,10 @@ namespace Swift {
 			virtual boost::filesystem::path getAvatarPath(const JID&) const;
 
 		public:
-			boost::signal<void (const JID&, const String& /*hash*/)> onAvatarChanged;
+			boost::signal<void (const JID&)> onAvatarChanged;
 
 		private:
 			VCardUpdateAvatarManager* vcardUpdateAvatarManager;
+			AvatarStorage* avatarStorage;
 	};
 }
diff --git a/Swiften/Avatars/AvatarProvider.cpp b/Swiften/Avatars/AvatarProvider.cpp
new file mode 100644
index 0000000..680b58d
--- /dev/null
+++ b/Swiften/Avatars/AvatarProvider.cpp
@@ -0,0 +1,14 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include "Swiften/Avatars/AvatarProvider.h"
+
+namespace Swift {
+
+AvatarProvider::~AvatarProvider() {
+}
+
+}
diff --git a/Swiften/Avatars/AvatarProvider.h b/Swiften/Avatars/AvatarProvider.h
new file mode 100644
index 0000000..b953ad3
--- /dev/null
+++ b/Swiften/Avatars/AvatarProvider.h
@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include "Swiften/Base/boost_bsignals.h"
+#include "Swiften/Base/String.h"
+
+namespace Swift {
+	class JID;
+
+	class AvatarProvider {
+		public:
+			virtual ~AvatarProvider();
+
+			virtual String getAvatarHash(const JID&) const = 0;
+
+			boost::signal<void (const JID&)> onAvatarChanged;
+	};
+}
diff --git a/Swiften/Avatars/SConscript b/Swiften/Avatars/SConscript
new file mode 100644
index 0000000..f9ce320
--- /dev/null
+++ b/Swiften/Avatars/SConscript
@@ -0,0 +1,10 @@
+Import("swiften_env")
+
+objects = swiften_env.StaticObject([
+			"AvatarFileStorage.cpp",
+			"VCardUpdateAvatarManager.cpp",
+			"AvatarManager.cpp",
+			"AvatarStorage.cpp",
+			"AvatarProvider.cpp",
+		])
+swiften_env.Append(SWIFTEN_OBJECTS = [objects])
diff --git a/Swiften/Avatars/UnitTest/VCardUpdateAvatarManagerTest.cpp b/Swiften/Avatars/UnitTest/VCardUpdateAvatarManagerTest.cpp
index 5a98a0e..3e8dbdf 100644
--- a/Swiften/Avatars/UnitTest/VCardUpdateAvatarManagerTest.cpp
+++ b/Swiften/Avatars/UnitTest/VCardUpdateAvatarManagerTest.cpp
@@ -70,8 +70,8 @@ class VCardUpdateAvatarManagerTest : public CppUnit::TestFixture {
 			stanzaChannel->onIQReceived(createVCardResult(avatar1));
 
 			CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changes.size()));
-			CPPUNIT_ASSERT_EQUAL(user1.toBare(), changes[0].first);
-			CPPUNIT_ASSERT_EQUAL(avatar1Hash, changes[0].second);
+			CPPUNIT_ASSERT_EQUAL(user1.toBare(), changes[0]);
+			CPPUNIT_ASSERT_EQUAL(avatar1Hash, testling->getAvatarHash(user1.toBare()));
 			CPPUNIT_ASSERT(avatarStorage->hasAvatar(avatar1Hash));
 			CPPUNIT_ASSERT_EQUAL(avatar1, avatarStorage->getAvatar(avatar1Hash));
 		}
@@ -100,8 +100,8 @@ class VCardUpdateAvatarManagerTest : public CppUnit::TestFixture {
 
 			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].first);
-			CPPUNIT_ASSERT_EQUAL(avatar1Hash, changes[0].second);
+			CPPUNIT_ASSERT_EQUAL(user2.toBare(), changes[0]);
+			CPPUNIT_ASSERT_EQUAL(avatar1Hash, testling->getAvatarHash(user2.toBare()));
 		}
 
 		void testVCardWithEmptyPhoto() {
@@ -110,7 +110,7 @@ class VCardUpdateAvatarManagerTest : public CppUnit::TestFixture {
 			stanzaChannel->onIQReceived(createVCardResult(ByteArray()));
 			
 			CPPUNIT_ASSERT(!avatarStorage->hasAvatar(Hexify::hexify(SHA1::getHash(ByteArray()))));
-			CPPUNIT_ASSERT_EQUAL(boost::filesystem::path(), testling->getAvatarPath(JID("foo@bar.com")));
+			CPPUNIT_ASSERT_EQUAL(String(), testling->getAvatarHash(JID("foo@bar.com")));
 		}
 
 		void testStanzaChannelReset() {
@@ -125,14 +125,14 @@ class VCardUpdateAvatarManagerTest : public CppUnit::TestFixture {
 			stanzaChannel->onPresenceReceived(createPresenceWithPhotoHash(user1, avatar1Hash));
 
 			CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changes.size()));
-			CPPUNIT_ASSERT_EQUAL(user1.toBare(), changes[0].first);
-			CPPUNIT_ASSERT_EQUAL(avatar1Hash, changes[0].second);
+			CPPUNIT_ASSERT_EQUAL(user1.toBare(), changes[0]);
+			CPPUNIT_ASSERT_EQUAL(avatar1Hash, testling->getAvatarHash(user1.toBare()));
 		}
 
 	private:
 		std::auto_ptr<VCardUpdateAvatarManager> createManager() {
 			std::auto_ptr<VCardUpdateAvatarManager> result(new VCardUpdateAvatarManager(vcardManager, stanzaChannel, avatarStorage, mucRegistry));
-			result->onAvatarChanged.connect(boost::bind(&VCardUpdateAvatarManagerTest::handleAvatarChanged, this, _1, _2));
+			result->onAvatarChanged.connect(boost::bind(&VCardUpdateAvatarManagerTest::handleAvatarChanged, this, _1));
 			return result;
 		}
 
@@ -151,8 +151,8 @@ class VCardUpdateAvatarManagerTest : public CppUnit::TestFixture {
 			return IQ::createResult(JID("baz@fum.com"), stanzaChannel->sentStanzas[0]->getID(), vcard);
 		}
 
-		void handleAvatarChanged(const JID& jid, const String& hash) {
-			changes.push_back(std::pair<JID,String>(jid, hash));
+		void handleAvatarChanged(const JID& jid) {
+			changes.push_back(jid);
 		}
 
 	private:
@@ -170,7 +170,7 @@ class VCardUpdateAvatarManagerTest : public CppUnit::TestFixture {
 		VCardMemoryStorage* vcardStorage;
 		ByteArray avatar1;
 		String avatar1Hash;
-		std::vector<std::pair<JID,String> > changes;
+		std::vector<JID> changes;
 		JID user1;
 		JID user2;
 };
diff --git a/Swiften/Avatars/VCardUpdateAvatarManager.cpp b/Swiften/Avatars/VCardUpdateAvatarManager.cpp
index 24abfa1..9d0ae2d 100644
--- a/Swiften/Avatars/VCardUpdateAvatarManager.cpp
+++ b/Swiften/Avatars/VCardUpdateAvatarManager.cpp
@@ -64,7 +64,7 @@ void VCardUpdateAvatarManager::handleVCardChanged(const JID& from, VCard::ref vC
 
 void VCardUpdateAvatarManager::setAvatarHash(const JID& from, const String& hash) {
 	avatarHashes_[from] = hash;
-	onAvatarChanged(from, hash);
+	onAvatarChanged(from);
 }
 
 /*
@@ -85,14 +85,6 @@ String VCardUpdateAvatarManager::getAvatarHash(const JID& jid) const {
 	}
 }
 
-boost::filesystem::path VCardUpdateAvatarManager::getAvatarPath(const JID& jid) const {
-	String hash = getAvatarHash(jid);
-	if (!hash.isEmpty()) {
-		return avatarStorage_->getAvatarPath(hash);
-	}
-	return boost::filesystem::path();
-}
-
 JID VCardUpdateAvatarManager::getAvatarJID(const JID& jid) const {
 	JID bareFrom = jid.toBare();
 	return (mucRegistry_ && mucRegistry_->isMUC(bareFrom)) ? jid : bareFrom;
diff --git a/Swiften/Avatars/VCardUpdateAvatarManager.h b/Swiften/Avatars/VCardUpdateAvatarManager.h
index e29db1c..2220906 100644
--- a/Swiften/Avatars/VCardUpdateAvatarManager.h
+++ b/Swiften/Avatars/VCardUpdateAvatarManager.h
@@ -6,12 +6,10 @@
 
 #pragma once
 
-#include <boost/filesystem.hpp>
 #include <boost/shared_ptr.hpp>
-#include <boost/optional.hpp>
 #include <map>
 
-#include "Swiften/Base/boost_bsignals.h"
+#include "Swiften/Avatars/AvatarProvider.h"
 #include "Swiften/JID/JID.h"
 #include "Swiften/Elements/Presence.h"
 #include "Swiften/Elements/VCard.h"
@@ -23,15 +21,12 @@ namespace Swift {
 	class StanzaChannel;
 	class VCardManager;
 
-	class VCardUpdateAvatarManager {
+	class VCardUpdateAvatarManager : public AvatarProvider {
 		public:
 			VCardUpdateAvatarManager(VCardManager*, StanzaChannel*, AvatarStorage*, MUCRegistry* = NULL);
 			virtual ~VCardUpdateAvatarManager();
 
-			virtual boost::filesystem::path getAvatarPath(const JID&) const;
-
-		public:
-			boost::signal<void (const JID&, const String& /*hash*/)> onAvatarChanged;
+			String getAvatarHash(const JID&) const;
 
 		private:
 			void handlePresenceReceived(boost::shared_ptr<Presence>);
@@ -39,7 +34,6 @@ namespace Swift {
 			void handleVCardChanged(const JID& from, VCard::ref);
 			void setAvatarHash(const JID& from, const String& hash);
 			JID getAvatarJID(const JID& o) const;
-			String getAvatarHash(const JID&) const;
 
 		private:
 			VCardManager* vcardManager_;
diff --git a/Swiften/SConscript b/Swiften/SConscript
index a9e3a01..f19698e 100644
--- a/Swiften/SConscript
+++ b/Swiften/SConscript
@@ -24,10 +24,6 @@ if env["SCONS_STAGE"] == "build" :
 	myenv.MergeFlags(myenv["ZLIB_FLAGS"])
 	myenv.MergeFlags(myenv["OPENSSL_FLAGS"])
 	sources = [
-			"Avatars/AvatarFileStorage.cpp",
-			"Avatars/VCardUpdateAvatarManager.cpp",
-			"Avatars/AvatarManager.cpp",
-			"Avatars/AvatarStorage.cpp",
 			"Chat/ChatStateTracker.cpp",
 			"Chat/ChatStateNotifier.cpp",
 			"Chat/ChatStateMessageSender.cpp",
@@ -117,6 +113,7 @@ if env["SCONS_STAGE"] == "build" :
 		sources += ["TLS/OpenSSL/OpenSSLContext.cpp"]
 
 	SConscript(dirs = [
+			"Avatars",
 			"Base",
 			"StringPrep",
 			"SASL",
-- 
cgit v0.10.2-6-g49f6