Browse Source

AStandardPaths includes trailing seperator

`AStandardPaths` now include an os-specific trailing directory seperator to more clearly distinguish directories from files. e.g.:

`/path/to/dir/` or `C:\path\to\dir\` 
instead of
`/path/to/dir` or `C:\path\to\dir`

Adjusted `ADatabaseSetup` class for this change, cleared up logic for `importDefaultData()`;
Felix 4 years ago
parent
commit
7cd8468159

+ 0 - 4
src/classes/asettings.cpp

@@ -16,7 +16,6 @@
  *along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 #include "asettings.h"
-#include "astandardpaths.h"
 #include <QSettings>
 
 
@@ -147,6 +146,3 @@ QString ASettings::stringOfKey (const ASettings::Setup key)
 
 QString ASettings::stringOfKey (const ASettings::UserData key)
 { return  userDataMap[key]; }
-
-// [F]: removed because the function was unused and wouldn't compile with qt 5.9.5. Not sure why it did in the first place.
-// see https://doc.qt.io/archives/qt-5.9/qobject.html#no-copy-constructor-or-assignment-operator for info

+ 19 - 11
src/classes/astandardpaths.cpp

@@ -1,30 +1,33 @@
 #include "src/classes/astandardpaths.h"
 
-QMap<AStandardPaths::Dirs, QString> AStandardPaths::dirs;
+QMap<AStandardPaths::Directories, QString> AStandardPaths::directories;
 
 void AStandardPaths::setup()
 {
     auto data_location = QStandardPaths::AppDataLocation;
-    dirs = {  // [G]: potential rename to `dirs`
-        {Database, QStandardPaths::writableLocation(data_location)},
-        {Templates, QDir(QStandardPaths::writableLocation(data_location)).filePath("templates")},
-        {DatabaseBackup, QDir(QStandardPaths::writableLocation(data_location)).filePath("backup")}
+    directories = { // [F]: Dir could be ambiguous since it is very similar to QDir
+        {Database, QDir::toNativeSeparators(
+         QStandardPaths::writableLocation(data_location) + '/')},
+        {Templates, QDir::toNativeSeparators(
+         QDir(QStandardPaths::writableLocation(data_location)).filePath(QStringLiteral("templates/")))},
+        {DatabaseBackup, QDir::toNativeSeparators(
+         QDir(QStandardPaths::writableLocation(data_location)).filePath(QStringLiteral("backup/")))}
     };
 }
 
-const QString& AStandardPaths::absPathOf(Dirs loc)
+const QString& AStandardPaths::absPathOf(Directories loc)
 {
-    return dirs[loc];
+    return directories[loc];
 }
 
-const QMap<AStandardPaths::Dirs, QString>& AStandardPaths::allPaths()
+const QMap<AStandardPaths::Directories, QString>& AStandardPaths::allPaths()
 {
-    return dirs;
+    return directories;
 }
 
 void AStandardPaths::scan_dirs()
 {
-    for(auto& dir : dirs){
+    for(auto& dir : directories){
         auto d = QDir(dir);
         if(!d.exists()) {
             d.mkpath(dir);
@@ -32,9 +35,14 @@ void AStandardPaths::scan_dirs()
     }
 }
 
+// [F]: We could make scan_dirs return bool and return false if !exists && !mkpath
+// This function seems like it would do the same as scan_dirs
+// Validating the contents would be rather complex to accomplish and difficult to
+// maintain I believe, since the contents of these directories might change and it
+// seems out of the scope of this class to verify the directories' contents.
 bool AStandardPaths::validate_dirs()
 {
-    for(auto& dir : dirs){
+    for(auto& dir : directories){
         //DEB << "Validating " << dir;
         if(false)  // determine path as valid (scan contents and parse for correctness)
             return false;

+ 6 - 6
src/classes/astandardpaths.h

@@ -10,18 +10,18 @@
 /*!
  * \brief The AStandardAppPaths class encapsulates a static QMap holding
  * the standard paths of the application. Setup should be called after
- * `QCoreApplication::setWhateverName`.
+ * `QCoreApplication::setWhateverName`. The paths contained in this class
+ * include a trailing seperator and all seperators are platform-agnostic.
  */
 class AStandardPaths{
 public:
-    enum Dirs {
+    enum Directories {
         Database,
         Templates,
-        Settings,
         DatabaseBackup
     };
 private:
-    static QMap<Dirs, QString> dirs;
+    static QMap<Directories, QString> directories;
 public:
     /// Initialise paths with corresponding StandardLocation paths
     static void setup();
@@ -30,8 +30,8 @@ public:
     // We should move away from getThis getThat functions.
     // I believe we can give better namings while avoiding this
     // OOP cliche of getEverything
-    static const QString& absPathOf(Dirs loc);
-    static const QMap<Dirs, QString>& allPaths();
+    static const QString& absPathOf(Directories loc);
+    static const QMap<Directories, QString>& allPaths();
 
     /// Ensure standard app directories exist, if not mkpath them.
     static void scan_dirs();

+ 21 - 21
src/database/adatabasesetup.cpp

@@ -309,7 +309,7 @@ bool ADataBaseSetup::backupOldData()
     auto backup_name = database_file.baseName() + "_bak_" + date_string + ".db";
     QFile file(aDB->databaseFile.absoluteFilePath());
 
-    if (!file.rename(backup_dir.absolutePath() + '/' + backup_name)) {
+    if (!file.rename(backup_dir.absolutePath() + backup_name)) {
         DEB << "Unable to backup old database.";
         return false;
     }
@@ -321,34 +321,34 @@ bool ADataBaseSetup::importDefaultData(bool use_local_data)
 {
     QSqlQuery query;
     // reset template tables
-    for (const auto& table : templateTables) {
+    for (const auto& table_name : templateTables) {
         //clear tables
-        query.prepare("DELETE FROM " + table);
+        query.prepare("DELETE FROM " + table_name);
         if (!query.exec()) {
             DEB << "Error: " << query.lastError().text();
+            return false;
+        }
+        // Prepare data
+        QVector<QStringList> data_to_commit;
+        QString error_message("Error importing data ");
+
+        if (use_local_data) {
+            data_to_commit = aReadCsv(QStringLiteral(":templates/database/templates/")
+                                      + table_name + QStringLiteral(".csv"));
+            error_message.append(" (local) ");
+        } else {
+            data_to_commit = aReadCsv(AStandardPaths::absPathOf(AStandardPaths::Templates)
+                                      + table_name + QStringLiteral(".csv"));
+            error_message.append(" (remote) ");
         }
 
         //fill with data from csv
-        if (use_local_data)
-        {
-            DEB << "Using local resources";
-            if (!commitData(aReadCsv(QStringLiteral(":templates/database/templates/")
-                                     + table + QStringLiteral(".csv")),
-                            table)) {
-                DEB << "Error importing data (local).";
-                return false;
-            }
-        } else {
-            DEB << "Using downloaded resources";
-            if (!commitData(aReadCsv(AStandardPaths::absPathOf(AStandardPaths::Templates)
-                                     % QLatin1Char('/')
-                                     % table % QStringLiteral(".csv")),
-                            table)) {
-                DEB << "Error importing data (remote).";
-                return false;
-            }
+        if (!commitData(data_to_commit, table_name)) {
+            DEB << error_message;
+            return false;
         }
     }
+
     return true;
 };