summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCatalin Badea <catalin.badea392@gmail.com>2012-06-27 17:53:07 (GMT)
committerCatalin Badea <catalin.badea392@gmail.com>2012-06-27 17:53:07 (GMT)
commit72819f0c95ad3fe264806d3a072fcbfab3131fcf (patch)
tree002cdb467597cfbe774b93f49400e3facd9efe43 /Swiften
parent0998d6a8f138e0fd00245fc3c1efc8878fa7b888 (diff)
downloadswift-contrib-72819f0c95ad3fe264806d3a072fcbfab3131fcf.zip
swift-contrib-72819f0c95ad3fe264806d3a072fcbfab3131fcf.tar.bz2
Display conversations by using roster selection
Diffstat (limited to 'Swiften')
-rw-r--r--Swiften/History/HistoryManager.h3
-rw-r--r--Swiften/History/SQLiteHistoryManager.cpp66
-rw-r--r--Swiften/History/SQLiteHistoryManager.h8
3 files changed, 70 insertions, 7 deletions
diff --git a/Swiften/History/HistoryManager.h b/Swiften/History/HistoryManager.h
index 7a4324b..151f8be 100644
--- a/Swiften/History/HistoryManager.h
+++ b/Swiften/History/HistoryManager.h
@@ -17,6 +17,7 @@ namespace Swift {
virtual void addMessage(const HistoryMessage& message) = 0;
- virtual std::vector<HistoryMessage> getMessages() const = 0;
+ virtual std::vector<HistoryMessage> getMessages(const JID&) const = 0;
+ virtual std::vector<JID> getAllContacts() const = 0;
};
}
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;
+}
+
}
diff --git a/Swiften/History/SQLiteHistoryManager.h b/Swiften/History/SQLiteHistoryManager.h
index ffd9492..ff82f5e 100644
--- a/Swiften/History/SQLiteHistoryManager.h
+++ b/Swiften/History/SQLiteHistoryManager.h
@@ -18,16 +18,18 @@ namespace Swift {
SQLiteHistoryManager(const std::string& file);
~SQLiteHistoryManager();
- virtual void addMessage(const HistoryMessage& message);
- virtual std::vector<HistoryMessage> getMessages() const;
+ void addMessage(const HistoryMessage& message);
+ std::vector<HistoryMessage> getMessages(const JID&) const;
+ std::vector<JID> getAllContacts() const;
+ private:
int getIDForJID(const JID&);
int addJID(const JID&);
+ void updateConversation(const HistoryMessage& message);
boost::optional<JID> getJIDFromID(int id) const;
boost::optional<int> getIDFromJID(const JID& jid) const;
- private:
sqlite3* db_;
};
}