Fix Quassel not rejoining newly joined channels
[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)
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   connect(QtUi::mainWindow()->systemTray(), SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
45                                             SLOT(notificationActivated(QSystemTrayIcon::ActivationReason)));
46 }
47
48 void SystrayNotificationBackend::notify(const Notification &notification) {
49   if(notification.type != Highlight && notification.type != PrivMsg)
50     return;
51
52   _notifications.append(notification);
53   if(_showBubble)
54     showBubble();
55
56   if(_animate)
57     QtUi::mainWindow()->systemTray()->setAlert(true);
58 }
59
60 void SystrayNotificationBackend::close(uint notificationId) {
61   QList<Notification>::iterator i = _notifications.begin();
62   while(i != _notifications.end()) {
63     if(i->notificationId == notificationId)
64       i = _notifications.erase(i);
65     else
66       ++i;
67   }
68
69   closeBubble();
70
71   if(!_notifications.count())
72     QtUi::mainWindow()->systemTray()->setAlert(false);
73 }
74
75 void SystrayNotificationBackend::showBubble() {
76   // fancy stuff later: show messages in order
77   // for now, we just show the last message
78   if(_notifications.isEmpty())
79     return;
80   Notification n = _notifications.last();
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()->systemTray()->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()->systemTray()->showMessage("", "", QSystemTrayIcon::NoIcon, 1);
90 #endif
91 }
92
93 void SystrayNotificationBackend::notificationActivated() {
94   if(QtUi::mainWindow()->systemTray()->isAlerted()) {
95     QtUi::mainWindow()->systemTray()->setInhibitActivation();
96     uint id = _notifications.count()? _notifications.last().notificationId : 0;
97     emit activated(id);
98   }
99 }
100
101 void SystrayNotificationBackend::notificationActivated(QSystemTrayIcon::ActivationReason reason) {
102   if(reason == QSystemTrayIcon::Trigger) {
103     notificationActivated();
104   }
105 }
106
107 void SystrayNotificationBackend::showBubbleChanged(const QVariant &v) {
108   _showBubble = v.toBool();
109 }
110
111 void SystrayNotificationBackend::animateChanged(const QVariant &v) {
112   _animate = v.toBool();
113 }
114
115 SettingsPage *SystrayNotificationBackend::createConfigWidget() const {
116   return new ConfigWidget();
117 }
118
119 /***************************************************************************/
120
121 SystrayNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent) : SettingsPage("Internal", "SystrayNotification", parent) {
122   QGroupBox *groupBox = new QGroupBox(tr("System Tray Icon"), this);
123   _animateBox = new QCheckBox(tr("Animate"));
124   connect(_animateBox, SIGNAL(toggled(bool)), this, SLOT(widgetChanged()));
125   _showBubbleBox = new QCheckBox(tr("Show bubble"));
126   connect(_showBubbleBox, SIGNAL(toggled(bool)), this, SLOT(widgetChanged()));
127   QVBoxLayout *layout = new QVBoxLayout(groupBox);
128   layout->addWidget(_animateBox);
129   layout->addWidget(_showBubbleBox);
130   layout->addStretch(1);
131   QVBoxLayout *globalLayout = new QVBoxLayout(this);
132   globalLayout->addWidget(groupBox);
133
134 }
135
136 void SystrayNotificationBackend::ConfigWidget::widgetChanged() {
137   bool changed = (_showBubble != _showBubbleBox->isChecked() || _animate != _animateBox->isChecked());
138   if(changed != hasChanged()) setChangedState(changed);
139 }
140
141 bool SystrayNotificationBackend::ConfigWidget::hasDefaults() const {
142   return true;
143 }
144
145 void SystrayNotificationBackend::ConfigWidget::defaults() {
146   _animateBox->setChecked(true);
147   _showBubbleBox->setChecked(false);
148   widgetChanged();
149 }
150
151 void SystrayNotificationBackend::ConfigWidget::load() {
152   NotificationSettings s;
153   _animate = s.value("Systray/Animate", true).toBool();
154   _showBubble = s.value("Systray/ShowBubble", false).toBool();
155   _animateBox->setChecked(_animate);
156   _showBubbleBox->setChecked(_showBubble);
157   setChangedState(false);
158 }
159
160 void SystrayNotificationBackend::ConfigWidget::save() {
161   NotificationSettings s;
162   s.setValue("Systray/Animate", _animateBox->isChecked());
163   s.setValue("Systray/ShowBubble", _showBubbleBox->isChecked());
164   load();
165 }