diff options
-rw-r--r-- | Swift/Controllers/Chat/ChatControllerBase.cpp | 4 | ||||
-rw-r--r-- | Swift/Controllers/HistoryController.cpp | 10 | ||||
-rw-r--r-- | Swift/Controllers/HistoryController.h | 4 | ||||
-rw-r--r-- | Swift/Controllers/HistoryViewController.cpp | 2 | ||||
-rw-r--r-- | Swift/QtUI/QtHistoryWindow.cpp | 5 | ||||
-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 |
9 files changed, 71 insertions, 49 deletions
diff --git a/Swift/Controllers/Chat/ChatControllerBase.cpp b/Swift/Controllers/Chat/ChatControllerBase.cpp index 97b574b..4fc378a 100644 --- a/Swift/Controllers/Chat/ChatControllerBase.cpp +++ b/Swift/Controllers/Chat/ChatControllerBase.cpp @@ -146,7 +146,7 @@ void ChatControllerBase::handleSendMessageRequest(const std::string &body, bool // log message if (historyController_) { - historyController_->addMessage(getBaseJID(), selfJID_, std::string("me"), body, now); + historyController_->addMessage(body, selfJID_, toJID_, now); } } @@ -255,7 +255,7 @@ void ChatControllerBase::handleIncomingMessage(boost::shared_ptr<MessageEvent> m else { lastMessagesUIID_[from] = addMessage(body, senderDisplayNameFromMessage(from), isIncomingMessageFromMe(message), label, std::string(avatarManager_->getAvatarPath(from).string()), timeStamp); if (historyController_) { - historyController_->addMessage(getBaseJID(), from, senderDisplayNameFromMessage(from), body, timeStamp); + historyController_->addMessage(body, from, selfJID_, timeStamp); } } } diff --git a/Swift/Controllers/HistoryController.cpp b/Swift/Controllers/HistoryController.cpp index 63d4690..7e3d1e4 100644 --- a/Swift/Controllers/HistoryController.cpp +++ b/Swift/Controllers/HistoryController.cpp @@ -19,13 +19,13 @@ HistoryController::~HistoryController() { delete localHistory_; } -void HistoryController::addMessage(const JID& baseJID, const JID& fromJID, const std::string& displayNick, const std::string& messageBody, boost::posix_time::ptime timeStamp) { - HistoryMessage message(messageBody, baseJID, fromJID, displayNick, timeStamp); - localHistory_->addMessage(message); +void HistoryController::addMessage(const std::string& message, const JID& fromJID, const JID& toJID, const boost::posix_time::ptime& timeStamp) { + HistoryMessage historyMessage(message, fromJID, toJID, timeStamp); + localHistory_->addMessage(historyMessage); } -std::vector<HistoryMessage> HistoryController::getMessages(const JID& baseJID) const { - return localHistory_->getMessages(baseJID); +std::vector<HistoryMessage> HistoryController::getMessages(const JID& selfJID, const JID& contactJID) const { + return localHistory_->getMessages(selfJID, contactJID); } std::vector<JID> HistoryController::getAllContacts() const { diff --git a/Swift/Controllers/HistoryController.h b/Swift/Controllers/HistoryController.h index 0d337f6..9a38b15 100644 --- a/Swift/Controllers/HistoryController.h +++ b/Swift/Controllers/HistoryController.h @@ -20,8 +20,8 @@ namespace Swift { HistoryController(); ~HistoryController(); - void addMessage(const JID& baseJID, const JID& fromJID, const std::string& displayNick, const std::string& messageBody, boost::posix_time::ptime timeStamp); - std::vector<HistoryMessage> getMessages(const JID& baseJID) const; + void addMessage(const std::string& message, const JID& fromJID, const JID& toJID, const boost::posix_time::ptime& timeStamp); + std::vector<HistoryMessage> getMessages(const JID& selfJID, const JID& contactJID) const; std::vector<JID> getAllContacts() const; private: diff --git a/Swift/Controllers/HistoryViewController.cpp b/Swift/Controllers/HistoryViewController.cpp index 5cc25e0..f208925 100644 --- a/Swift/Controllers/HistoryViewController.cpp +++ b/Swift/Controllers/HistoryViewController.cpp @@ -62,7 +62,7 @@ void HistoryViewController::handleSelectedContactChanged(RosterItem* newContact) ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(newContact); - std::vector<HistoryMessage> messages = historyController_->getMessages(contact->getJID()); + std::vector<HistoryMessage> messages; //TODO = historyController_->getMessages(contact->getJID()); for (std::vector<HistoryMessage>::iterator it = messages.begin(); it != messages.end(); it++) { historyWindow_->addMessage(*it); } diff --git a/Swift/QtUI/QtHistoryWindow.cpp b/Swift/QtUI/QtHistoryWindow.cpp index 2577f4b..24085d9 100644 --- a/Swift/QtUI/QtHistoryWindow.cpp +++ b/Swift/QtUI/QtHistoryWindow.cpp @@ -70,8 +70,9 @@ void QtHistoryWindow::setRosterModel(Roster* model) { } void QtHistoryWindow::addMessage(const HistoryMessage& message) { - boost::shared_ptr<MessageSnippet> snippet(new MessageSnippet(QString::fromStdString(message.getMessage()), QString::fromStdString(message.getDisplayNick()), QDateTime::currentDateTime(), "", false, false, theme_, "id")); - conversation_->addMessage(snippet); + // TODO + // boost::shared_ptr<MessageSnippet> snippet(new MessageSnippet(QString::fromStdString(message.getMessage()), QString::fromStdString(message.getDisplayNick()), QDateTime::currentDateTime(), "", false, false, theme_, "id")); + // conversation_->addMessage(snippet); } void QtHistoryWindow::handleSomethingSelectedChanged(RosterItem* item) { 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: |