From 0a48722a1ddeccfc89f1812c44b7582358671697 Mon Sep 17 00:00:00 2001 From: Daniel Teichmann Date: Sun, 1 Aug 2021 02:01:35 +0200 Subject: Introducing remote_control_manager.(h, cpp) The remote_control_manager class takes care of the GUI and the creation, deletion and maintaining of session objects. --- qml.qrc | 20 +- rwa-support-desktopapp.pro | 13 +- src/main.cpp | 22 +- src/scenes/remote_control/Scene_remote_control.qml | 382 +++++++++++++++ .../remote_control/remote_control_manager.cpp | 511 +++++++++++++++++++++ src/scenes/remote_control/remote_control_manager.h | 78 ++++ 6 files changed, 1009 insertions(+), 17 deletions(-) create mode 100644 src/scenes/remote_control/Scene_remote_control.qml create mode 100644 src/scenes/remote_control/remote_control_manager.cpp create mode 100644 src/scenes/remote_control/remote_control_manager.h diff --git a/qml.qrc b/qml.qrc index b472fe7..35e9142 100644 --- a/qml.qrc +++ b/qml.qrc @@ -1,13 +1,17 @@ + qtquickcontrols2.conf images/into-clipboard.svg - locales/bin/de_DE.qm - locales/bin/main_en.qm locales/de_DE.ts + locales/de_DE.qm locales/main_en.ts - locales/bin/es_ES.qm + locales/main_en.qm locales/es_ES.ts + locales/es_ES.qm src/main.qml + src/ListItem.qml + src/ToastManager.qml + src/Toast.qml src/main.cpp src/main_qmladaptor.cpp src/main_qmladaptor.h @@ -21,17 +25,15 @@ src/RWAHostModel.cpp src/DBusAPI.h src/DBusAPI.cpp - src/ListItem.qml - src/ToastManager.qml - src/Toast.qml - qtquickcontrols2.conf - src/scenes/Scene_remote_control.qml src/scenes/Scene_remote_view.qml src/scenes/Scene_settings.qml src/scenes/Scene_placeholder.qml src/scenes/Scene_no_server_available.qml - src/scenes/add_server_wizard/Scene_step_1.qml src/scenes/add_server_wizard/add_server_wizard.cpp src/scenes/add_server_wizard/add_server_wizard.h + src/scenes/add_server_wizard/Scene_step_1.qml + src/scenes/remote_control/remote_control_manager.cpp + src/scenes/remote_control/remote_control_manager.h + src/scenes/remote_control/Scene_remote_control.qml diff --git a/rwa-support-desktopapp.pro b/rwa-support-desktopapp.pro index a15c4bd..be6011e 100644 --- a/rwa-support-desktopapp.pro +++ b/rwa-support-desktopapp.pro @@ -47,19 +47,26 @@ SOURCES += src/main.cpp \ src/main_qmladaptor.cpp \ src/RWADBusAdaptor.cpp \ src/session.cpp \ - src/scenes/add_server_wizard/add_server_wizard.cpp \ src/RWAHostModel.cpp \ src/RWAHost.cpp \ src/DBusAPI.cpp \ + src/scenes/remote_control/remote_control_manager.cpp \ + src/scenes/add_server_wizard/add_server_wizard.cpp HEADERS += src/RWADBusAdaptor.h \ src/main_qmladaptor.h \ src/RWADBusAdaptor.h \ + src/scenes/remote_control/remote_control_manager.h \ src/session.h \ - src/scenes/add_server_wizard/add_server_wizard.h \ src/RWAHostModel.h \ src/RWAHost.h \ - src/DBusAPI.h + src/DBusAPI.h \ + src/scenes/remote_control/remote_control_manager.h \ + src/scenes/add_server_wizard/add_server_wizard.h + +TRANSLATIONS += locales/main_en.ts \ + locales/de_DE.ts \ + locales/es_ES.ts RESOURCES += qml.qrc diff --git a/src/main.cpp b/src/main.cpp index 7139883..9436a2b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -118,11 +118,23 @@ int main(int argc, char *argv[]) { main_gui.data(), SLOT(onRwaHostSelected(QString))); - // Make add_server_wizard available to QML - QScopedPointer wizard (new Add_Server_wizard(&app, - main_gui.data(), - dbus_api.data())); - engine.rootContext()->setContextProperty("add_server_wizard", wizard.data()); + // Make 'add_server_wizard' available to QML + QScopedPointer wizard ( + new Add_Server_wizard(&app, + main_gui.data(), + dbus_api.data()) + ); + engine.rootContext()-> + setContextProperty("add_server_wizard", wizard.data()); + + // Make 'remote_control_manager' available to QML + QScopedPointer remote_mngr ( + new RemoteControlManager(&engine, + main_gui.data(), + dbus_api.data()) + ); + engine.rootContext()-> + setContextProperty("remote_control_manager", remote_mngr.data()); return app.exec(); } diff --git a/src/scenes/remote_control/Scene_remote_control.qml b/src/scenes/remote_control/Scene_remote_control.qml new file mode 100644 index 0000000..dc152ef --- /dev/null +++ b/src/scenes/remote_control/Scene_remote_control.qml @@ -0,0 +1,382 @@ +import QtQuick 2.9 +import QtQuick.Window 2.2 +import QtQuick.Extras 1.4 +import QtQuick.Controls 2.2 +import QtQuick.Dialogs 1.2 +import QtQuick.Controls.Material 2.3 + +/*! + * This .qml file is a Scene which can be loaded through for + * example a StackView (main_content in main.qml). + */ + +Item { + id: scene_remote_control + objectName: "Scene_remote_control" + + Label { + id: dbus_api_status_text + text: qsTr("Unknown state of session service.") + anchors.leftMargin: 10 + 5 + dbus_api_status_indicator.width + anchors.bottom: parent.bottom + anchors.bottomMargin: 10 + wrapMode: Text.WordWrap + anchors.rightMargin: 10 + verticalAlignment: Text.AlignVCenter + font.pointSize: 11 + fontSizeMode: Text.Fit + objectName: "dbus_api_status_text" + anchors.left: parent.left + anchors.right: parent.right + + StatusIndicator { + id: dbus_api_status_indicator + width: height + height: 20 + objectName: "dbus_api_status_indicator" + color: "#73d216" + anchors.verticalCenter: parent.verticalCenter + anchors.right: parent.left + anchors.rightMargin: 5 + active: false + } + } + + Label { + id: explain_function_label + text: qsTr("Please tell your remote support \ +partner your access address and \ +your access-PIN to let your partner \ +connect to this computer.") + wrapMode: Text.WordWrap + font.pixelSize: 18 + anchors.topMargin: 10 + anchors.leftMargin: 10 + anchors.rightMargin: 10 + anchors.top: parent.top + anchors.left: parent.left + anchors.right: parent.right + horizontalAlignment: Text.AlignLeft + + color: Material.theme == Material.Light ? "#000000" : "#FFFFFF" + } + + Rectangle { + id: dbus_api_status_line + height: 1 + radius: 1 + anchors.right: parent.right + anchors.rightMargin: 10 + anchors.bottom: dbus_api_status_text.top + anchors.bottomMargin: 10 + opacity: 0.3 + gradient: Gradient { + GradientStop { + position: 0.391 + color: "#ffffff" + } + + GradientStop { + position: 0.975 + color: "#8b8b8b" + } + } + border.width: 1 + border.color: "#00000000" + anchors.left: parent.left + anchors.leftMargin: 10 + } + + Column { + id: column + spacing: 6 + anchors.right: parent.right + anchors.rightMargin: 10 + anchors.left: parent.left + anchors.leftMargin: 10 + anchors.bottom: dbus_api_status_line.top + anchors.bottomMargin: 10 + anchors.top: explain_function_label.bottom + anchors.topMargin: 10 + + Column { + id: url_group + width: parent.width + height: parent.height * 0.25 + spacing: 5 + + Label { + id: your_url_text + height: parent.height/2 + text: qsTr("Remote Support Address") + font.weight: Font.Bold + font.bold: true + verticalAlignment: Text.AlignBottom + horizontalAlignment: Text.AlignLeft + font.pointSize: 14 + fontSizeMode: Text.Fit + } + + TextEdit { + id: url_text + height: parent.height/2 + text: remote_control_manager.url + anchors.rightMargin: 10 + copy_url_to_clipboard_button.width + anchors.right: parent.right + wrapMode: Text.WrapAtWordBoundaryOrAnywhere + anchors.leftMargin: 10 + verticalAlignment: Text.AlignVCenter + horizontalAlignment: Text.AlignLeft + font.pointSize: 15 + + readOnly: true + color: Material.foreground + selectByMouse: true + anchors.left: parent.left + + leftPadding: 5 + Rectangle { + radius: 5 + color: Material.theme == Material.Light ? "#F0F0F0" : "#383838" + height: url_text.height + // whole line + copy-into-clipboard button + some margin + width: url_text.width + copy_url_to_clipboard_button.width + 5 + 5 + x: 0; y: 0 + z: -1 + } + + Button { + id: copy_url_to_clipboard_button + width: copy_url_to_clipboard_image.width + 6 + height: copy_url_to_clipboard_image.height + 6 + 10 + anchors.verticalCenter: parent.verticalCenter + display: AbstractButton.IconOnly + anchors.leftMargin: 5 + anchors.left: url_text.right + highlighted: false + flat: true + + Image { + id: copy_url_to_clipboard_image + x: 0 + y: -26 + anchors.horizontalCenter: parent.horizontalCenter + anchors.verticalCenter: parent.verticalCenter + source: "../../../images/into-clipboard.svg" + opacity: 0.65 + } + + onClicked: { + remote_control_manager.handleCopyToClipboardButtonClick(url_text.text); + toast.show(qsTr("Copied access address into clipboard!"), + "5000", + mainqmladaptor.ToastInfo); + } + + ToolTip.text: qsTr("Copy the access address into the clipboard") + hoverEnabled: true + + ToolTip.delay: 600 + ToolTip.timeout: 5000 + ToolTip.visible: hovered + } + } + } + + Column { + id: session_id_group + width: parent.width + height: parent.height * 0.25 + spacing: 5 + + Label { + id: your_session_id_text + height: parent.height/2 + text: qsTr("Session-ID") + font.weight: Font.Bold + font.bold: true + anchors.right: parent.right + anchors.rightMargin: 0 + anchors.left: parent.left + anchors.leftMargin: 0 + font.pointSize: 14 + verticalAlignment: Text.AlignBottom + horizontalAlignment: Text.AlignLeft + fontSizeMode: Text.Fit + } + + TextEdit { + objectName: "session_id_text" + id: session_id_text + height: parent.height/2 + text: remote_control_manager.session_id + font.letterSpacing: 10 + anchors.rightMargin: 10 + copy_session_id_to_clipboard_button.width + anchors.right: parent.right + font.pointSize: 15 + anchors.left: parent.left + anchors.leftMargin: 10 + horizontalAlignment: Text.AlignLeft + verticalAlignment: Text.AlignVCenter + + leftPadding: 5 + Rectangle { + radius: 5 + color: Material.theme == Material.Light ? "#F0F0F0" : "#383838" + height: session_id_text.height + // whole line + copy-into-clipboard button + some margin + width: session_id_text.width + copy_session_id_to_clipboard_button.width + 5 + 5 + x: 0; y: 0 + z: -1 + } + + readOnly: true + color: Material.foreground + wrapMode: Text.WordWrap + selectByMouse: true + + Button { + id: copy_session_id_to_clipboard_button + width: copy_session_id_to_clipboard_image.width + 6 + height: copy_session_id_to_clipboard_image.height + 6 + 10 + anchors.verticalCenter: parent.verticalCenter + flat: true + display: AbstractButton.IconOnly + anchors.left: session_id_text.right + anchors.leftMargin: 5 + + Image { + id: copy_session_id_to_clipboard_image + anchors.horizontalCenter: parent.horizontalCenter + opacity: 0.65 + anchors.verticalCenter: parent.verticalCenter + source: "../../../images/into-clipboard.svg" + fillMode: Image.PreserveAspectFit + } + + onClicked: { + remote_control_manager.handleCopyToClipboardButtonClick(session_id_text.text); + toast.show(qsTr("Copied session-ID into clipboard!"), + "5000", + mainqmladaptor.ToastInfo); + } + + ToolTip.text: qsTr("Copy the session-ID into the clipboard") + hoverEnabled: true + + ToolTip.delay: 600 + ToolTip.timeout: 5000 + ToolTip.visible: hovered + } + } + } + + Column { + id: pin_group + width: parent.width + height: parent.height * 0.25 + spacing: 5 + + Label { + id: your_pin_text + height: parent.height/2 + text: qsTr("Access-PIN") + font.weight: Font.Bold + font.bold: true + anchors.right: parent.right + anchors.rightMargin: 0 + anchors.left: parent.left + anchors.leftMargin: 0 + font.pointSize: 14 + verticalAlignment: Text.AlignBottom + horizontalAlignment: Text.AlignLeft + } + + TextEdit { + objectName: "pin_text" + id: pin_text + height: parent.height/2 + text: remote_control_manager.pin + anchors.rightMargin: 10 + copy_pin_to_clipboard_button.width + anchors.right: parent.right + font.pointSize: 15 + anchors.left: parent.left + anchors.leftMargin: 10 + font.letterSpacing: 10 + horizontalAlignment: Text.AlignLeft + verticalAlignment: Text.AlignVCenter + + leftPadding: 5 + Rectangle { + radius: 5 + color: Material.theme == Material.Light ? "#F0F0F0" : "#383838" + height: url_text.height + // whole line + copy-into-clipboard button + some margin + width: url_text.width + copy_url_to_clipboard_button.width + 5 + 5 + x: 0; y: 0 + z: -1 + } + + readOnly: true + color: Material.foreground + wrapMode: Text.WordWrap + selectByMouse: true + + Button { + id: copy_pin_to_clipboard_button + width: copy_pin_to_clipboard_image.width + 6 + height: copy_pin_to_clipboard_image.height + 6 + 10 + anchors.verticalCenter: parent.verticalCenter + flat: true + display: AbstractButton.IconOnly + anchors.left: pin_text.right + anchors.leftMargin: 5 + + Image { + id: copy_pin_to_clipboard_image + anchors.verticalCenter: parent.verticalCenter + opacity: 0.65 + anchors.horizontalCenter: parent.horizontalCenter + source: "../../../images/into-clipboard.svg" + fillMode: Image.PreserveAspectFit + } + + onClicked: { + remote_control_manager.handleCopyToClipboardButtonClick(pin_text.text); + toast.show(qsTr("Copied PIN into clipboard!"), + "5000", + mainqmladaptor.ToastInfo); + } + + ToolTip.text: qsTr("Copy the pin into the clipboard") + hoverEnabled: true + + ToolTip.delay: 600 + ToolTip.timeout: 5000 + ToolTip.visible: hovered + } + } + } + + } + + Button { + id: start_support_button + height: Math.min(50) + objectName: "start_support_button" + text: checked ? qsTr("Stop remote support session") : qsTr("Start remote support session") + anchors.rightMargin: column.anchors.leftMargin + anchors.bottom: dbus_api_status_line.top + anchors.bottomMargin: 10 + anchors.right: parent.right + checkable: true + + onClicked: remote_control_manager.handleConnectButtonClick(checked); + } +} + +/*##^## +Designer { + D{i:0;autoSize:true;height:480;width:640} +} +##^##*/ diff --git a/src/scenes/remote_control/remote_control_manager.cpp b/src/scenes/remote_control/remote_control_manager.cpp new file mode 100644 index 0000000..b76ea2a --- /dev/null +++ b/src/scenes/remote_control/remote_control_manager.cpp @@ -0,0 +1,511 @@ +#include "remote_control_manager.h" + +#ifndef QT_NO_DEBUG +#define CHECK_TRUE(instruction) Q_ASSERT(instruction) +#else +#define CHECK_TRUE(instruction) (instruction) +#endif + +RemoteControlManager::RemoteControlManager(QQmlApplicationEngine *engine, + MainQMLAdaptor *main_gui, + DBusAPI *dbus_api) : QObject() { + Q_ASSERT(dbus_api != nullptr); + Q_ASSERT(main_gui != nullptr); + Q_ASSERT(engine != nullptr); + + _current_session = nullptr; + _sessions = new QSet; + _dbus_api = dbus_api; + _main_gui = main_gui; + _engine = engine; +} + +bool RemoteControlManager::setConnectButtonEnabled(bool enabled) { + // Find item via 'objectName' + QQuickItem *scene_remote_control = _engine->rootObjects().takeFirst()-> + findChild("Scene_remote_control"); + QQuickItem *item = scene_remote_control-> + findChild("start_support_button"); + if (item) { + item->setProperty("enabled", enabled); + if (item->property("checked").toBool()) { + item->setProperty("text", tr("Stop remote support session")); + } else { + item->setProperty("text", tr("Start remote support session")); + } + } else { + qWarning() << "Unable to find 'start_support_button' Item!"; + return false; + } + + return true; +} + +bool RemoteControlManager::setConnectButtonChecked(bool checked) { + // Find item via 'objectName' + QQuickItem *scene_remote_control = _engine->rootObjects().takeFirst()-> + findChild("Scene_remote_control"); + QQuickItem *item = scene_remote_control-> + findChild("start_support_button"); + if (item) { + item->setProperty("checked", checked); + } else { + qWarning() << "Unable to find 'start_support_button' Item!"; + return false; + } + + return true; +} + +bool RemoteControlManager::setStatusIndicatorText(QString status_text) { + // Find item via 'objectName' + QQuickItem *scene_remote_control = _engine->rootObjects().takeFirst()-> + findChild("Scene_remote_control"); + QQuickItem *item = scene_remote_control-> + findChild("dbus_api_status_text"); + if (item) { + item->setProperty("text", status_text); + } else { + qWarning() << "Unable to find 'dbus_api_status_text' Item!"; + return false; + } + + return true; +} + +bool RemoteControlManager::setStatusIndicatorColor(bool active, QColor color) { + // Find item via 'objectName' + QQuickItem *scene_remote_control = _engine->rootObjects().takeFirst()-> + findChild("Scene_remote_control"); + QQuickItem *item = scene_remote_control-> + findChild("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 RemoteControlManager::handleCopyToClipboardButtonClick(QString copy_data) { + QClipboard *clipboard = QApplication::clipboard(); + QString originalText = clipboard->text(); + clipboard->setText(copy_data); + qDebug() << "Copied into clipboard:" << copy_data; +} + +QString RemoteControlManager::getURL() { + if (getCurrentSession() == nullptr) { + return tr("Not available yet"); + } + return getCurrentSession()->getURL(); +} + +QString RemoteControlManager::getPin() { + if (getCurrentSession() == nullptr) { + return "-----"; + } + return getCurrentSession()->getPin(); +} + +QString RemoteControlManager::getSessionID() { + if (getCurrentSession() == nullptr) { + return "-----"; + } + return getCurrentSession()->getSessionID(); +} + +Session* RemoteControlManager::getCurrentSession() { + return _current_session; +} + +void RemoteControlManager::addSession(Session *session) { + _sessions->insert(session); +} + +bool RemoteControlManager::removeSession(Session *session) { + if (getCurrentSession() == session) { + setCurrentSession(nullptr); + } + + bool ok = _sessions->remove(session); + + if (session != nullptr) { + session->disconnect(); + session->deleteLater(); + } + + setConnectButtonChecked(false); + setConnectButtonEnabled(true); + + currentSessionUrlChanged(tr("Not available yet")); + currentSessionPinChanged("-----"); + currentSessionSessionIDChanged("-----"); + + return ok; +} + +void RemoteControlManager::setCurrentSession(Session *session) { + if (session == nullptr) { + qDebug() << "Deselecting currentSession."; + _current_session = nullptr; + + return; + } + + if (_sessions->contains(session)) { + qDebug() << "Set currentSession to new session."; + _current_session = session; + + connectSession(_current_session); + } else { + qDebug() << "Given session was not in _sessions!"; + } +} + +void RemoteControlManager::connectSession(Session *session) { + // session --statusChanged-> this.currentSessionStatusChanged() + CHECK_TRUE(QObject::connect(session, + &Session::statusChanged, + this, + &RemoteControlManager::currentSessionStatusChanged)); + + // session --pinChanged-> this.currentSessionPinChanged() + CHECK_TRUE(QObject::connect(session, + &Session::pinChanged, + this, + &RemoteControlManager::currentSessionPinChanged)); + + // session --urlChanged-> this.currentSessionUrlChanged() + CHECK_TRUE(QObject::connect(session, + &Session::urlChanged, + this, + &RemoteControlManager::currentSessionUrlChanged)); + + // session --sessionIDChanged-> this.currentSessionSessionIDChanged() + CHECK_TRUE(QObject::connect(session, + &Session::sessionIDChanged, + this, + &RemoteControlManager::currentSessionSessionIDChanged)); + + + // session --startSucceeded -> this.currentSessionStartSucceeded() + CHECK_TRUE(QObject::connect(session, + &Session::startSucceeded, + this, + &RemoteControlManager::currentSessionStartSucceeded)); + + // session --startFailed-> this.currentSessionStartFailed() + CHECK_TRUE(QObject::connect(session, + &Session::startFailed, + this, + &RemoteControlManager::currentSessionStartFailed)); + + + // session --stopSucceeded-> this.currentSessionStopSucceeded() + CHECK_TRUE(QObject::connect(session, + &Session::stopSucceeded, + this, + &RemoteControlManager::currentSessionStopSucceeded)); + + // session --stopFailed-> this.currentSessionStopFailed() + CHECK_TRUE(QObject::connect(session, + &Session::stopFailed, + this, + &RemoteControlManager::currentSessionStopFailed)); + + + // session --statusSucceeded-> this.currentSessionStatusSucceeded() + CHECK_TRUE(QObject::connect(session, + &Session::statusSucceeded, + this, + &RemoteControlManager::currentSessionStatusSucceeded)); + + // session --statusFailed-> this.currentSessionStatusFailed() + CHECK_TRUE(QObject::connect(session, + &Session::statusFailed, + this, + &RemoteControlManager::currentSessionStatusFailed)); +} + +void RemoteControlManager::currentSessionStartFailed(QString error_message) { + _main_gui->showToast(error_message, 6000, Toast::ToastType::ToastError); + + // Start failed. No need to stop session, so remove it directly. + removeSession(getCurrentSession()); + + setConnectButtonChecked(false); + setConnectButtonEnabled(true); + + emit _main_gui->showWindow(); +} + +void RemoteControlManager::currentSessionStopFailed(QString error_message) { + _main_gui->showToast(error_message, 6000, Toast::ToastType::ToastError); + + // Stop failed, so don't do anything! The user should try again. + + // Set status indicator to the corresponding error message. + QString translated = translateStatusIndicatorText("stop_session_error"); + setStatusIndicatorText(translated); + + setConnectButtonChecked(true); + setConnectButtonEnabled(true); + + emit _main_gui->showWindow(); +} + +void RemoteControlManager::currentSessionStatusFailed(QString error_message) { + _main_gui->showToast(error_message, 6000, Toast::ToastType::ToastError); + + // Set status indicator to the corresponding error message. + QString translated = translateStatusIndicatorText("status_session_error"); + setStatusIndicatorText(translated); + + setConnectButtonChecked(false); + setConnectButtonEnabled(true); + + currentSessionUrlChanged(tr("Not available yet")); + currentSessionPinChanged("-----"); + currentSessionSessionIDChanged("-----"); + + emit _main_gui->showWindow(); +} + +void RemoteControlManager::currentSessionStartSucceeded() { + Session *current_session = getCurrentSession(); + if (current_session != nullptr) { + _main_gui->showToast( + tr("Session was started on '%0' successfully") + .arg(current_session->getHost()->alias()), + 6000, + Toast::ToastType::ToastSuccess + ); + } else { + qCritical().noquote() << tr("currentSessionStartSucceeded(): " + "Current Session is nullptr!"); + _engine->exit(1); + } + + // Set status indicator to the corresponding error message. + QString translated = translateStatusIndicatorText("start_session_success"); + setStatusIndicatorText(translated); + + setConnectButtonChecked(true); + setConnectButtonEnabled(true); +} + +void RemoteControlManager::currentSessionStopSucceeded() { + qDebug() << "Session stop succeeded: Delete current session object now."; + bool ok = removeSession(getCurrentSession()); + if (!ok) { + qWarning() << tr("Couldn't remove current session out of '_sessions' list."); + } + + _main_gui->showToast(tr("Remote support session was stopped."), + 6000, + Toast::ToastType::ToastSuccess); + + // Set status indicator to the corresponding error message. + QString translated = translateStatusIndicatorText("stop_session_success"); + setStatusIndicatorText(translated); + + setConnectButtonChecked(false); + setConnectButtonEnabled(true); +} + +void RemoteControlManager::currentSessionUnexpectedStop(QString error_message) { + _main_gui->showToast(error_message, 6000, Toast::ToastType::ToastError); + + setConnectButtonChecked(false); + setConnectButtonEnabled(true); + + qDebug() << "Delete current session object now."; + bool ok = removeSession(getCurrentSession()); + if (!ok) { + qWarning() << tr("Couldn't remove current session out of '_sessions' list."); + } +} + +void RemoteControlManager::currentSessionStatusSucceeded() { + // Nothing to do. +} + +void RemoteControlManager::currentSessionStatusChanged(QString new_status_code) { + Session *current_session = getCurrentSession(); + if (current_session == nullptr || !current_session->started) { + qDebug() << "RemoteControlManager::currentSessionStatusChanged(QString): " + "got called even though the session isn't even started."; + return; + } + QString translated = translateStatusIndicatorText(new_status_code); + setStatusIndicatorText(translated); +} + +QString RemoteControlManager::translateStatusIndicatorText(QString status_code) { + QString guiString = tr("Unknown state of service"); + setStatusIndicatorColor(false); + + if (status_code == "dead") { + + /* Session died */ + guiString = tr("Remote Support session was stopped ungracefully"); + + // Red color + setStatusIndicatorColor(true, QColor(255, 0, 0, 127)); + + currentSessionUnexpectedStop(guiString); + + } else if (status_code == "stopped") { + + /* Session was stopped normally somehow other than the users request. + * Remote support partner could have clicked exit for example */ + guiString = tr("Remote Support session was stopped"); + + // Green color + setStatusIndicatorColor(true, QColor(0, 255, 0, 127)); + + currentSessionUnexpectedStop(guiString); + + } else if (status_code == "active") { + + /* Partner is connected */ + guiString = tr("Your partner is connected to the Remote Support session"); + + // Green color + setStatusIndicatorColor(true, QColor(0, 255, 0, 127)); + + } else if (status_code == "start_session_success" || status_code == "running") { + + /* Session successfully started */ + guiString = tr("Remote Support session successfully started! " + "Waiting for your remote support partner to connect."); + + // yellow color (will be green when partner is connected) + setStatusIndicatorColor(true, QColor(255, 255, 0, 127)); + + } else if (status_code == "start_session_error") { + + /* Session couldn't be started */ + guiString = tr("Remote Support session couldn't be started!"); + + // Red color + setStatusIndicatorColor(true, QColor(255, 0, 0, 127)); + + } else if (status_code == "stop_session_success") { + + /* Session was successfully stopped by the users request */ + guiString = tr("Session stopped successfully."); + + // Green color + setStatusIndicatorColor(true, QColor(0, 255, 0, 127)); + + } else if (status_code == "stop_session_error") { + + /* Session couldn't be stopped */ + guiString = tr("Session could not be stopped!") + "\n" + + tr("remote support partner could still be connected!"); + + // Red color + setStatusIndicatorColor(true, QColor(255, 0, 0, 127)); + + } else if (status_code == "status_session_error") { + + /* Session's status couldn't be refreshed */ + guiString = tr("Session status could not be refreshed! " + "Your remote support partner could still be connected!"); + + // Red color + setStatusIndicatorColor(true, QColor(255, 0, 0, 127)); + + } + + qDebug().noquote() << QString("Translating status code '%0' to '%1'") + .arg(status_code) + .arg(guiString); + return guiString; +} + +void RemoteControlManager::currentSessionPinChanged(QString pin) { + emit pinChanged(pin); +} + +void RemoteControlManager::currentSessionUrlChanged(QString url) { + emit urlChanged(url); +} + +void RemoteControlManager::currentSessionSessionIDChanged(QString session_id) { + emit sessionIDChanged(session_id); +} + +void RemoteControlManager::connectToDBusAPI(Session *session) { + // _dbus_api --sessionStartResponse-> this.start_response() + CHECK_TRUE(QObject::connect(_dbus_api, + &DBusAPI::serviceStartResponse, + session, + &Session::start_response)); + + // _dbus_api --serviceStopResponse-> this.stop_response() + CHECK_TRUE(QObject::connect(_dbus_api, + &DBusAPI::serviceStopResponse, + session, + &Session::stop_response, + Qt::DirectConnection)); + + // _dbus_api --sessionStatusResponse-> this.status_response() + CHECK_TRUE(QObject::connect(_dbus_api, + &DBusAPI::serviceStatusResponse, + session, + &Session::status_response)); +} + +void RemoteControlManager::handleConnectButtonClick(bool checked) { + if (checked) { + // Create a Session object and start it. + qDebug() << "Creating and starting a new session object now."; + RWAHost *selected_host = _main_gui->getSelectedRWAHost(); + if (selected_host) { + qInfo() << tr("Creating a new session object."); + Session *new_session = new Session(_dbus_api, selected_host); + + connectToDBusAPI(new_session); + + qDebug() << "Adding session to QSet"; + addSession(new_session); + + qDebug() << "Setting session as current session."; + setCurrentSession(new_session); + + qInfo().noquote() << tr("Starting a remote support session on host '%0' " + "using the new session object.") + .arg(selected_host->uuid()); + new_session->start(); + } else { + qCritical().noquote() << tr("Can't start a remote support session. " + "There is no RWA host is selected!"); + } + + setConnectButtonChecked(false); + setConnectButtonEnabled(false); + } else { + Session *current_session = getCurrentSession(); + if (current_session != nullptr) { + emit current_session->stop(); + } else { + qCritical().noquote() << tr("RemoteControlManager::" + "handleConnectButtonClick(): Current Session " + "is nullptr!"); + setConnectButtonChecked(false); + setConnectButtonEnabled(true); + } + } +} + +/*void RemoteControlManager::onCloseHandler() { + // To cleanup things here + // check current session nullptr + getCurrentSession()->stop(); +}*/ diff --git a/src/scenes/remote_control/remote_control_manager.h b/src/scenes/remote_control/remote_control_manager.h new file mode 100644 index 0000000..6f2fbdb --- /dev/null +++ b/src/scenes/remote_control/remote_control_manager.h @@ -0,0 +1,78 @@ +#ifndef REMOTECONTROLMANAGER_H +#define REMOTECONTROLMANAGER_H + +#include + +#include "../../main_qmladaptor.h" +#include "../../DBusAPI.h" +#include "../../session.h" + +class RemoteControlManager : public QObject { + + Q_OBJECT + // 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) + // this makes session_id available as a QML property + Q_PROPERTY(QString session_id READ getSessionID NOTIFY sessionIDChanged) + +public: + explicit RemoteControlManager(QQmlApplicationEngine *engine = nullptr, + MainQMLAdaptor *main_gui = nullptr, + DBusAPI *dbus_api = nullptr); + + bool setConnectButtonEnabled(bool enabled); + bool setConnectButtonChecked(bool checked); + bool setStatusIndicatorColor(bool active, QColor color = QColor(255,255,255)); + + QString translateStatusIndicatorText(QString status_code); + bool setStatusIndicatorText(QString status_text); + +private: + DBusAPI *_dbus_api; + MainQMLAdaptor *_main_gui; + QQmlApplicationEngine *_engine; + Session *_current_session; + QSet *_sessions; + + void connectSession(Session *session); + void connectToDBusAPI(Session *session); + + bool refreshTimerIsRunning; + +signals: + void onConnectButtonClick(bool checked); + void pinChanged(QString pin); + void urlChanged(QString URL); + void sessionIDChanged(QString session_id); + +public slots: + void handleCopyToClipboardButtonClick(QString copy_data); + void handleConnectButtonClick(bool checked); + + void setCurrentSession(Session *session); + void addSession(Session *session); + bool removeSession(Session *session); + + void currentSessionStatusChanged(QString); + void currentSessionPinChanged(QString); + void currentSessionSessionIDChanged(QString); + void currentSessionUrlChanged(QString); + + QString getURL(); + QString getPin(); + QString getSessionID(); + Session* getCurrentSession(); + + void currentSessionStartFailed(QString error_message); + void currentSessionStopFailed(QString error_message); + void currentSessionStatusFailed(QString error_message); + void currentSessionUnexpectedStop(QString error_message); + + void currentSessionStartSucceeded(); + void currentSessionStopSucceeded(); + void currentSessionStatusSucceeded(); +}; + +#endif // REMOTECONTROLMANAGER_H -- cgit v1.2.3