Skip to content

Commit

Permalink
implemented the private messaging
Browse files Browse the repository at this point in the history
  • Loading branch information
czimbortibor committed Jan 8, 2017
1 parent ab4a109 commit ae6ea8f
Show file tree
Hide file tree
Showing 14 changed files with 364 additions and 103 deletions.
12 changes: 8 additions & 4 deletions Client/ChatApplication.pro
Expand Up @@ -17,13 +17,17 @@ SOURCES +=\
LoginDialog.cpp \
Main.cpp \
MainWindow.cpp \
../util/Packaging.cpp
../util/Packaging.cpp \
PrivateMessageDialog.cpp

HEADERS += \
Client.h \
LoginDialog.h \
MainWindow.h \
../util/Packaging.h
../util/Packaging.h \
PrivateMessageDialog.h

FORMS += mainwindow.ui \
logindialog.ui
FORMS += \
PrivateMessageDialog.ui \
LoginDialog.ui \
MainWindow.ui
31 changes: 23 additions & 8 deletions Client/Client.cpp
Expand Up @@ -67,18 +67,28 @@ void Client::onReadMsg() {
packaging.parsePackage(package.toStdString());
QString sender = QString::fromStdString(packaging.getSender());
QString message = QString::fromStdString(packaging.getMessage());
QString receiver = QString::fromStdString(packaging.getReceiver());
std::string request = packaging.identifyRequest(package.toStdString());
qDebug() << "request:" << QString::fromStdString(request);
/** string to be displayed on the message wall */
QString output;
// on login the server sends back the current time
if (sender == " ") {

qDebug() << "from server: " << res;
if (request.compare("time_package") == 0) {
output = "Login time: " + message;
emit receivedMessage(output);
}
// normal message from a user
else {
output = sender + ": "+ message;
else if (request.compare("online_users_package") == 0) {
emit receivedUsersList(message);
}
else if (request.compare("global_package") == 0) {
output = sender + ": " + message;
emit receivedMessage(output);
}
else if (request.compare("private_package") == 0) {
// qDebug() << sender + " -> " + receiver + ": " + message;
emit receivedPrivateMessage(sender, message, receiver);
}

emit receivedPackage(output);
}

void Client::sendPackage(QString package) {
Expand All @@ -88,5 +98,10 @@ void Client::sendPackage(QString package) {
QByteArray messageBlock = package.toUtf8();;

tcpSocket->write(messageBlock);
qDebug() << "message sent to the server.";
qDebug() << "package sent to the server.\n";
}

void Client::onSendPrivateMessage(QString receiver, QString message, QString sender) {
std::string package = packaging.createPivatePackage(receiver.toStdString(), message.toStdString(), sender.toStdString());
sendPackage(QString::fromStdString(package));
}
7 changes: 6 additions & 1 deletion Client/Client.h
Expand Up @@ -19,6 +19,8 @@ friend class MainWindow;
explicit Client(QObject* parent = 0);
Client(QString serverAddr = "127.0.0.1", QString portNr = "10013", QString username = "test user");

QString getUsername() { return username; }

void connectToServer();
void sendPackage(QString package);

Expand All @@ -35,13 +37,16 @@ friend class MainWindow;
void readyRead();
void error();
void login();
void receivedPackage(QString package);
void receivedMessage(QString message);
void receivedUsersList(QString usersList);
void receivedPrivateMessage(QString receiver, QString message, QString sender);

public slots:
void onLoginRequest();
void onReadMsg();
void onLogoutRequest();
void onGlobalPackage(QString message);
void onSendPrivateMessage(QString receiver, QString message, QString sender);
};

#endif // CLIENT_H
6 changes: 6 additions & 0 deletions Client/LoginDialog.ui
Expand Up @@ -75,6 +75,9 @@
</item>
<item>
<widget class="QPushButton" name="btnOk">
<property name="styleSheet">
<string notr="true">background-color: rgb(85, 255, 127);</string>
</property>
<property name="text">
<string>OK</string>
</property>
Expand All @@ -85,6 +88,9 @@
</item>
<item>
<widget class="QPushButton" name="btnCancel">
<property name="styleSheet">
<string notr="true">background-color: rgb(85, 255, 127);</string>
</property>
<property name="text">
<string>Cancel</string>
</property>
Expand Down
66 changes: 63 additions & 3 deletions Client/MainWindow.cpp
Expand Up @@ -8,6 +8,13 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWi

connect(loginDialog.data(), &LoginDialog::signIn, this, &MainWindow::onSignIn); // data(): extracts the raw pointer
connect(ui->btnSendMessage, &QPushButton::clicked, this, &MainWindow::onSendMessage);
// send private message via double click
connect(ui->listUsers, &QListWidget::itemDoubleClicked, this, &MainWindow::onPrivateMessage);
connect(client.data(), &Client::receivedPrivateMessage, this, &MainWindow::onReceivedPrivateMessage);
// TODO: send private message with the button
//connect(ui->btnSendPrivate, &QPushButton::clicked, this, &MainWindow::onPrivateMessage);
// enable the "Send private message" button when an online user was selected
connect(ui->listUsers, &QListWidget::itemPressed, this, &MainWindow::onUserSelected);
}

MainWindow::~MainWindow() {
Expand All @@ -29,7 +36,8 @@ void MainWindow::initClient() {
typedef void (QAbstractSocket::*QAbstractSocketErrorSignal)(QAbstractSocket::SocketError);
connect(client->tcpSocket.data(), static_cast<QAbstractSocketErrorSignal>(&QAbstractSocket::error), this, &MainWindow::onDisplayError);

connect(client.data(), &Client::receivedPackage, this, &MainWindow::onReceivedMessage);
connect(client.data(), &Client::receivedMessage, this, &MainWindow::onReceivedMessage);
connect(client.data(), &Client::receivedUsersList, this, &MainWindow::onReceivedUsersList);
connect(this->ui->btnLogout, &QPushButton::clicked, client.data(), &Client::onLogoutRequest);

client->connectToServer();
Expand All @@ -51,12 +59,29 @@ void MainWindow::onDisplayError(QAbstractSocket::SocketError socketError) {
default:
QMessageBox::information(this, title, tr("Unknown error occured."));
break;
}
}
}

void MainWindow::onReceivedMessage(QString message) {
ui->editRecvMessage->append(message);
qDebug() << "received message";
}

void MainWindow::onReceivedUsersList(QString usersList) {
// parse the string
size_t pos = 0;
std::string token;
std::string inputStr = usersList.toStdString();
while ((pos = inputStr.find(",")) != std::string::npos) {
// next token
token = inputStr.substr(0, pos);
QString text = QString::fromStdString(token);
// check if the list already contains the user and don't add the user itself to the list
if ((ui->listUsers->findItems(text, Qt::MatchExactly).size()) == 0 && text != this->username) {
ui->listUsers->addItem(text);
}
// erase the token + the glue
inputStr.erase(0, pos + 1);
}
}

void MainWindow::onSendMessage() {
Expand All @@ -65,3 +90,38 @@ void MainWindow::onSendMessage() {
client->onGlobalPackage(message);
ui->editSendMessage->clear();
}

void MainWindow::onUserSelected() {
ui->btnSendPrivate->setEnabled(true);
}

void MainWindow::onPrivateMessage(QListWidgetItem* item) {
QString username = item->text();
createPrivateDialog(username);
}

void MainWindow::onReceivedPrivateMessage(QString receiver, QString message, QString sender) {
createPrivateDialog(sender);
}

void MainWindow::createPrivateDialog(QString username) {
if (!privateDialogs.contains(username)) {
PrivateMessageDialog* privateDialog = new PrivateMessageDialog(this, client.data(), username);
// signal from the private message dialog when the user closes it so it can be removed from the list of opened dialogs
connect(privateDialog, &PrivateMessageDialog::closeDialog, this, &MainWindow::onClosePrivateDialog);
privateDialog->setWindowTitle("Chat with " + username);
privateDialog->show();

privateDialogs.push_back(username);
}
}

void MainWindow::onClosePrivateDialog(QString username) {
int i = 0;
for (i = 0; i < privateDialogs.length(); ++i) {
if (privateDialogs[i].compare(username) == 0) {
break;
}
}
privateDialogs.remove(i);
}
12 changes: 12 additions & 0 deletions Client/MainWindow.h
Expand Up @@ -5,9 +5,12 @@
#include <QScopedPointer>
#include <QSharedPointer>
#include <QMessageBox>
#include <QListWidgetItem>
#include <QVector>

#include "LoginDialog.h"
#include "Client.h"
#include "PrivateMessageDialog.h"


namespace Ui {
Expand All @@ -31,13 +34,22 @@ class MainWindow : public QMainWindow {
QSharedPointer<LoginDialog> loginDialog;
QSharedPointer<Client> client;

/** list of all the active private message dialogs */
QVector<QString> privateDialogs;

void initClient();
void createPrivateDialog(QString username);

private slots:
void onSignIn(QString username, QString password, QString serverAddr, QString port);
void onDisplayError(QAbstractSocket::SocketError socketError);
void onReceivedMessage(QString message);
void onSendMessage();
void onReceivedUsersList(QString usersList);
void onPrivateMessage(QListWidgetItem* item);
void onUserSelected();
void onClosePrivateDialog(QString username);
void onReceivedPrivateMessage(QString receiver, QString message, QString sender);
};


Expand Down
2 changes: 1 addition & 1 deletion Client/MainWindow.ui
Expand Up @@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>500</width>
<width>527</width>
<height>313</height>
</rect>
</property>
Expand Down
32 changes: 25 additions & 7 deletions Client/PrivateMessageDialog.cpp
@@ -1,14 +1,32 @@
#include "PrivateMessageDialog.hpp"
#include "PrivateMessageDialog.h"
#include "ui_PrivateMessageDialog.h"

PrivateMessageDialog::PrivateMessageDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::PrivateMessageDialog)
{
PrivateMessageDialog::PrivateMessageDialog(QWidget* parent, Client* client, QString chatPartner) : QDialog(parent),
ui(new Ui::PrivateMessageDialog), client(client) {
this->chatPartner = chatPartner;
ui->setupUi(this);

connect(client, &Client::receivedPrivateMessage, this, &PrivateMessageDialog::onReceivedPrivateMessage);
connect(ui->btnSendMessage, &QPushButton::clicked, this, &PrivateMessageDialog::onSendMessageButtonClicked);
connect(this, &PrivateMessageDialog::sendPrivateMessage, client, &Client::onSendPrivateMessage);
}

PrivateMessageDialog::~PrivateMessageDialog()
{
PrivateMessageDialog::~PrivateMessageDialog() {
delete ui;
}

void PrivateMessageDialog::onReceivedPrivateMessage(QString receiver, QString message, QString sender) {
QString output = receiver + ": " + message;
ui->editRecvMessage->append(output);
}

void PrivateMessageDialog::onSendMessageButtonClicked() {
QString message = ui->editSendMessage->toPlainText();
QString receiver = chatPartner;
QString sender = client->getUsername();

emit sendPrivateMessage(receiver, message, sender);
ui->editSendMessage->clear();
QString output = sender + ": " + message;
ui->editRecvMessage->append(output);
}
22 changes: 18 additions & 4 deletions Client/PrivateMessageDialog.h
Expand Up @@ -3,20 +3,34 @@

#include <QDialog>

#include "Client.h"

namespace Ui {
class PrivateMessageDialog;
}

class PrivateMessageDialog : public QDialog
{
class PrivateMessageDialog : public QDialog {
Q_OBJECT

public:
explicit PrivateMessageDialog(QWidget *parent = 0);
explicit PrivateMessageDialog(QWidget* parent = 0, Client* client = 0, QString chatPartner = "");
~PrivateMessageDialog();

QString getUsername() { return client->getUsername(); }

private:
Ui::PrivateMessageDialog *ui;
Ui::PrivateMessageDialog* ui;
QSharedPointer<Client> client;
/** the user's name whom the private chat is ongoing */
QString chatPartner;

signals:
void closeDialog(QString username);
void sendPrivateMessage(QString receiver, QString message, QString sender);

public slots:
void onReceivedPrivateMessage(QString receiver, QString message, QString sender);
void onSendMessageButtonClicked();
};

#endif // PRIVATEMESSAGEDIALOG_HPP

0 comments on commit ae6ea8f

Please sign in to comment.