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
23 #include "legacysystemtray.h"
29 LegacySystemTray::LegacySystemTray(QWidget *parent)
31 , _trayIcon{new QSystemTrayIcon(associatedWidget())}
35 connect(_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
36 SLOT(onActivated(QSystemTrayIcon::ActivationReason)));
38 connect(_trayIcon, SIGNAL(messageClicked()),
39 SLOT(onMessageClicked()));
41 _trayIcon->setContextMenu(trayMenu());
42 _trayIcon->setVisible(false);
44 setMode(Mode::Legacy);
46 connect(this, SIGNAL(visibilityChanged(bool)), this, SLOT(onVisibilityChanged(bool)));
47 connect(this, SIGNAL(modeChanged(Mode)), this, SLOT(onModeChanged(Mode)));
48 connect(this, SIGNAL(toolTipChanged(QString, QString)), SLOT(updateToolTip()));
49 connect(this, SIGNAL(iconsChanged()), this, SLOT(updateIcon()));
50 connect(this, SIGNAL(currentIconNameChanged()), this, SLOT(updateIcon()));
57 bool LegacySystemTray::isSystemTrayAvailable() const
59 return mode() == Mode::Legacy
60 ? QSystemTrayIcon::isSystemTrayAvailable()
61 : SystemTray::isSystemTrayAvailable();
65 void LegacySystemTray::onVisibilityChanged(bool isVisible)
67 if (mode() == Legacy) {
68 _trayIcon->setVisible(isVisible);
73 void LegacySystemTray::onModeChanged(Mode mode)
75 if (mode == Mode::Legacy) {
76 _trayIcon->setVisible(isVisible());
84 void LegacySystemTray::updateIcon()
86 QString iconName = (state() == NeedsAttention) ? currentAttentionIconName() : currentIconName();
87 _trayIcon->setIcon(icon::get(iconName, QString{":/icons/hicolor/24x24/status/%1.svg"}.arg(iconName)));
91 void LegacySystemTray::updateToolTip()
93 #if defined Q_OS_MAC || defined Q_OS_WIN
94 QString tooltip = QString("%1").arg(toolTipTitle());
95 if (!toolTipSubTitle().isEmpty())
96 tooltip += QString("\n%1").arg(toolTipSubTitle());
98 QString tooltip = QString("<b>%1</b>").arg(toolTipTitle());
99 if (!toolTipSubTitle().isEmpty())
100 tooltip += QString("<br>%1").arg(toolTipSubTitle());
103 _trayIcon->setToolTip(tooltip);
107 void LegacySystemTray::onActivated(QSystemTrayIcon::ActivationReason reason)
109 activate((SystemTray::ActivationReason)reason);
113 void LegacySystemTray::onMessageClicked()
115 emit messageClicked(_lastMessageId);
119 void LegacySystemTray::showMessage(const QString &title, const QString &message, SystemTray::MessageIcon icon, int msTimeout, uint id)
121 // fancy stuff later: show messages in order
122 // for now, we just show the last message
124 _trayIcon->showMessage(title, message, (QSystemTrayIcon::MessageIcon)icon, msTimeout);
128 void LegacySystemTray::closeMessage(uint notificationId)
130 Q_UNUSED(notificationId)
132 // there really seems to be no sane way to close the bubble... :(
134 showMessage("", "", NoIcon, 1);
139 #endif /* QT_NO_SYSTEMTRAYICON */