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