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 #include "systemtray.h"
23 #include <QApplication>
27 #include "actioncollection.h"
32 SystemTray::SystemTray(QWidget *parent)
34 _associatedWidget(parent)
38 NotificationSettings{}.initAndNotify("Systray/ChangeColor", this, SLOT(enableChangeColorChanged(QVariant)), true);
39 NotificationSettings{}.initAndNotify("Systray/Animate", this, SLOT(enableBlinkChanged(QVariant)), false);
40 UiStyleSettings{}.initAndNotify("Icons/InvertTray", this, SLOT(invertTrayIconChanged(QVariant)), false);
42 ActionCollection *coll = QtUi::actionCollection("General");
43 _minimizeRestoreAction = new Action(tr("&Minimize"), this, this, SLOT(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, SIGNAL(aboutToShow()), SLOT(trayMenuAboutToShow()));
57 connect(QtUi::instance(), SIGNAL(iconThemeRefreshed()), this, SIGNAL(iconsChanged()));
59 _blinkTimer.setInterval(1000);
60 _blinkTimer.setSingleShot(false);
61 connect(&_blinkTimer, SIGNAL(timeout()), SLOT(onBlinkTimeout()));
65 SystemTray::~SystemTray()
67 _trayMenu->deleteLater();
71 QWidget *SystemTray::associatedWidget() const
73 return _associatedWidget;
77 bool SystemTray::isSystemTrayAvailable() const
83 bool SystemTray::isVisible() const
89 void SystemTray::setVisible(bool visible)
91 if (visible != _isVisible) {
93 emit visibilityChanged(visible);
98 SystemTray::Mode SystemTray::mode() const
104 void SystemTray::setMode(Mode mode)
108 emit modeChanged(mode);
113 SystemTray::State SystemTray::state() const
119 void SystemTray::setState(State state)
121 if (_state != state) {
123 emit stateChanged(state);
125 if (state == NeedsAttention && _attentionBehavior == AttentionBehavior::Blink) {
133 emit currentIconNameChanged();
138 QString SystemTray::iconName(State state) const
143 name = "inactive-quassel-tray";
146 name = "active-quassel-tray";
148 case State::NeedsAttention:
149 name = "message-quassel-tray";
153 if (_trayIconInverted) {
161 QString SystemTray::currentIconName() const
163 if (state() == State::NeedsAttention) {
164 if (_attentionBehavior == AttentionBehavior::ChangeColor) {
165 return iconName(State::NeedsAttention);
167 if (_attentionBehavior == AttentionBehavior::Blink && _blinkState) {
168 return iconName(State::NeedsAttention);
170 return iconName(State::Active);
173 return iconName(state());
178 QString SystemTray::currentAttentionIconName() const
180 if (state() == State::NeedsAttention && _attentionBehavior == AttentionBehavior::Blink && !_blinkState) {
181 return iconName(State::Active);
183 return iconName(State::NeedsAttention);
187 bool SystemTray::isAlerted() const
189 return state() == State::NeedsAttention;
193 void SystemTray::setAlert(bool alerted)
196 setState(NeedsAttention);
199 setState(Client::isConnected() ? Active : Passive);
204 void SystemTray::onBlinkTimeout()
206 _blinkState = !_blinkState;
207 emit currentIconNameChanged();
211 QMenu *SystemTray::trayMenu() const
217 void SystemTray::trayMenuAboutToShow()
219 if (GraphicalUi::isMainWidgetVisible())
220 _minimizeRestoreAction->setText(tr("&Minimize"));
222 _minimizeRestoreAction->setText(tr("&Restore"));
226 void SystemTray::enableChangeColorChanged(const QVariant &v)
229 _attentionBehavior = AttentionBehavior::ChangeColor;
232 if (_attentionBehavior == AttentionBehavior::ChangeColor) {
233 _attentionBehavior = AttentionBehavior::DoNothing;
236 emit currentIconNameChanged();
240 void SystemTray::enableBlinkChanged(const QVariant &v)
243 _attentionBehavior = AttentionBehavior::Blink;
246 if (_attentionBehavior == AttentionBehavior::Blink) {
247 _attentionBehavior = AttentionBehavior::DoNothing;
250 emit currentIconNameChanged();
254 void SystemTray::invertTrayIconChanged(const QVariant &v)
256 _trayIconInverted = v.toBool();
261 QString SystemTray::toolTipTitle() const
263 return _toolTipTitle;
267 QString SystemTray::toolTipSubTitle() const
269 return _toolTipSubTitle;
273 void SystemTray::setToolTip(const QString &title, const QString &subtitle)
275 _toolTipTitle = title;
276 _toolTipSubTitle = subtitle;
277 emit toolTipChanged(title, subtitle);
281 void SystemTray::showMessage(const QString &title, const QString &message, MessageIcon icon, int millisecondsTimeoutHint, uint id)
286 Q_UNUSED(millisecondsTimeoutHint)
291 void SystemTray::closeMessage(uint notificationId)
293 Q_UNUSED(notificationId)
297 void SystemTray::activate(SystemTray::ActivationReason reason)
299 emit activated(reason);
303 void SystemTray::minimizeRestore()
305 GraphicalUi::toggleMainWidget();