Don't interpret HTML in KNotify popups, fixes #631
[quassel.git] / src / qtui / knotificationbackend.cpp
1 /***************************************************************************
2 *   Copyright (C) 2005-09 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 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19 ***************************************************************************/
20
21 #include <QVBoxLayout>
22
23 #include <KNotification>
24 #include <KNotifyConfigWidget>
25 #include <QTextDocument>
26
27 #include "knotificationbackend.h"
28
29 #include "client.h"
30 #include "icon.h"
31 #include "iconloader.h"
32 #include "networkmodel.h"
33 #include "qtui.h"
34 #include "systemtray.h"
35
36 KNotificationBackend::KNotificationBackend(QObject *parent) : AbstractNotificationBackend(parent) {
37
38 }
39
40 void KNotificationBackend::notify(const Notification &n) {
41   QString type;
42   switch(n.type) {
43     case Highlight:
44       type = "Highlight"; break;
45     case HighlightFocused:
46       type = "HighlightFocused"; break;
47     case PrivMsg:
48       type = "PrivMsg"; break;
49     case PrivMsgFocused:
50       type = "PrivMsgFocused"; break;
51   }
52
53   QString message = QString("<b>&lt;%1&gt;</b> %2").arg(n.sender, Qt::escape(n.message));
54   KNotification *notification = KNotification::event(type, message, DesktopIcon("dialog-information"), QtUi::mainWindow(),
55                                 KNotification::Persistent|KNotification::RaiseWidgetOnActivation|KNotification::CloseWhenWidgetActivated);
56   connect(notification, SIGNAL(activated(uint)), SLOT(notificationActivated()));
57   connect(notification, SIGNAL(closed()), SLOT(notificationClosed()));
58   notification->setActions(QStringList("View"));
59   _notificationIds[notification] = n.notificationId;
60
61   QtUi::mainWindow()->systemTray()->setAlert(true);
62 }
63
64 void KNotificationBackend::close(uint notificationId) {
65   Q_UNUSED(notificationId);
66   QtUi::mainWindow()->systemTray()->setAlert(false);
67 }
68
69 void KNotificationBackend::notificationActivated() {
70   uint id = 0;
71   KNotification *n = qobject_cast<KNotification *>(sender());
72   if(n && _notificationIds.contains(n))
73     id = _notificationIds.value(n);
74
75   emit activated(id);
76 }
77
78 void KNotificationBackend::notificationClosed() {
79   KNotification *n = qobject_cast<KNotification *>(sender());
80   if(n && _notificationIds.contains(n))
81     _notificationIds.remove(n);
82 }
83
84 SettingsPage *KNotificationBackend::createConfigWidget() const {
85   return new ConfigWidget();
86 }
87
88 /***************************************************************************/
89
90 KNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent) : SettingsPage("Internal", "KNotification", parent) {
91   _widget = new KNotifyConfigWidget(this);
92   _widget->setApplication("quassel");
93
94   QVBoxLayout *layout = new QVBoxLayout(this);
95   layout->addWidget(_widget);
96
97   connect(_widget, SIGNAL(changed(bool)), SLOT(widgetChanged(bool)));
98 }
99
100 void KNotificationBackend::ConfigWidget::widgetChanged(bool changed) {
101   setChangedState(changed);
102 }
103
104 void KNotificationBackend::ConfigWidget::load() {
105   setChangedState(false);
106 }
107
108 void KNotificationBackend::ConfigWidget::save() {
109   _widget->save();
110   load();
111 }