Select highlighted channel on clicking the blinking tray icon in all cases
[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)
37 : AbstractNotificationBackend(parent),
38   _lastNotificationId(0)
39 {
40   connect(QtUi::mainWindow()->systemTray(), SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
41                                             SLOT(notificationActivated(QSystemTrayIcon::ActivationReason)));
42 }
43
44 void KNotificationBackend::notify(const Notification &n) {
45   QString message = QString("<b>&lt;%1&gt;</b> %2").arg(n.sender, Qt::escape(n.message));
46   KNotification *notification = KNotification::event("Highlight", message, DesktopIcon("dialog-information"), QtUi::mainWindow(),
47                                 KNotification::RaiseWidgetOnActivation
48                                |KNotification::CloseWhenWidgetActivated
49                                |KNotification::CloseOnTimeout);
50   connect(notification, SIGNAL(activated(uint)), SLOT(notificationActivated()));
51   connect(notification, SIGNAL(closed()), SLOT(notificationClosed()));
52   notification->setActions(QStringList("View"));
53   _notificationIds[notification] = _lastNotificationId = n.notificationId;
54
55   QtUi::mainWindow()->systemTray()->setAlert(true);
56 }
57
58 void KNotificationBackend::removeNotificationById(uint notificationId) {
59   QHash<KNotification *, uint>::iterator i = _notificationIds.begin();
60   while(i != _notificationIds.end()) {
61     if(i.value() == notificationId)
62       i = _notificationIds.erase(i);
63     else
64       ++i;
65   }
66   if(_lastNotificationId == notificationId)
67     _lastNotificationId = 0;
68 }
69
70 void KNotificationBackend::close(uint notificationId) {
71   removeNotificationById(notificationId);
72   if(!_notificationIds.count())
73     QtUi::mainWindow()->systemTray()->setAlert(false);
74 }
75
76 void KNotificationBackend::notificationActivated() {
77   uint id = 0;
78   KNotification *n = qobject_cast<KNotification *>(sender());
79   if(n && _notificationIds.contains(n))
80     id = _notificationIds.value(n);
81
82   notificationActivated(id);
83 }
84
85 void KNotificationBackend::notificationActivated(QSystemTrayIcon::ActivationReason reason) {
86   if(reason == QSystemTrayIcon::Trigger && _lastNotificationId > 0) {
87     notificationActivated(_lastNotificationId); // most recent one
88   }
89 }
90
91 void KNotificationBackend::notificationActivated(uint notificationId) {
92   removeNotificationById(notificationId);
93
94   QtUi::mainWindow()->systemTray()->setInhibitActivation();
95   emit activated(notificationId);
96
97   if(!_notificationIds.count())
98     QtUi::mainWindow()->systemTray()->setAlert(false);
99
100 }
101
102 void KNotificationBackend::notificationClosed() {
103   //KNotification *n = qobject_cast<KNotification *>(sender());
104   //if(n && _notificationIds.contains(n))
105   //  _notificationIds.remove(n);
106 }
107
108 SettingsPage *KNotificationBackend::createConfigWidget() const {
109   return new ConfigWidget();
110 }
111
112 /***************************************************************************/
113
114 KNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent) : SettingsPage("Internal", "KNotification", parent) {
115   _widget = new KNotifyConfigWidget(this);
116   _widget->setApplication("quassel");
117
118   QVBoxLayout *layout = new QVBoxLayout(this);
119   layout->addWidget(_widget);
120
121   connect(_widget, SIGNAL(changed(bool)), SLOT(widgetChanged(bool)));
122 }
123
124 void KNotificationBackend::ConfigWidget::widgetChanged(bool changed) {
125   setChangedState(changed);
126 }
127
128 void KNotificationBackend::ConfigWidget::load() {
129   setChangedState(false);
130 }
131
132 void KNotificationBackend::ConfigWidget::save() {
133   _widget->save();
134   load();
135 }