configurator/Configurator.cpp

51 lines
1.5 KiB
C++
Raw Normal View History

#include "Configurator.h"
2024-09-03 13:40:08 +00:00
#include <dialogs/ServicesDialog.h>
#include <iostream>
#include <QJsonDocument>
#include <QJsonObject>
Configurator::Configurator(int &argc, char **&argv):QApplication(argc, argv),_nam(this),_settings("federated.computer", "configurator", this) {
setQuitOnLastWindowClosed(false);
_loginDialog = new FederatedLogin(0);
_loginDialog->setModal(true);
connect(_loginDialog, &QDialog::accepted, this, &Configurator::loginRequested);
connect(_loginDialog, &QDialog::rejected, this, &QApplication::quit);
_loginDialog->show();
}
void Configurator::loginRequested() {
2024-09-03 13:40:08 +00:00
QByteArray servicesJson = download("https://dashboard." + _loginDialog->domain() + "/apps");
if(servicesJson.isEmpty()) {
_loginDialog->setMessage(tr("Failed to retrieve app list. Is the domain name correct?"));
_loginDialog->show();
return;
}
2024-09-03 13:40:08 +00:00
QJsonDocument services = QJsonDocument::fromJson(servicesJson);
if(!services.isArray()) {
_loginDialog->setMessage(tr("App list did not return an array. Is the domain name correct?"));
_loginDialog->show();
return;
}
_services = services.array();
2024-09-03 13:40:08 +00:00
ServicesDialog *d = new ServicesDialog(0);
d->show();
}
QByteArray Configurator::download(QUrl const &url) {
QNetworkRequest req(url);
QNetworkReply *r = _nam.get(req);
waitForDownload(r);
QByteArray d = r->readAll();
delete r;
return d;
}
void Configurator::waitForDownload(QNetworkReply *r) const {
if(!r->isFinished()) {
QEventLoop loop;
connect(r, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
}
}