1 /***************************************************************************
2 * Copyright (C) 2005-2018 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #ifndef QT_NO_SYSTEMTRAYICON
25 #include "legacysystemtray.h"
29 LegacySystemTray::LegacySystemTray(QWidget *parent)
35 _trayIcon = new QSystemTrayIcon(associatedWidget());
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)));
43 connect(_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
44 SLOT(onActivated(QSystemTrayIcon::ActivationReason)));
46 connect(_trayIcon, SIGNAL(messageClicked()),
47 SLOT(onMessageClicked()));
49 _trayIcon->setContextMenu(trayMenu());
50 _trayIcon->setVisible(false);
52 setMode(Mode::Legacy);
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()));
60 _blinkTimer.setInterval(750);
61 _blinkTimer.setSingleShot(false);
62 connect(&_blinkTimer, SIGNAL(timeout()), SLOT(onBlinkTimeout()));
69 bool LegacySystemTray::isSystemTrayAvailable() const
71 return mode() == Mode::Legacy
72 ? QSystemTrayIcon::isSystemTrayAvailable()
73 : SystemTray::isSystemTrayAvailable();
77 void LegacySystemTray::onVisibilityChanged(bool isVisible)
79 if (mode() == Legacy) {
80 _trayIcon->setVisible(isVisible);
85 void LegacySystemTray::onModeChanged(Mode mode)
87 if (mode == Mode::Legacy) {
88 _trayIcon->setVisible(isVisible());
96 void LegacySystemTray::onStateChanged(State state)
98 if (state == NeedsAttention && animationEnabled())
108 void LegacySystemTray::updateIcon()
111 if (state() == State::NeedsAttention && !_blinkState) {
112 icon = iconName(State::Active);
115 icon = iconName(state());
117 _trayIcon->setIcon(QIcon::fromTheme(icon, QIcon{QString{":/icons/hicolor/24x24/status/%1.svg"}.arg(icon)}));
121 void LegacySystemTray::updateToolTip()
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());
128 QString tooltip = QString("<b>%1</b>").arg(toolTipTitle());
129 if (!toolTipSubTitle().isEmpty())
130 tooltip += QString("<br>%1").arg(toolTipSubTitle());
133 _trayIcon->setToolTip(tooltip);
137 void LegacySystemTray::onBlinkTimeout()
139 _blinkState = !_blinkState;
144 void LegacySystemTray::onActivated(QSystemTrayIcon::ActivationReason reason)
146 activate((SystemTray::ActivationReason)reason);
150 void LegacySystemTray::onMessageClicked()
152 emit messageClicked(_lastMessageId);
156 void LegacySystemTray::showMessage(const QString &title, const QString &message, SystemTray::MessageIcon icon, int msTimeout, uint id)
158 // fancy stuff later: show messages in order
159 // for now, we just show the last message
161 _trayIcon->showMessage(title, message, (QSystemTrayIcon::MessageIcon)icon, msTimeout);
165 void LegacySystemTray::closeMessage(uint notificationId)
167 Q_UNUSED(notificationId)
169 // there really seems to be no sane way to close the bubble... :(
171 showMessage("", "", NoIcon, 1);
176 #endif /* QT_NO_SYSTEMTRAYICON */