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 "iconloader.h"
28 #include "networkmodel.h"
29 #include "systraynotificationbackend.h"
35 #include <snore/core/snore.h>
36 #include <snore/core/notification/notification.h>
39 SnoreNotificationBackend::SnoreNotificationBackend (QObject *parent)
40 :AbstractNotificationBackend(parent),
41 m_systrayBackend(NULL)
43 NotificationSettings notificationSettings;
44 QString backend = notificationSettings.value("Snore/Backend", "Default").toString();
45 m_timeout = notificationSettings.value("Snore/Timeout",10).toInt();
47 notificationSettings.notify("Snore/Backend", this, SLOT(backendChanged(const QVariant &)));
48 notificationSettings.notify("Snore/Backend", this, SLOT(timeoutChanged(const QVariant &)));
50 //TODO: try to get an instance of the tray icon to be able to show popups
51 m_snore = new Snore::SnoreCore();
52 m_snore->hints().setValue("WINDOWS_APP_ID","QuasselProject.QuasselIRC");
53 m_snore->loadPlugins(Snore::PluginContainer::BACKEND);
54 Snore::Application *a = new Snore::Application("Quassel",Snore::Icon(DesktopIcon("quassel").toImage()));
56 connect(m_snore,SIGNAL(actionInvoked(Snore::Notification)),this,SLOT(actionInvoked(Snore::Notification)));
58 m_icon = Snore::Icon(DesktopIcon("dialog-information").toImage());
60 a->addAlert(new Snore::Alert(tr("Private Message"),tr("Private Message")));
62 m_snore->addApplication(a);
63 m_snore->applicationIsInitialized (a);
65 backendChanged(QVariant::fromValue(backend));
70 SnoreNotificationBackend::~SnoreNotificationBackend()
72 m_snore->removeApplication("Quassel");
73 m_snore->deleteLater();
76 void SnoreNotificationBackend::backendChanged(const QVariant &v)
78 QString backend = v.toString();
79 if(backend == "Default")
81 if(m_snore->setPrimaryNotificationBackend())//try to find the default backend for the platform
86 else if(backend != "SystemTray")
88 if(setSnoreBackend(backend))
96 void SnoreNotificationBackend::timeoutChanged(const QVariant &v)
98 m_timeout = v.toInt();
101 void SnoreNotificationBackend::notify(const Notification &n)
103 if(m_systrayBackend != NULL)
105 QString title = Client::networkModel()->networkName(n.bufferId) + " - " + Client::networkModel()->bufferName(n.bufferId);
106 QString message = QString("<%1> %2").arg(n.sender, n.message);
107 Snore::Notification noti("Quassel",tr("Private Message"),title,message,m_icon,m_timeout);
108 noti.hints().setValue("QUASSEL_ID",n.notificationId);
109 m_notificationIds.insert(n.notificationId,noti.id());
110 m_snore->broadcastNotification(noti);
113 void SnoreNotificationBackend::close(uint notificationId)
115 if(m_systrayBackend != NULL)
117 Snore::Notification n = m_snore->getActiveNotificationByID(m_notificationIds.take(notificationId));
118 m_snore->requestCloseNotification(n,Snore::NotificationEnums::CloseReasons::CLOSED);
121 void SnoreNotificationBackend::actionInvoked(Snore::Notification n)
123 emit activated(n.hints().value("QUASSEL_ID").toUInt());
126 SettingsPage *SnoreNotificationBackend::createConfigWidget()const
128 return new ConfigWidget(m_snore);
131 void SnoreNotificationBackend::setTraybackend()
133 if(m_systrayBackend == NULL){
134 m_systrayBackend = new SystrayNotificationBackend(this);
135 QtUi::registerNotificationBackend(m_systrayBackend);
139 bool SnoreNotificationBackend::setSnoreBackend(const QString &backend)
141 if(m_systrayBackend != NULL){
142 QtUi::unregisterNotificationBackend(m_systrayBackend);
143 delete m_systrayBackend;
144 m_systrayBackend = NULL;
146 return m_snore->setPrimaryNotificationBackend(backend);
152 /***************************************************************************/
154 SnoreNotificationBackend::ConfigWidget::ConfigWidget(Snore::SnoreCore *snore,QWidget *parent)
155 :SettingsPage("Internal", "SnoreNotification", parent),
159 ui.backends->insertItem(0,"Default");
160 ui.backends->insertItems(1,m_snore->notificationBackends());
162 connect(ui.backends, SIGNAL(currentIndexChanged(QString)), SLOT(backendChanged(QString)));
163 connect(ui.timeout,SIGNAL(valueChanged(int)),this,SLOT(timeoutChanged(int)));
166 void SnoreNotificationBackend::ConfigWidget::backendChanged(const QString &b)
168 ui.backends->setCurrentIndex(ui.backends->findText(b));
169 setChangedState(true);
172 void SnoreNotificationBackend::ConfigWidget::timeoutChanged(int i)
174 ui.timeout->setValue(i);
175 setChangedState(true);
179 bool SnoreNotificationBackend::ConfigWidget::hasDefaults() const
184 void SnoreNotificationBackend::ConfigWidget::defaults()
186 backendChanged("Default");
190 void SnoreNotificationBackend::ConfigWidget::load()
192 NotificationSettings s;
193 QString backend = m_snore->primaryNotificationBackend();
194 if(backend.isEmpty()){
195 backend = "SystemTray";
197 int timeout = s.value("Snore/Timeout",10).toInt();
198 ui.backends->setCurrentIndex(ui.backends->findText(backend));
199 ui.timeout->setValue(timeout);
200 setChangedState(false);
203 void SnoreNotificationBackend::ConfigWidget::save()
205 NotificationSettings s;
206 s.setValue("Snore/Backend", ui.backends->currentText());
207 s.setValue("Snore/Timeout",ui.timeout->value());