Integrate DesktopNotification into system tray/status notifier
[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 <QApplication>
22 #include <QCheckBox>
23 #include <QGroupBox>
24 #include <QVBoxLayout>
25
26 #include "systraynotificationbackend.h"
27
28 #include "client.h"
29 #include "clientsettings.h"
30 #include "icon.h"
31 #include "mainwin.h"
32 #include "networkmodel.h"
33 #include "qtui.h"
34 #include "systemtray.h"
35
36 SystrayNotificationBackend::SystrayNotificationBackend(QObject *parent)
37   : AbstractNotificationBackend(parent),
38   _blockActivation(false)
39 {
40   NotificationSettings notificationSettings;
41   _showBubble = notificationSettings.value("Systray/ShowBubble", true).toBool();
42   _animate = notificationSettings.value("Systray/Animate", true).toBool();
43
44   notificationSettings.notify("Systray/ShowBubble", this, SLOT(showBubbleChanged(const QVariant &)));
45   notificationSettings.notify("Systray/Animate", this, SLOT(animateChanged(const QVariant &)));
46
47   connect(QtUi::mainWindow()->systemTray(), SIGNAL(messageClicked(uint)), SLOT(notificationActivated(uint)));
48   connect(QtUi::mainWindow()->systemTray(), SIGNAL(activated(SystemTray::ActivationReason)),
49                                             SLOT(notificationActivated(SystemTray::ActivationReason)));
50
51   QApplication::instance()->installEventFilter(this);
52
53   updateToolTip();
54 }
55
56 void SystrayNotificationBackend::notify(const Notification &n) {
57   if(n.type != Highlight && n.type != PrivMsg)
58     return;
59
60   _notifications.append(n);
61   if(_showBubble) {
62     QString title = Client::networkModel()->networkName(n.bufferId) + " - " + Client::networkModel()->bufferName(n.bufferId);
63     QString message = QString("<%1> %2").arg(n.sender, n.message);
64     QtUi::mainWindow()->systemTray()->showMessage(title, message, SystemTray::Information, 10000, n.notificationId);
65   }
66
67   if(_animate)
68     QtUi::mainWindow()->systemTray()->setAlert(true);
69
70   updateToolTip();
71 }
72
73 void SystrayNotificationBackend::close(uint notificationId) {
74   QList<Notification>::iterator i = _notifications.begin();
75   while(i != _notifications.end()) {
76     if(i->notificationId == notificationId)
77       i = _notifications.erase(i);
78     else
79       ++i;
80   }
81
82   QtUi::mainWindow()->systemTray()->closeMessage(notificationId);
83
84   if(!_notifications.count())
85     QtUi::mainWindow()->systemTray()->setAlert(false);
86
87   updateToolTip();
88 }
89
90 void SystrayNotificationBackend::notificationActivated(uint notificationId) {
91   if(!_blockActivation) {
92     if(QtUi::mainWindow()->systemTray()->isAlerted()) {
93       _blockActivation = true; // prevent double activation because both tray icon and bubble might send a signal
94       if(!notificationId)
95         notificationId = _notifications.count()? _notifications.last().notificationId : 0;
96       emit activated(notificationId);
97     } else
98       GraphicalUi::toggleMainWidget();
99   }
100 }
101
102 void SystrayNotificationBackend::notificationActivated(SystemTray::ActivationReason reason) {
103   if(reason == SystemTray::Trigger) {
104     notificationActivated(0);
105   }
106 }
107
108 // moving the mouse or releasing the button means that we're not dealing with a double activation
109 bool SystrayNotificationBackend::eventFilter(QObject *obj, QEvent *event) {
110   if(event->type() == QEvent::MouseMove || event->type() == QEvent::MouseButtonRelease) {
111     _blockActivation = false;
112   }
113   return AbstractNotificationBackend::eventFilter(obj, event);
114 }
115
116 void SystrayNotificationBackend::showBubbleChanged(const QVariant &v) {
117   _showBubble = v.toBool();
118 }
119
120 void SystrayNotificationBackend::animateChanged(const QVariant &v) {
121   _animate = v.toBool();
122 }
123
124 void SystrayNotificationBackend::updateToolTip() {
125   QtUi::mainWindow()->systemTray()->setToolTip("Quassel IRC",
126                                                _notifications.count()? tr("%n pending highlights", "", _notifications.count()) : QString());
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 }