Semi-yearly copyright bump
[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 <QIcon>
24 #include <QTextDocument>
25 #include <QVBoxLayout>
26
27 #ifdef HAVE_KDE4
28 #  include <KNotification>
29 #  include <KNotifyConfigWidget>
30 #else
31 #  include <KNotifications/KNotification>
32 #  include <KNotifyConfig/KNotifyConfigWidget>
33 #endif
34
35 #include "client.h"
36 #include "mainwin.h"
37 #include "networkmodel.h"
38 #include "qtui.h"
39
40 KNotificationBackend::KNotificationBackend(QObject *parent)
41     : AbstractNotificationBackend(parent)
42 {
43     connect(QtUi::mainWindow()->systemTray(), SIGNAL(activated(SystemTray::ActivationReason)),
44         SLOT(notificationActivated(SystemTray::ActivationReason)));
45
46     updateToolTip();
47 }
48
49
50 void KNotificationBackend::notify(const Notification &n)
51 {
52     QString type;
53     switch (n.type) {
54     case Highlight:
55         type = "Highlight"; break;
56     case HighlightFocused:
57         type = "HighlightFocused"; break;
58     case PrivMsg:
59         type = "PrivMsg"; break;
60     case PrivMsgFocused:
61         type = "PrivMsgFocused"; break;
62     }
63
64 #if QT_VERSION < 0x050000
65     QString message = QString("<b>&lt;%1&gt;</b> %2").arg(n.sender, Qt::escape(n.message));
66 #else
67     QString message = QString("<b>&lt;%1&gt;</b> %2").arg(n.sender, n.message.toHtmlEscaped());
68 #endif
69     KNotification *notification = KNotification::event(type, message, QIcon::fromTheme("dialog-information").pixmap(48), QtUi::mainWindow(),
70         KNotification::RaiseWidgetOnActivation
71         |KNotification::CloseWhenWidgetActivated
72         |KNotification::CloseOnTimeout);
73     connect(notification, SIGNAL(activated(uint)), SLOT(notificationActivated()));
74     notification->setActions(QStringList("View"));
75     notification->setProperty("notificationId", n.notificationId);
76
77     _notifications.append(qMakePair(n.notificationId, QPointer<KNotification>(notification)));
78
79     updateToolTip();
80     QtUi::mainWindow()->systemTray()->setAlert(true);
81 }
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
100 void KNotificationBackend::close(uint notificationId)
101 {
102     removeNotificationById(notificationId);
103     //if(!_notifications.count()) // FIXME make configurable
104     QtUi::mainWindow()->systemTray()->setAlert(false);
105 }
106
107
108 void KNotificationBackend::notificationActivated()
109 {
110     uint id = 0;
111     KNotification *n = qobject_cast<KNotification *>(sender());
112     if (n)
113         id = n->property("notificationId").toUInt();
114
115     notificationActivated(id);
116 }
117
118
119 void KNotificationBackend::notificationActivated(SystemTray::ActivationReason reason)
120 {
121     if (reason == SystemTray::Trigger) {
122         if (_notifications.count())
123             notificationActivated(_notifications.first().first);  // oldest one
124         else
125             GraphicalUi::toggleMainWidget();
126     }
127 }
128
129
130 void KNotificationBackend::notificationActivated(uint notificationId)
131 {
132     emit activated(notificationId);
133 }
134
135
136 void KNotificationBackend::updateToolTip()
137 {
138     QtUi::mainWindow()->systemTray()->setToolTip("Quassel IRC",
139         _notifications.count() ? tr("%n pending highlight(s)", "", _notifications.count()) : QString());
140 }
141
142
143 SettingsPage *KNotificationBackend::createConfigWidget() const
144 {
145     return new ConfigWidget();
146 }
147
148
149 /***************************************************************************/
150
151 KNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent) : SettingsPage("Internal", "KNotification", parent)
152 {
153     _widget = new KNotifyConfigWidget(this);
154     _widget->setApplication("quassel");
155
156     QVBoxLayout *layout = new QVBoxLayout(this);
157     layout->addWidget(_widget);
158
159     connect(_widget, SIGNAL(changed(bool)), SLOT(widgetChanged(bool)));
160 }
161
162
163 void KNotificationBackend::ConfigWidget::widgetChanged(bool changed)
164 {
165     setChangedState(changed);
166 }
167
168
169 void KNotificationBackend::ConfigWidget::load()
170 {
171     setChangedState(false);
172 }
173
174
175 void KNotificationBackend::ConfigWidget::save()
176 {
177     _widget->save();
178     load();
179 }