client: Use sentence case for Core Info details
[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     _blockActivation(false)
39 {
40     NotificationSettings notificationSettings;
41     notificationSettings.initAndNotify("Systray/ShowBubble", this, SLOT(showBubbleChanged(QVariant)), true);
42
43     connect(QtUi::mainWindow()->systemTray(), SIGNAL(messageClicked(uint)), SLOT(notificationActivated(uint)));
44     connect(QtUi::mainWindow()->systemTray(), SIGNAL(activated(SystemTray::ActivationReason)),
45         SLOT(notificationActivated(SystemTray::ActivationReason)));
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::notificationActivated(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::notificationActivated(SystemTray::ActivationReason reason)
103 {
104     if (reason == SystemTray::Trigger) {
105         if (_notifications.count())
106             notificationActivated(_notifications.last().notificationId);
107         else
108             GraphicalUi::toggleMainWidget();
109     }
110 }
111
112
113 // moving the mouse or releasing the button means that we're not dealing with a double activation
114 bool SystrayNotificationBackend::eventFilter(QObject *obj, QEvent *event)
115 {
116     if (event->type() == QEvent::MouseMove || event->type() == QEvent::MouseButtonRelease) {
117         _blockActivation = false;
118     }
119     return AbstractNotificationBackend::eventFilter(obj, event);
120 }
121
122
123 void SystrayNotificationBackend::showBubbleChanged(const QVariant &v)
124 {
125     _showBubble = v.toBool();
126 }
127
128
129 void SystrayNotificationBackend::updateToolTip()
130 {
131     QtUi::mainWindow()->systemTray()->setToolTip("Quassel IRC",
132         _notifications.count() ? tr("%n pending highlight(s)", "", _notifications.count()) : QString());
133 }
134
135
136 SettingsPage *SystrayNotificationBackend::createConfigWidget() const
137 {
138     return new ConfigWidget();
139 }
140
141
142 /***************************************************************************/
143
144 SystrayNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent) : SettingsPage("Internal", "SystrayNotification", parent)
145 {
146     _showBubbleBox = new QCheckBox(tr("Show a message in a popup"));
147     _showBubbleBox->setIcon(icon::get("dialog-information"));
148     connect(_showBubbleBox, SIGNAL(toggled(bool)), this, SLOT(widgetChanged()));
149     QHBoxLayout *layout = new QHBoxLayout(this);
150     layout->addWidget(_showBubbleBox);
151 }
152
153
154 void SystrayNotificationBackend::ConfigWidget::widgetChanged()
155 {
156     bool changed = (_showBubble != _showBubbleBox->isChecked());
157     if (changed != hasChanged())
158         setChangedState(changed);
159 }
160
161
162 bool SystrayNotificationBackend::ConfigWidget::hasDefaults() const
163 {
164     return true;
165 }
166
167
168 void SystrayNotificationBackend::ConfigWidget::defaults()
169 {
170     _showBubbleBox->setChecked(false);
171     widgetChanged();
172 }
173
174
175 void SystrayNotificationBackend::ConfigWidget::load()
176 {
177     NotificationSettings s;
178     _showBubble = s.value("Systray/ShowBubble", false).toBool();
179     _showBubbleBox->setChecked(_showBubble);
180     setChangedState(false);
181 }
182
183
184 void SystrayNotificationBackend::ConfigWidget::save()
185 {
186     NotificationSettings s;
187     s.setValue("Systray/ShowBubble", _showBubbleBox->isChecked());
188     load();
189 }