From b94ffc40e941d6d862d40a09405ae31b74640152 Mon Sep 17 00:00:00 2001 From: neonew Date: Sat, 4 Nov 2017 22:27:04 +0100 Subject: [PATCH] #45 show message counts (sent/received) in chat list --- source/Platforms/Win32/GUI/MainWindow.cpp | 31 +++++++++++++++++-- source/WhatsApp/Chat.cpp | 14 +++++++-- source/WhatsApp/Chat.h | 7 ++++- source/WhatsApp/Database.cpp | 36 ++++++++++++++++++++++- source/WhatsApp/Database.h | 1 + 5 files changed, 82 insertions(+), 7 deletions(-) diff --git a/source/Platforms/Win32/GUI/MainWindow.cpp b/source/Platforms/Win32/GUI/MainWindow.cpp index 915894f..0dd9a66 100644 --- a/source/Platforms/Win32/GUI/MainWindow.cpp +++ b/source/Platforms/Win32/GUI/MainWindow.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -154,10 +155,10 @@ void MainWindow::createChildWindows() CreateWindowEx(0, STATUSCLASSNAME, L"file manager", WS_CHILD, 0, 0, 0, 0, dialog, reinterpret_cast(IDC_MAIN_STATUS), GetModuleHandle(NULL), 0); // create list view columns - WCHAR columnsStrings[][256] = { L"phone number", L"last message" }; - DWORD columnsWidths[] = { 220, 140 }; + WCHAR columnsStrings[][256] = { L"phone number", L"last message", L"messages (sent / received)" }; + DWORD columnsWidths[] = { 220, 140, 180 }; - for (DWORD i = 0; i < 2; i++) + for (DWORD i = 0; i < 3; i++) { LVCOLUMN column; ZeroMemory(&column, sizeof(LVCOLUMN)); @@ -211,6 +212,11 @@ void MainWindow::addChat(WhatsappChat &chat) std::wstring text = strtowstr(chat.getDisplayName()); std::wstring lastMessageText = strtowstr(formatTimestamp(chat.getLastMessage())); + std::stringstream messagesText; + messagesText.imbue(std::locale("")); + messagesText << std::fixed << chat.getMessagesSent() + chat.getMessagesReceived() << " (" << chat.getMessagesSent() << " / " << chat.getMessagesReceived() << ")"; + std::wstring messagesTextW = strtowstr(messagesText.str()); + LVITEM item; ZeroMemory(&item, sizeof(LVITEM)); @@ -221,6 +227,7 @@ void MainWindow::addChat(WhatsappChat &chat) ListView_InsertItem(GetDlgItem(dialog, IDC_MAIN_CHATS), &item); ListView_SetItemText(GetDlgItem(dialog, IDC_MAIN_CHATS), item.iItem, 1, const_cast(lastMessageText.c_str())); + ListView_SetItemText(GetDlgItem(dialog, IDC_MAIN_CHATS), item.iItem, 2, const_cast(messagesTextW.c_str())); } void MainWindow::selectChat(WhatsappChat *chat) @@ -288,6 +295,24 @@ int CALLBACK MainWindow::sortingCallback(LPARAM lParam1, LPARAM lParam2, LPARAM result = 1; } } break; + case 2: + { + int messages1 = chat1->getMessagesSent() + chat1->getMessagesReceived(); + int messages2 = chat2->getMessagesSent() + chat2->getMessagesReceived(); + + if (messages1 > messages2) + { + result = 1; + } + else if (messages1 < messages2) + { + result = -1; + } + else + { + result = 0; + } + } break; } if (mainWindow->sortingDirection == SORTING_DIRECTION_ASCENDING) diff --git a/source/WhatsApp/Chat.cpp b/source/WhatsApp/Chat.cpp index 4703b2b..d3713e4 100644 --- a/source/WhatsApp/Chat.cpp +++ b/source/WhatsApp/Chat.cpp @@ -3,8 +3,8 @@ #include "Message.h" #include "../VectorUtils.h" -WhatsappChat::WhatsappChat(WhatsappDatabase &database, const std::string &displayName, const std::string &key, const std::string &subject, long long creation, long long lastMessage) - : database(database), displayName(displayName), key(key), subject(subject), creation(creation), lastMessage(lastMessage), messagesLoaded(false) +WhatsappChat::WhatsappChat(WhatsappDatabase &database, const std::string &displayName, const std::string &key, const std::string &subject, long long creation, long long lastMessage, int messagesSent, int messagesReceived) + : database(database), displayName(displayName), key(key), subject(subject), creation(creation), lastMessage(lastMessage), messagesSent(messagesSent), messagesReceived(messagesReceived), messagesLoaded(false) { } @@ -38,6 +38,16 @@ long long WhatsappChat::getLastMessage() const return lastMessage; } +int WhatsappChat::getMessagesSent() const +{ + return messagesSent; +} + +int WhatsappChat::getMessagesReceived() const +{ + return messagesReceived; +} + std::vector WhatsappChat::getMessages(const volatile bool &running) { if (!messagesLoaded) diff --git a/source/WhatsApp/Chat.h b/source/WhatsApp/Chat.h index 533dd8d..0dfffed 100644 --- a/source/WhatsApp/Chat.h +++ b/source/WhatsApp/Chat.h @@ -17,11 +17,14 @@ class WhatsappChat long long creation; long long lastMessage; + int messagesSent; + int messagesReceived; + bool messagesLoaded; std::vector messages; public: - WhatsappChat(WhatsappDatabase &database, const std::string &displayName, const std::string &key, const std::string &subject, long long creation, long long lastMessage); + WhatsappChat(WhatsappDatabase &database, const std::string &displayName, const std::string &key, const std::string &subject, long long creation, long long lastMessage, int messagesSent, int messagesReceived); ~WhatsappChat(); const std::string& getDisplayName() const; @@ -29,6 +32,8 @@ class WhatsappChat const std::string& getSubject() const; long long getCreation() const; long long getLastMessage() const; + int getMessagesSent() const; + int getMessagesReceived() const; std::vector getMessages(const volatile bool &running); }; diff --git a/source/WhatsApp/Database.cpp b/source/WhatsApp/Database.cpp index 3fa95ff..9c793fe 100644 --- a/source/WhatsApp/Database.cpp +++ b/source/WhatsApp/Database.cpp @@ -42,13 +42,47 @@ void WhatsappDatabase::getChats(Settings &settings, std::vector & long long lastMessage = sqlite3_column_int64(res, 3); std::string displayName = settings.findDisplayName(key); - WhatsappChat *chat = new WhatsappChat(*this, displayName, key, subject, creation, lastMessage); + int messagesSent = messagesCount(key, 1); + int messagesReceived = messagesCount(key, 0); + + WhatsappChat *chat = new WhatsappChat(*this, displayName, key, subject, creation, lastMessage, messagesSent, messagesReceived); chats.push_back(chat); } sqlite3_finalize(res); } +int WhatsappDatabase::messagesCount(const std::string &chatId, int fromMe) +{ + const char *query = "SELECT count(_id) from messages where key_remote_jid = ? and key_from_me = ?"; + + sqlite3_stmt *res; + if (sqlite3_prepare_v2(database.getHandle(), query, -1, &res, NULL) != SQLITE_OK) + { + throw SQLiteException("Could not load messages", database); + } + + if (sqlite3_bind_text(res, 1, chatId.c_str(), -1, SQLITE_STATIC) != SQLITE_OK) + { + throw SQLiteException("Could not bind sql parameter", database); + } + + if (sqlite3_bind_int(res, 2, fromMe) != SQLITE_OK) + { + throw SQLiteException("Could not bind sql parameter", database); + } + + if (sqlite3_step(res) != SQLITE_ROW) + { + throw SQLiteException("No result for count query", database); + } + + int count = sqlite3_column_int(res, 0); + sqlite3_finalize(res); + + return count; +} + void WhatsappDatabase::getMessages(const std::string &chatId, std::vector &messages, const volatile bool &running) { QueryMessagesThread queryMessagesThread(*this, database, chatId, messages); diff --git a/source/WhatsApp/Database.h b/source/WhatsApp/Database.h index 012f6f8..123f673 100644 --- a/source/WhatsApp/Database.h +++ b/source/WhatsApp/Database.h @@ -15,6 +15,7 @@ class WhatsappDatabase private: SQLiteDatabase database; std::string findDisplayName(Settings &settings, const std::string &key); + int messagesCount(const std::string &chatId, int fromMe); public: WhatsappDatabase(const std::string &filename);