Disable the highlights page when not connected
[quassel.git] / src / qtui / systraynotificationbackend.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2016 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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include <QApplication>
22 #include <QCheckBox>
23 #include <QGroupBox>
24 #include <QIcon>
25 #include <QHBoxLayout>
26
27 #include "systraynotificationbackend.h"
28
29 #include "client.h"
30 #include "clientsettings.h"
31 #include "mainwin.h"
32 #include "networkmodel.h"
33 #include "qtui.h"
34 #include "systemtray.h"
35
36 SystrayNotificationBackend::SystrayNotificationBackend(QObject *parent)
37     : AbstractNotificationBackend(parent),
38     _blockActivation(false)
39 {
40     NotificationSettings notificationSettings;
41     notificationSettings.initAndNotify("Systray/ShowBubble", this, SLOT(showBubbleChanged(QVariant)), true);
42     notificationSettings.initAndNotify("Systray/Animate", this, SLOT(animateChanged(QVariant)), true);
43
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)));
47
48     QApplication::instance()->installEventFilter(this);
49
50     updateToolTip();
51 }
52
53
54 void SystrayNotificationBackend::notify(const Notification &n)
55 {
56     if (n.type != Highlight && n.type != PrivMsg)
57         return;
58
59     _notifications.append(n);
60     if (_showBubble) {
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);
64     }
65
66     if (_animate)
67         QtUi::mainWindow()->systemTray()->setAlert(true);
68
69     updateToolTip();
70 }
71
72
73 void SystrayNotificationBackend::close(uint notificationId)
74 {
75     QList<Notification>::iterator i = _notifications.begin();
76     while (i != _notifications.end()) {
77         if (i->notificationId == notificationId)
78             i = _notifications.erase(i);
79         else
80             ++i;
81     }
82
83     QtUi::mainWindow()->systemTray()->closeMessage(notificationId);
84
85     //if(!_notifications.count()) //FIXME make configurable
86     QtUi::mainWindow()->systemTray()->setAlert(false);
87
88     updateToolTip();
89 }
90
91
92 void SystrayNotificationBackend::notificationActivated(uint notificationId)
93 {
94     if (!_blockActivation) {
95         QList<Notification>::iterator i = _notifications.begin();
96         while (i != _notifications.end()) {
97             if (i->notificationId == notificationId) {
98                 if (QtUi::mainWindow()->systemTray()->mode() == SystemTray::Legacy)
99                     _blockActivation = true;  // prevent double activation because both tray icon and bubble might send a signal
100                 emit activated(notificationId);
101                 break;
102             }
103         ++i;
104         }
105     }
106 }
107
108
109 void SystrayNotificationBackend::notificationActivated(SystemTray::ActivationReason reason)
110 {
111     if (reason == SystemTray::Trigger) {
112         if (_notifications.count())
113             notificationActivated(_notifications.last().notificationId);
114         else
115             GraphicalUi::toggleMainWidget();
116     }
117 }
118
119
120 // moving the mouse or releasing the button means that we're not dealing with a double activation
121 bool SystrayNotificationBackend::eventFilter(QObject *obj, QEvent *event)
122 {
123     if (event->type() == QEvent::MouseMove || event->type() == QEvent::MouseButtonRelease) {
124         _blockActivation = false;
125     }
126     return AbstractNotificationBackend::eventFilter(obj, event);
127 }
128
129
130 void SystrayNotificationBackend::showBubbleChanged(const QVariant &v)
131 {
132     _showBubble = v.toBool();
133 }
134
135
136 void SystrayNotificationBackend::animateChanged(const QVariant &v)
137 {
138     _animate = v.toBool();
139 }
140
141
142 void SystrayNotificationBackend::updateToolTip()
143 {
144     QtUi::mainWindow()->systemTray()->setToolTip("Quassel IRC",
145         _notifications.count() ? tr("%n pending highlight(s)", "", _notifications.count()) : QString());
146 }
147
148
149 SettingsPage *SystrayNotificationBackend::createConfigWidget() const
150 {
151     return new ConfigWidget();
152 }
153
154
155 /***************************************************************************/
156
157 SystrayNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent) : SettingsPage("Internal", "SystrayNotification", parent)
158 {
159     _showBubbleBox = new QCheckBox(tr("Show a message in a popup"));
160     _showBubbleBox->setIcon(QIcon::fromTheme("dialog-information"));
161     connect(_showBubbleBox, SIGNAL(toggled(bool)), this, SLOT(widgetChanged()));
162     QHBoxLayout *layout = new QHBoxLayout(this);
163     layout->addWidget(_showBubbleBox);
164 }
165
166
167 void SystrayNotificationBackend::ConfigWidget::widgetChanged()
168 {
169     bool changed = (_showBubble != _showBubbleBox->isChecked());
170     if (changed != hasChanged())
171         setChangedState(changed);
172 }
173
174
175 bool SystrayNotificationBackend::ConfigWidget::hasDefaults() const
176 {
177     return true;
178 }
179
180
181 void SystrayNotificationBackend::ConfigWidget::defaults()
182 {
183     _showBubbleBox->setChecked(false);
184     widgetChanged();
185 }
186
187
188 void SystrayNotificationBackend::ConfigWidget::load()
189 {
190     NotificationSettings s;
191     _showBubble = s.value("Systray/ShowBubble", false).toBool();
192     _showBubbleBox->setChecked(_showBubble);
193     setChangedState(false);
194 }
195
196
197 void SystrayNotificationBackend::ConfigWidget::save()
198 {
199     NotificationSettings s;
200     s.setValue("Systray/ShowBubble", _showBubbleBox->isChecked());
201     load();
202 }