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