Browse Source

Added Standard Dirs

Felix Turo 2 years ago
parent
commit
e1a44d60f6
2 changed files with 21 additions and 34 deletions
  1. 6 30
      src/classes/paths.cpp
  2. 15 4
      src/classes/paths.h

+ 6 - 30
src/classes/paths.cpp

@@ -3,32 +3,9 @@
 
 namespace OPL {
 
-Paths::Paths()
-{
-
-}
-
-const bool Paths::setup()
-{
-
-// Define the application paths. This will be standard locations on all platforms eventually
-// but for now on Windows and MacOS use the application runtime directory to make it easier to
-// debug and develop. On Linux XDG standard directories are required for the flatpak to work.
-
-#ifdef linux
-    LOG << "Setting up directories at: " << QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
-    const QString dir_path = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
-    for(const auto& str : qAsConst(directories)){
-        QDir dir(dir_path + str);
-        if(!dir.exists()) {
-            if (!dir.mkpath(dir.absolutePath()))
-                return false;
-        }
-    }
-    return true;
-#else
-    LOG << "Setting up directories at: " << QCoreApplication::applicationDirPath();
-    const QString dir_path = QCoreApplication::applicationDirPath();
+const bool Paths::setup() {
+    LOG << "Setting up directories at: " << basePath;
+    const QString dir_path = basePath;
     for(const auto& str : qAsConst(directories)){
         QDir dir(dir_path + str);
         if(!dir.exists()) {
@@ -37,22 +14,21 @@ const bool Paths::setup()
         }
     }
     return true;
-#endif
 }
 
 const QDir Paths::directory(Directories location)
 {
-    return QDir(QCoreApplication::applicationDirPath() + directories[location]);
+    return QDir(basePath + directories[location]);
 }
 
 const QString Paths::path(Directories location)
 {
-    return QDir::toNativeSeparators(QCoreApplication::applicationDirPath() + directories[location]);
+    return QDir::toNativeSeparators(basePath + directories[location]);
 }
 
 const QString Paths::filePath(Directories location, const QString &filename)
 {
-    QDir dir(QCoreApplication::applicationDirPath() + directories[location]);
+    QDir dir(basePath + directories[location]);
     return dir.absoluteFilePath(filename);
 }
 

+ 15 - 4
src/classes/paths.h

@@ -4,6 +4,8 @@
 #include <QCoreApplication>
 #include <QDir>
 #include <QHash>
+#include <QStandardPaths>
+#include "src/opl.h"
 
 
 namespace OPL {
@@ -12,6 +14,8 @@ namespace OPL {
 class Paths
 {
 public:
+    Paths() = delete;
+
     enum Directories {
         Database,
         Templates,
@@ -30,8 +34,6 @@ public:
         {Settings, QLatin1String("/settings")},
     };
 
-    Paths();
-
     static const bool setup();
 
     /*!
@@ -42,14 +44,23 @@ public:
 
     static const QString path(Directories location);
 
-    static const QString appDir() {return QCoreApplication::applicationDirPath();}
-
     static const QString filePath(Directories location, const QString &filename);
 
     /*!
      * \brief returns a QFileInfo for the default database file.
      */
     static const QFileInfo databaseFileInfo();
+private:
+    // Define the paths where the application data will be written. This will be standard locations on all platforms eventually
+    // but for now on Windows and MacOS, we use the application runtime directory to make it easier to
+    // debug and develop. On Linux XDG standard directories are required for the flatpak to work.
+#ifdef linux
+    static const inline QString basePath = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + QDir::toNativeSeparators("/")
+            + ORGNAME + QDir::toNativeSeparators("/");
+#else
+    static const inline QString basePath = QCoreApplication::applicationDirPath();
+#endif
+
 };