diff options
-rw-r--r-- | Swiften/History/HistoryManager.cpp | 8 | ||||
-rw-r--r-- | Swiften/History/HistoryManager.h | 16 | ||||
-rw-r--r-- | Swiften/History/HistoryMessage.h | 37 | ||||
-rw-r--r-- | Swiften/History/Makefile.inc | 5 | ||||
-rw-r--r-- | Swiften/History/SQLiteHistoryManager.cpp | 134 | ||||
-rw-r--r-- | Swiften/History/SQLiteHistoryManager.h | 27 | ||||
-rw-r--r-- | Swiften/History/UnitTest/Makefile.inc | 2 | ||||
-rw-r--r-- | Swiften/History/UnitTest/SQLiteHistoryManagerTest.cpp | 109 | ||||
-rw-r--r-- | Swiften/Makefile.inc | 6 |
9 files changed, 343 insertions, 1 deletions
diff --git a/Swiften/History/HistoryManager.cpp b/Swiften/History/HistoryManager.cpp new file mode 100644 index 0000000..3d67c27 --- /dev/null +++ b/Swiften/History/HistoryManager.cpp @@ -0,0 +1,8 @@ +#include "Swiften/History/HistoryManager.h" + +namespace Swift { + +HistoryManager::~HistoryManager() { +} + +} diff --git a/Swiften/History/HistoryManager.h b/Swiften/History/HistoryManager.h new file mode 100644 index 0000000..2a7c040 --- /dev/null +++ b/Swiften/History/HistoryManager.h @@ -0,0 +1,16 @@ +#pragma once + +#include "Swiften/Base/String.h" +#include "Swiften/JID/JID.h" +#include "Swiften/History/HistoryMessage.h" + +namespace Swift { + class HistoryManager { + public: + virtual ~HistoryManager(); + + virtual void addMessage(const HistoryMessage& message) = 0; + + virtual std::vector<HistoryMessage> getMessages() const = 0; + }; +} diff --git a/Swiften/History/HistoryMessage.h b/Swiften/History/HistoryMessage.h new file mode 100644 index 0000000..3fda3e2 --- /dev/null +++ b/Swiften/History/HistoryMessage.h @@ -0,0 +1,37 @@ +#pragma once + +#include <boost/date_time/posix_time/posix_time_types.hpp> + +namespace Swift { + class HistoryMessage { + public: + HistoryMessage(const String& message, const JID& from, const JID& to, const boost::posix_time::ptime time) : message_(message), from_(from), to_(to), time_(time) { + } + + const String& getMessage() const { + return message_; + } + + const JID& getFrom() const { + return from_; + } + + const JID& getTo() const { + return to_; + } + + boost::posix_time::ptime getTime() const { + return time_; + } + + bool operator==(const HistoryMessage& o) const { + return message_ == o.message_ && from_ == o.from_ && to_ == o.to_ && time_ == o.time_; + } + + private: + String message_; + JID from_; + JID to_; + boost::posix_time::ptime time_; + }; +} diff --git a/Swiften/History/Makefile.inc b/Swiften/History/Makefile.inc new file mode 100644 index 0000000..fbd917b --- /dev/null +++ b/Swiften/History/Makefile.inc @@ -0,0 +1,5 @@ +SWIFTEN_SOURCES += \ + Swiften/History/HistoryManager.cpp \ + Swiften/History/SQLiteHistoryManager.cpp + +include Swiften/History/UnitTest/Makefile.inc diff --git a/Swiften/History/SQLiteHistoryManager.cpp b/Swiften/History/SQLiteHistoryManager.cpp new file mode 100644 index 0000000..865abba --- /dev/null +++ b/Swiften/History/SQLiteHistoryManager.cpp @@ -0,0 +1,134 @@ +#include <iostream> +#include <boost/lexical_cast.hpp> + +#include "sqlite3.h" +#include "Swiften/History/SQLiteHistoryManager.h" + +namespace { + +inline Swift::String getEscapedString(const Swift::String& s) { + Swift::String result(s); + result.replaceAll('\'', Swift::String("\\'")); + return result; +} + +} + + +namespace Swift { + +SQLiteHistoryManager::SQLiteHistoryManager(const String& file) : db_(0) { + sqlite3_open(file.getUTF8Data(), &db_); + if (!db_) { + std::cerr << "Error opening database " << file << std::endl; // FIXME + } + + char* errorMessage; + int result = sqlite3_exec(db_, "CREATE TABLE IF NOT EXISTS messages('from' INTEGER, 'to' INTEGER, 'message' STRING, 'time' INTEGER)", 0, 0, &errorMessage); + if (result != SQLITE_OK) { + std::cerr << "SQL Error: " << errorMessage << std::endl; + sqlite3_free(errorMessage); + } + + result = sqlite3_exec(db_, "CREATE TABLE IF NOT EXISTS jids('id' INTEGER PRIMARY KEY ASC AUTOINCREMENT, 'jid' STRING UNIQUE NOT NULL)", 0, 0, &errorMessage); + if (result != SQLITE_OK) { + std::cerr << "SQL Error: " << errorMessage << std::endl; + sqlite3_free(errorMessage); + } +} + +SQLiteHistoryManager::~SQLiteHistoryManager() { + sqlite3_close(db_); +} + +void SQLiteHistoryManager::addMessage(const HistoryMessage& message) { + int secondsSinceEpoch = (message.getTime() - boost::posix_time::ptime(boost::gregorian::date(1970, 1, 1))).total_seconds(); + String statement = String("INSERT INTO messages('from', 'to', 'message', 'time') VALUES(") + boost::lexical_cast<std::string>(getIDForJID(message.getFrom())) + ", " + boost::lexical_cast<std::string>(getIDForJID(message.getTo())) + ", '" + getEscapedString(message.getMessage()) + "', " + boost::lexical_cast<std::string>(secondsSinceEpoch) + ")"; + char* errorMessage; + int result = sqlite3_exec(db_, statement.getUTF8Data(), 0, 0, &errorMessage); + if (result != SQLITE_OK) { + std::cerr << "SQL Error: " << errorMessage << std::endl; + sqlite3_free(errorMessage); + } +} + +std::vector<HistoryMessage> SQLiteHistoryManager::getMessages() const { + std::vector<HistoryMessage> result; + sqlite3_stmt* selectStatement; + String selectQuery("SELECT messages.'from', messages.'to', messages.'message', messages.'time' FROM messages"); + int r = sqlite3_prepare(db_, selectQuery.getUTF8Data(), selectQuery.getUTF8Size(), &selectStatement, NULL); + if (r != SQLITE_OK) { + std::cout << "Error: " << sqlite3_errmsg(db_) << std::endl; + } + r = sqlite3_step(selectStatement); + while (r == SQLITE_ROW) { + boost::optional<JID> from(getJIDFromID(sqlite3_column_int(selectStatement, 0))); + boost::optional<JID> to(getJIDFromID(sqlite3_column_int(selectStatement, 1))); + String message(reinterpret_cast<const char*>(sqlite3_column_text(selectStatement, 2))); + int secondsSinceEpoch(sqlite3_column_int(selectStatement, 3)); + boost::posix_time::ptime time(boost::gregorian::date(1970, 1, 1), boost::posix_time::seconds(secondsSinceEpoch)); + + result.push_back(HistoryMessage(message, (from ? *from : JID()), (to ? *to : JID()), time)); + r = sqlite3_step(selectStatement); + } + if (r != SQLITE_DONE) { + std::cout << "Error: " << sqlite3_errmsg(db_) << std::endl; + } + sqlite3_finalize(selectStatement); + return result; +} + +int SQLiteHistoryManager::getIDForJID(const JID& jid) { + boost::optional<int> id = getIDFromJID(jid); + if (id) { + return *id; + } + else { + return addJID(jid); + } +} + +int SQLiteHistoryManager::addJID(const JID& jid) { + String statement = String("INSERT INTO jids('jid') VALUES('") + getEscapedString(jid.toString()) + "')"; + char* errorMessage; + int result = sqlite3_exec(db_, statement.getUTF8Data(), 0, 0, &errorMessage); + if (result != SQLITE_OK) { + std::cerr << "SQL Error: " << errorMessage << std::endl; + sqlite3_free(errorMessage); + } + return sqlite3_last_insert_rowid(db_); +} + +boost::optional<JID> SQLiteHistoryManager::getJIDFromID(int id) const { + boost::optional<JID> result; + sqlite3_stmt* selectStatement; + String selectQuery("SELECT jid FROM jids WHERE id=" + boost::lexical_cast<std::string>(id)); + int r = sqlite3_prepare(db_, selectQuery.getUTF8Data(), selectQuery.getUTF8Size(), &selectStatement, NULL); + if (r != SQLITE_OK) { + std::cout << "Error: " << sqlite3_errmsg(db_) << std::endl; + } + r = sqlite3_step(selectStatement); + if (r == SQLITE_ROW) { + result = boost::optional<JID>(reinterpret_cast<const char*>(sqlite3_column_text(selectStatement, 0))); + } + sqlite3_finalize(selectStatement); + return result; +} + +boost::optional<int> SQLiteHistoryManager::getIDFromJID(const JID& jid) const { + boost::optional<int> result; + sqlite3_stmt* selectStatement; + String selectQuery("SELECT id FROM jids WHERE jid='" + jid.toString() + "'"); + int r = sqlite3_prepare(db_, selectQuery.getUTF8Data(), selectQuery.getUTF8Size(), &selectStatement, NULL); + if (r != SQLITE_OK) { + std::cout << "Error: " << sqlite3_errmsg(db_) << std::endl; + } + r = sqlite3_step(selectStatement); + if (r == SQLITE_ROW) { + result = boost::optional<int>(sqlite3_column_int(selectStatement, 0)); + } + sqlite3_finalize(selectStatement); + return result; +} + +} diff --git a/Swiften/History/SQLiteHistoryManager.h b/Swiften/History/SQLiteHistoryManager.h new file mode 100644 index 0000000..ace42f7 --- /dev/null +++ b/Swiften/History/SQLiteHistoryManager.h @@ -0,0 +1,27 @@ +#pragma once + +#include <boost/optional.hpp> + +#include "Swiften/History/HistoryManager.h" + +struct sqlite3; + +namespace Swift { + class SQLiteHistoryManager : public HistoryManager { + public: + SQLiteHistoryManager(const String& file); + ~SQLiteHistoryManager(); + + virtual void addMessage(const HistoryMessage& message); + virtual std::vector<HistoryMessage> getMessages() const; + + int getIDForJID(const JID&); + int addJID(const JID&); + + boost::optional<JID> getJIDFromID(int id) const; + boost::optional<int> getIDFromJID(const JID& jid) const; + + private: + sqlite3* db_; + }; +} diff --git a/Swiften/History/UnitTest/Makefile.inc b/Swiften/History/UnitTest/Makefile.inc new file mode 100644 index 0000000..b1b44f8 --- /dev/null +++ b/Swiften/History/UnitTest/Makefile.inc @@ -0,0 +1,2 @@ +UNITTEST_SOURCES += \ + Swiften/History/UnitTest/SQLiteHistoryManagerTest.cpp diff --git a/Swiften/History/UnitTest/SQLiteHistoryManagerTest.cpp b/Swiften/History/UnitTest/SQLiteHistoryManagerTest.cpp new file mode 100644 index 0000000..f4f122a --- /dev/null +++ b/Swiften/History/UnitTest/SQLiteHistoryManagerTest.cpp @@ -0,0 +1,109 @@ +#include <cppunit/extensions/HelperMacros.h> +#include <cppunit/extensions/TestFactoryRegistry.h> +#include <boost/date_time/posix_time/posix_time.hpp> + +#include "Swiften/History/SQLiteHistoryManager.h" + +using namespace Swift; + +class SQLiteHistoryManagerTest : public CppUnit::TestFixture { + CPPUNIT_TEST_SUITE(SQLiteHistoryManagerTest); + CPPUNIT_TEST(testAddMessage); + CPPUNIT_TEST(testAddMessage_TwoMessages); + CPPUNIT_TEST(testGetIDForJID_SameJID); + CPPUNIT_TEST(testGetIDForJID_DifferentJIDs); + CPPUNIT_TEST(getJIDFromID); + CPPUNIT_TEST(getJIDFromID_UnexistingID); + CPPUNIT_TEST(getIDFromJID); + CPPUNIT_TEST(getIDFromJID_UnexistingJID); + CPPUNIT_TEST_SUITE_END(); + + public: + SQLiteHistoryManagerTest() {} + + void setUp() { + } + + void tearDown() { + } + + void testAddMessage() { + std::auto_ptr<SQLiteHistoryManager> testling(createHistoryManager()); + HistoryMessage testMessage("Test", JID("foo@bar.com"), JID("fum@baz.org"), boost::posix_time::time_from_string("1980-01-21 22:03")); + testling->addMessage(testMessage); + + std::vector<HistoryMessage> messages = testling->getMessages(); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(messages.size())); + CPPUNIT_ASSERT(testMessage == messages[0]); + } + + void testAddMessage_TwoMessages() { + std::auto_ptr<SQLiteHistoryManager> testling(createHistoryManager()); + HistoryMessage testMessage1("Test1", JID("foo@bar.com"), JID("fum@baz.org"), boost::posix_time::time_from_string("1980-01-21 22:03")); + testling->addMessage(testMessage1); + HistoryMessage testMessage2("Test2", JID("fum@baz.org"), JID("foo@bar.com"), boost::posix_time::time_from_string("1975-03-09 22:04")); + testling->addMessage(testMessage2); + + std::vector<HistoryMessage> messages = testling->getMessages(); + CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(messages.size())); + CPPUNIT_ASSERT(testMessage1 == messages[0]); + CPPUNIT_ASSERT(testMessage2 == messages[1]); + } + + void testGetIDForJID_SameJID() { + std::auto_ptr<SQLiteHistoryManager> testling(createHistoryManager()); + int id1 = testling->getIDForJID(JID("foo@bar.com")); + int id2 = testling->getIDForJID(JID("foo@bar.com")); + + CPPUNIT_ASSERT_EQUAL(id1, id2); + } + + void testGetIDForJID_DifferentJIDs() { + std::auto_ptr<SQLiteHistoryManager> testling(createHistoryManager()); + int id1 = testling->getIDForJID(JID("foo@bar.com")); + int id2 = testling->getIDForJID(JID("foo@baz.com")); + + CPPUNIT_ASSERT(id1 != id2); + } + + void getJIDFromID() { + std::auto_ptr<SQLiteHistoryManager> testling(createHistoryManager()); + int id = testling->addJID(JID("foo@bar.com")); + + boost::optional<JID> result(testling->getJIDFromID(id)); + CPPUNIT_ASSERT(result); + CPPUNIT_ASSERT_EQUAL(JID("foo@bar.com"), *result); + } + + void getJIDFromID_UnexistingID() { + std::auto_ptr<SQLiteHistoryManager> testling(createHistoryManager()); + + boost::optional<JID> result(testling->getJIDFromID(1)); + + CPPUNIT_ASSERT(!result); + } + + void getIDFromJID() { + std::auto_ptr<SQLiteHistoryManager> testling(createHistoryManager()); + int id = testling->addJID(JID("foo@bar.com")); + + boost::optional<int> result(testling->getIDFromJID(JID("foo@bar.com"))); + CPPUNIT_ASSERT(result); + CPPUNIT_ASSERT_EQUAL(id, *result); + } + + void getIDFromJID_UnexistingJID() { + std::auto_ptr<SQLiteHistoryManager> testling(createHistoryManager()); + + boost::optional<int> result(testling->getIDFromJID(JID("foo@bar.com"))); + + CPPUNIT_ASSERT(!result); + } + + private: + SQLiteHistoryManager* createHistoryManager() { + return new SQLiteHistoryManager(":memory:"); + } +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(SQLiteHistoryManagerTest); diff --git a/Swiften/Makefile.inc b/Swiften/Makefile.inc index 880887b..7eca8a5 100644 --- a/Swiften/Makefile.inc +++ b/Swiften/Makefile.inc @@ -20,6 +20,9 @@ include Swiften/Roster/Makefile.inc include Swiften/Disco/Makefile.inc include Swiften/Presence/Makefile.inc include Swiften/Notifier/Makefile.inc +include Swiften/History/Makefile.inc + +CPPFLAGS += $(SQLITE_CPPFLAGS) SWIFTEN_TARGET = Swiften/Swiften.a SWIFTEN_OBJECTS = \ @@ -27,7 +30,8 @@ SWIFTEN_OBJECTS = \ $(SWIFTEN_OBJECTIVE_SOURCES:.mm=.o) \ $(LIBIDN_OBJECTS) \ $(BOOST_OBJECTS) \ - $(ZLIB_OBJECTS) + $(ZLIB_OBJECTS) \ + $(SQLITE_OBJECTS) TARGETS += $(SWIFTEN_TARGET) CLEANFILES += $(SWIFTEN_TARGET) $(SWIFTEN_OBJECTS) |