From 9c3d535b1dc82ff600862a1210993c362fda9b8e Mon Sep 17 00:00:00 2001
From: Kevin Smith <git@kismith.co.uk>
Date: Tue, 16 Nov 2010 23:16:54 +0000
Subject: Remember previous MUC service searches.

Resolves: #615
Release-Notes: The MUC Search will now remember previously searched services.

diff --git a/Swift/Controllers/Chat/MUCSearchController.cpp b/Swift/Controllers/Chat/MUCSearchController.cpp
index 0cf02c7..e4592a9 100644
--- a/Swift/Controllers/Chat/MUCSearchController.cpp
+++ b/Swift/Controllers/Chat/MUCSearchController.cpp
@@ -21,12 +21,16 @@
 
 namespace Swift {
 
-MUCSearchController::MUCSearchController(const JID& jid, UIEventStream* uiEventStream, MUCSearchWindowFactory* factory, IQRouter* iqRouter) : jid_(jid) {
+static const String SEARCHED_SERVICES = "searchedServices";
+
+MUCSearchController::MUCSearchController(const JID& jid, UIEventStream* uiEventStream, MUCSearchWindowFactory* factory, IQRouter* iqRouter, SettingsProvider* settings) : jid_(jid) {
 	iqRouter_ = iqRouter;
+	settings_ = settings;
 	uiEventStream_ = uiEventStream;
 	uiEventConnection_ = uiEventStream_->onUIEvent.connect(boost::bind(&MUCSearchController::handleUIEvent, this, _1));
 	window_ = NULL;
 	factory_ = factory;
+	loadServices();
 }
 
 MUCSearchController::~MUCSearchController() {
@@ -39,6 +43,7 @@ void MUCSearchController::handleUIEvent(boost::shared_ptr<UIEvent> event) {
 		if (!window_) {
 			window_ = factory_->createMUCSearchWindow(uiEventStream_);
 			window_->onAddService.connect(boost::bind(&MUCSearchController::handleAddService, this, _1, true));
+			window_->addSavedServices(savedServices_);
 			handleAddService(JID(jid_.getDomain()), true);
 		}
 		window_->setMUC("");
@@ -48,7 +53,36 @@ void MUCSearchController::handleUIEvent(boost::shared_ptr<UIEvent> event) {
 	}
 }
 
+void MUCSearchController::loadServices() {
+	savedServices_.clear();
+	foreach (String stringItem, settings_->getStringSetting(SEARCHED_SERVICES).split('\n')) {
+		savedServices_.push_back(JID(stringItem));
+	}
+}
+
+void MUCSearchController::addAndSaveServices(const JID& jid) {
+	savedServices_.erase(std::remove(savedServices_.begin(), savedServices_.end(), jid), savedServices_.end());
+	savedServices_.push_back(jid);
+	String collapsed;
+	bool storeThis = savedServices_.size() < 15;
+	foreach (JID jidItem, savedServices_) {
+		if (!storeThis) {
+			storeThis = true;
+			continue;
+		}
+		if (!collapsed.isEmpty()) {
+			collapsed += "\n";
+		}
+		collapsed += jidItem.toString();
+	}
+	settings_->storeString(SEARCHED_SERVICES, collapsed);
+	window_->addSavedServices(savedServices_);
+}
+
 void MUCSearchController::handleAddService(const JID& jid, bool userTriggered) {
+	if (userTriggered) {
+		addAndSaveServices(jid);
+	}
 	if (std::find(services_.begin(), services_.end(), jid) != services_.end()) {
 		if (!userTriggered) {
 			/* No infinite recursion. (Some buggy servers do infinitely deep disco of themselves)*/
diff --git a/Swift/Controllers/Chat/MUCSearchController.h b/Swift/Controllers/Chat/MUCSearchController.h
index 44b51d8..10988ad 100644
--- a/Swift/Controllers/Chat/MUCSearchController.h
+++ b/Swift/Controllers/Chat/MUCSearchController.h
@@ -17,6 +17,7 @@
 
 #include "Swift/Controllers/UIEvents/UIEvent.h"
 #include "Swift/Controllers/Chat/MUCSearchController.h"
+#include "Swift/Controllers/Settings/SettingsProvider.h"
 #include "Swiften/Elements/DiscoInfo.h"
 #include "Swiften/Elements/DiscoItems.h"
 #include "Swiften/Elements/ErrorPayload.h"
@@ -85,7 +86,7 @@ namespace Swift {
 
 	class MUCSearchController {
 		public:
-		MUCSearchController(const JID& jid, UIEventStream* uiEventStream, MUCSearchWindowFactory* mucSearchWindowFactory, IQRouter* iqRouter);
+			MUCSearchController(const JID& jid, UIEventStream* uiEventStream, MUCSearchWindowFactory* mucSearchWindowFactory, IQRouter* iqRouter, SettingsProvider* settings);
 			~MUCSearchController();
 		private:
 			void handleUIEvent(boost::shared_ptr<UIEvent> event);
@@ -96,11 +97,15 @@ namespace Swift {
 			void handleDiscoError(const JID& jid, const ErrorPayload& error);
 			void removeService(const JID& jid);
 			void refreshView();
+			void loadServices();
+			void addAndSaveServices(const JID& jid);
 			UIEventStream* uiEventStream_;
 			MUCSearchWindow* window_;
 			MUCSearchWindowFactory* factory_;
+			SettingsProvider* settings_;
 			boost::bsignals::scoped_connection uiEventConnection_;
 			std::vector<JID> services_;
+			std::vector<JID> savedServices_;
 			std::map<JID, MUCService> serviceDetails_;
 			IQRouter* iqRouter_;
 			JID jid_;
diff --git a/Swift/Controllers/MainController.cpp b/Swift/Controllers/MainController.cpp
index 0542fd6..49c4a23 100644
--- a/Swift/Controllers/MainController.cpp
+++ b/Swift/Controllers/MainController.cpp
@@ -242,7 +242,7 @@ void MainController::handleConnected() {
 		client_->getDiscoManager()->setCapsNode(CLIENT_NODE);
 		client_->getDiscoManager()->setDiscoInfo(discoInfo);
 
-		mucSearchController_ = new MUCSearchController(jid_, uiEventStream_, mucSearchWindowFactory_, client_->getIQRouter());
+		mucSearchController_ = new MUCSearchController(jid_, uiEventStream_, mucSearchWindowFactory_, client_->getIQRouter(), settings_);
 	}
 	
 	client_->requestRoster();
diff --git a/Swift/Controllers/UIInterfaces/MUCSearchWindow.h b/Swift/Controllers/UIInterfaces/MUCSearchWindow.h
index 9a4b9b3..da54ded 100644
--- a/Swift/Controllers/UIInterfaces/MUCSearchWindow.h
+++ b/Swift/Controllers/UIInterfaces/MUCSearchWindow.h
@@ -8,6 +8,8 @@
 
 #include "Swiften/Base/boost_bsignals.h"
 
+#include <vector>
+
 #include "Swiften/Base/String.h"
 #include "Swiften/JID/JID.h"
 
@@ -23,6 +25,7 @@ namespace Swift {
 			virtual void setMUC(const String& nick) = 0;
 			virtual void clearList() = 0;
 			virtual void addService(const MUCService& service) = 0;
+			virtual void addSavedServices(const std::vector<JID>& services) = 0;
 
 			virtual void show() = 0;
 
diff --git a/Swift/QtUI/MUCSearch/QtMUCSearchWindow.cpp b/Swift/QtUI/MUCSearch/QtMUCSearchWindow.cpp
index 0ccd558..c31230c 100644
--- a/Swift/QtUI/MUCSearch/QtMUCSearchWindow.cpp
+++ b/Swift/QtUI/MUCSearch/QtMUCSearchWindow.cpp
@@ -34,7 +34,7 @@ QtMUCSearchWindow::QtMUCSearchWindow(UIEventStream* eventStream) {
 #ifdef SWIFT_PLATFORM_MACOSX
 	results_->setAlternatingRowColors(true);
 #endif
-	connect(service_, SIGNAL(returnPressed()), this, SLOT(handleSearch()));
+	connect(service_, SIGNAL(activated(const QString&)), this, SLOT(handleSearch(const QString&)));
 	connect(room_, SIGNAL(returnPressed()), this, SLOT(handleJoin()));
 	connect(nickName_, SIGNAL(returnPressed()), room_, SLOT(setFocus()));
 	connect(searchButton_, SIGNAL(clicked()), this, SLOT(handleSearch()));
@@ -47,6 +47,14 @@ QtMUCSearchWindow::~QtMUCSearchWindow() {
 
 }
 
+void QtMUCSearchWindow::addSavedServices(const std::vector<JID>& services) {
+	service_->clear();
+	foreach (JID jid, services) {
+		service_->addItem(P2QSTRING(jid.toString()));
+	}
+	service_->clearEditText();
+}
+
 void QtMUCSearchWindow::handleActivated(const QModelIndex& index) {
 	if (!index.isValid()) {
 		return;
@@ -69,13 +77,18 @@ void QtMUCSearchWindow::handleSelected(const QModelIndex& current) {
 
 }
 
-void QtMUCSearchWindow::handleSearch() {
-	if (service_->text().isEmpty()) {
+void QtMUCSearchWindow::handleSearch(const QString& text) {
+	if (text.isEmpty()) {
 		return;
 	}
-	onAddService(JID(Q2PSTRING(service_->text())));
+	onAddService(JID(Q2PSTRING(text)));
 }
 
+void QtMUCSearchWindow::handleSearch() {
+	handleSearch(service_->currentText());
+}
+
+
 void QtMUCSearchWindow::handleJoin() {
 	if (room_->text().isEmpty()) {
 		handleSelected(results_->currentIndex());
diff --git a/Swift/QtUI/MUCSearch/QtMUCSearchWindow.h b/Swift/QtUI/MUCSearch/QtMUCSearchWindow.h
index c0a8e72..27ccdcb 100644
--- a/Swift/QtUI/MUCSearch/QtMUCSearchWindow.h
+++ b/Swift/QtUI/MUCSearch/QtMUCSearchWindow.h
@@ -24,9 +24,11 @@ namespace Swift {
 			virtual void setMUC(const String& nick);
 			virtual void clearList();
 			virtual void addService(const MUCService& service);
+			virtual void addSavedServices(const std::vector<JID>& services);
 
 			virtual void show();
 		private slots:
+			void handleSearch(const QString& text);
 			void handleSearch();
 			void handleJoin();
 			void handleSelected(const QModelIndex& current);
diff --git a/Swift/QtUI/MUCSearch/QtMUCSearchWindow.ui b/Swift/QtUI/MUCSearch/QtMUCSearchWindow.ui
index 134807f..ef2524b 100644
--- a/Swift/QtUI/MUCSearch/QtMUCSearchWindow.ui
+++ b/Swift/QtUI/MUCSearch/QtMUCSearchWindow.ui
@@ -74,9 +74,9 @@
           </widget>
          </item>
          <item>
-          <widget class="QLineEdit" name="service_">
-           <property name="text">
-            <string/>
+          <widget class="QComboBox" name="service_">
+           <property name="editable">
+            <bool>true</bool>
            </property>
           </widget>
          </item>
-- 
cgit v0.10.2-6-g49f6