Skip to content

Commit

Permalink
Merge pull request #7160 from nextcloud/feature/hard-coded-servers
Browse files Browse the repository at this point in the history
Add ability to provide multiple hardcoded server URLs via CMake define
  • Loading branch information
mgallien authored Sep 24, 2024
2 parents 3edc209 + c16b620 commit 432369a
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 11 deletions.
2 changes: 1 addition & 1 deletion config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#cmakedefine APPLICATION_HELP_URL "@APPLICATION_HELP_URL@"
#cmakedefine APPLICATION_ICON_NAME "@APPLICATION_ICON_NAME@"
#cmakedefine APPLICATION_ICON_SET "@APPLICATION_ICON_SET@"
#cmakedefine APPLICATION_SERVER_URL "@APPLICATION_SERVER_URL@"
#cmakedefine APPLICATION_SERVER_URL R"(@APPLICATION_SERVER_URL@)"
#cmakedefine APPLICATION_SERVER_URL_ENFORCE "@APPLICATION_SERVER_URL_ENFORCE@"
#cmakedefine LINUX_APPLICATION_ID "@LINUX_APPLICATION_ID@"
#cmakedefine APPLICATION_WIZARD_HEADER_BACKGROUND_COLOR "@APPLICATION_WIZARD_HEADER_BACKGROUND_COLOR@"
Expand Down
3 changes: 2 additions & 1 deletion src/gui/accountmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,8 @@ AccountPtr AccountManager::loadAccountHelper(QSettings &settings)

const auto overrideUrl = Theme::instance()->overrideServerUrl();
const auto forceAuth = Theme::instance()->forceConfigAuthType();
if (!forceAuth.isEmpty() && !overrideUrl.isEmpty()) {
const auto multipleOverrideServers = Theme::instance()->multipleOverrideServers();
if (!forceAuth.isEmpty() && !overrideUrl.isEmpty() && !multipleOverrideServers) {
// If forceAuth is set, this might also mean the overrideURL has changed.
// See enterprise issues #1126
acc->setUrl(overrideUrl);
Expand Down
4 changes: 3 additions & 1 deletion src/gui/owncloudsetupwizard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ void OwncloudSetupWizard::startWizard()
{
AccountPtr account = AccountManager::createAccount();
account->setCredentials(CredentialsFactory::create("dummy"));
account->setUrl(Theme::instance()->overrideServerUrl());
const auto defaultUrl =
Theme::instance()->multipleOverrideServers() ? QString{} : Theme::instance()->overrideServerUrl();
account->setUrl(defaultUrl);
_ocWizard->setAccount(account);
_ocWizard->setOCUrl(account->url().toString());

Expand Down
3 changes: 3 additions & 0 deletions src/gui/wizard/owncloudsetupnocredspage.ui
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@
</item>
</layout>
</item>
<item>
<widget class="QComboBox" name="comboBox"/>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_5">
<property name="topMargin">
Expand Down
38 changes: 30 additions & 8 deletions src/gui/wizard/owncloudsetuppage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include <QPropertyAnimation>
#include <QGraphicsPixmapItem>
#include <QBuffer>
#include <QJsonArray>
#include <QJsonDocument>

#include "QProgressIndicator.h"

Expand All @@ -47,15 +49,30 @@ OwncloudSetupPage::OwncloudSetupPage(QWidget *parent)

setupServerAddressDescriptionLabel();

Theme *theme = Theme::instance();
const auto theme = Theme::instance();
if (theme->overrideServerUrl().isEmpty()) {
_ui.comboBox->hide();
_ui.leUrl->setPostfix(theme->wizardUrlPostfix());
_ui.leUrl->setPlaceholderText(theme->wizardUrlHint());
} else if (Theme::instance()->forceOverrideServerUrl()) {
} else if (theme->multipleOverrideServers() && theme->forceOverrideServerUrl()) {
_ui.leUrl->hide();
const auto overrideJsonUtf8 = theme->overrideServerUrl().toUtf8();
const auto serversJsonArray = QJsonDocument::fromJson(overrideJsonUtf8).array();

for (const auto &serverJson : serversJsonArray) {
const auto serverObject = serverJson.toObject();
const auto serverName = serverObject.value("name").toString();
const auto serverUrl = serverObject.value("url").toString();
const auto serverDisplayString = QString("%1 (%2)").arg(serverName, serverUrl);
_ui.comboBox->addItem(serverDisplayString, serverUrl);
}
} else if (theme->forceOverrideServerUrl()) {
_ui.comboBox->hide();
_ui.leUrl->setEnabled(false);
} else {
_ui.comboBox->hide();
}


registerField(QLatin1String("OCUrl*"), _ui.leUrl);

auto sizePolicy = _progressIndi->sizePolicy();
Expand Down Expand Up @@ -165,7 +182,7 @@ void OwncloudSetupPage::slotUrlEditFinished()

bool OwncloudSetupPage::isComplete() const
{
return !_ui.leUrl->text().isEmpty() && !_checking;
return (!_ui.leUrl->text().isEmpty() || !_ui.comboBox->currentData().toString().isEmpty()) && !_checking;
}

void OwncloudSetupPage::initializePage()
Expand All @@ -192,7 +209,7 @@ void OwncloudSetupPage::initializePage()
if (nextButton) {
nextButton->setFocus();
}
} else if (isServerUrlOverridden) {
} else if (isServerUrlOverridden && !Theme::instance()->multipleOverrideServers()) {
// If the overwritten url is not empty and we force this overwritten url
// we just check the server type and switch to next page
// immediately.
Expand Down Expand Up @@ -226,8 +243,12 @@ int OwncloudSetupPage::nextId() const

QString OwncloudSetupPage::url() const
{
QString url = _ui.leUrl->fullText().simplified();
return url;
const auto theme = Theme::instance();
if (theme->multipleOverrideServers() && theme->forceOverrideServerUrl()) {
return _ui.comboBox->currentData().toString();
} else {
return _ui.leUrl->fullText().simplified();
}
}

bool OwncloudSetupPage::validatePage()
Expand Down Expand Up @@ -270,7 +291,8 @@ void OwncloudSetupPage::setErrorString(const QString &err, bool retryHTTPonly)
_ui.errorLabel->setVisible(false);
} else {
if (retryHTTPonly) {
QUrl url(_ui.leUrl->fullText());
const auto urlString = url();
QUrl url(urlString);
if (url.scheme() == "https") {
// Ask the user how to proceed when connecting to a https:// URL fails.
// It is possible that the server is secured with client-side TLS certificates,
Expand Down
16 changes: 16 additions & 0 deletions src/libsync/theme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include <QSslSocket>
#include <QSvgRenderer>
#include <QPainter>
#include <QJsonDocument>
#include <QJsonArray>

#include "nextcloudtheme.h"

Expand Down Expand Up @@ -383,6 +385,7 @@ Theme::Theme()
#endif
#ifdef APPLICATION_SERVER_URL
_overrideServerUrl = QString::fromLatin1(APPLICATION_SERVER_URL);
updateMultipleOverrideServers();
#endif
}

Expand Down Expand Up @@ -432,6 +435,18 @@ bool Theme::forceOverrideServerUrl() const
return _forceOverrideServerUrl;
}

void Theme::updateMultipleOverrideServers()
{
const auto json = overrideServerUrl().toUtf8();
const auto doc = QJsonDocument::fromJson(json);
_multipleOverrideServers = doc.isArray() && !doc.array().empty();
}

bool Theme::multipleOverrideServers() const
{
return _multipleOverrideServers;
}

bool Theme::isVfsEnabled() const
{
return _isVfsEnabled;
Expand Down Expand Up @@ -954,6 +969,7 @@ void Theme::setOverrideServerUrl(const QString &overrideServerUrl)
{
if (_overrideServerUrl != overrideServerUrl) {
_overrideServerUrl = overrideServerUrl;
updateMultipleOverrideServers();
emit overrideServerUrlChanged();
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/libsync/theme.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,18 @@ class OWNCLOUDSYNC_EXPORT Theme : public QObject

/**
* Setting a value here will pre-define the server url.
* Can be a url OR a JSON array of servers description objects: {"name": "x", "url": "y"}
*
* The respective UI controls will be disabled only if forceOverrideServerUrl() is true
*/
[[nodiscard]] QString overrideServerUrl() const;

/**
* Indicates whether the override server URL is in fact a JSON array of server description
* objects.
*/
[[nodiscard]] bool multipleOverrideServers() const;

/**
* Enforce a pre-defined server url.
*
Expand Down Expand Up @@ -627,6 +634,7 @@ public slots:
Theme(Theme const &);
Theme &operator=(Theme const &);

void updateMultipleOverrideServers();
void connectToPaletteSignal();
#if defined(Q_OS_WIN)
QPalette reserveDarkPalette; // Windows 11 button and window dark colours
Expand All @@ -638,6 +646,7 @@ public slots:

QString _overrideServerUrl;
bool _forceOverrideServerUrl = false;
bool _multipleOverrideServers = false;
bool _isVfsEnabled = false;
bool _startLoginFlowAutomatically = false;

Expand Down

0 comments on commit 432369a

Please sign in to comment.