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 #include "systemtray.h"
23 #include <QApplication>
27 #include "actioncollection.h"
32 SystemTray::SystemTray(QWidget* parent)
34 , _associatedWidget(parent)
38 NotificationSettings{}.initAndNotify("Systray/ChangeColor", this, &SystemTray::enableChangeColorChanged, true);
39 NotificationSettings{}.initAndNotify("Systray/Animate", this, &SystemTray::enableBlinkChanged, false);
40 UiStyleSettings{}.initAndNotify("Icons/InvertTray", this, &SystemTray::invertTrayIconChanged, false);
42 ActionCollection* coll = QtUi::actionCollection("General");
43 _minimizeRestoreAction = new Action(tr("&Minimize"), this, this, &SystemTray::minimizeRestore);
45 _trayMenu = new QMenu(associatedWidget());
46 _trayMenu->setTitle("Quassel IRC");
47 _trayMenu->setAttribute(Qt::WA_Hover);
49 _trayMenu->addAction(coll->action("ConnectCore"));
50 _trayMenu->addAction(coll->action("DisconnectCore"));
51 _trayMenu->addAction(coll->action("CoreInfo"));
52 _trayMenu->addSeparator();
53 _trayMenu->addAction(_minimizeRestoreAction);
54 _trayMenu->addAction(coll->action("Quit"));
55 connect(_trayMenu, &QMenu::aboutToShow, this, &SystemTray::trayMenuAboutToShow);
57 connect(QtUi::instance(), &QtUi::iconThemeRefreshed, this, &SystemTray::iconsChanged);
59 _blinkTimer.setInterval(1000);
60 _blinkTimer.setSingleShot(false);
61 connect(&_blinkTimer, &QTimer::timeout, this, &SystemTray::onBlinkTimeout);
64 SystemTray::~SystemTray()
66 _trayMenu->deleteLater();
69 QWidget* SystemTray::associatedWidget() const
71 return _associatedWidget;
74 bool SystemTray::isSystemTrayAvailable() const
79 bool SystemTray::isVisible() const
84 void SystemTray::setVisible(bool visible)
86 if (visible != _isVisible) {
88 emit visibilityChanged(visible);
92 SystemTray::Mode SystemTray::mode() const
97 void SystemTray::setMode(Mode mode)
101 emit modeChanged(mode);
105 SystemTray::State SystemTray::state() const
110 void SystemTray::setState(State state)
112 if (_state != state) {
114 emit stateChanged(state);
116 if (state == NeedsAttention && _attentionBehavior == AttentionBehavior::Blink) {
124 emit currentIconNameChanged();
128 QString SystemTray::iconName(State state) const
133 name = "inactive-quassel-tray";
136 name = "active-quassel-tray";
138 case State::NeedsAttention:
139 name = "message-quassel-tray";
143 if (_trayIconInverted) {
150 QString SystemTray::currentIconName() const
152 if (state() == State::NeedsAttention) {
153 if (_attentionBehavior == AttentionBehavior::ChangeColor) {
154 return iconName(State::NeedsAttention);
156 if (_attentionBehavior == AttentionBehavior::Blink && _blinkState) {
157 return iconName(State::NeedsAttention);
159 return iconName(State::Active);
162 return iconName(state());
166 QString SystemTray::currentAttentionIconName() const
168 if (state() == State::NeedsAttention && _attentionBehavior == AttentionBehavior::Blink && !_blinkState) {
169 return iconName(State::Active);
171 return iconName(State::NeedsAttention);
174 bool SystemTray::isAlerted() const
176 return state() == State::NeedsAttention;
179 void SystemTray::setAlert(bool alerted)
182 setState(NeedsAttention);
185 setState(Client::isConnected() ? Active : Passive);
189 void SystemTray::onBlinkTimeout()
191 _blinkState = !_blinkState;
192 emit currentIconNameChanged();
195 QMenu* SystemTray::trayMenu() const
200 void SystemTray::trayMenuAboutToShow()
202 if (GraphicalUi::isMainWidgetVisible())
203 _minimizeRestoreAction->setText(tr("&Minimize"));
205 _minimizeRestoreAction->setText(tr("&Restore"));
208 void SystemTray::enableChangeColorChanged(const QVariant& v)
211 _attentionBehavior = AttentionBehavior::ChangeColor;
214 if (_attentionBehavior == AttentionBehavior::ChangeColor) {
215 _attentionBehavior = AttentionBehavior::DoNothing;
218 emit currentIconNameChanged();
221 void SystemTray::enableBlinkChanged(const QVariant& v)
224 _attentionBehavior = AttentionBehavior::Blink;
227 if (_attentionBehavior == AttentionBehavior::Blink) {
228 _attentionBehavior = AttentionBehavior::DoNothing;
231 emit currentIconNameChanged();
234 void SystemTray::invertTrayIconChanged(const QVariant& v)
236 _trayIconInverted = v.toBool();
240 QString SystemTray::toolTipTitle() const
242 return _toolTipTitle;
245 QString SystemTray::toolTipSubTitle() const
247 return _toolTipSubTitle;
250 void SystemTray::setToolTip(const QString& title, const QString& subtitle)
252 _toolTipTitle = title;
253 _toolTipSubTitle = subtitle;
254 emit toolTipChanged(title, subtitle);
257 void SystemTray::showMessage(const QString& title, const QString& message, MessageIcon icon, int millisecondsTimeoutHint, uint id)
262 Q_UNUSED(millisecondsTimeoutHint)
266 void SystemTray::closeMessage(uint notificationId)
268 Q_UNUSED(notificationId)
271 void SystemTray::activate(SystemTray::ActivationReason reason)
273 emit activated(reason);
276 void SystemTray::minimizeRestore()
278 GraphicalUi::toggleMainWidget();