Fixes #583 - identity settings: clicking delete or rename crashes client if there...
[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   _currentIdx(0)
35 {
36   loadAnimations();
37   _currentIdx = _idxOffEnd;
38   _trayIcon = new QSystemTrayIcon(_phases.at(_currentIdx), this);
39
40   _animationTimer.setInterval(150);
41   _animationTimer.setSingleShot(false);
42   connect(&_animationTimer, SIGNAL(timeout()), SLOT(nextPhase()));
43
44   ActionCollection *coll = QtUi::actionCollection("General");
45   _trayMenu = new QMenu();
46   _trayMenu->addAction(coll->action("ConnectCore"));
47   _trayMenu->addAction(coll->action("DisconnectCore"));
48   _trayMenu->addAction(coll->action("CoreInfo"));
49   _trayMenu->addSeparator();
50   _trayMenu->addAction(coll->action("Quit"));
51
52   _trayIcon->setContextMenu(_trayMenu);
53
54   QtUiSettings s;
55   if(s.value("UseSystemTrayIcon", QVariant(true)).toBool()) {
56     _trayIcon->show();
57   }
58
59   connect(_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), SIGNAL(activated(QSystemTrayIcon::ActivationReason)));
60   connect(_trayIcon, SIGNAL(messageClicked()), SIGNAL(messageClicked()));
61 }
62
63 SystemTray::~SystemTray() {
64   _trayMenu->deleteLater();
65 }
66
67 void SystemTray::loadAnimations() {
68 // system tray icon size
69 #ifdef Q_WS_WIN
70   const int size = 16;
71 #elif defined Q_WS_MAC
72   const int size = 128;
73 #else
74   const int size = 22;
75 #endif
76
77   _phases.clear();
78
79 #ifdef HAVE_KDE
80   KIconLoader *loader = KIconLoader::global();
81 #else
82   IconLoader *loader = IconLoader::global();
83 #endif
84
85   _idxOffStart = 0;
86   QString fadeOffName("quassel_tray-fade-off-%1");
87   for(int i = 2; i <= 10; i++)
88     _phases.append(loader->loadIcon(fadeOffName.arg(i), IconLoader::Panel, size));
89   _idxOffEnd = _idxOnStart = _phases.count() - 1;
90
91   QString fadeOnName("quassel_tray-fade-on-%1");
92   for(int i = 2; i <= 15; i++)
93     _phases.append(loader->loadIcon(fadeOnName.arg(i), IconLoader::Panel, size));
94   _idxOnEnd = _idxAlertStart = _phases.count() - 1;
95
96   QString alertName("quassel_tray-alert-%1");
97   for(int i = 1; i <= 10; i++)
98     _phases.append(loader->loadIcon(alertName.arg(i), IconLoader::Panel, size));
99 }
100
101 void SystemTray::nextPhase() {
102   if(_currentIdx == _idxOnEnd && !_alert && _state == Inactive)
103     _currentIdx = _idxOffStart; // skip alert phases
104
105   else if(++_currentIdx >= _phases.count()) {
106     if(_alert)
107       _currentIdx = _idxAlertStart;
108     else
109       if(_state == Active)
110         _currentIdx = _idxOnEnd;
111       else
112         _currentIdx = _idxOffStart;
113   }
114
115   _trayIcon->setIcon(_phases.at(_currentIdx));
116
117   if(_alert)
118     return;
119
120   if((_state == Active && _currentIdx == _idxOnEnd) || (_state == Inactive && _currentIdx == _idxOffEnd))
121     _animationTimer.stop();
122 }
123
124 void SystemTray::setState(State state) {
125   if(_state != state) {
126     _state = state;
127     if(state == Inactive && _alert)
128       _alert = false;
129     if(!_animationTimer.isActive())
130       _animationTimer.start();
131   }
132 }
133
134 void SystemTray::setAlert(bool alert) {
135   if(_alert != alert) {
136     _alert = alert;
137     if(!_animationTimer.isActive())
138       _animationTimer.start();
139   }
140 }
141
142 void SystemTray::setIconVisible(bool visible) {
143   if(visible)
144     _trayIcon->show();
145   else
146     _trayIcon->hide();
147 }
148
149 void SystemTray::setToolTip(const QString &tip) {
150   _trayIcon->setToolTip(tip);
151 }
152
153 void SystemTray::showMessage(const QString &title, const QString &message, QSystemTrayIcon::MessageIcon icon, int millisecondsTimeoutHint) {
154   _trayIcon->showMessage(title, message, icon, millisecondsTimeoutHint);
155 }