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 = _trayIcon->contextMenu();
56 _trayMenu = new QMenu();
57 _trayMenu->addAction(coll->action("ConnectCore"));
58 _trayMenu->addAction(coll->action("DisconnectCore"));
59 _trayMenu->addAction(coll->action("CoreInfo"));
61 _trayMenu->addSeparator();
62 _trayMenu->addAction(coll->action("Quit"));
65 _trayIcon->setContextMenu(_trayMenu);
68 if(s.value("UseSystemTrayIcon", QVariant(true)).toBool()) {
72 qApp->installEventFilter(this);
75 connect(_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), SLOT(on_activated(QSystemTrayIcon::ActivationReason)));
77 connect(_trayIcon, SIGNAL(messageClicked()), SIGNAL(messageClicked()));
80 SystemTray::~SystemTray() {
81 _trayMenu->deleteLater();
84 void SystemTray::loadAnimations() {
85 // system tray icon size
88 #elif defined Q_WS_MAC
97 KIconLoader *loader = KIconLoader::global();
99 IconLoader *loader = IconLoader::global();
103 QString fadeOffName("quassel_tray-fade-off-%1");
104 for(int i = 2; i <= 10; i++)
105 _phases.append(loader->loadIcon(fadeOffName.arg(i), IconLoader::Panel, size));
106 _idxOffEnd = _idxOnStart = _phases.count() - 1;
108 QString fadeOnName("quassel_tray-fade-on-%1");
109 for(int i = 2; i <= 15; i++)
110 _phases.append(loader->loadIcon(fadeOnName.arg(i), IconLoader::Panel, size));
111 _idxOnEnd = _idxAlertStart = _phases.count() - 1;
113 QString alertName("quassel_tray-alert-%1");
114 for(int i = 1; i <= 10; i++)
115 _phases.append(loader->loadIcon(alertName.arg(i), IconLoader::Panel, size));
118 void SystemTray::nextPhase() {
119 if(_currentIdx == _idxOnEnd && !_alert && _state == Inactive)
120 _currentIdx = _idxOffStart; // skip alert phases
122 else if(++_currentIdx >= _phases.count()) {
124 _currentIdx = _idxAlertStart;
127 _currentIdx = _idxOnEnd;
129 _currentIdx = _idxOffStart;
132 _trayIcon->setIcon(_phases.at(_currentIdx));
137 if((_state == Active && _currentIdx == _idxOnEnd) || (_state == Inactive && _currentIdx == _idxOffEnd))
138 _animationTimer.stop();
141 void SystemTray::setState(State state) {
142 if(_state != state) {
144 if(state == Inactive && _alert)
146 if(!_animationTimer.isActive())
147 _animationTimer.start();
151 void SystemTray::setAlert(bool alert) {
152 if(_alert != alert) {
154 if(!_animationTimer.isActive())
155 _animationTimer.start();
159 void SystemTray::setIconVisible(bool visible) {
166 void SystemTray::setToolTip(const QString &tip) {
167 _trayIcon->setToolTip(tip);
170 void SystemTray::showMessage(const QString &title, const QString &message, QSystemTrayIcon::MessageIcon icon, int millisecondsTimeoutHint) {
171 _trayIcon->showMessage(title, message, icon, millisecondsTimeoutHint);
174 bool SystemTray::eventFilter(QObject *obj, QEvent *event) {
176 if(event->type() == QEvent::MouseButtonRelease) {
177 _inhibitActivation = false;
182 void SystemTray::on_activated(QSystemTrayIcon::ActivationReason reason) {
183 emit activated(reason);
185 if(reason == QSystemTrayIcon::Trigger && !_inhibitActivation) {
188 // the slot is private, but meh, who cares :)
189 QMetaObject::invokeMethod(_trayIcon, "activateOrHide", Q_ARG(QSystemTrayIcon::ActivationReason, QSystemTrayIcon::Trigger));
191 QtUi::mainWindow()->toggleMinimizedToTray();