From e117ec6c534e1b6bc3f1bcd18dadf2005672c38b Mon Sep 17 00:00:00 2001 From: Catalin Badea Date: Sat, 30 Jun 2012 19:39:36 +0300 Subject: Changed HistoryMessage structure back diff --git a/Swift/Controllers/Chat/ChatControllerBase.cpp b/Swift/Controllers/Chat/ChatControllerBase.cpp index ee3a027..9d797e0 100644 --- a/Swift/Controllers/Chat/ChatControllerBase.cpp +++ b/Swift/Controllers/Chat/ChatControllerBase.cpp @@ -149,7 +149,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); } } @@ -258,7 +258,7 @@ void ChatControllerBase::handleIncomingMessage(boost::shared_ptr 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 HistoryController::getMessages(const JID& baseJID) const { - return localHistory_->getMessages(baseJID); +std::vector HistoryController::getMessages(const JID& selfJID, const JID& contactJID) const { + return localHistory_->getMessages(selfJID, contactJID); } std::vector 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 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 getMessages(const JID& selfJID, const JID& contactJID) const; std::vector 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(newContact); - std::vector messages = historyController_->getMessages(contact->getJID()); + std::vector messages; //TODO = historyController_->getMessages(contact->getJID()); for (std::vector::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 snippet(new MessageSnippet(QString::fromStdString(message.getMessage()), QString::fromStdString(message.getDisplayNick()), QDateTime::currentDateTime(), "", false, false, theme_, "id")); - conversation_->addMessage(snippet); + // TODO + // boost::shared_ptr 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 getMessages(const JID&) const = 0; + virtual std::vector getMessages(const JID& selfJID, const JID& contactJID) const = 0; virtual std::vector 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 #include -#include 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(getIDForJID(message.getBaseJID())) + ", " + - boost::lexical_cast(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(getIDForJID(message.getFromJID().toBare())) + ", '" + + getEscapedString(message.getFromJID().getResource()) + "', " + + boost::lexical_cast(getIDForJID(message.getFromJID().toBare())) + ", '" + + getEscapedString(message.getToJID().getResource()) + "', " + boost::lexical_cast(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 SQLiteHistoryManager::getMessages(const JID& baseJID) const { - std::vector result; +std::vector SQLiteHistoryManager::getMessages(const JID& selfJID, const JID& contactJID) const { sqlite3_stmt* selectStatement; - boost::optional baseID = getIDFromJID(baseJID); - if (!baseID) { + + boost::optional selfID = getIDFromJID(selfJID.toBare()); + boost::optional contactID = getIDFromJID(contactJID.toBare()); + + if (!selfID || !contactID) { + // JIDs missing from the database return std::vector(); } - 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(*selfID) + " AND fromBare=" + + boost::lexical_cast(*contactID) + ") OR (fromBare=" + + boost::lexical_cast(*contactID) + " AND fromBare=" + + boost::lexical_cast(*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 result; while (r == SQLITE_ROW) { - boost::optional baseJID(getJIDFromID(sqlite3_column_int(selectStatement, 0))); - boost::optional from(getJIDFromID(sqlite3_column_int(selectStatement, 1))); - std::string nick(reinterpret_cast(sqlite3_column_text(selectStatement, 2))); - std::string message(reinterpret_cast(sqlite3_column_text(selectStatement, 3))); - int secondsSinceEpoch(sqlite3_column_int(selectStatement, 4)); + std::string message(reinterpret_cast(sqlite3_column_text(selectStatement, 0))); + + // fromJID + boost::optional fromJID(getJIDFromID(sqlite3_column_int(selectStatement, 1))); + std::string fromResource(reinterpret_cast(sqlite3_column_text(selectStatement, 2))); + if (fromJID) { + fromJID = boost::optional(JID(fromJID->getNode(), fromJID->getDomain(), fromResource)); + } + + // toJID + boost::optional toJID(getJIDFromID(sqlite3_column_int(selectStatement, 3))); + std::string toResource(reinterpret_cast(sqlite3_column_text(selectStatement, 4))); + if (toJID) { + toJID = boost::optional(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 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 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 getMessages(const JID&) const; + std::vector getMessages(const JID& selfJID, const JID& contactJID) const; std::vector getAllContacts() const; private: -- cgit v0.10.2-6-g49f6