From 11787ea278bc6ff6d5bc797b597df3f26e2ec9b4 Mon Sep 17 00:00:00 2001 From: Daniel Teichmann Date: Tue, 6 Jul 2021 19:55:13 +0200 Subject: Introduce DBusAPI and RWAHost classes --- src/session.cpp | 270 ++++++++++++++------------------------------------------ 1 file changed, 66 insertions(+), 204 deletions(-) (limited to 'src/session.cpp') diff --git a/src/session.cpp b/src/session.cpp index f3aa168..1ee4eef 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -26,51 +26,74 @@ #include "session.h" -Session::Session(QObject *parent, MainQMLAdaptor* main_gui) : QObject(parent) { - _initDBus(); +Session::Session(QObject *parent, MainQMLAdaptor* main_gui, RWAHost *host) : QObject(parent) { + Q_ASSERT(main_gui != nullptr); + Q_ASSERT(host != nullptr); + _dbus_api = new DBusAPI(); _main_gui = main_gui; + _host = host; // QML -> MainQMLAdaptor::handleConnectButtonClick --onConnectButtonClick--> this::handleConnectButtonClick QObject::connect(_main_gui, - SIGNAL(onConnectButtonClick(bool)), - this, - SLOT(handleConnectButtonClick(bool))); + 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))); + 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))); + 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))); + SIGNAL(sessionIDChanged(QString)), + main_gui, + SLOT(setSessionID(QString))); // QML -> MainQMLAdaptor::onCloseHandler --onCloseSignal--> session::onCloseHandler QObject::connect(main_gui, - SIGNAL(onCloseSignal()), - this, - SLOT(onCloseHandler())); + 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 triggered"; - this->refresh_status_request_dbus(this->getHostID(), this->getID()); + qDebug() << "Status timer event got triggered just now."; + this->status(); } void Session::init_vars() { setPin("-----"); setSessionID("-----"); - setID("-----"); setURL(tr("Not available yet")); setStatus("unknown"); @@ -89,18 +112,10 @@ QString Session::getURL() { return _url; } -QString Session::getID() { - return QString(_id); -} - QString Session::getSessionID() { return QString(_session_id); } -QString Session::getHostID() { - return QString(_host_id); -} - QString Session::getPin() { return _pin; } @@ -201,11 +216,6 @@ void Session::setURL(QString url) { emit urlChanged(url); } -void Session::setID(QString id) { - _id = id; - emit idChanged(id); -} - void Session::setSessionID(QString session_id) { _session_id = session_id; emit sessionIDChanged(session_id); @@ -218,79 +228,33 @@ void Session::setPin(QString pin) { void Session::handleConnectButtonClick(bool checked) { qDebug() << "-----Connect button handler-----" << - "\nCurrent service-session #" << this->getID() << - "\nCurrent support-session #" << this->getSessionID(); + "\nCurrent session #" << this->getSessionID(); // Stopping even if nothing is running - this->stop_request_dbus(this->getID()); + _dbus_api->stop_request(_host, this->getSessionID()); if (checked) { // Start the Session again - this->start_request_dbus(getHostID()); + _dbus_api->start_request(_host); } qDebug() << "-----\\Connect button handler-----"; } -void Session::_initDBus() { - if (!QDBusConnection::sessionBus().isConnected()) { - qCritical() << "Cannot connect to the D-Bus session bus."; - } - - // Create DBus object - _dbus_rwa = new OrgArcticaProjectRWASupportSessionServiceInterface("org.ArcticaProject.RWASupportSessionService", "/RWASupportSessionService", - QDBusConnection::sessionBus(), this->parent()); - - qDebug("Initialized DBus object!"); -} - -void Session::start_request_dbus(QString host_id) { - qDebug() << "Requesting D-Bus session service to start a new session on host: " << host_id; - bool ok; - host_id.toLongLong(&ok); - if(ok == false){ - qErrnoWarning("Unable to convert id to "); - return; - } - - // Make an asynchrous 'start' call (Response will be sent to 'start_dbus_replied') - QDBusPendingCall async = _dbus_rwa->asyncCall("start", host_id); - QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(async, this); - - QObject::connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)), - this, SLOT(start_dbus_replied(QDBusPendingCallWatcher*))); - +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"); -} -void Session::start_dbus_replied(QDBusPendingCallWatcher *call) { - QString result = ""; - - QDBusPendingReply reply = *call; - if (reply.isError()) { - qDebug() << "D-Bus 'start' request failed, this was the reply:"; - qDebug() << reply.error(); - - // The user should have the oportunity to try again. - this->init_vars(); - - qCritical() << "An error occured while creating a new session!"; - this->setStatus("start_session_error"); - - return; - } else { - result = reply.argumentAt<0>(); - } - call->deleteLater(); + _dbus_api->start_request(_host); +} - qDebug() << "Raw JSON from starting session is:" << result; - QJsonDocument doc = QJsonDocument::fromJson(result.toUtf8()); +void Session::start_response(QJsonDocument *doc) { + Q_ASSERT(doc != nullptr); // Get the QJsonObject - QJsonObject jObject = doc.object(); + QJsonObject jObject = doc->object(); QVariantMap mainMap = jObject.toVariantMap(); // Status of request @@ -308,16 +272,7 @@ void Session::start_dbus_replied(QDBusPendingCallWatcher *call) { return; } - // Service ID == PID bool ok; - long long service_id = mainMap["id"].toLongLong(&ok); - this->setID(QString::number(service_id)); - // Sanity Check - if(ok == false){ - qErrnoWarning("Unable to parse out of dbus answer!"); - init_vars(); - return; - } // URL of remote web app frontend QString url = mainMap["url"].toString(); @@ -343,37 +298,17 @@ void Session::start_dbus_replied(QDBusPendingCallWatcher *call) { return; } - qDebug() << "Got service_id:" << service_id << - "\nGot session_id:" << session_id << + qDebug() << "Got session_id:" << session_id << "\nGot url:" << url << "\nGot pin:" << pin; emit pinChanged(QString::number(pin)); emit urlChanged(url); - emit idChanged(QString::number(service_id)); emit sessionIDChanged(QString::number(session_id)); } -void Session::stop_request_dbus(QString id) { - bool ok; - if (id.toLongLong(&ok) == 0 ){ - qDebug() << "Won't send a request to D-Bus service to stop a session when session ID == 0"; - return; - } - if(ok == false){ - qErrnoWarning("Unable to convert id to "); - return; - } - - // Stopping now. - qDebug() << "Requesting D-Bus session service to stop session #" << id; - - // Make an asynchrous 'stop' call (Response will be sent to 'stop_dbus_replied') - QDBusPendingCall async = _dbus_rwa->asyncCall("stop", id); - QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(async, this); - - QObject::connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)), - this, SLOT(stop_dbus_replied(QDBusPendingCallWatcher*))); +void Session::stop() { + _dbus_api->stop_request(_host, this->getSessionID()); // Clear current variables this->init_vars(); @@ -384,30 +319,14 @@ void Session::stop_request_dbus(QString id) { _main_gui->setConnectButtonEnabled(false); } -void Session::stop_dbus_replied(QDBusPendingCallWatcher *call) { - QString result = ""; - - QDBusPendingReply reply = *call; - if (reply.isError()) { - qDebug() << "D-Bus 'stop' request failed, this was the reply:"; - qDebug() << reply.error(); - - this->init_vars(); - this->setStatus("stop_session_error"); - - return; - } else { - result = reply.argumentAt<0>(); - } - call->deleteLater(); +void Session::stop_response(QJsonDocument *doc) { + Q_ASSERT(doc != nullptr); - // Get the QJsonObject - QJsonDocument doc = QJsonDocument::fromJson(result.toUtf8()); - QJsonObject jObject = doc.object(); + QJsonObject jObject = doc->object(); QVariantMap mainMap = jObject.toVariantMap(); QString new_status = mainMap["status"].toString(); - qDebug() << "stop_dbus_replied(): Refreshed status:" << new_status; + qDebug() << "stop_response() retrieved json. Status now:" << new_status; // Clear current variables this->init_vars(); @@ -416,74 +335,17 @@ void Session::stop_dbus_replied(QDBusPendingCallWatcher *call) { this->setStatus(new_status); } -void Session::status_request_dbus(QString id) { - bool ok; - if (id.toLongLong(&ok) == 0 ){ - qDebug() << "Won't send a request to D-Bus service of a session's status when session ID == 0"; - return; - } - if(ok == false){ - qErrnoWarning("Unable to convert id to "); - return; - } - - // Requesting status now. - qDebug() << "Requesting status for session #" << id; - - // Make an asynchrous 'status' call (Response will be sent to 'status_dbus_replied') - QDBusPendingCall async = _dbus_rwa->asyncCall("status", id); - QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(async, this); - - QObject::connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)), - this, SLOT(stop_dbus_replied(QDBusPendingCallWatcher*))); +void Session::status() { + _dbus_api->status_request(_host, this->getSessionID()); } -void Session::refresh_status_request_dbus(QString host_id, QString id) { - bool ok; - if (id.toLongLong(&ok) == 0){ - qDebug() << "Won't send a request to D-Bus service to refresh the status of a session when session ID == 0"; - return; - } - if(ok == false){ - qErrnoWarning("Unable to convert id to "); - return; - } - - // Refreshing status - qDebug() << "Requesting status refresh for session #" << id; - - // Make an asynchrous 'refresh_status' call (Response will be sent to 'status_dbus_replied') - QDBusPendingCall async = _dbus_rwa->asyncCall("refresh_status", host_id, id); - QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(async, this); - - QObject::connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)), - this, SLOT(status_dbus_replied(QDBusPendingCallWatcher*))); -} +void Session::status_response(QJsonDocument *doc) { + Q_ASSERT(doc != nullptr); -void Session::status_dbus_replied(QDBusPendingCallWatcher *call) { - QString result = ""; - - QDBusPendingReply reply = *call; - if (reply.isError()) { - qDebug() << "D-Bus '(refresh_)status' request failed, this was the reply:"; - qDebug() << reply.error(); - - this->init_vars(); - - this->setStatus("status_session_error"); - - return; - } else { - result = reply.argumentAt<0>(); - } - call->deleteLater(); - - // Get the QJsonObject - QJsonDocument doc = QJsonDocument::fromJson(result.toUtf8()); - QJsonObject jObject = doc.object(); + QJsonObject jObject = doc->object(); QVariantMap mainMap = jObject.toVariantMap(); QString new_status = mainMap["status"].toString(); - qDebug() << "status_dbus_replied(): Refreshed status:" << new_status; + qDebug() << "status_response() retrieved json. Status:" << new_status; // Enable (dis)connect button _main_gui->setConnectButtonEnabled(true); @@ -498,5 +360,5 @@ void Session::status_dbus_replied(QDBusPendingCallWatcher *call) { void Session::onCloseHandler() { // To cleanup things here - this->stop_request_dbus(this->getID()); + _dbus_api->stop_request(_host, this->getSessionID()); } -- cgit v1.2.3 From 98049d1507a6f2ae232782fd79f4f753ad53eead Mon Sep 17 00:00:00 2001 From: Daniel Teichmann Date: Tue, 6 Jul 2021 20:02:31 +0200 Subject: Fix copyright headers --- src/session.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/session.cpp') diff --git a/src/session.cpp b/src/session.cpp index 1ee4eef..4640786 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -1,8 +1,8 @@ /* * This file is part of Remote Support Desktop * https://gitlab.das-netzwerkteam.de/RemoteWebApp/rwa.support.desktopapp - * Copyright 2020-2021 Daniel Teichmann - * Copyright 2020-2021 Mike Gabriel + * Copyright 2020, 2021 Daniel Teichmann + * Copyright 2020, 2021 Mike Gabriel * SPDX-License-Identifier: GPL-2.0-or-later * * This program is free software; you can redistribute it and/or modify -- cgit v1.2.3 From 61b4ee3b8f1d4c5d85b7a561551167fa41b12400 Mon Sep 17 00:00:00 2001 From: Daniel Teichmann Date: Tue, 6 Jul 2021 20:04:48 +0200 Subject: Tidy up some debug statements or long strings --- src/session.cpp | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'src/session.cpp') diff --git a/src/session.cpp b/src/session.cpp index 4640786..14b6575 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -158,7 +158,8 @@ void Session::setStatus(QString status) { emit _main_gui->showWindow(); - _main_gui->showToast(tr("Remote Support session was stopped ungracefully"), 5000); + _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"); @@ -183,24 +184,29 @@ void Session::setStatus(QString status) { 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); + _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(tr("Session was started successfully")); + _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!"); + 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!"); + 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(); @@ -353,6 +359,8 @@ void Session::status_response(QJsonDocument *doc) { 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); } -- cgit v1.2.3 From 54249d9c37ce3994b03123ee6367c7f5519d3b40 Mon Sep 17 00:00:00 2001 From: Daniel Teichmann Date: Wed, 7 Jul 2021 15:03:13 +0200 Subject: Introduce RWAHostModel. RWAHost's are now loaded on start. Scene_remote_{view, control} are no longer available if no host is selected. --- src/session.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'src/session.cpp') diff --git a/src/session.cpp b/src/session.cpp index 14b6575..32aca7d 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -82,7 +82,6 @@ Session::Session(QObject *parent, MainQMLAdaptor* main_gui, RWAHost *host) : QOb this, SLOT(status_response(QJsonDocument*))); - this->init_vars(); } -- cgit v1.2.3 From 766a90125df68f065495354c20412faf9e1df77a Mon Sep 17 00:00:00 2001 From: Daniel Teichmann Date: Sun, 1 Aug 2021 02:19:15 +0200 Subject: move scenes/Scene_remote_control.qml into scenes/remote_control/Scene_remote_control.qml --- src/session.cpp | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) (limited to 'src/session.cpp') diff --git a/src/session.cpp b/src/session.cpp index 32aca7d..a524afe 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -310,22 +310,36 @@ void Session::start_response(QJsonDocument *doc) { emit pinChanged(QString::number(pin)); emit urlChanged(url); emit sessionIDChanged(QString::number(session_id)); -} -void Session::stop() { - _dbus_api->stop_request(_host, this->getSessionID()); + started = true; - // Clear current variables - this->init_vars(); + // Ask status every 1000 millisecond - // 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); + QTimer *timer = new QTimer(this); + connect(timer, &QTimer::timeout, this, + QOverload<>::of(&Session::statusTimerEvent)); + timer->start(1000); + + qDebug() << "Successfully started a session."; + this->setStatus("start_session_success"); + + emit startSucceeded(); +} + +void Session::stop() { + if (started) + _dbus_api->stop_request(getHost(), getSessionID()); } void Session::stop_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 stopFailed(tr("Can't connect to underlying session service! " + "Is the session service started?")); + return; + } QJsonObject jObject = doc->object(); QVariantMap mainMap = jObject.toVariantMap(); -- cgit v1.2.3 From 35343e5158ca441f8001c0a91685f4e93b53cb03 Mon Sep 17 00:00:00 2001 From: Daniel Teichmann Date: Sun, 1 Aug 2021 02:22:57 +0200 Subject: Toast: Add a variety of new toast colors; Session complete rewrite; A lot of bugfixing; New available toast colors/types: ToastType.Standard, ToastType.Info, ToastType.Warning ToastType.Success, ToastType.Error --- src/session.cpp | 320 ++++++++++++++++++-------------------------------------- 1 file changed, 103 insertions(+), 217 deletions(-) (limited to 'src/session.cpp') 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 out of dbus answer!"); - init_vars(); + QString error_message = "Unable to parse 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 out of dbus answer!"); - init_vars(); + QString error_message = "Unable to parse 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(); } -- cgit v1.2.3