configurator/dialogs/ServicesDialog.cpp

138 lines
4.8 KiB
C++
Raw Permalink Normal View History

2024-09-03 13:40:08 +00:00
#include "ServicesDialog.h"
#include "ServiceWidget.h"
#include "Configurator.h"
2024-09-03 16:59:36 +00:00
#include "HelpBrowser.h"
2024-09-03 13:40:08 +00:00
2024-09-03 16:59:36 +00:00
#include <QWebEngineView>
#include <QWebEngineHttpRequest>
2024-09-25 14:56:35 +00:00
#include <QMessageBox>
2024-09-03 16:59:36 +00:00
#include <QUrlQuery>
2024-09-03 13:40:08 +00:00
#include <QScrollBar>
2024-09-25 14:56:35 +00:00
#include <QDir>
2024-09-03 13:40:08 +00:00
#include <iostream>
ServicesDialog::ServicesDialog(QWidget *parent):QDialog(parent),_layout(this) {
int y = 0;
_welcome = new QLabel(tr("Welcome to your Federated Computer\n"
"We can automatically configure these services for you:"), this);
_layout.addWidget(_welcome, y, 0);
_knownServicesScroller = new QScrollArea(this);
_knownServices = new QWidget(this);
QBoxLayout *ksLayout = new QBoxLayout(QBoxLayout::TopToBottom, _knownServices);
_layout.addWidget(_knownServicesScroller, ++y, 0);
_usLabel = new QLabel(tr("Your subscription also includes the following services, which can not currently be auto-configured, but you can configure them manually:"), this);
_layout.addWidget(_usLabel, ++y, 0);
_unknownServicesScroller = new QScrollArea(this);
_unknownServices = new QWidget(this);
QBoxLayout *usLayout = new QBoxLayout(QBoxLayout::TopToBottom, _unknownServices);
_layout.addWidget(_unknownServicesScroller, ++y, 0);
int k = 0, u = 0;
for(auto i : app->services()) {
2024-09-03 16:59:36 +00:00
ServiceWidget *w;
2024-09-03 13:40:08 +00:00
if(i.title == "Nextcloud" || i.title == "Panel") {
2024-09-03 16:59:36 +00:00
w=new ServiceWidget(i, _knownServices);
2024-09-03 13:40:08 +00:00
ksLayout->addWidget(w, ++k);
} else {
2024-09-03 16:59:36 +00:00
w=new ServiceWidget(i, _unknownServices);
2024-09-03 13:40:08 +00:00
usLayout->addWidget(w, ++u);
}
2024-09-03 16:59:36 +00:00
connect(w, &ServiceWidget::goClicked, this, &ServicesDialog::configureService);
connect(w, &ServiceWidget::helpClicked, this, &ServicesDialog::serviceHelp);
2024-09-03 13:40:08 +00:00
}
_knownServicesScroller->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
_unknownServicesScroller->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
_knownServicesScroller->setWidget(_knownServices);
_unknownServicesScroller->setWidget(_unknownServices);
_buttons = new ButtonRow(this);
QPushButton *ok = new QPushButton(tr("&OK"), _buttons);
connect(ok, &QPushButton::clicked, this, &QDialog::accept);
ok->setDefault(true);
QPushButton *cancel = new QPushButton(tr("&Cancel"), _buttons);
connect(cancel, &QPushButton::clicked, this, &QDialog::reject);
_buttons->add(ok);
_buttons->add(cancel);
_layout.addWidget(_buttons, ++y, 0);
resizeEvent(0);
}
2024-09-03 16:59:36 +00:00
void ServicesDialog::configureService() {
ServiceWidget *w=qobject_cast<ServiceWidget*>(sender());
if(!w)
return;
2024-09-25 14:56:35 +00:00
Service const &s = w->service();
if(s.title == "Nextcloud") {
QSettings ncSettings(QDir::homePath() + "/.config/Nextcloud/nextcloud.cfg", QSettings::IniFormat, this);
ncSettings.beginGroup("Accounts");
int numAccounts = -1;
int ourAccount = -1;
for(QString &g : ncSettings.childGroups()) {
ncSettings.beginGroup(g);
if(!ncSettings.contains("url")) {
ncSettings.endGroup();
continue;
}
if(QUrl(ncSettings.value("url").toString()) == s.url && ncSettings.value("webflow_user") == app->user()) {
// This user is already configured -- update existing config
// rather than creating a new one
ourAccount = g.toInt();
ncSettings.endGroup();
break;
}
numAccounts = std::max(numAccounts, g.toInt());
ncSettings.endGroup();
}
if(ourAccount >= 0) {
if(QMessageBox::question(this, tr("Nextcloud"), tr("This account seems to be configured in your Nextcloud client already. Do you want to overwrite the existing configuration?")) == QMessageBox::No)
return;
} else {
ourAccount = numAccounts+1;
}
ncSettings.beginGroup(QString::number(ourAccount));
ncSettings.setValue("authType", "webflow");
ncSettings.setValue("dav_user", app->user().section('@', 0, 1));
ncSettings.setValue("displayName", app->user().section('@', 0, 1));
ncSettings.setValue("webflow_user", app->user());
ncSettings.setValue("url", s.url.toString());
ncSettings.setValue("version", 1);
ncSettings.endGroup();
ncSettings.endGroup();
} else if(s.title == "Panel") {
QWebEngineView *v = new QWebEngineView(static_cast<QWidget*>(nullptr));
QWebEngineHttpRequest req = QWebEngineHttpRequest::postRequest(s.url, {
2024-09-03 16:59:36 +00:00
{"user_id", app->user()},
{"password", app->password()}
});
v->load(req);
2024-09-25 14:56:35 +00:00
v->show();
} else {
QWebEngineView *v = new QWebEngineView(static_cast<QWidget*>(nullptr));
v->load(s.url);
v->show();
}
2024-09-03 16:59:36 +00:00
}
void ServicesDialog::serviceHelp() {
ServiceWidget *w=qobject_cast<ServiceWidget*>(sender());
if(!w)
return;
Service const &s = w->service();
HelpBrowser *viewer = new HelpBrowser(s.documentationUrl);
viewer->setWindowIcon(s.pixmap());
viewer->setWindowTitle(QString("%1 help").arg(s.title));
viewer->show();
}
2024-09-03 13:40:08 +00:00
void ServicesDialog::resizeEvent(QResizeEvent *e) {
// Make sure the known services and unknown services
// lists always use the full width available
if(e)
QDialog::resizeEvent(e);
int const w = _knownServicesScroller->width();
_knownServices->setFixedWidth(w);
_unknownServices->setFixedWidth(w);
}