Fix compiler warnings
[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   _lastMessageId(0)
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                      SLOT(on_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 #if defined Q_WS_MAC || defined Q_WS_WIN
67   QString tooltip = QString("%1").arg(toolTipTitle());
68   if(!toolTipSubTitle().isEmpty())
69     tooltip += QString("\n%1").arg(toolTipSubTitle());
70 #else
71   QString tooltip = QString("<b>%1</b>").arg(toolTipTitle());
72   if(!toolTipSubTitle().isEmpty())
73     tooltip += QString("<br>%1").arg(toolTipSubTitle());
74 #endif
75
76   _trayIcon->setToolTip(tooltip);
77 }
78
79 void LegacySystemTray::setVisible(bool visible) {
80   SystemTray::setVisible(visible);
81   if(mode() == Legacy) {
82     if(shouldBeVisible())
83       _trayIcon->show();
84     else
85       _trayIcon->hide();
86   }
87 }
88
89 bool LegacySystemTray::isVisible() const {
90   if(mode() == Legacy) {
91     return _trayIcon->isVisible();
92   }
93   return SystemTray::isVisible();
94 }
95
96 void LegacySystemTray::setMode(Mode mode_) {
97   if(mode_ == mode())
98     return;
99
100   SystemTray::setMode(mode_);
101
102   if(mode() == Legacy) {
103     syncLegacyIcon();
104     if(shouldBeVisible())
105       _trayIcon->show();
106     else
107       _trayIcon->hide();
108     if(state() == NeedsAttention)
109       _blinkTimer.start();
110   } else {
111     _trayIcon->hide();
112     _blinkTimer.stop();
113   }
114 }
115
116 void LegacySystemTray::setState(State state_) {
117   State oldstate = state();
118   SystemTray::setState(state_);
119   if(oldstate != state()) {
120     if(state() == NeedsAttention && mode() == Legacy && animationEnabled())
121       _blinkTimer.start();
122     else {
123       _blinkTimer.stop();
124       _blinkState = false;
125     }
126   }
127   if(mode() == Legacy)
128     _trayIcon->setIcon(stateIcon());
129 }
130
131 Icon LegacySystemTray::stateIcon() const {
132   if(mode() == Legacy && state() == NeedsAttention && !_blinkState)
133     return SystemTray::stateIcon(Active);
134   return SystemTray::stateIcon();
135 }
136
137 void LegacySystemTray::on_blinkTimeout() {
138   _blinkState = !_blinkState;
139   _trayIcon->setIcon(stateIcon());
140 }
141
142 void LegacySystemTray::on_activated(QSystemTrayIcon::ActivationReason reason) {
143   activate((SystemTray::ActivationReason)reason);
144 }
145
146 void LegacySystemTray::on_messageClicked() {
147   emit messageClicked(_lastMessageId);
148 }
149
150 void LegacySystemTray::showMessage(const QString &title, const QString &message, SystemTray::MessageIcon icon, int msTimeout, uint id) {
151   // fancy stuff later: show messages in order
152   // for now, we just show the last message
153   _lastMessageId = id;
154   _trayIcon->showMessage(title, message, (QSystemTrayIcon::MessageIcon)icon, msTimeout);
155 }
156
157 void LegacySystemTray::closeMessage(uint notificationId) {
158   Q_UNUSED(notificationId)
159
160   // there really seems to be no sane way to close the bubble... :(
161 #ifdef Q_WS_X11
162   showMessage("", "", NoIcon, 1);
163 #endif
164 }
165
166 #endif /* QT_NO_SYSTEMTRAYICON */