1 /***************************************************************************
2 * Copyright (C) 2005-09 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
23 #include "systemtray.h"
25 #include "actioncollection.h"
26 #include "iconloader.h"
28 #include "qtuisettings.h"
30 SystemTray::SystemTray(QObject *parent)
34 _inhibitActivation(false),
38 _currentIdx = _idxOffEnd;
41 _trayIcon = new QSystemTrayIcon(_phases.at(_currentIdx), QtUi::mainWindow());
43 _trayIcon = new KSystemTrayIcon(_phases.at(_currentIdx), QtUi::mainWindow());
44 // We don't want to trigger a minimize if a highlight is pending, so we brutally remove the internal connection for that
45 disconnect(_trayIcon, SIGNAL(activated( QSystemTrayIcon::ActivationReason)),
46 _trayIcon, SLOT(activateOrHide(QSystemTrayIcon::ActivationReason)));
49 _animationTimer.setInterval(150);
50 _animationTimer.setSingleShot(false);
51 connect(&_animationTimer, SIGNAL(timeout()), SLOT(nextPhase()));
53 ActionCollection *coll = QtUi::actionCollection("General");
54 _trayMenu = new QMenu();
55 _trayMenu->addAction(coll->action("ConnectCore"));
56 _trayMenu->addAction(coll->action("DisconnectCore"));
57 _trayMenu->addAction(coll->action("CoreInfo"));
58 _trayMenu->addSeparator();
59 _trayMenu->addAction(coll->action("Quit"));
61 _trayIcon->setContextMenu(_trayMenu);
64 if(s.value("UseSystemTrayIcon", QVariant(true)).toBool()) {
68 qApp->installEventFilter(this);
71 connect(_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), SLOT(on_activated(QSystemTrayIcon::ActivationReason)));
73 connect(_trayIcon, SIGNAL(messageClicked()), SIGNAL(messageClicked()));
76 SystemTray::~SystemTray() {
77 _trayMenu->deleteLater();
80 void SystemTray::loadAnimations() {
81 // system tray icon size
84 #elif defined Q_WS_MAC
93 KIconLoader *loader = KIconLoader::global();
95 IconLoader *loader = IconLoader::global();
99 QString fadeOffName("quassel_tray-fade-off-%1");
100 for(int i = 2; i <= 10; i++)
101 _phases.append(loader->loadIcon(fadeOffName.arg(i), IconLoader::Panel, size));
102 _idxOffEnd = _idxOnStart = _phases.count() - 1;
104 QString fadeOnName("quassel_tray-fade-on-%1");
105 for(int i = 2; i <= 15; i++)
106 _phases.append(loader->loadIcon(fadeOnName.arg(i), IconLoader::Panel, size));
107 _idxOnEnd = _idxAlertStart = _phases.count() - 1;
109 QString alertName("quassel_tray-alert-%1");
110 for(int i = 1; i <= 10; i++)
111 _phases.append(loader->loadIcon(alertName.arg(i), IconLoader::Panel, size));
114 void SystemTray::nextPhase() {
115 if(_currentIdx == _idxOnEnd && !_alert && _state == Inactive)
116 _currentIdx = _idxOffStart; // skip alert phases
118 else if(++_currentIdx >= _phases.count()) {
120 _currentIdx = _idxAlertStart;
123 _currentIdx = _idxOnEnd;
125 _currentIdx = _idxOffStart;
128 _trayIcon->setIcon(_phases.at(_currentIdx));
133 if((_state == Active && _currentIdx == _idxOnEnd) || (_state == Inactive && _currentIdx == _idxOffEnd))
134 _animationTimer.stop();
137 void SystemTray::setState(State state) {
138 if(_state != state) {
140 if(state == Inactive && _alert)
142 if(!_animationTimer.isActive())
143 _animationTimer.start();
147 void SystemTray::setAlert(bool alert) {
148 if(_alert != alert) {
150 if(!_animationTimer.isActive())
151 _animationTimer.start();
155 void SystemTray::setIconVisible(bool visible) {
162 void SystemTray::setToolTip(const QString &tip) {
163 _trayIcon->setToolTip(tip);
166 void SystemTray::showMessage(const QString &title, const QString &message, QSystemTrayIcon::MessageIcon icon, int millisecondsTimeoutHint) {
167 _trayIcon->showMessage(title, message, icon, millisecondsTimeoutHint);
170 bool SystemTray::eventFilter(QObject *obj, QEvent *event) {
172 if(event->type() == QEvent::MouseButtonRelease) {
173 _inhibitActivation = false;
178 void SystemTray::on_activated(QSystemTrayIcon::ActivationReason reason) {
179 emit activated(reason);
181 if(reason == QSystemTrayIcon::Trigger && !_inhibitActivation) {
184 // the slot is private, but meh, who cares :)
185 QMetaObject::invokeMethod(_trayIcon, "activateOrHide", Q_ARG(QSystemTrayIcon::ActivationReason, QSystemTrayIcon::Trigger));
187 QtUi::mainWindow()->toggleMinimizedToTray();