diff --git a/dialogs/ServicesDialog.cpp b/dialogs/ServicesDialog.cpp index d6a2669..260f58c 100644 --- a/dialogs/ServicesDialog.cpp +++ b/dialogs/ServicesDialog.cpp @@ -5,8 +5,10 @@ #include #include +#include #include #include +#include #include @@ -59,16 +61,56 @@ void ServicesDialog::configureService() { if(!w) return; - QWebEngineView *v = new QWebEngineView(static_cast(nullptr)); - if(w->service().title == "Panel") { - QWebEngineHttpRequest req = QWebEngineHttpRequest::postRequest(w->service().url, { + 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(nullptr)); + QWebEngineHttpRequest req = QWebEngineHttpRequest::postRequest(s.url, { {"user_id", app->user()}, {"password", app->password()} }); v->load(req); - } else - v->load(w->service().url); - v->show(); + v->show(); + } else { + QWebEngineView *v = new QWebEngineView(static_cast(nullptr)); + v->load(s.url); + v->show(); + } } void ServicesDialog::serviceHelp() {