Check folder invalidity reson when deciding to enable or disable the 'Finish' button in a Wizard page.

Signed-off-by: alex-z <blackslayer4@gmail.com>
This commit is contained in:
alex-z 2022-09-08 13:00:13 +03:00 committed by Matthieu Gallien
parent a6867f9343
commit f0b9ecd747
5 changed files with 55 additions and 36 deletions

View file

@ -1659,12 +1659,16 @@ static QString canonicalPath(const QString &path)
return selFile.canonicalFilePath();
}
QString FolderMan::checkPathValidityForNewFolder(const QString &path, const QUrl &serverUrl) const
QPair<FolderMan::PathValidityResult, QString> FolderMan::checkPathValidityForNewFolder(const QString &path, const QUrl &serverUrl) const
{
QPair<FolderMan::PathValidityResult, QString> result;
QString recursiveValidity = checkPathValidityRecursive(path);
if (!recursiveValidity.isEmpty()) {
qCDebug(lcFolderMan) << path << recursiveValidity;
return recursiveValidity;
result.first = FolderMan::ErrorRecursiveValidity;
result.second = recursiveValidity;
return result;
}
// check if the local directory isn't used yet in another ownCloud sync
@ -1680,15 +1684,19 @@ QString FolderMan::checkPathValidityForNewFolder(const QString &path, const QUrl
bool differentPaths = QString::compare(folderDir, userDir, cs) != 0;
if (differentPaths && folderDir.startsWith(userDir, cs)) {
return tr("The local folder %1 already contains a folder used in a folder sync connection. "
"Please pick another one!")
.arg(QDir::toNativeSeparators(path));
result.first = FolderMan::ErrorContainsFolder;
result.second = tr("The local folder %1 already contains a folder used in a folder sync connection. "
"Please pick another one!")
.arg(QDir::toNativeSeparators(path));
return result;
}
if (differentPaths && userDir.startsWith(folderDir, cs)) {
return tr("The local folder %1 is already contained in a folder used in a folder sync connection. "
result.first = FolderMan::ErrorContainedInFolder;
result.second = tr("The local folder %1 is already contained in a folder used in a folder sync connection. "
"Please pick another one!")
.arg(QDir::toNativeSeparators(path));
return result;
}
// if both pathes are equal, the server url needs to be different
@ -1700,13 +1708,15 @@ QString FolderMan::checkPathValidityForNewFolder(const QString &path, const QUrl
folderUrl.setUserName(user);
if (serverUrl == folderUrl) {
return tr("There is already a sync from the server to this local folder. "
result.first = FolderMan::ErrorNonEmptyFolder;
result.second = tr("There is already a sync from the server to this local folder. "
"Please pick another local folder!");
return result;
}
}
}
return QString();
return result;
}
QString FolderMan::findGoodPathForNewSyncFolder(const QString &basePath, const QUrl &serverUrl) const
@ -1728,7 +1738,7 @@ QString FolderMan::findGoodPathForNewSyncFolder(const QString &basePath, const Q
forever {
const bool isGood =
!QFileInfo(folder).exists()
&& FolderMan::instance()->checkPathValidityForNewFolder(folder, serverUrl).isEmpty();
&& FolderMan::instance()->checkPathValidityForNewFolder(folder, serverUrl).second.isEmpty();
if (isGood) {
break;
}

View file

@ -63,6 +63,14 @@ class FolderMan : public QObject
{
Q_OBJECT
public:
enum PathValidityResult {
Valid,
ErrorRecursiveValidity,
ErrorContainsFolder,
ErrorContainedInFolder,
ErrorNonEmptyFolder
};
~FolderMan() override;
static FolderMan *instance();
@ -134,9 +142,9 @@ public:
*
* Note that different accounts are allowed to sync to the same folder.
*
* @returns an empty string if it is allowed, or an error if it is not allowed
* @returns an empty string and PathValidityResult::Valid if it is allowed, or an error if it is not allowed
*/
QString checkPathValidityForNewFolder(const QString &path, const QUrl &serverUrl = QUrl()) const;
QPair<PathValidityResult, QString> checkPathValidityForNewFolder(const QString &path, const QUrl &serverUrl = QUrl()) const;
/**
* Attempts to find a non-existing, acceptable path for creating a new sync folder.

View file

@ -98,8 +98,8 @@ bool FolderWizardLocalPath::isComplete() const
QUrl serverUrl = _account->url();
serverUrl.setUserName(_account->credentials()->user());
QString errorStr = FolderMan::instance()->checkPathValidityForNewFolder(
QDir::fromNativeSeparators(_ui.localFolderLineEdit->text()), serverUrl);
const auto errorStr = FolderMan::instance()->checkPathValidityForNewFolder(
QDir::fromNativeSeparators(_ui.localFolderLineEdit->text()), serverUrl).second;
bool isOk = errorStr.isEmpty();

View file

@ -258,8 +258,9 @@ void OwncloudAdvancedSetupPage::updateStatus()
const QString locFolder = localFolder();
// check if the local folder exists. If so, and if its not empty, show a warning.
QString errorStr = FolderMan::instance()->checkPathValidityForNewFolder(locFolder, serverUrl());
_localFolderValid = errorStr.isEmpty();
const auto pathValidityCheckResult = FolderMan::instance()->checkPathValidityForNewFolder(locFolder, serverUrl());
auto errorStr = pathValidityCheckResult.second;
_localFolderValid = errorStr.isEmpty() || pathValidityCheckResult.first == FolderMan::ErrorNonEmptyFolder;
QString t;

View file

@ -67,15 +67,15 @@ private slots:
// those should be allowed
// QString FolderMan::checkPathValidityForNewFolder(const QString& path, const QUrl &serverUrl, bool forNewDirectory)
QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/sub/free"), QString());
QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/free2/"), QString());
QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/sub/free").second, QString());
QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/free2/").second, QString());
// Not an existing directory -> Ok
QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/sub/bliblablu"), QString());
QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/sub/free/bliblablu"), QString());
QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/sub/bliblablu").second, QString());
QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/sub/free/bliblablu").second, QString());
// QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/sub/bliblablu/some/more"), QString());
// A file -> Error
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/sub/file.txt").isNull());
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/sub/file.txt").second.isNull());
// There are folders configured in those folders, url needs to be taken into account: -> ERROR
QUrl url2(url);
@ -83,18 +83,18 @@ private slots:
url2.setUserName(user);
// The following both fail because they refer to the same account (user and url)
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/sub/ownCloud1", url2).isNull());
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/ownCloud2/", url2).isNull());
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/sub/ownCloud1", url2).second.isNull());
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/ownCloud2/", url2).second.isNull());
// Now it will work because the account is different
QUrl url3("http://anotherexample.org");
url3.setUserName("dummy");
QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/sub/ownCloud1", url3), QString());
QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/ownCloud2/", url3), QString());
QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/sub/ownCloud1", url3).second, QString());
QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/ownCloud2/", url3).second, QString());
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath).isNull());
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/sub/ownCloud1/folder").isNull());
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/sub/ownCloud1/folder/f").isNull());
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath).second.isNull());
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/sub/ownCloud1/folder").second.isNull());
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/sub/ownCloud1/folder/f").second.isNull());
#ifndef Q_OS_WIN // no links on windows, no permissions
// make a bunch of links
@ -135,25 +135,25 @@ private slots:
#ifdef Q_OS_WIN // drive-letter tests
if (!QFileInfo("v:/").exists()) {
QVERIFY(!folderman->checkPathValidityForNewFolder("v:").isNull());
QVERIFY(!folderman->checkPathValidityForNewFolder("v:/").isNull());
QVERIFY(!folderman->checkPathValidityForNewFolder("v:/foo").isNull());
QVERIFY(!folderman->checkPathValidityForNewFolder("v:").second.isNull());
QVERIFY(!folderman->checkPathValidityForNewFolder("v:/").second.isNull());
QVERIFY(!folderman->checkPathValidityForNewFolder("v:/foo").second.isNull());
}
if (QFileInfo("c:/").isWritable()) {
QVERIFY(folderman->checkPathValidityForNewFolder("c:").isNull());
QVERIFY(folderman->checkPathValidityForNewFolder("c:/").isNull());
QVERIFY(folderman->checkPathValidityForNewFolder("c:/foo").isNull());
QVERIFY(folderman->checkPathValidityForNewFolder("c:").second.isNull());
QVERIFY(folderman->checkPathValidityForNewFolder("c:/").second.isNull());
QVERIFY(folderman->checkPathValidityForNewFolder("c:/foo").second.isNull());
}
#endif
// Invalid paths
QVERIFY(!folderman->checkPathValidityForNewFolder("").isNull());
QVERIFY(!folderman->checkPathValidityForNewFolder("").second.isNull());
// REMOVE ownCloud2 from the filesystem, but keep a folder sync'ed to it.
QDir(dirPath + "/ownCloud2/").removeRecursively();
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/ownCloud2/blublu").isNull());
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/ownCloud2/sub/subsub/sub").isNull());
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/ownCloud2/blublu").second.isNull());
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/ownCloud2/sub/subsub/sub").second.isNull());
}
void testFindGoodPathForNewSyncFolder()