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

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