qtui: Fix quit sequence and lifetime issues
[quassel.git] / src / qtui / qtui.cpp
index a37bc40..a6c48c3 100644 (file)
 #include "types.h"
 #include "util.h"
 
-QtUi *QtUi::_instance = nullptr;
-MainWin *QtUi::_mainWin = nullptr;
 QList<AbstractNotificationBackend *> QtUi::_notificationBackends;
 QList<AbstractNotificationBackend::Notification> QtUi::_notifications;
 
+
+QtUi *QtUi::instance()
+{
+    return static_cast<QtUi*>(GraphicalUi::instance());
+}
+
+
 QtUi::QtUi()
     : GraphicalUi()
     , _systemIconTheme{QIcon::themeName()}
 {
-    if (_instance != nullptr) {
-        qWarning() << "QtUi has been instantiated again!";
-        return;
-    }
-    _instance = this;
+    QtUiSettings uiSettings;
+    Quassel::loadTranslation(uiSettings.value("Locale", QLocale::system()).value<QLocale>());
 
     if (Quassel::isOptionSet("icontheme")) {
         _systemIconTheme = Quassel::optionValue("icontheme");
         QIcon::setThemeName(_systemIconTheme);
     }
-
-    QtUiSettings uiSettings;
-    Quassel::loadTranslation(uiSettings.value("Locale", QLocale::system()).value<QLocale>());
-
     setupIconTheme();
-
     QApplication::setWindowIcon(icon::get("quassel"));
 
-    setContextMenuActionProvider(new ContextMenuActionProvider(this));
-    setToolBarActionProvider(new ToolBarActionProvider(this));
-
     setUiStyle(new QtUiStyle(this));
-    _mainWin = new MainWin();
-
-    setMainWidget(_mainWin);
-
-    connect(_mainWin, SIGNAL(connectToCore(const QVariantMap &)), this, SIGNAL(connectToCore(const QVariantMap &)));
-    connect(_mainWin, SIGNAL(disconnectFromCore()), this, SIGNAL(disconnectFromCore()));
-    connect(Client::instance(), SIGNAL(bufferMarkedAsRead(BufferId)), SLOT(closeNotifications(BufferId)));
 }
 
 
 QtUi::~QtUi()
 {
     unregisterAllNotificationBackends();
-    delete _mainWin;
-    _mainWin = nullptr;
-    _instance = nullptr;
 }
 
 
 void QtUi::init()
 {
+    setContextMenuActionProvider(new ContextMenuActionProvider(this));
+    setToolBarActionProvider(new ToolBarActionProvider(this));
+
+    _mainWin.reset(new MainWin());  // TODO C++14: std::make_unique
+    setMainWidget(_mainWin.get());
+
+    connect(_mainWin.get(), SIGNAL(connectToCore(const QVariantMap &)), this, SIGNAL(connectToCore(const QVariantMap &)));
+    connect(_mainWin.get(), SIGNAL(disconnectFromCore()), this, SIGNAL(disconnectFromCore()));
+    connect(Client::instance(), SIGNAL(bufferMarkedAsRead(BufferId)), SLOT(closeNotifications(BufferId)));
+
     _mainWin->init();
+
     QtUiSettings uiSettings;
     uiSettings.initAndNotify("UseSystemTrayIcon", this, SLOT(useSystemTrayChanged(QVariant)), true);
 
@@ -345,7 +340,7 @@ void QtUi::refreshIconTheme()
         }
     }
 
-    if (_systemIconTheme.isEmpty() || _systemIconTheme == fallbackTheme || s.value("Icons/OverrideSystemTheme", false).toBool()) {
+    if (_systemIconTheme.isEmpty() || _systemIconTheme == fallbackTheme || s.value("Icons/OverrideSystemTheme", true).toBool()) {
         // We have a valid fallback theme and want to override the system theme (if it's even defined), so we're basically done
         QIcon::setThemeName(fallbackTheme);
         emit iconThemeRefreshed();