Fix fullscreen mode
[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/Backend", 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->hints().setValue("WINDOWS_APP_ID","QuasselProject.QuasselIRC");
53     m_snore->loadPlugins(Snore::PluginContainer::BACKEND);
54     Snore::Application *a = new Snore::Application("Quassel", Snore::Icon(DesktopIcon("quassel").toImage()));
55
56     connect(m_snore, SIGNAL(actionInvoked(Snore::Notification)), this, SLOT(actionInvoked(Snore::Notification)));
57
58     m_icon = Snore::Icon(DesktopIcon("dialog-information").toImage());
59
60     a->addAlert(new Snore::Alert(tr("Private Message"), tr("Private Message")));
61
62     m_snore->addApplication(a);
63     m_snore->applicationIsInitialized (a);
64
65     backendChanged(QVariant::fromValue(backend));
66
67
68 }
69
70 SnoreNotificationBackend::~SnoreNotificationBackend()
71 {
72     m_snore->removeApplication("Quassel");
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 (m_snore->setPrimaryNotificationBackend()) {//try to find the default backend for the platform
81             return;
82         }
83     }
84     else if (backend != "SystemTray") {
85         if (setSnoreBackend(backend)) {
86             return;
87         }
88     }
89     setTraybackend();
90 }
91
92 void SnoreNotificationBackend::timeoutChanged(const QVariant &v)
93 {
94     m_timeout = v.toInt();
95 }
96
97 void SnoreNotificationBackend::notify(const Notification &n)
98 {
99     if (m_systrayBackend != NULL) {
100         return;
101     }
102     QString title = Client::networkModel()->networkName(n.bufferId) + " - " + Client::networkModel()->bufferName(n.bufferId);
103     QString message = QString("<%1> %2").arg(n.sender, n.message);
104     Snore::Notification noti("Quassel", tr("Private Message"), title, message, m_icon, m_timeout);
105     noti.hints().setValue("QUASSEL_ID", n.notificationId);
106     m_notificationIds.insert(n.notificationId, noti.id());
107     m_snore->broadcastNotification(noti);
108 }
109
110 void SnoreNotificationBackend::close(uint notificationId)
111 {
112     if (m_systrayBackend != NULL) {
113         return;
114     }
115     Snore::Notification n = m_snore->getActiveNotificationByID(m_notificationIds.take(notificationId));
116     m_snore->requestCloseNotification(n, Snore::NotificationEnums::CloseReasons::CLOSED);
117 }
118
119 void SnoreNotificationBackend::actionInvoked(Snore::Notification n)
120 {
121     emit activated(n.hints().value("QUASSEL_ID").toUInt());
122 }
123
124 SettingsPage *SnoreNotificationBackend::createConfigWidget()const
125 {
126     return new ConfigWidget(m_snore);
127 }
128
129 void SnoreNotificationBackend::setTraybackend()
130 {
131     if (m_systrayBackend == NULL) {
132         m_systrayBackend = new SystrayNotificationBackend(this);
133         QtUi::registerNotificationBackend(m_systrayBackend);
134     }
135 }
136
137 bool SnoreNotificationBackend::setSnoreBackend(const QString &backend)
138 {
139     if (m_systrayBackend != NULL) {
140         QtUi::unregisterNotificationBackend(m_systrayBackend);
141         delete m_systrayBackend;
142         m_systrayBackend = NULL;
143     }
144     return m_snore->setPrimaryNotificationBackend(backend);
145 }
146
147
148
149
150 /***************************************************************************/
151
152 SnoreNotificationBackend::ConfigWidget::ConfigWidget(Snore::SnoreCore *snore, QWidget *parent)
153     :SettingsPage("Internal", "SnoreNotification", parent),
154       m_snore(snore)
155 {
156     ui.setupUi(this);
157     ui.backends->insertItem(0, "Default");
158     ui.backends->insertItems(1, m_snore->notificationBackends());
159
160     connect(ui.backends, SIGNAL(currentIndexChanged(QString)), SLOT(backendChanged(QString)));
161     connect(ui.timeout, SIGNAL(valueChanged(int)), this, SLOT(timeoutChanged(int)));
162 }
163
164 void SnoreNotificationBackend::ConfigWidget::backendChanged(const QString &b)
165 {
166     ui.backends->setCurrentIndex(ui.backends->findText(b));
167     setChangedState(true);
168 }
169
170 void SnoreNotificationBackend::ConfigWidget::timeoutChanged(int i)
171 {
172     ui.timeout->setValue(i);
173     setChangedState(true);
174
175 }
176
177 bool SnoreNotificationBackend::ConfigWidget::hasDefaults() const
178 {
179     return true;
180 }
181
182 void SnoreNotificationBackend::ConfigWidget::defaults()
183 {
184     backendChanged("Default");
185     timeoutChanged(10);
186 }
187
188 void SnoreNotificationBackend::ConfigWidget::load()
189 {
190     NotificationSettings s;
191     QString backend = m_snore->primaryNotificationBackend();
192     if (backend.isEmpty()) {
193         backend = "SystemTray";
194     }
195     int timeout = s.value("Snore/Timeout", 10).toInt();
196     ui.backends->setCurrentIndex(ui.backends->findText(backend));
197     ui.timeout->setValue(timeout);
198     setChangedState(false);
199 }
200
201 void SnoreNotificationBackend::ConfigWidget::save()
202 {
203     NotificationSettings s;
204     s.setValue("Snore/Backend", ui.backends->currentText());
205     s.setValue("Snore/Timeout", ui.timeout->value());
206     load();
207 }