From 32dffa31b92ad29cc15cd6e6fe3cc4ef1b4ef23a Mon Sep 17 00:00:00 2001
From: Tobias Markmann <tm@ayena.de>
Date: Mon, 27 Jul 2015 00:11:38 +0200
Subject: Remember directory services used when searching for users

Test-Information:

Tested by searching at some services and checked that they
are present in the combo box after a Swift restart.

Change-Id: Ia30d932f20447f44e458af3b32a38665872da025

diff --git a/Swift/Controllers/Chat/UserSearchController.cpp b/Swift/Controllers/Chat/UserSearchController.cpp
index 24f3724..ece897f 100644
--- a/Swift/Controllers/Chat/UserSearchController.cpp
+++ b/Swift/Controllers/Chat/UserSearchController.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010-2014 Isode Limited.
+ * Copyright (c) 2010-2015 Isode Limited.
  * All rights reserved.
  * See the COPYING file for more information.
  */
@@ -10,32 +10,39 @@
 #include <boost/shared_ptr.hpp>
 #include <boost/smart_ptr/make_shared.hpp>
 
+#include <Swiften/Avatars/AvatarManager.h>
+#include <Swiften/Base/String.h>
 #include <Swiften/Base/foreach.h>
+#include <Swiften/Disco/DiscoServiceWalker.h>
 #include <Swiften/Disco/GetDiscoInfoRequest.h>
 #include <Swiften/Disco/GetDiscoItemsRequest.h>
-#include <Swiften/Disco/DiscoServiceWalker.h>
-#include <Swiften/VCards/VCardManager.h>
 #include <Swiften/Presence/PresenceOracle.h>
-#include <Swiften/Avatars/AvatarManager.h>
+#include <Swiften/VCards/VCardManager.h>
+
 #include <Swift/Controllers/ContactEditController.h>
+#include <Swift/Controllers/ContactSuggester.h>
 #include <Swift/Controllers/Intl.h>
-#include <Swift/Controllers/UIEvents/UIEventStream.h>
-#include <Swift/Controllers/UIEvents/RequestChatWithUserDialogUIEvent.h>
+#include <Swift/Controllers/ProfileSettingsProvider.h>
+#include <Swift/Controllers/Roster/RosterController.h>
 #include <Swift/Controllers/UIEvents/RequestAddUserDialogUIEvent.h>
+#include <Swift/Controllers/UIEvents/RequestChatWithUserDialogUIEvent.h>
 #include <Swift/Controllers/UIEvents/RequestInviteToMUCUIEvent.h>
+#include <Swift/Controllers/UIEvents/UIEventStream.h>
 #include <Swift/Controllers/UIInterfaces/UserSearchWindow.h>
 #include <Swift/Controllers/UIInterfaces/UserSearchWindowFactory.h>
-#include <Swift/Controllers/Roster/RosterController.h>
-#include <Swift/Controllers/ContactSuggester.h>
 
 namespace Swift {
-UserSearchController::UserSearchController(Type type, const JID& jid, UIEventStream* uiEventStream, VCardManager* vcardManager, UserSearchWindowFactory* factory, IQRouter* iqRouter, RosterController* rosterController, ContactSuggester* contactSuggester, AvatarManager* avatarManager, PresenceOracle* presenceOracle) : type_(type), jid_(jid), uiEventStream_(uiEventStream), vcardManager_(vcardManager), factory_(factory), iqRouter_(iqRouter), rosterController_(rosterController), contactSuggester_(contactSuggester), avatarManager_(avatarManager), presenceOracle_(presenceOracle) {
+
+static const std::string SEARCHED_DIRECTORIES = "searchedDirectories";
+
+UserSearchController::UserSearchController(Type type, const JID& jid, UIEventStream* uiEventStream, VCardManager* vcardManager, UserSearchWindowFactory* factory, IQRouter* iqRouter, RosterController* rosterController, ContactSuggester* contactSuggester, AvatarManager* avatarManager, PresenceOracle* presenceOracle, ProfileSettingsProvider* settings) : type_(type), jid_(jid), uiEventStream_(uiEventStream), vcardManager_(vcardManager), factory_(factory), iqRouter_(iqRouter), rosterController_(rosterController), contactSuggester_(contactSuggester), avatarManager_(avatarManager), presenceOracle_(presenceOracle), settings_(settings) {
 	uiEventStream_->onUIEvent.connect(boost::bind(&UserSearchController::handleUIEvent, this, _1));
 	vcardManager_->onVCardChanged.connect(boost::bind(&UserSearchController::handleVCardChanged, this, _1, _2));
 	avatarManager_->onAvatarChanged.connect(boost::bind(&UserSearchController::handleAvatarChanged, this, _1));
 	presenceOracle_->onPresenceChange.connect(boost::bind(&UserSearchController::handlePresenceChanged, this, _1));
 	window_ = NULL;
 	discoWalker_ = NULL;
+	loadSavedDirectories();
 }
 
 UserSearchController::~UserSearchController() {
@@ -94,6 +101,7 @@ void UserSearchController::handleUIEvent(boost::shared_ptr<UIEvent> event) {
 	if (handle) {
 		initializeUserWindow();
 		window_->show();
+		window_->addSavedServices(savedDirectories_);
 		if (addUserRequest) {
 			const std::string& name = addUserRequest->getPredefinedName();
 			const JID& jid = addUserRequest->getPredefinedJID();
@@ -159,6 +167,7 @@ void UserSearchController::handleFormResponse(boost::shared_ptr<SearchPayload> f
 }
 
 void UserSearchController::handleSearch(boost::shared_ptr<SearchPayload> fields, const JID& jid) {
+	addToSavedDirectories(jid);
 	boost::shared_ptr<GenericRequest<SearchPayload> > searchRequest(new GenericRequest<SearchPayload>(IQ::Set, jid, fields, iqRouter_));
 	searchRequest->onResponse.connect(boost::bind(&UserSearchController::handleSearchResponse, this, _1, _2));
 	searchRequest->send();
@@ -341,4 +350,37 @@ void UserSearchController::initializeUserWindow() {
 	}
 }
 
+void UserSearchController::loadSavedDirectories() {
+	savedDirectories_.clear();
+	foreach (std::string stringItem, String::split(settings_->getStringSetting(SEARCHED_DIRECTORIES), '\n')) {
+		if(!stringItem.empty()) {
+			savedDirectories_.push_back(JID(stringItem));
+		}
+	}
+}
+
+void UserSearchController::addToSavedDirectories(const JID& jid) {
+	if (!jid.isValid()) {
+		return;
+	}
+
+	savedDirectories_.erase(std::remove(savedDirectories_.begin(), savedDirectories_.end(), jid), savedDirectories_.end());
+	savedDirectories_.insert(savedDirectories_.begin(), jid);
+
+	std::string collapsed;
+	int i = 0;
+	foreach (JID jidItem, savedDirectories_) {
+		if (i >= 15) {
+			break;
+		}
+		if (!collapsed.empty()) {
+			collapsed += "\n";
+		}
+		collapsed += jidItem.toString();
+		++i;
+	}
+	settings_->storeString(SEARCHED_DIRECTORIES, collapsed);
+	window_->addSavedServices(savedDirectories_);
+}
+
 }
diff --git a/Swift/Controllers/Chat/UserSearchController.h b/Swift/Controllers/Chat/UserSearchController.h
index 3c45c04..0423a65 100644
--- a/Swift/Controllers/Chat/UserSearchController.h
+++ b/Swift/Controllers/Chat/UserSearchController.h
@@ -1,18 +1,17 @@
 /*
- * Copyright (c) 2010-2014 Isode Limited.
+ * Copyright (c) 2010-2015 Isode Limited.
  * All rights reserved.
  * See the COPYING file for more information.
  */
 
 #pragma once
 
-#include <boost/shared_ptr.hpp>
-
 #include <map>
-#include <vector>
 #include <string>
+#include <vector>
+
+#include <boost/shared_ptr.hpp>
 
-#include <Swift/Controllers/Contact.h>
 #include <Swiften/Base/boost_bsignals.h>
 #include <Swiften/Elements/DiscoInfo.h>
 #include <Swiften/Elements/DiscoItems.h>
@@ -22,6 +21,8 @@
 #include <Swiften/Elements/VCard.h>
 #include <Swiften/JID/JID.h>
 
+#include <Swift/Controllers/Contact.h>
+
 namespace Swift {
 	class UIEventStream;
 	class UIEvent;
@@ -34,6 +35,7 @@ namespace Swift {
 	class ContactSuggester;
 	class AvatarManager;
 	class PresenceOracle;
+	class ProfileSettingsProvider;
 
 	class UserSearchResult {
 		public:
@@ -48,7 +50,7 @@ namespace Swift {
 	class UserSearchController {
 		public:
 			enum Type {AddContact, StartChat, InviteToChat};
-			UserSearchController(Type type, const JID& jid, UIEventStream* uiEventStream, VCardManager* vcardManager, UserSearchWindowFactory* userSearchWindowFactory, IQRouter* iqRouter, RosterController* rosterController, ContactSuggester* contactSuggester, AvatarManager* avatarManager, PresenceOracle* presenceOracle);
+			UserSearchController(Type type, const JID& jid, UIEventStream* uiEventStream, VCardManager* vcardManager, UserSearchWindowFactory* userSearchWindowFactory, IQRouter* iqRouter, RosterController* rosterController, ContactSuggester* contactSuggester, AvatarManager* avatarManager, PresenceOracle* presenceOracle, ProfileSettingsProvider* settings);
 			~UserSearchController();
 
 			UserSearchWindow* getUserSearchWindow();
@@ -74,6 +76,9 @@ namespace Swift {
 			void endDiscoWalker();
 			void initializeUserWindow();
 
+			void loadSavedDirectories();
+			void addToSavedDirectories(const JID& jid);
+
 		private:
 			Type type_;
 			JID jid_;
@@ -88,5 +93,7 @@ namespace Swift {
 			ContactSuggester* contactSuggester_;
 			AvatarManager* avatarManager_;
 			PresenceOracle* presenceOracle_;
+			std::vector<JID> savedDirectories_;
+			ProfileSettingsProvider* settings_;
 	};
 }
diff --git a/Swift/Controllers/MainController.cpp b/Swift/Controllers/MainController.cpp
index cdaa207..a0dde5b 100644
--- a/Swift/Controllers/MainController.cpp
+++ b/Swift/Controllers/MainController.cpp
@@ -366,7 +366,7 @@ void MainController::handleConnected() {
 		contactSuggesterWithoutRoster_ = new ContactSuggester();
 		contactSuggesterWithRoster_ = new ContactSuggester();
 
-		userSearchControllerInvite_ = new UserSearchController(UserSearchController::InviteToChat, jid_, uiEventStream_, client_->getVCardManager(), uiFactory_, client_->getIQRouter(), rosterController_, contactSuggesterWithRoster_, client_->getAvatarManager(), client_->getPresenceOracle());
+		userSearchControllerInvite_ = new UserSearchController(UserSearchController::InviteToChat, jid_, uiEventStream_, client_->getVCardManager(), uiFactory_, client_->getIQRouter(), rosterController_, contactSuggesterWithRoster_, client_->getAvatarManager(), client_->getPresenceOracle(), profileSettings_);
 #ifdef SWIFT_EXPERIMENTAL_HISTORY
 		historyController_ = new HistoryController(storages_->getHistoryStorage());
 		historyViewController_ = new HistoryViewController(jid_, uiEventStream_, historyController_, client_->getNickResolver(), client_->getAvatarManager(), client_->getPresenceOracle(), uiFactory_);
@@ -405,8 +405,8 @@ void MainController::handleConnected() {
 		client_->getDiscoManager()->setCapsNode(CLIENT_NODE);
 		client_->getDiscoManager()->setDiscoInfo(discoInfo);
 
-		userSearchControllerChat_ = new UserSearchController(UserSearchController::StartChat, jid_, uiEventStream_, client_->getVCardManager(), uiFactory_, client_->getIQRouter(), rosterController_, contactSuggesterWithRoster_, client_->getAvatarManager(), client_->getPresenceOracle());
-		userSearchControllerAdd_ = new UserSearchController(UserSearchController::AddContact, jid_, uiEventStream_, client_->getVCardManager(), uiFactory_, client_->getIQRouter(), rosterController_, contactSuggesterWithoutRoster_, client_->getAvatarManager(), client_->getPresenceOracle());
+		userSearchControllerChat_ = new UserSearchController(UserSearchController::StartChat, jid_, uiEventStream_, client_->getVCardManager(), uiFactory_, client_->getIQRouter(), rosterController_, contactSuggesterWithRoster_, client_->getAvatarManager(), client_->getPresenceOracle(), profileSettings_);
+		userSearchControllerAdd_ = new UserSearchController(UserSearchController::AddContact, jid_, uiEventStream_, client_->getVCardManager(), uiFactory_, client_->getIQRouter(), rosterController_, contactSuggesterWithoutRoster_, client_->getAvatarManager(), client_->getPresenceOracle(), profileSettings_);
 		adHocManager_ = new AdHocManager(JID(boundJID_.getDomain()), uiFactory_, client_->getIQRouter(), uiEventStream_, rosterController_->getWindow());
 
 		chatsManager_->onImpromptuMUCServiceDiscovered.connect(boost::bind(&UserSearchController::setCanInitiateImpromptuMUC, userSearchControllerChat_, _1));
-- 
cgit v0.10.2-6-g49f6