Changeset View
Changeset View
Standalone View
Standalone View
applets/systemtray/entrymodel.cpp
- This file was added.
1 | /** | ||||
---|---|---|---|---|---|
2 | * Copyright 2019 Nicolas Fella <nicolas.fella@gmx.de> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU General Public License as | ||||
6 | * published by the Free Software Foundation; either version 2 of | ||||
7 | * the License or (at your option) version 3 or any later version | ||||
8 | * accepted by the membership of KDE e.V. (or its successor approved | ||||
9 | * by the membership of KDE e.V.), which shall act as a proxy | ||||
10 | * defined in Section 14 of version 3 of the license. | ||||
11 | * | ||||
12 | * This program 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 | ||||
15 | * GNU General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU General Public License | ||||
18 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | #include "entrymodel.h" | ||||
22 | | ||||
23 | #include <PluginLoader> | ||||
24 | #include <KPluginMetaData> | ||||
25 | #include <Plasma/DataEngineConsumer> | ||||
26 | #include <Plasma/DataEngine> | ||||
27 | #include <Plasma/DataContainer> | ||||
28 | | ||||
29 | EntryModel::EntryModel(SystemTray *systemTray) | ||||
30 | : QAbstractListModel(systemTray) | ||||
31 | , m_entries() | ||||
32 | { | ||||
33 | // load plasmoids | ||||
34 | for (const auto &info: Plasma::PluginLoader::self()->listAppletMetaData(QString())) { | ||||
35 | if (!info.isValid() || info.value(QStringLiteral("X-Plasma-NotificationArea")) != "true") { | ||||
36 | continue; | ||||
37 | } | ||||
38 | Entry e; | ||||
39 | e.name = info.name(); | ||||
40 | e.iconName = info.iconName(); | ||||
41 | e.taskId = info.pluginId(); | ||||
42 | e.isPlasmoid = true; | ||||
43 | | ||||
44 | const auto applets = systemTray->applets(); | ||||
45 | for (Plasma::Applet *applet : applets) { | ||||
46 | if (applet->pluginInfo().name() == info.name()) { | ||||
47 | e.applet = applet; | ||||
48 | } | ||||
49 | } | ||||
50 | m_entries.append(e); | ||||
51 | } | ||||
52 | | ||||
53 | // Load statusnotifieritems | ||||
54 | Plasma::DataEngineConsumer consumer; | ||||
55 | Plasma::DataEngine *eng = consumer.dataEngine(QStringLiteral("statusnotifieritem")); | ||||
56 | | ||||
57 | const auto sources = eng->sources(); | ||||
58 | for (const QString source : sources) { | ||||
59 | Plasma::DataContainer *container = eng->containerForSource(source); | ||||
60 | Entry e; | ||||
61 | e.name = container->data().value(QStringLiteral("Title")).toString(); | ||||
62 | e.taskId = container->data().value(QStringLiteral("Id")).toString(); | ||||
63 | e.iconName = container->data().value(QStringLiteral("IconName")).toString(); | ||||
64 | e.icon = container->data().value(QStringLiteral("Icon")); | ||||
65 | e.isPlasmoid = false; | ||||
66 | m_entries.append(e); | ||||
67 | } | ||||
68 | } | ||||
69 | | ||||
70 | QHash<int, QByteArray> EntryModel::roleNames() const | ||||
71 | { | ||||
72 | //Role names for QML | ||||
73 | QHash<int, QByteArray> names = QAbstractItemModel::roleNames(); | ||||
74 | names.insert(AppletRole, "applet"); | ||||
75 | names.insert(TaskIdRole, "taskId"); | ||||
76 | names.insert(NameRole, "name"); | ||||
77 | names.insert(IconNameRole, "iconName"); | ||||
78 | names.insert(ShortcutRole, "shortcut"); | ||||
79 | names.insert(IconRole, "icon"); | ||||
80 | names.insert(IsPlasmoidRole, "isPlasmoid"); | ||||
81 | | ||||
82 | return names; | ||||
83 | } | ||||
84 | | ||||
85 | EntryModel::~EntryModel() | ||||
86 | { | ||||
87 | } | ||||
88 | | ||||
89 | QVariant EntryModel::data(const QModelIndex& index, int role) const | ||||
90 | { | ||||
91 | if (!index.isValid() | ||||
92 | || index.row() < 0 | ||||
93 | || index.row() >= rowCount()) | ||||
94 | { | ||||
95 | return QVariant(); | ||||
96 | } | ||||
97 | | ||||
98 | switch (role) { | ||||
99 | case NameRole: | ||||
100 | return m_entries[index.row()].name; | ||||
101 | case TaskIdRole: | ||||
102 | return m_entries[index.row()].taskId; | ||||
103 | case IconNameRole: | ||||
104 | return m_entries[index.row()].iconName; | ||||
105 | case IconRole: | ||||
106 | return m_entries[index.row()].icon; | ||||
107 | case IsPlasmoidRole: | ||||
108 | return m_entries[index.row()].isPlasmoid; | ||||
109 | case ShortcutRole: | ||||
110 | return m_entries[index.row()].applet->globalShortcut(); | ||||
111 | default: | ||||
112 | return QVariant(); | ||||
113 | } | ||||
114 | } | ||||
115 | | ||||
116 | int EntryModel::rowCount(const QModelIndex& parent) const | ||||
117 | { | ||||
118 | if (parent.isValid()) { | ||||
119 | //Return size 0 if we are a child because this is not a tree | ||||
120 | return 0; | ||||
121 | } | ||||
122 | | ||||
123 | return m_entries.size(); | ||||
124 | } | ||||
125 | | ||||
126 | bool EntryModel::setData(const QModelIndex &index, const QVariant &value, int role) { | ||||
127 | | ||||
128 | if (role == ShortcutRole) { | ||||
129 | m_entries[index.row()].applet->setGlobalShortcut(value.value<QKeySequence>()); | ||||
130 | } | ||||
131 | | ||||
132 | return true; | ||||
133 | } |