Add a tooltip to the tray icon
[quassel.git] / src / qtui / systraynotificationbackend.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2010 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 "systraynotificationbackend.h"
22
23 #include <QtGui>
24
25 #include "client.h"
26 #include "clientsettings.h"
27 #include "icon.h"
28 #include "mainwin.h"
29 #include "networkmodel.h"
30 #include "qtui.h"
31 #include "systemtray.h"
32
33 SystrayNotificationBackend::SystrayNotificationBackend(QObject *parent)
34   : AbstractNotificationBackend(parent),
35   _blockActivation(false)
36 {
37   NotificationSettings notificationSettings;
38   _showBubble = notificationSettings.value("Systray/ShowBubble", true).toBool();
39   _animate = notificationSettings.value("Systray/Animate", true).toBool();
40
41   notificationSettings.notify("Systray/ShowBubble", this, SLOT(showBubbleChanged(const QVariant &)));
42   notificationSettings.notify("Systray/Animate", this, SLOT(animateChanged(const QVariant &)));
43
44   connect(QtUi::mainWindow()->systemTray(), SIGNAL(messageClicked()), SLOT(notificationActivated()));
45   connect(QtUi::mainWindow()->systemTray(), SIGNAL(activated(SystemTray::ActivationReason)),
46                                             SLOT(notificationActivated(SystemTray::ActivationReason)));
47
48   QApplication::instance()->installEventFilter(this);
49
50   updateToolTip();
51 }
52
53 void SystrayNotificationBackend::notify(const Notification &notification) {
54   if(notification.type != Highlight && notification.type != PrivMsg)
55     return;
56
57   _notifications.append(notification);
58   if(_showBubble)
59     showBubble();
60
61   if(_animate)
62     QtUi::mainWindow()->systemTray()->setAlert(true);
63
64   updateToolTip();
65 }
66
67 void SystrayNotificationBackend::close(uint notificationId) {
68   QList<Notification>::iterator i = _notifications.begin();
69   while(i != _notifications.end()) {
70     if(i->notificationId == notificationId)
71       i = _notifications.erase(i);
72     else
73       ++i;
74   }
75
76   closeBubble();
77
78   if(!_notifications.count())
79     QtUi::mainWindow()->systemTray()->setAlert(false);
80
81   updateToolTip();
82 }
83
84 void SystrayNotificationBackend::showBubble() {
85   // fancy stuff later: show messages in order
86   // for now, we just show the last message
87   if(_notifications.isEmpty())
88     return;
89   Notification n = _notifications.last();
90   QString title = Client::networkModel()->networkName(n.bufferId) + " - " + Client::networkModel()->bufferName(n.bufferId);
91   QString message = QString("<%1> %2").arg(n.sender, n.message);
92   QtUi::mainWindow()->systemTray()->showMessage(title, message);
93 }
94
95 void SystrayNotificationBackend::closeBubble() {
96   // there really seems to be no sane way to close the bubble... :(
97 #ifdef Q_WS_X11
98   QtUi::mainWindow()->systemTray()->showMessage("", "", SystemTray::NoIcon, 1);
99 #endif
100 }
101
102 void SystrayNotificationBackend::notificationActivated() {
103   if(!_blockActivation) {
104     if(QtUi::mainWindow()->systemTray()->isAlerted()) {
105       _blockActivation = true; // prevent double activation because both tray icon and bubble might send a signal
106       uint id = _notifications.count()? _notifications.last().notificationId : 0;
107       emit activated(id);
108     } else
109       GraphicalUi::toggleMainWidget();
110   }
111 }
112
113 void SystrayNotificationBackend::notificationActivated(SystemTray::ActivationReason reason) {
114   if(reason == SystemTray::Trigger) {
115     notificationActivated();
116   }
117 }
118
119 // moving the mouse or releasing the button means that we're not dealing with a double activation
120 bool SystrayNotificationBackend::eventFilter(QObject *obj, QEvent *event) {
121   if(event->type() == QEvent::MouseMove || event->type() == QEvent::MouseButtonRelease) {
122     _blockActivation = false;
123   }
124   return AbstractNotificationBackend::eventFilter(obj, event);
125 }
126
127 void SystrayNotificationBackend::showBubbleChanged(const QVariant &v) {
128   _showBubble = v.toBool();
129 }
130
131 void SystrayNotificationBackend::animateChanged(const QVariant &v) {
132   _animate = v.toBool();
133 }
134
135 void SystrayNotificationBackend::updateToolTip() {
136   QtUi::mainWindow()->systemTray()->setToolTip("Quassel IRC",
137                                                _notifications.count()? tr("%n pending highlights", "", _notifications.count()) : QString());
138 }
139
140 SettingsPage *SystrayNotificationBackend::createConfigWidget() const {
141   return new ConfigWidget();
142 }
143
144 /***************************************************************************/
145
146 SystrayNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent) : SettingsPage("Internal", "SystrayNotification", parent) {
147   QGroupBox *groupBox = new QGroupBox(tr("System Tray Icon"), this);
148   _animateBox = new QCheckBox(tr("Animate"));
149   connect(_animateBox, SIGNAL(toggled(bool)), this, SLOT(widgetChanged()));
150   _showBubbleBox = new QCheckBox(tr("Show bubble"));
151   connect(_showBubbleBox, SIGNAL(toggled(bool)), this, SLOT(widgetChanged()));
152   QVBoxLayout *layout = new QVBoxLayout(groupBox);
153   layout->addWidget(_animateBox);
154   layout->addWidget(_showBubbleBox);
155   layout->addStretch(1);
156   QVBoxLayout *globalLayout = new QVBoxLayout(this);
157   globalLayout->addWidget(groupBox);
158
159 }
160
161 void SystrayNotificationBackend::ConfigWidget::widgetChanged() {
162   bool changed = (_showBubble != _showBubbleBox->isChecked() || _animate != _animateBox->isChecked());
163   if(changed != hasChanged()) setChangedState(changed);
164 }
165
166 bool SystrayNotificationBackend::ConfigWidget::hasDefaults() const {
167   return true;
168 }
169
170 void SystrayNotificationBackend::ConfigWidget::defaults() {
171   _animateBox->setChecked(true);
172   _showBubbleBox->setChecked(false);
173   widgetChanged();
174 }
175
176 void SystrayNotificationBackend::ConfigWidget::load() {
177   NotificationSettings s;
178   _animate = s.value("Systray/Animate", true).toBool();
179   _showBubble = s.value("Systray/ShowBubble", false).toBool();
180   _animateBox->setChecked(_animate);
181   _showBubbleBox->setChecked(_showBubble);
182   setChangedState(false);
183 }
184
185 void SystrayNotificationBackend::ConfigWidget::save() {
186   NotificationSettings s;
187   s.setValue("Systray/Animate", _animateBox->isChecked());
188   s.setValue("Systray/ShowBubble", _showBubbleBox->isChecked());
189   load();
190 }