Changeset View
Changeset View
Standalone View
Standalone View
libnotificationmanager/notificationfilterproxymodel.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 "notificationfilterproxymodel_p.h" | ||||
22 | | ||||
23 | using namespace NotificationManager; | ||||
24 | | ||||
25 | NotificationFilterProxyModel::NotificationFilterProxyModel(QObject *parent) | ||||
26 | : QSortFilterProxyModel(parent) | ||||
27 | { | ||||
28 | setRecursiveFilteringEnabled(true); | ||||
29 | } | ||||
30 | | ||||
31 | NotificationFilterProxyModel::~NotificationFilterProxyModel() = default; | ||||
32 | | ||||
33 | Notifications::Urgencies NotificationFilterProxyModel::urgencies() const | ||||
34 | { | ||||
35 | return m_urgencies; | ||||
36 | } | ||||
37 | | ||||
38 | void NotificationFilterProxyModel::setUrgencies(Notifications::Urgencies urgencies) | ||||
39 | { | ||||
40 | if (m_urgencies != urgencies) { | ||||
41 | m_urgencies = urgencies; | ||||
42 | invalidateFilter(); | ||||
43 | emit urgenciesChanged(); | ||||
44 | } | ||||
45 | } | ||||
46 | | ||||
47 | bool NotificationFilterProxyModel::showExpired() const | ||||
48 | { | ||||
49 | return m_showExpired; | ||||
50 | } | ||||
51 | | ||||
52 | void NotificationFilterProxyModel::setShowExpired(bool show) | ||||
53 | { | ||||
54 | if (m_showExpired != show) { | ||||
55 | m_showExpired = show; | ||||
56 | invalidateFilter(); | ||||
57 | emit showExpiredChanged(); | ||||
58 | } | ||||
59 | } | ||||
60 | | ||||
61 | bool NotificationFilterProxyModel::showDismissed() const | ||||
62 | { | ||||
63 | return m_showDismissed; | ||||
64 | } | ||||
65 | | ||||
66 | void NotificationFilterProxyModel::setShowDismissed(bool show) | ||||
67 | { | ||||
68 | if (m_showDismissed != show) { | ||||
69 | m_showDismissed = show; | ||||
70 | invalidateFilter(); | ||||
71 | emit showDismissedChanged(); | ||||
72 | } | ||||
73 | } | ||||
74 | | ||||
75 | QStringList NotificationFilterProxyModel::blacklistedDesktopEntries() const | ||||
76 | { | ||||
77 | return m_blacklistedDesktopEntries; | ||||
78 | } | ||||
79 | | ||||
80 | void NotificationFilterProxyModel::setBlackListedDesktopEntries(const QStringList &blacklist) | ||||
81 | { | ||||
82 | if (m_blacklistedDesktopEntries != blacklist) { | ||||
83 | m_blacklistedDesktopEntries = blacklist; | ||||
84 | invalidateFilter(); | ||||
85 | emit blacklistedDesktopEntriesChanged(); | ||||
86 | } | ||||
87 | } | ||||
88 | | ||||
89 | QStringList NotificationFilterProxyModel::blacklistedNotifyRcNames() const | ||||
90 | { | ||||
91 | return m_blacklistedNotifyRcNames; | ||||
92 | } | ||||
93 | | ||||
94 | void NotificationFilterProxyModel::setBlacklistedNotifyRcNames(const QStringList &blacklist) | ||||
95 | { | ||||
96 | if (m_blacklistedNotifyRcNames != blacklist) { | ||||
97 | m_blacklistedNotifyRcNames = blacklist; | ||||
98 | invalidateFilter(); | ||||
99 | emit blacklistedNotifyRcNamesChanged(); | ||||
100 | } | ||||
101 | } | ||||
102 | | ||||
103 | QStringList NotificationFilterProxyModel::whitelistedDesktopEntries() const | ||||
104 | { | ||||
105 | return m_whitelistedDesktopEntries; | ||||
106 | } | ||||
107 | | ||||
108 | void NotificationFilterProxyModel::setWhiteListedDesktopEntries(const QStringList &whitelist) | ||||
109 | { | ||||
110 | if (m_whitelistedDesktopEntries != whitelist) { | ||||
111 | m_whitelistedDesktopEntries = whitelist; | ||||
112 | invalidateFilter(); | ||||
113 | emit whitelistedDesktopEntriesChanged(); | ||||
114 | } | ||||
115 | } | ||||
116 | | ||||
117 | QStringList NotificationFilterProxyModel::whitelistedNotifyRcNames() const | ||||
118 | { | ||||
119 | return m_whitelistedNotifyRcNames; | ||||
120 | } | ||||
121 | | ||||
122 | void NotificationFilterProxyModel::setWhitelistedNotifyRcNames(const QStringList &whitelist) | ||||
123 | { | ||||
124 | if (m_whitelistedNotifyRcNames != whitelist) { | ||||
125 | m_whitelistedNotifyRcNames = whitelist; | ||||
126 | invalidateFilter(); | ||||
127 | emit whitelistedNotifyRcNamesChanged(); | ||||
128 | } | ||||
129 | } | ||||
130 | | ||||
131 | bool NotificationFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const | ||||
132 | { | ||||
133 | const QModelIndex sourceIdx = sourceModel()->index(source_row, 0, source_parent); | ||||
134 | | ||||
135 | if (!m_showExpired && sourceIdx.data(Notifications::ExpiredRole).toBool()) { | ||||
136 | return false; | ||||
137 | } | ||||
138 | | ||||
139 | if (!m_showDismissed && sourceIdx.data(Notifications::DismissedRole).toBool()) { | ||||
140 | return false; | ||||
141 | } | ||||
142 | | ||||
143 | // Blacklist takes precedence over whitelist, i.e. when in doubt don't show | ||||
144 | if (!m_blacklistedDesktopEntries.isEmpty()) { | ||||
145 | const QString desktopEntry = sourceIdx.data(Notifications::DesktopEntryRole).toString(); | ||||
146 | if (!desktopEntry.isEmpty() && m_blacklistedDesktopEntries.contains(desktopEntry)) { | ||||
147 | return false; | ||||
148 | } | ||||
149 | } | ||||
150 | | ||||
151 | if (!m_blacklistedNotifyRcNames.isEmpty()) { | ||||
152 | const QString notifyRcName = sourceIdx.data(Notifications::NotifyRcNameRole).toString(); | ||||
153 | if (!notifyRcName.isEmpty() && m_blacklistedNotifyRcNames.contains(notifyRcName)) { | ||||
154 | return false; | ||||
155 | } | ||||
156 | } | ||||
157 | | ||||
158 | if (!m_whitelistedDesktopEntries.isEmpty()) { | ||||
159 | const QString desktopEntry = sourceIdx.data(Notifications::DesktopEntryRole).toString(); | ||||
160 | if (!desktopEntry.isEmpty() && m_whitelistedDesktopEntries.contains(desktopEntry)) { | ||||
161 | return true; | ||||
162 | } | ||||
163 | } | ||||
164 | | ||||
165 | if (!m_whitelistedNotifyRcNames.isEmpty()) { | ||||
166 | const QString notifyRcName = sourceIdx.data(Notifications::NotifyRcNameRole).toString(); | ||||
167 | if (!notifyRcName.isEmpty() && m_whitelistedNotifyRcNames.contains(notifyRcName)) { | ||||
168 | return true; | ||||
169 | } | ||||
170 | } | ||||
171 | | ||||
172 | bool ok; | ||||
173 | const auto urgency = static_cast<Notifications::Urgency>(sourceIdx.data(Notifications::UrgencyRole).toInt(&ok)); | ||||
174 | if (ok) { | ||||
175 | if (!m_urgencies.testFlag(urgency)) { | ||||
176 | return false; | ||||
177 | } | ||||
178 | } | ||||
179 | | ||||
180 | return true; | ||||
181 | } |