SPDX-FileCopyrightText: 2012-2016 Eike Hein <hein@kde.org>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "backend.h"
#include "log_settings.h"
#include <KConfigGroup>
#include <KDesktopFile>
#include <KFileItem>
#include <KFilePlacesModel>
#include <KLocalizedString>
#include <KNotificationJobUiDelegate>
#include <KProtocolInfo>
#include <KService>
#include <KServiceAction>
#include <KWindowEffects>
#include <KWindowSystem>
#include <KIO/ApplicationLauncherJob>
#include <KService/KApplicationTrader>
#include <QAction>
#include <QActionGroup>
#include <QApplication>
#include <QDBusConnection>
#include <QDBusConnectionInterface>
#include <QDBusMessage>
#include <QDBusMetaType>
#include <QDBusPendingCall>
#include <QDBusReply>
#include <QDBusServiceWatcher>
#include <QJsonArray>
#include <QMenu>
#include <QQuickItem>
#include <QQuickWindow>
#include <QStandardPaths>
#include <QTimer>
#include <QVersionNumber>
#include <KActivities/Consumer>
#include <KActivities/Stats/Cleaning>
#include <KActivities/Stats/ResultSet>
#include <KActivities/Stats/Terms>
#include <processcore/process.h>
#include <processcore/processes.h>
namespace KAStats = KActivities::Stats;
using namespace KAStats;
using namespace KAStats::Terms;
static const QString highlightWindowName = QStringLiteral("org.kde.KWin.HighlightWindow");
static const QString highlightWindowPath = QStringLiteral("/org/kde/KWin/HighlightWindow");
static const QString &highlightWindowInterface = highlightWindowName;
static const QString appViewName = QStringLiteral("org.kde.KWin.Effect.WindowView1");
static const QString appViewPath = QStringLiteral("/org/kde/KWin/Effect/WindowView1");
static const QString &appViewInterface = appViewName;
Backend::Backend(QObject *parent)
: QObject(parent)
, m_highlightWindows(false)
, m_actionGroup(new QActionGroup(this))
{
m_windowViewAvailable = QDBusConnection::sessionBus().interface()->isServiceRegistered(appViewName);
auto watcher = new QDBusServiceWatcher(appViewName,
QDBusConnection::sessionBus(),
QDBusServiceWatcher::WatchForRegistration | QDBusServiceWatcher::WatchForUnregistration,
this);
connect(watcher, &QDBusServiceWatcher::serviceRegistered, this, [this] {
m_windowViewAvailable = true;
Q_EMIT windowViewAvailableChanged();
});
connect(watcher, &QDBusServiceWatcher::serviceUnregistered, this, [this] {
m_windowViewAvailable = false;
Q_EMIT windowViewAvailableChanged();
});
}
Backend::~Backend()
{
}
QQuickItem *Backend::taskManagerItem() const
{
return m_taskManagerItem;
}
void Backend::setTaskManagerItem(QQuickItem *item)
{
if (item != m_taskManagerItem) {
m_taskManagerItem = item;
Q_EMIT taskManagerItemChanged();
}
}
bool Backend::highlightWindows() const
{
return m_highlightWindows;
}
void Backend::setHighlightWindows(bool highlight)
{
if (highlight != m_highlightWindows) {
m_highlightWindows = highlight;
updateWindowHighlight();
Q_EMIT highlightWindowsChanged();
}
}
QUrl Backend::tryDecodeApplicationsUrl(const QUrl &launcherUrl)
{
if (launcherUrl.isValid() && launcherUrl.scheme() == QLatin1String("applications")) {
const KService::Ptr service = KService::serviceByMenuId(launcherUrl.path());
if (service) {
return QUrl::fromLocalFile(service->entryPath());
}
}
return launcherUrl;
}
QStringList Backend::applicationCategories(const QUrl &launcherUrl)
{
const QUrl desktopEntryUrl = tryDecodeApplicationsUrl(launcherUrl);
if (!desktopEntryUrl.isValid() || !desktopEntryUrl.isLocalFile() || !KDesktopFile::isDesktopFile(desktopEntryUrl.toLocalFile())) {
return QStringList();
}
KDesktopFile desktopFile(desktopEntryUrl.toLocalFile());
return desktopFile.desktopGroup().readXdgListEntry(QStringLiteral("Categories"));
}
QVariantList Backend::jumpListActions(const QUrl &launcherUrl, QObject *parent)
{
QVariantList actions;
if (!parent) {
return actions;
}
QUrl desktopEntryUrl = tryDecodeApplicationsUrl(launcherUrl);
if (!desktopEntryUrl.isValid() || !desktopEntryUrl.isLocalFile() || !KDesktopFile::isDesktopFile(desktopEntryUrl.toLocalFile())) {
return actions;
}
const KService::Ptr service = KService::serviceByDesktopPath(desktopEntryUrl.toLocalFile());
if (!service) {
return actions;
}
if (service->storageId() == QLatin1String("systemsettings.desktop")) {
actions = systemSettingsActions(parent);
if (!actions.isEmpty()) {
return actions;
}
}
const auto jumpListActions = service->actions();
for (const KServiceAction &serviceAction : jumpListActions) {
if (serviceAction.noDisplay()) {
continue;
}
QAction *action = new QAction(parent);
action->setText(serviceAction.text());
action->setIcon(QIcon::fromTheme(serviceAction.icon()));
if (serviceAction.isSeparator()) {
action->setSeparator(true);
}
connect(action, &QAction::triggered, this, [serviceAction]() {
auto *job = new KIO::ApplicationLauncherJob(serviceAction);
auto *delegate = new KNotificationJobUiDelegate;
delegate->setAutoErrorHandlingEnabled(true);
job->setUiDelegate(delegate);
job->start();
});
actions << QVariant::fromValue<QAction *>(action);
}
return actions;
}
QVariantList Backend::systemSettingsActions(QObject *parent) const
{
QVariantList actions;
auto query = AllResources | Agent(QStringLiteral("org.kde.systemsettings")) | HighScoredFirst | Limit(5);
ResultSet results(query);
QStringList ids;
for (const ResultSet::Result &result : results) {
ids << QUrl(result.resource()).path();
}
if (ids.count() < 5) {
return actions;
}
for (const QString &id : qAsConst(ids)) {
KService::Ptr service = KService::serviceByStorageId(id);
if (!service || !service->isValid()) {
continue;
}
QAction *action = new QAction(parent);
action->setText(service->name());
action->setIcon(QIcon::fromTheme(service->icon()));
connect(action, &QAction::triggered, this, [service]() {
auto *job = new KIO::ApplicationLauncherJob(service);
auto *delegate = new KNotificationJobUiDelegate;
delegate->setAutoErrorHandlingEnabled(true);
job->setUiDelegate(delegate);
job->start();
});
actions << QVariant::fromValue<QAction *>(action);
}
return actions;
}
QVariantList Backend::placesActions(const QUrl &launcherUrl, bool showAllPlaces, QObject *parent)
{
if (!parent) {
return QVariantList();
}
QUrl desktopEntryUrl = tryDecodeApplicationsUrl(launcherUrl);
if (!desktopEntryUrl.isValid() || !desktopEntryUrl.isLocalFile() || !KDesktopFile::isDesktopFile(desktopEntryUrl.toLocalFile())) {
return QVariantList();
}
QVariantList actions;
if (!applicationCategories(launcherUrl).contains(QLatin1String("FileManager"))) {
return actions;
}
QString previousGroup;
QMenu *subMenu = nullptr;
std::unique_ptr<KFilePlacesModel> placesModel(new KFilePlacesModel());
for (int i = 0; i < placesModel->rowCount(); ++i) {
QModelIndex idx = placesModel->index(i, 0);
if (placesModel->isHidden(idx)) {
continue;
}
const QString &title = idx.data(Qt::DisplayRole).toString();
const QIcon &icon = idx.data(Qt::DecorationRole).value<QIcon>();
const QUrl &url = idx.data(KFilePlacesModel::UrlRole).toUrl();
QAction *placeAction = new QAction(icon, title, parent);
connect(placeAction, &QAction::triggered, this, [url, desktopEntryUrl] {
KService::Ptr service = KService::serviceByDesktopPath(desktopEntryUrl.toLocalFile());
if (!service) {
return;
}
auto *job = new KIO::ApplicationLauncherJob(service);
auto *delegate = new KNotificationJobUiDelegate;
delegate->setAutoErrorHandlingEnabled(true);
job->setUiDelegate(delegate);
job->setUrls({url});
job->start();
});
const QString &groupName = idx.data(KFilePlacesModel::GroupRole).toString();
if (previousGroup.isEmpty()) {
previousGroup = groupName;
}
if (previousGroup != groupName) {
QAction *subMenuAction = new QAction(groupName, parent);
subMenu = new QMenu();
connect(parent, &QObject::destroyed, subMenu, &QObject::deleteLater);
subMenuAction->setMenu(subMenu);
actions << QVariant::fromValue(subMenuAction);
previousGroup = groupName;
}
if (subMenu) {
subMenu->addAction(placeAction);
} else {
actions << QVariant::fromValue(placeAction);
}
}
if (!showAllPlaces && actions.count() > 7) {
const int totalActionCount = actions.count();
while (actions.count() > 5) {
actions.removeLast();
}
QAction *action = new QAction(parent);
action->setText(i18ncp("Show all user Places", "%1 more Place", "%1 more Places", totalActionCount - actions.count()));
connect(action, &QAction::triggered, this, &Backend::showAllPlaces);
actions << QVariant::fromValue(action);
}
return actions;
}
QVariantList Backend::recentDocumentActions(const QUrl &launcherUrl, QObject *parent)
{
QVariantList actions;
if (!parent) {
return actions;
}
QUrl desktopEntryUrl = tryDecodeApplicationsUrl(launcherUrl);
if (!desktopEntryUrl.isValid() || !desktopEntryUrl.isLocalFile() || !KDesktopFile::isDesktopFile(desktopEntryUrl.toLocalFile())) {
return QVariantList();
}
QString desktopName = desktopEntryUrl.fileName();
QString storageId = desktopName;
if (storageId.endsWith(QLatin1String(".desktop"))) {
storageId = storageId.left(storageId.length() - 8);
}
auto query = UsedResources | RecentlyUsedFirst | Agent(storageId) | Type::any() | Activity::current();
ResultSet results(query);
ResultSet::const_iterator resultIt = results.begin();
int actionCount = 0;
bool allFolders = true;
bool allDownloads = true;
bool allRemoteWithoutFileName = true;
const QString downloadsPath = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
while (actionCount < 5 && resultIt != results.end()) {
const QString resource = (*resultIt).resource();
const QString mimetype = (*resultIt).mimetype();
const QUrl url = (*resultIt).url();
++resultIt;
if (!url.isValid()) {
continue;
}
allFolders = allFolders && mimetype == QLatin1String("inode/directory");
allDownloads = allDownloads && url.toLocalFile().startsWith(downloadsPath);
allRemoteWithoutFileName = allRemoteWithoutFileName && !url.isLocalFile() && url.fileName().isEmpty();
QString name = url.fileName();
if (name.isEmpty()) {
name = url.toDisplayString();
}
QString iconName;
const QString protocol = url.scheme();
if (!KProtocolInfo::isKnownProtocol(protocol) || KProtocolInfo::isHelperProtocol(protocol)) {
const KService::Ptr service = KApplicationTrader::preferredService(QLatin1String("x-scheme-handler/") + protocol);
if (service) {
iconName = service->icon();
} else if (KProtocolInfo::isKnownProtocol(protocol)) {
Q_ASSERT(KProtocolInfo::isHelperProtocol(protocol));
iconName = KProtocolInfo::icon(protocol);
} else {
continue;
}
} else {
const KFileItem fileItem(url, mimetype);
iconName = fileItem.iconName();
}
QAction *action = new QAction(parent);
action->setText(name);
action->setIcon(QIcon::fromTheme(iconName, QIcon::fromTheme(QStringLiteral("unknown"))));
action->setProperty("agent", storageId);
action->setProperty("entryPath", desktopEntryUrl);
action->setProperty("mimeType", mimetype);
action->setData(url);
connect(action, &QAction::triggered, this, &Backend::handleRecentDocumentAction);
actions << QVariant::fromValue<QAction *>(action);
++actionCount;
}
if (actionCount > 0) {
if (allDownloads) {
actions.prepend(i18n("Recent Downloads"));
} else if (allRemoteWithoutFileName) {
actions.prepend(i18n("Recent Connections"));
} else if (allFolders) {
actions.prepend(i18n("Recent Places"));
}
QAction *separatorAction = new QAction(parent);
separatorAction->setSeparator(true);
actions << QVariant::fromValue<QAction *>(separatorAction);
QAction *action = new QAction(parent);
if (allDownloads) {
action->setText(i18nc("@action:inmenu", "Forget Recent Downloads"));
} else if (allRemoteWithoutFileName) {
action->setText(i18nc("@action:inmenu", "Forget Recent Connections"));
} else if (allFolders) {
action->setText(i18nc("@action:inmenu", "Forget Recent Places"));
} else {
action->setText(i18nc("@action:inmenu", "Forget Recent Files"));
}
action->setIcon(QIcon::fromTheme(QStringLiteral("edit-clear-history")));
action->setProperty("agent", storageId);
connect(action, &QAction::triggered, this, &Backend::handleRecentDocumentAction);
actions << QVariant::fromValue<QAction *>(action);
}
return actions;
}
void Backend::handleRecentDocumentAction() const
{
const QAction *action = qobject_cast<QAction *>(sender());
if (!action) {
return;
}
const QString agent = action->property("agent").toString();
if (agent.isEmpty()) {
return;
}
const QString desktopPath = action->property("entryPath").toUrl().toLocalFile();
const QUrl url = action->data().toUrl();
if (desktopPath.isEmpty() || url.isEmpty()) {
auto query = UsedResources | Agent(agent) | Type::any() | Activity::current() | Url::file();
KAStats::forgetResources(query);
return;
}
KService::Ptr service = KService::serviceByDesktopPath(desktopPath);
if (!service) {
return;
}
const auto mimetype = action->property("mimeType").toString();
if (!mimetype.isEmpty()) {
if (!service->hasMimeType(mimetype)) {
service = KApplicationTrader::preferredService(mimetype);
if (!service) {
return;
} else {
qCWarning(TASKMANAGER_DEBUG) << "Preventing the file to open with " << service->desktopEntryName() << "no alternative found";
}
}
}
auto *job = new KIO::ApplicationLauncherJob(service);
auto *delegate = new KNotificationJobUiDelegate;
delegate->setAutoErrorHandlingEnabled(true);
job->setUiDelegate(delegate);
job->setUrls({url});
job->start();
}
void Backend::setActionGroup(QAction *action) const
{
if (action) {
action->setActionGroup(m_actionGroup);
}
}
QRect Backend::globalRect(QQuickItem *item) const
{
if (!item || !item->window()) {
return QRect();
}
QRect iconRect(item->x(), item->y(), item->width(), item->height());
iconRect.moveTopLeft(item->parentItem()->mapToScene(iconRect.topLeft()).toPoint());
iconRect.moveTopLeft(item->window()->mapToGlobal(iconRect.topLeft()));
return iconRect;
}
bool Backend::windowViewAvailable() const
{
return m_windowViewAvailable;
}
void Backend::activateWindowView(const QVariant &_winIds)
{
if (m_windowsToHighlight.count()) {
m_windowsToHighlight.clear();
updateWindowHighlight();
}
auto message = QDBusMessage::createMethodCall(appViewName, appViewPath, appViewInterface, QStringLiteral("activate"));
message << _winIds.toStringList();
QDBusConnection::sessionBus().asyncCall(message);
}
bool Backend::isApplication(const QUrl &url) const
{
if (!url.isValid() || !url.isLocalFile()) {
return false;
}
const QString &localPath = url.toLocalFile();
if (!KDesktopFile::isDesktopFile(localPath)) {
return false;
}
KDesktopFile desktopFile(localPath);
return desktopFile.hasApplicationType();
}
QList<QUrl> Backend::jsonArrayToUrlList(const QJsonArray &array) const
{
QList<QUrl> urls;
urls.reserve(array.count());
for (auto it = array.constBegin(), end = array.constEnd(); it != end; ++it) {
urls << QUrl(it->toString());
}
return urls;
}
void Backend::cancelHighlightWindows()
{
m_windowsToHighlight.clear();
updateWindowHighlight();
}
qint64 Backend::parentPid(qint64 pid) const
{
KSysGuard::Processes procs;
procs.updateOrAddProcess(pid);
KSysGuard::Process *proc = procs.getProcess(pid);
if (!proc) {
return -1;
}
int parentPid = proc->parentPid();
if (parentPid != -1) {
procs.updateOrAddProcess(parentPid);
KSysGuard::Process *parentProc = procs.getProcess(parentPid);
if (!parentProc) {
return -1;
}
if (!proc->cGroup().isEmpty() && parentProc->cGroup() == proc->cGroup()) {
return parentProc->pid();
}
}
return -1;
}
void Backend::windowsHovered(const QVariant &_winIds, bool hovered)
{
m_windowsToHighlight.clear();
if (hovered) {
m_windowsToHighlight = _winIds.toStringList();
}
QTimer::singleShot(0, this, &Backend::updateWindowHighlight);
}
void Backend::updateWindowHighlight()
{
if (!m_highlightWindows) {
return;
}
auto message = QDBusMessage::createMethodCall(highlightWindowName, highlightWindowPath, highlightWindowInterface, QStringLiteral("highlightWindows"));
message << m_windowsToHighlight;
QDBusConnection::sessionBus().asyncCall(message);
}
#include "moc_backend.cpp"