Merge pull request #104 from mamarley/disconnectdesyncfix
[quassel.git] / src / qtui / systraynotificationbackend.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2015 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         if (_notifications.count()) {
96             if (QtUi::mainWindow()->systemTray()->mode() == SystemTray::Legacy)
97                 _blockActivation = true;  // prevent double activation because both tray icon and bubble might send a signal
98             if (!notificationId)
99                 notificationId = _notifications.count() ? _notifications.last().notificationId : 0;
100             emit activated(notificationId);
101         }
102         else
103             GraphicalUi::toggleMainWidget();
104     }
105 }
106
107
108 void SystrayNotificationBackend::notificationActivated(SystemTray::ActivationReason reason)
109 {
110     if (reason == SystemTray::Trigger) {
111         notificationActivated(0);
112     }
113 }
114
115
116 // moving the mouse or releasing the button means that we're not dealing with a double activation
117 bool SystrayNotificationBackend::eventFilter(QObject *obj, QEvent *event)
118 {
119     if (event->type() == QEvent::MouseMove || event->type() == QEvent::MouseButtonRelease) {
120         _blockActivation = false;
121     }
122     return AbstractNotificationBackend::eventFilter(obj, event);
123 }
124
125
126 void SystrayNotificationBackend::showBubbleChanged(const QVariant &v)
127 {
128     _showBubble = v.toBool();
129 }
130
131
132 void SystrayNotificationBackend::animateChanged(const QVariant &v)
133 {
134     _animate = v.toBool();
135 }
136
137
138 void SystrayNotificationBackend::updateToolTip()
139 {
140     QtUi::mainWindow()->systemTray()->setToolTip("Quassel IRC",
141         _notifications.count() ? tr("%n pending highlight(s)", "", _notifications.count()) : QString());
142 }
143
144
145 SettingsPage *SystrayNotificationBackend::createConfigWidget() const
146 {
147     return new ConfigWidget();
148 }
149
150
151 /***************************************************************************/
152
153 SystrayNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent) : SettingsPage("Internal", "SystrayNotification", parent)
154 {
155     _showBubbleBox = new QCheckBox(tr("Show a message in a popup"));
156     _showBubbleBox->setIcon(QIcon::fromTheme("dialog-information"));
157     connect(_showBubbleBox, SIGNAL(toggled(bool)), this, SLOT(widgetChanged()));
158     QHBoxLayout *layout = new QHBoxLayout(this);
159     layout->addWidget(_showBubbleBox);
160 }
161
162
163 void SystrayNotificationBackend::ConfigWidget::widgetChanged()
164 {
165     bool changed = (_showBubble != _showBubbleBox->isChecked());
166     if (changed != hasChanged())
167         setChangedState(changed);
168 }
169
170
171 bool SystrayNotificationBackend::ConfigWidget::hasDefaults() const
172 {
173     return true;
174 }
175
176
177 void SystrayNotificationBackend::ConfigWidget::defaults()
178 {
179     _showBubbleBox->setChecked(false);
180     widgetChanged();
181 }
182
183
184 void SystrayNotificationBackend::ConfigWidget::load()
185 {
186     NotificationSettings s;
187     _showBubble = s.value("Systray/ShowBubble", false).toBool();
188     _showBubbleBox->setChecked(_showBubble);
189     setChangedState(false);
190 }
191
192
193 void SystrayNotificationBackend::ConfigWidget::save()
194 {
195     NotificationSettings s;
196     s.setValue("Systray/ShowBubble", _showBubbleBox->isChecked());
197     load();
198 }