Changeset View
Changeset View
Standalone View
Standalone View
libnotificationmanager/utils.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright 2019 Kai Uwe Broulik <kde@privat.broulik.de> | ||||
3 | * | ||||
4 | * This library is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU Lesser General Public | ||||
6 | * License as published by the Free Software Foundation; either | ||||
7 | * version 2.1 of the License, or (at your option) version 3, or any | ||||
8 | * later version accepted by the membership of KDE e.V. (or its | ||||
9 | * successor approved by the membership of KDE e.V.), which shall | ||||
10 | * act as a proxy defined in Section 6 of version 3 of the license. | ||||
11 | * | ||||
12 | * This library is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
15 | * Lesser General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU Lesser General Public | ||||
18 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | #include "utils_p.h" | ||||
22 | | ||||
23 | #include <QAbstractItemModel> | ||||
24 | #include <QAbstractProxyModel> | ||||
25 | #include <QDBusConnection> | ||||
26 | #include <QDBusConnectionInterface> | ||||
27 | | ||||
28 | #include <KConcatenateRowsProxyModel> | ||||
29 | | ||||
30 | #include <QDebug> | ||||
31 | | ||||
32 | #include <processcore/processes.h> | ||||
33 | #include <processcore/process.h> | ||||
34 | | ||||
35 | using namespace NotificationManager; | ||||
36 | | ||||
37 | QString Utils::processNameFromDBusService(const QDBusConnection &connection, const QString &serviceName) | ||||
38 | { | ||||
39 | qDebug() << "gimme service pid for" << serviceName; | ||||
40 | QDBusReply<uint> pidReply = connection.interface()->servicePid(serviceName); | ||||
41 | if (!pidReply.isValid()) { | ||||
42 | return QString(); | ||||
43 | } | ||||
44 | | ||||
45 | const auto pid = pidReply.value(); | ||||
46 | qDebug() << "PIDINHIER" << pid; | ||||
47 | | ||||
48 | KSysGuard::Processes procs; | ||||
49 | procs.updateOrAddProcess(pid); | ||||
50 | | ||||
51 | KSysGuard::Process *proc = procs.getProcess(pid); | ||||
52 | | ||||
53 | if (!proc) { | ||||
54 | return QString(); | ||||
55 | } | ||||
56 | | ||||
57 | return proc->name(); | ||||
58 | } | ||||
59 | | ||||
60 | QModelIndex Utils::mapToModel(const QModelIndex &idx, const QAbstractItemModel *sourceModel) | ||||
61 | { | ||||
62 | // KModelIndexProxyMapper can only map diferent indices to a single source | ||||
63 | // but we have the other way round, a single index that splits into different source models | ||||
64 | qDebug() << "map to model" << idx << sourceModel; | ||||
65 | QModelIndex resolvedIdx = idx; | ||||
66 | while (resolvedIdx.isValid() && resolvedIdx.model() != sourceModel) { | ||||
67 | if (auto *proxyModel = qobject_cast<const QAbstractProxyModel *>(resolvedIdx.model())) { | ||||
68 | resolvedIdx = proxyModel->mapToSource(resolvedIdx); | ||||
69 | // KConcatenateRowsProxyModel isn't a "real" proxy model, so we need to special case for it :( | ||||
70 | } else if (auto *concatenateModel = qobject_cast<const KConcatenateRowsProxyModel *>(resolvedIdx.model())) { | ||||
71 | resolvedIdx = concatenateModel->mapToSource(resolvedIdx); | ||||
72 | } else { | ||||
73 | if (resolvedIdx.model() != sourceModel) { | ||||
74 | resolvedIdx = QModelIndex(); // give up | ||||
75 | } | ||||
76 | } | ||||
77 | } | ||||
78 | return resolvedIdx; | ||||
79 | } |