aboutsummaryrefslogtreecommitdiff
path: root/src/session.cpp
blob: 1fa97d095f9dcaea3a9dafc3a81c4c7963bcd0e3 (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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*
 * 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 "session.h"

Session::Session(QObject *parent, MainQMLAdaptor* main_gui) : QObject(parent) {
    _initDBus();

    _main_gui = main_gui;

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

    this->init_vars();
}

void Session::statusTimerEvent() {
    qDebug() << "Status timer event triggered";
    this->refresh_status_request_dbus(this->getId());
}

void Session::init_vars() {
    setPin("-----");
    setSessionID("-----");
    setId("-----");
    setURL(tr("Not available yet"));
    setStatus("unknown");

    _main_gui->setConnectButtonChecked(false);
    _main_gui->setConnectButtonEnabled(true);
    _main_gui->setStatusIndicator(false);

    _minimizedBefore = false;
}

QString Session::getStatus() {
    return _status;
}

QString Session::getURL() {
    return _url;
}

QString Session::getId() {
    return QString(_id);
}

QString Session::getSessionID() {
    return QString(_session_id);
}

QString Session::getPin() {
    return _pin;
}

bool Session::isSessionAliveOrRunning(QString status) {
    if (status == "running" || status == "active") {
        return true;
    } else {
        return false;
    }
}

void Session::minimizeWindow() {
    if (!_minimizedBefore) {
        qDebug() << "Minimizing window now...";
        emit _main_gui->minimizeWindow();
        _minimizedBefore = true;
    }
}

void Session::setStatus(QString status) {
    _status = 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(tr("Session was started successfully"));
    } 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();
    }

    _main_gui->setStatus(guiString);

    emit statusChanged(_status);
}

void Session::setURL(QString url) {
    _url = 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);
}

void Session::setPin(QString pin) {
    _pin = pin;
    emit pinChanged(pin);
}

void Session::handleConnectButtonClick(bool checked) {
    qDebug() << "-----Connect button handler-----" <<
                "\nCurrent service-session #" << this->getId() <<
                "\nCurrent support-session #" << this->getSessionID();

    // Stopping even if nothing is running
    this->stop_request_dbus(this->getId());

    if (checked) {
        // Start the Session again
        this->start_request_dbus();
    }
    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 OrgArcticaProjectRWAInterface("org.ArcticaProject.RWASupportSessionService", "/RWASupportSessionService",
                               QDBusConnection::sessionBus(), this->parent());

    qDebug("Initialized DBus object!");
}

void Session::start_request_dbus() {
    qDebug() << "Requesting D-Bus session service to start a new session";

    // Make an asynchrous 'start' call (Response will be sent to 'start_dbus_replied')
    QDBusPendingCall async = _dbus_rwa->asyncCall("start");
    QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(async, this);

    QObject::connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
                     this, SLOT(start_dbus_replied(QDBusPendingCallWatcher*)));

    // 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<QString> 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();

    qDebug() << "Raw JSON from starting session is:" << result;
    QJsonDocument doc = QJsonDocument::fromJson(result.toUtf8());

    // 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");
        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 <service_id> out of dbus answer!");
        init_vars();
        return;
    }

    // URL of remote web app frontend
    QString url = mainMap["url"].toString();
    this->setURL(url);

    // 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();
        return;
    }

    // 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();
        return;
    }

    qDebug() << "Got service_id:" << service_id <<
                "\nGot 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 <QString> id to <long long>");
        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*)));

    // Clear current variables
    this->init_vars();

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

void Session::stop_dbus_replied(QDBusPendingCallWatcher *call) {
    QString result = "";

    QDBusPendingReply<QString> 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();

    // Get the QJsonObject
    QJsonDocument doc = QJsonDocument::fromJson(result.toUtf8());
    QJsonObject jObject = doc.object();
    QVariantMap mainMap = jObject.toVariantMap();

    QString new_status = mainMap["status"].toString();
    qDebug() << "stop_dbus_replied(): Refreshed status:" << new_status;

    // Clear current variables
    this->init_vars();

    // But the status indicator should display that the Session has stopped
    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 <QString> id to <long long>");
        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::refresh_status_request_dbus(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 <QString> id to <long long>");
        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", id);
    QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(async, this);

    QObject::connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
                     this, SLOT(status_dbus_replied(QDBusPendingCallWatcher*)));
}

void Session::status_dbus_replied(QDBusPendingCallWatcher *call) {
    QString result = "";

    QDBusPendingReply<QString> 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();
    QVariantMap mainMap = jObject.toVariantMap();
    QString new_status = mainMap["status"].toString();
    qDebug() << "status_dbus_replied(): Refreshed status:" << new_status;

    // Enable (dis)connect button
    _main_gui->setConnectButtonEnabled(true);

    this->setStatus(new_status);

    if (this->isSessionAliveOrRunning(new_status)) {
        // Ask status every 1000 millisecond
        QTimer::singleShot(1000, this, &Session::statusTimerEvent);
    }
}

void Session::onCloseHandler() {
    // To cleanup things here
    this->stop_request_dbus(this->getId());
}