Make disabling the tray icon animation work again
[quassel.git] / src / qtui / legacysystemtray.cpp
index b57fd99..552324c 100644 (file)
 #ifndef QT_NO_SYSTEMTRAYICON
 
 #include "legacysystemtray.h"
+#include "mainwin.h"
 #include "qtui.h"
 
-LegacySystemTray::LegacySystemTray(QObject *parent)
+LegacySystemTray::LegacySystemTray(QWidget *parent)
   : SystemTray(parent),
   _blinkState(false),
-  _isVisible(true)
+  _isVisible(true),
+  _lastMessageId(0)
 {
 #ifndef HAVE_KDE
-  _trayIcon = new QSystemTrayIcon(QtUi::mainWindow());
+  _trayIcon = new QSystemTrayIcon(associatedWidget());
 #else
-  _trayIcon = new KSystemTrayIcon(QtUi::mainWindow());
+  _trayIcon = new KSystemTrayIcon(associatedWidget());
   // We don't want to trigger a minimize if a highlight is pending, so we brutally remove the internal connection for that
   disconnect(_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
                  _trayIcon, SLOT(activateOrHide(QSystemTrayIcon::ActivationReason)));
@@ -41,24 +43,31 @@ LegacySystemTray::LegacySystemTray(QObject *parent)
                      SLOT(on_activated(QSystemTrayIcon::ActivationReason)));
 #endif
   connect(_trayIcon, SIGNAL(messageClicked()),
-                     SIGNAL(messageClicked()));
-
-  setTrayMenu(_trayIcon->contextMenu());
-  _trayIcon->setContextMenu(trayMenu());
+                     SLOT(on_messageClicked()));
 
   _blinkTimer.setInterval(500);
   _blinkTimer.setSingleShot(false);
   connect(&_blinkTimer, SIGNAL(timeout()), SLOT(on_blinkTimeout()));
+
+  connect(this, SIGNAL(toolTipChanged(QString,QString)), SLOT(syncLegacyIcon()));
 }
 
 void LegacySystemTray::init() {
   if(mode() == Invalid) // derived class hasn't set a mode itself
     setMode(Legacy);
+
+  SystemTray::init();
+
+  _trayIcon->setContextMenu(trayMenu());
 }
 
 void LegacySystemTray::syncLegacyIcon() {
   _trayIcon->setIcon(stateIcon());
-  _trayIcon->setToolTip(toolTipTitle());
+
+  QString tooltip = QString("<b>%1</b>").arg(toolTipTitle());
+  if(!toolTipSubTitle().isEmpty())
+    tooltip += QString("<br>%1").arg(toolTipSubTitle());
+  _trayIcon->setToolTip(tooltip);
 }
 
 void LegacySystemTray::setVisible(bool visible) {
@@ -71,6 +80,13 @@ void LegacySystemTray::setVisible(bool visible) {
   }
 }
 
+bool LegacySystemTray::isVisible() const {
+  if(mode() == Legacy) {
+    return _trayIcon->isVisible();
+  }
+  return false;
+}
+
 void LegacySystemTray::setMode(Mode mode_) {
   SystemTray::setMode(mode_);
 
@@ -90,7 +106,7 @@ void LegacySystemTray::setState(State state_) {
   State oldstate = state();
   SystemTray::setState(state_);
   if(oldstate != state()) {
-    if(state() == NeedsAttention && mode() == Legacy)
+    if(state() == NeedsAttention && mode() == Legacy && animationEnabled())
       _blinkTimer.start();
     else {
       _blinkTimer.stop();
@@ -113,22 +129,27 @@ void LegacySystemTray::on_blinkTimeout() {
 }
 
 void LegacySystemTray::on_activated(QSystemTrayIcon::ActivationReason reason) {
-  emit activated((ActivationReason)reason);
-
-  if(reason == QSystemTrayIcon::Trigger && !isActivationInhibited()) {
+  activate((SystemTray::ActivationReason)reason);
+}
 
-#  ifdef HAVE_KDE
-     // the slot is private, but meh, who cares :)
-     QMetaObject::invokeMethod(_trayIcon, "activateOrHide", Q_ARG(QSystemTrayIcon::ActivationReason, QSystemTrayIcon::Trigger));
-#  else
-     QtUi::mainWindow()->toggleMinimizedToTray();
-#  endif
+void LegacySystemTray::on_messageClicked() {
+  emit messageClicked(_lastMessageId);
+}
 
-  }
+void LegacySystemTray::showMessage(const QString &title, const QString &message, SystemTray::MessageIcon icon, int msTimeout, uint id) {
+  // fancy stuff later: show messages in order
+  // for now, we just show the last message
+  _lastMessageId = id;
+  _trayIcon->showMessage(title, message, (QSystemTrayIcon::MessageIcon)icon, msTimeout);
 }
 
-void LegacySystemTray::showMessage(const QString &title, const QString &message, SystemTray::MessageIcon icon, int millisecondsTimeoutHint) {
-  _trayIcon->showMessage(title, message, (QSystemTrayIcon::MessageIcon)icon, millisecondsTimeoutHint);
+void LegacySystemTray::closeMessage(uint notificationId) {
+  Q_UNUSED(notificationId)
+
+  // there really seems to be no sane way to close the bubble... :(
+#ifdef Q_WS_X11
+  showMessage("", "", NoIcon, 1);
+#endif
 }
 
 #endif /* QT_NO_SYSTEMTRAYICON */