diff options
-rw-r--r-- | src/Toast.qml | 26 | ||||
-rw-r--r-- | src/ToastManager.qml | 19 | ||||
-rw-r--r-- | src/main.qml | 4 | ||||
-rw-r--r-- | src/main_qmladaptor.cpp | 84 | ||||
-rw-r--r-- | src/main_qmladaptor.h | 63 | ||||
-rw-r--r-- | src/scenes/add_server_wizard/Scene_step_1.qml | 1 | ||||
-rw-r--r-- | src/scenes/add_server_wizard/add_server_wizard.cpp | 17 | ||||
-rw-r--r-- | src/scenes/add_server_wizard/add_server_wizard.h | 4 | ||||
-rw-r--r-- | src/session.cpp | 320 | ||||
-rw-r--r-- | src/session.h | 52 |
10 files changed, 248 insertions, 342 deletions
diff --git a/src/Toast.qml b/src/Toast.qml index 0de53a9..7a18dc8 100644 --- a/src/Toast.qml +++ b/src/Toast.qml @@ -27,6 +27,7 @@ import QtQuick 2.0 import QtQuick.Controls 2.0 import QtQuick.Controls.Material 2.3 +import rwa.toast.type 1.0 /** * adapted from StackOverflow: @@ -50,8 +51,11 @@ Control { * * @param {string} text Text to show * @param {real} duration Duration to show in milliseconds, defaults to 3000 + * @param {enum} type Type of toast. Available is: + * ToastType.Standard, ToastType.Info, ToastType.Warning + * ToastType.Success, ToastType.Error */ - function show(text, duration) { + function show(text, duration, type) { message.text = text; // checks if parameter was passed @@ -60,6 +64,23 @@ Control { } else { time = defaultTime; } + + if (typeof type !== "undefined" ) { + if (type === ToastType.ToastStandard) { + selectedColor = "#dcdedc"; + } else if (type === ToastType.ToastInfo) { + selectedColor = "#0d5eaf"; + } else if (type === ToastType.ToastSuccess) { + selectedColor = "#0daf36"; + } else if (type === ToastType.ToastWarning) { + selectedColor = "#efef2a"; + } else if (type === ToastType.ToastError) { + selectedColor = "#ed1212"; + } + } else { + selectedColor = "#dcdedc"; + } + animation.start(); } @@ -72,6 +93,7 @@ Control { id: root + property color selectedColor: "#dcdedc" readonly property real defaultTime: 3000 property real time: defaultTime readonly property real fadeTime: 300 @@ -88,6 +110,8 @@ Control { background: Rectangle { color: (Material.theme == Material.Dark) ? "#212121" : "#dcdedc" + border.color: selectedColor + border.width: 1.5 radius: margin } diff --git a/src/ToastManager.qml b/src/ToastManager.qml index 1acf8d5..2f1600f 100644 --- a/src/ToastManager.qml +++ b/src/ToastManager.qml @@ -42,9 +42,12 @@ ListView { * * @param {string} text Text to show * @param {real} duration Duration to show in milliseconds, defaults to 3000 + * @param {enum} type Type of toast. Available is: + * ToastType.Standard, ToastType.Info, ToastType.Warning + * ToastType.Success, ToastType.Error */ - function show(text, duration) { - model.insert(0, {text: text, duration: duration}); + function show(text, duration, type) { + model.insert(0, {text: text, duration: duration, type: type}); } /** @@ -71,11 +74,13 @@ ListView { delegate: Toast { Component.onCompleted: { - if (typeof duration === "undefined") { - show(text); - } - else { - show(text, duration); + if (typeof duration === "undefined" && typeof type === "undefined") { + show(text, ToastType.ToastStandard); + } else if (typeof duration === "undefined" && + typeof type !== "undefined") { + show(text, type); + } else { + show(text, duration, type); } } } diff --git a/src/main.qml b/src/main.qml index 4782f4b..af41dd1 100644 --- a/src/main.qml +++ b/src/main.qml @@ -107,8 +107,8 @@ ApplicationWindow { Connections { target: mainqmladaptor - onShowToastSignal: { - toast.show(text, durationMs) + function onShowToastSignal(text, durationMs, type) { + toast.show(text, durationMs, type) } } diff --git a/src/main_qmladaptor.cpp b/src/main_qmladaptor.cpp index 5da0abb..d30243d 100644 --- a/src/main_qmladaptor.cpp +++ b/src/main_qmladaptor.cpp @@ -33,11 +33,22 @@ MainQMLAdaptor::MainQMLAdaptor(QObject *parent, QQmlApplicationEngine* engine, _engine = engine; _dbus_api = dbus_api; + _selected_rwa_host = nullptr; _rwaHostModel = new QList<QObject*>; QTimer *timer = new QTimer(this); - connect(timer, &QTimer::timeout, _dbus_api, QOverload<>::of(&DBusAPI::get_web_app_hosts_request)); + connect(timer, &QTimer::timeout, _dbus_api, + QOverload<>::of(&DBusAPI::get_web_app_hosts_request)); timer->start(10000); + + qmlRegisterUncreatableMetaObject( + Toast::staticMetaObject, // meta object created by Q_NAMESPACE macro + "rwa.toast.type", // import statement (can be any string) + 1, 0, // major and minor version of the import + "ToastType", // name in QML (does not have to match C++ name) + "Error: only enums" // error if someone tries to create a ToastType object + ); + } void MainQMLAdaptor::onRwaHostSelected(QString host_uuid) { @@ -55,11 +66,19 @@ void MainQMLAdaptor::onRwaHostSelected(QString host_uuid) { } Q_ASSERT(_host != nullptr); - qDebug() << "RWAHost was selected!" << _host->uuid() << "aka" << _host->alias(); + _selected_rwa_host = _host; + qDebug() << "RWAHost was selected!" << + _selected_rwa_host->uuid() << + "aka" << + _selected_rwa_host->alias(); setRWAHostSelected(true); } +RWAHost* MainQMLAdaptor::getSelectedRWAHost() { + return _selected_rwa_host; +} + void MainQMLAdaptor::setRWAHostModel(QList<QObject*> *rwa_hosts) { _rwaHostModel = rwa_hosts; emit rwaHostModelChanged(*rwa_hosts); @@ -108,7 +127,13 @@ void MainQMLAdaptor::get_web_app_hosts_response(QJsonDocument *doc) { if (doc == nullptr) { setRWAHostSelected(false); - showToast(tr("Can't connect to underlying session service!"), 9800); + showToast(tr("Can't connect to underlying session service!"), + 9800, + Toast::ToastError); + + // Go to the first page on the stack. + main_content_pop(nullptr); + return; } @@ -308,59 +333,12 @@ bool MainQMLAdaptor::getShowMessageDialog() { return _showMessageDialog; } -bool MainQMLAdaptor::setStatusIndicator(bool active, QColor color) { - // Find item via 'objectName' - QQuickItem *scene_remote_control = _engine->rootObjects().at(0)->findChild<QQuickItem*>("Scene_remote_control"); - QQuickItem *item = scene_remote_control->findChild<QQuickItem*>("dbus_api_status_indicator"); - if (item) { - item->setProperty("active", active); - item->setProperty("color", color); - } else { - qWarning() << "Unable to find 'dbus_api_status_indicator' Item!"; - return false; - } - - return true; -} - -void MainQMLAdaptor::handleCopyToClipboardButtonClick(QString copy_data) { - QClipboard *clipboard = QApplication::clipboard(); - QString originalText = clipboard->text(); - clipboard->setText(copy_data); - qDebug() << "Copied into clipboard:" << copy_data; -} - -void MainQMLAdaptor::handleConnectButtonClick(bool checked) { - emit onConnectButtonClick(checked); -} - -void MainQMLAdaptor::setPin(QString pin) { - _pin = pin; - emit pinChanged(pin); -} -void MainQMLAdaptor::setURL(QString URL) { - _url = URL; - emit urlChanged(URL); -} -void MainQMLAdaptor::setSessionID(QString session_id) { - _session_id = session_id; - emit sessionIDChanged(session_id); -} -QString MainQMLAdaptor::getURL() { - return _url; -} -QString MainQMLAdaptor::getPin() { - return _pin; -} -QString MainQMLAdaptor::getSessionID() { - return _session_id; -} - void MainQMLAdaptor::onCloseHandler() { // Do cleanup things here... emit onCloseSignal(); } -void MainQMLAdaptor::showToast(QString text, uint durationMs) { - emit showToastSignal(text, QString::number(durationMs)); +void MainQMLAdaptor::showToast(QString text, uint durationMs, uint type) { + // type is actually Toast::ToastType + emit showToastSignal(text, QString::number(durationMs), type); } diff --git a/src/main_qmladaptor.h b/src/main_qmladaptor.h index 9a2f7c3..70c075b 100644 --- a/src/main_qmladaptor.h +++ b/src/main_qmladaptor.h @@ -39,17 +39,34 @@ #include "RWAHost.h" #include "DBusAPI.h" -class MainQMLAdaptor : public QObject +namespace Toast { + Q_NAMESPACE + enum ToastType { + ToastStandard, + ToastInfo, + ToastSuccess, + ToastWarning, + ToastError + }; + Q_ENUM_NS(ToastType) +} + +/*#include <QObject> +namespace MyNamespace { + Q_NAMESPACE // required for meta object creation + enum EnStyle { + STYLE_RADIAL, + STYLE_ENVELOPE, + STYLE_FILLED + }; + Q_ENUM_NS(EnStyle) // register the enum in meta object data +}*/ + +class MainQMLAdaptor : public QObject { Q_OBJECT - // this makes url available as a QML property + // this makes rwaHostModel available as a QML property Q_PROPERTY(QList<QObject*> rwaHostModel READ getRWAHostModel NOTIFY rwaHostModelChanged) - // this makes url available as a QML property - Q_PROPERTY(QString url READ getURL WRITE setURL NOTIFY urlChanged) - // this makes pin available as a QML property - Q_PROPERTY(QString pin READ getPin WRITE setPin NOTIFY pinChanged) - // this makes session_id available as a QML property - Q_PROPERTY(QString session_id READ getSessionID WRITE setSessionID NOTIFY sessionIDChanged) // this makes showMessageDialog available as a QML property Q_PROPERTY(bool showMessageDialog READ getShowMessageDialog NOTIFY showMessageDialogChanged) // this makes showMessageDialogTitle available as a QML property @@ -85,27 +102,19 @@ signals: void minimizeWindow(); void showWindow(); - void onConnectButtonClick(bool checked); - - void pinChanged(QString pin); - void urlChanged(QString URL); - void sessionIDChanged(QString session_id); - void rwaHostModelChanged(QList<QObject*>); void onCloseSignal(); - void showToastSignal(QString text, QString durationMs); + void showToastSignal(QString text, QString durationMs, int type); protected: - QString _url; - QString _pin; - QString _session_id; DBusAPI *_dbus_api; QList<QObject*> *_rwaHostModel; private: - QQmlApplicationEngine* _engine; + QQmlApplicationEngine *_engine; + RWAHost *_selected_rwa_host; bool _showMessageDialog; QString _messageDialogTitle; @@ -113,27 +122,23 @@ private: QMessageBox::Icon _messageDialogIcon; public slots: - void handleCopyToClipboardButtonClick(QString copy_data); - void handleConnectButtonClick(bool checked); - void get_web_app_hosts_response(QJsonDocument *doc); - void setPin(QString pin); - void setURL(QString URL); - void setSessionID(QString session_id); void addRWAHost(RWAHost *rwa_host); void removeRWAHost(RWAHost *rwa_host); void setRWAHostSelected(bool value); - QString getURL(); - QString getPin(); - QString getSessionID(); // No pointer because QML doesn't // like this type much with pointer QList<QObject*> getRWAHostModel(); + RWAHost *getSelectedRWAHost(); + void onRwaHostSelected(QString host_uuid); void onCloseHandler(); - void showToast(QString text, uint durationMs = 3000); + // arg type is actually Toast::ToastType + void showToast(QString text, + uint durationMs = 3000, + uint type = 0); }; diff --git a/src/scenes/add_server_wizard/Scene_step_1.qml b/src/scenes/add_server_wizard/Scene_step_1.qml index e57f8a4..bfbe2ae 100644 --- a/src/scenes/add_server_wizard/Scene_step_1.qml +++ b/src/scenes/add_server_wizard/Scene_step_1.qml @@ -3,6 +3,7 @@ import QtQuick.Window 2.2 import QtQuick.Extras 1.4 import QtQuick.Controls 2.2 import QtQuick.Controls.Material 2.3 +import rwa.toast.type 1.0 /*! * This .qml file is a Scene which can be loaded through for diff --git a/src/scenes/add_server_wizard/add_server_wizard.cpp b/src/scenes/add_server_wizard/add_server_wizard.cpp index 9b67ebc..caac344 100644 --- a/src/scenes/add_server_wizard/add_server_wizard.cpp +++ b/src/scenes/add_server_wizard/add_server_wizard.cpp @@ -47,7 +47,7 @@ void Add_Server_wizard::processStep1(QString host_url, QString host_alias) { if(host_alias == "" || host_url == "") { QString reason = tr("Both textfields can't be empty!"); - emit step1Failed(reason); + emit step1Failed(reason, Toast::ToastType::ToastWarning); qDebug().noquote() << reason; return; } @@ -57,7 +57,7 @@ void Add_Server_wizard::processStep1(QString host_url, QString host_alias) { void Add_Server_wizard::processStep2() { qDebug() << "Processing Step 2 with args: No Args."; - emit step2Failed(tr("The features you expected here are not yet implemented.")); + emit step2Failed(tr("The features you expected here are not yet implemented."), Toast::ToastType::ToastWarning); // Just show placeholder scene now. emit step2Success(); } @@ -72,7 +72,9 @@ void Add_Server_wizard::add_web_app_host_response(QJsonDocument *doc) { // Don't use Q_ASSERT(doc != nullptr); instead use: if (doc == nullptr) { _main_gui->setRWAHostSelected(false); - _main_gui->showToast(tr("Can't connect to underlying session service!"), 9800); + _main_gui->showToast(tr("Can't connect to underlying session service!"), + 9800, + Toast::ToastType::ToastError); return; } @@ -90,25 +92,32 @@ void Add_Server_wizard::add_web_app_host_response(QJsonDocument *doc) { } else { qCritical().noquote() << tr("An error occured while adding a new host!"); + uint toast_type = Toast::ToastType::ToastStandard; QString reason = tr("An error occured while adding a new host!"); QString type = mainMap["type"].toString(); + if(type == "connection"){ reason = tr("Couldn't connect to the specified host!"); + toast_type = Toast::ToastType::ToastError; qCritical().noquote() << reason; } else if (type == "duplicate") { reason = tr("The specified host was already added!"); + toast_type = Toast::ToastType::ToastWarning; qCritical().noquote() << reason; } else if (type == "invalid_url") { reason = tr("The specified host address is not valid!"); + toast_type = Toast::ToastType::ToastWarning; qCritical().noquote() << reason; } else if (type == "permission_denied") { reason = tr("The specified host address does not grant access!"); + toast_type = Toast::ToastType::ToastError; qCritical().noquote() << reason; } else if (type == "unsupported_server") { reason = tr("The specified host address is not supported!"); + toast_type = Toast::ToastType::ToastError; qCritical().noquote() << reason; } - emit step1Failed(reason); + emit step1Failed(reason, toast_type); return; } diff --git a/src/scenes/add_server_wizard/add_server_wizard.h b/src/scenes/add_server_wizard/add_server_wizard.h index 03af824..dc205d1 100644 --- a/src/scenes/add_server_wizard/add_server_wizard.h +++ b/src/scenes/add_server_wizard/add_server_wizard.h @@ -47,9 +47,9 @@ private: signals: void step1Success(); - void step1Failed(QString reason); + void step1Failed(QString reason, uint toast_type); void step2Success(); - void step2Failed(QString reason); + void step2Failed(QString reason, uint toast_type); public slots: void processStep1(QString host_url, QString host_alias); diff --git a/src/session.cpp b/src/session.cpp index a524afe..25a8512 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -26,194 +26,60 @@ #include "session.h" -Session::Session(QObject *parent, MainQMLAdaptor* main_gui, RWAHost *host) : QObject(parent) { - Q_ASSERT(main_gui != nullptr); +Session::Session(DBusAPI *dbus_api, RWAHost *host) : QObject() { Q_ASSERT(host != nullptr); + Q_ASSERT(dbus_api != nullptr); - _dbus_api = new DBusAPI(); - _main_gui = main_gui; + _dbus_api = dbus_api; _host = host; - // QML -> MainQMLAdaptor::handleConnectButtonClick --onConnectButtonClick--> this::handleConnectButtonClick - QObject::connect(_main_gui, - SIGNAL(onConnectButtonClick(bool)), - this, - SLOT(handleConnectButtonClick(bool))); - - // session::setPin --pinChanged--> MainQMLAdaptor::setPin --pinChanged--> QML - QObject::connect(this, - SIGNAL(pinChanged(QString)), - main_gui, - SLOT(setPin(QString))); - - // session::setURL --urlChanged--> MainQMLAdaptor::setURL --urlChanged--> QML - QObject::connect(this, - SIGNAL(urlChanged(QString)), - main_gui, - SLOT(setURL(QString))); - - // session::setSessionID --sessionIDChanged--> MainQMLAdaptor::setSessionID --sessionIDChanged--> QML - QObject::connect(this, - SIGNAL(sessionIDChanged(QString)), - main_gui, - SLOT(setSessionID(QString))); - - // QML -> MainQMLAdaptor::onCloseHandler --onCloseSignal--> session::onCloseHandler - QObject::connect(main_gui, - SIGNAL(onCloseSignal()), - this, - SLOT(onCloseHandler())); - - // _dbus_api --sessionStartResponse-> this.start_response() - QObject::connect(_dbus_api, - SIGNAL(serviceStartResponse(QJsonDocument*)), - this, - SLOT(start_response(QJsonDocument*))); - - // _dbus_api --sessionStopResponse-> this.stop_response() - QObject::connect(_dbus_api, - SIGNAL(serviceStopResponse(QJsonDocument*)), - this, - SLOT(stop_response(QJsonDocument*))); - - // _dbus_api --sessionStatusResponse-> this.status_response() - QObject::connect(_dbus_api, - SIGNAL(serviceStatusResponse(QJsonDocument*)), - this, - SLOT(status_response(QJsonDocument*))); - - this->init_vars(); -} - -void Session::statusTimerEvent() { - qDebug() << "Status timer event got triggered just now."; - this->status(); -} - -void Session::init_vars() { setPin("-----"); setSessionID("-----"); setURL(tr("Not available yet")); setStatus("unknown"); - - _main_gui->setConnectButtonChecked(false); - _main_gui->setConnectButtonEnabled(true); - _main_gui->setStatusIndicator(false); - - _minimizedBefore = false; + started = false; } -QString Session::getStatus() { - return _status; +Session::~Session() { + qDebug().noquote() << QString("Session #'%0' on host '%1' will be deconstructed.") + .arg(getSessionID()) + .arg(getHost()->alias()); + stop(); + disconnect(); } -QString Session::getURL() { - return _url; -} - -QString Session::getSessionID() { - return QString(_session_id); -} +void Session::statusTimerEvent() { + qDebug() << "Status timer event triggered"; -QString Session::getPin() { - return _pin; + refresh_status(); } -bool Session::isSessionAliveOrRunning(QString status) { - if (status == "running" || status == "active") { +bool Session::isSessionAliveOrRunning() { + if (getStatus() == "running" || getStatus() == "active") { return true; } else { return false; } } -void Session::minimizeWindow() { - if (!_minimizedBefore) { - qDebug() << "Minimizing window now..."; - emit _main_gui->minimizeWindow(); - _minimizedBefore = true; - } +RWAHost* Session::getHost() { + return _host; } -void Session::setStatus(QString status) { - _status = status; +QString Session::getStatus() { + return _status; +} - QString guiString = tr("Unknown state of service"); - _main_gui->setStatusIndicator(false); - - qDebug() << "setStatus(): Setting status to " << status; - - if (status == "running") { - /* Session is running but no one is connected yet */ - guiString = tr("Remote Support session is ready to be connected to"); - _main_gui->setStatusIndicator(true, QColor(255, 255, 0, 127)); - } else if (status == "dead") { - /* Session died */ - guiString = tr("Remote Support session was stopped ungracefully"); - - // Clear current variables - this->init_vars(); - _main_gui->setStatusIndicator(true, QColor(255, 0, 0, 127)); - - emit _main_gui->showWindow(); - - _main_gui->showToast(tr("Remote Support session was " - "stopped ungracefully"), 5000); - } else if (status == "stopped") { - /* Session is stopped */ - guiString = tr("Remote Support session was stopped"); - - // Clear current variables - this->init_vars(); - - emit _main_gui->showWindow(); - - _main_gui->showToast(tr("Remote Support session was stopped"), 5000); - } else if (status == "active") { - /* Partner is connected */ - QTimer::singleShot(1000, this, &Session::minimizeWindow); - guiString = tr("Your partner is connected to the Remote Support session"); - _main_gui->setStatusIndicator(true, QColor(0, 255, 0, 127)); - - } else if (status == "waiting_start_request_answer") { - /* When pressing on start button display following message while waiting */ - guiString = tr("Trying to reach session service..."); - } else if (status == "start_session_error") { - /* Session couldn't be started */ - guiString = tr("Remote Support session couldn't be started!"); - _main_gui->setStatusIndicator(true, QColor(255, 0, 0, 127)); - - _main_gui->showToast(tr("An error occured while trying " - "to start a session!"), 5000); - } else if (status == "start_session_success") { - /* Session successfully started */ - guiString = tr("Remote Support session successfully started!"); - _main_gui->setStatusIndicator(true, QColor(255, 255, 0, 127)); - - _main_gui->showToast(QString(tr("Session was started " - "on '%0' successfully")) - .arg(_host->alias())); - } else if (status == "status_session_error") { - /* Session's status couldn't be refreshed */ - _main_gui->showToast(tr("Session status could not be refreshed!"), 5000); - guiString = tr("Session status could not be refreshed!") + "\n" + - tr("remote support partner could still be connected!"); - _main_gui->setStatusIndicator(true, QColor(255, 0, 0, 127)); - - emit _main_gui->showWindow(); - } else if (status == "stop_session_error") { - /* Session couldn't be stopped */ - _main_gui->showToast(tr("Session could not be stopped!"), 5000); - guiString = tr("Session could not be stopped!") + "\n" + - tr("remote support partner could still be connected!"); - _main_gui->setStatusIndicator(true, QColor(255, 0, 0, 127)); - - emit _main_gui->showWindow(); - } +QString Session::getURL() { + return _url; +} - _main_gui->setStatus(guiString); +QString Session::getSessionID() { + return _session_id; +} - emit statusChanged(_status); +QString Session::getPin() { + return _pin; } void Session::setURL(QString url) { @@ -231,49 +97,62 @@ void Session::setPin(QString pin) { emit pinChanged(pin); } -void Session::handleConnectButtonClick(bool checked) { - qDebug() << "-----Connect button handler-----" << - "\nCurrent session #" << this->getSessionID(); - - // Stopping even if nothing is running - _dbus_api->stop_request(_host, this->getSessionID()); +void Session::setHost(RWAHost *host) { + _host = host; + emit hostChanged(host); +} - if (checked) { - // Start the Session again - _dbus_api->start_request(_host); - } - qDebug() << "-----\\Connect button handler-----"; +void Session::setStatus(QString status) { + _status = status; + emit statusChanged(status); } void Session::start() { - // Disable connect button to reduce spam. - // And don't enable it as long there is no status update. - // (Or something fails) - _main_gui->setConnectButtonEnabled(false); - this->setStatus("waiting_start_request_answer"); - _dbus_api->start_request(_host); } void Session::start_response(QJsonDocument *doc) { - Q_ASSERT(doc != nullptr); + // Q_ASSERT lets the program crash immediatly after method call + // when the session service is not started. + // Don't use Q_ASSERT(doc != nullptr); instead use: + if (doc == nullptr) { + emit startFailed(tr("Can't connect to underlying session service! " + "Is the session service started?")); + return; + } // Get the QJsonObject QJsonObject jObject = doc->object(); QVariantMap mainMap = jObject.toVariantMap(); // Status of request - QString request_status = mainMap["status"].toString(); - if (request_status == "success") { - qDebug() << "Successfully started a Session."; - this->setStatus("start_session_success"); - - // Immediately ask for the status of the session - QTimer::singleShot(0, this, &Session::statusTimerEvent); - } else { - qCritical() << "An error occured while creating a new session!"; - this->init_vars(); - this->setStatus("start_session_error"); + QString status = mainMap["status"].toString(); + if (status != "success") { + QString type = mainMap["type"].toString(); + QString error_message = tr("An error occured while creating a new session!"); + + if (type == "multiple") { + // TODO: Ask to stop other session via a message dialog. + error_message = tr("The session service is configured " + "to not support multiple sessions " + "and there is already a session running."); + } else if (type == "connection") { + error_message = tr("Couldn't connect to host '%0'.") + .arg(getHost()->alias()); + } else if (type == "host_not_found") { + error_message = tr("The RWA host '%0' couldn't be found.") + .arg(getHost()->alias()); + } else if (type == "permission_denid") { + error_message = tr("The RWA host '%0' doesn't grant access.") + .arg(getHost()->alias()); + } else if (type == "unsupported_server") { + error_message = tr("The RWA host '%0' is not supported.") + .arg(getHost()->alias()); + } + + setStatus("start_session_error"); + qCritical().noquote() << error_message; + emit startFailed(error_message); return; } @@ -285,23 +164,25 @@ void Session::start_response(QJsonDocument *doc) { // PIN long long pin = mainMap["pin"].toLongLong(&ok); - this->setPin(QString::number(pin)); // Sanity Check if(ok == false){ - qErrnoWarning("Unable to parse <pin> out of dbus answer!"); - init_vars(); + QString error_message = "Unable to parse <pin> into longo long out of dbus answer!"; + qCritical().noquote() << error_message; + emit startFailed(error_message); return; } + this->setPin(QString::number(pin)); // session_id = remote support id from the rwa-server long long session_id = mainMap["session_id"].toLongLong(); - this->setSessionID(QString::number(session_id)); // Sanity Check if(ok == false){ - qErrnoWarning("Unable to parse <pin> out of dbus answer!"); - init_vars(); + QString error_message = "Unable to parse <session_id> into long long out of dbus answer!"; + qCritical().noquote() << error_message; + emit startFailed(error_message); return; } + this->setSessionID(QString::number(session_id)); qDebug() << "Got session_id:" << session_id << "\nGot url:" << url << @@ -347,39 +228,44 @@ void Session::stop_response(QJsonDocument *doc) { QString new_status = mainMap["status"].toString(); qDebug() << "stop_response() retrieved json. Status now:" << new_status; - // Clear current variables - this->init_vars(); + started = false; - // But the status indicator should display that the Session has stopped this->setStatus(new_status); + + emit stopSucceeded(); } void Session::status() { - _dbus_api->status_request(_host, this->getSessionID()); + _dbus_api->status_request(getHost(), this->getSessionID()); +} + +void Session::refresh_status() { + _dbus_api->refresh_status_request(getHost(), this->getSessionID()); } void Session::status_response(QJsonDocument *doc) { - Q_ASSERT(doc != nullptr); + // Q_ASSERT lets the program crash immediatly after method call, + // when the session service is not started. + // Don't use Q_ASSERT(doc != nullptr); instead use: + if (doc == nullptr) { + if (!_emitted_status_error_already) { + emit statusFailed(tr("Can't connect to underlying session service! " + "Is the session service started?")); + + _emitted_status_error_already = true; + } + + return; + } + + _emitted_status_error_already = false; QJsonObject jObject = doc->object(); QVariantMap mainMap = jObject.toVariantMap(); QString new_status = mainMap["status"].toString(); qDebug() << "status_response() retrieved json. Status:" << new_status; - // Enable (dis)connect button - _main_gui->setConnectButtonEnabled(true); - - this->setStatus(new_status); - - if (this->isSessionAliveOrRunning(new_status)) { - qDebug() << "Session is still alive or running. -> Restarting the status timer."; - - // Ask status every 1000 millisecond - QTimer::singleShot(1000, this, &Session::statusTimerEvent); - } -} + setStatus(new_status); -void Session::onCloseHandler() { - // To cleanup things here - _dbus_api->stop_request(_host, this->getSessionID()); + emit statusSucceeded(); } diff --git a/src/session.h b/src/session.h index 5935919..e1c924f 100644 --- a/src/session.h +++ b/src/session.h @@ -33,48 +33,41 @@ #include <QTimerEvent> #include <QTranslator> -#include "main_qmladaptor.h" #include "RWAHost.h" #include "DBusAPI.h" -class Session : public QObject -{ +class Session : public QObject { Q_OBJECT - // this makes status available as a QML property - Q_PROPERTY(QString status READ getStatus NOTIFY statusChanged) - // this makes session_id available as a QML property - Q_PROPERTY(QString session_id READ getSessionID NOTIFY sessionIDChanged) - // this makes url available as a QML property - Q_PROPERTY(QString url READ getURL NOTIFY urlChanged) - // this makes pin available as a QML property - Q_PROPERTY(QString pin READ getPin NOTIFY pinChanged) public: - explicit Session(QObject *parent, MainQMLAdaptor *main_gui = nullptr, + explicit Session(DBusAPI *dbus_api = nullptr, RWAHost *host = nullptr); + ~Session(); QString getStatus(); QString getURL(); QString getSessionID(); QString getPin(); + RWAHost* getHost(); void setStatus(QString status); void setURL(QString url); void setSessionID(QString session_id); void setPin(QString pin); + void setHost(RWAHost *host); void start(); void stop(); void status(); void refresh_status(); -protected: - QString _status; - void statusTimerEvent(); - void init_vars(); + bool started; private: - MainQMLAdaptor *_main_gui; + void statusTimerEvent(); + + bool _emitted_status_error_already; + QString _status; RWAHost *_host; DBusAPI *_dbus_api; @@ -82,25 +75,30 @@ private: QString _url; QString _pin; - // Returns true if a session is somewhat usable (Running, Alive, etc..) - bool isSessionAliveOrRunning(QString status); - bool _minimizedBefore = false; - void minimizeWindow(); + signals: - void finished(); void statusChanged(QString); void sessionIDChanged(QString); void urlChanged(QString); void pinChanged(QString); + void hostChanged(RWAHost*); + + void startFailed(QString error_message); + void stopFailed(QString error_message); + void statusFailed(QString error_message); + + void startSucceeded(); + void stopSucceeded(); + void statusSucceeded(); public slots: - void handleConnectButtonClick(bool checked); - void onCloseHandler(); + // Returns true if a session is somewhat usable (Running, Alive, etc..) + bool isSessionAliveOrRunning(); - void start_response(QJsonDocument *doc); - void stop_response(QJsonDocument *doc); - void status_response(QJsonDocument *doc); + void start_response(QJsonDocument*); + void stop_response(QJsonDocument*); + void status_response(QJsonDocument*); }; #endif // SESSION_H |