Skip to content

Commit

Permalink
Merge pull request #5546 from nextcloud/bugfix/overrideLocalDirReally…
Browse files Browse the repository at this point in the history
…WithExistingFolder

when local sync folder is overriden, respect this choice
  • Loading branch information
mgallien authored Mar 31, 2023
2 parents a3fbbda + 20422f0 commit a444564
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 15 deletions.
7 changes: 3 additions & 4 deletions src/gui/folderman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1751,7 +1751,7 @@ QPair<FolderMan::PathValidityResult, QString> 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;

Expand All @@ -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;
}
Expand Down
7 changes: 6 additions & 1 deletion src/gui/folderman.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ class FolderMan : public QObject
ErrorNonEmptyFolder
};

enum class GoodPathStrategy {
AllowOnlyNewPath,
AllowOverrideExistingPath,
};

~FolderMan() override;
static FolderMan *instance();

Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/gui/folderwizard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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."));

Expand Down
9 changes: 8 additions & 1 deletion src/gui/wizard/owncloudadvancedsetuppage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions test/testfolderman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
};
Expand Down

0 comments on commit a444564

Please sign in to comment.