9969800003af923570d2936bb8e74c5baea4cd9a
[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(QWidget *parent)
27   : SystemTray(parent),
28   _blinkState(false),
29   _isVisible(true)
30 {
31 #ifndef HAVE_KDE
32   _trayIcon = new QSystemTrayIcon(associatedWidget());
33 #else
34   _trayIcon = new KSystemTrayIcon(associatedWidget());
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   _blinkTimer.setInterval(500);
47   _blinkTimer.setSingleShot(false);
48   connect(&_blinkTimer, SIGNAL(timeout()), SLOT(on_blinkTimeout()));
49 }
50
51 void LegacySystemTray::init() {
52   if(mode() == Invalid) // derived class hasn't set a mode itself
53     setMode(Legacy);
54
55   SystemTray::init();
56
57   _trayIcon->setContextMenu(trayMenu());
58 }
59
60 void LegacySystemTray::syncLegacyIcon() {
61   _trayIcon->setIcon(stateIcon());
62   _trayIcon->setToolTip(toolTipTitle());
63 }
64
65 void LegacySystemTray::setVisible(bool visible) {
66   _isVisible = visible;
67   if(mode() == Legacy) {
68     if(visible)
69       _trayIcon->show();
70     else
71       _trayIcon->hide();
72   }
73 }
74
75 bool LegacySystemTray::isVisible() const {
76   if(mode() == Legacy) {
77     return _trayIcon->isVisible();
78   }
79   return false;
80 }
81
82 void LegacySystemTray::setMode(Mode mode_) {
83   SystemTray::setMode(mode_);
84
85   if(mode() == Legacy) {
86     syncLegacyIcon();
87     if(_isVisible)
88       _trayIcon->show();
89     if(state() == NeedsAttention)
90       _blinkTimer.start();
91   } else {
92     _trayIcon->hide();
93     _blinkTimer.stop();
94   }
95 }
96
97 void LegacySystemTray::setState(State state_) {
98   State oldstate = state();
99   SystemTray::setState(state_);
100   if(oldstate != state()) {
101     if(state() == NeedsAttention && mode() == Legacy)
102       _blinkTimer.start();
103     else {
104       _blinkTimer.stop();
105       _blinkState = false;
106     }
107   }
108   if(mode() == Legacy)
109     _trayIcon->setIcon(stateIcon());
110 }
111
112 Icon LegacySystemTray::stateIcon() const {
113   if(mode() == Legacy && state() == NeedsAttention && !_blinkState)
114     return SystemTray::stateIcon(Active);
115   return SystemTray::stateIcon();
116 }
117
118 void LegacySystemTray::on_blinkTimeout() {
119   _blinkState = !_blinkState;
120   _trayIcon->setIcon(stateIcon());
121 }
122
123 void LegacySystemTray::on_activated(QSystemTrayIcon::ActivationReason reason) {
124   activate((SystemTray::ActivationReason)reason);
125 }
126
127 void LegacySystemTray::showMessage(const QString &title, const QString &message, SystemTray::MessageIcon icon, int millisecondsTimeoutHint) {
128   _trayIcon->showMessage(title, message, (QSystemTrayIcon::MessageIcon)icon, millisecondsTimeoutHint);
129 }
130
131 #endif /* QT_NO_SYSTEMTRAYICON */