summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swiften/History/SQLiteHistoryManager.cpp')
-rw-r--r--Swiften/History/SQLiteHistoryManager.cpp66
1 files changed, 63 insertions, 3 deletions
diff --git a/Swiften/History/SQLiteHistoryManager.cpp b/Swiften/History/SQLiteHistoryManager.cpp
index e10de07..33d2c58 100644
--- a/Swiften/History/SQLiteHistoryManager.cpp
+++ b/Swiften/History/SQLiteHistoryManager.cpp
@@ -9,6 +9,7 @@
#include <sqlite3.h>
#include <Swiften/History/SQLiteHistoryManager.h>
+#include <sstream>
inline std::string getEscapedString(const std::string& s) {
std::string result(s);
@@ -36,6 +37,12 @@ SQLiteHistoryManager::SQLiteHistoryManager(const std::string& file) : db_(0) {
std::cerr << "SQL Error: " << errorMessage << std::endl;
sqlite3_free(errorMessage);
}
+
+ result = sqlite3_exec(db_, "CREATE TABLE IF NOT EXISTS conversations('id' INTEGER UNIQUE NOT NULL, 'time' INTEGER)", 0, 0, &errorMessage);
+ if (result != SQLITE_OK) {
+ std::cerr << "SQL Error: " << errorMessage << std::endl;
+ sqlite3_free(errorMessage);
+ }
}
SQLiteHistoryManager::~SQLiteHistoryManager() {
@@ -43,6 +50,8 @@ SQLiteHistoryManager::~SQLiteHistoryManager() {
}
void SQLiteHistoryManager::addMessage(const HistoryMessage& message) {
+ updateConversation(message);
+
int secondsSinceEpoch = (message.getTime() - boost::posix_time::ptime(boost::gregorian::date(1970, 1, 1))).total_seconds();
std::string statement = std::string("INSERT INTO messages('baseID', 'from', 'nick', 'message', 'time') VALUES(") +
boost::lexical_cast<std::string>(getIDForJID(message.getBaseJID())) + ", " +
@@ -58,11 +67,18 @@ void SQLiteHistoryManager::addMessage(const HistoryMessage& message) {
}
}
-std::vector<HistoryMessage> SQLiteHistoryManager::getMessages() const {
+std::vector<HistoryMessage> SQLiteHistoryManager::getMessages(const JID& baseJID) const {
std::vector<HistoryMessage> result;
sqlite3_stmt* selectStatement;
- std::string selectQuery("SELECT messages.'baseID', messages.'from', messages.'nick', messages.'message', messages.'time' FROM messages");
- int r = sqlite3_prepare(db_, selectQuery.c_str(), selectQuery.size(), &selectStatement, NULL);
+ boost::optional<int> baseID = getIDFromJID(baseJID);
+ if (!baseID) {
+ return std::vector<HistoryMessage>();
+ }
+
+ std::stringstream selectQuery;
+ selectQuery << "SELECT messages.'baseID', messages.'from', messages.'nick', messages.'message', messages.'time' FROM messages WHERE baseID=" << *baseID;
+
+ int r = sqlite3_prepare(db_, selectQuery.str().c_str(), selectQuery.str().size(), &selectStatement, NULL);
if (r != SQLITE_OK) {
std::cout << "Error: " << sqlite3_errmsg(db_) << std::endl;
}
@@ -138,4 +154,48 @@ boost::optional<int> SQLiteHistoryManager::getIDFromJID(const JID& jid) const {
return result;
}
+void SQLiteHistoryManager::updateConversation(const HistoryMessage& message) {
+ int id = getIDForJID(message.getBaseJID());
+ int secondsSinceEpoch = (message.getTime() - boost::posix_time::ptime(boost::gregorian::date(1970, 1, 1))).total_seconds(); // this might fail in 2038
+
+ std::stringstream statement;
+ statement << "insert or replace into conversations('id', 'time') VALUES(" <<
+ id <<", " << secondsSinceEpoch << ")";
+
+ char* errorMessage;
+ int result = sqlite3_exec(db_, statement.str().c_str(), 0, 0, &errorMessage);
+
+ if (result != SQLITE_OK) {
+ std::cerr << "SQL Error: " << errorMessage << std::endl;
+ sqlite3_free(errorMessage);
+ }
+}
+
+std::vector<JID> SQLiteHistoryManager::getAllContacts() const {
+ std::string query("SELECT conversations.'id' FROM conversations");
+ sqlite3_stmt* selectStatement;
+
+ int r = sqlite3_prepare(db_, query.c_str(), query.size(), &selectStatement, NULL);
+ if (r != SQLITE_OK) {
+ std::cout << "Error: " << sqlite3_errmsg(db_) << std::endl;
+ }
+
+ r = sqlite3_step(selectStatement);
+ std::vector<JID> result;
+ while (r == SQLITE_ROW) {
+ boost::optional<JID> contactJID(getJIDFromID(sqlite3_column_int(selectStatement, 0)));
+ if (contactJID) {
+ result.push_back(*contactJID);
+ }
+
+ r = sqlite3_step(selectStatement);
+ }
+ if (r != SQLITE_DONE) {
+ std::cout << "Error: " << sqlite3_errmsg(db_) << std::endl;
+ }
+
+ sqlite3_finalize(selectStatement);
+ return result;
+}
+
}