diff --git a/autotests/jobtest.h b/autotests/jobtest.h --- a/autotests/jobtest.h +++ b/autotests/jobtest.h @@ -91,6 +91,7 @@ void statSymlink(); #endif void mostLocalUrl(); + void mostLocalUrlHttp(); void chmodFile(); #ifdef Q_OS_UNIX void chmodSticky(); diff --git a/autotests/jobtest.cpp b/autotests/jobtest.cpp --- a/autotests/jobtest.cpp +++ b/autotests/jobtest.cpp @@ -1642,6 +1642,15 @@ QCOMPARE(job->mostLocalUrl().toLocalFile(), filePath); } +void JobTest::mostLocalUrlHttp() { + // the url is returned as-is, as an http url can't have a mostLocalUrl + const QUrl url("http://www.google.com"); + KIO::StatJob *httpStat = KIO::mostLocalUrl(url, KIO::HideProgressInfo); + QVERIFY(httpStat); + QVERIFY2(httpStat->exec(), qPrintable(httpStat->errorString())); + QCOMPARE(httpStat->mostLocalUrl(), url); +} + void JobTest::chmodFile() { const QString filePath = homeTmpDir() + "fileForChmod"; diff --git a/src/core/statjob.h b/src/core/statjob.h --- a/src/core/statjob.h +++ b/src/core/statjob.h @@ -108,18 +108,20 @@ /** * @brief most local URL - * Call this in the slot connected to result, - * and only after making sure no error happened. - * @return the most local URL for the URL we were stat'ing. + * + * Call this in a slot connected to the result signal, and only after making sure no error + * happened. + * + * @return the most local URL for the URL we were stat'ing * * Sample usage: * * @code - * KIO::StatJob* job = KIO::mostLocalUrl("desktop:/foo"); + * KIO::StatJob *job = KIO::mostLocalUrl("desktop:/foo"); * job->uiDelegate()->setWindow(this); * connect(job, &KJob::result, this, &MyClass::slotMostLocalUrlResult); * [...] - * // and in the slot slotMostLocalUrlResult(KJob *job) + * // and in slotMostLocalUrlResult(KJob *job) * if (job->error()) { * [...] // doesn't exist * } else { diff --git a/src/core/statjob.cpp b/src/core/statjob.cpp --- a/src/core/statjob.cpp +++ b/src/core/statjob.cpp @@ -23,6 +23,7 @@ #include "job_p.h" #include "slave.h" #include "scheduler.h" +#include #include #include @@ -126,14 +127,18 @@ QUrl StatJob::mostLocalUrl() const { - if (!url().isLocalFile()) { + const QUrl _url = url(); + + if (_url.isLocalFile()) { + return _url; + } + + QString path; + if (KProtocolInfo::protocolClass(_url.scheme()) == QLatin1String(":local")) { const UDSEntry &udsEntry = d_func()->m_statResult; - const QString path = udsEntry.stringValue(KIO::UDSEntry::UDS_LOCAL_PATH); - if (!path.isEmpty()) { - return QUrl::fromLocalFile(path); - } + path = udsEntry.stringValue(KIO::UDSEntry::UDS_LOCAL_PATH); } - return url(); + return !path.isEmpty() ? QUrl::fromLocalFile(path) : _url; } void StatJobPrivate::start(Slave *slave) @@ -212,7 +217,7 @@ StatJob *KIO::mostLocalUrl(const QUrl &url, JobFlags flags) { StatJob *job = statDetails(url, StatJob::SourceSide, KIO::StatDefaultDetails, flags); - if (url.isLocalFile()) { + if (url.isLocalFile() || KProtocolInfo::protocolClass(url.scheme()) != QLatin1String(":local")) { QTimer::singleShot(0, job, &StatJob::slotFinished); Scheduler::cancelJob(job); // deletes the slave if not 0 } diff --git a/src/ioslaves/trash/tests/testtrash.h b/src/ioslaves/trash/tests/testtrash.h --- a/src/ioslaves/trash/tests/testtrash.h +++ b/src/ioslaves/trash/tests/testtrash.h @@ -84,6 +84,7 @@ void listRootDir(); void listRecursiveRootDir(); + void mostLocalUrlTest(); void listSubDir(); void delRootFile(); diff --git a/src/ioslaves/trash/tests/testtrash.cpp b/src/ioslaves/trash/tests/testtrash.cpp --- a/src/ioslaves/trash/tests/testtrash.cpp +++ b/src/ioslaves/trash/tests/testtrash.cpp @@ -738,6 +738,20 @@ return MyNetAccess_stat(url, dummy); } +void TestTrash::mostLocalUrlTest() +{ + const QStringList trashFiles = QDir(m_trashDir + QLatin1String("/files/")).entryList(); + for (QString file : trashFiles) { + if (file == QLatin1Char('.') || file == QLatin1String("..") || file.contains(QLatin1Char('%'))) { + continue; + } + const QUrl url(QLatin1String("trash:/0-") + file); + KIO::StatJob *statJob = KIO::mostLocalUrl(url, KIO::HideProgressInfo); + QVERIFY(statJob->exec()); + QCOMPARE(url, statJob->mostLocalUrl()); + } +} + void TestTrash::statRoot() { QUrl url(QStringLiteral("trash:/"));