From 1d68c85c0e8a73489666a026059f8ae9b8769d9c Mon Sep 17 00:00:00 2001 From: Denis Davydov Date: Thu, 28 Jun 2018 16:08:23 +0300 Subject: [PATCH 01/26] Changed the structure of the Handler class, for convenient addition of new methods. --- src/kernel/handler.cpp | 21 ++++++++++++++++----- src/kernel/handler.hpp | 1 + 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/kernel/handler.cpp b/src/kernel/handler.cpp index 50f75b9..200db95 100644 --- a/src/kernel/handler.cpp +++ b/src/kernel/handler.cpp @@ -8,18 +8,29 @@ Handler::Handler() void Handler::handle(QJsonObject jsonReceived) +{ + using namespace std::placeholders; + std::map> handlers = { + {"createSession", std::bind(&Handler::createSession, this, _1)} + }; + + QString action = jsonReceived["action"].toString(); + handlers[action](jsonReceived); +} + +void Handler::createSession(QJsonObject jsonReceived) { QJsonObject jsonSend; - if(jsonReceived["action"] == "createSession" && !jsonReceived.contains("status")) + if(jsonReceived.contains("status") != true) { jsonSend["peerID"] = my_ipv6; jsonSend["action"] = "createSession"; jsonSend["status"] = true; QString peerReceiver = jsonReceived["peerID"].toString(); network->sendDatagram(jsonSend, peerReceiver); - } - else - { + } + else + { emit createSessionSuccess(); - } + } } diff --git a/src/kernel/handler.hpp b/src/kernel/handler.hpp index e2ace3a..18aaad3 100644 --- a/src/kernel/handler.hpp +++ b/src/kernel/handler.hpp @@ -15,6 +15,7 @@ class Handler : public QObject void createSessionSuccess(); private: Network *network; + void createSession(QJsonObject jsonReceived); private slots: void handle(QJsonObject jsonReceived); From bbfcfd00d9dc66d8f868a1442de11c160472feb1 Mon Sep 17 00:00:00 2001 From: Denis Davydov Date: Thu, 28 Jun 2018 16:38:28 +0300 Subject: [PATCH 02/26] Now when you click on the "Connect" button, the button stops being available, and the text on it changes to "Waiting..." until any message is not displayed. --- src/mainwindow.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 49df6fa..4d5d7db 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -18,6 +18,8 @@ MainWindow::~MainWindow() void MainWindow::on_connectToPeer_clicked() { + ui->connectToPeer->setEnabled(false); + ui->connectToPeer->setText("Ожидание..."); QJsonObject j; j["peerID"] = my_ipv6; j["action"] = "createSession"; @@ -40,6 +42,8 @@ void MainWindow::slotTimerAlarm() int ret = QMessageBox::critical(this,QObject::tr("Error"),tr("Timeout Error")); timer->stop(); delete timer; + ui->connectToPeer->setEnabled(true); + ui->connectToPeer->setText("Подключиться"); } } @@ -48,4 +52,6 @@ void MainWindow::peerReceiverConnected() { receive = true; int ret = QMessageBox::information(this,QObject::tr("Info"),tr("Peer Available!")); + ui->connectToPeer->setEnabled(true); + ui->connectToPeer->setText("Подключиться"); } From 8d8649e5f9ea06e0f89c296b2fbd5799fb0e3fe4 Mon Sep 17 00:00:00 2001 From: Denis Davydov Date: Thu, 28 Jun 2018 16:51:19 +0300 Subject: [PATCH 03/26] Changed the constant QHostAddress::LocalHost to QHostAddress::LocalHostIPv6 --- src/kernel/network.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/kernel/network.cpp b/src/kernel/network.cpp index f90cf17..027582f 100644 --- a/src/kernel/network.cpp +++ b/src/kernel/network.cpp @@ -36,7 +36,7 @@ QString Network::local_ipv6() { QHostAddress address; foreach (address, QNetworkInterface::allAddresses()) { - if (address.protocol() == QAbstractSocket::IPv6Protocol && address != QHostAddress(QHostAddress::LocalHost)) + if (address.protocol() == QAbstractSocket::IPv6Protocol && address != QHostAddress(QHostAddress::LocalHostIPv6)) break; } return(address.toString()); From f694a32f3e8237acb2b7967c554c2dabc707e000 Mon Sep 17 00:00:00 2001 From: Denis Davydov Date: Fri, 29 Jun 2018 12:37:38 +0300 Subject: [PATCH 04/26] Changed some structure of classes Handler and MainWindow. Also added the line "My IP" on the main window. --- src/kernel/handler.cpp | 10 +++++----- src/kernel/handler.hpp | 3 +-- src/kernel/network.cpp | 2 +- src/mainwindow.cpp | 6 +++--- src/mainwindow.ui | 20 +++++++++++++++++--- 5 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/kernel/handler.cpp b/src/kernel/handler.cpp index 200db95..6550151 100644 --- a/src/kernel/handler.cpp +++ b/src/kernel/handler.cpp @@ -2,6 +2,11 @@ Handler::Handler() { + using namespace std::placeholders; + + handlers = { + {"createSession", std::bind(&Handler::createSession, this, _1)} + }; network = new Network(); connect(network, &Network::json_received, this, &Handler::handle); } @@ -9,11 +14,6 @@ Handler::Handler() void Handler::handle(QJsonObject jsonReceived) { - using namespace std::placeholders; - std::map> handlers = { - {"createSession", std::bind(&Handler::createSession, this, _1)} - }; - QString action = jsonReceived["action"].toString(); handlers[action](jsonReceived); } diff --git a/src/kernel/handler.hpp b/src/kernel/handler.hpp index 18aaad3..a16be97 100644 --- a/src/kernel/handler.hpp +++ b/src/kernel/handler.hpp @@ -16,8 +16,7 @@ class Handler : public QObject private: Network *network; void createSession(QJsonObject jsonReceived); + std::map> handlers; private slots: void handle(QJsonObject jsonReceived); - - }; diff --git a/src/kernel/network.cpp b/src/kernel/network.cpp index 027582f..252f9b3 100644 --- a/src/kernel/network.cpp +++ b/src/kernel/network.cpp @@ -36,7 +36,7 @@ QString Network::local_ipv6() { QHostAddress address; foreach (address, QNetworkInterface::allAddresses()) { - if (address.protocol() == QAbstractSocket::IPv6Protocol && address != QHostAddress(QHostAddress::LocalHostIPv6)) + if (address.protocol() == QAbstractSocket::IPv6Protocol && address != QHostAddress(QHostAddress::LocalHostIPv6) && (address.toString()).contains("fc")) break; } return(address.toString()); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 4d5d7db..4ca2ca5 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -8,7 +8,10 @@ MainWindow::MainWindow(QWidget *parent) : ui->setupUi(this); network = new Network(false); handler = new Handler(); + timer = new QTimer(); + connect(timer, SIGNAL(timeout()), this, SLOT(slotTimerAlarm())); connect(handler, &Handler::createSessionSuccess, this, &MainWindow::peerReceiverConnected); + ui->myIP->setText(my_ipv6); } MainWindow::~MainWindow() @@ -25,8 +28,6 @@ void MainWindow::on_connectToPeer_clicked() j["action"] = "createSession"; QString s = ui->peerID->text(); network->sendDatagram(j, s); - timer = new QTimer(); - connect(timer, SIGNAL(timeout()), this, SLOT(slotTimerAlarm())); timer->start(10000); } @@ -41,7 +42,6 @@ void MainWindow::slotTimerAlarm() { int ret = QMessageBox::critical(this,QObject::tr("Error"),tr("Timeout Error")); timer->stop(); - delete timer; ui->connectToPeer->setEnabled(true); ui->connectToPeer->setText("Подключиться"); } diff --git a/src/mainwindow.ui b/src/mainwindow.ui index 7e212b7..0eb633a 100644 --- a/src/mainwindow.ui +++ b/src/mainwindow.ui @@ -29,21 +29,35 @@ Influence - - + + - + + + + Мой IP: + + + + Подключиться + + + + true + + + From 3e4778b576286be1b44a088f70942327bf71920e Mon Sep 17 00:00:00 2001 From: Denis Davydov Date: Fri, 6 Jul 2018 15:38:45 +0300 Subject: [PATCH 05/26] Changed the structure of the class Handler (in particular, rewrite the structure of createSession) --- src/kernel/handler.cpp | 24 +++++++++++------------- src/kernel/handler.hpp | 1 + 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/kernel/handler.cpp b/src/kernel/handler.cpp index 6550151..32ce270 100644 --- a/src/kernel/handler.cpp +++ b/src/kernel/handler.cpp @@ -5,7 +5,8 @@ Handler::Handler() using namespace std::placeholders; handlers = { - {"createSession", std::bind(&Handler::createSession, this, _1)} + {"createSession", std::bind(&Handler::createSession, this, _1)}, + {"createSessionSuccess", std::bind(&Handler::createSessionSuccessMethod, this, _1)} }; network = new Network(); connect(network, &Network::json_received, this, &Handler::handle); @@ -21,16 +22,13 @@ void Handler::handle(QJsonObject jsonReceived) void Handler::createSession(QJsonObject jsonReceived) { QJsonObject jsonSend; - if(jsonReceived.contains("status") != true) - { - jsonSend["peerID"] = my_ipv6; - jsonSend["action"] = "createSession"; - jsonSend["status"] = true; - QString peerReceiver = jsonReceived["peerID"].toString(); - network->sendDatagram(jsonSend, peerReceiver); - } - else - { - emit createSessionSuccess(); - } + jsonSend["peerID"] = my_ipv6; + jsonSend["action"] = "createSessionSuccess"; + QString peerReceiver = jsonReceived["peerID"].toString(); + network->sendDatagram(jsonSend, peerReceiver); +} + +void Handler::createSessionSuccessMethod(QJsonObject jsonReceived) +{ + emit createSessionSuccess(); } diff --git a/src/kernel/handler.hpp b/src/kernel/handler.hpp index a16be97..15f380c 100644 --- a/src/kernel/handler.hpp +++ b/src/kernel/handler.hpp @@ -17,6 +17,7 @@ class Handler : public QObject Network *network; void createSession(QJsonObject jsonReceived); std::map> handlers; + void createSessionSuccessMethod(QJsonObject jsonReceived); private slots: void handle(QJsonObject jsonReceived); }; From 6aefbb153b715ef672d3c6cdc4f9dee1c0a5cbe4 Mon Sep 17 00:00:00 2001 From: Denis Davydov Date: Fri, 6 Jul 2018 17:56:27 +0300 Subject: [PATCH 06/26] Added false to bool receive --- src/mainwindow.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mainwindow.h b/src/mainwindow.h index 278c2b1..c032634 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -31,6 +31,6 @@ class MainWindow : public QMainWindow Network *network; Handler *handler; - bool receive; + bool receive = false; const QString my_ipv6 = Network::local_ipv6(); }; From 2050f635ff0665bbf19a318382570380614a1b8d Mon Sep 17 00:00:00 2001 From: Denis Davydov Date: Wed, 18 Jul 2018 11:24:22 +0300 Subject: [PATCH 07/26] Added comparison to true in the condition that is checked when the timeout () slot is invoked --- src/mainwindow.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 4ca2ca5..01f1212 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -33,7 +33,7 @@ void MainWindow::on_connectToPeer_clicked() void MainWindow::slotTimerAlarm() { - if(receive) + if(receive == true) { timer->stop(); receive = false; From 8b75f7c5e19877d9648e34c0cae9ad1b99dbb629 Mon Sep 17 00:00:00 2001 From: Denis Davydov Date: Wed, 18 Jul 2018 11:36:33 +0300 Subject: [PATCH 08/26] A ChatWindow class template was added, which is intended to create chat windows --- src/Influence.pro | 9 ++++++--- src/chatwindow.cpp | 14 ++++++++++++++ src/chatwindow.hpp | 19 +++++++++++++++++++ src/chatwindow.ui | 21 +++++++++++++++++++++ 4 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 src/chatwindow.cpp create mode 100644 src/chatwindow.hpp create mode 100644 src/chatwindow.ui diff --git a/src/Influence.pro b/src/Influence.pro index 97a68fb..ad05a91 100644 --- a/src/Influence.pro +++ b/src/Influence.pro @@ -27,12 +27,15 @@ SOURCES += \ main.cpp \ mainwindow.cpp \ kernel/network.cpp \ - kernel/handler.cpp + kernel/handler.cpp \ + chatwindow.cpp HEADERS += \ kernel/network.hpp \ mainwindow.h \ - kernel/handler.hpp + kernel/handler.hpp \ + chatwindow.hpp FORMS += \ - mainwindow.ui + mainwindow.ui \ + chatwindow.ui diff --git a/src/chatwindow.cpp b/src/chatwindow.cpp new file mode 100644 index 0000000..49121f3 --- /dev/null +++ b/src/chatwindow.cpp @@ -0,0 +1,14 @@ +#include "chatwindow.hpp" +#include "ui_chatwindow.h" + +ChatWindow::ChatWindow(QWidget *parent) : + QWidget(parent), + ui(new Ui::Form) +{ + ui->setupUi(this); +} + +ChatWindow::~ChatWindow() +{ + delete ui; +} diff --git a/src/chatwindow.hpp b/src/chatwindow.hpp new file mode 100644 index 0000000..10c79c3 --- /dev/null +++ b/src/chatwindow.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include + +namespace Ui { + class Form; +} + +class ChatWindow : public QWidget +{ + Q_OBJECT + + public: + explicit ChatWindow(QWidget *parent = 0); + ~ChatWindow(); + + private: + Ui::ChatWindow *ui; +}; diff --git a/src/chatwindow.ui b/src/chatwindow.ui new file mode 100644 index 0000000..d527737 --- /dev/null +++ b/src/chatwindow.ui @@ -0,0 +1,21 @@ + + + + + Form + + + + 0 + 0 + 400 + 300 + + + + Form + + + + + From 65ab05979d11222c47a816ca028836bc07fe071b Mon Sep 17 00:00:00 2001 From: Denis Davydov Date: Wed, 18 Jul 2018 12:04:53 +0300 Subject: [PATCH 09/26] Changed "action" to check the availability of a peer --- src/kernel/handler.cpp | 8 ++++---- src/kernel/handler.hpp | 4 ++-- src/mainwindow.cpp | 6 +++--- src/mainwindow.h | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/kernel/handler.cpp b/src/kernel/handler.cpp index 32ce270..676615f 100644 --- a/src/kernel/handler.cpp +++ b/src/kernel/handler.cpp @@ -5,8 +5,8 @@ Handler::Handler() using namespace std::placeholders; handlers = { - {"createSession", std::bind(&Handler::createSession, this, _1)}, - {"createSessionSuccess", std::bind(&Handler::createSessionSuccessMethod, this, _1)} + {"checkPeer", std::bind(&Handler::createSession, this, _1)}, + {"checkPeerSuccess", std::bind(&Handler::checkPeerSuccessMethod, this, _1)} }; network = new Network(); connect(network, &Network::json_received, this, &Handler::handle); @@ -23,12 +23,12 @@ void Handler::createSession(QJsonObject jsonReceived) { QJsonObject jsonSend; jsonSend["peerID"] = my_ipv6; - jsonSend["action"] = "createSessionSuccess"; + jsonSend["action"] = "checkPeerSuccess"; QString peerReceiver = jsonReceived["peerID"].toString(); network->sendDatagram(jsonSend, peerReceiver); } void Handler::createSessionSuccessMethod(QJsonObject jsonReceived) { - emit createSessionSuccess(); + emit checkPeerSuccess(); } diff --git a/src/kernel/handler.hpp b/src/kernel/handler.hpp index 15f380c..a436845 100644 --- a/src/kernel/handler.hpp +++ b/src/kernel/handler.hpp @@ -12,12 +12,12 @@ class Handler : public QObject public: Handler(); signals: - void createSessionSuccess(); + void checkPeerSuccess(); private: Network *network; void createSession(QJsonObject jsonReceived); std::map> handlers; - void createSessionSuccessMethod(QJsonObject jsonReceived); + void checkPeerSuccessMethod(QJsonObject jsonReceived); private slots: void handle(QJsonObject jsonReceived); }; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 01f1212..389ab1e 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -10,7 +10,7 @@ MainWindow::MainWindow(QWidget *parent) : handler = new Handler(); timer = new QTimer(); connect(timer, SIGNAL(timeout()), this, SLOT(slotTimerAlarm())); - connect(handler, &Handler::createSessionSuccess, this, &MainWindow::peerReceiverConnected); + connect(handler, &Handler::checkPeerSuccessSuccess, this, &MainWindow::peerReceiverAvailable); ui->myIP->setText(my_ipv6); } @@ -25,7 +25,7 @@ void MainWindow::on_connectToPeer_clicked() ui->connectToPeer->setText("Ожидание..."); QJsonObject j; j["peerID"] = my_ipv6; - j["action"] = "createSession"; + j["action"] = "checkPeer"; QString s = ui->peerID->text(); network->sendDatagram(j, s); timer->start(10000); @@ -48,7 +48,7 @@ void MainWindow::slotTimerAlarm() } -void MainWindow::peerReceiverConnected() +void MainWindow::peerReceiverAvailable() { receive = true; int ret = QMessageBox::information(this,QObject::tr("Info"),tr("Peer Available!")); diff --git a/src/mainwindow.h b/src/mainwindow.h index c032634..69905db 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -20,7 +20,7 @@ class MainWindow : public QMainWindow explicit MainWindow(QWidget *parent = 0); ~MainWindow(); public slots: - void peerReceiverConnected(); + void peerReceiverAvailable(); private slots: void on_connectToPeer_clicked(); void slotTimerAlarm(); From 9475414ecd0b52cac711b4692902fdcc7a7a026a Mon Sep 17 00:00:00 2001 From: Denis Davydov Date: Thu, 19 Jul 2018 12:53:32 +0300 Subject: [PATCH 10/26] Added destructor to class Network --- src/kernel/network.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/kernel/network.cpp b/src/kernel/network.cpp index 252f9b3..12340fc 100644 --- a/src/kernel/network.cpp +++ b/src/kernel/network.cpp @@ -41,3 +41,8 @@ QString Network::local_ipv6() } return(address.toString()); } + +Network::~Network() +{ + delete udpSocket; +} From 630f739538164b97641b40f271edc4664e6d43af Mon Sep 17 00:00:00 2001 From: Denis Davydov Date: Thu, 26 Jul 2018 11:33:47 +0300 Subject: [PATCH 11/26] Added the ability to correspond, performed a small optimization (fixed the memory leak), changed the structure of all the code (in particular, changed the names of many variables, etc.) Also translated all into English. --- src/chatwindow.cpp | 59 +++++++++++++++++++++-- src/chatwindow.hpp | 20 ++++++-- src/chatwindow.ui | 70 +++++++++++++++++++++++---- src/kernel/handler.cpp | 83 ++++++++++++++++++++++++++++---- src/kernel/handler.hpp | 20 ++++++-- src/kernel/network.cpp | 9 ++-- src/kernel/network.hpp | 11 +++-- src/mainwindow.cpp | 104 ++++++++++++++++++++++++++++++++++++----- src/mainwindow.h | 17 +++++-- src/mainwindow.ui | 10 +++- 10 files changed, 350 insertions(+), 53 deletions(-) diff --git a/src/chatwindow.cpp b/src/chatwindow.cpp index 49121f3..3572676 100644 --- a/src/chatwindow.cpp +++ b/src/chatwindow.cpp @@ -1,14 +1,67 @@ #include "chatwindow.hpp" #include "ui_chatwindow.h" -ChatWindow::ChatWindow(QWidget *parent) : +ChatWindow::ChatWindow(QString pID, QString cUUID, Handler *h, Network *n, QWidget *parent) : QWidget(parent), - ui(new Ui::Form) + ui(new Ui::ChatWindow) { ui->setupUi(this); + this->setAttribute(Qt::WA_DeleteOnClose); + chatID = cUUID; + peerID = pID; + handler = h; + network = n; + ui->peerIDLabel->setText(ui->peerIDLabel->text() + pID); + connect(ui->sendMsgButton, &QAbstractButton::clicked, this, &ChatWindow::sendMsgButtonClicked); } - ChatWindow::~ChatWindow() { + leftFromChat(); + emit deleteChat(chatID); delete ui; + this->deleteLater(); +} + +void ChatWindow::sendMessage(QString msgText) +{ + QJsonObject jSend; + jSend["action"] = "msgSend"; + jSend["peerID"] = *network->myIPv6; + jSend["chatID"] = chatID; + jSend["msgText"] = msgText; + network->sendDatagram(jSend, peerID); + ui->chatEdit->append(tr("You: ") + msgText); +} + +void ChatWindow::sendMsgButtonClicked() +{ + QString msg = ui->msgEdit->text(); + sendMessage(msg); + ui->msgEdit->setText(""); +} + +void ChatWindow::displayMsg(QString msgText) +{ + ui->chatEdit->append(peerID + ": " + msgText); +} + +void ChatWindow::leftFromChat() +{ + QJsonObject json; + json["action"] = "leftChat"; + json["peerID"] = *network->myIPv6; + json["chatID"] = chatID; + network->sendDatagram(json, peerID); +} + +void ChatWindow::peerReceiverLeftFromChat() +{ + QString msg; + msg += tr("Peer "); + msg += peerID; + msg += tr(" left from this chat."); + QMessageBox::warning(this, tr("Peer receiver left from chat!"), msg, QMessageBox::Ok); + ui->chatEdit->append(peerID + tr("left from this chat.")); + ui->msgEdit->setEnabled(false); + ui->sendMsgButton->setEnabled(false); } diff --git a/src/chatwindow.hpp b/src/chatwindow.hpp index 10c79c3..1da87cd 100644 --- a/src/chatwindow.hpp +++ b/src/chatwindow.hpp @@ -1,9 +1,12 @@ #pragma once #include +#include +#include "kernel/handler.hpp" +#include "kernel/network.hpp" namespace Ui { - class Form; + class ChatWindow; } class ChatWindow : public QWidget @@ -11,9 +14,20 @@ class ChatWindow : public QWidget Q_OBJECT public: - explicit ChatWindow(QWidget *parent = 0); + explicit ChatWindow(QString pID, QString cUUID, Handler *h, Network *n, QWidget *parent = 0); ~ChatWindow(); - + QString chatID; + QString peerID; + Handler *handler; + Network *network; + void displayMsg(QString msgText); + void peerReceiverLeftFromChat(); private: Ui::ChatWindow *ui; + void sendMessage(QString msgText); + void leftFromChat(); + signals: + void deleteChat(QString cID); + private slots: + void sendMsgButtonClicked(); }; diff --git a/src/chatwindow.ui b/src/chatwindow.ui index d527737..9486e3d 100644 --- a/src/chatwindow.ui +++ b/src/chatwindow.ui @@ -1,21 +1,71 @@ + - - - - Form - + ChatWindow + 0 0 - 400 - 300 + 807 + 519 - Form + + + + + + + + + + 697 + 0 + + + + Contact PeerID: + + + + + + + + + + Send + + + + + + + true + + + + - - + + + + msgEdit + returnPressed() + sendMsgButton + click() + + + 360 + 497 + + + 757 + 497 + + + + diff --git a/src/kernel/handler.cpp b/src/kernel/handler.cpp index 676615f..465718e 100644 --- a/src/kernel/handler.cpp +++ b/src/kernel/handler.cpp @@ -5,30 +5,97 @@ Handler::Handler() using namespace std::placeholders; handlers = { - {"checkPeer", std::bind(&Handler::createSession, this, _1)}, - {"checkPeerSuccess", std::bind(&Handler::checkPeerSuccessMethod, this, _1)} + {"checkPeer", std::bind(&Handler::checkPeer, this, _1)}, + {"checkPeerSuccess", std::bind(&Handler::checkPeerSuccessMethod, this, _1)}, + {"createChat", std::bind(&Handler::createChatMethod, this, _1)}, + {"createChatSuccess", std::bind(&Handler::createChatSuccessMethod, this, _1)}, + {"createChatFailed", std::bind(&Handler::createChatFailedMethod, this, _1)}, + {"msgSend", std::bind(&Handler::msgReceiveMethod, this, _1)}, + {"leftChat", std::bind(&Handler::peerReceiverLeftFromChatMethod, this, _1)} }; + peerReceiver = new QString(); network = new Network(); - connect(network, &Network::json_received, this, &Handler::handle); + connect(network, &Network::jsonReceived, this, &Handler::handle); } void Handler::handle(QJsonObject jsonReceived) { QString action = jsonReceived["action"].toString(); + *peerReceiver = jsonReceived["peerID"].toString(); handlers[action](jsonReceived); } -void Handler::createSession(QJsonObject jsonReceived) +void Handler::checkPeer(QJsonObject jsonReceived) { QJsonObject jsonSend; - jsonSend["peerID"] = my_ipv6; + jsonSend["peerID"] = *network->myIPv6; jsonSend["action"] = "checkPeerSuccess"; - QString peerReceiver = jsonReceived["peerID"].toString(); - network->sendDatagram(jsonSend, peerReceiver); + network->sendDatagram(jsonSend, *peerReceiver); } -void Handler::createSessionSuccessMethod(QJsonObject jsonReceived) +void Handler::checkPeerSuccessMethod(QJsonObject jsonReceived) { emit checkPeerSuccess(); } + +Handler::~Handler() +{ + delete peerReceiver; + delete network; + //delete handlers; +} + +void Handler::createChatMethod(QJsonObject jsonReceived) +{ + QWidget *parent = 0; + QString msgTitle; + QString msg; + msgTitle += "Create chat"; + msg += tr("Peer "); + msg += *peerReceiver; + msg += tr(" want to create chat with you.\n"); + msg += tr("Do you want to create chat?"); + int ret = QMessageBox::warning(parent, msgTitle, msg, QMessageBox::Yes | QMessageBox::No); + if(ret == QMessageBox::Yes) + { + QJsonObject jsonSend; + jsonSend["peerID"] = *network->myIPv6; + jsonSend["action"] = "createChatSuccess"; + jsonSend["chatID"] = jsonReceived["chatUUID"].toString(); + network->sendDatagram(jsonSend, *peerReceiver); + } + else + { + QJsonObject jsonSend; + jsonSend["peerID"] = *network->myIPv6; + jsonSend["action"] = "createChatFailed"; + network->sendDatagram(jsonSend, *peerReceiver); + } +} + +void Handler::createChatFailedMethod(QJsonObject jsonReceived) +{ + QString msg; + msg += tr("Peer "); + msg += *peerReceiver; + msg += tr(" refused to create a chat with you"); + QWidget *parent = 0; + QMessageBox::critical(parent, tr("Create chat failed!"), msg, QMessageBox::Ok); + emit createChatFailed(); +} + +void Handler::createChatSuccessMethod(QJsonObject jsonReceived) +{ + emit createChatSuccess(jsonReceived["peerID"].toString(), jsonReceived["chatID"].toString()); +} + +void Handler::msgReceiveMethod(QJsonObject jsonReceived) +{ + emit msgReceived(jsonReceived["peerID"].toString(), jsonReceived["chatID"].toString(), jsonReceived["msgText"].toString()); +} + +void Handler::peerReceiverLeftFromChatMethod(QJsonObject jsonReceived) +{ + emit peerReceiverLeftFromChat(jsonReceived["peerID"].toString(), jsonReceived["chatID"].toString()); +} diff --git a/src/kernel/handler.hpp b/src/kernel/handler.hpp index a436845..de2dca9 100644 --- a/src/kernel/handler.hpp +++ b/src/kernel/handler.hpp @@ -2,22 +2,32 @@ #include "network.hpp" #include +#include class Handler : public QObject { Q_OBJECT - const QString my_ipv6 = Network::local_ipv6(); - public: Handler(); - signals: - void checkPeerSuccess(); + ~Handler(); + QString *peerReceiver; private: Network *network; - void createSession(QJsonObject jsonReceived); + void checkPeer(QJsonObject jsonReceived); std::map> handlers; void checkPeerSuccessMethod(QJsonObject jsonReceived); + void createChatMethod(QJsonObject jsonReceived); + void createChatSuccessMethod(QJsonObject jsonReceived); + void createChatFailedMethod(QJsonObject jsonReceived); + void msgReceiveMethod(QJsonObject jsonReceived); + void peerReceiverLeftFromChatMethod(QJsonObject jsonReceived); + signals: + void checkPeerSuccess(); + void createChatSuccess(QString peerID, QString chatID); + void createChatFailed(); + void msgReceived(QString peerID, QString chatID, QString msgText); + void peerReceiverLeftFromChat(QString peerID, QString chatID); private slots: void handle(QJsonObject jsonReceived); }; diff --git a/src/kernel/network.cpp b/src/kernel/network.cpp index 12340fc..892c717 100644 --- a/src/kernel/network.cpp +++ b/src/kernel/network.cpp @@ -1,4 +1,5 @@ #include "network.hpp" + Network::Network(bool is_server) { udpSocket = new QUdpSocket(this); @@ -7,6 +8,7 @@ Network::Network(bool is_server) udpSocket->bind(QHostAddress::AnyIPv6, 6552); connect(udpSocket, SIGNAL(readyRead()), this, SLOT(processTheDatagram())); } + myIPv6 = new QString(localIPv6()); } void Network::sendDatagram(QJsonObject j, QString s) @@ -23,16 +25,16 @@ void Network::processTheDatagram() { QByteArray baDatagram; do { - baDatagram.resize(udpSocket->pendingDatagramSize ()) ; + baDatagram.resize(udpSocket->pendingDatagramSize()) ; udpSocket->readDatagram (baDatagram.data(), baDatagram.size()) ; } while (udpSocket->hasPendingDatagrams()) ; QJsonDocument jbuff = QJsonDocument::fromJson(baDatagram); QJsonObject j = QJsonObject(jbuff.object()); - emit json_received(j); + emit jsonReceived(j); } -QString Network::local_ipv6() +QString Network::localIPv6() { QHostAddress address; foreach (address, QNetworkInterface::allAddresses()) { @@ -45,4 +47,5 @@ QString Network::local_ipv6() Network::~Network() { delete udpSocket; + delete myIPv6; } diff --git a/src/kernel/network.hpp b/src/kernel/network.hpp index 6b0679f..f94b89f 100644 --- a/src/kernel/network.hpp +++ b/src/kernel/network.hpp @@ -9,15 +9,18 @@ class Network : public QObject { Q_OBJECT - private: - QUdpSocket* udpSocket; + public: Network(bool is_server = true); - static QString local_ipv6(); + ~Network(); + static QString localIPv6(); + const QString *myIPv6; + private: + QUdpSocket* udpSocket; public slots: void sendDatagram(QJsonObject j, QString s); signals: - void json_received(QJsonObject &jsonReceived); + void jsonReceived(QJsonObject &jsonReceived); private slots: void processTheDatagram(); }; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 389ab1e..71a0bf1 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -10,21 +10,28 @@ MainWindow::MainWindow(QWidget *parent) : handler = new Handler(); timer = new QTimer(); connect(timer, SIGNAL(timeout()), this, SLOT(slotTimerAlarm())); - connect(handler, &Handler::checkPeerSuccessSuccess, this, &MainWindow::peerReceiverAvailable); - ui->myIP->setText(my_ipv6); + connect(handler, &Handler::checkPeerSuccess, this, &MainWindow::peerReceiverAvailable); + connect(handler, &Handler::createChatSuccess, this, &MainWindow::createChat); + connect(handler, &Handler::createChatFailed, this, &MainWindow::createChatFailedMethod); + connect(handler, &Handler::msgReceived, this, &MainWindow::msgReceivedMethod); + connect(handler, &Handler::peerReceiverLeftFromChat, this, &MainWindow::peerReceiverLeftFromChatMethod); + ui->myIP->setText(*network->myIPv6); + } MainWindow::~MainWindow() { delete ui; + delete network; + delete handler; + delete timer; } void MainWindow::on_connectToPeer_clicked() { - ui->connectToPeer->setEnabled(false); - ui->connectToPeer->setText("Ожидание..."); + setButtonToWaiting(); QJsonObject j; - j["peerID"] = my_ipv6; + j["peerID"] = *network->myIPv6; j["action"] = "checkPeer"; QString s = ui->peerID->text(); network->sendDatagram(j, s); @@ -40,10 +47,9 @@ void MainWindow::slotTimerAlarm() } else { - int ret = QMessageBox::critical(this,QObject::tr("Error"),tr("Timeout Error")); + QMessageBox::critical(this, tr("Error"), tr("Timeout Error")); timer->stop(); - ui->connectToPeer->setEnabled(true); - ui->connectToPeer->setText("Подключиться"); + setButtonToConnect(); } } @@ -51,7 +57,83 @@ void MainWindow::slotTimerAlarm() void MainWindow::peerReceiverAvailable() { receive = true; - int ret = QMessageBox::information(this,QObject::tr("Info"),tr("Peer Available!")); - ui->connectToPeer->setEnabled(true); - ui->connectToPeer->setText("Подключиться"); + int ret = QMessageBox::information(this, tr("Peer Available!"), + tr("Peer Available!\n Do you want to create chat?"), QMessageBox::Yes | QMessageBox::No); + if(ret == QMessageBox::Yes) + { + createChatSendDatagram(*handler->peerReceiver); + } + else + { + setButtonToConnect(); + } +} + +void MainWindow::setButtonToWaiting() // Function, which sets button "connectToPeer" in status "Waiting..." +{ + ui->connectToPeer->setEnabled(false); + ui->connectToPeer->setText(tr("Waiting...")); +} + +void MainWindow::setButtonToConnect() // Function, which sets button "connectToPeer" in status "Connect" +{ + ui->connectToPeer->setEnabled(true); + ui->connectToPeer->setText(tr("Connect")); +} + +void MainWindow::createChatSendDatagram(QString peerReceiver) +{ + QUuid chatID = QUuid::createUuid(); + QJsonObject jSend; + jSend["action"] = "createChat"; + jSend["peerID"] = *network->myIPv6; + jSend["chatUUID"] = chatID.toString(); + network->sendDatagram(jSend, peerReceiver); +} + +void MainWindow::createChat(QString peerID, QString chatID) +{ + pChatWindows.push_back(new ChatWindow(peerID, chatID, handler, network)); + connect(pChatWindows.back(), &ChatWindow::deleteChat, this, &MainWindow::deleteChatMethod); + pChatWindows.back()->show(); + setButtonToConnect(); +} + +void MainWindow::createChatFailedMethod() +{ + setButtonToConnect(); +} + +void MainWindow::deleteChatMethod(QString cID) +{ + for(int i = 0; i < pChatWindows.size(); i++) + { + if(pChatWindows.at(i)->chatID == cID) + { + pChatWindows.remove(i); + break; + } + } +} + +void MainWindow::msgReceivedMethod(QString peerID, QString chatID, QString msgText) +{ + for(int i = 0; i < pChatWindows.size(); i++) + { + if(pChatWindows.at(i)->peerID == peerID && pChatWindows.at(i)->chatID == chatID) + { + pChatWindows.at(i)->displayMsg(msgText); + } + } +} + +void MainWindow::peerReceiverLeftFromChatMethod(QString peerID, QString chatID) +{ + for(int i = 0; i < pChatWindows.size(); i++) + { + if(pChatWindows.at(i)->peerID == peerID && pChatWindows.at(i)->chatID == chatID) + { + pChatWindows.at(i)->peerReceiverLeftFromChat(); + } + } } diff --git a/src/mainwindow.h b/src/mainwindow.h index 69905db..918790a 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -3,9 +3,12 @@ #include #include "kernel/network.hpp" #include "kernel/handler.hpp" +#include "chatwindow.hpp" +#include #include #include #include +#include namespace Ui { @@ -19,18 +22,24 @@ class MainWindow : public QMainWindow public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); + QVector pChatWindows; + void setButtonToWaiting(); + void setButtonToConnect(); public slots: void peerReceiverAvailable(); private slots: void on_connectToPeer_clicked(); void slotTimerAlarm(); - + void createChatFailedMethod(); + void createChat(QString peerID, QString chatID); + void deleteChatMethod(QString cID); + void msgReceivedMethod(QString peerID, QString chatID, QString msgText); + void peerReceiverLeftFromChatMethod(QString peerID, QString chatID); private: + void createChatSendDatagram(QString peerReceiver); Ui::MainWindow *ui; QTimer *timer; Network *network; Handler *handler; - - bool receive = false; - const QString my_ipv6 = Network::local_ipv6(); + bool receive = false; }; diff --git a/src/mainwindow.ui b/src/mainwindow.ui index 0eb633a..d8c0504 100644 --- a/src/mainwindow.ui +++ b/src/mainwindow.ui @@ -39,15 +39,21 @@ + + + - Мой IP: + My IPv6: + + + - Подключиться + Connect From ee60b8b0927ce64f595e496333057b5981e25e5e Mon Sep 17 00:00:00 2001 From: Denis Davydov Date: Thu, 26 Jul 2018 11:53:05 +0300 Subject: [PATCH 12/26] Fixed the bug when chat was not created when signed up to create a chat. --- src/kernel/handler.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/kernel/handler.cpp b/src/kernel/handler.cpp index 465718e..666c478 100644 --- a/src/kernel/handler.cpp +++ b/src/kernel/handler.cpp @@ -64,6 +64,7 @@ void Handler::createChatMethod(QJsonObject jsonReceived) jsonSend["action"] = "createChatSuccess"; jsonSend["chatID"] = jsonReceived["chatUUID"].toString(); network->sendDatagram(jsonSend, *peerReceiver); + emit createChatSuccess(jsonReceived["peerID"].toString(), jsonReceived["chatID"].toString()); } else { From c158b9953be0574612520d419d9ecffd265cabf9 Mon Sep 17 00:00:00 2001 From: CupIvan Date: Thu, 26 Jul 2018 11:46:09 +0300 Subject: [PATCH 13/26] add make.sh --- .gitignore | 7 +++---- src/make.sh | 3 +++ 2 files changed, 6 insertions(+), 4 deletions(-) create mode 100755 src/make.sh diff --git a/.gitignore b/.gitignore index ec8c70c..37192ac 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ +.project *.user - -/.project - -*.autosave \ No newline at end of file +*.autosave +*.o diff --git a/src/make.sh b/src/make.sh new file mode 100755 index 0000000..a412cc1 --- /dev/null +++ b/src/make.sh @@ -0,0 +1,3 @@ +rm -f Influence +qmake +make From 14f478dfec7ab7832ec0b69a690899ca0c02f236 Mon Sep 17 00:00:00 2001 From: Denis Davydov Date: Thu, 26 Jul 2018 12:19:47 +0300 Subject: [PATCH 14/26] Fixed bug with incorrect json field when creating a chat --- src/kernel/handler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/kernel/handler.cpp b/src/kernel/handler.cpp index 666c478..14646dd 100644 --- a/src/kernel/handler.cpp +++ b/src/kernel/handler.cpp @@ -64,7 +64,7 @@ void Handler::createChatMethod(QJsonObject jsonReceived) jsonSend["action"] = "createChatSuccess"; jsonSend["chatID"] = jsonReceived["chatUUID"].toString(); network->sendDatagram(jsonSend, *peerReceiver); - emit createChatSuccess(jsonReceived["peerID"].toString(), jsonReceived["chatID"].toString()); + emit createChatSuccess(jsonReceived["peerID"].toString(), jsonReceived["chatUUID"].toString()); } else { From b42a306c58803ec4a96b4cade1cd3de25dea9f5d Mon Sep 17 00:00:00 2001 From: Denis Davydov Date: Thu, 26 Jul 2018 12:58:09 +0300 Subject: [PATCH 15/26] Added space to string " left from this chat." --- src/chatwindow.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/chatwindow.cpp b/src/chatwindow.cpp index 3572676..8252de6 100644 --- a/src/chatwindow.cpp +++ b/src/chatwindow.cpp @@ -61,7 +61,7 @@ void ChatWindow::peerReceiverLeftFromChat() msg += peerID; msg += tr(" left from this chat."); QMessageBox::warning(this, tr("Peer receiver left from chat!"), msg, QMessageBox::Ok); - ui->chatEdit->append(peerID + tr("left from this chat.")); + ui->chatEdit->append(peerID + tr(" left from this chat.")); ui->msgEdit->setEnabled(false); ui->sendMsgButton->setEnabled(false); } From 7100d5f9b599e2ffb8779cf47fa0fc12c54f744c Mon Sep 17 00:00:00 2001 From: Denis Davydov Date: Thu, 26 Jul 2018 12:59:43 +0300 Subject: [PATCH 16/26] Fixed a bug where no notification was sent that peer left from the chat --- src/mainwindow.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 71a0bf1..42cfe4c 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -135,5 +135,14 @@ void MainWindow::peerReceiverLeftFromChatMethod(QString peerID, QString chatID) { pChatWindows.at(i)->peerReceiverLeftFromChat(); } + else + { + QString msg; + msg += tr("Peer "); + msg += peerID; + msg += tr(" left from chat "); + msg += chatID; + QMessageBox::warning(this, tr("Peer receiver left from chat!"), msg, QMessageBox::Ok); + } } } From 128e648ab96a7fcad1de7ba9741d689fb5bd7e68 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Thu, 26 Jul 2018 13:00:18 +0300 Subject: [PATCH 17/26] Update main.md --- doc/ru/main.md | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/doc/ru/main.md b/doc/ru/main.md index e2c2c33..545c467 100644 --- a/doc/ru/main.md +++ b/doc/ru/main.md @@ -1,22 +1,13 @@ # Общая документация мессенджера Influence -## Структура мессенджера - -Связь между узлами - точка-точка - -GUI - API ==== API - GUI - -GUI - примитивная программа под Linux/Windows/Android, которая умеет отображать сообщения и вызывать функции ядра (API). -API - ядро мессенджера. Достаточно подключить *main.hpp*, чтобы вызывать функции ядра. Такая структура позволяет писать какие угодно клиенты. - ## Протокол обмена Обмен сообщениями ведётся по UDP протоколу в формате JSON. -При любой успешной отправке данных обновляем поле last_connect у контакта. -Если пришло сообщение от контакта, которого нет в адресной книге - то оно отбрасывается. +При любой успешной отправке данных обновляем поле last_connect у контакта. (not implemented) +Если пришло сообщение от контакта, которого нет в адресной книге - то оно отбрасывается. Исключения - запрос handshake. -### Добавление в друзья +### Добавление в друзья (not implemented) Используется, чтобы установить связь с контактом. Тот кто хочет добавиться в друзья - отправляет такой запрос: @@ -32,8 +23,7 @@ API - ядро мессенджера. Достаточно подключить ```json { "action":"handshakeSuccess", - "peerID":"*IPv6*", - "status": true + "peerID":"*IPv6*" } ``` @@ -72,7 +62,7 @@ API - ядро мессенджера. Достаточно подключить Если нет ответа, то сохраняем сообщение у себя в истории, чтобы потом переотправить. `status == true` - доставлено. -### Пинг - проверка онлайна +### Пинг - проверка онлайна (not implemented) Если прошло время last_connect > 1 минуты, от отправляем запрос @@ -113,7 +103,7 @@ API - ядро мессенджера. Достаточно подключить В ответ приходит массив с `true` или `false`. Если `true` - сообщение успешно отправлено, `false` - сообщение недоставлено. -### Отправление сообщения для другого абонента (это нужно чтобы отправлять тем, кто в offline) +### Отправление сообщения для другого абонента (это нужно чтобы отправлять тем, кто в offline) (not implemented) Если человек, которому мы отправляем сообщение offline - то, во-первых, оно сохраняется у себя на сервере, для последующей отправки. Во-вторых, можно попросить какого-то друга его переотправить. Для этого выбираем случайно несколько друзей из списка контактов, у которых `can_resend == 1` и отправляем им запрос. From 57f9ea67b973730588620643be6999f4d6e7fa76 Mon Sep 17 00:00:00 2001 From: Denis Davydov Date: Thu, 26 Jul 2018 13:10:31 +0300 Subject: [PATCH 18/26] Added a more detailed description for the error Timeout Error. --- src/mainwindow.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 42cfe4c..5f0663b 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -47,7 +47,7 @@ void MainWindow::slotTimerAlarm() } else { - QMessageBox::critical(this, tr("Error"), tr("Timeout Error")); + QMessageBox::critical(this, tr("Error"), tr("Timeout Error\nPerhaps you have not started the cjdns daemon, you entered the wrong IP, the peer is off, or the peer did not start the messenger.")); timer->stop(); setButtonToConnect(); } From b9cb29f7d74b1d6cfa94fa1de9626808bbe87f2b Mon Sep 17 00:00:00 2001 From: Denis Davydov Date: Thu, 26 Jul 2018 13:13:43 +0300 Subject: [PATCH 19/26] Added one more \n to the Timeout Error message. --- src/mainwindow.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 5f0663b..666de22 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -47,7 +47,7 @@ void MainWindow::slotTimerAlarm() } else { - QMessageBox::critical(this, tr("Error"), tr("Timeout Error\nPerhaps you have not started the cjdns daemon, you entered the wrong IP, the peer is off, or the peer did not start the messenger.")); + QMessageBox::critical(this, tr("Error"), tr("Timeout Error\n\nPerhaps you have not started the cjdns daemon, you entered the wrong IP, the peer is off, or the peer did not start the messenger.")); timer->stop(); setButtonToConnect(); } From 6019b3664224b9cfeb5d6151586fe790ed546ace Mon Sep 17 00:00:00 2001 From: Denis Davydov Date: Thu, 26 Jul 2018 13:20:04 +0300 Subject: [PATCH 20/26] Cancel of fix 7100d5f9b599e2ffb8779cf47fa0fc12c54f744c --- src/mainwindow.cpp | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 666de22..72178ff 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -58,7 +58,7 @@ void MainWindow::peerReceiverAvailable() { receive = true; int ret = QMessageBox::information(this, tr("Peer Available!"), - tr("Peer Available!\n Do you want to create chat?"), QMessageBox::Yes | QMessageBox::No); + tr("Peer Available!\nDo you want to create chat?"), QMessageBox::Yes | QMessageBox::No); if(ret == QMessageBox::Yes) { createChatSendDatagram(*handler->peerReceiver); @@ -135,14 +135,5 @@ void MainWindow::peerReceiverLeftFromChatMethod(QString peerID, QString chatID) { pChatWindows.at(i)->peerReceiverLeftFromChat(); } - else - { - QString msg; - msg += tr("Peer "); - msg += peerID; - msg += tr(" left from chat "); - msg += chatID; - QMessageBox::warning(this, tr("Peer receiver left from chat!"), msg, QMessageBox::Ok); - } } } From 3b58f486ecf1bee1abbe2dbe8d74e1d909739eed Mon Sep 17 00:00:00 2001 From: Denis Davydov Date: Thu, 26 Jul 2018 13:24:34 +0300 Subject: [PATCH 21/26] Added chatID label to ChatWindow --- src/chatwindow.cpp | 1 + src/chatwindow.ui | 13 ++++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/chatwindow.cpp b/src/chatwindow.cpp index 8252de6..d17d94e 100644 --- a/src/chatwindow.cpp +++ b/src/chatwindow.cpp @@ -12,6 +12,7 @@ ChatWindow::ChatWindow(QString pID, QString cUUID, Handler *h, Network *n, QWidg handler = h; network = n; ui->peerIDLabel->setText(ui->peerIDLabel->text() + pID); + ui->chatIDLabel->setText(ui->chatIDLabel->text() + cUUID); connect(ui->sendMsgButton, &QAbstractButton::clicked, this, &ChatWindow::sendMsgButtonClicked); } ChatWindow::~ChatWindow() diff --git a/src/chatwindow.ui b/src/chatwindow.ui index 9486e3d..76458d5 100644 --- a/src/chatwindow.ui +++ b/src/chatwindow.ui @@ -14,7 +14,7 @@ - + @@ -30,7 +30,7 @@ - + @@ -40,13 +40,20 @@ - + true + + + + ChatID: + + + From 5231302217cd8735a3359c26ca2bfdf80d71e5df Mon Sep 17 00:00:00 2001 From: Denis Davydov Date: Thu, 26 Jul 2018 14:42:37 +0300 Subject: [PATCH 22/26] Now the application does not start if it does not find ip, which starts with "fc" (ie, before starting the messenger, you need to run the cjdns daemon) --- src/kernel/network.cpp | 12 ++++++++++-- src/main.cpp | 4 ++++ src/mainwindow.cpp | 7 ++++++- src/mainwindow.h | 2 ++ 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/kernel/network.cpp b/src/kernel/network.cpp index 892c717..c055a6a 100644 --- a/src/kernel/network.cpp +++ b/src/kernel/network.cpp @@ -37,11 +37,19 @@ void Network::processTheDatagram() QString Network::localIPv6() { QHostAddress address; + QString addressString; foreach (address, QNetworkInterface::allAddresses()) { if (address.protocol() == QAbstractSocket::IPv6Protocol && address != QHostAddress(QHostAddress::LocalHostIPv6) && (address.toString()).contains("fc")) - break; + { + addressString = address.toString(); + break; + } + else + { + addressString = "null"; + } } - return(address.toString()); + return(addressString); } Network::~Network() diff --git a/src/main.cpp b/src/main.cpp index b48f94e..c86e878 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,6 +5,10 @@ int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; + if(w.doQuit()) + { + return -1; + } w.show(); return a.exec(); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 72178ff..ddb1da2 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -5,8 +5,13 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { - ui->setupUi(this); network = new Network(false); + if(*network->myIPv6 == "null") + { + QMessageBox::critical(parent, tr("Cjdns is not running!"), tr("Cjdns is not running, so the application will be closed.")); + mDoQuit = true; + } + ui->setupUi(this); handler = new Handler(); timer = new QTimer(); connect(timer, SIGNAL(timeout()), this, SLOT(slotTimerAlarm())); diff --git a/src/mainwindow.h b/src/mainwindow.h index 918790a..fa791d2 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -25,6 +25,7 @@ class MainWindow : public QMainWindow QVector pChatWindows; void setButtonToWaiting(); void setButtonToConnect(); + inline bool doQuit() const { return mDoQuit; } public slots: void peerReceiverAvailable(); private slots: @@ -42,4 +43,5 @@ class MainWindow : public QMainWindow Network *network; Handler *handler; bool receive = false; + bool mDoQuit = false; }; From 18714ddde68c6a9e54eab8e55fc680d041ed1c11 Mon Sep 17 00:00:00 2001 From: Denis Davydov Date: Thu, 26 Jul 2018 16:22:10 +0300 Subject: [PATCH 23/26] Added html tag in some places of chat. --- src/chatwindow.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/chatwindow.cpp b/src/chatwindow.cpp index d17d94e..4946160 100644 --- a/src/chatwindow.cpp +++ b/src/chatwindow.cpp @@ -31,7 +31,7 @@ void ChatWindow::sendMessage(QString msgText) jSend["chatID"] = chatID; jSend["msgText"] = msgText; network->sendDatagram(jSend, peerID); - ui->chatEdit->append(tr("You: ") + msgText); + ui->chatEdit->append(tr("You: ") + msgText); } void ChatWindow::sendMsgButtonClicked() @@ -43,7 +43,7 @@ void ChatWindow::sendMsgButtonClicked() void ChatWindow::displayMsg(QString msgText) { - ui->chatEdit->append(peerID + ": " + msgText); + ui->chatEdit->append("" + peerID + "" + ": " + msgText); } void ChatWindow::leftFromChat() @@ -62,7 +62,7 @@ void ChatWindow::peerReceiverLeftFromChat() msg += peerID; msg += tr(" left from this chat."); QMessageBox::warning(this, tr("Peer receiver left from chat!"), msg, QMessageBox::Ok); - ui->chatEdit->append(peerID + tr(" left from this chat.")); + ui->chatEdit->append("" + peerID + "" + tr(" left from this chat.")); ui->msgEdit->setEnabled(false); ui->sendMsgButton->setEnabled(false); } From a397b6fe2f0cdf3e09108498df486336e98dcb5b Mon Sep 17 00:00:00 2001 From: Denis Davydov Date: Thu, 26 Jul 2018 16:46:04 +0300 Subject: [PATCH 24/26] Added display of message time (local time) --- src/chatwindow.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/chatwindow.cpp b/src/chatwindow.cpp index 4946160..1380326 100644 --- a/src/chatwindow.cpp +++ b/src/chatwindow.cpp @@ -31,7 +31,8 @@ void ChatWindow::sendMessage(QString msgText) jSend["chatID"] = chatID; jSend["msgText"] = msgText; network->sendDatagram(jSend, peerID); - ui->chatEdit->append(tr("You: ") + msgText); + QDateTime date(QDateTime::currentDateTime()); + ui->chatEdit->append("[" + date.toString() + "] " + tr("You: ") + msgText); } void ChatWindow::sendMsgButtonClicked() @@ -43,7 +44,8 @@ void ChatWindow::sendMsgButtonClicked() void ChatWindow::displayMsg(QString msgText) { - ui->chatEdit->append("" + peerID + "" + ": " + msgText); + QDateTime date(QDateTime::currentDateTime()); + ui->chatEdit->append("[" + date.toString() + "] " + "" + peerID + "" + ": " + msgText); } void ChatWindow::leftFromChat() From 810bfbc246187daa298415febb1fd07c52f62a36 Mon Sep 17 00:00:00 2001 From: Denis Davydov Date: Thu, 26 Jul 2018 16:53:54 +0300 Subject: [PATCH 25/26] Swapped the tag and the square bracket in the message time --- src/chatwindow.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/chatwindow.cpp b/src/chatwindow.cpp index 1380326..b9dc5ef 100644 --- a/src/chatwindow.cpp +++ b/src/chatwindow.cpp @@ -32,7 +32,7 @@ void ChatWindow::sendMessage(QString msgText) jSend["msgText"] = msgText; network->sendDatagram(jSend, peerID); QDateTime date(QDateTime::currentDateTime()); - ui->chatEdit->append("[" + date.toString() + "] " + tr("You: ") + msgText); + ui->chatEdit->append("[" + date.toString() + "] " + tr("You: ") + msgText); } void ChatWindow::sendMsgButtonClicked() @@ -45,7 +45,7 @@ void ChatWindow::sendMsgButtonClicked() void ChatWindow::displayMsg(QString msgText) { QDateTime date(QDateTime::currentDateTime()); - ui->chatEdit->append("[" + date.toString() + "] " + "" + peerID + "" + ": " + msgText); + ui->chatEdit->append("[" + date.toString() + "] " + "" + peerID + "" + ": " + msgText); } void ChatWindow::leftFromChat() From d5c9371a65c0ecacf51a3a77b1605a7aeae50677 Mon Sep 17 00:00:00 2001 From: Denis Davydov Date: Thu, 26 Jul 2018 17:55:07 +0300 Subject: [PATCH 26/26] Now when you press Enter in the IP input field, you automatically start to connect to it. (similar to pressing the Connect button) --- src/mainwindow.ui | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/mainwindow.ui b/src/mainwindow.ui index d8c0504..4ed2caa 100644 --- a/src/mainwindow.ui +++ b/src/mainwindow.ui @@ -57,7 +57,7 @@ - + true @@ -69,5 +69,22 @@ - + + + peerID + returnPressed() + connectToPeer + click() + + + 122 + 90 + + + 281 + 90 + + + +