Browse Source

Merge remote-tracking branch 'origin/develop-removing-obsolete-code' into develop-removing-obsolete-code

Felix 4 years ago
parent
commit
cc2e753abf
6 changed files with 118 additions and 43 deletions
  1. 23 41
      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. 7 0
      src/classes/asettings.h

+ 23 - 41
main.cpp

@@ -20,62 +20,45 @@
 #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>
 
-
-const auto DATA_DIR = QLatin1String("data");
-/*!
- * \brief setup checks if data folder and settings files exists.
- * \return
- */
-bool setup()
-{
-    if (!QDir(DATA_DIR).exists())
-        QDir().mkdir(DATA_DIR);
-
-    QDir      settingspath(DATA_DIR + QLatin1Char('/') + QCoreApplication::organizationName());
-    QString   settingsfile = QCoreApplication::applicationName() + QLatin1String(".ini");
-    QFileInfo check_file(settingspath,settingsfile);
-
-    QSettings settings;
-    settings.setValue("setup/touch", true);
-    settings.sync();
-
-    return check_file.exists() && check_file.isFile();
-};
+#define APPNAME "openPilotLog"
+#define ORGNAME APPNAME
+#define ORGDOMAIN "https://github.com/fiffty-50/openpilotlog"
 
 int main(int argc, char *argv[])
 {
-    QCoreApplication::setOrganizationName("openPilotLog");
-    QCoreApplication::setOrganizationDomain("https://github.com/fiffty-50/openpilotlog");
-    QCoreApplication::setApplicationName("openPilotLog");
-
-    QSettings::setPath(QSettings::IniFormat, QSettings::UserScope, DATA_DIR);
-    QSettings::setDefaultFormat(QSettings::IniFormat);
-    QSettings settings;
-
-    aDB()->connect();
-
     QApplication openPilotLog(argc, argv);
-    if(!setup()){
-        DEB "error creating required directories";
-        return 0;
+    QCoreApplication::setOrganizationName(ORGNAME);
+    QCoreApplication::setOrganizationDomain(ORGDOMAIN);
+    QCoreApplication::setApplicationName(APPNAME);
+
+    AStandardPaths::setup();
+    AStandardPaths::scan_paths();
+    if(!AStandardPaths::validate_paths()){
+        DEB("Standard paths not valid.");
+        return 1;
     }
 
-    if (!ASettings::read("setup/setup_complete").toBool()) {
-        FirstRunDialog dialog;
-        dialog.exec();
-    }
+    ASettings::setup();
 
+    aDB()->connect();
 
+//    if (!ASettings::read("setup/setup_complete").toBool()) {
+//        FirstRunDialog dialog;
+//        dialog.exec();
+//    }
 
     //Theming
-    int selectedtheme = settings.value("main/theme").toInt();
+    int selectedtheme = ASettings::getSettings().value("main/theme").toInt();
     QDir::setCurrent("/themes");
-    switch (selectedtheme) {
+    switch (2) {
     case 1:{
         DEB "main :: Loading light theme";
         QFile file(":light.qss");
@@ -96,7 +79,6 @@ int main(int argc, char *argv[])
         break;
     }
 
-
     //sqlite does not deal well with multiple connections, ensure only one instance is running
     ARunGuard guard("opl_single_key");
         if ( !guard.tryToRun() ){

+ 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;
+}

+ 7 - 0
src/classes/asettings.h

@@ -26,10 +26,17 @@
  */
 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