Skip to content

Commit

Permalink
#45 show message counts (sent/received) in chat list
Browse files Browse the repository at this point in the history
  • Loading branch information
andreas-mausch committed Nov 4, 2017
1 parent 0846ca9 commit b94ffc4
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 7 deletions.
31 changes: 28 additions & 3 deletions source/Platforms/Win32/GUI/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <commctrl.h>
#include <algorithm>
#include <fstream>
#include <locale>
#include <sstream>
#include <vector>

Expand Down Expand Up @@ -154,10 +155,10 @@ void MainWindow::createChildWindows()
CreateWindowEx(0, STATUSCLASSNAME, L"file manager", WS_CHILD, 0, 0, 0, 0, dialog, reinterpret_cast<HMENU>(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));
Expand Down Expand Up @@ -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));

Expand All @@ -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<WCHAR *>(lastMessageText.c_str()));
ListView_SetItemText(GetDlgItem(dialog, IDC_MAIN_CHATS), item.iItem, 2, const_cast<WCHAR *>(messagesTextW.c_str()));
}

void MainWindow::selectChat(WhatsappChat *chat)
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 12 additions & 2 deletions source/WhatsApp/Chat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}

Expand Down Expand Up @@ -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<WhatsappMessage *> WhatsappChat::getMessages(const volatile bool &running)
{
if (!messagesLoaded)
Expand Down
7 changes: 6 additions & 1 deletion source/WhatsApp/Chat.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,23 @@ class WhatsappChat
long long creation;
long long lastMessage;

int messagesSent;
int messagesReceived;

bool messagesLoaded;
std::vector<WhatsappMessage *> 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;
const std::string& getKey() const;
const std::string& getSubject() const;
long long getCreation() const;
long long getLastMessage() const;
int getMessagesSent() const;
int getMessagesReceived() const;

std::vector<WhatsappMessage *> getMessages(const volatile bool &running);
};
36 changes: 35 additions & 1 deletion source/WhatsApp/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,47 @@ void WhatsappDatabase::getChats(Settings &settings, std::vector<WhatsappChat*> &
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<WhatsappMessage*> &messages, const volatile bool &running)
{
QueryMessagesThread queryMessagesThread(*this, database, chatId, messages);
Expand Down
1 change: 1 addition & 0 deletions source/WhatsApp/Database.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit b94ffc4

Please sign in to comment.