fixed focus when closing ChatViewSearchbar
[quassel.git] / src / qtui / systemtray.cpp
1 /***************************************************************************
2 *   Copyright (C) 2005-09 by the Quassel Project                          *
3 *   devel@quassel-irc.org                                                 *
4 *                                                                         *
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.                                           *
9 *                                                                         *
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.                          *
14 *                                                                         *
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 ***************************************************************************/
20
21 #ifndef QT_NO_SYSTEMTRAYICON
22
23 #include <QMenu>
24
25 #include "systemtray.h"
26
27 #include "actioncollection.h"
28 #include "iconloader.h"
29 #include "qtui.h"
30 #include "qtuisettings.h"
31
32 SystemTray::SystemTray(QObject *parent)
33 : QObject(parent),
34   _state(Inactive),
35   _alert(false),
36   _inhibitActivation(false),
37   _currentIdx(0)
38 {
39   loadAnimations();
40   _currentIdx = _idxOffEnd;
41
42 #ifndef HAVE_KDE
43   _trayIcon = new QSystemTrayIcon(_phases.at(_currentIdx), QtUi::mainWindow());
44 #else
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)));
49 #endif
50
51   _animationTimer.setInterval(150);
52   _animationTimer.setSingleShot(false);
53   connect(&_animationTimer, SIGNAL(timeout()), SLOT(nextPhase()));
54
55   ActionCollection *coll = QtUi::actionCollection("General");
56   _trayMenu = _trayIcon->contextMenu();
57   if (!_trayMenu)
58     _trayMenu = new QMenu();
59   _trayMenu->addAction(coll->action("ConnectCore"));
60   _trayMenu->addAction(coll->action("DisconnectCore"));
61   _trayMenu->addAction(coll->action("CoreInfo"));
62 #ifndef HAVE_KDE
63   _trayMenu->addSeparator();
64   _trayMenu->addAction(coll->action("Quit"));
65 #endif /* HAVE_KDE */
66
67   _trayIcon->setContextMenu(_trayMenu);
68
69   QtUiSettings s;
70   if(s.value("UseSystemTrayIcon", QVariant(true)).toBool()) {
71     _trayIcon->show();
72   }
73
74   qApp->installEventFilter(this);
75
76 #ifndef Q_WS_MAC
77   connect(_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), SLOT(on_activated(QSystemTrayIcon::ActivationReason)));
78 #endif
79   connect(_trayIcon, SIGNAL(messageClicked()), SIGNAL(messageClicked()));
80 }
81
82 SystemTray::~SystemTray() {
83   _trayMenu->deleteLater();
84 }
85
86 void SystemTray::loadAnimations() {
87 // system tray icon size
88 #ifdef Q_WS_WIN
89   const int size = 16;
90 #elif defined Q_WS_MAC
91   const int size = 128;
92 #else
93   const int size = 22;
94 #endif
95
96   _phases.clear();
97
98 #ifdef HAVE_KDE
99   KIconLoader *loader = KIconLoader::global();
100 #else
101   IconLoader *loader = IconLoader::global();
102 #endif
103
104   _idxOffStart = 0;
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;
109
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;
114
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));
118 }
119
120 void SystemTray::nextPhase() {
121   if(_currentIdx == _idxOnEnd && !_alert && _state == Inactive)
122     _currentIdx = _idxOffStart; // skip alert phases
123
124   else if(++_currentIdx >= _phases.count()) {
125     if(_alert)
126       _currentIdx = _idxAlertStart;
127     else
128       if(_state == Active)
129         _currentIdx = _idxOnEnd;
130       else
131         _currentIdx = _idxOffStart;
132   }
133
134   _trayIcon->setIcon(_phases.at(_currentIdx));
135
136   if(_alert)
137     return;
138
139   if((_state == Active && _currentIdx == _idxOnEnd) || (_state == Inactive && _currentIdx == _idxOffEnd))
140     _animationTimer.stop();
141 }
142
143 void SystemTray::setState(State state) {
144   if(_state != state) {
145     _state = state;
146     if(state == Inactive && _alert)
147       _alert = false;
148     if(!_animationTimer.isActive())
149       _animationTimer.start();
150   }
151 }
152
153 void SystemTray::setAlert(bool alert) {
154   if(_alert != alert) {
155     _alert = alert;
156     if(!_animationTimer.isActive())
157       _animationTimer.start();
158   }
159 }
160
161 void SystemTray::setIconVisible(bool visible) {
162   if(visible)
163     _trayIcon->show();
164   else
165     _trayIcon->hide();
166 }
167
168 void SystemTray::setToolTip(const QString &tip) {
169   _trayIcon->setToolTip(tip);
170 }
171
172 void SystemTray::showMessage(const QString &title, const QString &message, QSystemTrayIcon::MessageIcon icon, int millisecondsTimeoutHint) {
173   _trayIcon->showMessage(title, message, icon, millisecondsTimeoutHint);
174 }
175
176 bool SystemTray::eventFilter(QObject *obj, QEvent *event) {
177   Q_UNUSED(obj);
178   if(event->type() == QEvent::MouseButtonRelease) {
179     _inhibitActivation = false;
180   }
181   return false;
182 }
183
184 void SystemTray::on_activated(QSystemTrayIcon::ActivationReason reason) {
185   emit activated(reason);
186
187   if(reason == QSystemTrayIcon::Trigger && !_inhibitActivation) {
188
189 #  ifdef HAVE_KDE
190      // the slot is private, but meh, who cares :)
191      QMetaObject::invokeMethod(_trayIcon, "activateOrHide", Q_ARG(QSystemTrayIcon::ActivationReason, QSystemTrayIcon::Trigger));
192 #  else
193      QtUi::mainWindow()->toggleMinimizedToTray();
194 #  endif
195
196   }
197 }
198
199 #endif /* QT_NO_SYSTEMTRAYICON */