Bring copyright headers into 2016
[quassel.git] / src / qtui / snorenotificationbackend.cpp
1 /***************************************************************************
2  *   Copyright (C) 2011-2016 by Hannah 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 <QMetaObject>
26
27 #include "client.h"
28 #include "networkmodel.h"
29 #include "systraynotificationbackend.h"
30 #include "qtui.h"
31
32 #include <iostream>
33
34
35 #include <libsnore/snore.h>
36 #include <libsnore/notification/notification.h>
37
38
39 SnoreNotificationBackend::SnoreNotificationBackend (QObject *parent)
40     : AbstractNotificationBackend(parent),
41       m_icon(QIcon::fromTheme("quassel", QIcon(":/icons/quassel.png")))
42 {
43
44     Snore::SnoreCore::instance().loadPlugins(
45 #ifndef HAVE_KDE
46                 Snore::SnorePlugin::Backend |
47 #endif
48                 Snore::SnorePlugin::SecondaryBackend | Snore::SnorePlugin::Settings);
49     m_application = Snore::Application("Quassel", m_icon);
50     m_application.hints().setValue("windows-app-id","QuasselProject.QuasselIRC");
51     m_application.hints().setValue("pushover-token", "arNtsi983QSZUqU3KAZrFLKHGFPkdL");
52
53     connect(&Snore::SnoreCore::instance(), SIGNAL(actionInvoked(Snore::Notification)), this, SLOT(actionInvoked(Snore::Notification)));
54
55
56     m_alert = Snore::Alert(tr("Private Message"), m_icon);
57     m_application.addAlert(m_alert);
58     Snore::SnoreCore::instance().setDefaultApplication(m_application);
59
60     NotificationSettings notificationSettings;
61     bool enabled = notificationSettings.value("Snore/Enabled", false).toBool();
62     setTraybackend(enabled);
63     notificationSettings.notify("Snore/Enabled", this, SLOT(setTraybackend(const QVariant &)));
64 }
65
66 SnoreNotificationBackend::~SnoreNotificationBackend()
67 {
68     Snore::SnoreCore::instance().deregisterApplication(m_application);
69 }
70
71 void SnoreNotificationBackend::notify(const Notification &n)
72 {
73 #ifndef HAVE_KDE
74     if (m_systrayBackend != nullptr) {
75         return;
76     }
77 #endif
78     QString title =  QString("%1 - %2").arg(Client::networkModel()->networkName(n.bufferId), Client::networkModel()->bufferName(n.bufferId));
79     QString message = QString("<%1> %2").arg(n.sender, n.message);
80     Snore::Notification noti(m_application, m_alert, title, message, m_icon);
81     noti.hints().setValue("QUASSEL_ID", n.notificationId);
82     m_notificationIds.insert(n.notificationId, noti.id());
83     Snore::SnoreCore::instance().broadcastNotification(noti);
84 }
85
86 void SnoreNotificationBackend::close(uint notificationId)
87 {
88 #ifndef HAVE_KDE
89     if (m_systrayBackend != nullptr) {
90         return;
91     }
92 #endif
93     Snore::Notification n = Snore::SnoreCore::instance().getActiveNotificationByID(m_notificationIds.take(notificationId));
94     if (n.isValid()) { // Don't close the notification if it no longer exists.
95         Snore::SnoreCore::instance().requestCloseNotification(n, Snore::Notification::Closed);
96     }
97 }
98
99 void SnoreNotificationBackend::actionInvoked(Snore::Notification n)
100 {
101     emit activated(n.hints().value("QUASSEL_ID").toUInt());
102 }
103
104 SettingsPage *SnoreNotificationBackend::createConfigWidget()const
105 {
106     return new ConfigWidget();
107 }
108
109
110 void SnoreNotificationBackend::setTraybackend(const QVariant &b)
111 {
112 #ifndef HAVE_KDE
113     if (!b.toBool()) {
114         if (m_systrayBackend == nullptr) {
115             m_systrayBackend = new SystrayNotificationBackend(this);
116             QtUi::registerNotificationBackend(m_systrayBackend);
117         }
118     } else {
119         if (m_systrayBackend != nullptr) {
120             QtUi::unregisterNotificationBackend(m_systrayBackend);
121             m_systrayBackend->deleteLater();
122             m_systrayBackend = nullptr;
123         }
124     }
125 #endif
126     if (b.toBool()) {
127         Snore::SnoreCore::instance().registerApplication(m_application);
128     } else {
129         Snore::SnoreCore::instance().deregisterApplication(m_application);
130     }
131 }
132
133 /***************************************************************************/
134
135 SnoreNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent)
136     :SettingsPage("Internal", "SnoreNotification", parent)
137 {
138     ui.setupUi(this);
139     connect(ui.useSnoreCheckBox, SIGNAL(toggled(bool)), this, SLOT(useSnnoreChanged(bool)));
140 }
141
142 bool SnoreNotificationBackend::ConfigWidget::hasDefaults() const
143 {
144     return true;
145 }
146
147 void SnoreNotificationBackend::ConfigWidget::defaults()
148 {
149     useSnnoreChanged(false);
150     ui.widget->reset();
151 }
152
153 void SnoreNotificationBackend::ConfigWidget::load()
154 {
155     NotificationSettings s;
156     bool enabled = s.value("Snore/Enabled", false).toBool();
157     ui.useSnoreCheckBox->setChecked(enabled);
158     ui.widget->setEnabled(enabled);
159     setChangedState(false);
160     QMetaObject::invokeMethod(this, "changed", Qt::QueuedConnection);//hack to make apply and accept button work for snore settings widget
161 }
162
163 void SnoreNotificationBackend::ConfigWidget::save()
164 {
165     NotificationSettings s;
166     s.setValue("Snore/Enabled", ui.useSnoreCheckBox->isChecked());
167     ui.widget->accept();
168     load();
169 }
170
171 void SnoreNotificationBackend::ConfigWidget::useSnnoreChanged(bool b)
172 {
173     ui.useSnoreCheckBox->setChecked(b);
174     ui.widget->setEnabled(b);
175     setChangedState(true);
176 }
177
178