Fix modifier names for Mac
[quassel.git] / src / qtui / systraynotificationbackend.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2010 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include <QApplication>
22 #include <QCheckBox>
23 #include <QGroupBox>
24 #include <QHBoxLayout>
25
26 #include "systraynotificationbackend.h"
27
28 #include "client.h"
29 #include "clientsettings.h"
30 #include "icon.h"
31 #include "iconloader.h"
32 #include "mainwin.h"
33 #include "networkmodel.h"
34 #include "qtui.h"
35 #include "systemtray.h"
36
37 SystrayNotificationBackend::SystrayNotificationBackend(QObject *parent)
38   : AbstractNotificationBackend(parent),
39   _blockActivation(false)
40 {
41   NotificationSettings notificationSettings;
42   notificationSettings.initAndNotify("Systray/ShowBubble", this, SLOT(showBubbleChanged(QVariant)), true);
43   notificationSettings.initAndNotify("Systray/Animate", this, SLOT(animateChanged(QVariant)), true);
44
45   connect(QtUi::mainWindow()->systemTray(), SIGNAL(messageClicked(uint)), SLOT(notificationActivated(uint)));
46   connect(QtUi::mainWindow()->systemTray(), SIGNAL(activated(SystemTray::ActivationReason)),
47                                             SLOT(notificationActivated(SystemTray::ActivationReason)));
48
49   QApplication::instance()->installEventFilter(this);
50
51   updateToolTip();
52 }
53
54 void SystrayNotificationBackend::notify(const Notification &n) {
55   if(n.type != Highlight && n.type != PrivMsg)
56     return;
57
58   _notifications.append(n);
59   if(_showBubble) {
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);
63   }
64
65   if(_animate)
66     QtUi::mainWindow()->systemTray()->setAlert(true);
67
68   updateToolTip();
69 }
70
71 void SystrayNotificationBackend::close(uint notificationId) {
72   QList<Notification>::iterator i = _notifications.begin();
73   while(i != _notifications.end()) {
74     if(i->notificationId == notificationId)
75       i = _notifications.erase(i);
76     else
77       ++i;
78   }
79
80   QtUi::mainWindow()->systemTray()->closeMessage(notificationId);
81
82   //if(!_notifications.count()) //FIXME make configurable
83     QtUi::mainWindow()->systemTray()->setAlert(false);
84
85   updateToolTip();
86 }
87
88 void SystrayNotificationBackend::notificationActivated(uint notificationId) {
89   if(!_blockActivation) {
90     if(_notifications.count()) {
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       if(!notificationId)
94         notificationId = _notifications.count()? _notifications.last().notificationId : 0;
95       emit activated(notificationId);
96     } else
97       GraphicalUi::toggleMainWidget();
98   }
99 }
100
101 void SystrayNotificationBackend::notificationActivated(SystemTray::ActivationReason reason) {
102   if(reason == SystemTray::Trigger) {
103     notificationActivated(0);
104   }
105 }
106
107 // moving the mouse or releasing the button means that we're not dealing with a double activation
108 bool SystrayNotificationBackend::eventFilter(QObject *obj, QEvent *event) {
109   if(event->type() == QEvent::MouseMove || event->type() == QEvent::MouseButtonRelease) {
110     _blockActivation = false;
111   }
112   return AbstractNotificationBackend::eventFilter(obj, event);
113 }
114
115 void SystrayNotificationBackend::showBubbleChanged(const QVariant &v) {
116   _showBubble = v.toBool();
117 }
118
119 void SystrayNotificationBackend::animateChanged(const QVariant &v) {
120   _animate = v.toBool();
121 }
122
123 void SystrayNotificationBackend::updateToolTip() {
124   QtUi::mainWindow()->systemTray()->setToolTip("Quassel IRC",
125                                                _notifications.count()? tr("%n pending highlights", "", _notifications.count()) : QString());
126 }
127
128 SettingsPage *SystrayNotificationBackend::createConfigWidget() const {
129   return new ConfigWidget();
130 }
131
132 /***************************************************************************/
133
134 SystrayNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent) : SettingsPage("Internal", "SystrayNotification", parent) {
135   _showBubbleBox = new QCheckBox(tr("Show a message in a popup"));
136   _showBubbleBox->setIcon(SmallIcon("dialog-information"));
137   connect(_showBubbleBox, SIGNAL(toggled(bool)), this, SLOT(widgetChanged()));
138   QHBoxLayout *layout = new QHBoxLayout(this);
139   layout->addWidget(_showBubbleBox);
140 }
141
142 void SystrayNotificationBackend::ConfigWidget::widgetChanged() {
143   bool changed = (_showBubble != _showBubbleBox->isChecked());
144   if(changed != hasChanged())
145     setChangedState(changed);
146 }
147
148 bool SystrayNotificationBackend::ConfigWidget::hasDefaults() const {
149   return true;
150 }
151
152 void SystrayNotificationBackend::ConfigWidget::defaults() {
153   _showBubbleBox->setChecked(false);
154   widgetChanged();
155 }
156
157 void SystrayNotificationBackend::ConfigWidget::load() {
158   NotificationSettings s;
159   _showBubble = s.value("Systray/ShowBubble", false).toBool();
160   _showBubbleBox->setChecked(_showBubble);
161   setChangedState(false);
162 }
163
164 void SystrayNotificationBackend::ConfigWidget::save() {
165   NotificationSettings s;
166   s.setValue("Systray/ShowBubble", _showBubbleBox->isChecked());
167   load();
168 }