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 ***************************************************************************/
21 #ifndef QT_NO_SYSTEMTRAYICON
25 #include "systemtray.h"
27 #include "actioncollection.h"
28 #include "iconloader.h"
30 #include "qtuisettings.h"
32 SystemTray::SystemTray(QObject *parent)
36 _inhibitActivation(false),
40 _currentIdx = _idxOffEnd;
43 _trayIcon = new QSystemTrayIcon(_phases.at(_currentIdx), QtUi::mainWindow());
45 _trayIcon = new KSystemTrayIcon(_phases.at(_currentIdx), QtUi::mainWindow());
46 // We don't want to trigger a minimize if a highlight is pending, so we brutally remove the internal connection for that
47 disconnect(_trayIcon, SIGNAL(activated( QSystemTrayIcon::ActivationReason)),
48 _trayIcon, SLOT(activateOrHide(QSystemTrayIcon::ActivationReason)));
51 _animationTimer.setInterval(150);
52 _animationTimer.setSingleShot(false);
53 connect(&_animationTimer, SIGNAL(timeout()), SLOT(nextPhase()));
55 ActionCollection *coll = QtUi::actionCollection("General");
56 _trayMenu = _trayIcon->contextMenu();
58 _trayMenu = new QMenu();
59 _trayMenu->addAction(coll->action("ConnectCore"));
60 _trayMenu->addAction(coll->action("DisconnectCore"));
61 _trayMenu->addAction(coll->action("CoreInfo"));
63 _trayMenu->addSeparator();
64 _trayMenu->addAction(coll->action("Quit"));
67 _trayIcon->setContextMenu(_trayMenu);
70 if(s.value("UseSystemTrayIcon", QVariant(true)).toBool()) {
74 qApp->installEventFilter(this);
77 connect(_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), SLOT(on_activated(QSystemTrayIcon::ActivationReason)));
79 connect(_trayIcon, SIGNAL(messageClicked()), SIGNAL(messageClicked()));
82 SystemTray::~SystemTray() {
83 _trayMenu->deleteLater();
86 void SystemTray::loadAnimations() {
87 // system tray icon size
90 #elif defined Q_WS_MAC
99 KIconLoader *loader = KIconLoader::global();
101 IconLoader *loader = IconLoader::global();
105 QString fadeOffName("quassel_tray-fade-off-%1");
106 for(int i = 2; i <= 10; i++)
107 _phases.append(loader->loadIcon(fadeOffName.arg(i), IconLoader::Panel, size));
108 _idxOffEnd = _idxOnStart = _phases.count() - 1;
110 QString fadeOnName("quassel_tray-fade-on-%1");
111 for(int i = 2; i <= 15; i++)
112 _phases.append(loader->loadIcon(fadeOnName.arg(i), IconLoader::Panel, size));
113 _idxOnEnd = _idxAlertStart = _phases.count() - 1;
115 QString alertName("quassel_tray-alert-%1");
116 for(int i = 1; i <= 10; i++)
117 _phases.append(loader->loadIcon(alertName.arg(i), IconLoader::Panel, size));
120 void SystemTray::nextPhase() {
121 if(_currentIdx == _idxOnEnd && !_alert && _state == Inactive)
122 _currentIdx = _idxOffStart; // skip alert phases
124 else if(++_currentIdx >= _phases.count()) {
126 _currentIdx = _idxAlertStart;
129 _currentIdx = _idxOnEnd;
131 _currentIdx = _idxOffStart;
134 _trayIcon->setIcon(_phases.at(_currentIdx));
139 if((_state == Active && _currentIdx == _idxOnEnd) || (_state == Inactive && _currentIdx == _idxOffEnd))
140 _animationTimer.stop();
143 void SystemTray::setState(State state) {
144 if(_state != state) {
146 if(state == Inactive && _alert)
148 if(!_animationTimer.isActive())
149 _animationTimer.start();
153 void SystemTray::setAlert(bool alert) {
154 if(_alert != alert) {
156 if(!_animationTimer.isActive())
157 _animationTimer.start();
161 void SystemTray::setIconVisible(bool visible) {
168 void SystemTray::setToolTip(const QString &tip) {
169 _trayIcon->setToolTip(tip);
172 void SystemTray::showMessage(const QString &title, const QString &message, QSystemTrayIcon::MessageIcon icon, int millisecondsTimeoutHint) {
173 _trayIcon->showMessage(title, message, icon, millisecondsTimeoutHint);
176 bool SystemTray::eventFilter(QObject *obj, QEvent *event) {
178 if(event->type() == QEvent::MouseButtonRelease) {
179 _inhibitActivation = false;
184 void SystemTray::on_activated(QSystemTrayIcon::ActivationReason reason) {
185 emit activated(reason);
187 if(reason == QSystemTrayIcon::Trigger && !_inhibitActivation) {
190 // the slot is private, but meh, who cares :)
191 QMetaObject::invokeMethod(_trayIcon, "activateOrHide", Q_ARG(QSystemTrayIcon::ActivationReason, QSystemTrayIcon::Trigger));
193 QtUi::mainWindow()->toggleMinimizedToTray();
199 #endif /* QT_NO_SYSTEMTRAYICON */