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 <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);
43 connect(QtUi::mainWindow()->systemTray(), SIGNAL(messageClicked(uint)), SLOT(notificationActivated(uint)));
44 connect(QtUi::mainWindow()->systemTray(), SIGNAL(activated(SystemTray::ActivationReason)),
45 SLOT(notificationActivated(SystemTray::ActivationReason)));
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::notificationActivated(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::notificationActivated(SystemTray::ActivationReason reason)
104 if (reason == SystemTray::Trigger) {
105 if (_notifications.count())
106 notificationActivated(_notifications.last().notificationId);
108 GraphicalUi::toggleMainWidget();
113 // moving the mouse or releasing the button means that we're not dealing with a double activation
114 bool SystrayNotificationBackend::eventFilter(QObject *obj, QEvent *event)
116 if (event->type() == QEvent::MouseMove || event->type() == QEvent::MouseButtonRelease) {
117 _blockActivation = false;
119 return AbstractNotificationBackend::eventFilter(obj, event);
123 void SystrayNotificationBackend::showBubbleChanged(const QVariant &v)
125 _showBubble = v.toBool();
129 void SystrayNotificationBackend::updateToolTip()
131 QtUi::mainWindow()->systemTray()->setToolTip("Quassel IRC",
132 _notifications.count() ? tr("%n pending highlight(s)", "", _notifications.count()) : QString());
136 SettingsPage *SystrayNotificationBackend::createConfigWidget() const
138 return new ConfigWidget();
142 /***************************************************************************/
144 SystrayNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent) : SettingsPage("Internal", "SystrayNotification", parent)
146 _showBubbleBox = new QCheckBox(tr("Show a message in a popup"));
147 _showBubbleBox->setIcon(QIcon::fromTheme("dialog-information"));
148 connect(_showBubbleBox, SIGNAL(toggled(bool)), this, SLOT(widgetChanged()));
149 QHBoxLayout *layout = new QHBoxLayout(this);
150 layout->addWidget(_showBubbleBox);
154 void SystrayNotificationBackend::ConfigWidget::widgetChanged()
156 bool changed = (_showBubble != _showBubbleBox->isChecked());
157 if (changed != hasChanged())
158 setChangedState(changed);
162 bool SystrayNotificationBackend::ConfigWidget::hasDefaults() const
168 void SystrayNotificationBackend::ConfigWidget::defaults()
170 _showBubbleBox->setChecked(false);
175 void SystrayNotificationBackend::ConfigWidget::load()
177 NotificationSettings s;
178 _showBubble = s.value("Systray/ShowBubble", false).toBool();
179 _showBubbleBox->setChecked(_showBubble);
180 setChangedState(false);
184 void SystrayNotificationBackend::ConfigWidget::save()
186 NotificationSettings s;
187 s.setValue("Systray/ShowBubble", _showBubbleBox->isChecked());