diff options
author | Catalin Badea <catalin.badea392@gmail.com> | 2012-06-30 16:39:36 (GMT) |
---|---|---|
committer | Cătălin Badea <catalin.badea392@gmail.com> | 2012-08-11 15:52:57 (GMT) |
commit | e117ec6c534e1b6bc3f1bcd18dadf2005672c38b (patch) | |
tree | 71b1ef76e57f37693cd60b13ac17e92faaa33ed4 /Swiften | |
parent | 3a13bbd772f549a3ea218fe9f3a8ca6c293989f9 (diff) | |
download | swift-contrib-e117ec6c534e1b6bc3f1bcd18dadf2005672c38b.zip swift-contrib-e117ec6c534e1b6bc3f1bcd18dadf2005672c38b.tar.bz2 |
Changed HistoryMessage structure back
Diffstat (limited to 'Swiften')
-rw-r--r-- | Swiften/History/HistoryManager.h | 2 | ||||
-rw-r--r-- | Swiften/History/HistoryMessage.h | 21 | ||||
-rw-r--r-- | Swiften/History/SQLiteHistoryManager.cpp | 70 | ||||
-rw-r--r-- | Swiften/History/SQLiteHistoryManager.h | 2 |
4 files changed, 58 insertions, 37 deletions
diff --git a/Swiften/History/HistoryManager.h b/Swiften/History/HistoryManager.h index 151f8be..1c0d784 100644 --- a/Swiften/History/HistoryManager.h +++ b/Swiften/History/HistoryManager.h @@ -17,7 +17,7 @@ namespace Swift { virtual void addMessage(const HistoryMessage& message) = 0; - virtual std::vector<HistoryMessage> getMessages(const JID&) const = 0; + virtual std::vector<HistoryMessage> getMessages(const JID& selfJID, const JID& contactJID) const = 0; virtual std::vector<JID> getAllContacts() const = 0; }; } diff --git a/Swiften/History/HistoryMessage.h b/Swiften/History/HistoryMessage.h index 03e9206..0bcff99 100644 --- a/Swiften/History/HistoryMessage.h +++ b/Swiften/History/HistoryMessage.h @@ -14,14 +14,12 @@ namespace Swift { public: HistoryMessage( const std::string& message, - const JID& baseJID, const JID& fromJID, - const std::string& displayNick, - const boost::posix_time::ptime time) : + const JID& toJID, + const boost::posix_time::ptime& time) : message_(message), - baseJID_(baseJID), fromJID_(fromJID), - displayNick_(displayNick), + toJID_(toJID), time_(time) { } @@ -29,16 +27,12 @@ namespace Swift { return message_; } - const JID& getBaseJID() const { - return baseJID_; - } - const JID& getFromJID() const { return fromJID_; } - const std::string& getDisplayNick() const { - return displayNick_; + const JID& getToJID() const { + return toJID_; } boost::posix_time::ptime getTime() const { @@ -46,14 +40,13 @@ namespace Swift { } bool operator==(const HistoryMessage& o) const { - return baseJID_ == o.baseJID_ && message_ == o.message_ && fromJID_ == o.fromJID_ && displayNick_ == o.displayNick_ && time_ == o.time_; + return message_ == o.message_ && fromJID_ == o.fromJID_ && toJID_ == o.toJID_ && time_ == o.time_; } private: std::string message_; - JID baseJID_; JID fromJID_; - std::string displayNick_; + JID toJID_; boost::posix_time::ptime time_; }; } diff --git a/Swiften/History/SQLiteHistoryManager.cpp b/Swiften/History/SQLiteHistoryManager.cpp index 5a5c832..9f02ca2 100644 --- a/Swiften/History/SQLiteHistoryManager.cpp +++ b/Swiften/History/SQLiteHistoryManager.cpp @@ -9,7 +9,6 @@ #include <sqlite3.h> #include <Swiften/History/SQLiteHistoryManager.h> -#include <sstream> inline std::string getEscapedString(const std::string& s) { std::string result(s); @@ -31,7 +30,7 @@ SQLiteHistoryManager::SQLiteHistoryManager(const std::string& file) : db_(0) { } char* errorMessage; - int result = sqlite3_exec(db_, "CREATE TABLE IF NOT EXISTS messages('baseID' INTEGER, 'from' INTEGER, 'nick' STRING, 'message' STRING, 'time' INTEGER)", 0, 0, &errorMessage); + int result = sqlite3_exec(db_, "CREATE TABLE IF NOT EXISTS messages('message' STRING, 'fromBare' INTEGER, 'fromResource' STRING, 'toBare' INTEGER, 'toResource' STRING, 'time' INTEGER)", 0, 0, &errorMessage); if (result != SQLITE_OK) { std::cerr << "SQL Error: " << errorMessage << std::endl; sqlite3_free(errorMessage); @@ -43,6 +42,7 @@ SQLiteHistoryManager::SQLiteHistoryManager(const std::string& file) : db_(0) { sqlite3_free(errorMessage); } + // FIXME: remove 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; @@ -55,14 +55,16 @@ SQLiteHistoryManager::~SQLiteHistoryManager() { } void SQLiteHistoryManager::addMessage(const HistoryMessage& message) { - updateConversation(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())) + ", " + - boost::lexical_cast<std::string>(getIDForJID(message.getFromJID())) + ", '" + - message.getDisplayNick() + "', '" + - getEscapedString(message.getMessage()) + "', " + + + std::string statement = std::string("'message', 'fromBare', 'fromResource', 'toBare', 'toResource', 'time') VALUES(") + + "'" + getEscapedString(message.getMessage()) + "', " + + boost::lexical_cast<std::string>(getIDForJID(message.getFromJID().toBare())) + ", '" + + getEscapedString(message.getFromJID().getResource()) + "', " + + boost::lexical_cast<std::string>(getIDForJID(message.getFromJID().toBare())) + ", '" + + getEscapedString(message.getToJID().getResource()) + "', " + boost::lexical_cast<std::string>(secondsSinceEpoch) + ")"; char* errorMessage; int result = sqlite3_exec(db_, statement.c_str(), 0, 0, &errorMessage); @@ -72,37 +74,60 @@ void SQLiteHistoryManager::addMessage(const HistoryMessage& message) { } } -std::vector<HistoryMessage> SQLiteHistoryManager::getMessages(const JID& baseJID) const { - std::vector<HistoryMessage> result; +std::vector<HistoryMessage> SQLiteHistoryManager::getMessages(const JID& selfJID, const JID& contactJID) const { sqlite3_stmt* selectStatement; - boost::optional<int> baseID = getIDFromJID(baseJID); - if (!baseID) { + + boost::optional<int> selfID = getIDFromJID(selfJID.toBare()); + boost::optional<int> contactID = getIDFromJID(contactJID.toBare()); + + if (!selfID || !contactID) { + // JIDs missing from the database return std::vector<HistoryMessage>(); } - std::stringstream selectQuery; - selectQuery << "SELECT messages.'baseID', messages.'from', messages.'nick', messages.'message', messages.'time' FROM messages WHERE baseID=" << *baseID; + std::string selectQuery = "SELECT * FROM messages WHERE (fromBare=" + + boost::lexical_cast<std::string>(*selfID) + " AND fromBare=" + + boost::lexical_cast<std::string>(*contactID) + ") OR (fromBare=" + + boost::lexical_cast<std::string>(*contactID) + " AND fromBare=" + + boost::lexical_cast<std::string>(*selfID) + ")"; - int r = sqlite3_prepare(db_, selectQuery.str().c_str(), selectQuery.str().size(), &selectStatement, NULL); + int r = sqlite3_prepare(db_, selectQuery.c_str(), selectQuery.size(), &selectStatement, NULL); if (r != SQLITE_OK) { std::cout << "Error: " << sqlite3_errmsg(db_) << std::endl; } r = sqlite3_step(selectStatement); + + // Retrieve result + std::vector<HistoryMessage> result; while (r == SQLITE_ROW) { - boost::optional<JID> baseJID(getJIDFromID(sqlite3_column_int(selectStatement, 0))); - boost::optional<JID> from(getJIDFromID(sqlite3_column_int(selectStatement, 1))); - std::string nick(reinterpret_cast<const char*>(sqlite3_column_text(selectStatement, 2))); - std::string message(reinterpret_cast<const char*>(sqlite3_column_text(selectStatement, 3))); - int secondsSinceEpoch(sqlite3_column_int(selectStatement, 4)); + std::string message(reinterpret_cast<const char*>(sqlite3_column_text(selectStatement, 0))); + + // fromJID + boost::optional<JID> fromJID(getJIDFromID(sqlite3_column_int(selectStatement, 1))); + std::string fromResource(reinterpret_cast<const char*>(sqlite3_column_text(selectStatement, 2))); + if (fromJID) { + fromJID = boost::optional<JID>(JID(fromJID->getNode(), fromJID->getDomain(), fromResource)); + } + + // toJID + boost::optional<JID> toJID(getJIDFromID(sqlite3_column_int(selectStatement, 3))); + std::string toResource(reinterpret_cast<const char*>(sqlite3_column_text(selectStatement, 4))); + if (toJID) { + toJID = boost::optional<JID>(JID(toJID->getNode(), toJID->getDomain(), toResource)); + } + + // timestamp + int secondsSinceEpoch(sqlite3_column_int(selectStatement, 5)); boost::posix_time::ptime time(boost::gregorian::date(1970, 1, 1), boost::posix_time::seconds(secondsSinceEpoch)); - result.push_back(HistoryMessage(message, (baseJID ? *baseJID : JID()), (from ? *from : JID()), nick, time)); + result.push_back(HistoryMessage(message, (fromJID ? *fromJID : JID()), (toJID ? *toJID : JID()), time)); r = sqlite3_step(selectStatement); } if (r != SQLITE_DONE) { std::cout << "Error: " << sqlite3_errmsg(db_) << std::endl; } sqlite3_finalize(selectStatement); + return result; } @@ -160,6 +185,8 @@ boost::optional<int> SQLiteHistoryManager::getIDFromJID(const JID& jid) const { } 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 @@ -174,6 +201,7 @@ void SQLiteHistoryManager::updateConversation(const HistoryMessage& message) { std::cerr << "SQL Error: " << errorMessage << std::endl; sqlite3_free(errorMessage); } + */ } std::vector<JID> SQLiteHistoryManager::getAllContacts() const { diff --git a/Swiften/History/SQLiteHistoryManager.h b/Swiften/History/SQLiteHistoryManager.h index ff82f5e..9d9ec05 100644 --- a/Swiften/History/SQLiteHistoryManager.h +++ b/Swiften/History/SQLiteHistoryManager.h @@ -19,7 +19,7 @@ namespace Swift { ~SQLiteHistoryManager(); void addMessage(const HistoryMessage& message); - std::vector<HistoryMessage> getMessages(const JID&) const; + std::vector<HistoryMessage> getMessages(const JID& selfJID, const JID& contactJID) const; std::vector<JID> getAllContacts() const; private: |