X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fqtui%2Flegacysystemtray.cpp;h=0f1135a43180aefa4abe0d6a15bdbee91a3c2013;hp=b57fd995944f3a6e7e31db0e8215465f0a2da732;hb=a888a2886dc1466eb0b1bb3591f43350623c6330;hpb=e7d1bc1fa02e1233f140e4b04d99ab8f4685bce5 diff --git a/src/qtui/legacysystemtray.cpp b/src/qtui/legacysystemtray.cpp index b57fd995..0f1135a4 100644 --- a/src/qtui/legacysystemtray.cpp +++ b/src/qtui/legacysystemtray.cpp @@ -1,11 +1,11 @@ /*************************************************************************** - * Copyright (C) 2005-2010 by the Quassel Project * + * Copyright (C) 2005-2018 by the Quassel Project * * devel@quassel-irc.org * * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) version 3. * + * This file is free software; you can redistribute it and/or modify * + * it under the terms of the GNU Library General Public License (LGPL) * + * as published by the Free Software Foundation; either version 2 of the * + * License, or (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * @@ -15,120 +15,131 @@ * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * ***************************************************************************/ #ifndef QT_NO_SYSTEMTRAYICON +#include + #include "legacysystemtray.h" +#include "mainwin.h" #include "qtui.h" -LegacySystemTray::LegacySystemTray(QObject *parent) - : SystemTray(parent), - _blinkState(false), - _isVisible(true) +LegacySystemTray::LegacySystemTray(QWidget *parent) + : SystemTray(parent) { -#ifndef HAVE_KDE - _trayIcon = new QSystemTrayIcon(QtUi::mainWindow()); +#ifndef HAVE_KDE4 + _trayIcon = new QSystemTrayIcon(associatedWidget()); #else - _trayIcon = new KSystemTrayIcon(QtUi::mainWindow()); - // We don't want to trigger a minimize if a highlight is pending, so we brutally remove the internal connection for that - disconnect(_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), - _trayIcon, SLOT(activateOrHide(QSystemTrayIcon::ActivationReason))); + _trayIcon = new KSystemTrayIcon(associatedWidget()); + // We don't want to trigger a minimize if a highlight is pending, so we brutally remove the internal connection for that + disconnect(_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), + _trayIcon, SLOT(activateOrHide(QSystemTrayIcon::ActivationReason))); #endif -#ifndef Q_WS_MAC - connect(_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), - SLOT(on_activated(QSystemTrayIcon::ActivationReason))); +#ifndef Q_OS_MAC + connect(_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), + SLOT(onActivated(QSystemTrayIcon::ActivationReason))); #endif - connect(_trayIcon, SIGNAL(messageClicked()), - SIGNAL(messageClicked())); + connect(_trayIcon, SIGNAL(messageClicked()), + SLOT(onMessageClicked())); - setTrayMenu(_trayIcon->contextMenu()); - _trayIcon->setContextMenu(trayMenu()); + _trayIcon->setContextMenu(trayMenu()); + _trayIcon->setVisible(false); - _blinkTimer.setInterval(500); - _blinkTimer.setSingleShot(false); - connect(&_blinkTimer, SIGNAL(timeout()), SLOT(on_blinkTimeout())); -} + setMode(Mode::Legacy); -void LegacySystemTray::init() { - if(mode() == Invalid) // derived class hasn't set a mode itself - setMode(Legacy); -} + connect(this, SIGNAL(visibilityChanged(bool)), this, SLOT(onVisibilityChanged(bool))); + connect(this, SIGNAL(modeChanged(Mode)), this, SLOT(onModeChanged(Mode))); + connect(this, SIGNAL(toolTipChanged(QString, QString)), SLOT(updateToolTip())); + connect(this, SIGNAL(iconsChanged()), this, SLOT(updateIcon())); + connect(this, SIGNAL(currentIconNameChanged()), this, SLOT(updateIcon())); -void LegacySystemTray::syncLegacyIcon() { - _trayIcon->setIcon(stateIcon()); - _trayIcon->setToolTip(toolTipTitle()); + updateIcon(); + updateToolTip(); } -void LegacySystemTray::setVisible(bool visible) { - _isVisible = visible; - if(mode() == Legacy) { - if(visible) - _trayIcon->show(); - else - _trayIcon->hide(); - } + +bool LegacySystemTray::isSystemTrayAvailable() const +{ + return mode() == Mode::Legacy + ? QSystemTrayIcon::isSystemTrayAvailable() + : SystemTray::isSystemTrayAvailable(); } -void LegacySystemTray::setMode(Mode mode_) { - SystemTray::setMode(mode_); - - if(mode() == Legacy) { - syncLegacyIcon(); - if(_isVisible) - _trayIcon->show(); - if(state() == NeedsAttention) - _blinkTimer.start(); - } else { - _trayIcon->hide(); - _blinkTimer.stop(); - } + +void LegacySystemTray::onVisibilityChanged(bool isVisible) +{ + if (mode() == Legacy) { + _trayIcon->setVisible(isVisible); + } } -void LegacySystemTray::setState(State state_) { - State oldstate = state(); - SystemTray::setState(state_); - if(oldstate != state()) { - if(state() == NeedsAttention && mode() == Legacy) - _blinkTimer.start(); + +void LegacySystemTray::onModeChanged(Mode mode) +{ + if (mode == Mode::Legacy) { + _trayIcon->setVisible(isVisible()); + } else { - _blinkTimer.stop(); - _blinkState = false; + _trayIcon->hide(); } - } - if(mode() == Legacy) - _trayIcon->setIcon(stateIcon()); } -Icon LegacySystemTray::stateIcon() const { - if(mode() == Legacy && state() == NeedsAttention && !_blinkState) - return SystemTray::stateIcon(Active); - return SystemTray::stateIcon(); + +void LegacySystemTray::updateIcon() +{ + QString iconName = (state() == NeedsAttention) ? currentAttentionIconName() : currentIconName(); + _trayIcon->setIcon(QIcon::fromTheme(iconName, QIcon{QString{":/icons/hicolor/24x24/status/%1.svg"}.arg(iconName)})); +} + + +void LegacySystemTray::updateToolTip() +{ +#if defined Q_OS_MAC || defined Q_OS_WIN + QString tooltip = QString("%1").arg(toolTipTitle()); + if (!toolTipSubTitle().isEmpty()) + tooltip += QString("\n%1").arg(toolTipSubTitle()); +#else + QString tooltip = QString("%1").arg(toolTipTitle()); + if (!toolTipSubTitle().isEmpty()) + tooltip += QString("
%1").arg(toolTipSubTitle()); +#endif + + _trayIcon->setToolTip(tooltip); } -void LegacySystemTray::on_blinkTimeout() { - _blinkState = !_blinkState; - _trayIcon->setIcon(stateIcon()); + +void LegacySystemTray::onActivated(QSystemTrayIcon::ActivationReason reason) +{ + activate((SystemTray::ActivationReason)reason); } -void LegacySystemTray::on_activated(QSystemTrayIcon::ActivationReason reason) { - emit activated((ActivationReason)reason); - if(reason == QSystemTrayIcon::Trigger && !isActivationInhibited()) { +void LegacySystemTray::onMessageClicked() +{ + emit messageClicked(_lastMessageId); +} -# ifdef HAVE_KDE - // the slot is private, but meh, who cares :) - QMetaObject::invokeMethod(_trayIcon, "activateOrHide", Q_ARG(QSystemTrayIcon::ActivationReason, QSystemTrayIcon::Trigger)); -# else - QtUi::mainWindow()->toggleMinimizedToTray(); -# endif - } +void LegacySystemTray::showMessage(const QString &title, const QString &message, SystemTray::MessageIcon icon, int msTimeout, uint id) +{ + // fancy stuff later: show messages in order + // for now, we just show the last message + _lastMessageId = id; + _trayIcon->showMessage(title, message, (QSystemTrayIcon::MessageIcon)icon, msTimeout); } -void LegacySystemTray::showMessage(const QString &title, const QString &message, SystemTray::MessageIcon icon, int millisecondsTimeoutHint) { - _trayIcon->showMessage(title, message, (QSystemTrayIcon::MessageIcon)icon, millisecondsTimeoutHint); + +void LegacySystemTray::closeMessage(uint notificationId) +{ + Q_UNUSED(notificationId) + + // there really seems to be no sane way to close the bubble... :( +#ifdef Q_WS_X11 + showMessage("", "", NoIcon, 1); +#endif } + #endif /* QT_NO_SYSTEMTRAYICON */