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"
27 #include "networkmodel.h"
28 #include "systraynotificationbackend.h"
34 #include <snore/core/snore.h>
35 #include <snore/core/notification/notification.h>
38 SnoreNotificationBackend::SnoreNotificationBackend (QObject *parent)
39 : AbstractNotificationBackend(parent),
40 m_systrayBackend(NULL)
42 NotificationSettings notificationSettings;
43 QString backend = notificationSettings.value("Snore/Backend", "Default").toString();
44 m_timeout = notificationSettings.value("Snore/Timeout", 10).toInt();
46 notificationSettings.notify("Snore/Backend", this, SLOT(backendChanged(const QVariant &)));
47 notificationSettings.notify("Snore/Timeout", this, SLOT(timeoutChanged(const QVariant &)));
49 //TODO: try to get an instance of the tray icon to be able to show popups
50 m_snore = new Snore::SnoreCore();
51 m_snore->loadPlugins(Snore::SnorePlugin::BACKEND);
52 m_icon = Snore::Icon(QIcon::fromTheme("quassel", QIcon(":/icons/quassel.png")).pixmap(48).toImage());
53 m_application = Snore::Application("Quassel", m_icon);
54 m_application.hints().setValue("WINDOWS_APP_ID","QuasselProject.QuasselIRC");
56 connect(m_snore, SIGNAL(actionInvoked(Snore::Notification)), this, SLOT(actionInvoked(Snore::Notification)));
59 m_alert = Snore::Alert(tr("Private Message"), m_icon);
60 m_application.addAlert(m_alert);
62 m_snore->registerApplication(m_application);
64 backendChanged(QVariant::fromValue(backend));
69 SnoreNotificationBackend::~SnoreNotificationBackend()
71 m_snore->deregisterApplication(m_application);
72 m_snore->deleteLater();
75 void SnoreNotificationBackend::backendChanged(const QVariant &v)
77 QString backend = v.toString();
78 if (backend != "Default") {
79 if (setSnoreBackend(backend)) {
86 void SnoreNotificationBackend::timeoutChanged(const QVariant &v)
88 m_timeout = v.toInt();
91 void SnoreNotificationBackend::notify(const Notification &n)
93 if (m_systrayBackend != NULL) {
96 QString title = Client::networkModel()->networkName(n.bufferId) + " - " + Client::networkModel()->bufferName(n.bufferId);
97 QString message = QString("<%1> %2").arg(n.sender, n.message);
98 Snore::Notification noti(m_application, m_alert, title, message, m_icon, m_timeout);
99 noti.hints().setValue("QUASSEL_ID", n.notificationId);
100 m_notificationIds.insert(n.notificationId, noti.id());
101 m_snore->broadcastNotification(noti);
104 void SnoreNotificationBackend::close(uint notificationId)
106 if (m_systrayBackend != NULL) {
109 Snore::Notification n = m_snore->getActiveNotificationByID(m_notificationIds.take(notificationId));
110 m_snore->requestCloseNotification(n, Snore::Notification::CLOSED);
113 void SnoreNotificationBackend::actionInvoked(Snore::Notification n)
115 emit activated(n.hints().value("QUASSEL_ID").toUInt());
118 SettingsPage *SnoreNotificationBackend::createConfigWidget()const
120 return new ConfigWidget(m_snore);
123 void SnoreNotificationBackend::setTraybackend()
125 if (m_systrayBackend == NULL) {
126 m_systrayBackend = new SystrayNotificationBackend(this);
127 QtUi::registerNotificationBackend(m_systrayBackend);
131 bool SnoreNotificationBackend::setSnoreBackend(const QString &backend)
133 if (m_systrayBackend != NULL) {
134 QtUi::unregisterNotificationBackend(m_systrayBackend);
135 delete m_systrayBackend;
136 m_systrayBackend = NULL;
138 return m_snore->setPrimaryNotificationBackend(backend);
144 /***************************************************************************/
146 SnoreNotificationBackend::ConfigWidget::ConfigWidget(Snore::SnoreCore *snore, QWidget *parent)
147 :SettingsPage("Internal", "SnoreNotification", parent),
151 QStringList backends = m_snore->notificationBackends();
152 backends.append("Default");
154 ui.backends->insertItems(0, backends);
156 connect(ui.backends, SIGNAL(currentIndexChanged(QString)), SLOT(backendChanged(QString)));
157 connect(ui.timeout, SIGNAL(valueChanged(int)), this, SLOT(timeoutChanged(int)));
160 void SnoreNotificationBackend::ConfigWidget::backendChanged(const QString &b)
162 ui.backends->setCurrentIndex(ui.backends->findText(b));
163 setChangedState(true);
166 void SnoreNotificationBackend::ConfigWidget::timeoutChanged(int i)
168 ui.timeout->setValue(i);
169 setChangedState(true);
173 bool SnoreNotificationBackend::ConfigWidget::hasDefaults() const
178 void SnoreNotificationBackend::ConfigWidget::defaults()
180 backendChanged("Default");
184 void SnoreNotificationBackend::ConfigWidget::load()
186 NotificationSettings s;
187 QString backend = s.value("Snore/Backend", "Default").toString();
188 int timeout = s.value("Snore/Timeout", 10).toInt();
189 ui.backends->setCurrentIndex(ui.backends->findText(backend));
190 ui.timeout->setValue(timeout);
191 setChangedState(false);
194 void SnoreNotificationBackend::ConfigWidget::save()
196 NotificationSettings s;
197 s.setValue("Snore/Backend", ui.backends->currentText());
198 s.setValue("Snore/Timeout", ui.timeout->value());