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
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 QByteArray environ = environFile.readAll();
89#if (defined(__GNUC__) && __GNUC__ >= 12) || !defined(__GNUC__)
90 for (const QByteArrayView line : QByteArrayView(environ) | std::views::split('\0')) {
91#else
92 for (const QByteArrayView line : environ.split('\0')) {
93#endif
94 const auto equalsIdx = line.indexOf('=');
95 if (equalsIdx == -1) {
96 continue;
97 }
98
99 const QByteArrayView key = line.sliced(0, equalsIdx);
100 if (key == bamfDesktopFileHint) {
101 return QString::fromUtf8(line.sliced(equalsIdx + 1));
102 }
103 }
104 }
105
106 return QString();
107}
108
109QModelIndex Utils::mapToModel(const QModelIndex &idx, const QAbstractItemModel *sourceModel)
110{
111 // KModelIndexProxyMapper can only map different indices to a single source
112 // but we have the other way round, a single index that splits into different source models
113 QModelIndex resolvedIdx = idx;
114 while (resolvedIdx.isValid() && resolvedIdx.model() != sourceModel) {
115 if (auto *proxyModel = qobject_cast<const QAbstractProxyModel *>(resolvedIdx.model())) {
116 resolvedIdx = proxyModel->mapToSource(resolvedIdx);
117 // QConcatenateTablesProxyModel isn't a "real" proxy model, so we need to special case for it :(
118 } else if (auto *concatenateModel = qobject_cast<const QConcatenateTablesProxyModel *>(resolvedIdx.model())) {
119 resolvedIdx = concatenateModel->mapToSource(resolvedIdx);
120 } else {
121 if (resolvedIdx.model() != sourceModel) {
122 resolvedIdx = QModelIndex(); // give up
123 }
124 }
125 }
126 return resolvedIdx;
127}
128
129bool Utils::isDBusMaster()
130{
131 return qApp->property("_plasma_dbus_master").toBool();
132}
@ IdRole
A notification identifier. This can be uint notification ID or string application job source.
QString name(StandardAction id)
QList< QByteArray > split(char sep) const const
QByteArrayView sliced(qsizetype pos) const const
bool exists() const const
iterator insert(const Key &key, const T &value)
bool isEmpty() const const
QMetaEnum fromType()
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 Mon Nov 18 2024 12:14:59 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.