Plasma-workspace

notificationfilterproxymodel.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 "notificationfilterproxymodel_p.h"
8
9using namespace NotificationManager;
10
11NotificationFilterProxyModel::NotificationFilterProxyModel(QObject *parent)
12 : QSortFilterProxyModel(parent)
13{
14 setRecursiveFilteringEnabled(true);
15}
16
17NotificationFilterProxyModel::~NotificationFilterProxyModel() = default;
18
19Notifications::Urgencies NotificationFilterProxyModel::urgencies() const
20{
21 return m_urgencies;
22}
23
24void NotificationFilterProxyModel::setUrgencies(Notifications::Urgencies urgencies)
25{
26 if (m_urgencies != urgencies) {
27 m_urgencies = urgencies;
28 invalidateFilter();
29 Q_EMIT urgenciesChanged();
30 }
31}
32
33bool NotificationFilterProxyModel::showExpired() const
34{
35 return m_showExpired;
36}
37
38void NotificationFilterProxyModel::setShowExpired(bool show)
39{
40 if (m_showExpired != show) {
41 m_showExpired = show;
42 invalidateFilter();
43 Q_EMIT showExpiredChanged();
44 }
45}
46
47bool NotificationFilterProxyModel::showDismissed() const
48{
49 return m_showDismissed;
50}
51
52void NotificationFilterProxyModel::setShowDismissed(bool show)
53{
54 if (m_showDismissed != show) {
55 m_showDismissed = show;
56 invalidateFilter();
57 Q_EMIT showDismissedChanged();
58 }
59}
60
61bool NotificationFilterProxyModel::showAddedDuringInhibition() const
62{
63 return m_showDismissed;
64}
65
66void NotificationFilterProxyModel::setShowAddedDuringInhibition(bool show)
67{
68 if (m_showAddedDuringInhibition != show) {
69 m_showAddedDuringInhibition = show;
70 invalidateFilter();
71 Q_EMIT showAddedDuringInhibitionChanged();
72 }
73}
74
75QStringList NotificationFilterProxyModel::blacklistedDesktopEntries() const
76{
77 return m_blacklistedDesktopEntries;
78}
79
80void NotificationFilterProxyModel::setBlackListedDesktopEntries(const QStringList &blacklist)
81{
82 if (m_blacklistedDesktopEntries != blacklist) {
83 m_blacklistedDesktopEntries = blacklist;
84 invalidateFilter();
85 Q_EMIT blacklistedDesktopEntriesChanged();
86 }
87}
88
89QStringList NotificationFilterProxyModel::blacklistedNotifyRcNames() const
90{
91 return m_blacklistedNotifyRcNames;
92}
93
94void NotificationFilterProxyModel::setBlacklistedNotifyRcNames(const QStringList &blacklist)
95{
96 if (m_blacklistedNotifyRcNames != blacklist) {
97 m_blacklistedNotifyRcNames = blacklist;
98 invalidateFilter();
99 Q_EMIT blacklistedNotifyRcNamesChanged();
100 }
101}
102
103QStringList NotificationFilterProxyModel::whitelistedDesktopEntries() const
104{
105 return m_whitelistedDesktopEntries;
106}
107
108void NotificationFilterProxyModel::setWhiteListedDesktopEntries(const QStringList &whitelist)
109{
110 if (m_whitelistedDesktopEntries != whitelist) {
111 m_whitelistedDesktopEntries = whitelist;
112 invalidateFilter();
113 Q_EMIT whitelistedDesktopEntriesChanged();
114 }
115}
116
117QStringList NotificationFilterProxyModel::whitelistedNotifyRcNames() const
118{
119 return m_whitelistedNotifyRcNames;
120}
121
122void NotificationFilterProxyModel::setWhitelistedNotifyRcNames(const QStringList &whitelist)
123{
124 if (m_whitelistedNotifyRcNames != whitelist) {
125 m_whitelistedNotifyRcNames = whitelist;
126 invalidateFilter();
127 Q_EMIT whitelistedNotifyRcNamesChanged();
128 }
129}
130
131bool NotificationFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
132{
133 const QModelIndex sourceIdx = sourceModel()->index(source_row, 0, source_parent);
134
135 const bool expired = sourceIdx.data(Notifications::ExpiredRole).toBool();
136 if (!m_showExpired && expired) {
137 return false;
138 }
139
140 if (!m_showDismissed && sourceIdx.data(Notifications::DismissedRole).toBool()) {
141 return false;
142 }
143
144 QString desktopEntry = sourceIdx.data(Notifications::DesktopEntryRole).toString();
145 if (desktopEntry.isEmpty()) {
146 // For non-configurable notifications use the fake "@other" category.
148 // jobs are never configurable so this only applies to notifications
150 desktopEntry = QStringLiteral("@other");
151 }
152 }
153
154 // Blacklist takes precedence over whitelist, i.e. when in doubt don't show
155 if (!m_blacklistedDesktopEntries.isEmpty()) {
156 if (!desktopEntry.isEmpty() && m_blacklistedDesktopEntries.contains(desktopEntry)) {
157 return false;
158 }
159 }
160
161 if (!m_blacklistedNotifyRcNames.isEmpty()) {
162 const QString notifyRcName = sourceIdx.data(Notifications::NotifyRcNameRole).toString();
163 if (!notifyRcName.isEmpty() && m_blacklistedNotifyRcNames.contains(notifyRcName)) {
164 return false;
165 }
166 }
167
168 if (!m_whitelistedDesktopEntries.isEmpty()) {
169 if (!desktopEntry.isEmpty() && m_whitelistedDesktopEntries.contains(desktopEntry)) {
170 return true;
171 }
172 }
173
174 if (!m_whitelistedNotifyRcNames.isEmpty()) {
175 const QString notifyRcName = sourceIdx.data(Notifications::NotifyRcNameRole).toString();
176 if (!notifyRcName.isEmpty() && m_whitelistedNotifyRcNames.contains(notifyRcName)) {
177 return true;
178 }
179 }
180
181 const bool userActionFeedback = sourceIdx.data(Notifications::UserActionFeedbackRole).toBool();
182 if (userActionFeedback) {
183 return true;
184 }
185
186 bool ok;
187 const auto urgency = static_cast<Notifications::Urgency>(sourceIdx.data(Notifications::UrgencyRole).toInt(&ok));
188 if (ok) {
189 if (!m_urgencies.testFlag(urgency)) {
190 return false;
191 }
192 }
193
194 if (!m_showAddedDuringInhibition && sourceIdx.data(Notifications::WasAddedDuringInhibitionRole).toBool()) {
195 return false;
196 }
197
198 return true;
199}
200
201#include "moc_notificationfilterproxymodel_p.cpp"
@ NotificationType
This item represents a notification.
Urgency
The notification urgency.
@ ConfigurableRole
Whether the notification can be configured because a desktopEntry or notifyRcName is known,...
@ NotifyRcNameRole
The notifyrc name (e.g. spectaclerc) of the application that sent the notification.
@ DismissedRole
The notification got temporarily hidden by the user but could still be interacted with.
@ DesktopEntryRole
The desktop entry (without .desktop suffix, e.g. org.kde.spectacle) of the application that sent the ...
@ WasAddedDuringInhibitionRole
Whether the notification was added while inhibition was active.
@ ExpiredRole
The notification timed out and closed. Actions on it cannot be invoked anymore.
@ UserActionFeedbackRole
Whether this notification is a response/confirmation to an explicit user action.
@ UrgencyRole
The notification urgency, either LowUrgency, NormalUrgency, or CriticalUrgency. Jobs do not have an u...
@ TypeRole
The type of model entry, either NotificationType or JobType.
QVariant data(int role) const const
bool isEmpty() const const
bool toBool() const const
int toInt(bool *ok) const const
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 31 2025 12:05:31 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.