diff options
author | Remko Tronçon <git@el-tramo.be> | 2010-03-28 13:40:14 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2010-03-28 13:40:43 (GMT) |
commit | b61486fefe602e0d18fa5279021006f87b965307 (patch) | |
tree | 437585cbef1179e1ec31f79789591d5610200c29 /Swiften/History | |
parent | dae28dd45e43fc6e6ef2ec4c6c65d5d736ed86f8 (diff) | |
download | swift-b61486fefe602e0d18fa5279021006f87b965307.zip swift-b61486fefe602e0d18fa5279021006f87b965307.tar.bz2 |
Moved Swiften to a separate module.
Diffstat (limited to 'Swiften/History')
m--------- | Swiften | 0 | ||||
-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/SConscript | 11 | ||||
-rw-r--r-- | Swiften/History/SQLiteHistoryManager.cpp | 134 | ||||
-rw-r--r-- | Swiften/History/SQLiteHistoryManager.h | 27 | ||||
-rw-r--r-- | Swiften/History/UnitTest/SQLiteHistoryManagerTest.cpp | 109 |
8 files changed, 0 insertions, 342 deletions
diff --git a/Swiften b/Swiften new file mode 160000 +Subproject 8213ba16d0043d2461f4b031c881d61dda5a38c diff --git a/Swiften/History/HistoryManager.cpp b/Swiften/History/HistoryManager.cpp deleted file mode 100644 index 3d67c27..0000000 --- a/Swiften/History/HistoryManager.cpp +++ /dev/null @@ -1,8 +0,0 @@ -#include "Swiften/History/HistoryManager.h" - -namespace Swift { - -HistoryManager::~HistoryManager() { -} - -} diff --git a/Swiften/History/HistoryManager.h b/Swiften/History/HistoryManager.h deleted file mode 100644 index 2a7c040..0000000 --- a/Swiften/History/HistoryManager.h +++ /dev/null @@ -1,16 +0,0 @@ -#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 deleted file mode 100644 index 3fda3e2..0000000 --- a/Swiften/History/HistoryMessage.h +++ /dev/null @@ -1,37 +0,0 @@ -#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/SConscript b/Swiften/History/SConscript deleted file mode 100644 index bc80780..0000000 --- a/Swiften/History/SConscript +++ /dev/null @@ -1,11 +0,0 @@ -Import("swiften_env") - -myenv = swiften_env.Clone() -if myenv["target"] == "native": - myenv.MergeFlags(swiften_env["SQLITE_FLAGS"]) - -objects = myenv.StaticObject([ - "HistoryManager.cpp", - "SQLiteHistoryManager.cpp", - ]) -swiften_env.Append(SWIFTEN_OBJECTS = [objects]) diff --git a/Swiften/History/SQLiteHistoryManager.cpp b/Swiften/History/SQLiteHistoryManager.cpp deleted file mode 100644 index 865abba..0000000 --- a/Swiften/History/SQLiteHistoryManager.cpp +++ /dev/null @@ -1,134 +0,0 @@ -#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 deleted file mode 100644 index ace42f7..0000000 --- a/Swiften/History/SQLiteHistoryManager.h +++ /dev/null @@ -1,27 +0,0 @@ -#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/SQLiteHistoryManagerTest.cpp b/Swiften/History/UnitTest/SQLiteHistoryManagerTest.cpp deleted file mode 100644 index e738a6e..0000000 --- a/Swiften/History/UnitTest/SQLiteHistoryManagerTest.cpp +++ /dev/null @@ -1,109 +0,0 @@ -#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); |