From 308ff634379e73a0c8668ffb0593d23f95b4dfa5 Mon Sep 17 00:00:00 2001
From: Kevin Smith <git@kismith.co.uk>
Date: Tue, 22 Dec 2009 21:00:48 +0000
Subject: Add a PreviousStatusStore.


diff --git a/Swift/Controllers/PreviousStatusStore.cpp b/Swift/Controllers/PreviousStatusStore.cpp
new file mode 100644
index 0000000..42f2124
--- /dev/null
+++ b/Swift/Controllers/PreviousStatusStore.cpp
@@ -0,0 +1,47 @@
+#include "PreviousStatusStore.h"
+
+#include "Swiften/Base/foreach.h"
+
+namespace Swift {
+
+PreviousStatusStore::PreviousStatusStore() {
+
+}
+
+PreviousStatusStore::~PreviousStatusStore() {
+
+}
+
+void PreviousStatusStore::addStatus(StatusShow::Type status, const String& message) {
+	//FIXME: remove old entries
+	store_.push_back(TypeStringPair(status, message));
+}
+
+std::vector<TypeStringPair> PreviousStatusStore::exactMatchSuggestions(StatusShow::Type status, const String& message) {
+	std::vector<TypeStringPair> suggestions;
+	suggestions.push_back(TypeStringPair(status, message));
+	return suggestions;
+}
+
+std::vector<TypeStringPair> PreviousStatusStore::getSuggestions(const String& message) {
+	std::vector<TypeStringPair> suggestions;
+	foreach (TypeStringPair status, store_) {
+		if (status.second == message) {
+			suggestions.clear();
+			suggestions.push_back(status);
+			break;
+		} else if (status.second.contains(message)) {
+			suggestions.push_back(status);
+		}
+	}
+	if (suggestions.size() == 0) {
+		TypeStringPair suggestion(StatusShow::Online, message);
+		suggestions.push_back(suggestion);
+	}
+	if (suggestions.size() == 1) {
+		suggestions = exactMatchSuggestions(suggestions[0].first, suggestions[0].second);
+	}
+	return suggestions;
+}
+
+}
diff --git a/Swift/Controllers/PreviousStatusStore.h b/Swift/Controllers/PreviousStatusStore.h
new file mode 100644
index 0000000..476febe
--- /dev/null
+++ b/Swift/Controllers/PreviousStatusStore.h
@@ -0,0 +1,22 @@
+#pragma once
+
+#include <utility> /* std::pair */
+#include <vector>
+
+#include "Swiften/Base/String.h"
+#include "Swiften/Elements/StatusShow.h"
+
+namespace Swift {
+	typedef std::pair<StatusShow::Type, String> TypeStringPair;
+	class PreviousStatusStore {
+		public:
+			PreviousStatusStore();
+			~PreviousStatusStore();
+			void addStatus(StatusShow::Type status, const String& message);
+			std::vector<TypeStringPair> getSuggestions(const String& message);
+
+		private:
+			std::vector<TypeStringPair> exactMatchSuggestions(StatusShow::Type status, const String& message);
+			std::vector<TypeStringPair> store_;
+	};
+}
diff --git a/Swift/Controllers/SConscript b/Swift/Controllers/SConscript
index 79357b5..02d0b52 100644
--- a/Swift/Controllers/SConscript
+++ b/Swift/Controllers/SConscript
@@ -21,10 +21,12 @@ myenv.StaticLibrary("SwiftControllers", [
 		"XMLConsoleController.cpp",
 		"UIEvents/UIEvent.cpp",
 		"UIInterfaces/XMLConsoleWidget.cpp",
+		"PreviousStatusStore.cpp",
 	])
 
 env.Append(UNITTEST_SOURCES = [
 		File("UnitTest/NickResolverTest.cpp"),
 		File("UnitTest/RosterControllerTest.cpp"),
-		File("UnitTest/XMPPRosterControllerTest.cpp")
+		File("UnitTest/XMPPRosterControllerTest.cpp"),
+		File("UnitTest/PreviousStatusStoreTest.cpp"),
 	])
diff --git a/Swift/Controllers/UnitTest/PreviousStatusStoreTest.cpp b/Swift/Controllers/UnitTest/PreviousStatusStoreTest.cpp
new file mode 100644
index 0000000..c9e55e4
--- /dev/null
+++ b/Swift/Controllers/UnitTest/PreviousStatusStoreTest.cpp
@@ -0,0 +1,39 @@
+#include <cppunit/extensions/HelperMacros.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+
+#include "Swift/Controllers/PreviousStatusStore.h"
+
+using namespace Swift;
+
+class PreviousStatusStoreTest : public CppUnit::TestFixture
+{
+	CPPUNIT_TEST_SUITE(PreviousStatusStoreTest);
+	CPPUNIT_TEST(testGetAll);
+	//CPPUNIT_TEST(testGetAllLimited);
+	//CPPUNIT_TEST(testGetSuggestionsInexact);
+	//CPPUNIT_TEST(testGetSuggestionsExact);
+	CPPUNIT_TEST_SUITE_END();
+
+public:
+	PreviousStatusStoreTest() {};
+
+	void setUp() {
+		store_ = new PreviousStatusStore();
+		store_->addStatus(StatusShow::Online, "At home in the study");
+		store_->addStatus(StatusShow::DND, "In a meeting");
+		store_->addStatus(StatusShow::DND, "With a client");
+		store_->addStatus(StatusShow::Away, "Walking the elephant");
+		store_->addStatus(StatusShow::Online, "In the office, at my desk");
+	}
+
+	void tearDown() {
+		
+	}
+
+	void testGetAll() {
+
+	}
+
+private:
+	PreviousStatusStore* store_;
+};
diff --git a/Swiften/Base/String.h b/Swiften/Base/String.h
index 0a5530c..7a6a9cc 100644
--- a/Swiften/Base/String.h
+++ b/Swiften/Base/String.h
@@ -94,6 +94,10 @@ namespace Swift {
 				return *this;
 			}
 
+			bool contains(const String& o) {
+				return data_.find(o.data_) != std::string::npos;
+			}
+
 			char operator[](size_t i) const {
 				return data_[i];
 			}
diff --git a/Swiften/Base/UnitTest/StringTest.cpp b/Swiften/Base/UnitTest/StringTest.cpp
index 6ad12fd..87e1a99 100644
--- a/Swiften/Base/UnitTest/StringTest.cpp
+++ b/Swiften/Base/UnitTest/StringTest.cpp
@@ -25,6 +25,9 @@ class StringTest : public CppUnit::TestFixture
 		CPPUNIT_TEST(testReplaceAll_MatchingReplace);
 		CPPUNIT_TEST(testGetLowerCase);
 		CPPUNIT_TEST(testSplit);
+ 		CPPUNIT_TEST(testContains);
+		CPPUNIT_TEST(testContainsFalse);
+		CPPUNIT_TEST(testContainsExact);
 		CPPUNIT_TEST_SUITE_END();
 
 	public:
@@ -158,6 +161,19 @@ class StringTest : public CppUnit::TestFixture
 			CPPUNIT_ASSERT_EQUAL(String("def"), result[1]);
 			CPPUNIT_ASSERT_EQUAL(String("ghi"), result[2]);
 		}
+
+		void testContains() {
+			CPPUNIT_ASSERT(String("abcde").contains(String("bcd")));
+		}
+
+		void testContainsFalse() {
+			CPPUNIT_ASSERT(!String("abcde").contains(String("abcdef")));
+		}
+
+		void testContainsExact() {
+			CPPUNIT_ASSERT(String("abcde").contains(String("abcde")));
+		}
+
 };
 
 CPPUNIT_TEST_SUITE_REGISTRATION(StringTest);
-- 
cgit v0.10.2-6-g49f6