Allow specifying the server force override URL and local sync folder path via command...
authoralex-z <blackslayer4@gmail.com>
Fri, 13 Jan 2023 16:55:35 +0000 (17:55 +0100)
committeralex-z <blackslayer4@gmail.com>
Tue, 17 Jan 2023 16:27:19 +0000 (17:27 +0100)
Signed-off-by: alex-z <blackslayer4@gmail.com>
src/gui/accountmanager.cpp
src/gui/accountmanager.h
src/gui/application.cpp
src/gui/owncloudsetupwizard.cpp
src/gui/wizard/owncloudadvancedsetuppage.cpp
src/libsync/configfile.cpp
src/libsync/configfile.h
src/libsync/theme.cpp
src/libsync/theme.h

index 00098bc8669ac15a2a71e5b692c9dc96efbeb179..41ad72efe1ccb27c35b8228af8ec999225c76a61 100644 (file)
@@ -69,7 +69,7 @@ AccountManager *AccountManager::instance()
     return &instance;
 }
 
-bool AccountManager::restore()
+bool AccountManager::restore(bool alsoRestoreLegacySettings)
 {
     QStringList skipSettingsKeys;
     backwardMigrationSettingsKeys(&skipSettingsKeys, &skipSettingsKeys);
@@ -88,8 +88,7 @@ bool AccountManager::restore()
     }
 
     // If there are no accounts, check the old format.
-    if (settings->childGroups().isEmpty()
-        && !settings->contains(QLatin1String(versionC))) {
+    if (settings->childGroups().isEmpty() && !settings->contains(QLatin1String(versionC)) && alsoRestoreLegacySettings) {
         restoreFromLegacySettings();
         return true;
     }
index 574197af91ab7100257e5ec6d077eb33a9e2b77d..c1c56c7cc1e5db894bb7ee5e4dadeeeddbda1731 100644 (file)
@@ -41,7 +41,7 @@ public:
      * Returns false if there was an error reading the settings,
      * but note that settings not existing is not an error.
      */
-    bool restore();
+    bool restore(bool alsoRestoreLegacySettings = true);
 
     /**
      * Add this account in the list of saved accounts.
index 8af99a7dfc6afab85bbe2a4f6a3f9ef5f1fd37cc..3a262a305b6712991cb23af8cf29ce2227eb86b5 100644 (file)
@@ -87,6 +87,8 @@ namespace {
         "  --logdebug           : also output debug-level messages in the log.\n"
         "  --confdir <dirname>  : Use the given configuration folder.\n"
         "  --background         : launch the application in the background.\n"
+        "  --overrideserverurl  : specify a server URL to use for the force override to be used in the account setup wizard.\n"
+        "  --overridelocaldir   : specify a local dir to be used in the account setup wizard.\n"
         "  --userid             : userId (username as on the server) to pass when creating an account via command-line.\n"
         "  --apppassword        : appPassword to pass when creating an account via command-line.\n"
         "  --localdirpath       : (optional) path where to create a local sync folder when creating an account via command-line.\n"
@@ -335,12 +337,12 @@ Application::Application(int &argc, char **argv)
 
     connect(this, &SharedTools::QtSingleApplication::messageReceived, this, &Application::slotParseMessage);
 
-    if (!AccountManager::instance()->restore()) {
+    if (!AccountManager::instance()->restore(cfg.overrideServerUrl().isEmpty())) {
         // If there is an error reading the account settings, try again
         // after a couple of seconds, if that fails, give up.
         // (non-existence is not an error)
         Utility::sleep(5);
-        if (!AccountManager::instance()->restore()) {
+        if (!AccountManager::instance()->restore(cfg.overrideServerUrl().isEmpty())) {
             qCCritical(lcApplication) << "Could not read the account settings, quitting";
             QMessageBox::critical(
                 nullptr,
@@ -620,6 +622,8 @@ void Application::parseOptions(const QStringList &options)
     if (it.hasNext())
         it.next();
 
+    bool shouldExit = false;
+
     //parse options; if help or bad option exit
     while (it.hasNext()) {
         QString option = it.next();
@@ -680,6 +684,26 @@ void Application::parseOptions(const QStringList &options)
                 qCInfo(lcApplication) << errorParsingLocalFileEditingUrl;
                 showHint(errorParsingLocalFileEditingUrl.toStdString());
             }
+        } else if (option == QStringLiteral("--overrideserverurl")) {
+            if (it.hasNext() && !it.peekNext().startsWith(QLatin1String("--"))) {
+                const auto overrideUrl = it.next();
+                const auto isUrlValid = (overrideUrl.startsWith(QStringLiteral("http://")) || overrideUrl.startsWith(QStringLiteral("https://")))
+                    && QUrl::fromUserInput(overrideUrl).isValid();
+                if (!isUrlValid) {
+                    showHint("Invalid URL passed to --overrideserverurl");
+                } else {
+                    ConfigFile().setOverrideServerUrl(overrideUrl);
+                    shouldExit = true;
+                }
+            } else {
+                showHint("Invalid URL passed to --overrideserverurl");
+            }
+        } else if (option == QStringLiteral("--overridelocaldir")) {
+            if (it.hasNext() && !it.peekNext().startsWith(QLatin1String("--"))) {
+                ConfigFile().setOverrideLocalDir(it.next());
+            } else {
+                showHint("Invalid URL passed to --overridelocaldir");
+            }
         }
         else {
             QString errorMessage;
@@ -692,6 +716,9 @@ void Application::parseOptions(const QStringList &options)
             }
         }
     }
+    if (shouldExit) {
+        std::exit(0);
+    }
 }
 
 // Helpers for displaying messages. Note that there is no console on Windows.
index 0ce78f58e3d583f48ded301a0fdb99efbbb00ca1..27f7d6c9f046bb003d4279bb6cc99787e2b88257 100644 (file)
@@ -69,6 +69,12 @@ static QPointer<OwncloudSetupWizard> wiz = nullptr;
 
 void OwncloudSetupWizard::runWizard(QObject *obj, const char *amember, QWidget *parent)
 {
+    ConfigFile cfg;
+    if (!cfg.overrideServerUrl().isEmpty()) {
+        Theme::instance()->setOverrideServerUrl(cfg.overrideServerUrl());
+        Theme::instance()->setForceOverrideServerUrl(true);
+        Theme::instance()->setStartLoginFlowAutomatically(true);
+    }
     if (!wiz.isNull()) {
         bringWizardToFrontIfVisible();
         return;
@@ -109,6 +115,12 @@ void OwncloudSetupWizard::startWizard()
     }
 
     _ocWizard->setProperty("localFolder", localFolder);
+    {
+        ConfigFile cfg;
+        if (!cfg.overrideLocalDir().isEmpty()) {
+            _ocWizard->setProperty("localFolder", cfg.overrideLocalDir());
+        }
+    }
 
     // remember the local folder to compare later if it changed, but clean first
     QString lf = QDir::fromNativeSeparators(localFolder);
@@ -120,8 +132,11 @@ void OwncloudSetupWizard::startWizard()
 
     _ocWizard->setRemoteFolder(_remoteFolder);
 
+    const auto isEnforcedServerSetup =
+        Theme::instance()->startLoginFlowAutomatically() && Theme::instance()->forceOverrideServerUrl() && !account->url().isEmpty();
+
 #ifdef WITH_PROVIDERS
-    const auto startPage = WizardCommon::Page_Welcome;
+    const auto startPage = isEnforcedServerSetup ? WizardCommon::Page_ServerSetup : WizardCommon::Page_Welcome;
 #else // WITH_PROVIDERS
     const auto startPage = WizardCommon::Page_ServerSetup;
 #endif // WITH_PROVIDERS
index 668a0171a6258213c248a16aa4e6e138ebefbb9b..7e49980ed24a9d66758797638ebef911c053720b 100644 (file)
@@ -193,6 +193,16 @@ void OwncloudAdvancedSetupPage::initializePage()
     if (nextButton) {
         nextButton->setDefault(true);
     }
+    if (Theme::instance()->forceOverrideServerUrl()) {
+        QTimer::singleShot(0, this, [this]() {
+            connect(_ocWizard, &QDialog::accepted, []() {
+                ConfigFile cfg;
+                cfg.setOverrideServerUrl({});
+                cfg.setOverrideLocalDir({});
+            });
+            _ocWizard->accept();
+        });
+    }
 }
 
 void OwncloudAdvancedSetupPage::fetchUserAvatar()
index a7293343184021b15cfb1449e7145b2a33c41ebd..08064026f405875327bb65ffff8e0a29d1d7a660 100644 (file)
@@ -67,6 +67,8 @@ static constexpr char autoUpdateCheckC[] = "autoUpdateCheck";
 static constexpr char updateCheckIntervalC[] = "updateCheckInterval";
 static constexpr char updateSegmentC[] = "updateSegment";
 static constexpr char updateChannelC[] = "updateChannel";
+static constexpr char overrideServerUrlC[] = "overrideServerUrl";
+static constexpr char overrideLocalDirC[] = "overrideLocalDir";
 static constexpr char geometryC[] = "geometry";
 static constexpr char timeoutC[] = "timeout";
 static constexpr char chunkSizeC[] = "chunkSize";
@@ -688,6 +690,30 @@ void ConfigFile::setUpdateChannel(const QString &channel)
     settings.setValue(QLatin1String(updateChannelC), channel);
 }
 
+[[nodiscard]] QString ConfigFile::overrideServerUrl() const
+{
+    QSettings settings(configFile(), QSettings::IniFormat);
+    return settings.value(QLatin1String(overrideServerUrlC), {}).toString();
+}
+
+void ConfigFile::setOverrideServerUrl(const QString &url)
+{
+    QSettings settings(configFile(), QSettings::IniFormat);
+    settings.setValue(QLatin1String(overrideServerUrlC), url);
+}
+
+[[nodiscard]] QString ConfigFile::overrideLocalDir() const
+{
+    QSettings settings(configFile(), QSettings::IniFormat);
+    return settings.value(QLatin1String(overrideLocalDirC), {}).toString();
+}
+
+void ConfigFile::setOverrideLocalDir(const QString &localDir)
+{
+    QSettings settings(configFile(), QSettings::IniFormat);
+    settings.setValue(QLatin1String(overrideLocalDirC), localDir);
+}
+
 void ConfigFile::setProxyType(int proxyType,
     const QString &host,
     int port, bool needsAuth,
index e731f3016eed724a7d5031db33fc94eba9b1e650..719cce0835ada0e981e845bac75092ee1cd605e6 100644 (file)
@@ -186,6 +186,12 @@ public:
     [[nodiscard]] QString updateChannel() const;
     void setUpdateChannel(const QString &channel);
 
+    [[nodiscard]] QString overrideServerUrl() const;
+    void setOverrideServerUrl(const QString &url);
+
+    [[nodiscard]] QString overrideLocalDir() const;
+    void setOverrideLocalDir(const QString &localDir);
+
     void saveGeometryHeader(QHeaderView *header);
     void restoreGeometryHeader(QHeaderView *header);
 
index a28558b493273b83df0d2d8972bd97719d830530..caee23bc361e6050b51bc77bd2a6514626c1d155 100644 (file)
@@ -371,6 +371,13 @@ Theme::Theme()
     reserveDarkPalette.setColor(QPalette::Disabled, QPalette::HighlightedText,
                                 QColor(127, 127, 127));
 #endif
+
+#ifdef APPLICATION_SERVER_URL_ENFORCE
+    _forceOverrideServerUrl = true;
+#endif
+#ifdef APPLICATION_SERVER_URL
+    _overrideServerUrl = QString::fromLatin1(APPLICATION_SERVER_URL);
+#endif
 }
 
 // If this option returns true, the client only supports one folder to sync.
@@ -411,20 +418,17 @@ QString Theme::conflictHelpUrl() const
 
 QString Theme::overrideServerUrl() const
 {
-#ifdef APPLICATION_SERVER_URL
-    return QString::fromLatin1(APPLICATION_SERVER_URL);
-#else
-    return QString();
-#endif
+    return _overrideServerUrl;
 }
 
 bool Theme::forceOverrideServerUrl() const
 {
-#ifdef APPLICATION_SERVER_URL_ENFORCE
-    return true;
-#else
-    return false;
-#endif
+    return _forceOverrideServerUrl;
+}
+
+bool Theme::startLoginFlowAutomatically() const
+{
+    return _startLoginFlowAutomatically;
 }
 
 bool Theme::enableStaplingOCSP() const
@@ -952,4 +956,27 @@ bool Theme::darkMode()
 #endif
 }
 
+void Theme::setOverrideServerUrl(const QString &overrideServerUrl)
+{
+    if (_overrideServerUrl != overrideServerUrl) {
+        _overrideServerUrl = overrideServerUrl;
+        emit overrideServerUrlChanged();
+    }
+}
+void Theme::setForceOverrideServerUrl(bool forceOverride)
+{
+    if (_forceOverrideServerUrl != forceOverride) {
+        _forceOverrideServerUrl = forceOverride;
+        emit forceOverrideServerUrlChanged();
+    }
+}
+
+void Theme::setStartLoginFlowAutomatically(bool startLoginFlowAuto)
+{
+    if (_startLoginFlowAutomatically != startLoginFlowAuto) {
+        _startLoginFlowAutomatically = startLoginFlowAuto;
+        emit startLoginFlowAutomaticallyChanged();
+    }
+}
+
 } // end namespace client
index db86b282b1d1057be5c5ea36762908ac33c5efcd..de721f71c273a5e8f94f84e6fa5a383e8226d663 100644 (file)
@@ -55,8 +55,9 @@ class OWNCLOUDSYNC_EXPORT Theme : public QObject
     Q_PROPERTY(QString version READ version CONSTANT)
     Q_PROPERTY(QString helpUrl READ helpUrl CONSTANT)
     Q_PROPERTY(QString conflictHelpUrl READ conflictHelpUrl CONSTANT)
-    Q_PROPERTY(QString overrideServerUrl READ overrideServerUrl CONSTANT)
-    Q_PROPERTY(bool forceOverrideServerUrl READ forceOverrideServerUrl CONSTANT)
+    Q_PROPERTY(QString overrideServerUrl READ overrideServerUrl WRITE setOverrideServerUrl NOTIFY overrideServerUrlChanged)
+    Q_PROPERTY(bool forceOverrideServerUrl READ forceOverrideServerUrl WRITE setForceOverrideServerUrl NOTIFY forceOverrideServerUrlChanged)
+    Q_PROPERTY(bool startLoginFlowAutomatically READ startLoginFlowAutomatically WRITE setStartLoginFlowAutomatically NOTIFY startLoginFlowAutomaticallyChanged)
 #ifndef TOKEN_AUTH_ONLY
     Q_PROPERTY(QColor wizardHeaderTitleColor READ wizardHeaderTitleColor CONSTANT)
     Q_PROPERTY(QColor wizardHeaderBackgroundColor READ wizardHeaderBackgroundColor CONSTANT)
@@ -241,6 +242,13 @@ public:
      * When true, the respective UI controls will be disabled
      */
     virtual bool forceOverrideServerUrl() const;
+
+    /**
+     * Automatically start login flow
+     *
+     * When true, the browser will get opened automatically
+     */
+    virtual bool startLoginFlowAutomatically() const;
     
     /**
      * Enable OCSP stapling for SSL handshakes
@@ -584,6 +592,11 @@ public:
     QPalette systemPalette();
     bool darkMode();
 
+public slots:
+    virtual void setOverrideServerUrl(const QString &overrideServerUrl);
+    virtual void setForceOverrideServerUrl(bool forceOverride);
+    virtual void setStartLoginFlowAutomatically(bool startLoginFlowAuto);
+
 protected:
 #ifndef TOKEN_AUTH_ONLY
     QIcon themeIcon(const QString &name, bool sysTray = false) const;
@@ -602,6 +615,9 @@ signals:
     void systrayUseMonoIconsChanged(bool);
     void systemPaletteChanged(const QPalette &palette);
     void darkModeChanged();
+    void overrideServerUrlChanged();
+    void forceOverrideServerUrlChanged();
+    void startLoginFlowAutomaticallyChanged();
 
 private:
     Theme(Theme const &);
@@ -616,6 +632,10 @@ private:
     bool _mono = false;
     bool _paletteSignalsConnected = false;
 
+    QString _overrideServerUrl;
+    bool _forceOverrideServerUrl = false;
+    bool _startLoginFlowAutomatically = false;
+
 #ifndef TOKEN_AUTH_ONLY
     mutable QHash<QString, QIcon> _iconCache;
 #endif