Add a tooltip to the tray icon
[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   connect(this, SIGNAL(toolTipChanged(QString,QString)), SLOT(syncLegacyIcon()));
51 }
52
53 void LegacySystemTray::init() {
54   if(mode() == Invalid) // derived class hasn't set a mode itself
55     setMode(Legacy);
56
57   SystemTray::init();
58
59   _trayIcon->setContextMenu(trayMenu());
60 }
61
62 void LegacySystemTray::syncLegacyIcon() {
63   _trayIcon->setIcon(stateIcon());
64
65   QString tooltip = QString("<b>%1</b>").arg(toolTipTitle());
66   if(!toolTipSubTitle().isEmpty())
67     tooltip += QString("<br>%1").arg(toolTipSubTitle());
68   _trayIcon->setToolTip(tooltip);
69 }
70
71 void LegacySystemTray::setVisible(bool visible) {
72   _isVisible = visible;
73   if(mode() == Legacy) {
74     if(visible)
75       _trayIcon->show();
76     else
77       _trayIcon->hide();
78   }
79 }
80
81 bool LegacySystemTray::isVisible() const {
82   if(mode() == Legacy) {
83     return _trayIcon->isVisible();
84   }
85   return false;
86 }
87
88 void LegacySystemTray::setMode(Mode mode_) {
89   SystemTray::setMode(mode_);
90
91   if(mode() == Legacy) {
92     syncLegacyIcon();
93     if(_isVisible)
94       _trayIcon->show();
95     if(state() == NeedsAttention)
96       _blinkTimer.start();
97   } else {
98     _trayIcon->hide();
99     _blinkTimer.stop();
100   }
101 }
102
103 void LegacySystemTray::setState(State state_) {
104   State oldstate = state();
105   SystemTray::setState(state_);
106   if(oldstate != state()) {
107     if(state() == NeedsAttention && mode() == Legacy)
108       _blinkTimer.start();
109     else {
110       _blinkTimer.stop();
111       _blinkState = false;
112     }
113   }
114   if(mode() == Legacy)
115     _trayIcon->setIcon(stateIcon());
116 }
117
118 Icon LegacySystemTray::stateIcon() const {
119   if(mode() == Legacy && state() == NeedsAttention && !_blinkState)
120     return SystemTray::stateIcon(Active);
121   return SystemTray::stateIcon();
122 }
123
124 void LegacySystemTray::on_blinkTimeout() {
125   _blinkState = !_blinkState;
126   _trayIcon->setIcon(stateIcon());
127 }
128
129 void LegacySystemTray::on_activated(QSystemTrayIcon::ActivationReason reason) {
130   activate((SystemTray::ActivationReason)reason);
131 }
132
133 void LegacySystemTray::showMessage(const QString &title, const QString &message, SystemTray::MessageIcon icon, int millisecondsTimeoutHint) {
134   _trayIcon->showMessage(title, message, (QSystemTrayIcon::MessageIcon)icon, millisecondsTimeoutHint);
135 }
136
137 #endif /* QT_NO_SYSTEMTRAYICON */