when local sync folder is overriden, respect this choice

may lead to inadvertant data loss, not sure if that could be an issue

Signed-off-by: Matthieu Gallien <matthieu.gallien@nextcloud.com>
This commit is contained in:
Matthieu Gallien 2023-03-24 12:29:41 +01:00 коммит произвёл Matthieu Gallien
Родитель a3fbbda7f5
Коммит 20422f0247
5 изменённых файлов: 26 добавлений и 15 удалений

Просмотреть файл

@ -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;
@ -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;
}

Просмотреть файл

@ -74,6 +74,11 @@ public:
ErrorNonEmptyFolder
};
enum class GoodPathStrategy {
AllowOnlyNewPath,
AllowOverrideExistingPath,
};
~FolderMan() override;
static FolderMan *instance();
@ -158,7 +163,7 @@ public:
* 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,

Просмотреть файл

@ -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."));

Просмотреть файл

@ -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

Просмотреть файл

@ -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"));
}
};