diff --git a/autotests/applicationlauncherjobtest.h b/autotests/applicationlauncherjobtest.h --- a/autotests/applicationlauncherjobtest.h +++ b/autotests/applicationlauncherjobtest.h @@ -41,6 +41,8 @@ void shouldFailOnNonExistingExecutable_data(); void shouldFailOnNonExistingExecutable(); + void shouldFailOnInvalidService(); + private: QString createTempService(); void writeTempServiceDesktopFile(const QString &filePath); diff --git a/autotests/applicationlauncherjobtest.cpp b/autotests/applicationlauncherjobtest.cpp --- a/autotests/applicationlauncherjobtest.cpp +++ b/autotests/applicationlauncherjobtest.cpp @@ -32,6 +32,7 @@ #include // kill #endif +#include #include #include #include @@ -186,6 +187,26 @@ QFile::remove(desktopFilePath); } +void ApplicationLauncherJobTest::shouldFailOnInvalidService() +{ + const QString desktopFilePath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/kservices5/invalid_service.desktop"); + KDesktopFile file(desktopFilePath); + KConfigGroup group = file.desktopGroup(); + group.writeEntry("Name", "KRunUnittestService"); + group.writeEntry("Type", "NoSuchType"); + group.writeEntry("Exec", "does_not_exist"); + file.sync(); + + QTest::ignoreMessage(QtWarningMsg, QRegularExpression("The desktop entry file \".*\" has Type.*\"NoSuchType\" instead of \"Application\" or \"Service\"")); + KService::Ptr servicePtr(new KService(desktopFilePath)); + KIO::ApplicationLauncherJob *job = new KIO::ApplicationLauncherJob(servicePtr, this); + QVERIFY(!job->exec()); + QCOMPARE(job->error(), KJob::UserDefinedError); + QCOMPARE(job->errorString(), QStringLiteral("The desktop entry file\n%1\nis not valid.").arg(desktopFilePath)); + + QFile::remove(desktopFilePath); +} + void ApplicationLauncherJobTest::writeTempServiceDesktopFile(const QString &filePath) { if (!QFile::exists(filePath)) { diff --git a/src/gui/kprocessrunner.cpp b/src/gui/kprocessrunner.cpp --- a/src/gui/kprocessrunner.cpp +++ b/src/gui/kprocessrunner.cpp @@ -50,6 +50,11 @@ m_executable(KIO::DesktopExecParser::executablePath(service->exec())) { ++s_instanceCount; + + if (!service->isValid()) { + emitDelayedError(i18n("The desktop entry file\n%1\nis not valid.", service->entryPath())); + return; + } KIO::DesktopExecParser execParser(*service, urls); const QString realExecutable = execParser.resultingArguments().at(0); @@ -134,7 +139,7 @@ { if (service && !service->entryPath().isEmpty() && !KDesktopFile::isAuthorizedDesktopFile(service->entryPath())) { - qCWarning(KIO_GUI) << "No authorization to execute " << service->entryPath(); + qCWarning(KIO_GUI) << "No authorization to execute" << service->entryPath(); emitDelayedError(i18n("You are not authorized to execute this file.")); return; } diff --git a/src/widgets/kdesktopfileactions.cpp b/src/widgets/kdesktopfileactions.cpp --- a/src/widgets/kdesktopfileactions.cpp +++ b/src/widgets/kdesktopfileactions.cpp @@ -54,7 +54,6 @@ enum BuiltinServiceType { ST_MOUNT = 0x0E1B05B0, ST_UNMOUNT = 0x0E1B05B1 }; // random numbers static bool runFSDevice(const QUrl &_url, const KDesktopFile &cfg, const QByteArray &asn); -static bool runApplication(const QUrl &_url, const QString &_serviceFile, const QByteArray &asn); static bool runLink(const QUrl &_url, const KDesktopFile &cfg, const QByteArray &asn); @@ -91,7 +90,8 @@ return runFSDevice(u, cfg, asn); } else if (cfg.hasApplicationType() || (cfg.readType() == QLatin1String("Service") && !cfg.desktopGroup().readEntry("Exec").isEmpty())) { // for kio_settings - return runApplication(u, u.toLocalFile(), asn); + KService service(u.toLocalFile()); + return KRun::runApplication(service, QList(), nullptr /*TODO - window*/, KRun::RunFlags{}, QString(), asn); } else if (cfg.hasLinkType()) { return runLink(u, cfg, asn); } @@ -137,19 +137,6 @@ return retval; } -static bool runApplication(const QUrl &_url, const QString &_serviceFile, const QByteArray &asn) -{ - KService s(_serviceFile); - if (!s.isValid()) - { - QString tmp = i18n("The desktop entry file\n%1\nis not valid.", _url.toString()); - KMessageBox::error(nullptr, tmp); - return false; - } - - return KRun::runApplication(s, QList(), nullptr /*TODO - window*/, KRun::RunFlags{}, QString(), asn); -} - static bool runLink(const QUrl &_url, const KDesktopFile &cfg, const QByteArray &asn) { QString u = cfg.readUrl();