From 0998d6a8f138e0fd00245fc3c1efc8878fa7b888 Mon Sep 17 00:00:00 2001 From: Catalin Badea Date: Wed, 27 Jun 2012 15:02:47 +0300 Subject: Log messages to the sqlite db and dump all messages in the history viewer diff --git a/BuildTools/SCons/SConstruct b/BuildTools/SCons/SConstruct index 5640877..8f1f725 100644 --- a/BuildTools/SCons/SConstruct +++ b/BuildTools/SCons/SConstruct @@ -360,21 +360,21 @@ else : env["LIBNATPMP_FLAGS"] = {} # SQLite -#sqlite_conf_env = conf_env.Clone() -#sqlite_flags = {} -#if env.get("sqlite_libdir", None) : -# sqlite_flags["LIBPATH"] = [env["sqlite_libdir"]] -#if env.get("sqlite_includedir", None) : -# sqlite_flags["CPPPATH"] = [env["sqlite_includedir"]] -#sqlite_conf_env.MergeFlags(sqlite_flags) -#conf = Configure(sqlite_conf_env) -#if conf.CheckCHeader("sqlite3.h") and conf.CheckLib(env["sqlite_libname"]) : -# env["HAVE_SQLITE"] = 1 -# env["SQLITE_FLAGS"] = { "LIBS": [env["sqlite_libname"]] } -# env["SQLITE_FLAGS"].update(sqlite_flags) -#else : -# env["SQLITE_BUNDLED"] = 1 -#conf.Finish() +sqlite_conf_env = conf_env.Clone() +sqlite_flags = {} +if env.get("sqlite_libdir", None) : + sqlite_flags["LIBPATH"] = [env["sqlite_libdir"]] +if env.get("sqlite_includedir", None) : + sqlite_flags["CPPPATH"] = [env["sqlite_includedir"]] +sqlite_conf_env.MergeFlags(sqlite_flags) +conf = Configure(sqlite_conf_env) +if conf.CheckCHeader("sqlite3.h") and conf.CheckLib(env["sqlite_libname"]) : + env["HAVE_SQLITE"] = 1 + env["SQLITE_FLAGS"] = { "LIBS": [env["sqlite_libname"]] } + env["SQLITE_FLAGS"].update(sqlite_flags) +else : + env["SQLITE_BUNDLED"] = 1 +conf.Finish() # Lua env["LUA_BUNDLED"] = 1 diff --git a/Swift/Controllers/Chat/ChatControllerBase.cpp b/Swift/Controllers/Chat/ChatControllerBase.cpp index bf39216..302f793 100644 --- a/Swift/Controllers/Chat/ChatControllerBase.cpp +++ b/Swift/Controllers/Chat/ChatControllerBase.cpp @@ -145,7 +145,7 @@ void ChatControllerBase::handleSendMessageRequest(const std::string &body, bool onActivity(message->getBody()); // log message - historyController_->addMessage(getBaseJID(), selfJID_, toJID_, body, now); + historyController_->addMessage(getBaseJID(), selfJID_, std::string("me"), body, now); } void ChatControllerBase::handleSecurityLabelsCatalogResponse(boost::shared_ptr catalog, ErrorPayload::ref error) { @@ -252,8 +252,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); - // void HistoryController::addMessage(const JID& baseJID, const JID& fromJID, const JID& toJID, std::string messageBody, boost::posix_time::ptime timeStamp) { - historyController_->addMessage(getBaseJID(), from, message->getTo(), body, timeStamp); + historyController_->addMessage(getBaseJID(), from, senderDisplayNameFromMessage(from), body, timeStamp); } } chatWindow_->show(); diff --git a/Swift/Controllers/HistoryController.cpp b/Swift/Controllers/HistoryController.cpp index a19e321..c82892a 100644 --- a/Swift/Controllers/HistoryController.cpp +++ b/Swift/Controllers/HistoryController.cpp @@ -6,19 +6,26 @@ #include #include +#include namespace Swift { - HistoryController::HistoryController() : remoteArchiveSupported_(false) { - std::string file("testDB.db"); - localHistory_ = new SQLiteHistoryManager(file); - } - HistoryController::~HistoryController() { - delete localHistory_; - } +HistoryController::HistoryController() : remoteArchiveSupported_(false) { + std::string file("testDB.db"); + localHistory_ = new SQLiteHistoryManager(file); +} + +HistoryController::~HistoryController() { + delete localHistory_; +} - void HistoryController::addMessage(const JID& baseJID, const JID& fromJID, const JID& toJID, std::string messageBody, boost::posix_time::ptime timeStamp) { - std::cout << baseJID << " " << fromJID << " " << toJID << " " << messageBody << " " << to_simple_string(timeStamp); - } +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); +} + +std::vector HistoryController::getMessages() const { + return localHistory_->getMessages(); +} } diff --git a/Swift/Controllers/HistoryController.h b/Swift/Controllers/HistoryController.h index 9949c19..6ead3f1 100644 --- a/Swift/Controllers/HistoryController.h +++ b/Swift/Controllers/HistoryController.h @@ -8,9 +8,11 @@ #include #include +#include namespace Swift { class HistoryManager; + class HistoryMessage; class JID; class HistoryController { @@ -18,7 +20,8 @@ namespace Swift { HistoryController(); ~HistoryController(); - void addMessage(const JID& baseJID, const JID& fromJID, const JID& toJID, std::string messageBody, boost::posix_time::ptime timeStamp); + 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; private: HistoryManager* localHistory_; diff --git a/Swift/Controllers/HistoryViewController.cpp b/Swift/Controllers/HistoryViewController.cpp index cbfa687..1061e78 100644 --- a/Swift/Controllers/HistoryViewController.cpp +++ b/Swift/Controllers/HistoryViewController.cpp @@ -8,10 +8,19 @@ #include #include +#include +#include namespace Swift { -HistoryViewController::HistoryViewController(UIEventStream* uiEventStream, HistoryWindowFactory* historyWindowFactory) : uiEventStream_(uiEventStream), historyWindowFactory_(historyWindowFactory), historyWindow_(NULL) { +HistoryViewController::HistoryViewController( + UIEventStream* uiEventStream, + HistoryController* historyController, + HistoryWindowFactory* historyWindowFactory) : + uiEventStream_(uiEventStream), + historyController_(historyController), + historyWindowFactory_(historyWindowFactory), + historyWindow_(NULL) { uiEventStream_->onUIEvent.connect(boost::bind(&HistoryViewController::handleUIEvent, this, _1)); } @@ -37,6 +46,11 @@ void HistoryViewController::handleUIEvent(boost::shared_ptr rawEvent) { roster_->addContact(medvedev, medvedev, "Dmitri Medvedev", "Recent", ""); roster_->addContact(kev, kev, "Kev", "Recent", ""); } + + std::vector messages = historyController_->getMessages(); + for (std::vector::iterator it = messages.begin(); it != messages.end(); it++) { + historyWindow_->addMessage(*it); + } historyWindow_->activate(); } } diff --git a/Swift/Controllers/HistoryViewController.h b/Swift/Controllers/HistoryViewController.h index 08e6324..f64dd35 100644 --- a/Swift/Controllers/HistoryViewController.h +++ b/Swift/Controllers/HistoryViewController.h @@ -16,10 +16,11 @@ namespace Swift { class HistoryWindowFactory; class HistoryWindow; class Roster; + class HistoryController; class HistoryViewController { public: - HistoryViewController(UIEventStream* uiEventStream, HistoryWindowFactory* historyWindowFactory); + HistoryViewController(UIEventStream* uiEventStream, HistoryController* historyController, HistoryWindowFactory* historyWindowFactory); ~HistoryViewController(); private: @@ -27,6 +28,7 @@ namespace Swift { private: UIEventStream* uiEventStream_; + HistoryController* historyController_; HistoryWindowFactory* historyWindowFactory_; HistoryWindow* historyWindow_; Roster* roster_; diff --git a/Swift/Controllers/MainController.cpp b/Swift/Controllers/MainController.cpp index 295ad3e..e376dc2 100644 --- a/Swift/Controllers/MainController.cpp +++ b/Swift/Controllers/MainController.cpp @@ -172,7 +172,7 @@ MainController::MainController( xmlConsoleController_ = new XMLConsoleController(uiEventStream_, uiFactory_); historyController_ = new HistoryController(); - historyViewController_ = new HistoryViewController(uiEventStream_, uiFactory_); + historyViewController_ = new HistoryViewController(uiEventStream_, historyController_, uiFactory_); fileTransferListController_ = new FileTransferListController(uiEventStream_, uiFactory_); diff --git a/Swift/Controllers/UIInterfaces/HistoryWindow.h b/Swift/Controllers/UIInterfaces/HistoryWindow.h index 713f986..9ec79e2 100644 --- a/Swift/Controllers/UIInterfaces/HistoryWindow.h +++ b/Swift/Controllers/UIInterfaces/HistoryWindow.h @@ -9,11 +9,14 @@ #include namespace Swift { + class HistoryMessage; + class HistoryWindow { public: virtual ~HistoryWindow() {}; virtual void activate() = 0; virtual void setRosterModel(Roster*) = 0; + virtual void addMessage(const HistoryMessage& message) = 0; }; } diff --git a/Swift/QtUI/QtHistoryWindow.cpp b/Swift/QtUI/QtHistoryWindow.cpp index 4b459de..d067190 100644 --- a/Swift/QtUI/QtHistoryWindow.cpp +++ b/Swift/QtUI/QtHistoryWindow.cpp @@ -9,6 +9,7 @@ #include #include +#include #include #include @@ -19,10 +20,10 @@ namespace Swift { QtHistoryWindow::QtHistoryWindow(SettingsProvider* settings, UIEventStream* eventStream) { ui_.setupUi(this); - QtChatTheme* theme = new QtChatTheme(""); // FIXME: leak + theme_ = new QtChatTheme(""); delete ui_.conversation_; - conversation_ = new QtChatView(theme, this); + conversation_ = new QtChatView(theme_, this); QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); sizePolicy.setHorizontalStretch(80); sizePolicy.setVerticalStretch(0); @@ -40,18 +41,11 @@ QtHistoryWindow::QtHistoryWindow(SettingsProvider* settings, UIEventStream* even ui_.bottomLeftLayout_->setDirection(QBoxLayout::BottomToTop); ui_.bottomLeftLayout_->addWidget(conversationRoster_); - conversation_->addMessage(boost::make_shared("Hi", "Me", QDateTime::currentDateTime(), "http://swarm.cs.pub.ro/~cbadea/swift/iron-man.png", false, false, theme, "id")); - conversation_->addMessage(boost::make_shared("Hi", "You", QDateTime::currentDateTime(), "http://swarm.cs.pub.ro/~cbadea/swift/putin.png", true, false, theme, "id2")); - conversation_->addMessage(boost::make_shared("How is it going?", "Me", QDateTime::currentDateTime(), "http://swarm.cs.pub.ro/~cbadea/swift/iron-man.png", false, false, theme, "id")); - conversation_->addMessage(boost::make_shared("Fine, just going through some documents.", "You", QDateTime::currentDateTime(), "http://swarm.cs.pub.ro/~cbadea/swift/putin.png", true, false, theme, "id2")); - conversation_->addMessage(boost::make_shared("Cool. Hey, do you want to go for a beer?", "Me", QDateTime::currentDateTime(), "http://swarm.cs.pub.ro/~cbadea/swift/iron-man.png", false, false, theme, "id")); - conversation_->addMessage(boost::make_shared("Sure. Meet me at the pub around 10?", "You", QDateTime::currentDateTime(), "http://swarm.cs.pub.ro/~cbadea/swift/putin.png", true, false, theme, "id2")); - conversation_->addMessage(boost::make_shared("See you there.", "Me", QDateTime::currentDateTime(), "http://swarm.cs.pub.ro/~cbadea/swift/iron-man.png", false, false, theme, "id")); - setWindowTitle(tr("History")); } QtHistoryWindow::~QtHistoryWindow() { + delete theme_; } void QtHistoryWindow::activate() { @@ -73,4 +67,9 @@ void QtHistoryWindow::setRosterModel(Roster* model) { conversationRoster_->setRosterModel(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); +} + } diff --git a/Swift/QtUI/QtHistoryWindow.h b/Swift/QtUI/QtHistoryWindow.h index d61fb49..d522f53 100644 --- a/Swift/QtUI/QtHistoryWindow.h +++ b/Swift/QtUI/QtHistoryWindow.h @@ -25,12 +25,14 @@ namespace Swift { ~QtHistoryWindow(); void activate(); void setRosterModel(Roster*); + void addMessage(const HistoryMessage& message); private: virtual void closeEvent(QCloseEvent* event); virtual void showEvent(QShowEvent* event); Ui::QtHistoryWindow ui_; + QtChatTheme* theme_; QtChatView* conversation_; QtRosterWidget* conversationRoster_; }; diff --git a/Swiften/History/HistoryMessage.h b/Swiften/History/HistoryMessage.h index 461f5de..03e9206 100644 --- a/Swiften/History/HistoryMessage.h +++ b/Swiften/History/HistoryMessage.h @@ -12,19 +12,33 @@ namespace Swift { class HistoryMessage { public: - HistoryMessage(const std::string& message, const JID& from, const JID& to, const boost::posix_time::ptime time) : message_(message), from_(from), to_(to), time_(time) { + HistoryMessage( + const std::string& message, + const JID& baseJID, + const JID& fromJID, + const std::string& displayNick, + const boost::posix_time::ptime time) : + message_(message), + baseJID_(baseJID), + fromJID_(fromJID), + displayNick_(displayNick), + time_(time) { } const std::string& getMessage() const { return message_; } - const JID& getFrom() const { - return from_; + const JID& getBaseJID() const { + return baseJID_; } - const JID& getTo() const { - return to_; + const JID& getFromJID() const { + return fromJID_; + } + + const std::string& getDisplayNick() const { + return displayNick_; } boost::posix_time::ptime getTime() const { @@ -32,13 +46,14 @@ namespace Swift { } bool operator==(const HistoryMessage& o) const { - return message_ == o.message_ && from_ == o.from_ && to_ == o.to_ && time_ == o.time_; + return baseJID_ == o.baseJID_ && message_ == o.message_ && fromJID_ == o.fromJID_ && displayNick_ == o.displayNick_ && time_ == o.time_; } private: std::string message_; - JID from_; - JID to_; + JID baseJID_; + JID fromJID_; + std::string displayNick_; boost::posix_time::ptime time_; }; } diff --git a/Swiften/History/SQLiteHistoryManager.cpp b/Swiften/History/SQLiteHistoryManager.cpp index a24dec2..e10de07 100644 --- a/Swiften/History/SQLiteHistoryManager.cpp +++ b/Swiften/History/SQLiteHistoryManager.cpp @@ -25,7 +25,7 @@ SQLiteHistoryManager::SQLiteHistoryManager(const std::string& file) : db_(0) { } char* errorMessage; - int result = sqlite3_exec(db_, "CREATE TABLE IF NOT EXISTS messages('from' INTEGER, 'to' INTEGER, 'message' STRING, 'time' INTEGER)", 0, 0, &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); if (result != SQLITE_OK) { std::cerr << "SQL Error: " << errorMessage << std::endl; sqlite3_free(errorMessage); @@ -44,7 +44,12 @@ SQLiteHistoryManager::~SQLiteHistoryManager() { void SQLiteHistoryManager::addMessage(const HistoryMessage& 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('from', 'to', 'message', 'time') VALUES(") + boost::lexical_cast(getIDForJID(message.getFrom())) + ", " + boost::lexical_cast(getIDForJID(message.getTo())) + ", '" + getEscapedString(message.getMessage()) + "', " + boost::lexical_cast(secondsSinceEpoch) + ")"; + 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()) + "', " + + boost::lexical_cast(secondsSinceEpoch) + ")"; char* errorMessage; int result = sqlite3_exec(db_, statement.c_str(), 0, 0, &errorMessage); if (result != SQLITE_OK) { @@ -56,20 +61,21 @@ void SQLiteHistoryManager::addMessage(const HistoryMessage& message) { std::vector SQLiteHistoryManager::getMessages() const { std::vector result; sqlite3_stmt* selectStatement; - std::string selectQuery("SELECT messages.'from', messages.'to', messages.'message', messages.'time' FROM messages"); + 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); if (r != SQLITE_OK) { std::cout << "Error: " << sqlite3_errmsg(db_) << std::endl; } r = sqlite3_step(selectStatement); while (r == SQLITE_ROW) { - boost::optional from(getJIDFromID(sqlite3_column_int(selectStatement, 0))); - boost::optional to(getJIDFromID(sqlite3_column_int(selectStatement, 1))); - std::string message(reinterpret_cast(sqlite3_column_text(selectStatement, 2))); - int secondsSinceEpoch(sqlite3_column_int(selectStatement, 3)); + 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)); boost::posix_time::ptime time(boost::gregorian::date(1970, 1, 1), boost::posix_time::seconds(secondsSinceEpoch)); - result.push_back(HistoryMessage(message, (from ? *from : JID()), (to ? *to : JID()), time)); + result.push_back(HistoryMessage(message, (baseJID ? *baseJID : JID()), (from ? *from : JID()), nick, time)); r = sqlite3_step(selectStatement); } if (r != SQLITE_DONE) { diff --git a/Swiften/SConscript b/Swiften/SConscript index 340e8a5..9b09977 100644 --- a/Swiften/SConscript +++ b/Swiften/SConscript @@ -6,7 +6,7 @@ Import("env") # Flags ################################################################################ -swiften_dep_modules = ["BOOST", "GCONF", "ICU", "LIBIDN", "ZLIB", "OPENSSL", "LIBXML", "EXPAT", "AVAHI", "LIBMINIUPNPC", "LIBNATPMP"] +swiften_dep_modules = ["BOOST", "GCONF", "ICU", "LIBIDN", "ZLIB", "OPENSSL", "LIBXML", "EXPAT", "AVAHI", "LIBMINIUPNPC", "LIBNATPMP", "SQLITE"] if env["SCONS_STAGE"] == "flags" : env["SWIFTEN_VERSION"] = Version.getBuildVersion(env.Dir("#").abspath, "swift") -- cgit v0.10.2-6-g49f6