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()));
59 _blinkTimer.setInterval(750);
60 _blinkTimer.setSingleShot(false);
61 connect(&_blinkTimer, SIGNAL(timeout()), SLOT(onBlinkTimeout()));
68 bool LegacySystemTray::isSystemTrayAvailable() const
70 return mode() == Mode::Legacy
71 ? QSystemTrayIcon::isSystemTrayAvailable()
72 : SystemTray::isSystemTrayAvailable();
76 void LegacySystemTray::onVisibilityChanged(bool isVisible)
78 if (mode() == Legacy) {
79 _trayIcon->setVisible(isVisible);
84 void LegacySystemTray::onModeChanged(Mode mode)
86 if (mode == Mode::Legacy) {
87 _trayIcon->setVisible(isVisible());
95 void LegacySystemTray::onStateChanged(State state)
97 if (state == NeedsAttention && animationEnabled())
107 void LegacySystemTray::updateIcon()
110 if (state() == State::NeedsAttention && !_blinkState) {
111 icon = iconName(State::Active);
114 icon = iconName(state());
116 _trayIcon->setIcon(QIcon::fromTheme(icon, QIcon{QString{":/icons/hicolor/24x24/status/%1.svg"}.arg(icon)}));
120 void LegacySystemTray::updateToolTip()
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());
127 QString tooltip = QString("<b>%1</b>").arg(toolTipTitle());
128 if (!toolTipSubTitle().isEmpty())
129 tooltip += QString("<br>%1").arg(toolTipSubTitle());
132 _trayIcon->setToolTip(tooltip);
136 void LegacySystemTray::onBlinkTimeout()
138 _blinkState = !_blinkState;
143 void LegacySystemTray::onActivated(QSystemTrayIcon::ActivationReason reason)
145 activate((SystemTray::ActivationReason)reason);
149 void LegacySystemTray::onMessageClicked()
151 emit messageClicked(_lastMessageId);
155 void LegacySystemTray::showMessage(const QString &title, const QString &message, SystemTray::MessageIcon icon, int msTimeout, uint id)
157 // fancy stuff later: show messages in order
158 // for now, we just show the last message
160 _trayIcon->showMessage(title, message, (QSystemTrayIcon::MessageIcon)icon, msTimeout);
164 void LegacySystemTray::closeMessage(uint notificationId)
166 Q_UNUSED(notificationId)
168 // there really seems to be no sane way to close the bubble... :(
170 showMessage("", "", NoIcon, 1);
175 #endif /* QT_NO_SYSTEMTRAYICON */