Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,50 @@ menu - Preferences - LXQt Settings - Desktop Notifications and is provided by th
[Configuration Center](https://github.com/lxqt/lxqt-config#configuration-center)
of [lxqt-config](https://github.com/lxqt/lxqt-config) as well.

### Filtering
It is possible to filter/suppress notifications with a [PCRE](https://www.pcre.org) by setting the
`application_pcre_filter`, `body_pcre_filter`, or `summary_pcre_filter` in the
`lxqt/notifications.conf`. These variables can be set with any valid PCRE with
each section being checked against its respective PCRE filter. If any of the
PCREs are captured in the application, body, or summary then the message will
not be shown. The following example `lxqt/notifications.conf` any notification with the
summary containing the words "hello", "goodbye", or "nope" will not be shown:
```text
[General]
...
application_pcre_filter=
body_pcre_filter=
summary_pcre_filter=(?:hello|goodbye|nope)
...
```
https://regex101.com/r/nhtRtx/1

#### Filtering Examples
Filter/block all notifications from the applications: `noisy_app1`,
`noisy_app2`, and `noisy_app3`.
Add the following to lxqt/notification.conf:
```text
[General]
...
applicaiton_pcre_filter=(?:noisy_app1|noisy_app2|noisy_app3)
body_pcre_filter=
summary_pcre_filter=
...
```

Filter/block all notifications containing the strings: `This is a test` and
`This is another test`
```text
[General]
...
applicaiton_pcre_filter=
body_pcre_filter=
summary_pcre_filter=^(?=.*(?:This is a test|This is another test)).*$
...
```
https://regex101.com/r/thW0qo/1


## Translations

Translations can be done in [LXQt-Weblate](https://translate.lxqt-project.org/projects/lxqt-configuration/lxqt-notificationd)
Expand Down
34 changes: 33 additions & 1 deletion src/notificationlayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <QBrush>
#include <QSettings>
#include <QStandardPaths>
#include <QRegularExpression>

NotificationLayout::NotificationLayout(QWidget *parent)
: QWidget(parent),
Expand Down Expand Up @@ -71,16 +72,47 @@ void NotificationLayout::setSizes(int space, int width)
}
}

void NotificationLayout::setPCREFilter(QRegularExpression& re, const QString& pattern) {
re.setPattern(pattern);
qWarning() << "Unable to compile regexp from pattern " << pattern << ", error: "
<< re.errorString() << ", at position " << re.patternErrorOffset();
}

bool NotificationLayout::filter(const QString& input, const QRegularExpression& re) {
if (re.pattern().isEmpty() || !re.isValid())
return false;

return re.match(input).hasMatch();
}

void NotificationLayout::addNotification(uint id, const QString &application,
const QString &summary, const QString &body,
const QString &icon, int timeout,
const QStringList& actions, const QVariantMap& hints,
bool noSave)
{
// qDebug() << "NotificationLayout::addNotification" << id << application << summary << body << icon << timeout;

bool applicationPCRECaptured = filter(application, m_application_pcre_filter);
bool bodyPCRECaptured = filter(summary, m_body_pcre_filter);
bool summaryPCRECaptured = filter(summary, m_summary_pcre_filter);
qInfo() << "New notification: ";
qInfo() << " application: " << application;
qInfo() << " application PCRE: " << m_application_pcre_filter;
qInfo() << " application PCRE captured: " << QString::fromStdString( (applicationPCRECaptured ? "true" : "false"));
qInfo() << " body: " << body;
qInfo() << " body PCRE: " << m_body_pcre_filter;
qInfo() << " body PCRE captured: " << QString::fromStdString( (bodyPCRECaptured ? "true" : "false"));
qInfo() << " summary: " << summary;
qInfo() << " summary PCRE: " << m_summary_pcre_filter;
qInfo() << " summary PCRE captured: " << QString::fromStdString( (summaryPCRECaptured ? "true" : "false"));

bool showNotification(!m_doNotDisturb
// always show our test notifications
|| application == QL1S("lxqt-config-notificationd"));
showNotification = showNotification && !applicationPCRECaptured;
showNotification = showNotification && !bodyPCRECaptured;
showNotification = showNotification && !summaryPCRECaptured;
qInfo() << " showNotification: " << QString::fromStdString( (showNotification ? "true" : "false"));
if (m_notifications.contains(id))
{
// TODO/FIXME: it can be deleted by timer in this block. Locking?
Expand Down
20 changes: 20 additions & 0 deletions src/notificationlayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#define NotificationLayout_H

#include "notification.h"
#include <QRegularExpression>


class NotificationLayout : public QWidget
Expand All @@ -54,6 +55,20 @@ class NotificationLayout : public QWidget
m_doNotDisturb = value;
}

void setPCREFilter(QRegularExpression& re, const QString& pattern);

void setApplicationPCREFilter(const QString& pcre) {
setPCREFilter(m_application_pcre_filter, pcre);
}

void setBodyPCREFilter(const QString& pcre) {
setPCREFilter(m_body_pcre_filter, pcre);
}

void setSummaryPCREFilter(const QString& pcre) {
setPCREFilter(m_summary_pcre_filter, pcre);
}

void setBlackList(const QStringList &l) {
m_blackList = l;
}
Expand Down Expand Up @@ -107,6 +122,9 @@ public slots:
QVBoxLayout *m_layout;
int m_unattendedMaxNum;
bool m_doNotDisturb;
QRegularExpression m_application_pcre_filter;
QRegularExpression m_body_pcre_filter;
QRegularExpression m_summary_pcre_filter;
QStringList m_blackList;
QString m_cacheFile;
QString m_cacheDateFormat;
Expand All @@ -117,6 +135,8 @@ public slots:
*/
void checkHeight();

bool filter(const QString& input, const QRegularExpression& re);

private slots:
/*! \c Notification's timer timeouted, so closing the notifiaction
*/
Expand Down
3 changes: 3 additions & 0 deletions src/notifyd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ void Notifyd::reloadSettings()
m_settings->value(QSL("screenWithMouse"),false).toBool(),
m_doNotDisturb ? QStringList() : m_settings->value(QSL("blackList")).toStringList());
m_area->layout()->setDoNotDisturb(m_doNotDisturb);
m_area->layout()->setApplicationPCREFilter(m_settings->value(QL1S("application_pcre_filter")).toString());
m_area->layout()->setBodyPCREFilter(m_settings->value(QL1S("body_pcre_filter")).toString());
m_area->layout()->setSummaryPCREFilter(m_settings->value(QL1S("summary_pcre_filter")).toString());

if (m_trayIcon.isNull())
{
Expand Down