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 | 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; | ||||
47 | procs.updateOrAddProcess(pid); | ||||
48 | | ||||
49 | KSysGuard::Process *proc = procs.getProcess(pid); | ||||
50 | | ||||
51 | if (!proc) { | ||||
52 | return QString(); | ||||
53 | } | ||||
54 | | ||||
55 | return proc->name(); | ||||
56 | } | ||||
57 | | ||||
58 | QModelIndex Utils::mapToModel(const QModelIndex &idx, const QAbstractItemModel *sourceModel) | ||||
59 | { | ||||
60 | // 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 | ||||
62 | QModelIndex resolvedIdx = idx; | ||||
63 | while (resolvedIdx.isValid() && resolvedIdx.model() != sourceModel) { | ||||
64 | if (auto *proxyModel = qobject_cast<const QAbstractProxyModel *>(resolvedIdx.model())) { | ||||
65 | resolvedIdx = proxyModel->mapToSource(resolvedIdx); | ||||
66 | // KConcatenateRowsProxyModel isn't a "real" proxy model, so we need to special case for it :( | ||||
67 | } else if (auto *concatenateModel = qobject_cast<const KConcatenateRowsProxyModel *>(resolvedIdx.model())) { | ||||
68 | resolvedIdx = concatenateModel->mapToSource(resolvedIdx); | ||||
69 | } else { | ||||
70 | if (resolvedIdx.model() != sourceModel) { | ||||
71 | resolvedIdx = QModelIndex(); // give up | ||||
72 | } | ||||
73 | } | ||||
74 | } | ||||
75 | return resolvedIdx; | ||||
76 | } |