Performance tweak: rem. pointless recursive calls
[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
26 #include "client.h"
27 #include "networkmodel.h"
28 #include "systraynotificationbackend.h"
29 #include "qtui.h"
30
31 #include <iostream>
32
33
34 #include <snore/core/snore.h>
35 #include <snore/core/notification/notification.h>
36
37
38 SnoreNotificationBackend::SnoreNotificationBackend (QObject *parent)
39     : AbstractNotificationBackend(parent),
40       m_systrayBackend(NULL)
41 {
42     NotificationSettings notificationSettings;
43     QString backend = notificationSettings.value("Snore/Backend", "Default").toString();
44     m_timeout = notificationSettings.value("Snore/Timeout", 10).toInt();
45
46     notificationSettings.notify("Snore/Backend", this, SLOT(backendChanged(const QVariant &)));
47     notificationSettings.notify("Snore/Timeout", this, SLOT(timeoutChanged(const QVariant &)));
48
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");
55
56     connect(m_snore, SIGNAL(actionInvoked(Snore::Notification)), this, SLOT(actionInvoked(Snore::Notification)));
57
58
59     m_alert = Snore::Alert(tr("Private Message"), m_icon);
60     m_application.addAlert(m_alert);
61
62     m_snore->registerApplication(m_application);
63
64     backendChanged(QVariant::fromValue(backend));
65
66
67 }
68
69 SnoreNotificationBackend::~SnoreNotificationBackend()
70 {
71     m_snore->deregisterApplication(m_application);
72     m_snore->deleteLater();
73 }
74
75 void SnoreNotificationBackend::backendChanged(const QVariant &v)
76 {
77     QString backend = v.toString();
78     if (backend != "Default") {
79         if (setSnoreBackend(backend)) {
80             return;
81         }
82     }
83     setTraybackend();
84 }
85
86 void SnoreNotificationBackend::timeoutChanged(const QVariant &v)
87 {
88     m_timeout = v.toInt();
89 }
90
91 void SnoreNotificationBackend::notify(const Notification &n)
92 {
93     if (m_systrayBackend != NULL) {
94         return;
95     }
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);
102 }
103
104 void SnoreNotificationBackend::close(uint notificationId)
105 {
106     if (m_systrayBackend != NULL) {
107         return;
108     }
109     Snore::Notification n = m_snore->getActiveNotificationByID(m_notificationIds.take(notificationId));
110     m_snore->requestCloseNotification(n, Snore::Notification::CLOSED);
111 }
112
113 void SnoreNotificationBackend::actionInvoked(Snore::Notification n)
114 {
115     emit activated(n.hints().value("QUASSEL_ID").toUInt());
116 }
117
118 SettingsPage *SnoreNotificationBackend::createConfigWidget()const
119 {
120     return new ConfigWidget(m_snore);
121 }
122
123 void SnoreNotificationBackend::setTraybackend()
124 {
125     if (m_systrayBackend == NULL) {
126         m_systrayBackend = new SystrayNotificationBackend(this);
127         QtUi::registerNotificationBackend(m_systrayBackend);
128     }
129 }
130
131 bool SnoreNotificationBackend::setSnoreBackend(const QString &backend)
132 {
133     if (m_systrayBackend != NULL) {
134         QtUi::unregisterNotificationBackend(m_systrayBackend);
135         delete m_systrayBackend;
136         m_systrayBackend = NULL;
137     }
138     return m_snore->setPrimaryNotificationBackend(backend);
139 }
140
141
142
143
144 /***************************************************************************/
145
146 SnoreNotificationBackend::ConfigWidget::ConfigWidget(Snore::SnoreCore *snore, QWidget *parent)
147     :SettingsPage("Internal", "SnoreNotification", parent),
148       m_snore(snore)
149 {
150     ui.setupUi(this);
151     QStringList backends = m_snore->notificationBackends();
152     backends.append("Default");
153     qSort(backends);
154     ui.backends->insertItems(0, backends);
155
156     connect(ui.backends, SIGNAL(currentIndexChanged(QString)), SLOT(backendChanged(QString)));
157     connect(ui.timeout, SIGNAL(valueChanged(int)), this, SLOT(timeoutChanged(int)));
158 }
159
160 void SnoreNotificationBackend::ConfigWidget::backendChanged(const QString &b)
161 {
162     ui.backends->setCurrentIndex(ui.backends->findText(b));
163     setChangedState(true);
164 }
165
166 void SnoreNotificationBackend::ConfigWidget::timeoutChanged(int i)
167 {
168     ui.timeout->setValue(i);
169     setChangedState(true);
170
171 }
172
173 bool SnoreNotificationBackend::ConfigWidget::hasDefaults() const
174 {
175     return true;
176 }
177
178 void SnoreNotificationBackend::ConfigWidget::defaults()
179 {
180     backendChanged("Default");
181     timeoutChanged(10);
182 }
183
184 void SnoreNotificationBackend::ConfigWidget::load()
185 {
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);
192 }
193
194 void SnoreNotificationBackend::ConfigWidget::save()
195 {
196     NotificationSettings s;
197     s.setValue("Snore/Backend", ui.backends->currentText());
198     s.setValue("Snore/Timeout", ui.timeout->value());
199     load();
200 }