1 /***************************************************************************
2 * Copyright (C) 2005-2016 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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include <QApplication>
25 #include <QHBoxLayout>
27 #include "systraynotificationbackend.h"
30 #include "clientsettings.h"
32 #include "networkmodel.h"
34 #include "systemtray.h"
36 SystrayNotificationBackend::SystrayNotificationBackend(QObject *parent)
37 : AbstractNotificationBackend(parent),
38 _blockActivation(false)
40 NotificationSettings notificationSettings;
41 notificationSettings.initAndNotify("Systray/ShowBubble", this, SLOT(showBubbleChanged(QVariant)), true);
42 notificationSettings.initAndNotify("Systray/Animate", this, SLOT(animateChanged(QVariant)), true);
44 connect(QtUi::mainWindow()->systemTray(), SIGNAL(messageClicked(uint)), SLOT(notificationActivated(uint)));
45 connect(QtUi::mainWindow()->systemTray(), SIGNAL(activated(SystemTray::ActivationReason)),
46 SLOT(notificationActivated(SystemTray::ActivationReason)));
48 QApplication::instance()->installEventFilter(this);
54 void SystrayNotificationBackend::notify(const Notification &n)
56 if (n.type != Highlight && n.type != PrivMsg)
59 _notifications.append(n);
61 QString title = Client::networkModel()->networkName(n.bufferId) + " - " + Client::networkModel()->bufferName(n.bufferId);
62 QString message = QString("<%1> %2").arg(n.sender, n.message);
63 QtUi::mainWindow()->systemTray()->showMessage(title, message, SystemTray::Information, 10000, n.notificationId);
67 QtUi::mainWindow()->systemTray()->setAlert(true);
73 void SystrayNotificationBackend::close(uint notificationId)
75 QList<Notification>::iterator i = _notifications.begin();
76 while (i != _notifications.end()) {
77 if (i->notificationId == notificationId)
78 i = _notifications.erase(i);
83 QtUi::mainWindow()->systemTray()->closeMessage(notificationId);
85 //if(!_notifications.count()) //FIXME make configurable
86 QtUi::mainWindow()->systemTray()->setAlert(false);
92 void SystrayNotificationBackend::notificationActivated(uint notificationId)
94 if (!_blockActivation) {
95 QList<Notification>::iterator i = _notifications.begin();
96 while (i != _notifications.end()) {
97 if (i->notificationId == notificationId) {
98 if (QtUi::mainWindow()->systemTray()->mode() == SystemTray::Legacy)
99 _blockActivation = true; // prevent double activation because both tray icon and bubble might send a signal
100 emit activated(notificationId);
109 void SystrayNotificationBackend::notificationActivated(SystemTray::ActivationReason reason)
111 if (reason == SystemTray::Trigger) {
112 if (_notifications.count())
113 notificationActivated(_notifications.last().notificationId);
115 GraphicalUi::toggleMainWidget();
120 // moving the mouse or releasing the button means that we're not dealing with a double activation
121 bool SystrayNotificationBackend::eventFilter(QObject *obj, QEvent *event)
123 if (event->type() == QEvent::MouseMove || event->type() == QEvent::MouseButtonRelease) {
124 _blockActivation = false;
126 return AbstractNotificationBackend::eventFilter(obj, event);
130 void SystrayNotificationBackend::showBubbleChanged(const QVariant &v)
132 _showBubble = v.toBool();
136 void SystrayNotificationBackend::animateChanged(const QVariant &v)
138 _animate = v.toBool();
142 void SystrayNotificationBackend::updateToolTip()
144 QtUi::mainWindow()->systemTray()->setToolTip("Quassel IRC",
145 _notifications.count() ? tr("%n pending highlight(s)", "", _notifications.count()) : QString());
149 SettingsPage *SystrayNotificationBackend::createConfigWidget() const
151 return new ConfigWidget();
155 /***************************************************************************/
157 SystrayNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent) : SettingsPage("Internal", "SystrayNotification", parent)
159 _showBubbleBox = new QCheckBox(tr("Show a message in a popup"));
160 _showBubbleBox->setIcon(QIcon::fromTheme("dialog-information"));
161 connect(_showBubbleBox, SIGNAL(toggled(bool)), this, SLOT(widgetChanged()));
162 QHBoxLayout *layout = new QHBoxLayout(this);
163 layout->addWidget(_showBubbleBox);
167 void SystrayNotificationBackend::ConfigWidget::widgetChanged()
169 bool changed = (_showBubble != _showBubbleBox->isChecked());
170 if (changed != hasChanged())
171 setChangedState(changed);
175 bool SystrayNotificationBackend::ConfigWidget::hasDefaults() const
181 void SystrayNotificationBackend::ConfigWidget::defaults()
183 _showBubbleBox->setChecked(false);
188 void SystrayNotificationBackend::ConfigWidget::load()
190 NotificationSettings s;
191 _showBubble = s.value("Systray/ShowBubble", false).toBool();
192 _showBubbleBox->setChecked(_showBubble);
193 setChangedState(false);
197 void SystrayNotificationBackend::ConfigWidget::save()
199 NotificationSettings s;
200 s.setValue("Systray/ShowBubble", _showBubbleBox->isChecked());