Integrate DesktopNotification into system tray/status notifier
[quassel.git] / src / qtui / mainwin.cpp
index d943bfa..fe050fb 100644 (file)
 #include "qtuistyle.h"
 #include "settingsdlg.h"
 #include "settingspagedlg.h"
+#include "statusnotifieritem.h"
 #include "toolbaractionprovider.h"
 #include "topicwidget.h"
 #include "verticaldock.h"
 
 #ifndef HAVE_KDE
-#  ifdef HAVE_DBUS
-#    include "desktopnotificationbackend.h"
-#  endif
 #  ifdef HAVE_PHONON
 #    include "phononnotificationbackend.h"
 #  endif
@@ -135,9 +133,7 @@ MainWin::MainWin(QWidget *parent)
     _awayLog(0),
     _layoutLoaded(false)
 {
-#ifdef Q_WS_WIN
-  dwTickCount = 0;
-#endif
+  setAttribute(Qt::WA_DeleteOnClose, false);  // we delete the mainwin manually
 
   QtUiSettings uiSettings;
   QString style = uiSettings.value("Style", QString()).toString();
@@ -200,9 +196,6 @@ void MainWin::init() {
 #  ifdef HAVE_PHONON
   QtUi::registerNotificationBackend(new PhononNotificationBackend(this));
 #  endif
-#  ifdef HAVE_DBUS
-  QtUi::registerNotificationBackend(new DesktopNotificationBackend(this));
-#  endif
 
 #else /* HAVE_KDE */
   QtUi::registerNotificationBackend(new KNotificationBackend(this));
@@ -279,8 +272,8 @@ void MainWin::restoreStateFromSettings(UiSettings &s) {
   move(_normalPos);
 #endif
 
-  if(s.value("MainWinHidden").toBool())
-    hideToTray();
+  if(s.value("MainWinHidden").toBool() && QtUi::haveSystemTray())
+    QtUi::hideMainWidget();
   else if(s.value("MainWinMinimized").toBool())
     showMinimized();
   else if(maximized)
@@ -680,7 +673,9 @@ void MainWin::saveStatusBarStatus(bool enabled) {
 }
 
 void MainWin::setupSystray() {
-#ifndef QT_NO_SYSTEMTRAYICON
+#ifdef HAVE_DBUS
+  _systemTray = new StatusNotifierItem(this);
+#elif !defined QT_NO_SYSTEMTRAYICON
   _systemTray = new LegacySystemTray(this);
 #else
   _systemTray = new SystemTray(this); // dummy
@@ -749,9 +744,7 @@ void MainWin::setConnectedState() {
 
   _coreConnectionStatusWidget->setVisible(!Client::internalCore());
   updateIcon();
-#ifndef QT_NO_SYSTEMTRAYICON
   systemTray()->setState(SystemTray::Active);
-#endif
 
   if(Client::networkIds().isEmpty()) {
     IrcConnectionWizard *wizard = new IrcConnectionWizard(this, Qt::Sheet);
@@ -776,8 +769,9 @@ void MainWin::loadLayout() {
 
 void MainWin::saveLayout() {
   QtUiSettings s;
-  int accountId = Client::currentCoreAccount().accountId().toInt();
-  if(accountId > 0) s.setValue(QString("MainWinState-%1").arg(accountId) , saveState(accountId));
+  int accountId = _bufferViews.count()? Client::currentCoreAccount().accountId().toInt() : 0; // only save if we still have a layout!
+  if(accountId > 0)
+    s.setValue(QString("MainWinState-%1").arg(accountId) , saveState(accountId));
 }
 
 void MainWin::disconnectedFromCore() {
@@ -1011,86 +1005,16 @@ void MainWin::resizeEvent(QResizeEvent *event) {
 }
 
 void MainWin::closeEvent(QCloseEvent *event) {
-#ifndef QT_NO_SYSTEMTRAYICON
   QtUiSettings s;
   QtUiApplication* app = qobject_cast<QtUiApplication*> qApp;
   Q_ASSERT(app);
-  if(!app->isAboutToQuit() && s.value("UseSystemTrayIcon").toBool() && s.value("MinimizeOnClose").toBool()) {
-    hideToTray();
+  if(!app->isAboutToQuit() && QtUi::haveSystemTray() && s.value("MinimizeOnClose").toBool()) {
+    QtUi::hideMainWidget();
     event->ignore();
   } else {
     event->accept();
     quit();
   }
-#else
-  event->accept();
-  quit();
-#endif
-}
-
-void MainWin::changeEvent(QEvent *event) {
-#ifdef Q_WS_WIN
-  if(event->type() == QEvent::ActivationChange)
-    dwTickCount = GetTickCount();  // needed for toggleMinimizedToTray()
-#endif
-
-  QMainWindow::changeEvent(event);
-}
-
-void MainWin::hideToTray() {
-  if(!systemTray()->isSystemTrayAvailable()) {
-    qWarning() << Q_FUNC_INFO << "was called with no SystemTray available!";
-    return;
-  }
-  hide();
-  systemTray()->setVisible();
-}
-
-void MainWin::toggleMinimizedToTray() {
-#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 :)
-  // courtesy: KSystemTrayIcon
-  if(GetTickCount() - dwTickCount >= 300)
-    // we weren't active in the last 300ms -> activate
-    forceActivated();
-  else
-    hideToTray();
-
-#else
-
-  if(!isVisible() || isMinimized())
-    // restore
-    forceActivated();
-  else
-    hideToTray();
-
-#endif
-}
-
-void MainWin::forceActivated() {
-#ifdef HAVE_KDE
-  show();
-  KWindowSystem::forceActiveWindow(winId());
-#else
-
-#ifdef Q_WS_X11
-  // Bypass focus stealing prevention
-  QX11Info::setAppUserTime(QX11Info::appTime());
-#endif
-
-  if(windowState() & Qt::WindowMinimized) {
-    // restore
-    setWindowState((windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
-  }
-
-  // this does not actually work on all platforms... and causes more evil than good
-  // move(frameGeometry().topLeft()); // avoid placement policies
-  show();
-  raise();
-  activateWindow();
-#endif /* HAVE_KDE */
 }
 
 void MainWin::messagesInserted(const QModelIndex &parent, int start, int end) {