diff --git a/src/lib/io/kfileutils.h b/src/lib/io/kfileutils.h --- a/src/lib/io/kfileutils.h +++ b/src/lib/io/kfileutils.h @@ -39,5 +39,19 @@ */ KCOREADDONS_EXPORT QString suggestName(const QUrl &baseURL, const QString &oldName); +/** + * Returns whether the @p url of @p mimetype is executable. + * To be executable the file must pass the following rules: + * -# Must reside on the local filesystem. + * -# Must be marked as executable for the user by the filesystem. + * -# The mime type must inherit application/x-executable, application/x-executable-script or application/x-sharedlib. + * To allow a script to run when the above rules are satisfied add the entry + * @code + * X-KDE-IsAlso=application/x-executable-script + * @endcode + * to the mimetype's desktop file. + */ +KCOREADDONS_EXPORT bool isExecutableFile(const QUrl &url, const QString &mimetype); + } #endif diff --git a/src/lib/io/kfileutils.cpp b/src/lib/io/kfileutils.cpp --- a/src/lib/io/kfileutils.cpp +++ b/src/lib/io/kfileutils.cpp @@ -71,3 +71,26 @@ return suggestName(baseURL, suggestedName); } } + +bool KFileUtils::isExecutableFile(const QUrl &url, const QString &mimetype) +{ + if (!url.isLocalFile()) { + return false; + } + QFileInfo file(url.toLocalFile()); + if (file.isExecutable()) { // Got a prospective file to run + QMimeDatabase db; + QMimeType mimeType = db.mimeTypeForName(mimetype); + if (mimeType.inherits(QStringLiteral("application/x-executable")) || +#ifdef Q_OS_WIN + mimeType.inherits(QStringLiteral("application/x-ms-dos-executable")) || +#endif + mimeType.inherits(QStringLiteral("application/x-executable-script")) || + mimeType.inherits(QStringLiteral("application/x-sharedlib")) + ) { + return true; + } + } + return false; +} +