Added action receiver, refactored controller, extracted parsing logic from controller

This commit is contained in:
David Kaya
2017-02-03 14:21:14 +01:00
parent 5d800be890
commit 001093d3bb
8 changed files with 116 additions and 75 deletions

62
actionreceiver.cpp Normal file
View File

@@ -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<void(QWebSocket::*)(QAbstractSocket::SocketError)>(&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();
}

29
actionreceiver.h Normal file
View File

@@ -0,0 +1,29 @@
#ifndef ACTIONRECEIVER_H
#define ACTIONRECEIVER_H
#include <QObject>
#include <QtWebSockets>
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

View File

@@ -1,60 +1,14 @@
#include <QJsonDocument>
#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<void(QWebSocket::*)(QAbstractSocket::SocketError)>(&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();
}

View File

@@ -2,27 +2,19 @@
#define APPLICATIONCONTROLLER_H
#include <QObject>
#include <QJsonObject>
#include <QAbstractSocket>
#include <QtWebSockets/QWebSocket>
#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

View File

@@ -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();
}

View File

@@ -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()

View File

@@ -1,12 +1,14 @@
#ifndef NOTIFICATION_H
#define NOTIFICATION_H
#include <QObject>
#include <string>
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() { }

View File

@@ -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