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