modernize: Reformat ALL the source... again!
[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(),
43             &SystemTray::messageClicked,
44             this,
45             selectOverload<uint>(&SystrayNotificationBackend::onNotificationActivated));
46     connect(QtUi::mainWindow()->systemTray(),
47             &SystemTray::activated,
48             this,
49             selectOverload<SystemTray::ActivationReason>(&SystrayNotificationBackend::onNotificationActivated));
50
51     QApplication::instance()->installEventFilter(this);
52
53     updateToolTip();
54 }
55
56 void SystrayNotificationBackend::notify(const Notification& n)
57 {
58     if (n.type != Highlight && n.type != PrivMsg)
59         return;
60
61     _notifications.append(n);
62     if (_showBubble) {
63         QString title = Client::networkModel()->networkName(n.bufferId) + " - " + Client::networkModel()->bufferName(n.bufferId);
64         QString message = QString("<%1> %2").arg(n.sender, n.message);
65         QtUi::mainWindow()->systemTray()->showMessage(title, message, SystemTray::Information, 10000, n.notificationId);
66     }
67
68     updateToolTip();
69 }
70
71 void SystrayNotificationBackend::close(uint notificationId)
72 {
73     QList<Notification>::iterator i = _notifications.begin();
74     while (i != _notifications.end()) {
75         if (i->notificationId == notificationId)
76             i = _notifications.erase(i);
77         else
78             ++i;
79     }
80
81     QtUi::mainWindow()->systemTray()->closeMessage(notificationId);
82
83     updateToolTip();
84 }
85
86 void SystrayNotificationBackend::onNotificationActivated(uint notificationId)
87 {
88     if (!_blockActivation) {
89         QList<Notification>::iterator i = _notifications.begin();
90         while (i != _notifications.end()) {
91             if (i->notificationId == notificationId) {
92                 if (QtUi::mainWindow()->systemTray()->mode() == SystemTray::Legacy)
93                     _blockActivation = true;  // prevent double activation because both tray icon and bubble might send a signal
94                 emit activated(notificationId);
95                 break;
96             }
97             ++i;
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 // moving the mouse or releasing the button means that we're not dealing with a double activation
115 bool SystrayNotificationBackend::eventFilter(QObject* obj, QEvent* event)
116 {
117     if (event->type() == QEvent::MouseMove || event->type() == QEvent::MouseButtonRelease) {
118         _blockActivation = false;
119     }
120     return AbstractNotificationBackend::eventFilter(obj, event);
121 }
122
123 void SystrayNotificationBackend::showBubbleChanged(const QVariant& v)
124 {
125     _showBubble = v.toBool();
126 }
127
128 void SystrayNotificationBackend::updateToolTip()
129 {
130     QtUi::mainWindow()->systemTray()->setToolTip("Quassel IRC",
131                                                  _notifications.count() ? tr("%n pending highlight(s)", "", _notifications.count())
132                                                                         : QString());
133 }
134
135 SettingsPage* SystrayNotificationBackend::createConfigWidget() const
136 {
137     return new ConfigWidget();
138 }
139
140 /***************************************************************************/
141
142 SystrayNotificationBackend::ConfigWidget::ConfigWidget(QWidget* parent)
143     : 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, &QAbstractButton::toggled, this, &ConfigWidget::widgetChanged);
148     auto* layout = new QHBoxLayout(this);
149     layout->addWidget(_showBubbleBox);
150 }
151
152 void SystrayNotificationBackend::ConfigWidget::widgetChanged()
153 {
154     bool changed = (_showBubble != _showBubbleBox->isChecked());
155     if (changed != hasChanged())
156         setChangedState(changed);
157 }
158
159 bool SystrayNotificationBackend::ConfigWidget::hasDefaults() const
160 {
161     return true;
162 }
163
164 void SystrayNotificationBackend::ConfigWidget::defaults()
165 {
166     _showBubbleBox->setChecked(false);
167     widgetChanged();
168 }
169
170 void SystrayNotificationBackend::ConfigWidget::load()
171 {
172     NotificationSettings s;
173     _showBubble = s.value("Systray/ShowBubble", false).toBool();
174     _showBubbleBox->setChecked(_showBubble);
175     setChangedState(false);
176 }
177
178 void SystrayNotificationBackend::ConfigWidget::save()
179 {
180     NotificationSettings s;
181     s.setValue("Systray/ShowBubble", _showBubbleBox->isChecked());
182     load();
183 }