qtui: Refactor the system tray implementations
[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
59     _blinkTimer.setInterval(750);
60     _blinkTimer.setSingleShot(false);
61     connect(&_blinkTimer, SIGNAL(timeout()), SLOT(onBlinkTimeout()));
62
63     updateIcon();
64     updateToolTip();
65 }
66
67
68 bool LegacySystemTray::isSystemTrayAvailable() const
69 {
70     return mode() == Mode::Legacy
71             ? QSystemTrayIcon::isSystemTrayAvailable()
72             : SystemTray::isSystemTrayAvailable();
73 }
74
75
76 void LegacySystemTray::onVisibilityChanged(bool isVisible)
77 {
78     if (mode() == Legacy) {
79         _trayIcon->setVisible(isVisible);
80     }
81 }
82
83
84 void LegacySystemTray::onModeChanged(Mode mode)
85 {
86     if (mode == Mode::Legacy) {
87         _trayIcon->setVisible(isVisible());
88     }
89     else {
90         _trayIcon->hide();
91     }
92 }
93
94
95 void LegacySystemTray::onStateChanged(State state)
96 {
97     if (state == NeedsAttention && animationEnabled())
98         _blinkTimer.start();
99     else {
100         _blinkTimer.stop();
101         _blinkState = false;
102     }
103     updateIcon();
104 }
105
106
107 void LegacySystemTray::updateIcon()
108 {
109     QString icon;
110     if (state() == State::NeedsAttention && !_blinkState) {
111         icon = iconName(State::Active);
112     }
113     else {
114         icon = iconName(state());
115     }
116     _trayIcon->setIcon(QIcon::fromTheme(icon, QIcon{QString{":/icons/hicolor/24x24/status/%1.svg"}.arg(icon)}));
117 }
118
119
120 void LegacySystemTray::updateToolTip()
121 {
122 #if defined Q_OS_MAC || defined Q_OS_WIN
123     QString tooltip = QString("%1").arg(toolTipTitle());
124     if (!toolTipSubTitle().isEmpty())
125         tooltip += QString("\n%1").arg(toolTipSubTitle());
126 #else
127     QString tooltip = QString("<b>%1</b>").arg(toolTipTitle());
128     if (!toolTipSubTitle().isEmpty())
129         tooltip += QString("<br>%1").arg(toolTipSubTitle());
130 #endif
131
132     _trayIcon->setToolTip(tooltip);
133 }
134
135
136 void LegacySystemTray::onBlinkTimeout()
137 {
138     _blinkState = !_blinkState;
139     updateIcon();
140 }
141
142
143 void LegacySystemTray::onActivated(QSystemTrayIcon::ActivationReason reason)
144 {
145     activate((SystemTray::ActivationReason)reason);
146 }
147
148
149 void LegacySystemTray::onMessageClicked()
150 {
151     emit messageClicked(_lastMessageId);
152 }
153
154
155 void LegacySystemTray::showMessage(const QString &title, const QString &message, SystemTray::MessageIcon icon, int msTimeout, uint id)
156 {
157     // fancy stuff later: show messages in order
158     // for now, we just show the last message
159     _lastMessageId = id;
160     _trayIcon->showMessage(title, message, (QSystemTrayIcon::MessageIcon)icon, msTimeout);
161 }
162
163
164 void LegacySystemTray::closeMessage(uint notificationId)
165 {
166     Q_UNUSED(notificationId)
167
168     // there really seems to be no sane way to close the bubble... :(
169 #ifdef Q_WS_X11
170     showMessage("", "", NoIcon, 1);
171 #endif
172 }
173
174
175 #endif /* QT_NO_SYSTEMTRAYICON */