Improve systray notifications
[quassel.git] / src / qtui / systraynotificationbackend.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 "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), _activeId(0)
35 {
36   NotificationSettings notificationSettings;
37   _showBubble = notificationSettings.value("Systray/ShowBubble", true).toBool();
38   _animate = notificationSettings.value("Systray/Animate", true).toBool();
39
40   notificationSettings.notify("Systray/ShowBubble", this, SLOT(showBubbleChanged(const QVariant &)));
41   notificationSettings.notify("Systray/Animate", this, SLOT(animateChanged(const QVariant &)));
42
43   connect(QtUi::mainWindow()->systemTray(), SIGNAL(messageClicked()), SLOT(notificationActivated()));
44 }
45
46 void SystrayNotificationBackend::notify(const Notification &notification) {
47   /* fancy stuff to be implemented later: show notifications in order
48   _notifications.append(notification);
49   if(_showBubble && _notifications.count() == 1) {
50     showBubble();
51   }
52   */
53   _notifications.clear();
54   _notifications.append(notification);
55   if(_showBubble)
56     showBubble();
57
58   if(_animate)
59     QtUi::mainWindow()->systemTray()->setAlert(true);
60 }
61
62 void SystrayNotificationBackend::close(uint notificationId) {
63   Q_UNUSED(notificationId);
64   /* fancy stuff to be implemented later
65   int idx = _notifications.indexOf(notificationId);
66
67   if(_notifications.isEmpty()) {
68   */
69   _notifications.clear();
70   closeBubble();
71   QtUi::mainWindow()->systemTray()->setAlert(false);
72 }
73
74 void SystrayNotificationBackend::showBubble() {
75   // fancy stuff later: show messages in order
76   // for now, we just show the last message
77   if(_notifications.isEmpty()) return;
78   Notification n = _notifications.takeLast();
79   _activeId = n.notificationId;
80   QString title = Client::networkModel()->networkName(n.bufferId) + " - " + Client::networkModel()->bufferName(n.bufferId);
81   QString message = QString("<%1> %2").arg(n.sender, n.message);
82   QtUi::mainWindow()->systemTray()->showMessage(title, message);
83 }
84
85 void SystrayNotificationBackend::closeBubble() {
86   // there really seems to be no sane way to close the bubble... :(
87 #ifdef Q_WS_X11
88   QtUi::mainWindow()->systemTray()->showMessage("", "", QSystemTrayIcon::NoIcon, 1);
89 #endif
90 }
91
92 void SystrayNotificationBackend::notificationActivated() {
93   emit activated(_activeId);
94 }
95
96 void SystrayNotificationBackend::showBubbleChanged(const QVariant &v) {
97   _showBubble = v.toBool();
98 }
99
100 void SystrayNotificationBackend::animateChanged(const QVariant &v) {
101   _animate = v.toBool();
102 }
103
104 SettingsPage *SystrayNotificationBackend::createConfigWidget() const {
105   return new ConfigWidget();
106 }
107
108 /***************************************************************************/
109
110 SystrayNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent) : SettingsPage("Internal", "SystrayNotification", parent) {
111   QGroupBox *groupBox = new QGroupBox(tr("System Tray Icon"), this);
112   _animateBox = new QCheckBox(tr("Animate"));
113   connect(_animateBox, SIGNAL(toggled(bool)), this, SLOT(widgetChanged()));
114   _showBubbleBox = new QCheckBox(tr("Show bubble"));
115   connect(_showBubbleBox, SIGNAL(toggled(bool)), this, SLOT(widgetChanged()));
116   QVBoxLayout *layout = new QVBoxLayout(groupBox);
117   layout->addWidget(_animateBox);
118   layout->addWidget(_showBubbleBox);
119   layout->addStretch(1);
120   QVBoxLayout *globalLayout = new QVBoxLayout(this);
121   globalLayout->addWidget(groupBox);
122
123 }
124
125 void SystrayNotificationBackend::ConfigWidget::widgetChanged() {
126   bool changed = (_showBubble != _showBubbleBox->isChecked() || _animate != _animateBox->isChecked());
127   if(changed != hasChanged()) setChangedState(changed);
128 }
129
130 bool SystrayNotificationBackend::ConfigWidget::hasDefaults() const {
131   return true;
132 }
133
134 void SystrayNotificationBackend::ConfigWidget::defaults() {
135   _animateBox->setChecked(true);
136   _showBubbleBox->setChecked(false);
137   widgetChanged();
138 }
139
140 void SystrayNotificationBackend::ConfigWidget::load() {
141   NotificationSettings s;
142   _animate = s.value("Systray/Animate", true).toBool();
143   _showBubble = s.value("Systray/ShowBubble", false).toBool();
144   _animateBox->setChecked(_animate);
145   _showBubbleBox->setChecked(_showBubble);
146   setChangedState(false);
147 }
148
149 void SystrayNotificationBackend::ConfigWidget::save() {
150   NotificationSettings s;
151   s.setValue("Systray/Animate", _animateBox->isChecked());
152   s.setValue("Systray/ShowBubble", _showBubbleBox->isChecked());
153   load();
154 }