summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swiften/History/SQLiteHistoryManager.cpp')
-rw-r--r--Swiften/History/SQLiteHistoryManager.cpp36
1 files changed, 27 insertions, 9 deletions
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);