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