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