35dd2dd70c0c16fee481f756293c65da472ec21a
[quassel.git] / src / qtui / systraynotificationbackend.cpp
1 /***************************************************************************
2 *   Copyright (C) 2005-08 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
32 SystrayNotificationBackend::SystrayNotificationBackend(QObject *parent) : AbstractNotificationBackend(parent) {
33   NotificationSettings notificationSettings;
34   _showBubble = notificationSettings.value("Systray/ShowBubble", true).toBool();
35   _animate = notificationSettings.value("Systray/Animate", true).toBool();
36
37   notificationSettings.notify("Systray/ShowBubble", this, SLOT(showBubbleChanged(const QVariant &)));
38   notificationSettings.notify("Systray/Animate", this, SLOT(animateChanged(const QVariant &)));
39
40   _configWidget = new ConfigWidget();
41   _iconActive = false;
42   connect(&_animationTimer, SIGNAL(timeout()), SLOT(blink()));
43 }
44
45 SystrayNotificationBackend::~SystrayNotificationBackend() {
46   delete _configWidget;
47 }
48
49 void SystrayNotificationBackend::notify(const Notification &notification) {
50   /* fancy stuff to be implemented later: show notifications in order
51   _notifications.append(notification);
52   if(_showBubble && _notifications.count() == 1) {
53     showBubble();
54   }
55   */
56   _notifications.clear();
57   _notifications.append(notification);
58   if(_showBubble) {
59     showBubble();
60   }
61   if(_animate) {
62     startAnimation();
63   }
64 }
65
66 void SystrayNotificationBackend::close(uint notificationId) {
67   Q_UNUSED(notificationId);
68   /* fancy stuff to be implemented later
69   int idx = _notifications.indexOf(notificationId);
70
71   if(_notifications.isEmpty()) {
72   */
73   _notifications.clear();
74   closeBubble();
75   stopAnimation();
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()) return;
82   Notification n = _notifications.takeLast();
83   QString title = Client::networkModel()->networkName(n.bufferId) + " - " + Client::networkModel()->bufferName(n.bufferId);
84   QString message = QString("<%1> %2").arg(n.sender, n.message);
85   QtUi::mainWindow()->systemTrayIcon()->showMessage(title, message);
86 }
87
88 void SystrayNotificationBackend::closeBubble() {
89   // there really seems to be no decent way to close the bubble...
90   // in addition, windows ignores the timeout -_-
91 #ifndef Q_WS_WIN
92   QtUi::mainWindow()->systemTrayIcon()->showMessage("", "", QSystemTrayIcon::NoIcon, 1);
93 #endif
94 }
95
96 void SystrayNotificationBackend::showBubbleChanged(const QVariant &v) {
97   _showBubble = v.toBool();
98 }
99
100 void SystrayNotificationBackend::startAnimation() {
101   if(!_animationTimer.isActive())
102     _animationTimer.start(500);
103 }
104
105 void SystrayNotificationBackend::stopAnimation() {
106   _animationTimer.stop();
107   QtUi::mainWindow()->systemTrayIcon()->setIcon(Icon("quassel"));
108   _iconActive = false;
109 }
110
111 void SystrayNotificationBackend::blink() {
112   QtUi::mainWindow()->systemTrayIcon()->setIcon(_iconActive ? Icon("quassel") : Icon("quassel_newmessage"));
113   _iconActive = !_iconActive;
114 }
115
116 void SystrayNotificationBackend::animateChanged(const QVariant &v) {
117   _animate = v.toBool();
118 }
119
120 SettingsPage *SystrayNotificationBackend::configWidget() const {
121   return _configWidget;
122 }
123
124 /***************************************************************************/
125
126 SystrayNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent) : SettingsPage("Internal", "SystrayNotification", parent) {
127   QGroupBox *groupBox = new QGroupBox(tr("System Tray Icon"), this);
128   _animateBox = new QCheckBox(tr("Animate"));
129   connect(_animateBox, SIGNAL(toggled(bool)), this, SLOT(widgetChanged()));
130   _showBubbleBox = new QCheckBox(tr("Show bubble"));
131   connect(_showBubbleBox, SIGNAL(toggled(bool)), this, SLOT(widgetChanged()));
132   QVBoxLayout *layout = new QVBoxLayout(groupBox);
133   layout->addWidget(_animateBox);
134   layout->addWidget(_showBubbleBox);
135   layout->addStretch(1);
136   QVBoxLayout *globalLayout = new QVBoxLayout(this);
137   globalLayout->addWidget(groupBox);
138
139 }
140
141 void SystrayNotificationBackend::ConfigWidget::widgetChanged() {
142   bool changed = (_showBubble != _showBubbleBox->isChecked() || _animate != _animateBox->isChecked());
143   if(changed != hasChanged()) setChangedState(changed);
144 }
145
146 bool SystrayNotificationBackend::ConfigWidget::hasDefaults() const {
147   return true;
148 }
149
150 void SystrayNotificationBackend::ConfigWidget::defaults() {
151   _animateBox->setChecked(true);
152   _showBubbleBox->setChecked(false);
153   widgetChanged();
154 }
155
156 void SystrayNotificationBackend::ConfigWidget::load() {
157   NotificationSettings s;
158   _animate = s.value("Systray/Animate", true).toBool();
159   _showBubble = s.value("Systray/ShowBubble", false).toBool();
160   _animateBox->setChecked(_animate);
161   _showBubbleBox->setChecked(_showBubble);
162   setChangedState(false);
163 }
164
165 void SystrayNotificationBackend::ConfigWidget::save() {
166   NotificationSettings s;
167   s.setValue("Systray/Animate", _animateBox->isChecked());
168   s.setValue("Systray/ShowBubble", _showBubbleBox->isChecked());
169   load();
170 }