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"
28 #include "iconloader.h"
29 #include "networkmodel.h"
30 #include "systraynotificationbackend.h"
36 #include <snore/core/snore.h>
37 #include <snore/core/notification/notification.h>
40 SnoreNotificationBackend::SnoreNotificationBackend (QObject *parent)
41 :AbstractNotificationBackend(parent),
42 m_systrayBackend(NULL)
44 NotificationSettings notificationSettings;
45 QString backend = notificationSettings.value("Snore/Backend", "Default").toString();
46 m_timeout = notificationSettings.value("Snore/Timeout",10).toInt();
48 notificationSettings.notify("Snore/Backend", this, SLOT(backendChanged(const QVariant &)));
49 notificationSettings.notify("Snore/Backend", this, SLOT(timeoutChanged(const QVariant &)));
51 //TODO: try to get an instance of the tray icon to be able to show popups
52 m_snore = new Snore::SnoreCore();
53 m_snore->hints().setValue("WINDOWS_APP_ID","QuasselProject.QuasselIRC");
54 m_snore->loadPlugins(Snore::PluginContainer::BACKEND);
55 Snore::Application *a = new Snore::Application("Quassel",Snore::Icon(DesktopIcon("quassel").toImage()));
57 connect(m_snore,SIGNAL(actionInvoked(Snore::Notification)),this,SLOT(actionInvoked(Snore::Notification)));
59 m_icon = Snore::Icon(DesktopIcon("dialog-information").toImage());
61 a->addAlert(new Snore::Alert(tr("Private Message"),tr("Private Message")));
63 m_snore->addApplication(a);
64 m_snore->applicationIsInitialized (a);
66 backendChanged(QVariant::fromValue(backend));
71 SnoreNotificationBackend::~SnoreNotificationBackend()
73 m_snore->removeApplication("Quassel");
74 m_snore->deleteLater();
77 void SnoreNotificationBackend::backendChanged(const QVariant &v)
79 QString backend = v.toString();
80 if(backend == "Default")
82 if(m_snore->setPrimaryNotificationBackend())//try to find the default backend for the platform
87 else if(backend != "SystemTray")
89 if(setSnoreBackend(backend))
97 void SnoreNotificationBackend::timeoutChanged(const QVariant &v)
99 m_timeout = v.toInt();
102 void SnoreNotificationBackend::notify(const Notification &n)
104 if(m_systrayBackend != NULL)
106 QString title = Client::networkModel()->networkName(n.bufferId) + " - " + Client::networkModel()->bufferName(n.bufferId);
107 QString message = QString("<%1> %2").arg(n.sender, n.message);
108 Snore::Notification noti("Quassel",tr("Private Message"),title,message,m_icon,m_timeout);
109 noti.hints().setValue("QUASSEL_ID",n.notificationId);
110 m_notificationIds.insert(n.notificationId,noti.id());
111 m_snore->broadcastNotification(noti);
114 void SnoreNotificationBackend::close(uint notificationId)
116 if(m_systrayBackend != NULL)
118 Snore::Notification n = m_snore->getActiveNotificationByID(m_notificationIds.take(notificationId));
119 m_snore->requestCloseNotification(n,Snore::NotificationEnums::CloseReasons::CLOSED);
122 void SnoreNotificationBackend::actionInvoked(Snore::Notification n)
124 emit activated(n.hints().value("QUASSEL_ID").toUInt());
127 SettingsPage *SnoreNotificationBackend::createConfigWidget()const
129 return new ConfigWidget(m_snore);
132 void SnoreNotificationBackend::setTraybackend()
134 if(m_systrayBackend == NULL){
135 m_systrayBackend = new SystrayNotificationBackend(this);
136 QtUi::registerNotificationBackend(m_systrayBackend);
140 bool SnoreNotificationBackend::setSnoreBackend(const QString &backend)
142 if(m_systrayBackend != NULL){
143 QtUi::unregisterNotificationBackend(m_systrayBackend);
144 delete m_systrayBackend;
145 m_systrayBackend = NULL;
147 return m_snore->setPrimaryNotificationBackend(backend);
153 /***************************************************************************/
155 SnoreNotificationBackend::ConfigWidget::ConfigWidget(Snore::SnoreCore *snore,QWidget *parent)
156 :SettingsPage("Internal", "SnoreNotification", parent),
160 ui.backends->insertItem(0,"Default");
161 ui.backends->insertItems(1,m_snore->notificationBackends());
163 connect(ui.backends, SIGNAL(currentIndexChanged(QString)), SLOT(backendChanged(QString)));
164 connect(ui.timeout,SIGNAL(valueChanged(int)),this,SLOT(timeoutChanged(int)));
167 void SnoreNotificationBackend::ConfigWidget::backendChanged(const QString &b)
169 ui.backends->setCurrentIndex(ui.backends->findText(b));
170 setChangedState(true);
173 void SnoreNotificationBackend::ConfigWidget::timeoutChanged(int i)
175 ui.timeout->setValue(i);
176 setChangedState(true);
180 bool SnoreNotificationBackend::ConfigWidget::hasDefaults() const
185 void SnoreNotificationBackend::ConfigWidget::defaults()
187 backendChanged("Default");
191 void SnoreNotificationBackend::ConfigWidget::load()
193 NotificationSettings s;
194 QString backend = m_snore->primaryNotificationBackend();
195 if(backend.isEmpty()){
196 backend = "SystemTray";
198 qDebug() << s.value("Snore/Timeout",10);
199 int timeout = s.value("Snore/Timeout",10).toInt();
200 ui.backends->setCurrentIndex(ui.backends->findText(backend));
201 ui.timeout->setValue(timeout);
202 setChangedState(false);
205 void SnoreNotificationBackend::ConfigWidget::save()
207 NotificationSettings s;
208 s.setValue("Snore/Backend", ui.backends->currentText());
209 s.setValue("Snore/Timeout",ui.timeout->value());