mainwindow: save/load complete window geometry and state

- Replaces the use of position and size 2d vectors when saving qt
  application settings to the registry with geometry and state
  byte arrays which includes information about the window's screen
  number and fixes a bug which prevents the main window from ever
  starting correctly maximized due to the disregarded taskbar
  offset.

Change-Id: Iaa15f075970675e85ffcdb26da962a19a1914252
diff --git a/main.cpp b/main.cpp
index 019780c..1f25c8c 100644
--- a/main.cpp
+++ b/main.cpp
@@ -197,12 +197,12 @@
         delete sem;
     });
 #endif
-

-    auto ret = a.exec();

-

-    QCoreApplication::exit();

-    GlobalSystemTray::instance().deleteLater();

-    GlobalSystemTray::instance().hide();

-

+
+    auto ret = a.exec();
+
+    QCoreApplication::exit();
+    GlobalSystemTray::instance().deleteLater();
+    GlobalSystemTray::instance().hide();
+
     return ret;
 }
diff --git a/mainwindow.cpp b/mainwindow.cpp
index 531d9c3..ed7b5aa 100644
--- a/mainwindow.cpp
+++ b/mainwindow.cpp
@@ -83,17 +83,7 @@
     }
 #endif
 
-    QSettings settings;
-    QVariant size = settings.value(SettingsKey::savedSize);
-    QVariant posV = settings.value(SettingsKey::savedPos);
-    if (size.isValid() && posV.isValid()) {
-        resize(size.toSize());
-        auto screenSize = QApplication::desktop()->screenGeometry();
-        auto pos = posV.toPoint();
-        if (pos.rx() < screenSize.width() && pos.ry() < screenSize.height())
-            move(pos);
-    } else
-        resize(800, 600);
+    readSettingsFromRegistry();
 
     win_sparkle_set_appcast_url("http://dl.ring.cx/windows/winsparkle-ring.xml");
     win_sparkle_set_app_details(L"Savoir-faire Linux", L"Ring", QString(NIGHTLY_VERSION).toStdWString().c_str());
@@ -111,9 +101,6 @@
 
     setContextMenuPolicy(Qt::NoContextMenu);
 
-    if (not settings.contains(SettingsKey::enableNotifications)) {
-        settings.setValue(SettingsKey::enableNotifications, true);
-    }
     connect(&GlobalSystemTray::instance(), SIGNAL(messageClicked()), this, SLOT(notificationClicked()));
 
     connect(&netManager_, &QNetworkConfigurationManager::onlineStateChanged, [=](bool online) {
@@ -227,7 +214,19 @@
         this->hide();
         event->ignore();
     } else {
-        settings.setValue(SettingsKey::savedSize, size());
-        settings.setValue(SettingsKey::savedPos, pos());
+        settings.setValue(SettingsKey::geometry, saveGeometry());

+        settings.setValue(SettingsKey::windowState, saveState());
     }
+    QMainWindow::closeEvent(event);
 }
+
+void

+MainWindow::readSettingsFromRegistry()

+{

+    QSettings settings;

+    restoreGeometry(settings.value(SettingsKey::geometry).toByteArray());

+    restoreState(settings.value(SettingsKey::windowState).toByteArray());

+    if (not settings.contains(SettingsKey::enableNotifications)) {
+        settings.setValue(SettingsKey::enableNotifications, true);
+    }

+}
\ No newline at end of file
diff --git a/mainwindow.h b/mainwindow.h
index 54c19fa..25306ea 100644
--- a/mainwindow.h
+++ b/mainwindow.h
@@ -62,7 +62,10 @@
 
 private:
     explicit MainWindow(QWidget* parent = 0);
-     ~MainWindow();
+    ~MainWindow();
+
+    void readSettingsFromRegistry();
+    
     Ui::MainWindow* ui;
     QNetworkConfigurationManager netManager_;
 };
diff --git a/settingskey.h b/settingskey.h
index d09a91e..9fd4234 100644
--- a/settingskey.h
+++ b/settingskey.h
@@ -22,8 +22,8 @@
 
 constexpr static char closeOrMinimized[] = "closeOrMin";
 constexpr static char autoAnswer[] = "autoAnswer";
-constexpr static char savedSize[] = "savedSize";
-constexpr static char savedPos[] = "savedPos";
+constexpr static char geometry[] = "geometry";
+constexpr static char windowState[] = "windowState";
 constexpr static char imShowAuthor[] = "imShowAuthor";
 constexpr static char imShowDate[] = "imShowDate";
 constexpr static char enableNotifications[] = "enableNotifications";