modernize: Use std::make_unique
[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, SLOT(showBubbleChanged(QVariant)), true);
41
42     connect(QtUi::mainWindow()->systemTray(), SIGNAL(messageClicked(uint)), SLOT(notificationActivated(uint)));
43     connect(QtUi::mainWindow()->systemTray(), SIGNAL(activated(SystemTray::ActivationReason)),
44         SLOT(notificationActivated(SystemTray::ActivationReason)));
45
46     QApplication::instance()->installEventFilter(this);
47
48     updateToolTip();
49 }
50
51
52 void SystrayNotificationBackend::notify(const Notification &n)
53 {
54     if (n.type != Highlight && n.type != PrivMsg)
55         return;
56
57     _notifications.append(n);
58     if (_showBubble) {
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);
62     }
63
64     updateToolTip();
65 }
66
67
68 void SystrayNotificationBackend::close(uint notificationId)
69 {
70     QList<Notification>::iterator i = _notifications.begin();
71     while (i != _notifications.end()) {
72         if (i->notificationId == notificationId)
73             i = _notifications.erase(i);
74         else
75             ++i;
76     }
77
78     QtUi::mainWindow()->systemTray()->closeMessage(notificationId);
79
80     updateToolTip();
81 }
82
83
84 void SystrayNotificationBackend::notificationActivated(uint notificationId)
85 {
86     if (!_blockActivation) {
87         QList<Notification>::iterator i = _notifications.begin();
88         while (i != _notifications.end()) {
89             if (i->notificationId == notificationId) {
90                 if (QtUi::mainWindow()->systemTray()->mode() == SystemTray::Legacy)
91                     _blockActivation = true;  // prevent double activation because both tray icon and bubble might send a signal
92                 emit activated(notificationId);
93                 break;
94             }
95         ++i;
96         }
97     }
98 }
99
100
101 void SystrayNotificationBackend::notificationActivated(SystemTray::ActivationReason reason)
102 {
103     if (reason == SystemTray::Trigger) {
104         if (_notifications.count())
105             notificationActivated(_notifications.last().notificationId);
106         else
107             GraphicalUi::toggleMainWidget();
108     }
109 }
110
111
112 // moving the mouse or releasing the button means that we're not dealing with a double activation
113 bool SystrayNotificationBackend::eventFilter(QObject *obj, QEvent *event)
114 {
115     if (event->type() == QEvent::MouseMove || event->type() == QEvent::MouseButtonRelease) {
116         _blockActivation = false;
117     }
118     return AbstractNotificationBackend::eventFilter(obj, event);
119 }
120
121
122 void SystrayNotificationBackend::showBubbleChanged(const QVariant &v)
123 {
124     _showBubble = v.toBool();
125 }
126
127
128 void SystrayNotificationBackend::updateToolTip()
129 {
130     QtUi::mainWindow()->systemTray()->setToolTip("Quassel IRC",
131         _notifications.count() ? tr("%n pending highlight(s)", "", _notifications.count()) : QString());
132 }
133
134
135 SettingsPage *SystrayNotificationBackend::createConfigWidget() const
136 {
137     return new ConfigWidget();
138 }
139
140
141 /***************************************************************************/
142
143 SystrayNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent) : SettingsPage("Internal", "SystrayNotification", parent)
144 {
145     _showBubbleBox = new QCheckBox(tr("Show a message in a popup"));
146     _showBubbleBox->setIcon(icon::get("dialog-information"));
147     connect(_showBubbleBox, SIGNAL(toggled(bool)), this, SLOT(widgetChanged()));
148     auto *layout = new QHBoxLayout(this);
149     layout->addWidget(_showBubbleBox);
150 }
151
152
153 void SystrayNotificationBackend::ConfigWidget::widgetChanged()
154 {
155     bool changed = (_showBubble != _showBubbleBox->isChecked());
156     if (changed != hasChanged())
157         setChangedState(changed);
158 }
159
160
161 bool SystrayNotificationBackend::ConfigWidget::hasDefaults() const
162 {
163     return true;
164 }
165
166
167 void SystrayNotificationBackend::ConfigWidget::defaults()
168 {
169     _showBubbleBox->setChecked(false);
170     widgetChanged();
171 }
172
173
174 void SystrayNotificationBackend::ConfigWidget::load()
175 {
176     NotificationSettings s;
177     _showBubble = s.value("Systray/ShowBubble", false).toBool();
178     _showBubbleBox->setChecked(_showBubble);
179     setChangedState(false);
180 }
181
182
183 void SystrayNotificationBackend::ConfigWidget::save()
184 {
185     NotificationSettings s;
186     s.setValue("Systray/ShowBubble", _showBubbleBox->isChecked());
187     load();
188 }