7513bc812bc3eba672101355f9c9c21f6cbbd0b3
[quassel.git] / src / qtui / legacysystemtray.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This file is free software; you can redistribute it and/or modify     *
6  *   it under the terms of the GNU Library General Public License (LGPL)   *
7  *   as published by the Free Software Foundation; either version 2 of the *
8  *   License, or (at your option) any later version.                       *
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 #ifndef QT_NO_SYSTEMTRAYICON
22
23 #include <QIcon>
24
25 #include "legacysystemtray.h"
26 #include "mainwin.h"
27 #include "qtui.h"
28
29 LegacySystemTray::LegacySystemTray(QWidget *parent)
30     : SystemTray(parent),
31     _blinkState(false),
32     _lastMessageId(0)
33 {
34 #ifndef HAVE_KDE4
35     _trayIcon = new QSystemTrayIcon(associatedWidget());
36 #else
37     _trayIcon = new KSystemTrayIcon(associatedWidget());
38     // We don't want to trigger a minimize if a highlight is pending, so we brutally remove the internal connection for that
39     disconnect(_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
40         _trayIcon, SLOT(activateOrHide(QSystemTrayIcon::ActivationReason)));
41 #endif
42 #ifndef Q_OS_MAC
43     connect(_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
44         SLOT(onActivated(QSystemTrayIcon::ActivationReason)));
45 #endif
46     connect(_trayIcon, SIGNAL(messageClicked()),
47         SLOT(onMessageClicked()));
48
49     _trayIcon->setContextMenu(trayMenu());
50     _trayIcon->setVisible(false);
51
52     setMode(Mode::Legacy);
53
54     connect(this, SIGNAL(visibilityChanged(bool)), this, SLOT(onVisibilityChanged(bool)));
55     connect(this, SIGNAL(modeChanged(Mode)), this, SLOT(onModeChanged(Mode)));
56     connect(this, SIGNAL(stateChanged(State)), this, SLOT(onStateChanged(State)));
57     connect(this, SIGNAL(toolTipChanged(QString, QString)), SLOT(updateToolTip()));
58     connect(this, SIGNAL(iconsChanged()), this, SLOT(updateIcon()));
59
60     _blinkTimer.setInterval(750);
61     _blinkTimer.setSingleShot(false);
62     connect(&_blinkTimer, SIGNAL(timeout()), SLOT(onBlinkTimeout()));
63
64     updateIcon();
65     updateToolTip();
66 }
67
68
69 bool LegacySystemTray::isSystemTrayAvailable() const
70 {
71     return mode() == Mode::Legacy
72             ? QSystemTrayIcon::isSystemTrayAvailable()
73             : SystemTray::isSystemTrayAvailable();
74 }
75
76
77 void LegacySystemTray::onVisibilityChanged(bool isVisible)
78 {
79     if (mode() == Legacy) {
80         _trayIcon->setVisible(isVisible);
81     }
82 }
83
84
85 void LegacySystemTray::onModeChanged(Mode mode)
86 {
87     if (mode == Mode::Legacy) {
88         _trayIcon->setVisible(isVisible());
89     }
90     else {
91         _trayIcon->hide();
92     }
93 }
94
95
96 void LegacySystemTray::onStateChanged(State state)
97 {
98     if (state == NeedsAttention && animationEnabled())
99         _blinkTimer.start();
100     else {
101         _blinkTimer.stop();
102         _blinkState = false;
103     }
104     updateIcon();
105 }
106
107
108 void LegacySystemTray::updateIcon()
109 {
110     QString icon;
111     if (state() == State::NeedsAttention && !_blinkState) {
112         icon = iconName(State::Active);
113     }
114     else {
115         icon = iconName(state());
116     }
117     _trayIcon->setIcon(QIcon::fromTheme(icon, QIcon{QString{":/icons/hicolor/24x24/status/%1.svg"}.arg(icon)}));
118 }
119
120
121 void LegacySystemTray::updateToolTip()
122 {
123 #if defined Q_OS_MAC || defined Q_OS_WIN
124     QString tooltip = QString("%1").arg(toolTipTitle());
125     if (!toolTipSubTitle().isEmpty())
126         tooltip += QString("\n%1").arg(toolTipSubTitle());
127 #else
128     QString tooltip = QString("<b>%1</b>").arg(toolTipTitle());
129     if (!toolTipSubTitle().isEmpty())
130         tooltip += QString("<br>%1").arg(toolTipSubTitle());
131 #endif
132
133     _trayIcon->setToolTip(tooltip);
134 }
135
136
137 void LegacySystemTray::onBlinkTimeout()
138 {
139     _blinkState = !_blinkState;
140     updateIcon();
141 }
142
143
144 void LegacySystemTray::onActivated(QSystemTrayIcon::ActivationReason reason)
145 {
146     activate((SystemTray::ActivationReason)reason);
147 }
148
149
150 void LegacySystemTray::onMessageClicked()
151 {
152     emit messageClicked(_lastMessageId);
153 }
154
155
156 void LegacySystemTray::showMessage(const QString &title, const QString &message, SystemTray::MessageIcon icon, int msTimeout, uint id)
157 {
158     // fancy stuff later: show messages in order
159     // for now, we just show the last message
160     _lastMessageId = id;
161     _trayIcon->showMessage(title, message, (QSystemTrayIcon::MessageIcon)icon, msTimeout);
162 }
163
164
165 void LegacySystemTray::closeMessage(uint notificationId)
166 {
167     Q_UNUSED(notificationId)
168
169     // there really seems to be no sane way to close the bubble... :(
170 #ifdef Q_WS_X11
171     showMessage("", "", NoIcon, 1);
172 #endif
173 }
174
175
176 #endif /* QT_NO_SYSTEMTRAYICON */