File indexing completed on 2024-05-26 04:48:40

0001 /*
0002     SnoreNotify is a Notification Framework based on Qt
0003     Copyright (C) 2014-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 "snorenotifier.h"
0020 #include "notifywidget.h"
0021 #include "snorebackendconstants.h"
0022 #include "libsnore/notification/notification_p.h"
0023 #include "libsnore/snore_p.h"
0024 
0025 #include <QThread>
0026 
0027 SnorePlugin::Snore::Snore()
0028 {
0029 
0030 }
0031 
0032 SnorePlugin::Snore::~Snore()
0033 {
0034     qDeleteAll(m_widgets);
0035 }
0036 
0037 void SnorePlugin::Snore::slotNotify(::Snore::Notification notification)
0038 {
0039     auto display = [this](NotifyWidget * w, ::Snore::Notification notification) {
0040         w->display(notification);
0041         notification.hints().setPrivateValue(this, "id", w->id());
0042         slotNotificationDisplayed(notification);
0043     };
0044 
0045     if (notification.isUpdate()) {
0046         if (notification.old().hints().privateValue(this, "id").isValid()) {
0047             NotifyWidget *w = m_widgets[notification.old().hints().privateValue(this, "id").toInt()];
0048             if (w->notification().isValid() && w->notification().id() == notification.old().id()) {
0049                 qCDebug(SNORE) << "replacing notification" << w->notification().id() << notification.id();
0050                 display(w, notification);
0051             }
0052         }
0053         return;
0054     }
0055     foreach(NotifyWidget * w, m_widgets) {
0056         if (w->acquire(notification.timeout())) {
0057             display(w, notification);
0058             return;
0059         }
0060     }
0061 }
0062 
0063 void SnorePlugin::Snore::slotCloseNotification(::Snore::Notification notification)
0064 {
0065     NotifyWidget *w = m_widgets[notification.hints().privateValue(this, "id").toInt()];
0066     w->release();
0067 }
0068 
0069 void SnorePlugin::Snore::slotRegisterApplication(const ::Snore::Application &)
0070 {
0071     if (!m_widgets.isEmpty())
0072         return;
0073     m_widgets.resize(3);
0074     for (int i = 0; i < m_widgets.size(); ++i) {
0075         auto *w = new NotifyWidget(i, this);
0076         m_widgets[i] = w;
0077         connect(w, &NotifyWidget::dismissed, [this, w]() {
0078             ::Snore::Notification notification = w->notification();
0079             closeNotification(notification, ::Snore::Notification::Dismissed);
0080             slotCloseNotification(notification);
0081         });
0082 
0083         connect(w, &NotifyWidget::invoked, [this, w]() {
0084             ::Snore::Notification notification = w->notification();
0085             slotNotificationActionInvoked(notification);
0086             closeNotification(notification, ::Snore::Notification::Activated);
0087             slotCloseNotification(notification);
0088         });
0089     }
0090 
0091 
0092 }
0093 
0094 bool SnorePlugin::Snore::canCloseNotification() const
0095 {
0096     return true;
0097 }
0098 
0099 bool SnorePlugin::Snore::canUpdateNotification() const
0100 {
0101     return true;
0102 }
0103 
0104 int SnorePlugin::Snore::maxNumberOfActiveNotifications() const
0105 {
0106     return m_widgets.size();
0107 }
0108 
0109 void SnorePlugin::Snore::setDefaultSettings()
0110 {
0111     setDefaultSettingsValue(SnoreBackendConstants::Position, Qt::TopRightCorner);
0112     SnoreBackend::setDefaultSettings();
0113 }