aboutsummaryrefslogtreecommitdiff
path: root/src/main_qmladaptor.cpp
blob: c321ec8f79927c5def261d338ec06486647f306b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
 * This file is part of Remote Support Desktop
 * https://gitlab.das-netzwerkteam.de/RemoteWebApp/rwa.support.desktopapp
 * Copyright 2020, 2021 Daniel Teichmann <daniel.teichmann@das-netzwerkteam.de>
 * Copyright 2020, 2021 Mike Gabriel <mike.gabriel@das-netzwerkteam.de>
 * 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 <https://www.gnu.org/licenses/>.
 */

#include "main_qmladaptor.h"

MainQMLAdaptor::MainQMLAdaptor(QObject *parent, QQmlApplicationEngine* engine,
                               DBusAPI *dbus_api) : QObject(parent) {
    Q_ASSERT(engine != nullptr);
    Q_ASSERT(dbus_api != nullptr);

    _engine = engine;
    _dbus_api = dbus_api;
    _rwaHostModel = new QList<QObject*>;

    QTimer *timer = new QTimer(this);
    connect(timer, &QTimer::timeout, _dbus_api, QOverload<>::of(&DBusAPI::get_web_app_hosts_request));
    timer->start(10000);
}

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<RWAHost *>(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();

    // Find item via 'objectName'
    QObject *sidebar_drawer = _engine->rootObjects().takeFirst()->findChild<QObject*>("sidebar_drawer");
    if (sidebar_drawer) {
        sidebar_drawer->setProperty("rwaHostIsSelected", true);
    } else {
        qWarning() << "Unable to find 'sidebar_drawer' Item!";
    }
}

void MainQMLAdaptor::setRWAHostModel(QList<QObject*> *rwa_hosts) {
    _rwaHostModel = rwa_hosts;
    emit rwaHostModelChanged(*rwa_hosts);
}

void MainQMLAdaptor::addRWAHost(RWAHost *rwa_host) {
    _rwaHostModel->append(rwa_host);
    emit rwaHostModelChanged(*_rwaHostModel);
}

QList<QObject*> MainQMLAdaptor::getRWAHostModel() {
    return *_rwaHostModel;
}

void MainQMLAdaptor::get_web_app_hosts_response(QJsonDocument *doc) {
    Q_ASSERT(doc != nullptr);

    delete _rwaHostModel;
    setRWAHostModel(new QList<QObject*>);
    bool atLeastOneHostAvailable = false;

    // 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;

                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);
            atLeastOneHostAvailable = true;

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

    if (!atLeastOneHostAvailable) {
        main_content_replace("scenes/Scene_no_server_available.qml");
        //addRWAHost(new RWAHost("-","Kein Server verfügbar!","-"));
    }
}

void MainQMLAdaptor::main_content_push(QString scene) {
    // Find item via 'objectName'
    QObject *main_content = _engine->rootObjects().takeFirst()->findChild<QObject*>("main_content");
    Q_ASSERT(main_content != nullptr);
    QMetaObject::invokeMethod(main_content, "push", Q_ARG(QString, scene));
}

void MainQMLAdaptor::main_content_pop(QString scene) {
    // Find item via 'objectName'
    QObject *main_content = _engine->rootObjects().takeFirst()->findChild<QObject*>("main_content");
    Q_ASSERT(main_content != nullptr);
    QMetaObject::invokeMethod(main_content, "pop", Q_ARG(QString, scene));
}

void MainQMLAdaptor::main_content_replace(QString scene) {
    // Find item via 'objectName'
    QObject *window = _engine->rootObjects().takeFirst();
    Q_ASSERT(window != nullptr);

    QVariant arg = QVariant::fromValue(scene);
    if(!QMetaObject::invokeMethod(window, "main_content_replace", Q_ARG(QVariant, arg)))
        qDebug() << "Failed to invoke push";
}

bool MainQMLAdaptor::setConnectButtonEnabled(bool enabled) {
    // Find item via 'objectName'
    QQuickItem *scene_remote_control = _engine->rootObjects().takeFirst()->findChild<QQuickItem*>("Scene_remote_control");
    QQuickItem *item = scene_remote_control->findChild<QQuickItem*>("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 *scene_remote_control = _engine->rootObjects().at(0)->findChild<QQuickItem*>("Scene_remote_control");
    QQuickItem *item = scene_remote_control->findChild<QQuickItem*>("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 *scene_remote_control = _engine->rootObjects().at(0)->findChild<QQuickItem*>("Scene_remote_control");
    QQuickItem *item = scene_remote_control->findChild<QQuickItem*>("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 *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));
}