diff --git a/src/gui/folderman.cpp b/src/gui/folderman.cpp index b8cecf7d297a0..677be03965914 100644 --- a/src/gui/folderman.cpp +++ b/src/gui/folderman.cpp @@ -1751,7 +1751,7 @@ QPair FolderMan::checkPathValidityForNew return result; } -QString FolderMan::findGoodPathForNewSyncFolder(const QString &basePath, const QUrl &serverUrl) const +QString FolderMan::findGoodPathForNewSyncFolder(const QString &basePath, const QUrl &serverUrl, GoodPathStrategy allowExisting) const { QString folder = basePath; @@ -1768,9 +1768,8 @@ QString FolderMan::findGoodPathForNewSyncFolder(const QString &basePath, const Q int attempt = 1; forever { - const bool isGood = - !QFileInfo(folder).exists() - && FolderMan::instance()->checkPathValidityForNewFolder(folder, serverUrl).second.isEmpty(); + const auto isGood = FolderMan::instance()->checkPathValidityForNewFolder(folder, serverUrl).second.isEmpty() && + (allowExisting == GoodPathStrategy::AllowOverrideExistingPath || !QFileInfo::exists(folder)); if (isGood) { break; } diff --git a/src/gui/folderman.h b/src/gui/folderman.h index a384bd6434e15..21e26035700f2 100644 --- a/src/gui/folderman.h +++ b/src/gui/folderman.h @@ -74,6 +74,11 @@ class FolderMan : public QObject ErrorNonEmptyFolder }; + enum class GoodPathStrategy { + AllowOnlyNewPath, + AllowOverrideExistingPath, + }; + ~FolderMan() override; static FolderMan *instance(); @@ -158,7 +163,7 @@ class FolderMan : public QObject * subfolder of ~ would be a good candidate. When that happens \a basePath * is returned. */ - [[nodiscard]] QString findGoodPathForNewSyncFolder(const QString &basePath, const QUrl &serverUrl) const; + [[nodiscard]] QString findGoodPathForNewSyncFolder(const QString &basePath, const QUrl &serverUrl, GoodPathStrategy allowExisting) const; /** * While ignoring hidden files can theoretically be switched per folder, diff --git a/src/gui/folderwizard.cpp b/src/gui/folderwizard.cpp index 99d147f627446..1c82938cb1bbd 100644 --- a/src/gui/folderwizard.cpp +++ b/src/gui/folderwizard.cpp @@ -71,7 +71,7 @@ FolderWizardLocalPath::FolderWizardLocalPath(const AccountPtr &account) QUrl serverUrl = _account->url(); serverUrl.setUserName(_account->credentials()->user()); QString defaultPath = QDir::homePath() + QLatin1Char('/') + Theme::instance()->appName(); - defaultPath = FolderMan::instance()->findGoodPathForNewSyncFolder(defaultPath, serverUrl); + defaultPath = FolderMan::instance()->findGoodPathForNewSyncFolder(defaultPath, serverUrl, FolderMan::GoodPathStrategy::AllowOnlyNewPath); _ui.localFolderLineEdit->setText(QDir::toNativeSeparators(defaultPath)); _ui.localFolderLineEdit->setToolTip(tr("Enter the path to the local folder.")); diff --git a/src/gui/wizard/owncloudadvancedsetuppage.cpp b/src/gui/wizard/owncloudadvancedsetuppage.cpp index 2dbf427ec906b..a4fa1105b4b29 100644 --- a/src/gui/wizard/owncloudadvancedsetuppage.cpp +++ b/src/gui/wizard/owncloudadvancedsetuppage.cpp @@ -155,7 +155,14 @@ void OwncloudAdvancedSetupPage::initializePage() _ui.lSyncEverythingSizeLabel->clear(); // Update the local folder - this is not guaranteed to find a good one - QString goodLocalFolder = FolderMan::instance()->findGoodPathForNewSyncFolder(localFolder(), serverUrl()); + ConfigFile cfg; + const auto overrideLocalDir = !cfg.overrideLocalDir().isEmpty(); + + auto goodLocalFolder = FolderMan::instance()->findGoodPathForNewSyncFolder(localFolder(), serverUrl(), FolderMan::GoodPathStrategy::AllowOnlyNewPath); + if (overrideLocalDir) { + ConfigFile cfg; + goodLocalFolder = FolderMan::instance()->findGoodPathForNewSyncFolder(cfg.overrideLocalDir(), serverUrl(), FolderMan::GoodPathStrategy::AllowOverrideExistingPath); + } wizard()->setProperty("localFolder", goodLocalFolder); // call to init label diff --git a/test/testfolderman.cpp b/test/testfolderman.cpp index 75101045cd0e3..e81f2bfb659fd 100644 --- a/test/testfolderman.cpp +++ b/test/testfolderman.cpp @@ -404,25 +404,25 @@ private slots: // TEST - QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/oc", url), + QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/oc", url, FolderMan::GoodPathStrategy::AllowOnlyNewPath), QString(dirPath + "/oc")); - QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/ownCloud", url), + QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/ownCloud", url, FolderMan::GoodPathStrategy::AllowOnlyNewPath), QString(dirPath + "/ownCloud3")); - QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/ownCloud2", url), + QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/ownCloud2", url, FolderMan::GoodPathStrategy::AllowOnlyNewPath), QString(dirPath + "/ownCloud22")); - QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/ownCloud2/foo", url), + QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/ownCloud2/foo", url, FolderMan::GoodPathStrategy::AllowOnlyNewPath), QString(dirPath + "/ownCloud2/foo")); - QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/ownCloud2/bar", url), + QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/ownCloud2/bar", url, FolderMan::GoodPathStrategy::AllowOnlyNewPath), QString(dirPath + "/ownCloud2/bar")); - QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/sub", url), + QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/sub", url, FolderMan::GoodPathStrategy::AllowOnlyNewPath), QString(dirPath + "/sub2")); // REMOVE ownCloud2 from the filesystem, but keep a folder sync'ed to it. // We should still not suggest this folder as a new folder. QDir(dirPath + "/ownCloud2/").removeRecursively(); - QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/ownCloud", url), + QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/ownCloud", url, FolderMan::GoodPathStrategy::AllowOnlyNewPath), QString(dirPath + "/ownCloud3")); - QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/ownCloud2", url), + QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/ownCloud2", url, FolderMan::GoodPathStrategy::AllowOnlyNewPath), QString(dirPath + "/ownCloud22")); } };