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>
28 # include <KWindowInfo>
29 # include <KWindowSystem>
33 #include "actioncollection.h"
38 SystemTray::SystemTray(QWidget *parent)
40 _associatedWidget(parent)
44 NotificationSettings{}.initAndNotify("Systray/ChangeColor", this, SLOT(enableChangeColorChanged(QVariant)), true);
45 NotificationSettings{}.initAndNotify("Systray/Animate", this, SLOT(enableBlinkChanged(QVariant)), false);
46 UiStyleSettings{}.initAndNotify("Icons/InvertTray", this, SLOT(invertTrayIconChanged(QVariant)), false);
48 ActionCollection *coll = QtUi::actionCollection("General");
49 _minimizeRestoreAction = new Action(tr("&Minimize"), this, this, SLOT(minimizeRestore()));
53 _trayMenu = kmenu = new KMenu();
54 kmenu->addTitle(icon::get(iconName(State::Active)), "Quassel IRC");
56 _trayMenu = new QMenu(associatedWidget());
59 _trayMenu->setTitle("Quassel IRC");
62 _trayMenu->setAttribute(Qt::WA_Hover);
65 _trayMenu->addAction(coll->action("ConnectCore"));
66 _trayMenu->addAction(coll->action("DisconnectCore"));
67 _trayMenu->addAction(coll->action("CoreInfo"));
68 _trayMenu->addSeparator();
69 _trayMenu->addAction(_minimizeRestoreAction);
70 _trayMenu->addAction(coll->action("Quit"));
71 connect(_trayMenu, SIGNAL(aboutToShow()), SLOT(trayMenuAboutToShow()));
73 connect(QtUi::instance(), SIGNAL(iconThemeRefreshed()), this, SIGNAL(iconsChanged()));
75 _blinkTimer.setInterval(1000);
76 _blinkTimer.setSingleShot(false);
77 connect(&_blinkTimer, SIGNAL(timeout()), SLOT(onBlinkTimeout()));
81 SystemTray::~SystemTray()
83 _trayMenu->deleteLater();
87 QWidget *SystemTray::associatedWidget() const
89 return _associatedWidget;
93 bool SystemTray::isSystemTrayAvailable() const
99 bool SystemTray::isVisible() const
105 void SystemTray::setVisible(bool visible)
107 if (visible != _isVisible) {
108 _isVisible = visible;
109 emit visibilityChanged(visible);
114 SystemTray::Mode SystemTray::mode() const
120 void SystemTray::setMode(Mode mode)
126 if (mode == Mode::Legacy) {
127 _trayMenu->setWindowFlags(Qt::Popup);
130 _trayMenu->setWindowFlags(Qt::Window);
134 emit modeChanged(mode);
139 SystemTray::State SystemTray::state() const
145 void SystemTray::setState(State state)
147 if (_state != state) {
149 emit stateChanged(state);
151 if (state == NeedsAttention && _attentionBehavior == AttentionBehavior::Blink) {
159 emit currentIconNameChanged();
164 QString SystemTray::iconName(State state) const
169 name = "inactive-quassel-tray";
172 name = "active-quassel-tray";
174 case State::NeedsAttention:
175 name = "message-quassel-tray";
179 if (_trayIconInverted) {
187 QString SystemTray::currentIconName() const
189 if (state() == State::NeedsAttention) {
190 if (_attentionBehavior == AttentionBehavior::ChangeColor) {
191 return iconName(State::NeedsAttention);
193 if (_attentionBehavior == AttentionBehavior::Blink && _blinkState) {
194 return iconName(State::NeedsAttention);
196 return iconName(State::Active);
199 return iconName(state());
204 QString SystemTray::currentAttentionIconName() const
206 if (state() == State::NeedsAttention && _attentionBehavior == AttentionBehavior::Blink && !_blinkState) {
207 return iconName(State::Active);
209 return iconName(State::NeedsAttention);
213 bool SystemTray::isAlerted() const
215 return state() == State::NeedsAttention;
219 void SystemTray::setAlert(bool alerted)
222 setState(NeedsAttention);
225 setState(Client::isConnected() ? Active : Passive);
230 void SystemTray::onBlinkTimeout()
232 _blinkState = !_blinkState;
233 emit currentIconNameChanged();
237 QMenu *SystemTray::trayMenu() const
243 void SystemTray::trayMenuAboutToShow()
245 if (GraphicalUi::isMainWidgetVisible())
246 _minimizeRestoreAction->setText(tr("&Minimize"));
248 _minimizeRestoreAction->setText(tr("&Restore"));
252 void SystemTray::enableChangeColorChanged(const QVariant &v)
255 _attentionBehavior = AttentionBehavior::ChangeColor;
258 if (_attentionBehavior == AttentionBehavior::ChangeColor) {
259 _attentionBehavior = AttentionBehavior::DoNothing;
262 emit currentIconNameChanged();
266 void SystemTray::enableBlinkChanged(const QVariant &v)
269 _attentionBehavior = AttentionBehavior::Blink;
272 if (_attentionBehavior == AttentionBehavior::Blink) {
273 _attentionBehavior = AttentionBehavior::DoNothing;
276 emit currentIconNameChanged();
280 void SystemTray::invertTrayIconChanged(const QVariant &v)
282 _trayIconInverted = v.toBool();
287 QString SystemTray::toolTipTitle() const
289 return _toolTipTitle;
293 QString SystemTray::toolTipSubTitle() const
295 return _toolTipSubTitle;
299 void SystemTray::setToolTip(const QString &title, const QString &subtitle)
301 _toolTipTitle = title;
302 _toolTipSubTitle = subtitle;
303 emit toolTipChanged(title, subtitle);
307 void SystemTray::showMessage(const QString &title, const QString &message, MessageIcon icon, int millisecondsTimeoutHint, uint id)
312 Q_UNUSED(millisecondsTimeoutHint)
317 void SystemTray::closeMessage(uint notificationId)
319 Q_UNUSED(notificationId)
323 void SystemTray::activate(SystemTray::ActivationReason reason)
325 emit activated(reason);
329 void SystemTray::minimizeRestore()
331 GraphicalUi::toggleMainWidget();