1 /***************************************************************************
2 * Copyright (C) 2005-2015 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 if (_notifications.count()) {
96 if (QtUi::mainWindow()->systemTray()->mode() == SystemTray::Legacy)
97 _blockActivation = true; // prevent double activation because both tray icon and bubble might send a signal
99 notificationId = _notifications.count() ? _notifications.last().notificationId : 0;
100 emit activated(notificationId);
103 GraphicalUi::toggleMainWidget();
108 void SystrayNotificationBackend::notificationActivated(SystemTray::ActivationReason reason)
110 if (reason == SystemTray::Trigger) {
111 notificationActivated(0);
116 // moving the mouse or releasing the button means that we're not dealing with a double activation
117 bool SystrayNotificationBackend::eventFilter(QObject *obj, QEvent *event)
119 if (event->type() == QEvent::MouseMove || event->type() == QEvent::MouseButtonRelease) {
120 _blockActivation = false;
122 return AbstractNotificationBackend::eventFilter(obj, event);
126 void SystrayNotificationBackend::showBubbleChanged(const QVariant &v)
128 _showBubble = v.toBool();
132 void SystrayNotificationBackend::animateChanged(const QVariant &v)
134 _animate = v.toBool();
138 void SystrayNotificationBackend::updateToolTip()
140 QtUi::mainWindow()->systemTray()->setToolTip("Quassel IRC",
141 _notifications.count() ? tr("%n pending highlight(s)", "", _notifications.count()) : QString());
145 SettingsPage *SystrayNotificationBackend::createConfigWidget() const
147 return new ConfigWidget();
151 /***************************************************************************/
153 SystrayNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent) : SettingsPage("Internal", "SystrayNotification", parent)
155 _showBubbleBox = new QCheckBox(tr("Show a message in a popup"));
156 _showBubbleBox->setIcon(QIcon::fromTheme("dialog-information"));
157 connect(_showBubbleBox, SIGNAL(toggled(bool)), this, SLOT(widgetChanged()));
158 QHBoxLayout *layout = new QHBoxLayout(this);
159 layout->addWidget(_showBubbleBox);
163 void SystrayNotificationBackend::ConfigWidget::widgetChanged()
165 bool changed = (_showBubble != _showBubbleBox->isChecked());
166 if (changed != hasChanged())
167 setChangedState(changed);
171 bool SystrayNotificationBackend::ConfigWidget::hasDefaults() const
177 void SystrayNotificationBackend::ConfigWidget::defaults()
179 _showBubbleBox->setChecked(false);
184 void SystrayNotificationBackend::ConfigWidget::load()
186 NotificationSettings s;
187 _showBubble = s.value("Systray/ShowBubble", false).toBool();
188 _showBubbleBox->setChecked(_showBubble);
189 setChangedState(false);
193 void SystrayNotificationBackend::ConfigWidget::save()
195 NotificationSettings s;
196 s.setValue("Systray/ShowBubble", _showBubbleBox->isChecked());