fix trayicon-tooltip for mac and win
[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   _lastMessageId(0)
32 {
33 #ifndef HAVE_KDE
34   _trayIcon = new QSystemTrayIcon(associatedWidget());
35 #else
36   _trayIcon = new KSystemTrayIcon(associatedWidget());
37   // We don't want to trigger a minimize if a highlight is pending, so we brutally remove the internal connection for that
38   disconnect(_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
39                  _trayIcon, SLOT(activateOrHide(QSystemTrayIcon::ActivationReason)));
40 #endif
41 #ifndef Q_WS_MAC
42   connect(_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
43                      SLOT(on_activated(QSystemTrayIcon::ActivationReason)));
44 #endif
45   connect(_trayIcon, SIGNAL(messageClicked()),
46                      SLOT(on_messageClicked()));
47
48   _blinkTimer.setInterval(500);
49   _blinkTimer.setSingleShot(false);
50   connect(&_blinkTimer, SIGNAL(timeout()), SLOT(on_blinkTimeout()));
51
52   connect(this, SIGNAL(toolTipChanged(QString,QString)), SLOT(syncLegacyIcon()));
53 }
54
55 void LegacySystemTray::init() {
56   if(mode() == Invalid) // derived class hasn't set a mode itself
57     setMode(Legacy);
58
59   SystemTray::init();
60
61   _trayIcon->setContextMenu(trayMenu());
62 }
63
64 void LegacySystemTray::syncLegacyIcon() {
65   _trayIcon->setIcon(stateIcon());
66
67 #if defined Q_WS_MAC || defined Q_WS_WIN
68   QString tooltip = QString("%1").arg(toolTipTitle());
69 #else
70   QString tooltip = QString("<b>%1</b>").arg(toolTipTitle());
71 #endif
72   if(!toolTipSubTitle().isEmpty())
73     tooltip += QString("\n%1").arg(toolTipSubTitle());
74   _trayIcon->setToolTip(tooltip);
75 }
76
77 void LegacySystemTray::setVisible(bool visible) {
78   _isVisible = visible;
79   if(mode() == Legacy) {
80     if(visible)
81       _trayIcon->show();
82     else
83       _trayIcon->hide();
84   }
85 }
86
87 bool LegacySystemTray::isVisible() const {
88   if(mode() == Legacy) {
89     return _trayIcon->isVisible();
90   }
91   return false;
92 }
93
94 void LegacySystemTray::setMode(Mode mode_) {
95   SystemTray::setMode(mode_);
96
97   if(mode() == Legacy) {
98     syncLegacyIcon();
99     if(_isVisible)
100       _trayIcon->show();
101     if(state() == NeedsAttention)
102       _blinkTimer.start();
103   } else {
104     _trayIcon->hide();
105     _blinkTimer.stop();
106   }
107 }
108
109 void LegacySystemTray::setState(State state_) {
110   State oldstate = state();
111   SystemTray::setState(state_);
112   if(oldstate != state()) {
113     if(state() == NeedsAttention && mode() == Legacy && animationEnabled())
114       _blinkTimer.start();
115     else {
116       _blinkTimer.stop();
117       _blinkState = false;
118     }
119   }
120   if(mode() == Legacy)
121     _trayIcon->setIcon(stateIcon());
122 }
123
124 Icon LegacySystemTray::stateIcon() const {
125   if(mode() == Legacy && state() == NeedsAttention && !_blinkState)
126     return SystemTray::stateIcon(Active);
127   return SystemTray::stateIcon();
128 }
129
130 void LegacySystemTray::on_blinkTimeout() {
131   _blinkState = !_blinkState;
132   _trayIcon->setIcon(stateIcon());
133 }
134
135 void LegacySystemTray::on_activated(QSystemTrayIcon::ActivationReason reason) {
136   activate((SystemTray::ActivationReason)reason);
137 }
138
139 void LegacySystemTray::on_messageClicked() {
140   emit messageClicked(_lastMessageId);
141 }
142
143 void LegacySystemTray::showMessage(const QString &title, const QString &message, SystemTray::MessageIcon icon, int msTimeout, uint id) {
144   // fancy stuff later: show messages in order
145   // for now, we just show the last message
146   _lastMessageId = id;
147   _trayIcon->showMessage(title, message, (QSystemTrayIcon::MessageIcon)icon, msTimeout);
148 }
149
150 void LegacySystemTray::closeMessage(uint notificationId) {
151   Q_UNUSED(notificationId)
152
153   // there really seems to be no sane way to close the bubble... :(
154 #ifdef Q_WS_X11
155   showMessage("", "", NoIcon, 1);
156 #endif
157 }
158
159 #endif /* QT_NO_SYSTEMTRAYICON */