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)
33 _trayIcon = new QSystemTrayIcon(associatedWidget());
35 _trayIcon = new KSystemTrayIcon(associatedWidget());
36 // We don't want to trigger a minimize if a highlight is pending, so we brutally remove the internal connection for that
37 disconnect(_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
38 _trayIcon, SLOT(activateOrHide(QSystemTrayIcon::ActivationReason)));
41 connect(_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
42 SLOT(onActivated(QSystemTrayIcon::ActivationReason)));
44 connect(_trayIcon, SIGNAL(messageClicked()),
45 SLOT(onMessageClicked()));
47 _trayIcon->setContextMenu(trayMenu());
48 _trayIcon->setVisible(false);
50 setMode(Mode::Legacy);
52 connect(this, SIGNAL(visibilityChanged(bool)), this, SLOT(onVisibilityChanged(bool)));
53 connect(this, SIGNAL(modeChanged(Mode)), this, SLOT(onModeChanged(Mode)));
54 connect(this, SIGNAL(toolTipChanged(QString, QString)), SLOT(updateToolTip()));
55 connect(this, SIGNAL(iconsChanged()), this, SLOT(updateIcon()));
56 connect(this, SIGNAL(currentIconNameChanged()), this, SLOT(updateIcon()));
63 bool LegacySystemTray::isSystemTrayAvailable() const
65 return mode() == Mode::Legacy
66 ? QSystemTrayIcon::isSystemTrayAvailable()
67 : SystemTray::isSystemTrayAvailable();
71 void LegacySystemTray::onVisibilityChanged(bool isVisible)
73 if (mode() == Legacy) {
74 _trayIcon->setVisible(isVisible);
79 void LegacySystemTray::onModeChanged(Mode mode)
81 if (mode == Mode::Legacy) {
82 _trayIcon->setVisible(isVisible());
90 void LegacySystemTray::updateIcon()
92 QString iconName = (state() == NeedsAttention) ? currentAttentionIconName() : currentIconName();
93 _trayIcon->setIcon(icon::get(iconName, QString{":/icons/hicolor/24x24/status/%1.svg"}.arg(iconName)));
97 void LegacySystemTray::updateToolTip()
99 #if defined Q_OS_MAC || defined Q_OS_WIN
100 QString tooltip = QString("%1").arg(toolTipTitle());
101 if (!toolTipSubTitle().isEmpty())
102 tooltip += QString("\n%1").arg(toolTipSubTitle());
104 QString tooltip = QString("<b>%1</b>").arg(toolTipTitle());
105 if (!toolTipSubTitle().isEmpty())
106 tooltip += QString("<br>%1").arg(toolTipSubTitle());
109 _trayIcon->setToolTip(tooltip);
113 void LegacySystemTray::onActivated(QSystemTrayIcon::ActivationReason reason)
115 activate((SystemTray::ActivationReason)reason);
119 void LegacySystemTray::onMessageClicked()
121 emit messageClicked(_lastMessageId);
125 void LegacySystemTray::showMessage(const QString &title, const QString &message, SystemTray::MessageIcon icon, int msTimeout, uint id)
127 // fancy stuff later: show messages in order
128 // for now, we just show the last message
130 _trayIcon->showMessage(title, message, (QSystemTrayIcon::MessageIcon)icon, msTimeout);
134 void LegacySystemTray::closeMessage(uint notificationId)
136 Q_UNUSED(notificationId)
138 // there really seems to be no sane way to close the bubble... :(
140 showMessage("", "", NoIcon, 1);
145 #endif /* QT_NO_SYSTEMTRAYICON */