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(), SIGNAL(messageClicked(uint)), SLOT(notificationActivated(uint)));
43 connect(QtUi::mainWindow()->systemTray(), SIGNAL(activated(SystemTray::ActivationReason)),
44 SLOT(notificationActivated(SystemTray::ActivationReason)));
46 QApplication::instance()->installEventFilter(this);
52 void SystrayNotificationBackend::notify(const Notification &n)
54 if (n.type != Highlight && n.type != PrivMsg)
57 _notifications.append(n);
59 QString title = Client::networkModel()->networkName(n.bufferId) + " - " + Client::networkModel()->bufferName(n.bufferId);
60 QString message = QString("<%1> %2").arg(n.sender, n.message);
61 QtUi::mainWindow()->systemTray()->showMessage(title, message, SystemTray::Information, 10000, n.notificationId);
68 void SystrayNotificationBackend::close(uint notificationId)
70 QList<Notification>::iterator i = _notifications.begin();
71 while (i != _notifications.end()) {
72 if (i->notificationId == notificationId)
73 i = _notifications.erase(i);
78 QtUi::mainWindow()->systemTray()->closeMessage(notificationId);
84 void SystrayNotificationBackend::notificationActivated(uint notificationId)
86 if (!_blockActivation) {
87 QList<Notification>::iterator i = _notifications.begin();
88 while (i != _notifications.end()) {
89 if (i->notificationId == notificationId) {
90 if (QtUi::mainWindow()->systemTray()->mode() == SystemTray::Legacy)
91 _blockActivation = true; // prevent double activation because both tray icon and bubble might send a signal
92 emit activated(notificationId);
101 void SystrayNotificationBackend::notificationActivated(SystemTray::ActivationReason reason)
103 if (reason == SystemTray::Trigger) {
104 if (_notifications.count())
105 notificationActivated(_notifications.last().notificationId);
107 GraphicalUi::toggleMainWidget();
112 // moving the mouse or releasing the button means that we're not dealing with a double activation
113 bool SystrayNotificationBackend::eventFilter(QObject *obj, QEvent *event)
115 if (event->type() == QEvent::MouseMove || event->type() == QEvent::MouseButtonRelease) {
116 _blockActivation = false;
118 return AbstractNotificationBackend::eventFilter(obj, event);
122 void SystrayNotificationBackend::showBubbleChanged(const QVariant &v)
124 _showBubble = v.toBool();
128 void SystrayNotificationBackend::updateToolTip()
130 QtUi::mainWindow()->systemTray()->setToolTip("Quassel IRC",
131 _notifications.count() ? tr("%n pending highlight(s)", "", _notifications.count()) : QString());
135 SettingsPage *SystrayNotificationBackend::createConfigWidget() const
137 return new ConfigWidget();
141 /***************************************************************************/
143 SystrayNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent) : 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);
153 void SystrayNotificationBackend::ConfigWidget::widgetChanged()
155 bool changed = (_showBubble != _showBubbleBox->isChecked());
156 if (changed != hasChanged())
157 setChangedState(changed);
161 bool SystrayNotificationBackend::ConfigWidget::hasDefaults() const
167 void SystrayNotificationBackend::ConfigWidget::defaults()
169 _showBubbleBox->setChecked(false);
174 void SystrayNotificationBackend::ConfigWidget::load()
176 NotificationSettings s;
177 _showBubble = s.value("Systray/ShowBubble", false).toBool();
178 _showBubbleBox->setChecked(_showBubble);
179 setChangedState(false);
183 void SystrayNotificationBackend::ConfigWidget::save()
185 NotificationSettings s;
186 s.setValue("Systray/ShowBubble", _showBubbleBox->isChecked());