1 /***************************************************************************
2 * Copyright (C) 2005-2020 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
23 # include "legacysystemtray.h"
29 LegacySystemTray::LegacySystemTray(QWidget* parent)
31 , _trayIcon{new QSystemTrayIcon(associatedWidget())}
34 connect(_trayIcon, &QSystemTrayIcon::activated, this, &LegacySystemTray::onActivated);
36 connect(_trayIcon, &QSystemTrayIcon::messageClicked, this, &LegacySystemTray::onMessageClicked);
38 _trayIcon->setContextMenu(trayMenu());
39 _trayIcon->setVisible(false);
41 setMode(Mode::Legacy);
43 connect(this, &SystemTray::visibilityChanged, this, &LegacySystemTray::onVisibilityChanged);
44 connect(this, &SystemTray::modeChanged, this, &LegacySystemTray::onModeChanged);
45 connect(this, &SystemTray::toolTipChanged, this, &LegacySystemTray::updateToolTip);
46 connect(this, &SystemTray::iconsChanged, this, &LegacySystemTray::updateIcon);
47 connect(this, &SystemTray::currentIconNameChanged, this, &LegacySystemTray::updateIcon);
53 bool LegacySystemTray::isSystemTrayAvailable() const
55 return mode() == Mode::Legacy ? QSystemTrayIcon::isSystemTrayAvailable() : SystemTray::isSystemTrayAvailable();
58 void LegacySystemTray::onVisibilityChanged(bool isVisible)
60 if (mode() == Legacy) {
61 _trayIcon->setVisible(isVisible);
65 void LegacySystemTray::onModeChanged(Mode mode)
67 if (mode == Mode::Legacy) {
68 _trayIcon->setVisible(isVisible());
75 void LegacySystemTray::updateIcon()
77 QString iconName = (state() == NeedsAttention) ? currentAttentionIconName() : currentIconName();
78 _trayIcon->setIcon(icon::get(iconName, QString{":/icons/hicolor/24x24/status/%1.svg"}.arg(iconName)));
81 void LegacySystemTray::updateToolTip()
83 # if defined Q_OS_MAC || defined Q_OS_WIN
84 QString tooltip = QString("%1").arg(toolTipTitle());
85 if (!toolTipSubTitle().isEmpty())
86 tooltip += QString("\n%1").arg(toolTipSubTitle());
88 QString tooltip = QString("<b>%1</b>").arg(toolTipTitle());
89 if (!toolTipSubTitle().isEmpty())
90 tooltip += QString("<br>%1").arg(toolTipSubTitle());
93 _trayIcon->setToolTip(tooltip);
96 void LegacySystemTray::onActivated(QSystemTrayIcon::ActivationReason reason)
98 activate((SystemTray::ActivationReason)reason);
101 void LegacySystemTray::onMessageClicked()
103 emit messageClicked(_lastMessageId);
106 void LegacySystemTray::showMessage(const QString& title, const QString& message, SystemTray::MessageIcon icon, int msTimeout, uint id)
108 // fancy stuff later: show messages in order
109 // for now, we just show the last message
111 _trayIcon->showMessage(title, message, (QSystemTrayIcon::MessageIcon)icon, msTimeout);
114 void LegacySystemTray::closeMessage(uint notificationId)
116 Q_UNUSED(notificationId)
118 // there really seems to be no sane way to close the bubble... :(
120 showMessage("", "", NoIcon, 1);
124 #endif /* QT_NO_SYSTEMTRAYICON */