4e024daf9eddeb81ea0da2e1313c9c495c870a77
[quassel.git] / src / qtui / systraynotificationbackend.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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 "systraynotificationbackend.h"
22
23 #include <QApplication>
24 #include <QCheckBox>
25 #include <QGroupBox>
26 #include <QHBoxLayout>
27
28 #include "client.h"
29 #include "clientsettings.h"
30 #include "icon.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 {
39     NotificationSettings notificationSettings;
40     notificationSettings.initAndNotify("Systray/ShowBubble", this, &SystrayNotificationBackend::showBubbleChanged, true);
41
42     connect(QtUi::mainWindow()->systemTray(), &SystemTray::messageClicked,
43             this, selectOverload<uint>(&SystrayNotificationBackend::onNotificationActivated));
44     connect(QtUi::mainWindow()->systemTray(), &SystemTray::activated,
45             this, selectOverload<SystemTray::ActivationReason>(&SystrayNotificationBackend::onNotificationActivated));
46
47     QApplication::instance()->installEventFilter(this);
48
49     updateToolTip();
50 }
51
52
53 void SystrayNotificationBackend::notify(const Notification &n)
54 {
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     updateToolTip();
66 }
67
68
69 void SystrayNotificationBackend::close(uint notificationId)
70 {
71     QList<Notification>::iterator i = _notifications.begin();
72     while (i != _notifications.end()) {
73         if (i->notificationId == notificationId)
74             i = _notifications.erase(i);
75         else
76             ++i;
77     }
78
79     QtUi::mainWindow()->systemTray()->closeMessage(notificationId);
80
81     updateToolTip();
82 }
83
84
85 void SystrayNotificationBackend::onNotificationActivated(uint notificationId)
86 {
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);
94                 break;
95             }
96         ++i;
97         }
98     }
99 }
100
101
102 void SystrayNotificationBackend::onNotificationActivated(SystemTray::ActivationReason reason)
103 {
104     if (reason == SystemTray::Trigger) {
105         if (_notifications.count()) {
106             onNotificationActivated(_notifications.last().notificationId);
107         }
108         else {
109             GraphicalUi::toggleMainWidget();
110         }
111     }
112 }
113
114
115 // moving the mouse or releasing the button means that we're not dealing with a double activation
116 bool SystrayNotificationBackend::eventFilter(QObject *obj, QEvent *event)
117 {
118     if (event->type() == QEvent::MouseMove || event->type() == QEvent::MouseButtonRelease) {
119         _blockActivation = false;
120     }
121     return AbstractNotificationBackend::eventFilter(obj, event);
122 }
123
124
125 void SystrayNotificationBackend::showBubbleChanged(const QVariant &v)
126 {
127     _showBubble = v.toBool();
128 }
129
130
131 void SystrayNotificationBackend::updateToolTip()
132 {
133     QtUi::mainWindow()->systemTray()->setToolTip("Quassel IRC",
134         _notifications.count() ? tr("%n pending highlight(s)", "", _notifications.count()) : QString());
135 }
136
137
138 SettingsPage *SystrayNotificationBackend::createConfigWidget() const
139 {
140     return new ConfigWidget();
141 }
142
143
144 /***************************************************************************/
145
146 SystrayNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent) : SettingsPage("Internal", "SystrayNotification", parent)
147 {
148     _showBubbleBox = new QCheckBox(tr("Show a message in a popup"));
149     _showBubbleBox->setIcon(icon::get("dialog-information"));
150     connect(_showBubbleBox, &QAbstractButton::toggled, this, &ConfigWidget::widgetChanged);
151     auto *layout = new QHBoxLayout(this);
152     layout->addWidget(_showBubbleBox);
153 }
154
155
156 void SystrayNotificationBackend::ConfigWidget::widgetChanged()
157 {
158     bool changed = (_showBubble != _showBubbleBox->isChecked());
159     if (changed != hasChanged())
160         setChangedState(changed);
161 }
162
163
164 bool SystrayNotificationBackend::ConfigWidget::hasDefaults() const
165 {
166     return true;
167 }
168
169
170 void SystrayNotificationBackend::ConfigWidget::defaults()
171 {
172     _showBubbleBox->setChecked(false);
173     widgetChanged();
174 }
175
176
177 void SystrayNotificationBackend::ConfigWidget::load()
178 {
179     NotificationSettings s;
180     _showBubble = s.value("Systray/ShowBubble", false).toBool();
181     _showBubbleBox->setChecked(_showBubble);
182     setChangedState(false);
183 }
184
185
186 void SystrayNotificationBackend::ConfigWidget::save()
187 {
188     NotificationSettings s;
189     s.setValue("Systray/ShowBubble", _showBubbleBox->isChecked());
190     load();
191 }