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