1 /***************************************************************************
2 * Copyright (C) 2011-2013 by Patrick 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>
28 #include "networkmodel.h"
29 #include "systraynotificationbackend.h"
35 #include <libsnore/snore.h>
36 #include <libsnore/notification/notification.h>
39 SnoreNotificationBackend::SnoreNotificationBackend (QObject *parent)
40 : AbstractNotificationBackend(parent),
41 m_icon(QIcon::fromTheme("quassel", QIcon(":/icons/quassel.png")))
44 Snore::SnoreCore::instance().loadPlugins(
46 Snore::SnorePlugin::BACKEND |
48 Snore::SnorePlugin::SECONDARY_BACKEND);
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 Snore::SnoreCore::instance().requestCloseNotification(n, Snore::Notification::CLOSED);
97 void SnoreNotificationBackend::actionInvoked(Snore::Notification n)
99 emit activated(n.hints().value("QUASSEL_ID").toUInt());
102 SettingsPage *SnoreNotificationBackend::createConfigWidget()const
104 return new ConfigWidget();
108 void SnoreNotificationBackend::setTraybackend(const QVariant &b)
112 if (m_systrayBackend == nullptr) {
113 m_systrayBackend = new SystrayNotificationBackend(this);
114 QtUi::registerNotificationBackend(m_systrayBackend);
117 if (m_systrayBackend != nullptr) {
118 QtUi::unregisterNotificationBackend(m_systrayBackend);
119 m_systrayBackend->deleteLater();
120 m_systrayBackend = nullptr;
125 Snore::SnoreCore::instance().registerApplication(m_application);
127 Snore::SnoreCore::instance().deregisterApplication(m_application);
131 /***************************************************************************/
133 SnoreNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent)
134 :SettingsPage("Internal", "SnoreNotification", parent)
137 connect(ui.useSnoreCheckBox, SIGNAL(toggled(bool)), this, SLOT(useSnnoreChanged(bool)));
140 bool SnoreNotificationBackend::ConfigWidget::hasDefaults() const
145 void SnoreNotificationBackend::ConfigWidget::defaults()
147 useSnnoreChanged(false);
151 void SnoreNotificationBackend::ConfigWidget::load()
153 NotificationSettings s;
154 bool enabled = s.value("Snore/Enabled", false).toBool();
155 ui.useSnoreCheckBox->setChecked(enabled);
156 ui.widget->setEnabled(enabled);
157 setChangedState(false);
158 QMetaObject::invokeMethod(this, "changed", Qt::QueuedConnection);//hack to make apply and accept button work for snore settings widget
161 void SnoreNotificationBackend::ConfigWidget::save()
163 NotificationSettings s;
164 s.setValue("Snore/Enabled", ui.useSnoreCheckBox->isChecked());
169 void SnoreNotificationBackend::ConfigWidget::useSnnoreChanged(bool b)
171 ui.useSnoreCheckBox->setChecked(b);
172 ui.widget->setEnabled(b);
173 setChangedState(true);