Move "Appearance" to "Interface" in settingspages
[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 = 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"));
60
61   _trayIcon->setContextMenu(_trayMenu);
62
63   QtUiSettings s;
64   if(s.value("UseSystemTrayIcon", QVariant(true)).toBool()) {
65     _trayIcon->show();
66   }
67
68   qApp->installEventFilter(this);
69
70 #ifndef Q_WS_MAC
71   connect(_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), SLOT(on_activated(QSystemTrayIcon::ActivationReason)));
72 #endif
73   connect(_trayIcon, SIGNAL(messageClicked()), SIGNAL(messageClicked()));
74 }
75
76 SystemTray::~SystemTray() {
77   _trayMenu->deleteLater();
78 }
79
80 void SystemTray::loadAnimations() {
81 // system tray icon size
82 #ifdef Q_WS_WIN
83   const int size = 16;
84 #elif defined Q_WS_MAC
85   const int size = 128;
86 #else
87   const int size = 22;
88 #endif
89
90   _phases.clear();
91
92 #ifdef HAVE_KDE
93   KIconLoader *loader = KIconLoader::global();
94 #else
95   IconLoader *loader = IconLoader::global();
96 #endif
97
98   _idxOffStart = 0;
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;
103
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;
108
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));
112 }
113
114 void SystemTray::nextPhase() {
115   if(_currentIdx == _idxOnEnd && !_alert && _state == Inactive)
116     _currentIdx = _idxOffStart; // skip alert phases
117
118   else if(++_currentIdx >= _phases.count()) {
119     if(_alert)
120       _currentIdx = _idxAlertStart;
121     else
122       if(_state == Active)
123         _currentIdx = _idxOnEnd;
124       else
125         _currentIdx = _idxOffStart;
126   }
127
128   _trayIcon->setIcon(_phases.at(_currentIdx));
129
130   if(_alert)
131     return;
132
133   if((_state == Active && _currentIdx == _idxOnEnd) || (_state == Inactive && _currentIdx == _idxOffEnd))
134     _animationTimer.stop();
135 }
136
137 void SystemTray::setState(State state) {
138   if(_state != state) {
139     _state = state;
140     if(state == Inactive && _alert)
141       _alert = false;
142     if(!_animationTimer.isActive())
143       _animationTimer.start();
144   }
145 }
146
147 void SystemTray::setAlert(bool alert) {
148   if(_alert != alert) {
149     _alert = alert;
150     if(!_animationTimer.isActive())
151       _animationTimer.start();
152   }
153 }
154
155 void SystemTray::setIconVisible(bool visible) {
156   if(visible)
157     _trayIcon->show();
158   else
159     _trayIcon->hide();
160 }
161
162 void SystemTray::setToolTip(const QString &tip) {
163   _trayIcon->setToolTip(tip);
164 }
165
166 void SystemTray::showMessage(const QString &title, const QString &message, QSystemTrayIcon::MessageIcon icon, int millisecondsTimeoutHint) {
167   _trayIcon->showMessage(title, message, icon, millisecondsTimeoutHint);
168 }
169
170 bool SystemTray::eventFilter(QObject *obj, QEvent *event) {
171   Q_UNUSED(obj);
172   if(event->type() == QEvent::MouseButtonRelease) {
173     _inhibitActivation = false;
174   }
175   return false;
176 }
177
178 void SystemTray::on_activated(QSystemTrayIcon::ActivationReason reason) {
179   emit activated(reason);
180
181   if(reason == QSystemTrayIcon::Trigger && !_inhibitActivation) {
182
183 #  ifdef HAVE_KDE
184      // the slot is private, but meh, who cares :)
185      QMetaObject::invokeMethod(_trayIcon, "activateOrHide", Q_ARG(QSystemTrayIcon::ActivationReason, QSystemTrayIcon::Trigger));
186 #  else
187      QtUi::mainWindow()->toggleMinimizedToTray();
188 #  endif
189
190   }
191 }