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