configurator/dialogs/ServicesDialog.cpp
Bernhard Rosenkränzer 4c1025f501 Nextcloud configuration
2024-09-25 16:56:35 +02:00

138 lines
4.8 KiB
C++

#include "ServicesDialog.h"
#include "ServiceWidget.h"
#include "Configurator.h"
#include "HelpBrowser.h"
#include <QWebEngineView>
#include <QWebEngineHttpRequest>
#include <QMessageBox>
#include <QUrlQuery>
#include <QScrollBar>
#include <QDir>
#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()) {
ServiceWidget *w;
if(i.title == "Nextcloud" || i.title == "Panel") {
w=new ServiceWidget(i, _knownServices);
ksLayout->addWidget(w, ++k);
} else {
w=new ServiceWidget(i, _unknownServices);
usLayout->addWidget(w, ++u);
}
connect(w, &ServiceWidget::goClicked, this, &ServicesDialog::configureService);
connect(w, &ServiceWidget::helpClicked, this, &ServicesDialog::serviceHelp);
}
_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);
}
void ServicesDialog::configureService() {
ServiceWidget *w=qobject_cast<ServiceWidget*>(sender());
if(!w)
return;
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, {
{"user_id", app->user()},
{"password", app->password()}
});
v->load(req);
v->show();
} else {
QWebEngineView *v = new QWebEngineView(static_cast<QWidget*>(nullptr));
v->load(s.url);
v->show();
}
}
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();
}
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);
}