diff options
Diffstat (limited to 'Swiften/History')
-rw-r--r-- | Swiften/History/HistoryManager.h | 8 | ||||
-rw-r--r-- | Swiften/History/SQLiteHistoryManager.cpp | 36 | ||||
-rw-r--r-- | Swiften/History/SQLiteHistoryManager.h | 4 |
3 files changed, 35 insertions, 13 deletions
diff --git a/Swiften/History/HistoryManager.h b/Swiften/History/HistoryManager.h index 51039e4..908d35c 100644 --- a/Swiften/History/HistoryManager.h +++ b/Swiften/History/HistoryManager.h @@ -7,18 +7,22 @@ #pragma once #include <set> +#include <map> #include <vector> #include <Swiften/JID/JID.h> #include <Swiften/History/HistoryMessage.h> +#include <boost/date_time/gregorian/gregorian_types.hpp> namespace Swift { + typedef std::map<JID, std::set<boost::gregorian::date> > ContactsMap; + class HistoryManager { public: virtual ~HistoryManager(); virtual void addMessage(const HistoryMessage& message) = 0; - virtual std::vector<HistoryMessage> getMessages(const JID& selfJID, const JID& contactJID, HistoryMessage::Type type) const = 0; - virtual std::set<JID> getContacts(const JID& selfJID, HistoryMessage::Type type, const std::string& keyword) const = 0; + virtual std::vector<HistoryMessage> getMessages(const JID& selfJID, const JID& contactJID, HistoryMessage::Type type, const boost::gregorian::date& date) const = 0; + virtual ContactsMap getContacts(const JID& selfJID, HistoryMessage::Type type, const std::string& keyword) const = 0; }; } diff --git a/Swiften/History/SQLiteHistoryManager.cpp b/Swiften/History/SQLiteHistoryManager.cpp index 0a14b99..7cddf43 100644 --- a/Swiften/History/SQLiteHistoryManager.cpp +++ b/Swiften/History/SQLiteHistoryManager.cpp @@ -9,6 +9,7 @@ #include <sqlite3.h> #include <Swiften/History/SQLiteHistoryManager.h> +#include <boost/date_time/gregorian/gregorian_types.hpp> inline std::string getEscapedString(const std::string& s) { std::string result(s); @@ -66,7 +67,7 @@ void SQLiteHistoryManager::addMessage(const HistoryMessage& message) { } } -std::vector<HistoryMessage> SQLiteHistoryManager::getMessages(const JID& selfJID, const JID& contactJID, HistoryMessage::Type type) const { +std::vector<HistoryMessage> SQLiteHistoryManager::getMessages(const JID& selfJID, const JID& contactJID, HistoryMessage::Type type, const boost::gregorian::date& date) const { sqlite3_stmt* selectStatement; boost::optional<int> selfID = getIDFromJID(selfJID.toBare()); @@ -77,12 +78,12 @@ std::vector<HistoryMessage> SQLiteHistoryManager::getMessages(const JID& selfJID return std::vector<HistoryMessage>(); } - std::string selectQuery = "SELECT * FROM messages WHERE type=" + boost::lexical_cast<std::string>(type); + std::string selectQuery = "SELECT * FROM messages WHERE (type=" + boost::lexical_cast<std::string>(type); if (contactJID.isBare()) { // match only bare jid selectQuery += " AND ((fromBare=" + boost::lexical_cast<std::string>(*selfID) + " AND toBare=" + boost::lexical_cast<std::string>(*contactID) + ") OR (fromBare=" + - boost::lexical_cast<std::string>(*contactID) + " AND toBare=" + boost::lexical_cast<std::string>(*selfID) + "))"; + boost::lexical_cast<std::string>(*contactID) + " AND toBare=" + boost::lexical_cast<std::string>(*selfID) + ")))"; } else { // match resource too @@ -91,7 +92,15 @@ std::vector<HistoryMessage> SQLiteHistoryManager::getMessages(const JID& selfJID getEscapedString(contactJID.getResource()) + "')) OR ((fromBare=" + boost::lexical_cast<std::string>(*contactID) + " AND fromResource='" + getEscapedString(contactJID.getResource()) + "') AND toBare=" + - boost::lexical_cast<std::string>(*selfID) + "))"; + boost::lexical_cast<std::string>(*selfID) + ")))"; + } + + if (!date.is_not_a_date()) { + int lowerBound = (boost::posix_time::ptime(date) - boost::posix_time::ptime(boost::gregorian::date(1970, 1, 1))).total_seconds(); + int upperBound = lowerBound + 86400; + + selectQuery += " AND (time>=" + boost::lexical_cast<std::string>(lowerBound) + + " AND time<" + boost::lexical_cast<std::string>(upperBound) + ")"; } int r = sqlite3_prepare(db_, selectQuery.c_str(), selectQuery.size(), &selectStatement, NULL); @@ -190,8 +199,8 @@ boost::optional<int> SQLiteHistoryManager::getIDFromJID(const JID& jid) const { return result; } -std::set<JID> SQLiteHistoryManager::getContacts(const JID& selfJID, HistoryMessage::Type type, const std::string& keyword) const { - std::set<JID> result; +ContactsMap SQLiteHistoryManager::getContacts(const JID& selfJID, HistoryMessage::Type type, const std::string& keyword) const { + ContactsMap result; sqlite3_stmt* selectStatement; // get id @@ -201,9 +210,15 @@ std::set<JID> SQLiteHistoryManager::getContacts(const JID& selfJID, HistoryMessa } // get contacts - std::string query = "SELECT DISTINCT messages.'fromBare', messages.'fromResource', messages.'toBare', messages.'toResource' FROM messages WHERE type=" + std::string query = "SELECT DISTINCT messages.'fromBare', messages.'fromResource', messages.'toBare', messages.'toResource', messages.'time' " + "FROM messages WHERE (type=" + boost::lexical_cast<std::string>(type) + " AND (toBare=" - + boost::lexical_cast<std::string>(*id) + " OR fromBare=" + boost::lexical_cast<std::string>(*id) + ")"; + + boost::lexical_cast<std::string>(*id) + " OR fromBare=" + boost::lexical_cast<std::string>(*id) + "))"; + + // match keyword + if (getEscapedString(keyword).length()) { + query += " AND message LIKE '%" + getEscapedString(keyword) + "%'"; + } int r = sqlite3_prepare(db_, query.c_str(), query.size(), &selectStatement, NULL); if (r != SQLITE_OK) { @@ -218,6 +233,9 @@ std::set<JID> SQLiteHistoryManager::getContacts(const JID& selfJID, HistoryMessa std::string toResource(reinterpret_cast<const char*>(sqlite3_column_text(selectStatement, 3))); std::string resource; + int secondsSinceEpoch(sqlite3_column_int(selectStatement, 4)); + boost::posix_time::ptime time(boost::gregorian::date(1970, 1, 1), boost::posix_time::seconds(secondsSinceEpoch)); + boost::optional<JID> contactJID; if (fromBareID == *id) { @@ -235,7 +253,7 @@ std::set<JID> SQLiteHistoryManager::getContacts(const JID& selfJID, HistoryMessa } if (contactJID) { - result.insert(*contactJID); + result[*contactJID].insert(time.date()); } r = sqlite3_step(selectStatement); diff --git a/Swiften/History/SQLiteHistoryManager.h b/Swiften/History/SQLiteHistoryManager.h index a02ecc3..638d73f 100644 --- a/Swiften/History/SQLiteHistoryManager.h +++ b/Swiften/History/SQLiteHistoryManager.h @@ -19,8 +19,8 @@ namespace Swift { ~SQLiteHistoryManager(); void addMessage(const HistoryMessage& message); - std::vector<HistoryMessage> getMessages(const JID& selfJID, const JID& contactJID, HistoryMessage::Type type) const; - std::set<JID> getContacts(const JID& selfJID, HistoryMessage::Type type, const std::string& keyword) const; + ContactsMap getContacts(const JID& selfJID, HistoryMessage::Type type, const std::string& keyword) const; + std::vector<HistoryMessage> getMessages(const JID& selfJID, const JID& contactJID, HistoryMessage::Type type, const boost::gregorian::date& date) const; private: int getIDForJID(const JID&); |