Check for KNotifications version
[quassel.git] / src / qtui / knotificationbackend.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2019 by the Quassel Project                        *
3  *   devel@quassel-irc.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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "knotificationbackend.h"
22
23 #include <QTextDocument>
24 #include <QVBoxLayout>
25
26 #include <KNotifications/KNotification>
27 #include <KNotifyConfig/KNotifyConfigWidget>
28 #include <knotifications_version.h>
29
30 #include "client.h"
31 #include "icon.h"
32 #include "mainwin.h"
33 #include "networkmodel.h"
34 #include "qtui.h"
35 #include "util.h"
36
37 KNotificationBackend::KNotificationBackend(QObject* parent)
38     : AbstractNotificationBackend(parent)
39 {
40     connect(QtUi::mainWindow()->systemTray(),
41             &SystemTray::activated,
42             this,
43             selectOverload<SystemTray::ActivationReason>(&KNotificationBackend::notificationActivated));
44
45     updateToolTip();
46 }
47
48 void KNotificationBackend::notify(const Notification& n)
49 {
50     QString type;
51     switch (n.type) {
52     case Highlight:
53         type = "Highlight";
54         break;
55     case HighlightFocused:
56         type = "HighlightFocused";
57         break;
58     case PrivMsg:
59         type = "PrivMsg";
60         break;
61     case PrivMsgFocused:
62         type = "PrivMsgFocused";
63         break;
64     }
65
66     QString message = QString("<b>&lt;%1&gt;</b> %2").arg(n.sender, n.message.toHtmlEscaped());
67     KNotification* notification = KNotification::event(type,
68                                                        message,
69                                                        QStringLiteral("dialog-information"),
70                                                        QtUi::mainWindow(),
71                                                        KNotification::RaiseWidgetOnActivation | KNotification::CloseWhenWidgetActivated
72                                                            | KNotification::CloseOnTimeout);
73     connect(notification,
74             selectOverload<uint>(&KNotification::activated),
75             this,
76             selectOverload<>(&KNotificationBackend::notificationActivated));
77 #if KNOTIFICATIONS_VERSION >= QT_VERSION_CHECK(5,31,0)
78     notification->setDefaultAction(tr("View"));
79 #else
80     notification->setActions(QStringList{tr("View")});
81 #endif
82     notification->setProperty("notificationId", n.notificationId);
83
84     _notifications.append(qMakePair(n.notificationId, QPointer<KNotification>(notification)));
85
86     updateToolTip();
87 }
88
89 void KNotificationBackend::removeNotificationById(uint notificationId)
90 {
91     QList<QPair<uint, QPointer<KNotification>>>::iterator i = _notifications.begin();
92     while (i != _notifications.end()) {
93         if (i->first == notificationId) {
94             if (i->second)
95                 i->second->close();
96             i = _notifications.erase(i);
97         }
98         else
99             ++i;
100     }
101     updateToolTip();
102 }
103
104 void KNotificationBackend::close(uint notificationId)
105 {
106     removeNotificationById(notificationId);
107     // if(!_notifications.count()) // FIXME make configurable
108 }
109
110 void KNotificationBackend::notificationActivated()
111 {
112     uint id = 0;
113     KNotification* n = qobject_cast<KNotification*>(sender());
114     if (n)
115         id = n->property("notificationId").toUInt();
116
117     notificationActivated(id);
118 }
119
120 void KNotificationBackend::notificationActivated(SystemTray::ActivationReason reason)
121 {
122     if (reason == SystemTray::Trigger) {
123         if (_notifications.count())
124             notificationActivated(_notifications.first().first);  // oldest one
125         else
126             GraphicalUi::toggleMainWidget();
127     }
128 }
129
130 void KNotificationBackend::notificationActivated(uint notificationId)
131 {
132     emit activated(notificationId);
133 }
134
135 void KNotificationBackend::updateToolTip()
136 {
137     QtUi::mainWindow()->systemTray()->setToolTip("Quassel IRC",
138                                                  _notifications.count() ? tr("%n pending highlight(s)", "", _notifications.count())
139                                                                         : QString());
140 }
141
142 SettingsPage* KNotificationBackend::createConfigWidget() const
143 {
144     return new ConfigWidget();
145 }
146
147 /***************************************************************************/
148
149 KNotificationBackend::ConfigWidget::ConfigWidget(QWidget* parent)
150     : SettingsPage("Internal", "KNotification", parent)
151 {
152     _widget = new KNotifyConfigWidget(this);
153     _widget->setApplication("quassel");
154
155     QVBoxLayout* layout = new QVBoxLayout(this);
156     layout->addWidget(_widget);
157
158     connect(_widget, &KNotifyConfigWidget::changed, this, &ConfigWidget::widgetChanged);
159 }
160
161 void KNotificationBackend::ConfigWidget::widgetChanged(bool changed)
162 {
163     setChangedState(changed);
164 }
165
166 void KNotificationBackend::ConfigWidget::load()
167 {
168     setChangedState(false);
169 }
170
171 void KNotificationBackend::ConfigWidget::save()
172 {
173     _widget->save();
174     load();
175 }