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", "SystemTray").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));
69 SnoreNotificationBackend::~SnoreNotificationBackend(){
70 m_snore->removeApplication("Quassel");
71 m_snore->deleteLater();
74 void SnoreNotificationBackend::backendChanged(const QVariant &v){
75 QString backend = v.toString();
76 if(backend == "SystemTray"){
77 if(m_systrayBackend == NULL){
78 m_systrayBackend = new SystrayNotificationBackend(this);
79 QtUi::registerNotificationBackend(m_systrayBackend);
82 if(m_systrayBackend != NULL){
83 QtUi::unregisterNotificationBackend(m_systrayBackend);
84 delete m_systrayBackend;
85 m_systrayBackend = NULL;
87 m_snore->setPrimaryNotificationBackend(backend);
91 void SnoreNotificationBackend::timeoutChanged(const QVariant &v){
92 m_timeout = v.toInt();
95 void SnoreNotificationBackend::notify(const Notification &n){
96 if(m_systrayBackend != NULL)
98 QString title = Client::networkModel()->networkName(n.bufferId) + " - " + Client::networkModel()->bufferName(n.bufferId);
99 QString message = QString("<%1> %2").arg(n.sender, n.message);
100 Snore::Notification noti("Quassel",tr("Private Message"),title,message,m_icon,m_timeout);
101 noti.hints().setValue("QUASSEL_ID",n.notificationId);
102 m_notificationIds.insert(n.notificationId,noti.id());
103 m_snore->broadcastNotification(noti);
106 void SnoreNotificationBackend::close(uint notificationId){
107 if(m_systrayBackend != NULL)
109 Snore::Notification n = m_snore->getActiveNotificationByID(m_notificationIds.take(notificationId));
110 m_snore->requestCloseNotification(n,Snore::NotificationEnums::CloseReasons::CLOSED);
113 void SnoreNotificationBackend::actionInvoked(Snore::Notification n){
114 emit activated(n.hints().value("QUASSEL_ID").toUInt());
117 SettingsPage *SnoreNotificationBackend::createConfigWidget()const{
118 return new ConfigWidget(m_snore);
122 /***************************************************************************/
124 SnoreNotificationBackend::ConfigWidget::ConfigWidget(Snore::SnoreCore *snore,QWidget *parent)
125 :SettingsPage("Internal", "SnoreNotification", parent),
129 ui.backends->insertItems(0,m_snore->notificationBackends());
131 connect(ui.backends, SIGNAL(currentIndexChanged(QString)), SLOT(backendChanged(QString)));
132 connect(ui.timeout,SIGNAL(valueChanged(int)),this,SLOT(timeoutChanged(int)));
135 void SnoreNotificationBackend::ConfigWidget::backendChanged(const QString &b){
136 ui.backends->setCurrentIndex(ui.backends->findText(b));
137 setChangedState(true);
140 void SnoreNotificationBackend::ConfigWidget::timeoutChanged(int i){
141 ui.timeout->setValue(i);
142 setChangedState(true);
146 bool SnoreNotificationBackend::ConfigWidget::hasDefaults() const {
150 void SnoreNotificationBackend::ConfigWidget::defaults() {
151 backendChanged("SystemTray");
155 void SnoreNotificationBackend::ConfigWidget::load() {
156 NotificationSettings s;
157 QString backend = s.value("Snore/Backend", "SystemTray").toString();
158 int timeout = s.value("Snore/Timeout",10).toInt();
159 ui.backends->setCurrentIndex(ui.backends->findText(backend));
160 ui.timeout->setValue(timeout);
161 setChangedState(false);
164 void SnoreNotificationBackend::ConfigWidget::save() {
165 NotificationSettings s;
166 s.setValue("Snore/Backend", ui.backends->currentText());
167 s.setValue("Snore/Timeout",ui.timeout->value());