/* * 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 * SPDX-License-Identifier: GPL-2.0-or-later * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the * Free Software Foundation, Inc., * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "main_qmladaptor.h" MainQMLAdaptor::MainQMLAdaptor(QObject *parent, QQmlApplicationEngine* engine) : QObject(parent) { Q_ASSERT(engine != nullptr); _engine = engine; _rwaHostModel = new QList; } void MainQMLAdaptor::onRwaHostSelected(QString host_uuid) { Q_ASSERT(host_uuid != ""); RWAHost *_host = nullptr; for (int i = 0; i < getRWAHostModel().size(); i++) { QObject *obj = getRWAHostModel().value(i); RWAHost *host = qobject_cast(obj); Q_ASSERT(host != nullptr); if (host->uuid() == host_uuid) { _host = host; } } Q_ASSERT(_host != nullptr); qDebug() << "RWAHost was selected!" << _host->uuid() << "aka" << _host->alias(); } void MainQMLAdaptor::setRWAHostModel(QList rwa_hosts) { _rwaHostModel = &rwa_hosts; emit rwaHostModelChanged(rwa_hosts); } void MainQMLAdaptor::addRWAHost(RWAHost *rwa_host) { _rwaHostModel->append(rwa_host); emit rwaHostModelChanged(*_rwaHostModel); } QList MainQMLAdaptor::getRWAHostModel() { return *_rwaHostModel; } void MainQMLAdaptor::get_web_app_hosts_response(QJsonDocument *doc) { Q_ASSERT(doc != nullptr); // Get the QJsonObject QJsonObject jObject = doc->object(); QVariantMap mainMap = jObject.toVariantMap(); // Status of request QString request_status = mainMap["status"].toString(); if (request_status == "success") { // Building host_objects QJsonArray host_objects = jObject.value("hosts").toArray(); foreach (const QJsonValue &host_object, host_objects) { QString host_uuid = host_object["uuid"].toString(); QString host_alias = host_object["alias"].toString(); QString host_url = host_object["url"].toString(); if (host_url == "" || host_uuid == "") { // This two values are required and can't be omitted. QString reason = tr("A host object in the response of D-Bus " "service lacks a necessary value. (host_url or host_uuid)"); qCritical().noquote() << tr("An error occured while adding a new host:") << reason; //emit step1Failed(reason); return; } if (host_alias == "") { qDebug().noquote() << QString("An alias for the host wasn't delivered " "so just use '%0' as alias.").arg(host_url); host_alias = host_url; } // Now built RWAHost object. RWAHost *rwa_host = new RWAHost(host_uuid, host_alias, host_url); addRWAHost(rwa_host); qInfo().noquote() << QString(tr("Successfully added new RWAHost '%0'")).arg(rwa_host->alias()); } } else { QString reason = tr("An error occured while adding a new host:"); qCritical().noquote() << reason; QString type = mainMap["type"].toString(); if (type != "") { reason = QString(tr("The error is not clear. The session service " "responded with status type '%0'")).arg(type); qCritical().noquote() << reason; } else { reason = QString(tr("The error is not clear. The session service " "responded with no status type!")); qCritical().noquote() << reason; } return; } } bool MainQMLAdaptor::setConnectButtonEnabled(bool enabled) { // Find item via 'objectName' QQuickItem *item = _engine->rootObjects().at(0)->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 MainQMLAdaptor::setConnectButtonChecked(bool checked) { // Find item via 'objectName' QQuickItem *item = _engine->rootObjects().at(0)->findChild("start_support_button"); if (item) { item->setProperty("checked", checked); } else { qWarning() << "Unable to find 'start_support_button' Item!"; return false; } return true; } bool MainQMLAdaptor::setStatus(QString status) { // Find item via 'objectName' QQuickItem *item = _engine->rootObjects().at(0)->findChild("dbus_api_status_text"); if (item) { item->setProperty("text", status); } else { qWarning() << "Unable to find 'dbus_api_status_text' Item!"; return false; } return true; } bool MainQMLAdaptor::openMessageDialog(QString title, QString text, QMessageBox::Icon icon) { _messageDialogText = text; _messageDialogTitle = title; _messageDialogIcon = icon; _showMessageDialog = true; emit messageDialogIconChanged(_messageDialogIcon); emit messageDialogTitleChanged(_messageDialogTitle); emit messageDialogTextChanged(_messageDialogText); emit showMessageDialogChanged(_showMessageDialog); qDebug() << "Opening MessageDialog!"; return true; } QString MainQMLAdaptor::getMessageDialogTitle() { return _messageDialogTitle; } QString MainQMLAdaptor::getMessageDialogText() { return _messageDialogText; } QMessageBox::Icon MainQMLAdaptor::getMessageDialogIcon() { return _messageDialogIcon; } bool MainQMLAdaptor::getShowMessageDialog() { return _showMessageDialog; } bool MainQMLAdaptor::setStatusIndicator(bool active, QColor color) { // Find item via 'objectName' QQuickItem *item = _engine->rootObjects().at(0)->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 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::onCloseHandler() { // qDebug() << "Inside MainQMLAdaptor::onCloseHandler()"; // // Sending onClose signal to main and there to Session::onCloseHandler() // emit MainQMLAdaptor::onCloseSignal(); //}