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