Plasma-workspace

utils.cpp
1/*
2 SPDX-FileCopyrightText: 2019 Kai Uwe Broulik <kde@privat.broulik.de>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#include "utils_p.h"
8
9#include <ranges>
10
11#include "notifications.h"
12
13#include <QAbstractItemModel>
14#include <QAbstractProxyModel>
15#include <QConcatenateTablesProxyModel>
16#include <QCoreApplication>
17#include <QDBusConnection>
18#include <QDBusConnectionInterface>
19#include <QFile>
20#include <QFileInfo>
21#include <QMetaEnum>
22#include <QSettings>
23#include <QTextStream>
24
25#include <KProcessList>
26
27using namespace NotificationManager;
28
29QHash<int, QByteArray> Utils::roleNames()
30{
31 static QHash<int, QByteArray> s_roles;
32
33 if (s_roles.isEmpty()) {
34 // This generates role names from the Roles enum in the form of: FooRole -> foo
35 const QMetaEnum e = QMetaEnum::fromType<Notifications::Roles>();
36
37 // Qt built-in roles we use
38 s_roles.insert(Qt::DisplayRole, QByteArrayLiteral("display"));
39 s_roles.insert(Qt::DecorationRole, QByteArrayLiteral("decoration"));
40 s_roles.insert(Qt::AccessibleDescriptionRole, QByteArrayLiteral("accessibleDescription"));
41
42 for (int i = 0; i < e.keyCount(); ++i) {
43 const int value = e.value(i);
44
45 QByteArray key(e.key(i));
46 key[0] = key[0] + 32; // lower case first letter
47 key.chop(4); // strip "Role" suffix
48
49 s_roles.insert(value, key);
50 }
51
52 s_roles.insert(Notifications::IdRole, QByteArrayLiteral("notificationId")); // id is QML-reserved
53 }
54
55 return s_roles;
56}
57
58QString Utils::processNameFromPid(uint pid)
59{
60 auto processInfo = KProcessList::processInfo(pid);
61
62 if (!processInfo.isValid()) {
63 return QString();
64 }
65
66 return processInfo.name();
67}
68
69QString Utils::desktopEntryFromPid(uint pid)
70{
71 const QString flatpakInfoPath = QStringLiteral("/proc/%1/root/.flatpak-info").arg(QString::number(pid));
72 if (QFileInfo::exists(flatpakInfoPath)) {
73 QSettings flatpakInfo(flatpakInfoPath, QSettings::IniFormat);
74
75 const QString name = flatpakInfo.value("Application/name").toString();
76 if (!name.isEmpty()) {
77 return name;
78 }
79
80 // If it's a flatpak, can't be a snap, bail out.
81 return QString();
82 }
83
84 QFile environFile(QStringLiteral("/proc/%1/environ").arg(QString::number(pid)));
85 if (environFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
86 constexpr QByteArrayView bamfDesktopFileHint("BAMF_DESKTOP_FILE_HINT");
87
88 const QByteArrayList lines = environFile.readAll().split('\0');
89 for (const QByteArrayView line : lines) {
90 const int equalsIdx = line.indexOf('=');
91 if (equalsIdx <= 0) {
92 continue;
93 }
94
95 const QByteArrayView key = line.sliced(0, equalsIdx);
96 if (key == bamfDesktopFileHint) {
97 const QByteArrayView value = line.sliced(equalsIdx + 1);
98 return QString::fromUtf8(value);
99 }
100 }
101 }
102
103 return QString();
104}
105
106QModelIndex Utils::mapToModel(const QModelIndex &idx, const QAbstractItemModel *sourceModel)
107{
108 // KModelIndexProxyMapper can only map different indices to a single source
109 // but we have the other way round, a single index that splits into different source models
110 QModelIndex resolvedIdx = idx;
111 while (resolvedIdx.isValid() && resolvedIdx.model() != sourceModel) {
112 if (auto *proxyModel = qobject_cast<const QAbstractProxyModel *>(resolvedIdx.model())) {
113 resolvedIdx = proxyModel->mapToSource(resolvedIdx);
114 // QConcatenateTablesProxyModel isn't a "real" proxy model, so we need to special case for it :(
115 } else if (auto *concatenateModel = qobject_cast<const QConcatenateTablesProxyModel *>(resolvedIdx.model())) {
116 resolvedIdx = concatenateModel->mapToSource(resolvedIdx);
117 } else {
118 if (resolvedIdx.model() != sourceModel) {
119 resolvedIdx = QModelIndex(); // give up
120 }
121 }
122 }
123 return resolvedIdx;
124}
125
126bool Utils::isDBusMaster()
127{
128 return qApp->property("_plasma_dbus_master").toBool();
129}
@ IdRole
A notification identifier. This can be uint notification ID or string application job source.
QString name(StandardShortcut id)
QByteArrayView sliced(qsizetype pos) const const
bool exists() const const
iterator insert(const Key &key, const T &value)
bool isEmpty() const const
qsizetype indexOf(const AT &value, qsizetype from) const const
const char * key(int index) const const
int keyCount() const const
int value(int index) const const
bool isValid() const const
const QAbstractItemModel * model() const const
QString arg(Args &&... args) const const
QString fromUtf8(QByteArrayView str)
bool isEmpty() const const
QString number(double n, char format, int precision)
DisplayRole
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:17:42 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.