Improve systray activation behavior; more refactoring
[quassel.git] / src / qtui / systemtray.cpp
index 140f9e2..27aeb08 100644 (file)
 /***************************************************************************
-*   Copyright (C) 2005-09 by the Quassel Project                          *
-*   devel@quassel-irc.org                                                 *
-*                                                                         *
-*   This program is free software; you can redistribute it and/or modify  *
-*   it under the terms of the GNU General Public License as published by  *
-*   the Free Software Foundation; either version 2 of the License, or     *
-*   (at your option) version 3.                                           *
-*                                                                         *
-*   This program is distributed in the hope that it will be useful,       *
-*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
-*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
-*   GNU General Public License for more details.                          *
-*                                                                         *
-*   You should have received a copy of the GNU General Public License     *
-*   along with this program; if not, write to the                         *
-*   Free Software Foundation, Inc.,                                       *
-*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
-***************************************************************************/
-
+ *   Copyright (C) 2005-2010 by the Quassel Project                        *
+ *   devel@quassel-irc.org                                                 *
+ *                                                                         *
+ *   This contains code from KStatusNotifierItem, part of the KDE libs     *
+ *   Copyright (C) 2009 Marco Martin <notmart@gmail.com>                   *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) version 3.                                           *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
+ ***************************************************************************/
 #include <QMenu>
 
 #include "systemtray.h"
 
 #include "actioncollection.h"
+#include "client.h"
 #include "iconloader.h"
 #include "qtui.h"
-#include "qtuisettings.h"
 
-SystemTray::SystemTray(QObject *parent)
+#ifdef HAVE_KDE
+#  include <KWindowInfo>
+#  include <KWindowSystem>
+#endif
+
+SystemTray::SystemTray(QWidget *parent)
 : QObject(parent),
-  _state(Inactive),
-  _alert(false),
+  _mode(Invalid),
+  _state(Passive),
   _inhibitActivation(false),
-  _currentIdx(0)
+  _passiveIcon(DesktopIcon("quassel_inactive")),
+  _activeIcon(DesktopIcon("quassel")),
+  _needsAttentionIcon(DesktopIcon("quassel_message")),
+  _trayMenu(0),
+  _associatedWidget(parent)
 {
-  loadAnimations();
-  _currentIdx = _idxOffEnd;
+  Q_ASSERT(parent);
 
-#ifndef HAVE_KDE
-  _trayIcon = new QSystemTrayIcon(_phases.at(_currentIdx), QtUi::mainWindow());
-#else
-  _trayIcon = new KSystemTrayIcon(_phases.at(_currentIdx), QtUi::mainWindow());
-  // 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)));
+#ifdef Q_WS_WIN
+  _dwTickCount = 0;
+  associatedWidget()->installEventFilter(this);
+#endif
+
+  qApp->installEventFilter(this);
+}
+
+SystemTray::~SystemTray() {
+#ifdef Q_WS_WIN
+  associatedWidget()->removeEventFilter(this);
 #endif
 
-  _animationTimer.setInterval(150);
-  _animationTimer.setSingleShot(false);
-  connect(&_animationTimer, SIGNAL(timeout()), SLOT(nextPhase()));
+  _trayMenu->deleteLater();
+}
+
+QWidget *SystemTray::associatedWidget() const {
+  return _associatedWidget;
+}
+
+void SystemTray::setTrayMenu(QMenu *menu) {
+  if(menu)
+    _trayMenu = menu;
+  else
+    _trayMenu = new QMenu();
 
   ActionCollection *coll = QtUi::actionCollection("General");
-  _trayMenu = new QMenu();
+
   _trayMenu->addAction(coll->action("ConnectCore"));
   _trayMenu->addAction(coll->action("DisconnectCore"));
   _trayMenu->addAction(coll->action("CoreInfo"));
+#ifndef HAVE_KDE
   _trayMenu->addSeparator();
   _trayMenu->addAction(coll->action("Quit"));
+#endif /* HAVE_KDE */
+}
 
-  _trayIcon->setContextMenu(_trayMenu);
-
-  QtUiSettings s;
-  if(s.value("UseSystemTrayIcon", QVariant(true)).toBool()) {
-    _trayIcon->show();
+void SystemTray::setMode(Mode mode_) {
+  if(mode_ != _mode) {
+    _mode = mode_;
+    if(_mode == Legacy) {
+      _trayMenu->setWindowFlags(Qt::Popup);
+    } else {
+      _trayMenu->setWindowFlags(Qt::Window);
+    }
   }
-
-  qApp->installEventFilter(this);
-
-#ifndef Q_WS_MAC
-  connect(_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), SLOT(on_activated(QSystemTrayIcon::ActivationReason)));
-#endif
-  connect(_trayIcon, SIGNAL(messageClicked()), SIGNAL(messageClicked()));
 }
 
-SystemTray::~SystemTray() {
-  _trayMenu->deleteLater();
+Icon SystemTray::stateIcon() const {
+  return stateIcon(state());
 }
 
-void SystemTray::loadAnimations() {
-// system tray icon size
-#ifdef Q_WS_WIN
-  const int size = 16;
-#elif defined Q_WS_MAC
-  const int size = 128;
-#else
-  const int size = 22;
-#endif
-
-  _phases.clear();
-
-#ifdef HAVE_KDE
-  KIconLoader *loader = KIconLoader::global();
-#else
-  IconLoader *loader = IconLoader::global();
-#endif
-
-  _idxOffStart = 0;
-  QString fadeOffName("quassel_tray-fade-off-%1");
-  for(int i = 2; i <= 10; i++)
-    _phases.append(loader->loadIcon(fadeOffName.arg(i), IconLoader::Panel, size));
-  _idxOffEnd = _idxOnStart = _phases.count() - 1;
-
-  QString fadeOnName("quassel_tray-fade-on-%1");
-  for(int i = 2; i <= 15; i++)
-    _phases.append(loader->loadIcon(fadeOnName.arg(i), IconLoader::Panel, size));
-  _idxOnEnd = _idxAlertStart = _phases.count() - 1;
-
-  QString alertName("quassel_tray-alert-%1");
-  for(int i = 1; i <= 10; i++)
-    _phases.append(loader->loadIcon(alertName.arg(i), IconLoader::Panel, size));
-}
-
-void SystemTray::nextPhase() {
-  if(_currentIdx == _idxOnEnd && !_alert && _state == Inactive)
-    _currentIdx = _idxOffStart; // skip alert phases
-
-  else if(++_currentIdx >= _phases.count()) {
-    if(_alert)
-      _currentIdx = _idxAlertStart;
-    else
-      if(_state == Active)
-        _currentIdx = _idxOnEnd;
-      else
-        _currentIdx = _idxOffStart;
+Icon SystemTray::stateIcon(State state) const {
+  switch(state) {
+  case Passive:
+    return _passiveIcon;
+  case Active:
+    return _activeIcon;
+  case NeedsAttention:
+    return _needsAttentionIcon;
   }
-
-  _trayIcon->setIcon(_phases.at(_currentIdx));
-
-  if(_alert)
-    return;
-
-  if((_state == Active && _currentIdx == _idxOnEnd) || (_state == Inactive && _currentIdx == _idxOffEnd))
-    _animationTimer.stop();
+  return Icon();
 }
 
 void SystemTray::setState(State state) {
   if(_state != state) {
     _state = state;
-    if(state == Inactive && _alert)
-      _alert = false;
-    if(!_animationTimer.isActive())
-      _animationTimer.start();
   }
 }
 
-void SystemTray::setAlert(bool alert) {
-  if(_alert != alert) {
-    _alert = alert;
-    if(!_animationTimer.isActive())
-      _animationTimer.start();
-  }
+void SystemTray::setAlert(bool alerted) {
+  if(alerted)
+    setState(NeedsAttention);
+  else
+    setState(Client::isConnected() ? Active : Passive);
 }
 
-void SystemTray::setIconVisible(bool visible) {
-  if(visible)
-    _trayIcon->show();
-  else
-    _trayIcon->hide();
+void SystemTray::setVisible(bool visible) {
+  Q_UNUSED(visible)
+}
+
+void SystemTray::setToolTip(const QString &title, const QString &subtitle) {
+  _toolTipTitle = title;
+  _toolTipSubTitle = subtitle;
+  emit toolTipChanged(title, subtitle);
 }
 
-void SystemTray::setToolTip(const QString &tip) {
-  _trayIcon->setToolTip(tip);
+void SystemTray::showMessage(const QString &title, const QString &message, MessageIcon icon, int millisecondsTimeoutHint) {
+  Q_UNUSED(title)
+  Q_UNUSED(message)
+  Q_UNUSED(icon)
+  Q_UNUSED(millisecondsTimeoutHint)
 }
 
-void SystemTray::showMessage(const QString &title, const QString &message, QSystemTrayIcon::MessageIcon icon, int millisecondsTimeoutHint) {
-  _trayIcon->showMessage(title, message, icon, millisecondsTimeoutHint);
+void SystemTray::activate(SystemTray::ActivationReason reason) {
+
+  emit activated(reason);
+
+  if(reason == Trigger && !isActivationInhibited()) {
+    toggleMainWidget();
+  }
 }
 
 bool SystemTray::eventFilter(QObject *obj, QEvent *event) {
-  Q_UNUSED(obj);
-  if(event->type() == QEvent::MouseButtonRelease) {
+  if(event->type() == QEvent::MouseMove || event->type() == QEvent::MouseButtonRelease) {
     _inhibitActivation = false;
   }
-  return false;
+#ifdef Q_WS_WIN
+  if(obj == associatedWidget() && event->type() == QEvent::ActivationChange) {
+    _dwTickCount = GetTickCount();
+  }
+#endif
+  return QObject::eventFilter(obj, event);
 }
 
-void SystemTray::on_activated(QSystemTrayIcon::ActivationReason reason) {
-  emit activated(reason);
+// Code taken from KStatusNotifierItem for handling minimize/restore
 
-  if(reason == QSystemTrayIcon::Trigger && !_inhibitActivation) {
+bool SystemTray::checkVisibility(bool perform) {
+#ifdef Q_WS_WIN
+  // the problem is that we lose focus when the systray icon is activated
+  // and we don't know the former active window
+  // therefore we watch for activation event and use our stopwatch :)
+  if(GetTickCount() - _dwTickCount < 300) {
+    // we were active in the last 300ms -> hide it
+    minimizeRestore(false);
+  } else {
+    minimizeRestore(true);
+  }
 
-#  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
+#elif defined(HAVE_KDE) && defined(Q_WS_X11)
+  KWindowInfo info1 = KWindowSystem::windowInfo(associatedWidget()->winId(), NET::XAWMState | NET::WMState | NET::WMDesktop);
+  // mapped = visible (but possibly obscured)
+  bool mapped = (info1.mappingState() == NET::Visible) && !info1.isMinimized();
+
+  //    - not mapped -> show, raise, focus
+  //    - mapped
+  //        - obscured -> raise, focus
+  //        - not obscured -> hide
+  //info1.mappingState() != NET::Visible -> window on another desktop?
+  if(!mapped) {
+    if(perform)
+      minimizeRestore(true);
+    return true;
+
+  } else {
+    QListIterator< WId > it (KWindowSystem::stackingOrder());
+    it.toBack();
+    while(it.hasPrevious()) {
+      WId id = it.previous();
+      if(id == associatedWidget()->winId())
+        break;
+
+      KWindowInfo info2 = KWindowSystem::windowInfo(id, NET::WMDesktop | NET::WMGeometry | NET::XAWMState | NET::WMState | NET::WMWindowType);
+
+      if(info2.mappingState() != NET::Visible)
+        continue; // not visible on current desktop -> ignore
+
+      if(!info2.geometry().intersects(associatedWidget()->geometry()))
+        continue; // not obscuring the window -> ignore
+
+      if(!info1.hasState(NET::KeepAbove) && info2.hasState(NET::KeepAbove))
+        continue; // obscured by window kept above -> ignore
+
+      NET::WindowType type = info2.windowType(NET::NormalMask | NET::DesktopMask
+                                              | NET::DockMask | NET::ToolbarMask | NET::MenuMask | NET::DialogMask
+                                              | NET::OverrideMask | NET::TopMenuMask | NET::UtilityMask | NET::SplashMask);
+
+      if(type == NET::Dock || type == NET::TopMenu)
+        continue; // obscured by dock or topmenu -> ignore
+
+      if(perform) {
+        KWindowSystem::raiseWindow(associatedWidget()->winId());
+        KWindowSystem::activateWindow(associatedWidget()->winId());
+      }
+      return true;
+    }
+
+    //not on current desktop?
+    if(!info1.isOnCurrentDesktop()) {
+      if(perform)
+        KWindowSystem::activateWindow(associatedWidget()->winId());
+      return true;
+    }
+
+    if(perform)
+      minimizeRestore(false); // hide
+    return false;
+  }
+#else
 
+  if(!associatedWidget()->isVisible() || associatedWidget()->isMinimized()) {
+    if(perform)
+      minimizeRestore(true);
+    return true;
+  } else {
+    if(perform)
+      minimizeRestore(false);
+    return false;
   }
+
+#endif
+
+  return true;
+}
+
+void SystemTray::minimizeRestore(bool show) {
+  if(show)
+    GraphicalUi::activateMainWidget();
+  else {
+    if(isSystemTrayAvailable()) {
+      if(!isVisible())
+        setVisible();
+      GraphicalUi::hideMainWidget();
+    }
+  }
+}
+
+void SystemTray::hideMainWidget() {
+  minimizeRestore(false);
+}
+
+void SystemTray::toggleMainWidget() {
+  checkVisibility(true);
 }