72873c1e8861656ae1584616ca878f9775401064
[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 #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::SECONDARY_BACKEND);
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     Snore::SnoreCore::instance().requestCloseNotification(n, Snore::Notification::CLOSED);
95 }
96
97 void SnoreNotificationBackend::actionInvoked(Snore::Notification n)
98 {
99     emit activated(n.hints().value("QUASSEL_ID").toUInt());
100 }
101
102 SettingsPage *SnoreNotificationBackend::createConfigWidget()const
103 {
104     return new ConfigWidget();
105 }
106
107
108 void SnoreNotificationBackend::setTraybackend(const QVariant &b)
109 {
110 #ifndef HAVE_KDE
111     if (!b.toBool()) {
112         if (m_systrayBackend == nullptr) {
113             m_systrayBackend = new SystrayNotificationBackend(this);
114             QtUi::registerNotificationBackend(m_systrayBackend);
115         }
116     } else {
117         if (m_systrayBackend != nullptr) {
118             QtUi::unregisterNotificationBackend(m_systrayBackend);
119             m_systrayBackend->deleteLater();
120             m_systrayBackend = nullptr;
121         }
122     }
123 #endif
124     if (b.toBool()) {
125         Snore::SnoreCore::instance().registerApplication(m_application);
126     } else {
127         Snore::SnoreCore::instance().deregisterApplication(m_application);
128     }
129 }
130
131 /***************************************************************************/
132
133 SnoreNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent)
134     :SettingsPage("Internal", "SnoreNotification", parent)
135 {
136     ui.setupUi(this);
137     connect(ui.useSnoreCheckBox, SIGNAL(toggled(bool)), this, SLOT(useSnnoreChanged(bool)));
138 }
139
140 bool SnoreNotificationBackend::ConfigWidget::hasDefaults() const
141 {
142     return true;
143 }
144
145 void SnoreNotificationBackend::ConfigWidget::defaults()
146 {
147     useSnnoreChanged(false);
148     ui.widget->reset();
149 }
150
151 void SnoreNotificationBackend::ConfigWidget::load()
152 {
153     NotificationSettings s;
154     bool enabled = s.value("Snore/Enabled", false).toBool();
155     ui.useSnoreCheckBox->setChecked(enabled);
156     ui.widget->setEnabled(enabled);
157     setChangedState(false);
158     QMetaObject::invokeMethod(this, "changed", Qt::QueuedConnection);//hack to make apply and accept button work for snore settings widget
159 }
160
161 void SnoreNotificationBackend::ConfigWidget::save()
162 {
163     NotificationSettings s;
164     s.setValue("Snore/Enabled", ui.useSnoreCheckBox->isChecked());
165     ui.widget->accept();
166     load();
167 }
168
169 void SnoreNotificationBackend::ConfigWidget::useSnnoreChanged(bool b)
170 {
171     ui.useSnoreCheckBox->setChecked(b);
172     ui.widget->setEnabled(b);
173     setChangedState(true);
174 }
175
176