소스 검색

Added AStandardPaths, refactored ASettings

Both classes are static and encapsulate program-wide data such ass QStandardPaths as strings and a single QSettings instance to avoid creating and destroying it all the time.
George 4 년 전
부모
커밋
9534f722f6
7개의 변경된 파일109개의 추가작업 그리고 47개의 파일을 삭제
  1. 12 45
      main.cpp
  2. 2 0
      openPilotLog.pro
  3. 38 0
      src/astandardpaths.cpp
  4. 33 0
      src/astandardpaths.h
  5. 15 2
      src/classes/asettings.cpp
  6. 8 0
      src/classes/asettings.h
  7. 1 0
      src/database/adatabase.cpp

+ 12 - 45
main.cpp

@@ -20,45 +20,14 @@
 #include "src/classes/arunguard.h"
 #include "src/database/adatabase.h"
 #include "src/classes/asettings.h"
+#include "src/astandardpaths.h"
+#include "src/classes/asettings.h"
 #include <QApplication>
 #include <QProcess>
 #include <QSettings>
 #include <QFileInfo>
 #include <QStandardPaths>
 
-/// Utility struct for standard app paths. Struct should be created after QCoreApplication::set___Name.
-struct StandardAppPaths{
-    QMap<QStandardPaths::StandardLocation, QString> paths;
-    StandardAppPaths()
-        : paths({{QStandardPaths::AppConfigLocation, QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation)},
-                 {QStandardPaths::AppDataLocation, QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)},})
-    {}
-};
-
-/// Ensure standard app paths exist, if not mkdir them.
-static inline
-void scan_paths(StandardAppPaths paths)
-{
-    for(auto& path : paths.paths.values()){
-        auto dir = QDir(path);
-        DEB("Scanning " << dir.path());
-        if(!dir.exists())
-            DEB("Creating " << dir.path());
-            dir.mkdir(path);
-    }
-}
-
-/// Validate standard app paths are valid in structure and contents.
-static inline
-bool validate_paths(StandardAppPaths paths)
-{
-    for(auto& path : paths.paths.values()){
-        DEB("Validating " << path);
-        if(false)  // determine path as valid (scan contents and parse for correctness)
-            return false;
-    }
-    return true;
-}
 
 int main(int argc, char *argv[])
 {
@@ -67,27 +36,25 @@ int main(int argc, char *argv[])
     QCoreApplication::setOrganizationDomain("https://github.com/fiffty-50/openpilotlog");
     QCoreApplication::setApplicationName("openPilotLog");
 
-    StandardAppPaths std_paths;
-    scan_paths(std_paths);
-    if(!validate_paths(std_paths)){
+    AStandardPaths::setup();
+    AStandardPaths::scan_paths();
+    if(!AStandardPaths::validate_paths()){
         DEB("Standard paths not valid.");
         return 1;
     }
 
-    QSettings::setDefaultFormat(QSettings::IniFormat);
-    QSettings::setPath(QSettings::IniFormat, QSettings::UserScope, std_paths.paths[QStandardPaths::AppDataLocation]);
-    QSettings settings;
+    ASettings::setup();
 
     aDB()->connect();
 
-//    if (!ASettings::read("setup/setup_complete").toBool()) {
-//        FirstRunDialog dialog;
-//        dialog.exec();
-//    }
+    if (!ASettings::read("setup/setup_complete").toBool()) {
+        FirstRunDialog dialog;
+        dialog.exec();
+    }
 
     //Theming
-//    int selectedtheme = settings.value("main/theme").toInt();
-//    QDir::setCurrent("/themes");
+    int selectedtheme = ASettings::getSettings().value("main/theme").toInt();
+    QDir::setCurrent("/themes");
     switch (2) {
     case 1:{
         DEB "main :: Loading light theme";

+ 2 - 0
openPilotLog.pro

@@ -18,6 +18,7 @@ DEFINES += QT_DEPRECATED_WARNINGS
 SOURCES += \
     main.cpp \
     mainwindow.cpp \
+    src/astandardpaths.cpp \
     src/classes/aaircraftentry.cpp \
     src/classes/adownload.cpp \
     src/classes/aentry.cpp \
@@ -47,6 +48,7 @@ SOURCES += \
 
 HEADERS += \
     mainwindow.h \
+    src/astandardpaths.h \
     src/classes/aaircraftentry.h \
     src/classes/adownload.h \
     src/classes/aentry.h \

+ 38 - 0
src/astandardpaths.cpp

@@ -0,0 +1,38 @@
+#include "src/astandardpaths.h"
+
+QMap<QStandardPaths::StandardLocation, QString> AStandardPaths::paths;
+
+void AStandardPaths::setup()
+{
+     paths = {
+        {QStandardPaths::AppConfigLocation, QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation)},
+        {QStandardPaths::AppDataLocation, QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)},
+    };
+}
+
+QMap<QStandardPaths::StandardLocation, QString> AStandardPaths::getPaths()
+{
+    return paths;
+}
+
+void AStandardPaths::scan_paths()
+{
+    for(auto& path : paths.values()){
+        auto dir = QDir(path);
+        DEB "Scanning " << dir.path();
+        if(!dir.exists()) {
+            DEB"Creating " << dir.path();
+            dir.mkdir(path);
+        }
+    }
+}
+
+bool AStandardPaths::validate_paths()
+{
+    for(auto& path : paths.values()){
+        DEB "Validating " << path;
+        if(false)  // determine path as valid (scan contents and parse for correctness)
+            return false;
+    }
+    return true;
+}

+ 33 - 0
src/astandardpaths.h

@@ -0,0 +1,33 @@
+#ifndef ASTANDARDPATHS_H
+#define ASTANDARDPATHS_H
+
+#include "src/testing/adebug.h"
+#include <QStandardPaths>
+#include <QString>
+#include <QMap>
+#include <QDir>
+
+/*!
+ * \brief The AStandardAppPaths class encapsulates a static QMap holding
+ * the standard paths of the application. Setup should be called after
+ * `QCoreApplication::setWhateverName`.
+ */
+class AStandardPaths{
+private:
+    static
+    QMap<QStandardPaths::StandardLocation, QString> paths;
+public:
+    /// Initialise paths with corresponding StandardLocation paths
+    static void setup();
+
+    static QMap<QStandardPaths::StandardLocation, QString> getPaths();
+
+    /// Ensure standard app paths exist, if not mkdir them.
+    static void scan_paths();
+
+    /// Validate standard app paths are valid in structure and contents.
+    static bool validate_paths();
+};
+
+
+#endif // ASTANDARDPATHS_H

+ 15 - 2
src/classes/asettings.cpp

@@ -16,15 +16,28 @@
  *along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 #include "asettings.h"
+#include "src/astandardpaths.h"
+#include <QSettings>
+
+QSettings ASettings::settings;
+
+void ASettings::setup()
+{
+    settings.setDefaultFormat(QSettings::IniFormat);
+    settings.setPath(QSettings::IniFormat, QSettings::UserScope, AStandardPaths::getPaths()[QStandardPaths::AppDataLocation]);
+}
 
 QVariant ASettings::read(const QString &key)
 {
-    QSettings settings;
     return settings.value(key);
 }
 
 void ASettings::write(const QString &key, const QVariant &val)
 {
-    QSettings settings;
     settings.setValue(key, val);
 }
+
+QSettings& ASettings::getSettings()
+{
+    return settings;
+}

+ 8 - 0
src/classes/asettings.h

@@ -24,12 +24,20 @@
  * \brief The Settings class is a thin wrapper for the QSettings class,
  * simplifying reading and writing of settings.
  */
+
 class ASettings
 {
+private:
+    static QSettings settings;
+
 public:
+    static void setup();
+
     static QVariant read(const QString &key);
 
     static void write(const QString &key, const QVariant &val);
+
+    static QSettings& getSettings();
 };
 
 #endif // ASETTINGS_H

+ 1 - 0
src/database/adatabase.cpp

@@ -67,6 +67,7 @@ bool ADatabase::connect()
     if (!QSqlDatabase::isDriverAvailable(SQL_DRIVER))
         return false;
 
+
     QDir directory("data");
     QString databaseLocation = directory.filePath("logbook.db");
     QSqlDatabase db = QSqlDatabase::addDatabase(SQL_DRIVER);