1 /***************************************************************************
2 * Copyright (C) 2005-2010 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include <QApplication>
24 #include <QVBoxLayout>
26 #include "systraynotificationbackend.h"
29 #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);
53 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);
65 QtUi::mainWindow()->systemTray()->setAlert(true);
70 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);
81 if(!_notifications.count())
82 QtUi::mainWindow()->systemTray()->setAlert(false);
87 void SystrayNotificationBackend::notificationActivated(uint notificationId) {
88 if(!_blockActivation) {
89 if(QtUi::mainWindow()->systemTray()->isAlerted()) {
90 _blockActivation = true; // prevent double activation because both tray icon and bubble might send a signal
92 notificationId = _notifications.count()? _notifications.last().notificationId : 0;
93 emit activated(notificationId);
95 GraphicalUi::toggleMainWidget();
99 void SystrayNotificationBackend::notificationActivated(SystemTray::ActivationReason reason) {
100 if(reason == SystemTray::Trigger) {
101 notificationActivated(0);
105 // moving the mouse or releasing the button means that we're not dealing with a double activation
106 bool SystrayNotificationBackend::eventFilter(QObject *obj, QEvent *event) {
107 if(event->type() == QEvent::MouseMove || event->type() == QEvent::MouseButtonRelease) {
108 _blockActivation = false;
110 return AbstractNotificationBackend::eventFilter(obj, event);
113 void SystrayNotificationBackend::showBubbleChanged(const QVariant &v) {
114 _showBubble = v.toBool();
117 void SystrayNotificationBackend::animateChanged(const QVariant &v) {
118 _animate = v.toBool();
121 void SystrayNotificationBackend::updateToolTip() {
122 QtUi::mainWindow()->systemTray()->setToolTip("Quassel IRC",
123 _notifications.count()? tr("%n pending highlights", "", _notifications.count()) : QString());
126 SettingsPage *SystrayNotificationBackend::createConfigWidget() const {
127 return new ConfigWidget();
130 /***************************************************************************/
132 SystrayNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent) : SettingsPage("Internal", "SystrayNotification", parent) {
133 QGroupBox *groupBox = new QGroupBox(tr("System Tray Icon"), this);
134 _animateBox = new QCheckBox(tr("Animate"));
135 connect(_animateBox, SIGNAL(toggled(bool)), this, SLOT(widgetChanged()));
136 _showBubbleBox = new QCheckBox(tr("Show bubble"));
137 connect(_showBubbleBox, SIGNAL(toggled(bool)), this, SLOT(widgetChanged()));
138 QVBoxLayout *layout = new QVBoxLayout(groupBox);
139 layout->addWidget(_animateBox);
140 layout->addWidget(_showBubbleBox);
141 layout->addStretch(1);
142 QVBoxLayout *globalLayout = new QVBoxLayout(this);
143 globalLayout->addWidget(groupBox);
147 void SystrayNotificationBackend::ConfigWidget::widgetChanged() {
148 bool changed = (_showBubble != _showBubbleBox->isChecked() || _animate != _animateBox->isChecked());
149 if(changed != hasChanged()) setChangedState(changed);
152 bool SystrayNotificationBackend::ConfigWidget::hasDefaults() const {
156 void SystrayNotificationBackend::ConfigWidget::defaults() {
157 _animateBox->setChecked(true);
158 _showBubbleBox->setChecked(false);
162 void SystrayNotificationBackend::ConfigWidget::load() {
163 NotificationSettings s;
164 _animate = s.value("Systray/Animate", true).toBool();
165 _showBubble = s.value("Systray/ShowBubble", false).toBool();
166 _animateBox->setChecked(_animate);
167 _showBubbleBox->setChecked(_showBubble);
168 setChangedState(false);
171 void SystrayNotificationBackend::ConfigWidget::save() {
172 NotificationSettings s;
173 s.setValue("Systray/Animate", _animateBox->isChecked());
174 s.setValue("Systray/ShowBubble", _showBubbleBox->isChecked());