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