b57fd995944f3a6e7e31db0e8215465f0a2da732
[quassel.git] / src / qtui / legacysystemtray.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2010 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 "legacysystemtray.h"
24 #include "qtui.h"
25
26 LegacySystemTray::LegacySystemTray(QObject *parent)
27   : SystemTray(parent),
28   _blinkState(false),
29   _isVisible(true)
30 {
31 #ifndef HAVE_KDE
32   _trayIcon = new QSystemTrayIcon(QtUi::mainWindow());
33 #else
34   _trayIcon = new KSystemTrayIcon(QtUi::mainWindow());
35   // We don't want to trigger a minimize if a highlight is pending, so we brutally remove the internal connection for that
36   disconnect(_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
37                  _trayIcon, SLOT(activateOrHide(QSystemTrayIcon::ActivationReason)));
38 #endif
39 #ifndef Q_WS_MAC
40   connect(_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
41                      SLOT(on_activated(QSystemTrayIcon::ActivationReason)));
42 #endif
43   connect(_trayIcon, SIGNAL(messageClicked()),
44                      SIGNAL(messageClicked()));
45
46   setTrayMenu(_trayIcon->contextMenu());
47   _trayIcon->setContextMenu(trayMenu());
48
49   _blinkTimer.setInterval(500);
50   _blinkTimer.setSingleShot(false);
51   connect(&_blinkTimer, SIGNAL(timeout()), SLOT(on_blinkTimeout()));
52 }
53
54 void LegacySystemTray::init() {
55   if(mode() == Invalid) // derived class hasn't set a mode itself
56     setMode(Legacy);
57 }
58
59 void LegacySystemTray::syncLegacyIcon() {
60   _trayIcon->setIcon(stateIcon());
61   _trayIcon->setToolTip(toolTipTitle());
62 }
63
64 void LegacySystemTray::setVisible(bool visible) {
65   _isVisible = visible;
66   if(mode() == Legacy) {
67     if(visible)
68       _trayIcon->show();
69     else
70       _trayIcon->hide();
71   }
72 }
73
74 void LegacySystemTray::setMode(Mode mode_) {
75   SystemTray::setMode(mode_);
76
77   if(mode() == Legacy) {
78     syncLegacyIcon();
79     if(_isVisible)
80       _trayIcon->show();
81     if(state() == NeedsAttention)
82       _blinkTimer.start();
83   } else {
84     _trayIcon->hide();
85     _blinkTimer.stop();
86   }
87 }
88
89 void LegacySystemTray::setState(State state_) {
90   State oldstate = state();
91   SystemTray::setState(state_);
92   if(oldstate != state()) {
93     if(state() == NeedsAttention && mode() == Legacy)
94       _blinkTimer.start();
95     else {
96       _blinkTimer.stop();
97       _blinkState = false;
98     }
99   }
100   if(mode() == Legacy)
101     _trayIcon->setIcon(stateIcon());
102 }
103
104 Icon LegacySystemTray::stateIcon() const {
105   if(mode() == Legacy && state() == NeedsAttention && !_blinkState)
106     return SystemTray::stateIcon(Active);
107   return SystemTray::stateIcon();
108 }
109
110 void LegacySystemTray::on_blinkTimeout() {
111   _blinkState = !_blinkState;
112   _trayIcon->setIcon(stateIcon());
113 }
114
115 void LegacySystemTray::on_activated(QSystemTrayIcon::ActivationReason reason) {
116   emit activated((ActivationReason)reason);
117
118   if(reason == QSystemTrayIcon::Trigger && !isActivationInhibited()) {
119
120 #  ifdef HAVE_KDE
121      // the slot is private, but meh, who cares :)
122      QMetaObject::invokeMethod(_trayIcon, "activateOrHide", Q_ARG(QSystemTrayIcon::ActivationReason, QSystemTrayIcon::Trigger));
123 #  else
124      QtUi::mainWindow()->toggleMinimizedToTray();
125 #  endif
126
127   }
128 }
129
130 void LegacySystemTray::showMessage(const QString &title, const QString &message, SystemTray::MessageIcon icon, int millisecondsTimeoutHint) {
131   _trayIcon->showMessage(title, message, (QSystemTrayIcon::MessageIcon)icon, millisecondsTimeoutHint);
132 }
133
134 #endif /* QT_NO_SYSTEMTRAYICON */