Systray icon improvements
[quassel.git] / src / qtui / mainwin.cpp
index 6609295..6867d06 100644 (file)
 #  include <KStatusBar>
 #endif
 
+#ifdef Q_WS_X11
+#  include <QX11Info>
+#endif
+
 #include "aboutdlg.h"
 #include "awaylogfilter.h"
 #include "awaylogview.h"
@@ -55,6 +59,7 @@
 #include "inputwidget.h"
 #include "inputline.h"
 #include "irclistmodel.h"
+#include "ircconnectionwizard.h"
 #include "jumpkeyhandler.h"
 #include "msgprocessorstatuswidget.h"
 #include "nicklistwidget.h"
@@ -64,6 +69,7 @@
 #include "sessionsettings.h"
 #include "settingsdlg.h"
 #include "settingspagedlg.h"
+#include "systemtray.h"
 #include "toolbaractionprovider.h"
 #include "topicwidget.h"
 #include "verticaldock.h"
@@ -87,7 +93,6 @@
 #include "settingspages/bufferviewsettingspage.h"
 #include "settingspages/chatmonitorsettingspage.h"
 #include "settingspages/colorsettingspage.h"
-#include "settingspages/fontssettingspage.h"
 #include "settingspages/generalsettingspage.h"
 #include "settingspages/highlightsettingspage.h"
 #include "settingspages/identitiessettingspage.h"
@@ -105,9 +110,12 @@ MainWin::MainWin(QWidget *parent)
     sslLabel(new QLabel()),
     msgProcessorStatusWidget(new MsgProcessorStatusWidget()),
     _titleSetter(this),
-    _trayIcon(new QSystemTrayIcon(this)),
     _awayLog(0)
 {
+#ifdef Q_WS_WIN
+  dwTickCount = 0;
+#endif
+
   QtUiSettings uiSettings;
   QString style = uiSettings.value("Style", QString()).toString();
   if(!style.isEmpty()) {
@@ -137,6 +145,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 +192,7 @@ void MainWin::init() {
   setDisconnectedState();  // Disable menus and stuff
 
   show();
+
   if(Quassel::runMode() != Quassel::Monolithic) {
     showCoreConnectionDlg(true); // autoconnect if appropriate
   } else {
@@ -197,14 +208,19 @@ MainWin::~MainWin() {
 }
 
 void MainWin::updateIcon() {
+#ifdef Q_WS_MAC
+  const int size = 128;
+#else
+  const int size = 48;
+#endif
+
   QPixmap icon;
   if(Client::isConnected())
-    icon = DesktopIcon("quassel", IconLoader::SizeEnormous);
+    icon = DesktopIcon("quassel", size);
   else
-    icon = DesktopIcon("quassel_disconnected", IconLoader::SizeEnormous);
+    icon = DesktopIcon("quassel_inactive", size);
   setWindowIcon(icon);
   qApp->setWindowIcon(icon);
-  systemTrayIcon()->setIcon(icon);
 }
 
 void MainWin::setupActions() {
@@ -489,7 +505,6 @@ void MainWin::setupStatusBar() {
   statusBar()->addPermanentWidget(sslLabel);
   sslLabel->hide();
 
-  _viewMenu->addSeparator();
   QAction *showStatusbar = QtUi::actionCollection("General")->action("ToggleStatusBar");
 
   QtUiSettings uiSettings;
@@ -508,27 +523,7 @@ 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();
-  }
-
-#ifndef Q_WS_MAC
-  connect(systemTrayIcon(), SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(systrayActivated(QSystemTrayIcon::ActivationReason)));
-#endif
+  _systemTray = new SystemTray(this);
 }
 
 void MainWin::setupToolBars() {
@@ -537,8 +532,12 @@ void MainWin::setupToolBars() {
   connect(_nickListWidget, SIGNAL(nickSelectionChanged(QModelIndexList)),
           QtUi::toolBarActionProvider(), SLOT(nickSelectionChanged(QModelIndexList)));
 
+#ifdef Q_WS_MAC
+  setUnifiedTitleAndToolBarOnMac(true);
+#endif
   _mainToolBar = addToolBar("Main Toolbar");
   _mainToolBar->setObjectName("MainToolBar");
+
   QtUi::toolBarActionProvider()->addActions(_mainToolBar, ToolBarActionProvider::MainToolBar);
   _toolbarMenu->addAction(_mainToolBar->toggleViewAction());
 
@@ -552,18 +551,6 @@ void MainWin::setupToolBars() {
 #endif
 }
 
-void MainWin::changeEvent(QEvent *event) {
-  if(event->type() == QEvent::WindowStateChange) {
-    if(windowState() & Qt::WindowMinimized) {
-      QtUiSettings s;
-      if(s.value("UseSystemTrayIcon").toBool() && s.value("MinimizeOnMinimize").toBool()) {
-       hideToTray();
-       event->accept();
-      }
-    }
-  }
-}
-
 void MainWin::connectedToCore() {
   Q_CHECK_PTR(Client::bufferViewManager());
   connect(Client::bufferViewManager(), SIGNAL(bufferViewConfigAdded(int)), this, SLOT(addBufferView(int)));
@@ -609,6 +596,12 @@ void MainWin::setConnectedState() {
   sslLabel->setVisible(!Client::internalCore());
   coreLagLabel->setVisible(!Client::internalCore());
   updateIcon();
+  systemTray()->setState(SystemTray::Active);
+
+  if(Client::networkIds().isEmpty()) {
+    IrcConnectionWizard *wizard = new IrcConnectionWizard(this, Qt::Sheet);
+    wizard->show();
+  }
 }
 
 void MainWin::loadLayout() {
@@ -668,6 +661,7 @@ void MainWin::setDisconnectedState() {
   if(msgProcessorStatusWidget)
     msgProcessorStatusWidget->setProgress(0, 0);
   updateIcon();
+  systemTray()->setState(SystemTray::Inactive);
 }
 
 void MainWin::startInternalCore() {
@@ -718,20 +712,19 @@ void MainWin::showSettingsDlg() {
   SettingsDlg *dlg = new SettingsDlg();
 
   //Category: Appearance
-  dlg->registerSettingsPage(new ColorSettingsPage(dlg));
-  dlg->registerSettingsPage(new FontsSettingsPage(dlg));
   dlg->registerSettingsPage(new AppearanceSettingsPage(dlg)); //General
-  //Category: Behaviour
-  dlg->registerSettingsPage(new GeneralSettingsPage(dlg));
-  dlg->registerSettingsPage(new BacklogSettingsPage(dlg));
+  dlg->registerSettingsPage(new ColorSettingsPage(dlg));
   dlg->registerSettingsPage(new HighlightSettingsPage(dlg));
-  dlg->registerSettingsPage(new AliasesSettingsPage(dlg));
   dlg->registerSettingsPage(new NotificationsSettingsPage(dlg));
+  dlg->registerSettingsPage(new BacklogSettingsPage(dlg));
+  dlg->registerSettingsPage(new BufferViewSettingsPage(dlg));
   dlg->registerSettingsPage(new ChatMonitorSettingsPage(dlg));
-  //Category: General
+
+  //Category: Misc
+  dlg->registerSettingsPage(new GeneralSettingsPage(dlg));
   dlg->registerSettingsPage(new IdentitiesSettingsPage(dlg));
   dlg->registerSettingsPage(new NetworksSettingsPage(dlg));
-  dlg->registerSettingsPage(new BufferViewSettingsPage(dlg));
+  dlg->registerSettingsPage(new AliasesSettingsPage(dlg));
 
   dlg->show();
 }
@@ -759,33 +752,79 @@ void MainWin::closeEvent(QCloseEvent *event) {
   }
 }
 
-void MainWin::systrayActivated(QSystemTrayIcon::ActivationReason activationReason) {
-  if(activationReason == QSystemTrayIcon::Trigger) {
-    toggleMinimizedToTray();
+bool MainWin::event(QEvent *event) {
+  if(event->type() == QEvent::WindowActivate)
+    QtUi::closeNotifications();
+  return QMainWindow::event(event);
+}
+
+void MainWin::changeEvent(QEvent *event) {
+  if(event->type() == QEvent::WindowStateChange) {
+    if(windowState() & Qt::WindowMinimized) {
+      QtUiSettings s;
+      if(s.value("UseSystemTrayIcon").toBool() && s.value("MinimizeOnMinimize").toBool()) {
+        hideToTray();
+        event->accept();
+        return;
+      }
+    }
   }
+
+#ifdef Q_WS_WIN
+  else if(event->type() == QEvent::ActivationChange)
+    dwTickCount = GetTickCount();  // needed for toggleMinimizedToTray()
+#endif
+
+  event->ignore();
 }
 
 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() {
+#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 Q_WS_X11
+  // Bypass focus stealing prevention
+  QX11Info::setAppUserTime(QX11Info::appTime());
+#endif
+
   if(windowState() & Qt::WindowMinimized) {
     // restore
     setWindowState((windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
-    show();
-    raise();
-  } else {
-    setWindowState((windowState() & ~Qt::WindowActive) | Qt::WindowMinimized);
-    hideToTray();
   }
+
+  move(frameGeometry().topLeft()); // avoid placement policies
+  show();
+  raise();
+  activateWindow();
 }
 
 void MainWin::messagesInserted(const QModelIndex &parent, int start, int end) {
@@ -817,12 +856,6 @@ void MainWin::messagesInserted(const QModelIndex &parent, int start, int end) {
   }
 }
 
-bool MainWin::event(QEvent *event) {
-  if(event->type() == QEvent::WindowActivate)
-    QtUi::closeNotifications();
-  return QMainWindow::event(event);
-}
-
 void MainWin::clientNetworkCreated(NetworkId id) {
   const Network *net = Client::network(id);
   QAction *act = new QAction(net->networkName(), this);