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