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