1 /***************************************************************************
2 * Copyright (C) 2011-2018 by Hannah von Reth *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "snorenotificationbackend.h"
27 #include <QMetaObject>
29 #include <libsnore/snore.h>
30 #include <libsnore/notification/notification.h>
34 #include "networkmodel.h"
35 #include "systraynotificationbackend.h"
39 SnoreNotificationBackend::SnoreNotificationBackend (QObject *parent)
40 : AbstractNotificationBackend(parent)
41 , m_icon(icon::get("quassel"))
44 Snore::SnoreCore::instance().loadPlugins(
46 Snore::SnorePlugin::Backend |
48 Snore::SnorePlugin::SecondaryBackend | Snore::SnorePlugin::Settings);
49 m_application = Snore::Application("Quassel", m_icon);
50 m_application.hints().setValue("windows-app-id","QuasselProject.QuasselIRC");
51 m_application.hints().setValue("pushover-token", "arNtsi983QSZUqU3KAZrFLKHGFPkdL");
53 connect(&Snore::SnoreCore::instance(), SIGNAL(actionInvoked(Snore::Notification)), this, SLOT(actionInvoked(Snore::Notification)));
56 m_alert = Snore::Alert(tr("Private Message"), m_icon);
57 m_application.addAlert(m_alert);
58 Snore::SnoreCore::instance().setDefaultApplication(m_application);
60 NotificationSettings notificationSettings;
61 bool enabled = notificationSettings.value("Snore/Enabled", false).toBool();
62 setTraybackend(enabled);
63 notificationSettings.notify("Snore/Enabled", this, SLOT(setTraybackend(const QVariant &)));
66 SnoreNotificationBackend::~SnoreNotificationBackend()
68 Snore::SnoreCore::instance().deregisterApplication(m_application);
71 void SnoreNotificationBackend::notify(const Notification &n)
74 if (m_systrayBackend != nullptr) {
78 QString title = QString("%1 - %2").arg(Client::networkModel()->networkName(n.bufferId), Client::networkModel()->bufferName(n.bufferId));
79 QString message = QString("<%1> %2").arg(n.sender, n.message);
80 Snore::Notification noti(m_application, m_alert, title, message, m_icon);
81 noti.hints().setValue("QUASSEL_ID", n.notificationId);
82 m_notificationIds.insert(n.notificationId, noti.id());
83 Snore::SnoreCore::instance().broadcastNotification(noti);
86 void SnoreNotificationBackend::close(uint notificationId)
89 if (m_systrayBackend != nullptr) {
93 Snore::Notification n = Snore::SnoreCore::instance().getActiveNotificationByID(m_notificationIds.take(notificationId));
94 if (n.isValid()) { // Don't close the notification if it no longer exists.
95 Snore::SnoreCore::instance().requestCloseNotification(n, Snore::Notification::Closed);
99 void SnoreNotificationBackend::actionInvoked(Snore::Notification n)
101 emit activated(n.hints().value("QUASSEL_ID").toUInt());
104 SettingsPage *SnoreNotificationBackend::createConfigWidget()const
106 return new ConfigWidget();
110 void SnoreNotificationBackend::setTraybackend(const QVariant &b)
114 if (m_systrayBackend == nullptr) {
115 m_systrayBackend = new SystrayNotificationBackend(this);
116 QtUi::registerNotificationBackend(m_systrayBackend);
119 if (m_systrayBackend != nullptr) {
120 QtUi::unregisterNotificationBackend(m_systrayBackend);
121 m_systrayBackend->deleteLater();
122 m_systrayBackend = nullptr;
127 if (!Snore::SnoreCore::instance().aplications().contains(m_application.name())) {
128 Snore::SnoreCore::instance().registerApplication(m_application);
131 if (Snore::SnoreCore::instance().aplications().contains(m_application.name())) {
132 Snore::SnoreCore::instance().deregisterApplication(m_application);
137 /***************************************************************************/
139 SnoreNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent)
140 :SettingsPage("Internal", "SnoreNotification", parent)
143 connect(ui.useSnoreCheckBox, SIGNAL(toggled(bool)), this, SLOT(useSnnoreChanged(bool)));
146 bool SnoreNotificationBackend::ConfigWidget::hasDefaults() const
151 void SnoreNotificationBackend::ConfigWidget::defaults()
153 useSnnoreChanged(false);
157 void SnoreNotificationBackend::ConfigWidget::load()
159 NotificationSettings s;
160 bool enabled = s.value("Snore/Enabled", false).toBool();
161 ui.useSnoreCheckBox->setChecked(enabled);
162 ui.widget->setEnabled(enabled);
163 setChangedState(false);
164 QMetaObject::invokeMethod(this, "changed", Qt::QueuedConnection);//hack to make apply and accept button work for snore settings widget
167 void SnoreNotificationBackend::ConfigWidget::save()
169 NotificationSettings s;
170 s.setValue("Snore/Enabled", ui.useSnoreCheckBox->isChecked());
175 void SnoreNotificationBackend::ConfigWidget::useSnnoreChanged(bool b)
177 ui.useSnoreCheckBox->setChecked(b);
178 ui.widget->setEnabled(b);
179 setChangedState(true);