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