From 001093d3bb773dd6cbbbabfca82ed3c93b3e85dc Mon Sep 17 00:00:00 2001 From: David Kaya Date: Fri, 3 Feb 2017 14:21:14 +0100 Subject: [PATCH] Added action receiver, refactored controller, extracted parsing logic from controller --- actionreceiver.cpp | 62 +++++++++++++++++++++++++++++++++++++++ actionreceiver.h | 29 ++++++++++++++++++ applicationcontroller.cpp | 60 +++++-------------------------------- applicationcontroller.h | 18 ++++-------- main.cpp | 4 +-- notification.cpp | 4 +-- notification.h | 8 +++-- testa.pro | 6 ++-- 8 files changed, 116 insertions(+), 75 deletions(-) create mode 100644 actionreceiver.cpp create mode 100644 actionreceiver.h diff --git a/actionreceiver.cpp b/actionreceiver.cpp new file mode 100644 index 0000000..b249f76 --- /dev/null +++ b/actionreceiver.cpp @@ -0,0 +1,62 @@ +#include "actionreceiver.h" + +ActionReceiver::ActionReceiver(const QUrl& address, QObject *parent) + : QObject(parent), + address(address), + webSocket(new QWebSocket()) +{ + connect(webSocket, &QWebSocket::connected, this, &ActionReceiver::socketConnected); + connect(webSocket, &QWebSocket::textMessageReceived, this, &ActionReceiver::socketReceivedStringMessage); + connect(webSocket, static_cast(&QWebSocket::error), this, &ActionReceiver::socketError); + + webSocket->open(address); +} + +ActionReceiver::~ActionReceiver() +{ + webSocket->close(); + delete webSocket; +} + +void ActionReceiver::socketConnected() +{ + qInfo() << "Socket connected to server"; +} + +void ActionReceiver::socketReceivedStringMessage(const QString &message) +{ + QJsonDocument jsonDocument = QJsonDocument::fromJson(message.toUtf8()); + QJsonObject jsonObject = jsonDocument.object(); + + QString messageType = getActionType(jsonObject); + QJsonObject parameters = getParameters(jsonObject); + + if (messageType == "loadUrl") { + emit loadUrlReceived(parameters["url"].toString()); + } else if (messageType == "showNotification") { + const QString title = parameters["title"].toString(); + const QString message = parameters["message"].toString(); + emit showNotificationReceived(title, message); + } else if (messageType == "setProgress") { + float value = parameters["value"].toDouble(); + emit setProgressReceived(value); + } else { + qWarning() << "Unsupported action type [" << messageType << "]"; + } +} + +void ActionReceiver::socketError(QAbstractSocket::SocketError socketError) +{ + Q_UNUSED(socketError); + qWarning() << "There was an error:" << webSocket->errorString(); +} + +QString ActionReceiver::getActionType(const QJsonObject &jsonObject) +{ + return jsonObject["action"].toString(); +} + +QJsonObject ActionReceiver::getParameters(const QJsonObject &jsonObject) +{ + return jsonObject["parameters"].toObject(); +} diff --git a/actionreceiver.h b/actionreceiver.h new file mode 100644 index 0000000..749a12e --- /dev/null +++ b/actionreceiver.h @@ -0,0 +1,29 @@ +#ifndef ACTIONRECEIVER_H +#define ACTIONRECEIVER_H + +#include +#include + +class ActionReceiver : public QObject +{ + Q_OBJECT +public: + explicit ActionReceiver(const QUrl &address, QObject *parent = 0); + ~ActionReceiver(); +private slots: + void socketConnected(); + void socketReceivedStringMessage(const QString &message); + void socketError(QAbstractSocket::SocketError socketError); +signals: + void loadUrlReceived(QString); + void setProgressReceived(float); + void showNotificationReceived(QString, QString); +private: + QUrl address; + QWebSocket* webSocket; + + QString getActionType(const QJsonObject &jsonObject); + QJsonObject getParameters(const QJsonObject &jsonObject); +}; + +#endif // ACTIONRECEIVER_H diff --git a/applicationcontroller.cpp b/applicationcontroller.cpp index e2657a5..d3c21a4 100644 --- a/applicationcontroller.cpp +++ b/applicationcontroller.cpp @@ -1,60 +1,14 @@ -#include #include "applicationcontroller.h" #include "notification.h" #include "progressbar.h" -ApplicationController::ApplicationController(WebEngineView* webEngineView, QObject *parent) : - QObject(parent), - webSocket(new QWebSocket()), - webEngineView(webEngineView) +ApplicationController::ApplicationController(WebEngineView* webEngineView, ActionReceiver *actionReceiver, QObject *parent) : + QObject(parent), + webEngineView(webEngineView), + actionReceiver(actionReceiver) { - connect(webSocket, &QWebSocket::connected, this, &ApplicationController::connected); - connect(webSocket, &QWebSocket::textMessageReceived, this, &ApplicationController::messageReceived); - connect(webSocket, static_cast(&QWebSocket::error), this, &ApplicationController::error); - - webSocket->open(QUrl("ws://localhost:12345")); // TODO: extract to configuration + connect(actionReceiver, &ActionReceiver::showNotificationReceived, Notification::show); + connect(actionReceiver, &ActionReceiver::setProgressReceived, ProgressBar::show); + connect(actionReceiver, &ActionReceiver::loadUrlReceived, webEngineView, &WebEngineView::loadUrl); } -void ApplicationController::connected() -{ - qDebug() << "Connected"; -} - -void ApplicationController::messageReceived(const QString &message) -{ - QJsonDocument jsonDocument = QJsonDocument::fromJson(message.toUtf8()); - QJsonObject jsonObject = jsonDocument.object(); - - QString messageType = getMessageType(jsonObject); - QJsonObject parameters = getParameters(jsonObject); - - if (messageType == "loadUrl") { - QUrl url(parameters["url"].toString()); - - webEngineView->loadUrl(url); - } else if (messageType == "showNotification") { - const QString title = parameters["title"].toString(); - const QString message = parameters["message"].toString(); - - Notification::show(title.toStdString(), message.toStdString()); - } else if (messageType == "setProgress") { - float value = parameters["value"].toDouble(); - - ProgressBar::show(value); - } -} -void ApplicationController::error(QAbstractSocket::SocketError error) -{ - Q_UNUSED(error); - qDebug() << webSocket->errorString(); -} - -QString ApplicationController::getMessageType(const QJsonObject &jsonObject) -{ - return jsonObject["action"].toString(); -} - -QJsonObject ApplicationController::getParameters(const QJsonObject &jsonObject) -{ - return jsonObject["parameters"].toObject(); -} diff --git a/applicationcontroller.h b/applicationcontroller.h index 50fec3f..7f25dbf 100644 --- a/applicationcontroller.h +++ b/applicationcontroller.h @@ -2,27 +2,19 @@ #define APPLICATIONCONTROLLER_H #include -#include -#include -#include + #include "webengineview.h" +#include "actionreceiver.h" class ApplicationController : public QObject { Q_OBJECT public: - explicit ApplicationController(WebEngineView *webEngineView, QObject *parent = 0); + explicit ApplicationController(WebEngineView *webEngineView, ActionReceiver *actionReceiver, QObject *parent = 0); void test(); -private slots: - void connected(); - void messageReceived(const QString &message); - void error(QAbstractSocket::SocketError error); -private: - QWebSocket* webSocket; +private: WebEngineView* webEngineView; - - QString getMessageType(const QJsonObject &jsonObject); - QJsonObject getParameters(const QJsonObject &jsonObject); + ActionReceiver* actionReceiver; }; #endif // APPLICATIONCONTROLLER_H diff --git a/main.cpp b/main.cpp index 23a00cd..c7b21c6 100644 --- a/main.cpp +++ b/main.cpp @@ -10,8 +10,8 @@ int main(int argc, char *argv[]) QApplication a(argc, argv); WebEngineView view; - - ApplicationController applicationController(&view); + ActionReceiver actionReceiver(QUrl("ws://localhost:12345")); + ApplicationController applicationController(&view, &actionReceiver); return a.exec(); } diff --git a/notification.cpp b/notification.cpp index 5d54fec..a9195c3 100644 --- a/notification.cpp +++ b/notification.cpp @@ -4,12 +4,12 @@ static QSystemTrayIcon* sharedSystemTrayIcon; -void Notification::show(const std::string& title, const std::string& message) +void Notification::show(const QString &title, const QString &message) { if (sharedSystemTrayIcon == nullptr) sharedSystemTrayIcon = new QSystemTrayIcon(); sharedSystemTrayIcon->show(); - sharedSystemTrayIcon->showMessage(QString::fromStdString(title), QString::fromStdString(message)); + sharedSystemTrayIcon->showMessage(title, message); } void Notification::hide() diff --git a/notification.h b/notification.h index 7184014..b94541f 100644 --- a/notification.h +++ b/notification.h @@ -1,12 +1,14 @@ #ifndef NOTIFICATION_H #define NOTIFICATION_H +#include #include -class Notification +class Notification : public QObject { -public: - static void show(const std::string& title, const std::string& message); + Q_OBJECT +public slots: + static void show(const QString &title, const QString &message); static void hide(); private: Notification() { } diff --git a/testa.pro b/testa.pro index a56fd32..10e9b64 100644 --- a/testa.pro +++ b/testa.pro @@ -29,7 +29,8 @@ SOURCES += main.cpp \ mainwindow.cpp \ webengineview.cpp \ notification.cpp \ - applicationcontroller.cpp + applicationcontroller.cpp \ + actionreceiver.cpp macx { OBJECTIVE_SOURCES += nativeprogressbar_mac.mm \ @@ -40,4 +41,5 @@ HEADERS += mainwindow.h \ webengineview.h \ progressbar.h \ notification.h \ - applicationcontroller.h + applicationcontroller.h \ + actionreceiver.h