Relicense SystemTray and LegacySystemTray under LGPL 2+
[quassel.git] / src / qtui / legacysystemtray.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2013 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This file is free software; you can redistribute it and/or modify     *
6  *   it under the terms of the GNU Library General Public License (LGPL)   *
7  *   as published by the Free Software Foundation; either version 2 of the *
8  *   License, or (at your option) any later version.                       *
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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 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
55 void LegacySystemTray::init()
56 {
57     if (mode() == Invalid) // derived class hasn't set a mode itself
58         setMode(Legacy);
59
60     SystemTray::init();
61
62     _trayIcon->setContextMenu(trayMenu());
63 }
64
65
66 void LegacySystemTray::syncLegacyIcon()
67 {
68     _trayIcon->setIcon(stateIcon());
69
70 #if defined Q_WS_MAC || defined Q_WS_WIN
71     QString tooltip = QString("%1").arg(toolTipTitle());
72     if (!toolTipSubTitle().isEmpty())
73         tooltip += QString("\n%1").arg(toolTipSubTitle());
74 #else
75     QString tooltip = QString("<b>%1</b>").arg(toolTipTitle());
76     if (!toolTipSubTitle().isEmpty())
77         tooltip += QString("<br>%1").arg(toolTipSubTitle());
78 #endif
79
80     _trayIcon->setToolTip(tooltip);
81 }
82
83
84 void LegacySystemTray::setVisible(bool visible)
85 {
86     SystemTray::setVisible(visible);
87     if (mode() == Legacy) {
88         if (shouldBeVisible())
89             _trayIcon->show();
90         else
91             _trayIcon->hide();
92     }
93 }
94
95
96 bool LegacySystemTray::isVisible() const
97 {
98     if (mode() == Legacy) {
99         return _trayIcon->isVisible();
100     }
101     return SystemTray::isVisible();
102 }
103
104
105 void LegacySystemTray::setMode(Mode mode_)
106 {
107     if (mode_ == mode())
108         return;
109
110     SystemTray::setMode(mode_);
111
112     if (mode() == Legacy) {
113         syncLegacyIcon();
114         if (shouldBeVisible())
115             _trayIcon->show();
116         else
117             _trayIcon->hide();
118         if (state() == NeedsAttention)
119             _blinkTimer.start();
120     }
121     else {
122         _trayIcon->hide();
123         _blinkTimer.stop();
124     }
125 }
126
127
128 void LegacySystemTray::setState(State state_)
129 {
130     State oldstate = state();
131     SystemTray::setState(state_);
132     if (oldstate != state()) {
133         if (state() == NeedsAttention && mode() == Legacy && animationEnabled())
134             _blinkTimer.start();
135         else {
136             _blinkTimer.stop();
137             _blinkState = false;
138         }
139     }
140     if (mode() == Legacy)
141         _trayIcon->setIcon(stateIcon());
142 }
143
144
145 Icon LegacySystemTray::stateIcon() const
146 {
147     if (mode() == Legacy && state() == NeedsAttention && !_blinkState)
148         return SystemTray::stateIcon(Active);
149     return SystemTray::stateIcon();
150 }
151
152
153 void LegacySystemTray::on_blinkTimeout()
154 {
155     _blinkState = !_blinkState;
156     _trayIcon->setIcon(stateIcon());
157 }
158
159
160 void LegacySystemTray::on_activated(QSystemTrayIcon::ActivationReason reason)
161 {
162     activate((SystemTray::ActivationReason)reason);
163 }
164
165
166 void LegacySystemTray::on_messageClicked()
167 {
168     emit messageClicked(_lastMessageId);
169 }
170
171
172 void LegacySystemTray::showMessage(const QString &title, const QString &message, SystemTray::MessageIcon icon, int msTimeout, uint id)
173 {
174     // fancy stuff later: show messages in order
175     // for now, we just show the last message
176     _lastMessageId = id;
177     _trayIcon->showMessage(title, message, (QSystemTrayIcon::MessageIcon)icon, msTimeout);
178 }
179
180
181 void LegacySystemTray::closeMessage(uint notificationId)
182 {
183     Q_UNUSED(notificationId)
184
185     // there really seems to be no sane way to close the bubble... :(
186 #ifdef Q_WS_X11
187     showMessage("", "", NoIcon, 1);
188 #endif
189 }
190
191
192 #endif /* QT_NO_SYSTEMTRAYICON */