From f098a08aafdb6086b1c6c918c77d000845ebe052 Mon Sep 17 00:00:00 2001
From: Matthias Puchner <github@mpuchner.de>
Date: Fri, 27 Aug 2021 11:28:56 +0200
Subject: [PATCH] refactor

---
 GUI/Application/Application.cpp | 11 +++++++++++
 GUI/Application/Application.h   |  7 +++++++
 GUI/main/main.cpp               | 14 ++++----------
 3 files changed, 22 insertions(+), 10 deletions(-)

diff --git a/GUI/Application/Application.cpp b/GUI/Application/Application.cpp
index 641e3bf8188..f2c99148008 100644
--- a/GUI/Application/Application.cpp
+++ b/GUI/Application/Application.cpp
@@ -17,6 +17,7 @@
 #include "GUI/utils/hostosinfo.h"
 #include <QFile>
 #include <QIcon>
+#include <QStandardPaths>
 
 Application::Application(int& argc, char** argv)
     : QApplication(argc, argv), m_currentStyle(ApplicationSettings::Style::native)
@@ -100,3 +101,13 @@ InstrumentLibrary& Application::instrumentLibrary()
 {
     return m_instrumentLibrary;
 }
+
+QString Application::instrumentLibraryFilePath() const
+{
+    return appDataFolder() + "/BornAgainInstrumentLibrary.balib";
+}
+
+QString Application::appDataFolder() const
+{
+    return QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
+}
diff --git a/GUI/Application/Application.h b/GUI/Application/Application.h
index 940bb7237ba..88a9dc864d0 100644
--- a/GUI/Application/Application.h
+++ b/GUI/Application/Application.h
@@ -40,6 +40,13 @@ public:
 
     InstrumentLibrary& instrumentLibrary();
 
+    QString instrumentLibraryFilePath() const;
+
+    //! The folder where persistent application data shall be stored. E.g. in Windows this is the
+    //! AppData/Roaming location.
+    //! Used e.g. for storing the instrument library.
+    QString appDataFolder() const;
+
 private:
     ApplicationSettings m_settings;
     InstrumentLibrary m_instrumentLibrary;
diff --git a/GUI/main/main.cpp b/GUI/main/main.cpp
index 50fca7a3ab9..65e261079bf 100644
--- a/GUI/main/main.cpp
+++ b/GUI/main/main.cpp
@@ -20,7 +20,6 @@
 #include <QDir>
 #include <QLocale>
 #include <QMetaType>
-#include <QStandardPaths>
 
 void messageHandler(QtMsgType, const QMessageLogContext&, const QString&) {}
 
@@ -44,15 +43,10 @@ int main(int argc, char* argv[])
 
     app.loadStyle(app.settings().styleToUse());
 
-    const QString folderForInstrumentLibrary =
-        QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
-    const QString filePathForInstrumentLibrary =
-        folderForInstrumentLibrary + "/BornAgainInstrumentLibrary.balib";
+    if (!QDir().exists(app.appDataFolder()))
+        QDir().mkpath(app.appDataFolder());
 
-    if (!QDir().exists(folderForInstrumentLibrary))
-        QDir().mkpath(folderForInstrumentLibrary);
-
-    app.instrumentLibrary().load(filePathForInstrumentLibrary);
+    app.instrumentLibrary().load(app.instrumentLibraryFilePath());
 
     MainWindow win;
     if (options.find("geometry"))
@@ -62,7 +56,7 @@ int main(int argc, char* argv[])
 
     int ret = QApplication::exec();
 
-    app.instrumentLibrary().saveIfModified(filePathForInstrumentLibrary);
+    app.instrumentLibrary().saveIfModified(app.instrumentLibraryFilePath());
 
     return ret;
 }
-- 
GitLab