Use SystemTray in MainWin and notification backends
authorManuel Nickschas <sputnick@quassel-irc.org>
Sat, 14 Feb 2009 21:41:20 +0000 (22:41 +0100)
committerManuel Nickschas <sputnick@quassel-irc.org>
Sat, 14 Feb 2009 21:41:33 +0000 (22:41 +0100)
src/qtui/knotificationbackend.cpp
src/qtui/mainwin.cpp
src/qtui/mainwin.h
src/qtui/systraynotificationbackend.cpp
src/qtui/systraynotificationbackend.h

index df6fe7e..64bdfdc 100644 (file)
@@ -30,6 +30,7 @@
 #include "iconloader.h"
 #include "networkmodel.h"
 #include "qtui.h"
+#include "systemtray.h"
 
 KNotificationBackend::KNotificationBackend(QObject *parent) : AbstractNotificationBackend(parent) {
 
@@ -41,10 +42,12 @@ void KNotificationBackend::notify(const Notification &n) {
   KNotification *notification = KNotification::event("Highlight", message, DesktopIcon("dialog-information"), QtUi::mainWindow(),
                                 KNotification::Persistent|KNotification::RaiseWidgetOnActivation|KNotification::CloseWhenWidgetActivated);
   connect(notification, SIGNAL(activated()), SLOT(notificationActivated()));
+  QtUi::mainWindow()->systemTray()->setAlert(true);
 }
 
 void KNotificationBackend::close(uint notificationId) {
   Q_UNUSED(notificationId);
+  QtUi::mainWindow()->systemTray()->setAlert(false);
 }
 
 void KNotificationBackend::notificationActivated() {
index e983d66..5a7b77f 100644 (file)
@@ -64,6 +64,7 @@
 #include "sessionsettings.h"
 #include "settingsdlg.h"
 #include "settingspagedlg.h"
+#include "systemtray.h"
 #include "toolbaractionprovider.h"
 #include "topicwidget.h"
 #include "verticaldock.h"
@@ -105,7 +106,6 @@ MainWin::MainWin(QWidget *parent)
     sslLabel(new QLabel()),
     msgProcessorStatusWidget(new MsgProcessorStatusWidget()),
     _titleSetter(this),
-    _trayIcon(new QSystemTrayIcon(this)),
     _awayLog(0)
 {
   QtUiSettings uiSettings;
@@ -137,6 +137,8 @@ void MainWin::init() {
   connect(QApplication::instance(), SIGNAL(aboutToQuit()), SLOT(saveLayout()));
   connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), SLOT(clientNetworkCreated(NetworkId)));
   connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), SLOT(clientNetworkRemoved(NetworkId)));
+  connect(Client::messageModel(), SIGNAL(rowsInserted(const QModelIndex &, int, int)),
+           SLOT(messagesInserted(const QModelIndex &, int, int)));
   connect(GraphicalUi::contextMenuActionProvider(), SIGNAL(showChannelList(NetworkId)), SLOT(showChannelList(NetworkId)));
 
   // Setup Dock Areas
@@ -182,6 +184,7 @@ void MainWin::init() {
   setDisconnectedState();  // Disable menus and stuff
 
   show();
+
   if(Quassel::runMode() != Quassel::Monolithic) {
     showCoreConnectionDlg(true); // autoconnect if appropriate
   } else {
@@ -196,6 +199,7 @@ MainWin::~MainWin() {
   s.setValue("MainWinState", saveState());
 }
 
+// FIXME
 void MainWin::updateIcon() {
   QPixmap icon;
   if(Client::isConnected())
@@ -204,7 +208,6 @@ void MainWin::updateIcon() {
     icon = DesktopIcon("quassel_disconnected", IconLoader::SizeEnormous);
   setWindowIcon(icon);
   qApp->setWindowIcon(icon);
-  systemTrayIcon()->setIcon(icon);
 }
 
 void MainWin::setupActions() {
@@ -507,27 +510,12 @@ void MainWin::saveStatusBarStatus(bool enabled) {
 }
 
 void MainWin::setupSystray() {
-  connect(Client::messageModel(), SIGNAL(rowsInserted(const QModelIndex &, int, int)),
-                                  SLOT(messagesInserted(const QModelIndex &, int, int)));
-
-  ActionCollection *coll = QtUi::actionCollection("General");
-  systrayMenu = new QMenu(this);
-  systrayMenu->addAction(coll->action("ConnectCore"));
-  systrayMenu->addAction(coll->action("DisconnectCore"));
-  systrayMenu->addAction(coll->action("CoreInfo"));
-  systrayMenu->addSeparator();
-  systrayMenu->addAction(coll->action("Quit"));
-
-  systemTrayIcon()->setContextMenu(systrayMenu);
-
-  QtUiSettings s;
-  if(s.value("UseSystemTrayIcon", QVariant(true)).toBool()) {
-    systemTrayIcon()->show();
-  }
+  _systemTray = new SystemTray(this);
 
 #ifndef Q_WS_MAC
-  connect(systemTrayIcon(), SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(systrayActivated(QSystemTrayIcon::ActivationReason)));
+  connect(systemTray(), SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(systrayActivated(QSystemTrayIcon::ActivationReason)));
 #endif
+
 }
 
 void MainWin::setupToolBars() {
@@ -612,6 +600,7 @@ void MainWin::setConnectedState() {
   sslLabel->setVisible(!Client::internalCore());
   coreLagLabel->setVisible(!Client::internalCore());
   updateIcon();
+  systemTray()->setState(SystemTray::Active);
 }
 
 void MainWin::loadLayout() {
@@ -671,6 +660,7 @@ void MainWin::setDisconnectedState() {
   if(msgProcessorStatusWidget)
     msgProcessorStatusWidget->setProgress(0, 0);
   updateIcon();
+  systemTray()->setState(SystemTray::Inactive);
 }
 
 void MainWin::startInternalCore() {
@@ -769,14 +759,13 @@ void MainWin::systrayActivated(QSystemTrayIcon::ActivationReason activationReaso
 }
 
 void MainWin::hideToTray() {
-  if(!systemTrayIcon()->isSystemTrayAvailable()) {
+  if(!systemTray()->isSystemTrayAvailable()) {
     qWarning() << Q_FUNC_INFO << "was called with no SystemTray available!";
     return;
   }
-
   clearFocus();
   hide();
-  systemTrayIcon()->show();
+  systemTray()->setIconVisible();
 }
 
 void MainWin::toggleMinimizedToTray() {
@@ -785,6 +774,7 @@ void MainWin::toggleMinimizedToTray() {
     setWindowState((windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
     show();
     raise();
+    
   } else {
     setWindowState((windowState() & ~Qt::WindowActive) | Qt::WindowMinimized);
     hideToTray();
index 4d16825..000e570 100644 (file)
@@ -42,7 +42,7 @@ class BufferWidget;
 class InputWidget;
 class MsgProcessorStatusWidget;
 class NickListWidget;
-class SystemTrayIcon;
+class SystemTray;
 
 class QMenu;
 class QLabel;
@@ -68,7 +68,7 @@ class MainWin
     void addBufferView(ClientBufferViewConfig *config);
     BufferView *allBuffersView() const;
 
-    inline QSystemTrayIcon *systemTrayIcon() const;
+    inline SystemTray *systemTray() const;
 
     virtual bool event(QEvent *event);
 
@@ -134,7 +134,6 @@ class MainWin
     KHelpMenu *_kHelpMenu;
 #endif
 
-    QMenu *systrayMenu;
     QLabel *coreLagLabel;
     QLabel *sslLabel;
     MsgProcessorStatusWidget *msgProcessorStatusWidget;
@@ -158,7 +157,7 @@ class MainWin
     void toggleMinimizedToTray();
     void enableMenus();
 
-    QSystemTrayIcon *_trayIcon;
+    SystemTray *_systemTray;
 
     QList<BufferViewDock *> _bufferViews;
     BufferWidget *_bufferWidget;
@@ -173,8 +172,8 @@ class MainWin
     friend class QtUi;
 };
 
-QSystemTrayIcon *MainWin::systemTrayIcon() const {
-  return _trayIcon;
+SystemTray *MainWin::systemTray() const {
+  return _systemTray;
 }
 
 #endif
index c97860c..3d9d74b 100644 (file)
@@ -28,6 +28,7 @@
 #include "mainwin.h"
 #include "networkmodel.h"
 #include "qtui.h"
+#include "systemtray.h"
 
 SystrayNotificationBackend::SystrayNotificationBackend(QObject *parent)
   : AbstractNotificationBackend(parent)
@@ -39,9 +40,7 @@ SystrayNotificationBackend::SystrayNotificationBackend(QObject *parent)
   notificationSettings.notify("Systray/ShowBubble", this, SLOT(showBubbleChanged(const QVariant &)));
   notificationSettings.notify("Systray/Animate", this, SLOT(animateChanged(const QVariant &)));
 
-  _iconActive = false;
-  connect(&_animationTimer, SIGNAL(timeout()), SLOT(blink()));
-  connect(QtUi::mainWindow()->systemTrayIcon(), SIGNAL(messageClicked()), this, SIGNAL(activated()));
+  connect(QtUi::mainWindow()->systemTray(), SIGNAL(messageClicked()), this, SIGNAL(activated()));
 }
 
 void SystrayNotificationBackend::notify(const Notification &notification) {
@@ -53,12 +52,11 @@ void SystrayNotificationBackend::notify(const Notification &notification) {
   */
   _notifications.clear();
   _notifications.append(notification);
-  if(_showBubble) {
+  if(_showBubble)
     showBubble();
-  }
-  if(_animate) {
-    startAnimation();
-  }
+
+  if(_animate)
+    QtUi::mainWindow()->systemTray()->setAlert(true);
 }
 
 void SystrayNotificationBackend::close(uint notificationId) {
@@ -70,7 +68,7 @@ void SystrayNotificationBackend::close(uint notificationId) {
   */
   _notifications.clear();
   closeBubble();
-  stopAnimation();
+  QtUi::mainWindow()->systemTray()->setAlert(false);
 }
 
 void SystrayNotificationBackend::showBubble() {
@@ -80,13 +78,13 @@ void SystrayNotificationBackend::showBubble() {
   Notification n = _notifications.takeLast();
   QString title = Client::networkModel()->networkName(n.bufferId) + " - " + Client::networkModel()->bufferName(n.bufferId);
   QString message = QString("<%1> %2").arg(n.sender, n.message);
-  QtUi::mainWindow()->systemTrayIcon()->showMessage(title, message);
+  QtUi::mainWindow()->systemTray()->showMessage(title, message);
 }
 
 void SystrayNotificationBackend::closeBubble() {
   // there really seems to be no sane way to close the bubble... :(
 #ifdef Q_WS_X11
-  QtUi::mainWindow()->systemTrayIcon()->showMessage("", "", QSystemTrayIcon::NoIcon, 1);
+  QtUi::mainWindow()->systemTray()->showMessage("", "", QSystemTrayIcon::NoIcon, 1);
 #endif
 }
 
@@ -94,22 +92,6 @@ void SystrayNotificationBackend::showBubbleChanged(const QVariant &v) {
   _showBubble = v.toBool();
 }
 
-void SystrayNotificationBackend::startAnimation() {
-  if(!_animationTimer.isActive())
-    _animationTimer.start(500);
-}
-
-void SystrayNotificationBackend::stopAnimation() {
-  _animationTimer.stop();
-  QtUi::mainWindow()->systemTrayIcon()->setIcon(Icon("quassel"));
-  _iconActive = false;
-}
-
-void SystrayNotificationBackend::blink() {
-  QtUi::mainWindow()->systemTrayIcon()->setIcon(_iconActive ? Icon("quassel") : Icon("quassel_newmessage"));
-  _iconActive = !_iconActive;
-}
-
 void SystrayNotificationBackend::animateChanged(const QVariant &v) {
   _animate = v.toBool();
 }
index 4ab8df0..2a7784b 100644 (file)
@@ -40,9 +40,6 @@ public:
 private slots:
   void showBubble();
   void closeBubble();
-  void startAnimation();
-  void stopAnimation();
-  void blink();
 
   void animateChanged(const QVariant &);
   void showBubbleChanged(const QVariant &);
@@ -52,8 +49,6 @@ private:
 
   bool _showBubble;
   bool _animate;
-  bool _iconActive;
-  QTimer _animationTimer;
   QList<Notification> _notifications;
 };