diff options
Diffstat (limited to 'Swiften')
-rw-r--r-- | Swiften/History/HistoryManager.h | 4 | ||||
-rw-r--r-- | Swiften/History/HistoryMessage.h | 18 | ||||
-rw-r--r-- | Swiften/History/SQLiteHistoryManager.cpp | 58 | ||||
-rw-r--r-- | Swiften/History/SQLiteHistoryManager.h | 4 |
4 files changed, 36 insertions, 48 deletions
diff --git a/Swiften/History/HistoryManager.h b/Swiften/History/HistoryManager.h index a10bb41..bc1680b 100644 --- a/Swiften/History/HistoryManager.h +++ b/Swiften/History/HistoryManager.h @@ -18,7 +18,7 @@ namespace Swift { virtual void addMessage(const HistoryMessage& message) = 0; - virtual std::vector<HistoryMessage> getMessages(const JID& selfJID, const JID& contactJID, bool isGroupChat) const = 0; - virtual void getAllContacts(const JID& selfJID, std::set<JID>& mucs, std::set<JID>& contacts) const = 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 = 0; }; } diff --git a/Swiften/History/HistoryMessage.h b/Swiften/History/HistoryMessage.h index 021d6b9..d35474f 100644 --- a/Swiften/History/HistoryMessage.h +++ b/Swiften/History/HistoryMessage.h @@ -12,16 +12,22 @@ namespace Swift { class HistoryMessage { public: + enum Type { + Chat = 0, + Groupchat = 1, + PrivateMessage = 2 + }; + HistoryMessage( const std::string& message, const JID& fromJID, const JID& toJID, - bool isGroupChat, + Type type, const boost::posix_time::ptime& time) : message_(message), fromJID_(fromJID), toJID_(toJID), - isGroupChat_(isGroupChat), + type_(type), time_(time) { } @@ -37,8 +43,8 @@ namespace Swift { return toJID_; } - bool isGroupChat() const { - return isGroupChat_; + Type getType() const { + return type_; } boost::posix_time::ptime getTime() const { @@ -46,14 +52,14 @@ namespace Swift { } bool operator==(const HistoryMessage& o) const { - return message_ == o.message_ && fromJID_ == o.fromJID_ && toJID_ == o.toJID_ && isGroupChat_ == o.isGroupChat_ && time_ == o.time_; + return message_ == o.message_ && fromJID_ == o.fromJID_ && toJID_ == o.toJID_ && type_ == o.type_ && time_ == o.time_; } private: std::string message_; JID fromJID_; JID toJID_; - bool isGroupChat_; // TODO: <- maybe use Message::Type ? + Type type_; boost::posix_time::ptime time_; }; } diff --git a/Swiften/History/SQLiteHistoryManager.cpp b/Swiften/History/SQLiteHistoryManager.cpp index af09b66..3304a07 100644 --- a/Swiften/History/SQLiteHistoryManager.cpp +++ b/Swiften/History/SQLiteHistoryManager.cpp @@ -30,7 +30,7 @@ SQLiteHistoryManager::SQLiteHistoryManager(const std::string& file) : db_(0) { } char* errorMessage; - int result = sqlite3_exec(db_, "CREATE TABLE IF NOT EXISTS messages('message' STRING, 'fromBare' INTEGER, 'fromResource' STRING, 'toBare' INTEGER, 'toResource' STRING, 'groupChat' INTEGER, '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, 'type' INTEGER, 'time' INTEGER)", 0, 0, &errorMessage); if (result != SQLITE_OK) { std::cerr << "SQL Error: " << errorMessage << std::endl; sqlite3_free(errorMessage); @@ -50,13 +50,13 @@ 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('message', 'fromBare', 'fromResource', 'toBare', 'toResource', 'groupChat', 'time') VALUES(") + + std::string statement = std::string("INSERT INTO messages('message', 'fromBare', 'fromResource', 'toBare', 'toResource', 'type', '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.getToJID().toBare())) + ", '" + getEscapedString(message.getToJID().getResource()) + "', " + - boost::lexical_cast<std::string>(message.isGroupChat()) + ", " + + boost::lexical_cast<std::string>(message.getType()) + ", " + boost::lexical_cast<std::string>(secondsSinceEpoch) + ")"; char* errorMessage; int result = sqlite3_exec(db_, statement.c_str(), 0, 0, &errorMessage); @@ -66,7 +66,7 @@ void SQLiteHistoryManager::addMessage(const HistoryMessage& message) { } } -std::vector<HistoryMessage> SQLiteHistoryManager::getMessages(const JID& selfJID, const JID& contactJID, bool isGroupChat) const { +std::vector<HistoryMessage> SQLiteHistoryManager::getMessages(const JID& selfJID, const JID& contactJID, HistoryMessage::Type type) const { sqlite3_stmt* selectStatement; boost::optional<int> selfID = getIDFromJID(selfJID.toBare()); @@ -77,7 +77,7 @@ std::vector<HistoryMessage> SQLiteHistoryManager::getMessages(const JID& selfJID return std::vector<HistoryMessage>(); } - std::string selectQuery = "SELECT * FROM messages WHERE groupChat=" + boost::lexical_cast<std::string>(isGroupChat); + 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=" + @@ -120,13 +120,13 @@ std::vector<HistoryMessage> SQLiteHistoryManager::getMessages(const JID& selfJID } // message type - bool isGroupChat = sqlite3_column_int(selectStatement, 5); + HistoryMessage::Type type = static_cast<HistoryMessage::Type>(sqlite3_column_int(selectStatement, 5)); // timestamp int secondsSinceEpoch(sqlite3_column_int(selectStatement, 6)); boost::posix_time::ptime time(boost::gregorian::date(1970, 1, 1), boost::posix_time::seconds(secondsSinceEpoch)); - result.push_back(HistoryMessage(message, (fromJID ? *fromJID : JID()), (toJID ? *toJID : JID()), isGroupChat, time)); + result.push_back(HistoryMessage(message, (fromJID ? *fromJID : JID()), (toJID ? *toJID : JID()), type, time)); r = sqlite3_step(selectStatement); } if (r != SQLITE_DONE) { @@ -190,42 +190,22 @@ boost::optional<int> SQLiteHistoryManager::getIDFromJID(const JID& jid) const { return result; } -void SQLiteHistoryManager::getAllContacts(const JID& selfJID, std::set<JID>& mucs, std::set<JID>& contacts) const { +std::set<JID> SQLiteHistoryManager::getContacts(const JID& selfJID, HistoryMessage::Type type) const { + std::set<JID> result; + sqlite3_stmt* selectStatement; + // get id boost::optional<int> id = getIDFromJID(selfJID); if (!id) { - return; + return result; } - // get all MUCs - std::string query = "SELECT DISTINCT messages.'fromBare' FROM messages WHERE groupChat=1 AND toBare=" + boost::lexical_cast<std::string>(*id); - sqlite3_stmt* selectStatement; - - int r = sqlite3_prepare(db_, query.c_str(), query.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<JID> contactJID(getJIDFromID(sqlite3_column_int(selectStatement, 0))); - if (contactJID) { - mucs.insert(*contactJID); - } - - r = sqlite3_step(selectStatement); - } - - if (r != SQLITE_DONE) { - std::cout << "Error: " << sqlite3_errmsg(db_) << std::endl; - } - sqlite3_finalize(selectStatement); - - // get all contacts - query = "SELECT DISTINCT messages.'fromBare', messages.'fromResource', messages.'toBare', messages.'toResource' FROM messages WHERE groupChat=0 AND (toBare=" + // get contacts + std::string query = "SELECT DISTINCT messages.'fromBare', messages.'fromResource', messages.'toBare', messages.'toResource' 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) + ")"; - r = sqlite3_prepare(db_, query.c_str(), query.size(), &selectStatement, NULL); + int r = sqlite3_prepare(db_, query.c_str(), query.size(), &selectStatement, NULL); if (r != SQLITE_OK) { std::cout << "Error: " << sqlite3_errmsg(db_) << std::endl; } @@ -250,12 +230,12 @@ void SQLiteHistoryManager::getAllContacts(const JID& selfJID, std::set<JID>& muc } // check if it is a MUC contact (from a private conversation) - if (contactJID && mucs.count(*contactJID)) { + if (type == HistoryMessage::PrivateMessage) { contactJID = boost::optional<JID>(JID(contactJID->getNode(), contactJID->getDomain(), resource)); } if (contactJID) { - contacts.insert(*contactJID); + result.insert(*contactJID); } r = sqlite3_step(selectStatement); @@ -265,6 +245,8 @@ void SQLiteHistoryManager::getAllContacts(const JID& selfJID, std::set<JID>& muc std::cout << "Error: " << sqlite3_errmsg(db_) << std::endl; } sqlite3_finalize(selectStatement); + + return result; } } diff --git a/Swiften/History/SQLiteHistoryManager.h b/Swiften/History/SQLiteHistoryManager.h index 480d570..fce30f8 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, bool isGroupChat) const; - void getAllContacts(const JID& selfJID, std::set<JID>& mucs, std::set<JID>& contacts) const; + 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; private: int getIDForJID(const JID&); |