diff --git a/applets/notifications/CMakeLists.txt b/applets/notifications/CMakeLists.txt --- a/applets/notifications/CMakeLists.txt +++ b/applets/notifications/CMakeLists.txt @@ -3,6 +3,7 @@ set(notificationapplet_SRCS notificationapplet.cpp filemenu.cpp + globalshortcuts.cpp thumbnailer.cpp ) @@ -16,6 +17,7 @@ KF5::I18n KF5::Plasma KF5::PlasmaQuick + KF5::GlobalAccel KF5::KIOWidgets # for PreviewJob ) diff --git a/applets/notifications/globalshortcuts.cpp b/applets/notifications/globalshortcuts.cpp new file mode 100644 --- /dev/null +++ b/applets/notifications/globalshortcuts.cpp @@ -0,0 +1,63 @@ +/* + * Copyright 2019 Kai Uwe Broulik + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 3 of + * the License or any later version accepted by the membership of + * KDE e.V. (or its successor approved by the membership of KDE + * e.V.), which shall act as a proxy defined in Section 14 of + * version 3 of the license. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "globalshortcuts.h" + +#include +#include +#include + +#include + +#include + +GlobalShortcuts::GlobalShortcuts(QObject *parent) + : QObject(parent) + , m_toggleDoNotDisturbAction(new QAction(this)) +{ + m_toggleDoNotDisturbAction->setObjectName(QStringLiteral("toggle do not disturb")); + m_toggleDoNotDisturbAction->setProperty("componentName", QStringLiteral("plasmashell")); + m_toggleDoNotDisturbAction->setText(i18n("Toggle do not disturb")); + m_toggleDoNotDisturbAction->setIcon(QIcon::fromTheme(QStringLiteral("notifications-disabled"))); + m_toggleDoNotDisturbAction->setShortcutContext(Qt::ApplicationShortcut); + connect(m_toggleDoNotDisturbAction, &QAction::triggered, this, &GlobalShortcuts::toggleDoNotDisturbTriggered); + + KGlobalAccel::self()->setGlobalShortcut(m_toggleDoNotDisturbAction, QKeySequence()); +} + +GlobalShortcuts::~GlobalShortcuts() = default; + +void GlobalShortcuts::showDoNotDisturbOsd(bool doNotDisturb) const +{ + QDBusMessage msg = QDBusMessage::createMethodCall( + QStringLiteral("org.kde.plasmashell"), + QStringLiteral("/org/kde/osdService"), + QStringLiteral("org.kde.osdService"), + QStringLiteral("showText") + ); + + const QString iconName = doNotDisturb ? QStringLiteral("notifications-disabled") : QStringLiteral("notifications"); + const QString text = doNotDisturb ? i18nc("OSD popup, keep short", "Notifications Off") + : i18nc("OSD popup, keep short", "Notifications On"); + + msg.setArguments({iconName, text}); + + QDBusConnection::sessionBus().call(msg, QDBus::NoBlock); +} diff --git a/applets/notifications/notificationapplet.cpp b/applets/notifications/notificationapplet.cpp --- a/applets/notifications/notificationapplet.cpp +++ b/applets/notifications/notificationapplet.cpp @@ -33,6 +33,7 @@ #include #include "filemenu.h" +#include "globalshortcuts.h" #include "thumbnailer.h" NotificationApplet::NotificationApplet(QObject *parent, const QVariantList &data) @@ -42,6 +43,7 @@ if (!s_typesRegistered) { const char uri[] = "org.kde.plasma.private.notifications"; qmlRegisterType(uri, 2, 0, "FileMenu"); + qmlRegisterType(uri, 2, 0, "GlobalShortcuts"); qmlRegisterType(uri, 2, 0, "Thumbnailer"); qmlProtectModule(uri, 2); s_typesRegistered = true; diff --git a/applets/notifications/package/contents/ui/FullRepresentation.qml b/applets/notifications/package/contents/ui/FullRepresentation.qml --- a/applets/notifications/package/contents/ui/FullRepresentation.qml +++ b/applets/notifications/package/contents/ui/FullRepresentation.qml @@ -99,12 +99,7 @@ // but disable only on click onClicked: { if (Globals.inhibited) { - notificationSettings.notificationsInhibitedUntil = undefined; - notificationSettings.revokeApplicationInhibitions(); - // overrules current mirrored screen setup, updates again when screen configuration changes - notificationSettings.screensMirrored = false; - - notificationSettings.save(); + Globals.revokeInhibitions(); } } diff --git a/applets/notifications/package/contents/ui/global/Globals.qml b/applets/notifications/package/contents/ui/global/Globals.qml --- a/applets/notifications/package/contents/ui/global/Globals.qml +++ b/applets/notifications/package/contents/ui/global/Globals.qml @@ -29,6 +29,8 @@ import org.kde.notificationmanager 1.0 as NotificationManager +import org.kde.plasma.private.notifications 2.0 as Notifications + import ".." // This singleton object contains stuff shared between all notification plasmoids, namely: @@ -215,6 +217,15 @@ }); } + function revokeInhibitions() { + notificationSettings.notificationsInhibitedUntil = undefined; + notificationSettings.revokeApplicationInhibitions(); + // overrules current mirrored screen setup, updates again when screen configuration changes + notificationSettings.screensMirrored = false; + + notificationSettings.save(); + } + function rectIntersect(rect1 /*dialog*/, rect2 /*popup*/) { return rect1.x < rect2.x + rect2.width && rect2.x < rect1.x + rect1.width @@ -476,4 +487,21 @@ property: "inhibited" value: globals.inhibited } + + property Notifications.GlobalShortcuts shortcuts: Notifications.GlobalShortcuts { + onToggleDoNotDisturbTriggered: { + if (globals.inhibited) { + globals.revokeInhibitions(); + } else { + // Effectively "in a year" is "until turned off" + var d = new Date(); + d.setFullYear(d.getFullYear() + 1); + notificationSettings.notificationsInhibitedUntil = d; + notificationSettings.save(); + } + + checkInhibition(); + showDoNotDisturbOsd(globals.inhibited); + } + } }