qtui: Refactor the system tray implementations
[quassel.git] / src / qtui / systemtray.cpp
index 642f794..4a68dd6 100644 (file)
@@ -1,11 +1,11 @@
 /***************************************************************************
- *   Copyright (C) 2005-2012 by the Quassel Project                        *
+ *   Copyright (C) 2005-2018 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 file is free software; you can redistribute it and/or modify     *
+ *   it under the terms of the GNU Library General Public License (LGPL)   *
+ *   as published by the Free Software Foundation; either version 2 of the *
+ *   License, or (at your option) any later version.                       *
  *                                                                         *
  *   This program is distributed in the hope that it will be useful,       *
  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
@@ -15,8 +15,9 @@
  *   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.             *
+ *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
  ***************************************************************************/
+
 #include <QApplication>
 #include <QMenu>
 
 #include "action.h"
 #include "actioncollection.h"
 #include "client.h"
-#include "iconloader.h"
 #include "qtui.h"
 
-#ifdef HAVE_KDE
+#ifdef HAVE_KDE4
 #  include <KMenu>
 #  include <KWindowInfo>
 #  include <KWindowSystem>
 
 SystemTray::SystemTray(QWidget *parent)
     : QObject(parent),
-    _mode(Invalid),
-    _state(Passive),
-    _shouldBeVisible(true),
-    _passiveIcon(DesktopIcon("quassel-inactive")),
-    _activeIcon(DesktopIcon("quassel")),
-    _needsAttentionIcon(DesktopIcon("quassel-message")),
-    _trayMenu(0),
     _associatedWidget(parent)
 {
     Q_ASSERT(parent);
-}
-
-
-SystemTray::~SystemTray()
-{
-    _trayMenu->deleteLater();
-}
-
-
-QWidget *SystemTray::associatedWidget() const
-{
-    return _associatedWidget;
-}
 
+    NotificationSettings{}.initAndNotify("Systray/Animate", this, SLOT(enableAnimationChanged(QVariant)), true);
+    UiStyleSettings{}.initAndNotify("Icons/InvertTray", this, SLOT(invertTrayIconChanged(QVariant)), false);
 
-void SystemTray::init()
-{
     ActionCollection *coll = QtUi::actionCollection("General");
     _minimizeRestoreAction = new Action(tr("&Minimize"), this, this, SLOT(minimizeRestore()));
 
-#ifdef HAVE_KDE
+#ifdef HAVE_KDE4
     KMenu *kmenu;
     _trayMenu = kmenu = new KMenu();
     kmenu->addTitle(_activeIcon, "Quassel IRC");
@@ -76,7 +56,7 @@ void SystemTray::init()
 
     _trayMenu->setTitle("Quassel IRC");
 
-#ifndef HAVE_KDE
+#ifndef HAVE_KDE4
     _trayMenu->setAttribute(Qt::WA_Hover);
 #endif
 
@@ -88,28 +68,55 @@ void SystemTray::init()
     _trayMenu->addAction(coll->action("Quit"));
 
     connect(_trayMenu, SIGNAL(aboutToShow()), SLOT(trayMenuAboutToShow()));
+}
+
 
-    NotificationSettings notificationSettings;
-    notificationSettings.initAndNotify("Systray/Animate", this, SLOT(enableAnimationChanged(QVariant)), true);
+SystemTray::~SystemTray()
+{
+    _trayMenu->deleteLater();
 }
 
 
-void SystemTray::trayMenuAboutToShow()
+QWidget *SystemTray::associatedWidget() const
 {
-    if (GraphicalUi::isMainWidgetVisible())
-        _minimizeRestoreAction->setText(tr("&Minimize"));
-    else
-        _minimizeRestoreAction->setText(tr("&Restore"));
+    return _associatedWidget;
+}
+
+
+bool SystemTray::isSystemTrayAvailable() const
+{
+    return false;
+}
+
+
+bool SystemTray::isVisible() const
+{
+    return _isVisible;
+}
+
+
+void SystemTray::setVisible(bool visible)
+{
+    if (visible != _isVisible) {
+        _isVisible = visible;
+        emit visibilityChanged(visible);
+    }
 }
 
 
-void SystemTray::setMode(Mode mode_)
+SystemTray::Mode SystemTray::mode() const
 {
-    if (mode_ != _mode) {
-        _mode = mode_;
-#ifdef HAVE_KDE
+    return _mode;
+}
+
+
+void SystemTray::setMode(Mode mode)
+{
+    if (mode != _mode) {
+        _mode = mode;
+#ifdef HAVE_KDE4
         if (_trayMenu) {
-            if (_mode == Legacy) {
+            if (mode == Mode::Legacy) {
                 _trayMenu->setWindowFlags(Qt::Popup);
             }
             else {
@@ -117,35 +124,52 @@ void SystemTray::setMode(Mode mode_)
             }
         }
 #endif
+        emit modeChanged(mode);
     }
 }
 
 
-Icon SystemTray::stateIcon() const
+SystemTray::State SystemTray::state() const
 {
-    return stateIcon(state());
+    return _state;
 }
 
 
-Icon SystemTray::stateIcon(State state) const
+void SystemTray::setState(State state)
 {
-    switch (state) {
-    case Passive:
-        return _passiveIcon;
-    case Active:
-        return _activeIcon;
-    case NeedsAttention:
-        return _needsAttentionIcon;
+    if (_state != state) {
+        _state = state;
+        emit stateChanged(state);
     }
-    return Icon();
 }
 
 
-void SystemTray::setState(State state)
+QString SystemTray::iconName(State state) const
 {
-    if (_state != state) {
-        _state = state;
+    QString name;
+    switch (state) {
+    case State::Passive:
+        name = "inactive-quassel-tray";
+        break;
+    case State::Active:
+        name = "active-quassel-tray";
+        break;
+    case State::NeedsAttention:
+        name = "message-quassel-tray";
+        break;
+    }
+
+    if (_trayIconInverted) {
+        name += "-inverted";
     }
+
+    return name;
+}
+
+
+bool SystemTray::isAlerted() const
+{
+    return state() == State::NeedsAttention;
 }
 
 
@@ -158,9 +182,49 @@ void SystemTray::setAlert(bool alerted)
 }
 
 
-void SystemTray::setVisible(bool visible)
+QMenu *SystemTray::trayMenu() const
+{
+    return _trayMenu;
+}
+
+
+void SystemTray::trayMenuAboutToShow()
+{
+    if (GraphicalUi::isMainWidgetVisible())
+        _minimizeRestoreAction->setText(tr("&Minimize"));
+    else
+        _minimizeRestoreAction->setText(tr("&Restore"));
+}
+
+
+bool SystemTray::animationEnabled() const
+{
+    return _animationEnabled;
+}
+
+
+void SystemTray::enableAnimationChanged(const QVariant &v)
+{
+    _animationEnabled = v.toBool();
+    emit animationEnabledChanged(v.toBool());
+}
+
+
+void SystemTray::invertTrayIconChanged(const QVariant &v)
+{
+    _trayIconInverted = v.toBool();
+}
+
+
+QString SystemTray::toolTipTitle() const
+{
+    return _toolTipTitle;
+}
+
+
+QString SystemTray::toolTipSubTitle() const
 {
-    _shouldBeVisible = visible;
+    return _toolTipSubTitle;
 }
 
 
@@ -182,20 +246,19 @@ void SystemTray::showMessage(const QString &title, const QString &message, Messa
 }
 
 
-void SystemTray::activate(SystemTray::ActivationReason reason)
+void SystemTray::closeMessage(uint notificationId)
 {
-    emit activated(reason);
+    Q_UNUSED(notificationId)
 }
 
 
-void SystemTray::minimizeRestore()
+void SystemTray::activate(SystemTray::ActivationReason reason)
 {
-    GraphicalUi::toggleMainWidget();
+    emit activated(reason);
 }
 
 
-void SystemTray::enableAnimationChanged(const QVariant &v)
+void SystemTray::minimizeRestore()
 {
-    _animationEnabled = v.toBool();
-    emit animationEnabledChanged(v.toBool());
+    GraphicalUi::toggleMainWidget();
 }