1 /***************************************************************************
2 * Copyright (C) 2011-2019 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"
25 #include <QMetaObject>
29 #include <libsnore/notification/notification.h>
30 #include <libsnore/snore.h>
34 #include "networkmodel.h"
36 #include "systraynotificationbackend.h"
38 SnoreNotificationBackend::SnoreNotificationBackend(QObject* parent)
39 : AbstractNotificationBackend(parent)
40 , m_icon(icon::get("quassel"))
42 Snore::SnoreCore::instance().loadPlugins(
44 Snore::SnorePlugin::Backend |
46 Snore::SnorePlugin::SecondaryBackend | Snore::SnorePlugin::Settings);
47 m_application = Snore::Application("Quassel", m_icon);
48 m_application.hints().setValue("windows-app-id", "QuasselProject.QuasselIRC");
49 m_application.hints().setValue("pushover-token", "arNtsi983QSZUqU3KAZrFLKHGFPkdL");
51 connect(&Snore::SnoreCore::instance(), &Snore::SnoreCore::actionInvoked, this, &SnoreNotificationBackend::actionInvoked);
53 m_alert = Snore::Alert(tr("Private Message"), m_icon);
54 m_application.addAlert(m_alert);
55 Snore::SnoreCore::instance().setDefaultApplication(m_application);
57 NotificationSettings notificationSettings;
58 bool enabled = notificationSettings.value("Snore/Enabled", false).toBool();
59 setTraybackend(enabled);
60 notificationSettings.notify("Snore/Enabled", this, &SnoreNotificationBackend::setTraybackend);
63 SnoreNotificationBackend::~SnoreNotificationBackend()
65 Snore::SnoreCore::instance().deregisterApplication(m_application);
68 void SnoreNotificationBackend::notify(const Notification& n)
71 if (m_systrayBackend != nullptr) {
75 QString title = QString("%1 - %2").arg(Client::networkModel()->networkName(n.bufferId), Client::networkModel()->bufferName(n.bufferId));
76 QString message = QString("<%1> %2").arg(n.sender, n.message);
77 Snore::Notification noti(m_application, m_alert, title, message, m_icon);
78 noti.hints().setValue("QUASSEL_ID", n.notificationId);
79 m_notificationIds.insert(n.notificationId, noti.id());
80 Snore::SnoreCore::instance().broadcastNotification(noti);
83 void SnoreNotificationBackend::close(uint notificationId)
86 if (m_systrayBackend != nullptr) {
90 Snore::Notification n = Snore::SnoreCore::instance().getActiveNotificationByID(m_notificationIds.take(notificationId));
91 if (n.isValid()) { // Don't close the notification if it no longer exists.
92 Snore::SnoreCore::instance().requestCloseNotification(n, Snore::Notification::Closed);
96 void SnoreNotificationBackend::actionInvoked(Snore::Notification n)
98 emit activated(n.hints().value("QUASSEL_ID").toUInt());
101 SettingsPage* SnoreNotificationBackend::createConfigWidget() const
103 return new ConfigWidget();
106 void SnoreNotificationBackend::setTraybackend(const QVariant& b)
110 if (m_systrayBackend == nullptr) {
111 m_systrayBackend = new SystrayNotificationBackend(this);
112 QtUi::registerNotificationBackend(m_systrayBackend);
116 if (m_systrayBackend != nullptr) {
117 QtUi::unregisterNotificationBackend(m_systrayBackend);
118 m_systrayBackend->deleteLater();
119 m_systrayBackend = nullptr;
124 if (!Snore::SnoreCore::instance().aplications().contains(m_application.name())) {
125 Snore::SnoreCore::instance().registerApplication(m_application);
129 if (Snore::SnoreCore::instance().aplications().contains(m_application.name())) {
130 Snore::SnoreCore::instance().deregisterApplication(m_application);
135 /***************************************************************************/
137 SnoreNotificationBackend::ConfigWidget::ConfigWidget(QWidget* parent)
138 : SettingsPage("Internal", "SnoreNotification", parent)
141 connect(ui.useSnoreCheckBox, &QCheckBox::toggled, this, &ConfigWidget::useSnoreChanged);
144 bool SnoreNotificationBackend::ConfigWidget::hasDefaults() const
149 void SnoreNotificationBackend::ConfigWidget::defaults()
151 useSnoreChanged(false);
155 void SnoreNotificationBackend::ConfigWidget::load()
157 NotificationSettings s;
158 bool enabled = s.value("Snore/Enabled", false).toBool();
159 ui.useSnoreCheckBox->setChecked(enabled);
160 ui.widget->setEnabled(enabled);
161 setChangedState(false);
162 QMetaObject::invokeMethod(this, "changed", Qt::QueuedConnection); // hack to make apply and accept button work for snore settings widget
165 void SnoreNotificationBackend::ConfigWidget::save()
167 NotificationSettings s;
168 s.setValue("Snore/Enabled", ui.useSnoreCheckBox->isChecked());
173 void SnoreNotificationBackend::ConfigWidget::useSnoreChanged(bool b)
175 ui.useSnoreCheckBox->setChecked(b);
176 ui.widget->setEnabled(b);
177 setChangedState(true);