code style in snore backend, and a more user friendly config widget
[quassel.git] / src / qtui / snorenotificationbackend.cpp
1 /***************************************************************************
2 *   Copyright (C) 2011-2013 by Patrick von Reth                           *
3 *   vonreth@kde.org                                                       *
4 *                                                                         *
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.                                           *
9 *                                                                         *
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.                          *
14 *                                                                         *
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 ***************************************************************************/
20
21 #include "snorenotificationbackend.h"
22
23 #include <QtGui>
24 #include <QtGlobal>
25 #include <QDebug>
26
27 #include "client.h"
28 #include "iconloader.h"
29 #include "networkmodel.h"
30 #include "systraynotificationbackend.h"
31 #include "qtui.h"
32
33 #include <iostream>
34
35
36 #include <snore/core/snore.h>
37 #include <snore/core/notification/notification.h>
38
39
40 SnoreNotificationBackend::SnoreNotificationBackend (QObject *parent)
41     :AbstractNotificationBackend(parent),
42       m_systrayBackend(NULL)
43 {
44     NotificationSettings notificationSettings;
45     QString backend = notificationSettings.value("Snore/Backend", "Default").toString();
46     m_timeout = notificationSettings.value("Snore/Timeout",10).toInt();
47
48     notificationSettings.notify("Snore/Backend", this, SLOT(backendChanged(const QVariant &)));
49     notificationSettings.notify("Snore/Backend", this, SLOT(timeoutChanged(const QVariant &)));
50
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()));
56
57     connect(m_snore,SIGNAL(actionInvoked(Snore::Notification)),this,SLOT(actionInvoked(Snore::Notification)));
58
59     m_icon = Snore::Icon(DesktopIcon("dialog-information").toImage());
60
61     a->addAlert(new Snore::Alert(tr("Private Message"),tr("Private Message")));
62
63     m_snore->addApplication(a);
64     m_snore->applicationIsInitialized (a);
65
66     backendChanged(QVariant::fromValue(backend));
67
68
69 }
70
71 SnoreNotificationBackend::~SnoreNotificationBackend()
72 {
73     m_snore->removeApplication("Quassel");
74     m_snore->deleteLater();
75 }
76
77 void SnoreNotificationBackend::backendChanged(const QVariant &v)
78 {
79     QString backend = v.toString();
80     if(backend == "Default")
81     {
82         if(m_snore->setPrimaryNotificationBackend())//try to find the default backend for the platform
83         {
84             return;
85         }
86     }
87     else if(backend != "SystemTray")
88     {
89         if(setSnoreBackend(backend))
90         {
91             return;
92         }
93     }
94     setTraybackend();
95 }
96
97 void SnoreNotificationBackend::timeoutChanged(const QVariant &v)
98 {
99     m_timeout = v.toInt();
100 }
101
102 void SnoreNotificationBackend::notify(const Notification &n)
103 {
104     if(m_systrayBackend != NULL)
105         return;
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);
112 }
113
114 void SnoreNotificationBackend::close(uint notificationId)
115 {
116     if(m_systrayBackend != NULL)
117         return;
118     Snore::Notification n = m_snore->getActiveNotificationByID(m_notificationIds.take(notificationId));
119     m_snore->requestCloseNotification(n,Snore::NotificationEnums::CloseReasons::CLOSED);
120 }
121
122 void SnoreNotificationBackend::actionInvoked(Snore::Notification n)
123 {
124     emit activated(n.hints().value("QUASSEL_ID").toUInt());
125 }
126
127 SettingsPage *SnoreNotificationBackend::createConfigWidget()const
128 {
129     return new ConfigWidget(m_snore);
130 }
131
132 void SnoreNotificationBackend::setTraybackend()
133 {
134     if(m_systrayBackend == NULL){
135         m_systrayBackend = new SystrayNotificationBackend(this);
136         QtUi::registerNotificationBackend(m_systrayBackend);
137     }
138 }
139
140 bool SnoreNotificationBackend::setSnoreBackend(const QString &backend)
141 {
142     if(m_systrayBackend != NULL){
143         QtUi::unregisterNotificationBackend(m_systrayBackend);
144         delete m_systrayBackend;
145         m_systrayBackend = NULL;
146     }
147     return m_snore->setPrimaryNotificationBackend(backend);
148 }
149
150
151
152
153 /***************************************************************************/
154
155 SnoreNotificationBackend::ConfigWidget::ConfigWidget(Snore::SnoreCore *snore,QWidget *parent)
156     :SettingsPage("Internal", "SnoreNotification", parent),
157       m_snore(snore)
158 {
159     ui.setupUi(this);
160     ui.backends->insertItem(0,"Default");
161     ui.backends->insertItems(1,m_snore->notificationBackends());
162
163     connect(ui.backends, SIGNAL(currentIndexChanged(QString)), SLOT(backendChanged(QString)));
164     connect(ui.timeout,SIGNAL(valueChanged(int)),this,SLOT(timeoutChanged(int)));
165 }
166
167 void SnoreNotificationBackend::ConfigWidget::backendChanged(const QString &b)
168 {
169     ui.backends->setCurrentIndex(ui.backends->findText(b));
170     setChangedState(true);
171 }
172
173 void SnoreNotificationBackend::ConfigWidget::timeoutChanged(int i)
174 {
175     ui.timeout->setValue(i);
176     setChangedState(true);
177
178 }
179
180 bool SnoreNotificationBackend::ConfigWidget::hasDefaults() const
181 {
182     return true;
183 }
184
185 void SnoreNotificationBackend::ConfigWidget::defaults()
186 {
187     backendChanged("Default");
188     timeoutChanged(10);
189 }
190
191 void SnoreNotificationBackend::ConfigWidget::load()
192 {
193     NotificationSettings s;
194     QString backend = m_snore->primaryNotificationBackend();
195     if(backend.isEmpty()){
196         backend = "SystemTray";
197     }
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);
203 }
204
205 void SnoreNotificationBackend::ConfigWidget::save()
206 {
207     NotificationSettings s;
208     s.setValue("Snore/Backend", ui.backends->currentText());
209     s.setValue("Snore/Timeout",ui.timeout->value());
210     load();
211 }