1 /***************************************************************************
2 * Copyright (C) 2005-09 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include "systraynotificationbackend.h"
26 #include "clientsettings.h"
29 #include "networkmodel.h"
31 #include "systemtray.h"
33 SystrayNotificationBackend::SystrayNotificationBackend(QObject *parent)
34 : AbstractNotificationBackend(parent), _activeId(0)
36 NotificationSettings notificationSettings;
37 _showBubble = notificationSettings.value("Systray/ShowBubble", true).toBool();
38 _animate = notificationSettings.value("Systray/Animate", true).toBool();
40 notificationSettings.notify("Systray/ShowBubble", this, SLOT(showBubbleChanged(const QVariant &)));
41 notificationSettings.notify("Systray/Animate", this, SLOT(animateChanged(const QVariant &)));
43 connect(QtUi::mainWindow()->systemTray(), SIGNAL(messageClicked()), SLOT(notificationActivated()));
44 connect(QtUi::mainWindow()->systemTray(), SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
45 SLOT(notificationActivated(QSystemTrayIcon::ActivationReason)));
48 void SystrayNotificationBackend::notify(const Notification ¬ification) {
49 if(notification.type != Highlight && notification.type != PrivMsg)
52 /* fancy stuff to be implemented later: show notifications in order
53 _notifications.append(notification);
54 if(_showBubble && _notifications.count() == 1) {
58 _notifications.clear();
59 _notifications.append(notification);
64 QtUi::mainWindow()->systemTray()->setAlert(true);
67 void SystrayNotificationBackend::close(uint notificationId) {
68 Q_UNUSED(notificationId);
69 /* fancy stuff to be implemented later
70 int idx = _notifications.indexOf(notificationId);
72 if(_notifications.isEmpty()) {
74 _notifications.clear();
77 QtUi::mainWindow()->systemTray()->setAlert(false);
80 void SystrayNotificationBackend::showBubble() {
81 // fancy stuff later: show messages in order
82 // for now, we just show the last message
83 if(_notifications.isEmpty())
85 Notification n = _notifications.takeLast();
86 _activeId = n.notificationId;
87 QString title = Client::networkModel()->networkName(n.bufferId) + " - " + Client::networkModel()->bufferName(n.bufferId);
88 QString message = QString("<%1> %2").arg(n.sender, n.message);
89 QtUi::mainWindow()->systemTray()->showMessage(title, message);
92 void SystrayNotificationBackend::closeBubble() {
93 // there really seems to be no sane way to close the bubble... :(
95 QtUi::mainWindow()->systemTray()->showMessage("", "", QSystemTrayIcon::NoIcon, 1);
99 void SystrayNotificationBackend::notificationActivated() {
100 if(QtUi::mainWindow()->systemTray()->isAlerted()) {
101 QtUi::mainWindow()->systemTray()->setInhibitActivation();
102 emit activated(_activeId);
106 void SystrayNotificationBackend::notificationActivated(QSystemTrayIcon::ActivationReason reason) {
107 if(reason == QSystemTrayIcon::Trigger) {
108 notificationActivated();
112 void SystrayNotificationBackend::showBubbleChanged(const QVariant &v) {
113 _showBubble = v.toBool();
116 void SystrayNotificationBackend::animateChanged(const QVariant &v) {
117 _animate = v.toBool();
120 SettingsPage *SystrayNotificationBackend::createConfigWidget() const {
121 return new ConfigWidget();
124 /***************************************************************************/
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);
141 void SystrayNotificationBackend::ConfigWidget::widgetChanged() {
142 bool changed = (_showBubble != _showBubbleBox->isChecked() || _animate != _animateBox->isChecked());
143 if(changed != hasChanged()) setChangedState(changed);
146 bool SystrayNotificationBackend::ConfigWidget::hasDefaults() const {
150 void SystrayNotificationBackend::ConfigWidget::defaults() {
151 _animateBox->setChecked(true);
152 _showBubbleBox->setChecked(false);
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);
165 void SystrayNotificationBackend::ConfigWidget::save() {
166 NotificationSettings s;
167 s.setValue("Systray/Animate", _animateBox->isChecked());
168 s.setValue("Systray/ShowBubble", _showBubbleBox->isChecked());