1 /***************************************************************************
2 * Copyright (C) 2005-2018 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 "systraynotificationbackend.h"
23 #include <QApplication>
26 #include <QHBoxLayout>
29 #include "clientsettings.h"
32 #include "networkmodel.h"
34 #include "systemtray.h"
36 SystrayNotificationBackend::SystrayNotificationBackend(QObject *parent)
37 : AbstractNotificationBackend(parent)
39 NotificationSettings notificationSettings;
40 notificationSettings.initAndNotify("Systray/ShowBubble", this, SLOT(showBubbleChanged(QVariant)), true);
42 connect(QtUi::mainWindow()->systemTray(), &SystemTray::messageClicked,
43 this, selectOverload<uint>(&SystrayNotificationBackend::onNotificationActivated));
44 connect(QtUi::mainWindow()->systemTray(), &SystemTray::activated,
45 this, selectOverload<SystemTray::ActivationReason>(&SystrayNotificationBackend::onNotificationActivated));
47 QApplication::instance()->installEventFilter(this);
53 void SystrayNotificationBackend::notify(const Notification &n)
55 if (n.type != Highlight && n.type != PrivMsg)
58 _notifications.append(n);
60 QString title = Client::networkModel()->networkName(n.bufferId) + " - " + Client::networkModel()->bufferName(n.bufferId);
61 QString message = QString("<%1> %2").arg(n.sender, n.message);
62 QtUi::mainWindow()->systemTray()->showMessage(title, message, SystemTray::Information, 10000, n.notificationId);
69 void SystrayNotificationBackend::close(uint notificationId)
71 QList<Notification>::iterator i = _notifications.begin();
72 while (i != _notifications.end()) {
73 if (i->notificationId == notificationId)
74 i = _notifications.erase(i);
79 QtUi::mainWindow()->systemTray()->closeMessage(notificationId);
85 void SystrayNotificationBackend::onNotificationActivated(uint notificationId)
87 if (!_blockActivation) {
88 QList<Notification>::iterator i = _notifications.begin();
89 while (i != _notifications.end()) {
90 if (i->notificationId == notificationId) {
91 if (QtUi::mainWindow()->systemTray()->mode() == SystemTray::Legacy)
92 _blockActivation = true; // prevent double activation because both tray icon and bubble might send a signal
93 emit activated(notificationId);
102 void SystrayNotificationBackend::onNotificationActivated(SystemTray::ActivationReason reason)
104 if (reason == SystemTray::Trigger) {
105 if (_notifications.count()) {
106 onNotificationActivated(_notifications.last().notificationId);
109 GraphicalUi::toggleMainWidget();
115 // moving the mouse or releasing the button means that we're not dealing with a double activation
116 bool SystrayNotificationBackend::eventFilter(QObject *obj, QEvent *event)
118 if (event->type() == QEvent::MouseMove || event->type() == QEvent::MouseButtonRelease) {
119 _blockActivation = false;
121 return AbstractNotificationBackend::eventFilter(obj, event);
125 void SystrayNotificationBackend::showBubbleChanged(const QVariant &v)
127 _showBubble = v.toBool();
131 void SystrayNotificationBackend::updateToolTip()
133 QtUi::mainWindow()->systemTray()->setToolTip("Quassel IRC",
134 _notifications.count() ? tr("%n pending highlight(s)", "", _notifications.count()) : QString());
138 SettingsPage *SystrayNotificationBackend::createConfigWidget() const
140 return new ConfigWidget();
144 /***************************************************************************/
146 SystrayNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent) : SettingsPage("Internal", "SystrayNotification", parent)
148 _showBubbleBox = new QCheckBox(tr("Show a message in a popup"));
149 _showBubbleBox->setIcon(icon::get("dialog-information"));
150 connect(_showBubbleBox, &QAbstractButton::toggled, this, &ConfigWidget::widgetChanged);
151 auto *layout = new QHBoxLayout(this);
152 layout->addWidget(_showBubbleBox);
156 void SystrayNotificationBackend::ConfigWidget::widgetChanged()
158 bool changed = (_showBubble != _showBubbleBox->isChecked());
159 if (changed != hasChanged())
160 setChangedState(changed);
164 bool SystrayNotificationBackend::ConfigWidget::hasDefaults() const
170 void SystrayNotificationBackend::ConfigWidget::defaults()
172 _showBubbleBox->setChecked(false);
177 void SystrayNotificationBackend::ConfigWidget::load()
179 NotificationSettings s;
180 _showBubble = s.value("Systray/ShowBubble", false).toBool();
181 _showBubbleBox->setChecked(_showBubble);
182 setChangedState(false);
186 void SystrayNotificationBackend::ConfigWidget::save()
188 NotificationSettings s;
189 s.setValue("Systray/ShowBubble", _showBubbleBox->isChecked());