File indexing completed on 2024-05-12 04:45:34

0001 /*
0002     SnoreNotify is a Notification Framework based on Qt
0003     Copyright (C) 2013-2015  Hannah von Reth <vonreth@kde.org>
0004 
0005     SnoreNotify is free software: you can redistribute it and/or modify
0006     it under the terms of the GNU Lesser General Public License as published by
0007     the Free Software Foundation, either version 3 of the License, or
0008     (at your option) any later version.
0009 
0010     SnoreNotify is distributed in the hope that it will be useful,
0011     but WITHOUT ANY WARRANTY; without even the implied warranty of
0012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013     GNU Lesser General Public License for more details.
0014 
0015     You should have received a copy of the GNU Lesser General Public License
0016     along with SnoreNotify.  If not, see <http://www.gnu.org/licenses/>.
0017 */
0018 
0019 #include "snorebackend.h"
0020 #include "../snore.h"
0021 #include "../snore_p.h"
0022 #include "../application.h"
0023 #include "../notification/notification.h"
0024 #include "../notification/notification_p.h"
0025 
0026 #include <QTimer>
0027 #include <QThread>
0028 #include <QMetaMethod>
0029 
0030 using namespace Snore;
0031 
0032 SnoreBackend::SnoreBackend()
0033 {
0034     connect(this, &SnoreBackend::enabledChanged, [this](bool enabled) {
0035         if (enabled) {
0036             connect(SnoreCorePrivate::instance(), &SnoreCorePrivate::applicationRegistered, this, &SnoreBackend::slotRegisterApplication, Qt::QueuedConnection);
0037             connect(SnoreCorePrivate::instance(), &SnoreCorePrivate::applicationDeregistered, this, &SnoreBackend::slotDeregisterApplication, Qt::QueuedConnection);
0038 
0039             connect(this, &SnoreBackend::notificationClosed, SnoreCorePrivate::instance(), &SnoreCorePrivate::slotNotificationClosed, Qt::QueuedConnection);
0040             connect(SnoreCorePrivate::instance(), &SnoreCorePrivate::notify, this, &SnoreBackend::slotNotify, Qt::QueuedConnection);
0041 
0042             for (const Application &a : SnoreCore::instance().aplications()) {
0043                 slotRegisterApplication(a);
0044             }
0045         } else {
0046             for (const Application &a : SnoreCore::instance().aplications()) {
0047                 slotDeregisterApplication(a);
0048             }
0049             disconnect(SnoreCorePrivate::instance(), &SnoreCorePrivate::applicationRegistered, this, &SnoreBackend::slotRegisterApplication);
0050             disconnect(SnoreCorePrivate::instance(), &SnoreCorePrivate::applicationDeregistered, this, &SnoreBackend::slotDeregisterApplication);
0051 
0052             disconnect(this, &SnoreBackend::notificationClosed, SnoreCorePrivate::instance(), &SnoreCorePrivate::slotNotificationClosed);
0053             disconnect(SnoreCorePrivate::instance(), &SnoreCorePrivate::notify, this, &SnoreBackend::slotNotify);
0054         }
0055     });
0056 }
0057 
0058 SnoreBackend::~SnoreBackend()
0059 {
0060     qCDebug(SNORE) << "Deleting" << name();
0061 }
0062 
0063 void SnoreBackend::requestCloseNotification(Notification notification, Notification::CloseReasons reason)
0064 {
0065     if (notification.isValid()) {
0066         if (canCloseNotification()) {
0067             slotCloseNotification(notification);
0068             closeNotification(notification, reason);
0069         }
0070     }
0071 }
0072 
0073 void SnoreBackend::closeNotification(Notification n, Notification::CloseReasons reason)
0074 {
0075     if (!n.isValid()) {
0076         return;
0077     }
0078     n.removeActiveIn(this);
0079     if (n.isUpdate()) {
0080         n.old().removeActiveIn(this);
0081     }
0082     n.data()->setCloseReason(reason);
0083     qCDebug(SNORE) << n << reason;
0084     emit notificationClosed(n);
0085 }
0086 
0087 void SnoreBackend::slotCloseNotification(Notification notification)
0088 {
0089     Q_UNUSED(notification)
0090 }
0091 
0092 bool SnoreBackend::canCloseNotification() const
0093 {
0094     return false;
0095 }
0096 
0097 bool SnoreBackend::canUpdateNotification() const
0098 {
0099     return false;
0100 }
0101 
0102 int SnoreBackend::maxNumberOfActiveNotifications() const
0103 {
0104     return 3;
0105 }
0106 
0107 SnorePlugin::PluginTypes SnoreBackend::type() const
0108 {
0109     return Backend;
0110 }
0111 
0112 void SnoreBackend::slotRegisterApplication(const Application &application)
0113 {
0114     Q_UNUSED(application);
0115 }
0116 
0117 void SnoreBackend::slotDeregisterApplication(const Application &application)
0118 {
0119     Q_UNUSED(application);
0120 }
0121 
0122 void SnoreBackend::slotNotificationDisplayed(Notification notification)
0123 {
0124     notification.addActiveIn(this);
0125     SnoreCorePrivate::instance()->slotNotificationDisplayed(notification);
0126 }
0127 
0128 void SnoreBackend::slotNotificationActionInvoked(Notification notification, const Action &action)
0129 {
0130     notification.data()->setActionInvoked(action);
0131     SnoreCorePrivate::instance()->slotNotificationActionInvoked(notification);
0132 }
0133