From 734cdf96ee07357382914c1e38e19a5bf1f5078d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Sat, 21 Aug 2010 20:29:33 +0200
Subject: Store avatars per profile.


diff --git a/Swift/Controllers/MainController.cpp b/Swift/Controllers/MainController.cpp
index b267dd2..cef09eb 100644
--- a/Swift/Controllers/MainController.cpp
+++ b/Swift/Controllers/MainController.cpp
@@ -51,6 +51,7 @@
 #include "Swiften/Queries/Requests/GetDiscoInfoRequest.h"
 #include "Swiften/Queries/Requests/GetVCardRequest.h"
 #include "Swiften/Avatars/AvatarFileStorage.h"
+#include "Swiften/Avatars/AvatarStorageFactory.h"
 #include "Swiften/Avatars/AvatarManager.h"
 #include "Swiften/StringCodecs/SHA1.h"
 #include "Swiften/StringCodecs/Hexify.h"
@@ -63,7 +64,7 @@ static const String CLIENT_VERSION = "1.0-devel";
 static const String CLIENT_NODE = "http://swift.im";
 
 
-MainController::MainController(ChatWindowFactory* chatWindowFactory, MainWindowFactory *mainWindowFactory, LoginWindowFactory *loginWindowFactory, EventWindowFactory* eventWindowFactory, SettingsProvider *settings, SystemTray* systemTray, SoundPlayer* soundPlayer, XMLConsoleWidgetFactory* xmlConsoleWidgetFactory, ChatListWindowFactory* chatListWindowFactory, MUCSearchWindowFactory* mucSearchWindowFactory, AvatarStorage* avatarStorage, ApplicationMessageDisplay* applicationMessageDisplay, bool useDelayForLatency)
+MainController::MainController(ChatWindowFactory* chatWindowFactory, MainWindowFactory *mainWindowFactory, LoginWindowFactory *loginWindowFactory, EventWindowFactory* eventWindowFactory, SettingsProvider *settings, SystemTray* systemTray, SoundPlayer* soundPlayer, XMLConsoleWidgetFactory* xmlConsoleWidgetFactory, ChatListWindowFactory* chatListWindowFactory, MUCSearchWindowFactory* mucSearchWindowFactory, AvatarStorageFactory* avatarStorageFactory, ApplicationMessageDisplay* applicationMessageDisplay, bool useDelayForLatency)
 	: timerFactory_(&boostIOServiceThread_.getIOService()), idleDetector_(&idleQuerier_, &timerFactory_, 100), chatWindowFactory_(chatWindowFactory), mainWindowFactory_(mainWindowFactory), loginWindowFactory_(loginWindowFactory), settings_(settings), loginWindow_(NULL), useDelayForLatency_(useDelayForLatency)  {
 	presenceOracle_ = NULL;
 	avatarManager_ = NULL;
@@ -88,7 +89,7 @@ MainController::MainController(ChatWindowFactory* chatWindowFactory, MainWindowF
 	chatListWindowFactory_ = chatListWindowFactory;
 	uiEventStream_ = new UIEventStream();
 
-	avatarStorage_ = avatarStorage;
+	avatarStorageFactory_ = avatarStorageFactory;
 	eventController_ = new EventController();
 	eventController_->onEventQueueLengthChange.connect(boost::bind(&MainController::handleEventQueueLengthChange, this, _1));
 
@@ -130,11 +131,15 @@ MainController::MainController(ChatWindowFactory* chatWindowFactory, MainWindowF
 MainController::~MainController() {
 	delete systemTrayController_;
 	delete soundEventController_;
-	delete avatarStorage_;
+	delete avatarStorageFactory_;
 	delete xmlConsoleController_;
 	delete uiEventStream_;
 	delete eventController_;
 	resetClient();
+	for(std::map<JID, AvatarStorage*>::const_iterator i = avatarStorages_.begin(); i != avatarStorages_.end(); ++i) {
+		delete i->second;
+	}
+	avatarStorages_.clear();
 }
 
 void MainController::resetClient() {
@@ -200,7 +205,7 @@ void MainController::handleConnected() {
 		presenceOracle_ = new PresenceOracle(client_);
 		nickResolver_ = new NickResolver(xmppRoster_);		
 
-		avatarManager_ = new AvatarManager(client_, client_, avatarStorage_);
+		avatarManager_ = new AvatarManager(client_, client_, getAvatarStorageForProfile(jid_));
 
 		rosterController_ = new RosterController(jid_, xmppRoster_, avatarManager_, mainWindowFactory_, nickResolver_, presenceOracle_, eventController_, uiEventStream_, client_);
 		rosterController_->onChangeStatusRequest.connect(boost::bind(&MainController::handleChangeStatusRequest, this, _1, _2));
@@ -470,4 +475,13 @@ void MainController::handleOwnVCardReceived(boost::shared_ptr<VCard> vCard, cons
 	}
 }
 
+AvatarStorage* MainController::getAvatarStorageForProfile(const JID& jid) {
+	std::pair< std::map<JID, AvatarStorage*>::iterator, bool > r = avatarStorages_.insert(std::make_pair<JID,AvatarStorage*>(jid, NULL));
+	if (r.second) {
+		r.first->second = avatarStorageFactory_->createAvatarStorage(jid);
+	}
+	return r.first->second;
+}
+
+
 }
diff --git a/Swift/Controllers/MainController.h b/Swift/Controllers/MainController.h
index e220211..e68e6d5 100644
--- a/Swift/Controllers/MainController.h
+++ b/Swift/Controllers/MainController.h
@@ -9,6 +9,7 @@
 #include "Swiften/Base/boost_bsignals.h"
 #include <boost/shared_ptr.hpp>
 #include <vector>
+#include <map>
 
 #include "Swiften/Network/BoostIOServiceThread.h"
 #include "Swiften/Network/BoostTimerFactory.h"
@@ -28,6 +29,7 @@
 #include "Swiften/Roster/XMPPRoster.h"
 
 namespace Swift {
+	class AvatarStorageFactory;
 	class AvatarStorage;
 	class Application;
 	class Client;
@@ -66,11 +68,12 @@ namespace Swift {
 
 	class MainController {
 		public:
-			MainController(ChatWindowFactory* chatWindowFactory, MainWindowFactory *mainWindowFactory, LoginWindowFactory *loginWindowFactory, EventWindowFactory* eventWindowFactory, SettingsProvider *settings, SystemTray* systemTray, SoundPlayer* soundPlayer, XMLConsoleWidgetFactory* xmlConsoleWidgetFactory, ChatListWindowFactory* chatListWindowFactory_, MUCSearchWindowFactory* mucSearchWindowFactory, AvatarStorage* avatarStorage, ApplicationMessageDisplay* applicationMessageDisplay, bool useDelayForLatency);
+			MainController(ChatWindowFactory* chatWindowFactory, MainWindowFactory *mainWindowFactory, LoginWindowFactory *loginWindowFactory, EventWindowFactory* eventWindowFactory, SettingsProvider *settings, SystemTray* systemTray, SoundPlayer* soundPlayer, XMLConsoleWidgetFactory* xmlConsoleWidgetFactory, ChatListWindowFactory* chatListWindowFactory_, MUCSearchWindowFactory* mucSearchWindowFactory, AvatarStorageFactory* avatarStorageFactory, ApplicationMessageDisplay* applicationMessageDisplay, bool useDelayForLatency);
 			~MainController();
 
 
 		private:
+			AvatarStorage* getAvatarStorageForProfile(const JID&);
 			void resetClient();
 			void handleConnected();
 			void handleLoginRequest(const String& username, const String& password, const String& certificateFile, bool remember, bool loginAutomatically);
@@ -104,7 +107,7 @@ namespace Swift {
 			EventWindowFactory* eventWindowFactory_;
 			SettingsProvider *settings_;
 			ProfileSettingsProvider* profileSettings_;
-			AvatarStorage* avatarStorage_;
+			AvatarStorageFactory* avatarStorageFactory_;
 			ApplicationMessageDisplay* applicationMessageDisplay_;
 			ChatController* chatController_;
 			XMPPRosterController* xmppRosterController_;
@@ -137,5 +140,6 @@ namespace Swift {
 			int timeBeforeNextReconnect_;
 			Timer::ref reconnectTimer_;
 			StatusTracker* statusTracker_;
+			std::map<JID, AvatarStorage*> avatarStorages_;
 	};
 }
diff --git a/Swift/QtUI/QtSwift.cpp b/Swift/QtUI/QtSwift.cpp
index 55bc112..897fae8 100644
--- a/Swift/QtUI/QtSwift.cpp
+++ b/Swift/QtUI/QtSwift.cpp
@@ -23,7 +23,7 @@
 #include "Swiften/Application/Application.h"
 #include "Swiften/Application/PlatformApplication.h"
 #include "Swiften/Application/PlatformApplicationPathProvider.h"
-#include "Swiften/Avatars/AvatarFileStorage.h"
+#include "Swiften/Avatars/AvatarFileStorageFactory.h"
 #include "Swiften/Base/String.h"
 #include "Swiften/Base/Platform.h"
 #include "Swiften/Elements/Presence.h"
@@ -78,7 +78,7 @@ QtSwift::QtSwift(po::variables_map options) : autoUpdater_(NULL) {
 	settings_ = new QtSettingsProvider();
 	application_ = new PlatformApplication(SWIFT_APPLICATION_NAME);
 	applicationPathProvider_ = new PlatformApplicationPathProvider(SWIFT_APPLICATION_NAME);
-	avatarStorage_ = new AvatarFileStorage(applicationPathProvider_->getAvatarDir());
+	avatarStorageFactory_ = new AvatarFileStorageFactory(applicationPathProvider_->getAvatarDir());
 	chatWindowFactory_ = new QtChatWindowFactory(splitter_, settings_, tabs_);
 	soundPlayer_ = new QtSoundPlayer(applicationPathProvider_);
 	if (splitter_) {
@@ -100,7 +100,7 @@ QtSwift::QtSwift(po::variables_map options) : autoUpdater_(NULL) {
 		chatListWindowFactories_.push_back(chatListWindowFactory);
 		QtMUCSearchWindowFactory* mucSearchWindowFactory = new QtMUCSearchWindowFactory();
 		mucSearchWindowFactories_.push_back(mucSearchWindowFactory);
-		MainController* mainController = new MainController(chatWindowFactory_, rosterWindowFactory, loginWindowFactory, eventWindowFactory, settings_, systemTray, soundPlayer_, xmlConsoleWidgetFactory, chatListWindowFactory, mucSearchWindowFactory, avatarStorage_, application_->getApplicationMessageDisplay(), options.count("latency-debug") > 0);
+		MainController* mainController = new MainController(chatWindowFactory_, rosterWindowFactory, loginWindowFactory, eventWindowFactory, settings_, systemTray, soundPlayer_, xmlConsoleWidgetFactory, chatListWindowFactory, mucSearchWindowFactory, avatarStorageFactory_, application_->getApplicationMessageDisplay(), options.count("latency-debug") > 0);
 		mainControllers_.push_back(mainController);
 	}
 
@@ -114,6 +114,7 @@ QtSwift::QtSwift(po::variables_map options) : autoUpdater_(NULL) {
 
 QtSwift::~QtSwift() {
 	delete autoUpdater_;
+	delete avatarStorageFactory_;
 	delete chatWindowFactory_;
 	foreach (QtMainWindowFactory* factory, rosterWindowFactories_) {
 		delete factory;
diff --git a/Swift/QtUI/QtSwift.h b/Swift/QtUI/QtSwift.h
index d205f9f..d877e66 100644
--- a/Swift/QtUI/QtSwift.h
+++ b/Swift/QtUI/QtSwift.h
@@ -25,7 +25,7 @@ namespace Swift {
 	class AutoUpdater;
 	class Application;
 	class ApplicationPathProvider;
-	class AvatarStorage;
+	class AvatarStorageFactory;
 	class MainController;
 	class QtChatWindowFactory;
 	class QtMainWindowFactory;
@@ -60,7 +60,7 @@ namespace Swift {
 			QtChatTabs* tabs_;
 			Application* application_;
 			ApplicationPathProvider* applicationPathProvider_;
-			AvatarStorage* avatarStorage_;
+			AvatarStorageFactory* avatarStorageFactory_;
 			AutoUpdater* autoUpdater_;
 
 	};
diff --git a/Swiften/Avatars/AvatarFileStorage.cpp b/Swiften/Avatars/AvatarFileStorage.cpp
index a0ebd21..aeb8db5 100644
--- a/Swiften/Avatars/AvatarFileStorage.cpp
+++ b/Swiften/Avatars/AvatarFileStorage.cpp
@@ -12,6 +12,7 @@
 namespace Swift {
 
 AvatarFileStorage::AvatarFileStorage(const boost::filesystem::path& path) : path_(path) {
+	std::cerr << "Creating avatar file storage: " << path.string() << std::endl;
 	try {
 		boost::filesystem::create_directory(path_);
 	}
diff --git a/Swiften/Avatars/AvatarFileStorageFactory.h b/Swiften/Avatars/AvatarFileStorageFactory.h
new file mode 100644
index 0000000..fb5963c
--- /dev/null
+++ b/Swiften/Avatars/AvatarFileStorageFactory.h
@@ -0,0 +1,27 @@
+/*
+ * 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 <boost/filesystem.hpp>
+
+#include "AvatarStorageFactory.h"
+#include "AvatarFileStorage.h"
+
+namespace Swift {
+	class AvatarFileStorageFactory: public AvatarStorageFactory {
+		public:
+			AvatarFileStorageFactory(const boost::filesystem::path& basePath) : basePath(basePath) {
+			}
+
+			virtual AvatarStorage* createAvatarStorage(const JID& profile) {
+				return new AvatarFileStorage(basePath / profile.getBare().getUTF8String());
+			}
+
+		private:
+			boost::filesystem::path basePath;
+	};
+}
diff --git a/Swiften/Avatars/AvatarStorageFactory.cpp b/Swiften/Avatars/AvatarStorageFactory.cpp
new file mode 100644
index 0000000..b525909
--- /dev/null
+++ b/Swiften/Avatars/AvatarStorageFactory.cpp
@@ -0,0 +1,15 @@
+/*
+ * 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/AvatarStorageFactory.h"
+
+namespace Swift {
+
+AvatarStorageFactory::~AvatarStorageFactory() {
+
+}
+
+}
diff --git a/Swiften/Avatars/AvatarStorageFactory.h b/Swiften/Avatars/AvatarStorageFactory.h
new file mode 100644
index 0000000..efc26e9
--- /dev/null
+++ b/Swiften/Avatars/AvatarStorageFactory.h
@@ -0,0 +1,19 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+namespace Swift {
+	class AvatarStorage;
+	class JID;
+
+	class AvatarStorageFactory {
+		public:
+			virtual ~AvatarStorageFactory();
+
+			virtual AvatarStorage* createAvatarStorage(const JID& profile) = 0;
+	};
+}
diff --git a/Swiften/SConscript b/Swiften/SConscript
index 3d4d5f6..0a4530a 100644
--- a/Swiften/SConscript
+++ b/Swiften/SConscript
@@ -27,6 +27,7 @@ if env["SCONS_STAGE"] == "build" :
 			"Avatars/AvatarFileStorage.cpp",
 			"Avatars/AvatarManager.cpp",
 			"Avatars/AvatarStorage.cpp",
+			"Avatars/AvatarStorageFactory.cpp",
 			"Chat/ChatStateTracker.cpp",
 			"Chat/ChatStateNotifier.cpp",
 			"Chat/ChatStateMessageSender.cpp",
-- 
cgit v0.10.2-6-g49f6