Make Quassel work with Qt 4.5 again
[quassel.git] / src / qtui / mainwin.cpp
index 32b59ab..b35cad8 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2005-09 by the Quassel Project                          *
+ *   Copyright (C) 2005-2010 by the Quassel Project                        *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
@@ -70,6 +70,7 @@
 #include "irclistmodel.h"
 #include "ircconnectionwizard.h"
 #include "jumpkeyhandler.h"
+#include "legacysystemtray.h"
 #include "msgprocessorstatuswidget.h"
 #include "nicklistwidget.h"
 #include "qtuiapplication.h"
 #include "qtuistyle.h"
 #include "settingsdlg.h"
 #include "settingspagedlg.h"
-#include "systemtray.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();
@@ -193,17 +189,15 @@ void MainWin::init() {
   setupHotList();
 
 #ifndef HAVE_KDE
-  QtUi::registerNotificationBackend(new TaskbarNotificationBackend(this));
-#  ifndef QT_NO_SYSTEMTRAYICON
-  QtUi::registerNotificationBackend(new SystrayNotificationBackend(this));
-#  endif
 #  ifdef HAVE_PHONON
   QtUi::registerNotificationBackend(new PhononNotificationBackend(this));
 #  endif
-#  ifdef HAVE_DBUS
-  QtUi::registerNotificationBackend(new DesktopNotificationBackend(this));
+#  ifndef QT_NO_SYSTEMTRAYICON
+  QtUi::registerNotificationBackend(new SystrayNotificationBackend(this));
 #  endif
 
+  QtUi::registerNotificationBackend(new TaskbarNotificationBackend(this));
+
 #else /* HAVE_KDE */
   QtUi::registerNotificationBackend(new KNotificationBackend(this));
 #endif /* HAVE_KDE */
@@ -253,6 +247,9 @@ void MainWin::saveStateToSettings(UiSettings &s) {
   s.setValue("MainWinMinimized", isMinimized());
   s.setValue("MainWinMaximized", isMaximized());
   s.setValue("MainWinHidden", !isVisible());
+  BufferId lastBufId = Client::bufferModel()->currentBuffer();
+  if(lastBufId.isValid())
+    s.setValue("LastUsedBufferId", lastBufId.toInt());
 
 #ifdef HAVE_KDE
   saveAutoSaveSettings();
@@ -279,13 +276,9 @@ void MainWin::restoreStateFromSettings(UiSettings &s) {
   move(_normalPos);
 #endif
 
-#ifndef QT_NO_SYSTEMTRAYICON
-  if(s.value("MainWinHidden").toBool()) {
-    hideToTray();
-    return;
-  }
-#endif
-  if(s.value("MainWinMinimized").toBool())
+  if(s.value("MainWinHidden").toBool() && QtUi::haveSystemTray())
+    QtUi::hideMainWidget();
+  else if(s.value("MainWinMinimized").toBool())
     showMinimized();
   else if(maximized)
     showMaximized();
@@ -320,8 +313,9 @@ void MainWin::setupActions() {
                                           this, SLOT(showCoreInfoDlg())));
   coll->addAction("ConfigureNetworks", new Action(SmallIcon("configure"), tr("Configure &Networks..."), coll,
                                               this, SLOT(on_actionConfigureNetworks_triggered())));
+  // FIXME: use QKeySequence::Quit once we depend on Qt 4.6
   coll->addAction("Quit", new Action(SmallIcon("application-exit"), tr("&Quit"), coll,
-                                      this, SLOT(quit()), tr("Ctrl+Q")));
+                                     this, SLOT(quit()), Qt::CTRL + Qt::Key_Q));
 
   // View
   coll->addAction("ConfigureBufferViews", new Action(tr("&Configure Chat Lists..."), coll,
@@ -336,14 +330,14 @@ void MainWin::setupActions() {
   coll->addAction("ShowAwayLog", new Action(tr("Show Away Log"), coll,
                                             this, SLOT(showAwayLog())));
   coll->addAction("ToggleMenuBar", new Action(SmallIcon("show-menu"), tr("Show &Menubar"), coll,
-                                                0, 0, tr("Ctrl+M")))->setCheckable(true);
+                                                0, 0, QKeySequence(Qt::CTRL + Qt::Key_M)))->setCheckable(true);
 
   coll->addAction("ToggleStatusBar", new Action(tr("Show Status &Bar"), coll,
                                                 0, 0))->setCheckable(true);
 
   // Settings
   coll->addAction("ConfigureQuassel", new Action(SmallIcon("configure"), tr("&Configure Quassel..."), coll,
-                                                  this, SLOT(showSettingsDlg()), tr("F7")));
+                                                  this, SLOT(showSettingsDlg()), QKeySequence(Qt::Key_F7)));
 
   // Help
   coll->addAction("AboutQuassel", new Action(SmallIcon("quassel"), tr("&About Quassel"), coll,
@@ -620,17 +614,19 @@ void MainWin::setupInputWidget() {
   _bufferWidget->setFocusProxy(_inputWidget);
 
   _inputWidget->inputLine()->installEventFilter(_bufferWidget);
+
+  connect(_topicWidget, SIGNAL(switchedPlain()), _bufferWidget, SLOT(setFocus()));
 }
 
 void MainWin::setupTopicWidget() {
   VerticalDock *dock = new VerticalDock(tr("Topic"), this);
   dock->setObjectName("TopicDock");
-  TopicWidget *topicwidget = new TopicWidget(dock);
+  _topicWidget = new TopicWidget(dock);
 
-  dock->setWidget(topicwidget);
+  dock->setWidget(_topicWidget);
 
-  topicwidget->setModel(Client::bufferModel());
-  topicwidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
+  _topicWidget->setModel(Client::bufferModel());
+  _topicWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
 
   addDockWidget(Qt::TopDockWidgetArea, dock, Qt::Vertical);
 
@@ -682,9 +678,14 @@ void MainWin::saveStatusBarStatus(bool enabled) {
 }
 
 void MainWin::setupSystray() {
-#ifndef QT_NO_SYSTEMTRAYICON
-  _systemTray = new SystemTray(this);
+#ifdef HAVE_DBUS
+  _systemTray = new StatusNotifierItem(this);
+#elif !defined QT_NO_SYSTEMTRAYICON
+  _systemTray = new LegacySystemTray(this);
+#else
+  _systemTray = new SystemTray(this); // dummy
 #endif
+  _systemTray->init();
 }
 
 void MainWin::setupToolBars() {
@@ -748,14 +749,18 @@ 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);
     wizard->show();
   }
+  else {
+    QtUiSettings s;
+    BufferId lastUsedBufferId(s.value("LastUsedBufferId").toInt());
+    if(lastUsedBufferId.isValid())
+      Client::bufferModel()->switchToBuffer(lastUsedBufferId);
+  }
 }
 
 void MainWin::loadLayout() {
@@ -763,9 +768,9 @@ void MainWin::loadLayout() {
   int accountId = Client::currentCoreAccount().accountId().toInt();
   QByteArray state = s.value(QString("MainWinState-%1").arg(accountId)).toByteArray();
   if(state.isEmpty()) {
-    // Make sure that the default bufferview is shown
-    if(_bufferViews.count())
-      _bufferViews.at(0)->show();
+    foreach(BufferViewDock *view, _bufferViews)
+      view->show();
+    _layoutLoaded = true;
     return;
   }
 
@@ -775,8 +780,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() {
@@ -816,9 +822,7 @@ void MainWin::setDisconnectedState() {
   if(_msgProcessorStatusWidget)
     _msgProcessorStatusWidget->setProgress(0, 0);
   updateIcon();
-#ifndef QT_NO_SYSTEMTRAYICON
-  systemTray()->setState(SystemTray::Inactive);
-#endif
+  systemTray()->setState(SystemTray::Passive);
 }
 
 void MainWin::userAuthenticationRequired(CoreAccount *account, bool *valid, const QString &errorMessage) {
@@ -1012,90 +1016,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);
-}
-
-#ifndef QT_NO_SYSTEMTRAYICON
-
-void MainWin::hideToTray() {
-  if(!systemTray()->isSystemTrayAvailable()) {
-    qWarning() << Q_FUNC_INFO << "was called with no SystemTray available!";
-    return;
-  }
-  hide();
-  systemTray()->setIconVisible();
-}
-
-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
-}
-
-#endif /* QT_NO_SYSTEMTRAYICON */
-
-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) {
@@ -1260,4 +1190,3 @@ void MainWin::on_actionDebugLog_triggered() {
 void MainWin::showStatusBarMessage(const QString &message) {
   statusBar()->showMessage(message, 10000);
 }
-