51 lines
1.5 KiB
C++
51 lines
1.5 KiB
C++
#include "Configurator.h"
|
|
#include <dialogs/ServicesDialog.h>
|
|
#include <iostream>
|
|
#include <QJsonDocument>
|
|
#include <QJsonObject>
|
|
|
|
Configurator::Configurator(int &argc, char **&argv):QApplication(argc, argv),_nam(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() {
|
|
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;
|
|
}
|
|
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();
|
|
|
|
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();
|
|
}
|
|
}
|