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, &SystrayNotificationBackend::showBubbleChanged, true);
42 connect(QtUi::mainWindow()->systemTray(),
43 &SystemTray::messageClicked,
45 selectOverload<uint>(&SystrayNotificationBackend::onNotificationActivated));
46 connect(QtUi::mainWindow()->systemTray(),
47 &SystemTray::activated,
49 selectOverload<SystemTray::ActivationReason>(&SystrayNotificationBackend::onNotificationActivated));
51 QApplication::instance()->installEventFilter(this);
56 void SystrayNotificationBackend::notify(const Notification& n)
58 if (n.type != Highlight && n.type != PrivMsg)
61 _notifications.append(n);
63 QString title = Client::networkModel()->networkName(n.bufferId) + " - " + Client::networkModel()->bufferName(n.bufferId);
64 QString message = QString("<%1> %2").arg(n.sender, n.message);
65 QtUi::mainWindow()->systemTray()->showMessage(title, message, SystemTray::Information, 10000, n.notificationId);
71 void SystrayNotificationBackend::close(uint notificationId)
73 QList<Notification>::iterator i = _notifications.begin();
74 while (i != _notifications.end()) {
75 if (i->notificationId == notificationId)
76 i = _notifications.erase(i);
81 QtUi::mainWindow()->systemTray()->closeMessage(notificationId);
86 void SystrayNotificationBackend::onNotificationActivated(uint notificationId)
88 if (!_blockActivation) {
89 QList<Notification>::iterator i = _notifications.begin();
90 while (i != _notifications.end()) {
91 if (i->notificationId == notificationId) {
92 if (QtUi::mainWindow()->systemTray()->mode() == SystemTray::Legacy)
93 _blockActivation = true; // prevent double activation because both tray icon and bubble might send a signal
94 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();
114 // moving the mouse or releasing the button means that we're not dealing with a double activation
115 bool SystrayNotificationBackend::eventFilter(QObject* obj, QEvent* event)
117 if (event->type() == QEvent::MouseMove || event->type() == QEvent::MouseButtonRelease) {
118 _blockActivation = false;
120 return AbstractNotificationBackend::eventFilter(obj, event);
123 void SystrayNotificationBackend::showBubbleChanged(const QVariant& v)
125 _showBubble = v.toBool();
128 void SystrayNotificationBackend::updateToolTip()
130 QtUi::mainWindow()->systemTray()->setToolTip("Quassel IRC",
131 _notifications.count() ? tr("%n pending highlight(s)", "", _notifications.count())
135 SettingsPage* SystrayNotificationBackend::createConfigWidget() const
137 return new ConfigWidget();
140 /***************************************************************************/
142 SystrayNotificationBackend::ConfigWidget::ConfigWidget(QWidget* parent)
143 : SettingsPage("Internal", "SystrayNotification", parent)
145 _showBubbleBox = new QCheckBox(tr("Show a message in a popup"));
146 _showBubbleBox->setIcon(icon::get("dialog-information"));
147 connect(_showBubbleBox, &QAbstractButton::toggled, this, &ConfigWidget::widgetChanged);
148 auto* layout = new QHBoxLayout(this);
149 layout->addWidget(_showBubbleBox);
152 void SystrayNotificationBackend::ConfigWidget::widgetChanged()
154 bool changed = (_showBubble != _showBubbleBox->isChecked());
155 if (changed != hasChanged())
156 setChangedState(changed);
159 bool SystrayNotificationBackend::ConfigWidget::hasDefaults() const
164 void SystrayNotificationBackend::ConfigWidget::defaults()
166 _showBubbleBox->setChecked(false);
170 void SystrayNotificationBackend::ConfigWidget::load()
172 NotificationSettings s;
173 _showBubble = s.value("Systray/ShowBubble", false).toBool();
174 _showBubbleBox->setChecked(_showBubble);
175 setChangedState(false);
178 void SystrayNotificationBackend::ConfigWidget::save()
180 NotificationSettings s;
181 s.setValue("Systray/ShowBubble", _showBubbleBox->isChecked());