aboutsummaryrefslogtreecommitdiff
path: root/src/DBusAPI.cpp
blob: 18c5974144994cf23cc772f5e429b8a4f10fe52c (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
/*
 * This file is part of Remote Support Desktop
 * https://gitlab.das-netzwerkteam.de/RemoteWebApp/rwa.support.desktopapp
 * Copyright 2021 Daniel Teichmann <daniel.teichmann@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 "DBusAPI.h"

/*!
 * \class DBusAPI
 * \brief The DBusAPI class provides all necessary D-Bus methods and distributes the response via signals.
 */
DBusAPI::DBusAPI() {
    _initDBus();
}

/*!
 * \brief Initializes private _dbus_rwa object.
 */
void DBusAPI::_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!");
}

/*!
 * \brief Ask the session service to start a session.
 *
 * \a host RWAHost object which includes all necessary information about a RWA host.
 */
void DBusAPI::start_request(RWAHost *host) {
    Q_ASSERT(host != nullptr);

    qDebug() << "Requesting D-Bus service to start a new session on host:" << host->alias();

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

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

/*!
 * \brief Method gets called when a D-Bus response is ready.
 *
 * \a call contains the D-Bus response (session service or for example maybe just a error message).
 *
 * TODO: Example of json input
 */
void DBusAPI::start_reply(QDBusPendingCallWatcher *call) {
    QString result = "";
    QDBusPendingReply<QString> reply = *call;
    if (reply.isError()) {
        qCritical() << "D-Bus 'start' request failed, this was the reply:";
        qCritical() << reply.error();

        emit serviceStartResponse(nullptr);
        return;
    } else {
        result = reply.argumentAt<0>();
    }
    call->deleteLater();

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

    emit serviceStartResponse(&doc);
}

/*!
 * \brief Ask the session service to stop session #<session_id>.
 *
 * \a host RWAHost object which includes all necessary information about a RWA host.
 *
 * \a session_id Unique identifier for a session in a specific host.
 */
void DBusAPI::stop_request(RWAHost *host, QString session_id) {
    Q_ASSERT(host != nullptr);
    Q_ASSERT(session_id != "");

    bool ok;
    long long session_id_number = session_id.toLongLong(&ok);

    // Sanity Check
    if(ok == false){
        qWarning() << QString("Unable to parse '%0' as long long!").arg(session_id);
        return;
    }

    qDebug().noquote() << QString("Requesting D-Bus service to stop "
                                  "session #'%0' on host '%1'")
                          .arg(session_id)
                          .arg(host->alias());

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

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

/*!
 * \brief Method gets called when a D-Bus response is ready.
 *
 * \a call contains the D-Bus response (session service or for example maybe just a error message).
 *
 * TODO: Example of json input
 */
void DBusAPI::stop_reply(QDBusPendingCallWatcher *call) {
    QString result = "";

    QDBusPendingReply<QString> reply = *call;
    if (reply.isError()) {
        qCritical() << "D-Bus 'stop' request failed, this was the reply:";
        qCritical() << reply.error();

        emit serviceStopResponse(nullptr);
    } else {
        result = reply.argumentAt<0>();
    }
    call->deleteLater();

    qDebug() << "Raw JSON from stopping a session is:" << result.toUtf8().replace('"', "");
    QJsonDocument doc = QJsonDocument::fromJson(result.toUtf8());

    emit serviceStopResponse(&doc);
}

/*!
 * \brief Ask the session service for status of <session_id>.
 *
 * \a host RWAHost object which includes all necessary information about a RWA host.
 *
 * \a session_id Unique identifier for a session in a specific host.
 */
void DBusAPI::status_request(RWAHost *host, QString session_id) {
    Q_ASSERT(host != nullptr);
    Q_ASSERT(session_id != "");

    bool ok;
    long long session_id_number = session_id.toLongLong(&ok);

    // Sanity Check
    if(ok == false){
        qWarning() << QString("Unable to parse '%0' as long long!").arg(session_id);
        return;
    }

    qDebug().noquote() << QString("Requesting D-Bus service for status of session "
                                  "#'%0' on host '%1'").arg(session_id).arg(host->alias());

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

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

/*!
 * \brief Forces the session service to refresh it's status of <session_id> and tell us about it then.
 *
 * \a host RWAHost object which includes all necessary information about a RWA host.
 *
 * \a session_id Unique identifier for a session in a specific host.
 */
void DBusAPI::refresh_status_request(RWAHost *host, QString session_id) {
    Q_ASSERT(host != nullptr);
    Q_ASSERT(session_id != "");

    bool ok;
    long long session_id_number = session_id.toLongLong(&ok);

    // Sanity Check
    if(ok == false){
        qWarning() << QString("Unable to parse '%0' as long long!").arg(session_id);
        return;
    }

    qDebug().noquote() << QString("Requesting D-Bus service for refresh_status of session "
                                  "#'%0' on host '%1'").arg(session_id).arg(host->alias());

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

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

/*!
 * \brief Method gets called when a D-Bus response is ready.
 *
 * \a call contains the D-Bus response (session service or for example maybe just a error message).
 *
 * TODO: Example of json input
 */
void DBusAPI::status_reply(QDBusPendingCallWatcher *call){
    QString result = "";

    QDBusPendingReply<QString> reply = *call;
    if (reply.isError()) {
        qCritical() << "D-Bus '(refresh_)status' request failed, this was the reply:";
        qCritical() << reply.error();

        emit serviceStatusResponse(nullptr);
        return;
    } else {
        result = reply.argumentAt<0>();
    }
    call->deleteLater();

    // Get the QJsonObject
    qDebug() << "Raw JSON from a status request for a session is:" << result.toUtf8().replace('"', "");
    QJsonDocument doc = QJsonDocument::fromJson(result.toUtf8());

    emit serviceStatusResponse(&doc);
}

/*!
 * \brief This method requests the session service to list all RWAHosts.
 */
void DBusAPI::get_web_app_hosts_request() {
    qDebug().noquote() << QString("Requesting D-Bus service to list "
                                  "all remote web app hosts");

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

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

/*!
 * \brief This method requests the session service to add a RWAHost to its configuration files.
 *
 * \a host_url is the remote web app adress which will be used by the session service to coordinate
 * sessions, connections, settings and such.
 */
void DBusAPI::add_web_app_host_request(QString host_url, QString host_alias) {
    Q_ASSERT(host_url != "");
    Q_ASSERT(host_alias != "");

    qDebug().noquote() << QString("Requesting D-Bus service to register new "
                                  "remote web app host '%0' with url '%1'").arg(host_alias).arg(host_url);

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

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

/*!
 * \brief This method requests the session service to remove a RWAHost from its configuration files.
 *
 * \a host_uuid Unique identifier which all hosts have.
 */
void DBusAPI::remove_web_app_host_request(QString host_uuid) {
    Q_ASSERT(host_uuid != "");

    qDebug().noquote() << QString("Requesting D-Bus service to list "
                                  "all remote web app hosts");

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

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

/*!
 * \brief Method gets called when a D-Bus response is ready.
 *
 * \a call contains the D-Bus response (session service or for example maybe just a error message).
 *
 * TODO: Example of json input
 */
void DBusAPI::get_web_app_hosts_reply(QDBusPendingCallWatcher *call){
    QString result = "";

    QDBusPendingReply<QString> reply = *call;
    if (reply.isError()) {
        qCritical() << "D-Bus 'get_web_app_hosts' request failed, this was the reply:";
        qCritical() << reply.error();

        emit serviceGetWebAppHostsResponse(nullptr);
        return;
    } else {
        result = reply.argumentAt<0>();
    }
    call->deleteLater();

    // Get the QJsonObject
    qDebug() << "Raw JSON from the remote web app host listing request:" << result.toUtf8().replace('"', "");
    QJsonDocument doc = QJsonDocument::fromJson(result.toUtf8());

    emit serviceGetWebAppHostsResponse(&doc);
}

/*!
 * \brief Method gets called when a D-Bus response is ready.
 *
 * \a call contains the D-Bus response (session service or for example maybe just a error message).
 *
 * TODO: Example of json input
 */
void DBusAPI::add_web_app_host_reply(QDBusPendingCallWatcher *call){
    QString result = "";

    QDBusPendingReply<QString> reply = *call;
    if (reply.isError()) {
        qCritical() << "D-Bus 'add_web_app_host' request failed, this was the reply:";
        qCritical() << reply.error();

        emit serviceAddWebAppHostResponse(nullptr);
        return;
    } else {
        result = reply.argumentAt<0>();
    }
    call->deleteLater();

    // Get the QJsonObject
    qDebug() << "Raw JSON from the remote web app host register request:" << result.toUtf8().replace('"', "");
    QJsonDocument doc = QJsonDocument::fromJson(result.toUtf8());

    emit serviceAddWebAppHostResponse(&doc);
}

/*!
 * \brief Method gets called when a D-Bus response is ready.
 *
 * \a call contains the D-Bus response (session service or for example maybe just a error message).
 *
 * TODO: Example of json input
 */
void DBusAPI::remove_web_app_host_reply(QDBusPendingCallWatcher *call){
    QString result = "";

    QDBusPendingReply<QString> reply = *call;
    if (reply.isError()) {
        qCritical() << "D-Bus 'remove_web_app_host' request failed, this was the reply:";
        qCritical() << reply.error();

        emit serviceRemoveWebAppHostResponse(nullptr);
        return;
    } else {
        result = reply.argumentAt<0>();
    }
    call->deleteLater();

    // Get the QJsonObject
    qDebug() << "Raw JSON from the remote web app host deletion request:" << result.toUtf8().replace('"', "");
    QJsonDocument doc = QJsonDocument::fromJson(result.toUtf8());

    emit serviceRemoveWebAppHostResponse(&doc);
}