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