qtui: Hide "fallback" and "Override" if no system theme is configured
[quassel.git] / src / qtui / snorenotificationbackend.cpp
1 /***************************************************************************
2  *   Copyright (C) 2011-2018 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         if (!Snore::SnoreCore::instance().aplications().contains(m_application.name())) {
128             Snore::SnoreCore::instance().registerApplication(m_application);
129         }
130     } else {
131         if (Snore::SnoreCore::instance().aplications().contains(m_application.name())) {
132             Snore::SnoreCore::instance().deregisterApplication(m_application);
133         }
134     }
135 }
136
137 /***************************************************************************/
138
139 SnoreNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent)
140     :SettingsPage("Internal", "SnoreNotification", parent)
141 {
142     ui.setupUi(this);
143     connect(ui.useSnoreCheckBox, SIGNAL(toggled(bool)), this, SLOT(useSnnoreChanged(bool)));
144 }
145
146 bool SnoreNotificationBackend::ConfigWidget::hasDefaults() const
147 {
148     return true;
149 }
150
151 void SnoreNotificationBackend::ConfigWidget::defaults()
152 {
153     useSnnoreChanged(false);
154     ui.widget->reset();
155 }
156
157 void SnoreNotificationBackend::ConfigWidget::load()
158 {
159     NotificationSettings s;
160     bool enabled = s.value("Snore/Enabled", false).toBool();
161     ui.useSnoreCheckBox->setChecked(enabled);
162     ui.widget->setEnabled(enabled);
163     setChangedState(false);
164     QMetaObject::invokeMethod(this, "changed", Qt::QueuedConnection);//hack to make apply and accept button work for snore settings widget
165 }
166
167 void SnoreNotificationBackend::ConfigWidget::save()
168 {
169     NotificationSettings s;
170     s.setValue("Snore/Enabled", ui.useSnoreCheckBox->isChecked());
171     ui.widget->accept();
172     load();
173 }
174
175 void SnoreNotificationBackend::ConfigWidget::useSnnoreChanged(bool b)
176 {
177     ui.useSnoreCheckBox->setChecked(b);
178     ui.widget->setEnabled(b);
179     setChangedState(true);
180 }
181
182