File indexing completed on 2024-05-19 15:09:22

0001 /*
0002     SPDX-FileCopyrightText: 2014 Antonis Tsiapaliokas <antonis.tsiapaliokas@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include "krunproxy.h"
0008 
0009 #include <QDebug>
0010 #include <QMimeDatabase>
0011 
0012 #include <KIO/ApplicationLauncherJob>
0013 #include <KIO/JobUiDelegate>
0014 #include <KIO/OpenUrlJob>
0015 #include <KNotificationJobUiDelegate>
0016 #include <KService>
0017 
0018 KRunProxy::KRunProxy(QObject *parent)
0019     : QObject(parent)
0020 {
0021     qWarning() << "Using KRun from QML is deprecated (since 5.88)."
0022                << "Use Qt.openUrlExternally, or the KIO::OpenUrlJob/KIO::ApplicationLauncherJob classes from C++ instead.";
0023 }
0024 
0025 bool KRunProxy::openUrl(const QString &file)
0026 {
0027     QUrl fileUrl(file);
0028     QMimeDatabase db;
0029     QMimeType mime = db.mimeTypeForUrl(fileUrl);
0030     const QString fileMimeType = mime.name();
0031 
0032     if (fileMimeType == QLatin1String("application/x-executable") || !mime.isValid()) {
0033         // for security reasons we should not be able to execute applications.
0034         // We should use its desktop file to access it.
0035         return false;
0036     }
0037 
0038     if (fileMimeType == QLatin1String("application/x-desktop") && fileUrl.isLocalFile()) {
0039         // If our mimetype is a desktop file, then we don't want to open
0040         // the desktop file itself but the application in which it is associated
0041         // with.
0042         return openService(fileUrl.toLocalFile());
0043     } else {
0044         KIO::OpenUrlJob *job = new KIO::OpenUrlJob(fileUrl, fileMimeType);
0045         // JobUiDelegate is widgets-based, but that's currently the only way to get the open-with dialog
0046         job->setUiDelegate(new KIO::JobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, nullptr));
0047         job->start();
0048         return true;
0049     }
0050 }
0051 
0052 bool KRunProxy::openService(const QString &serviceName)
0053 {
0054     KService::Ptr service = KService::serviceByDesktopName(serviceName);
0055     if (service) {
0056         KIO::ApplicationLauncherJob *job = new KIO::ApplicationLauncherJob(service);
0057         job->setUiDelegate(new KNotificationJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled));
0058         job->start();
0059         return true;
0060     }
0061     return false;
0062 }
0063 
0064 #include "moc_krunproxy.cpp"