KDeclarative

krunproxy.cpp
1 /*
2  SPDX-FileCopyrightText: 2014 Antonis Tsiapaliokas <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.1-or-later
5 */
6 
7 #include "krunproxy.h"
8 
9 #include <QMimeDatabase>
10 
11 #include <KIO/ApplicationLauncherJob>
12 #include <KIO/OpenUrlJob>
13 #include <KIO/JobUiDelegate>
14 #include <KNotificationJobUiDelegate>
15 #include <KService>
16 
17 KRunProxy::KRunProxy(QObject* parent)
18  : QObject(parent)
19 {
20 }
21 
22 // Apparently unused
23 bool KRunProxy::openUrl(const QString &file)
24 {
25  QUrl fileUrl(file);
26  QMimeDatabase db;
27  QMimeType mime = db.mimeTypeForUrl(fileUrl);
28  const QString fileMimeType = mime.name();
29 
30  if (fileMimeType == QLatin1String("application/x-executable") || !mime.isValid()) {
31  //for security reasons we should not be able to execute applications.
32  //We should use its desktop file to access it.
33  return false;
34  }
35 
36  if (fileMimeType == QLatin1String("application/x-desktop") && fileUrl.isLocalFile()) {
37  // If our mimetype is a desktop file, then we don't want to open
38  // the desktop file itself but the application in which it is associated
39  // with.
40  return openService(fileUrl.toLocalFile());
41  } else {
42  KIO::OpenUrlJob *job = new KIO::OpenUrlJob(fileUrl, fileMimeType);
43  // JobUiDelegate is widgets-based, but that's currently the only way to get the open-with dialog
44  job->setUiDelegate(new KIO::JobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, nullptr));
45  job->start();
46  return true;
47  }
48 }
49 
50 bool KRunProxy::openService(const QString &serviceName)
51 {
52  KService::Ptr service = KService::serviceByDesktopName(serviceName);
53  if (service) {
56  job->start();
57  return true;
58  }
59  return false;
60 }
static Ptr serviceByDesktopName(const QString &_name)
void start() override
QMimeType mimeTypeForUrl(const QUrl &url) const const
bool isValid() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Jan 25 2021 22:44:28 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.