Changeset View
Changeset View
Standalone View
Standalone View
libnotificationmanager/utils.cpp
Show All 18 Lines | |||||
19 | */ | 19 | */ | ||
20 | 20 | | |||
21 | #include "utils_p.h" | 21 | #include "utils_p.h" | ||
22 | 22 | | |||
23 | #include <QAbstractItemModel> | 23 | #include <QAbstractItemModel> | ||
24 | #include <QAbstractProxyModel> | 24 | #include <QAbstractProxyModel> | ||
25 | #include <QDBusConnection> | 25 | #include <QDBusConnection> | ||
26 | #include <QDBusConnectionInterface> | 26 | #include <QDBusConnectionInterface> | ||
27 | #include <QFile> | ||||
28 | #include <QTextStream> | ||||
27 | 29 | | |||
28 | #include <KConcatenateRowsProxyModel> | 30 | #include <KConcatenateRowsProxyModel> | ||
29 | 31 | | |||
30 | #include <QDebug> | | |||
31 | | ||||
32 | #include <processcore/processes.h> | 32 | #include <processcore/processes.h> | ||
33 | #include <processcore/process.h> | 33 | #include <processcore/process.h> | ||
34 | 34 | | |||
35 | using namespace NotificationManager; | 35 | using namespace NotificationManager; | ||
36 | 36 | | |||
37 | QString Utils::processNameFromDBusService(const QDBusConnection &connection, const QString &serviceName) | 37 | QString Utils::processNameFromPid(uint pid) | ||
38 | { | 38 | { | ||
39 | QDBusReply<uint> pidReply = connection.interface()->servicePid(serviceName); | | |||
40 | if (!pidReply.isValid()) { | | |||
41 | return QString(); | | |||
42 | } | | |||
43 | | ||||
44 | const auto pid = pidReply.value(); | | |||
45 | | ||||
46 | KSysGuard::Processes procs; | 39 | KSysGuard::Processes procs; | ||
47 | procs.updateOrAddProcess(pid); | 40 | procs.updateOrAddProcess(pid); | ||
48 | 41 | | |||
49 | KSysGuard::Process *proc = procs.getProcess(pid); | 42 | KSysGuard::Process *proc = procs.getProcess(pid); | ||
50 | 43 | | |||
51 | if (!proc) { | 44 | if (!proc) { | ||
52 | return QString(); | 45 | return QString(); | ||
53 | } | 46 | } | ||
54 | 47 | | |||
55 | return proc->name(); | 48 | return proc->name(); | ||
56 | } | 49 | } | ||
57 | 50 | | |||
51 | QString Utils::desktopEntryFromPid(uint pid) | ||||
52 | { | ||||
53 | QFile environFile(QStringLiteral("/proc/%1/environ").arg(QString::number(pid))); | ||||
54 | if (!environFile.open(QIODevice::ReadOnly | QIODevice::Text)) { | ||||
55 | return QString(); | ||||
56 | } | ||||
57 | | ||||
58 | const QByteArray bamfDesktopFileHint = QByteArrayLiteral("BAMF_DESKTOP_FILE_HINT"); | ||||
59 | | ||||
60 | const auto lines = environFile.readAll().split('\0'); | ||||
61 | for (const QByteArray &line : lines) { | ||||
62 | const int equalsIdx = line.indexOf('='); | ||||
63 | if (equalsIdx <= 0) { | ||||
64 | continue; | ||||
65 | } | ||||
66 | | ||||
67 | const QByteArray key = line.left(equalsIdx); | ||||
68 | const QByteArray value = line.mid(equalsIdx + 1); | ||||
69 | if (key == bamfDesktopFileHint) { | ||||
70 | return value; | ||||
71 | } | ||||
72 | } | ||||
73 | | ||||
74 | return QString(); | ||||
75 | } | ||||
76 | | ||||
58 | QModelIndex Utils::mapToModel(const QModelIndex &idx, const QAbstractItemModel *sourceModel) | 77 | QModelIndex Utils::mapToModel(const QModelIndex &idx, const QAbstractItemModel *sourceModel) | ||
59 | { | 78 | { | ||
60 | // KModelIndexProxyMapper can only map diferent indices to a single source | 79 | // KModelIndexProxyMapper can only map diferent indices to a single source | ||
61 | // but we have the other way round, a single index that splits into different source models | 80 | // but we have the other way round, a single index that splits into different source models | ||
62 | QModelIndex resolvedIdx = idx; | 81 | QModelIndex resolvedIdx = idx; | ||
63 | while (resolvedIdx.isValid() && resolvedIdx.model() != sourceModel) { | 82 | while (resolvedIdx.isValid() && resolvedIdx.model() != sourceModel) { | ||
64 | if (auto *proxyModel = qobject_cast<const QAbstractProxyModel *>(resolvedIdx.model())) { | 83 | if (auto *proxyModel = qobject_cast<const QAbstractProxyModel *>(resolvedIdx.model())) { | ||
65 | resolvedIdx = proxyModel->mapToSource(resolvedIdx); | 84 | resolvedIdx = proxyModel->mapToSource(resolvedIdx); | ||
Show All 11 Lines |