Skip to content

Commit

Permalink
kdiroperator: fix full file path not being stripped
Browse files Browse the repository at this point in the history
QDir::isReadable always returns true for an unknown path if the
parent folder is readable, so use QFileInfo::isReadable instead.

Need to use QUrl::StripTrailingSlash and QUrl::RemoveFilename separately
since in QUrlPrivate::appendPath, QUrl::RemoveFilename is done before
QUrl::StripTrailingSlash.

BUG: 459900
FIXED-IN: 5.102
  • Loading branch information
easyteacher committed Dec 31, 2022
1 parent a4cd546 commit ed569ec
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
22 changes: 22 additions & 0 deletions autotests/kdiroperatortest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,28 @@ private Q_SLOTS:
QVERIFY(finishedSpy.wait(1000));
QVERIFY(dirOp.selectedItems().isEmpty());
}

/**
* If one copies the location of a file and then paste that into the location bar,
* the directory browser should show the directory of the file instead of showing an error.
* @see https://bugs.kde.org/459900
*/
void test_bug459900()
{
KDirOperator dirOp;
QSignalSpy urlEnteredSpy(&dirOp, &KDirOperator::urlEntered);
// Try to open a file
const QString filePath = QFINDTESTDATA("servicemenu_protocol_mime_test_data/kio/servicemenus/mimetype_dir.desktop");
dirOp.setUrl(QUrl::fromLocalFile(filePath), true);
// Should accept the file and use its parent directory
QCOMPARE(urlEnteredSpy.size(), 1);
const QUrl fileUrl = QUrl::fromLocalFile(QFileInfo(filePath).absolutePath());
QCOMPARE(urlEnteredSpy.takeFirst().at(0).toUrl().adjusted(QUrl::StripTrailingSlash), fileUrl);
// Even in the same directory, KDirOperator should update the text in pathCombo
dirOp.setUrl(QUrl::fromLocalFile(filePath), true);
QCOMPARE(urlEnteredSpy.size(), 1);
QCOMPARE(urlEnteredSpy.takeFirst().at(0).toUrl().adjusted(QUrl::StripTrailingSlash), fileUrl.adjusted(QUrl::StripTrailingSlash));
}
};

QTEST_MAIN(KDirOperatorTest)
Expand Down
10 changes: 8 additions & 2 deletions src/filewidgets/kdiroperator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -971,8 +971,9 @@ void KDirOperator::setUrl(const QUrl &_newurl, bool clearforward)

if (!d->isReadable(newurl)) {
// maybe newurl is a file? check its parent directory
newurl = newurl.adjusted(QUrl::RemoveFilename | QUrl::StripTrailingSlash);
newurl = newurl.adjusted(QUrl::StripTrailingSlash).adjusted(QUrl::RemoveFilename);
if (newurl.matches(d->m_currUrl, QUrl::StripTrailingSlash)) {
Q_EMIT urlEntered(newurl); // To remove the filename in pathCombo
return; // parent is current dir, nothing to do (fixes #173454, too)
}
KIO::StatJob *job = KIO::stat(newurl);
Expand Down Expand Up @@ -3110,7 +3111,12 @@ bool KDirOperatorPrivate::isReadable(const QUrl &url)
if (!url.isLocalFile()) {
return true; // what else can we say?
}
return QDir(url.toLocalFile()).isReadable();
const QFileInfo fileInfo(url.toLocalFile());
#ifdef Q_OS_WIN
return fileInfo.isReadable() && fileInfo.isDir();
#else
return fileInfo.isReadable();
#endif
}

void KDirOperatorPrivate::slotDirectoryCreated(const QUrl &url)
Expand Down

0 comments on commit ed569ec

Please sign in to comment.