Browse Source

Started work on Application Paths

Rework started on StandardPaths. Goal is to keep all files in the application folder instead of spreading them out to OS specific standard location in order to keep it simple and increase portability.
Felix Turo 2 years ago
parent
commit
0494f722ea

+ 2 - 0
CMakeLists.txt

@@ -79,6 +79,8 @@ set(PROJECT_SOURCES
     # Classes
     src/classes/astyle.h
     src/classes/astyle.cpp
+    src/classes/paths.h
+    src/classes/paths.cpp
     src/classes/astandardpaths.h
     src/classes/astandardpaths.cpp
     src/classes/adownload.h

+ 2 - 3
main.cpp

@@ -21,11 +21,10 @@
 #include "src/gui/dialogues/firstrundialog.h"
 #include "src/classes/arunguard.h"
 #include "src/classes/asettings.h"
-#include "src/classes/astandardpaths.h"
 #include "src/classes/asettings.h"
 #include "src/classes/astyle.h"
 #include "src/functions/alog.h"
-#include "src/classes/atranslator.h"
+#include "src/classes/paths.h"
 #include <QApplication>
 #include <QProcess>
 #include <QSettings>
@@ -46,7 +45,7 @@ namespace Main {
 void init()
 {
     LOG << "Setting up / verifying Application Directories...";
-    if(AStandardPaths::setup()) {
+    if(OPL::Paths::setup()) {
         LOG << "Application Directories... verified";
     } else {
         LOG << "Unable to create directories.";

+ 15 - 12
mainwindow.cpp

@@ -18,21 +18,26 @@
 #include <QToolBar>
 #include "mainwindow.h"
 #include "ui_mainwindow.h"
-#include "src/functions/alog.h"
 #include "src/database/database.h"
 #include "src/classes/astyle.h"
 #include "src/gui/dialogues/firstrundialog.h"
 #include "src/gui/dialogues/newflightdialog.h"
 #include "src/gui/dialogues/newsimdialog.h"
 // Quick and dirty Debug area
-#include <QTimeZone>
-#include "src/gui/dialogues/newairportdialog.h"
+#include "src/classes/paths.h"
 void MainWindow::doDebugStuff()
 {
-    //const auto list = QTimeZone::availableTimeZoneIds();
-    //DEB << list;
-    auto nad = new NewAirportDialog(5, this);
-    nad->exec();
+    AStandardPaths::setup();
+    DEB << OPL::Paths::directory(OPL::Paths::Backup);
+    DEB << OPL::Paths::path(OPL::Paths::Backup);
+    DEB << OPL::Paths::filePath(OPL::Paths::Database, "logbook.db");
+    DEB << OPL::Paths::setup();
+
+    QDir dir(QCoreApplication::applicationDirPath());
+    QString filename("file.f");
+
+    DEB << AStandardPaths::asChildOfDir(AStandardPaths::Backup, filename);
+    DEB << OPL::Paths::filePath(OPL::Paths::Backup, filename);
 }
 
 MainWindow::MainWindow(QWidget *parent)
@@ -44,10 +49,7 @@ MainWindow::MainWindow(QWidget *parent)
     setActionIcons(AStyle::getStyleType());
 
     // connect to the Database
-    QFileInfo database_file(AStandardPaths::directory(AStandardPaths::Database).
-                                         absoluteFilePath(QStringLiteral("logbook.db")));
-
-    if (database_file.size() == 0)
+    if (OPL::Paths::databaseFileInfo().size() == 0)
         onDatabaseInvalid();
 
     if(!DB->connect()){
@@ -232,7 +234,8 @@ void MainWindow::onDatabaseInvalid()
         DEB << "Yes(Import Backup)";
         QString db_path = QFileDialog::getOpenFileName(this,
                                                        tr("Select Database"),
-                                                       AStandardPaths::directory(AStandardPaths::Backup).canonicalPath(),
+                                                       OPL::Paths::directory(OPL::Paths::Backup).canonicalPath(),
+                                                       //AStandardPaths::directory(AStandardPaths::Backup).canonicalPath(),
                                                        tr("Database file (*.db)"));
         if (!db_path.isEmpty()) {
             if(!DB->restoreBackup(db_path)) {

+ 44 - 0
src/classes/paths.cpp

@@ -0,0 +1,44 @@
+#include "paths.h"
+
+namespace OPL {
+
+Paths::Paths()
+{
+
+}
+
+const bool Paths::setup()
+{
+    const QString dir_path = QCoreApplication::applicationDirPath();
+    for(const auto& str : qAsConst(directories)){
+        QDir dir(dir_path + str);
+        if(!dir.exists()) {
+            if (!dir.mkpath(dir.absolutePath()))
+                return false;
+        }
+    }
+    return true;
+}
+
+const QDir Paths::directory(Directories location)
+{
+    return QDir(QCoreApplication::applicationDirPath() + directories[location]);
+}
+
+const QString Paths::path(Directories location)
+{
+    return QDir::toNativeSeparators(QCoreApplication::applicationDirPath() + directories[location]);
+}
+
+const QString Paths::filePath(Directories location, const QString &filename)
+{
+    QDir dir(QCoreApplication::applicationDirPath() + directories[location]);
+    return dir.absoluteFilePath(filename);
+}
+
+const QFileInfo Paths::databaseFileInfo()
+{
+    return QFileInfo(directory(Database), QStringLiteral("logbook.db"));
+}
+
+} // namespace OPL

+ 53 - 0
src/classes/paths.h

@@ -0,0 +1,53 @@
+#ifndef PATHS_H
+#define PATHS_H
+
+#include <QCoreApplication>
+#include <QDir>
+#include <QHash>
+
+
+namespace OPL {
+
+
+class Paths
+{
+public:
+    enum Directories {
+        Database,
+        Templates,
+        Backup,
+        Log,
+    };
+
+    const static inline QHash<Directories, QLatin1String> directories = {
+        {Database, QLatin1String("/database")},
+        {Templates, QLatin1String("/templates")},
+        {Backup, QLatin1String("/backup")},
+        {Log, QLatin1String("/log")},
+    };
+
+    Paths();
+
+    static const bool setup();
+
+    /*!
+     * \brief Returns the QDir for the standard directory referenced
+     * by the Directories enum 'loc'
+     */
+    static const QDir directory(Directories location);
+
+    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();
+};
+
+
+} // namespace OPL
+#endif // PATHS_H

+ 6 - 3
src/database/database.cpp

@@ -686,9 +686,12 @@ bool Database::importTemplateData(bool use_local_ressources)
                                       + table_name + QLatin1String(".json")).array();
             error_message.append(QLatin1String(" (ressource) "));
         } else {
-            data_to_commit = AJson::readFileToDoc(AStandardPaths::directory(
-                                          AStandardPaths::Templates).absoluteFilePath(
-                                          table_name + QLatin1String(".json"))).array();
+            const QString file_path = OPL::Paths::filePath(OPL::Paths::Templates,
+                                                           table_name + QLatin1String(".json"));
+            data_to_commit = AJson::readFileToDoc(file_path).array();
+            //data_to_commit = AJson::readFileToDoc(AStandardPaths::directory(
+            //                              AStandardPaths::Templates).absoluteFilePath(
+            //                              table_name + QLatin1String(".json"))).array();
             error_message.append(QLatin1String(" (downloaded) "));
         }
 

+ 2 - 2
src/database/database.h

@@ -31,6 +31,7 @@
 #include <QSqlRecord>
 #include <QSqlField>
 
+#include "src/classes/paths.h"
 #include "src/opl.h"
 #include "src/database/row.h"
 #include "src/classes/astandardpaths.h"
@@ -59,8 +60,7 @@ class Database : public QObject {
 private:
     Q_OBJECT
     Database()
-        : databaseFile(QFileInfo(AStandardPaths::directory(AStandardPaths::Database).
-                                 absoluteFilePath(QStringLiteral("logbook.db"))))
+        : databaseFile(OPL::Paths::databaseFileInfo())
     {}
     static Database* self;
     const QFileInfo databaseFile;

+ 2 - 1
src/functions/alog.cpp

@@ -16,6 +16,7 @@
  *along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 #include "alog.h"
+#include "src/classes/paths.h"
 #include <QMessageBox>
 #include <QTextStream>
 
@@ -60,7 +61,7 @@ void deleteOldLogs()
 bool init(bool log_debug)
 {
     logDebug = log_debug;
-    logFolder = AStandardPaths::directory(AStandardPaths::Log);
+    logFolder = OPL::Paths::directory(OPL::Paths::Log);
     deleteOldLogs();
     setLogFileName();
 

+ 7 - 7
src/gui/dialogues/firstrundialog.cpp

@@ -18,7 +18,6 @@
 #include "firstrundialog.h"
 #include "ui_firstrundialog.h"
 #include "src/opl.h"
-#include "src/functions/alog.h"
 #include "src/database/database.h"
 #include "src/database/dbsummary.h"
 #include "src/gui/widgets/backupwidget.h"
@@ -27,9 +26,7 @@
 #include "src/classes/asettings.h"
 #include "src/functions/adate.h"
 #include "src/classes/astyle.h"
-#include "src/functions/adatetime.h"
 #include "src/classes/ahash.h"
-#include "src/testing/atimer.h"
 #include <QErrorMessage>
 #include <QFileDialog>
 #include <QKeyEvent>
@@ -120,8 +117,9 @@ bool FirstRunDialog::finishSetup()
 {
     writeSettings();
 
-    QFileInfo database_file(AStandardPaths::directory(AStandardPaths::Database).
-                                 absoluteFilePath(QStringLiteral("logbook.db")));
+    QFileInfo database_file(OPL::Paths::databaseFileInfo());
+    //QFileInfo database_file(AStandardPaths::directory(AStandardPaths::Database).
+    //                             absoluteFilePath(QStringLiteral("logbook.db")));
     if (database_file.exists()) {
 
         QMessageBox message_box(QMessageBox::Question, tr("Existing Database found"),
@@ -200,7 +198,8 @@ bool FirstRunDialog::downloadTemplates(QString branch_name)
     template_url_string.append(branch_name);
     template_url_string.append(QLatin1String("/assets/database/templates/"));
 
-    QDir template_dir(AStandardPaths::directory(AStandardPaths::Templates));
+    QDir template_dir(OPL::Paths::directory(OPL::Paths::Templates));
+    //QDir template_dir(AStandardPaths::directory(AStandardPaths::Templates));
 
     QStringList template_table_names;
     for (const auto table : DB->getTemplateTables())
@@ -253,7 +252,8 @@ bool FirstRunDialog::verifyTemplates()
     for (const auto table : DB->getTemplateTables())
         template_table_names.append(OPL::GLOBALS->getDbTableName(table));
     for (const auto &table_name : template_table_names) {
-        const QString path = AStandardPaths::asChildOfDir(AStandardPaths::Templates, table_name);
+        const QString path = OPL::Paths::filePath(OPL::Paths::Templates, table_name);
+        //const QString path = AStandardPaths::asChildOfDir(AStandardPaths::Templates, table_name);
 
         QFileInfo check_file(path + QLatin1String(".json"));
         AHash hash(check_file);

+ 27 - 26
src/gui/widgets/backupwidget.cpp

@@ -18,8 +18,6 @@
 #include "backupwidget.h"
 #include "ui_backupwidget.h"
 #include "src/opl.h"
-#include "src/classes/astandardpaths.h"
-#include "src/functions/alog.h"
 #include "src/database/database.h"
 #include "src/functions/adatetime.h"
 #include "src/database/dbsummary.h"
@@ -58,19 +56,21 @@ void BackupWidget::changeEvent(QEvent *event)
 void BackupWidget::refresh()
 {
     // First column in table, would be created by listing the files in backupdir
-    QDir backup_dir = QDir(AStandardPaths::directory(AStandardPaths::Backup));
+    QDir backup_dir = OPL::Paths::directory(OPL::Paths::Backup);
+    //QDir backup_dir = QDir(AStandardPaths::directory(AStandardPaths::Backup));
     const QStringList entries = backup_dir.entryList(QStringList{"*.db"}, QDir::Files, QDir::Time);
     QFileIconProvider provider;
 
     // Get summary of each db file and populate lists (columns) of data
     for (const auto &entry : entries) {
+        DEB << "Filename string: " << entry;
         QMap<OPL::DbSummaryKey, QString> summary = OPL::DbSummary::databaseSummary(backup_dir.absoluteFilePath(entry));
         model->appendRow({new QStandardItem(summary[OPL::DbSummaryKey::total_time]),
                           new QStandardItem(summary[OPL::DbSummaryKey::total_flights]),
                           new QStandardItem(summary[OPL::DbSummaryKey::total_tails]),
                           new QStandardItem(summary[OPL::DbSummaryKey::total_pilots]),
                           new QStandardItem(summary[OPL::DbSummaryKey::last_flight]),
-                          new AFileStandardItem(provider.icon(QFileIconProvider::File), entry, AStandardPaths::Backup)
+                          new FileStandardItem(provider.icon(QFileIconProvider::File), entry, OPL::Paths::Backup)
                          });
     }
 
@@ -83,7 +83,8 @@ const QString BackupWidget::absoluteBackupPath()
     const QString backup_name = QLatin1String("logbook_backup_")
             + ADateTime::toString(QDateTime::currentDateTime(), OPL::DateTimeFormat::Backup)
             + QLatin1String(".db");
-    return AStandardPaths::asChildOfDir(AStandardPaths::Backup, backup_name);
+    return OPL::Paths::filePath(OPL::Paths::Backup, backup_name);
+    //return AStandardPaths::asChildOfDir(AStandardPaths::Backup, backup_name);
 }
 
 const QString BackupWidget::backupName()
@@ -95,8 +96,9 @@ const QString BackupWidget::backupName()
 
 void BackupWidget::on_tableView_clicked(const QModelIndex &index)
 {
-    selectedFileInfo = static_cast<AFileStandardItem*>(model->item(index.row(), 0));
-    DEB << "Item at row:" << index.row() << "->" << selectedFileInfo->data(Qt::DisplayRole);
+    selectedRows.clear();
+    selectedRows.append(index.row());
+    DEB << model->item(index.row(), 5)->data(Qt::DisplayRole);
 }
 
 void BackupWidget::on_createLocalPushButton_clicked()
@@ -118,21 +120,19 @@ void BackupWidget::on_createLocalPushButton_clicked()
                          new QStandardItem(summary[OPL::DbSummaryKey::total_tails]),
                          new QStandardItem(summary[OPL::DbSummaryKey::total_pilots]),
                          new QStandardItem(summary[OPL::DbSummaryKey::last_flight]),
-                         new AFileStandardItem(provider.icon(QFileIconProvider::File), QFileInfo(filename)),
+                         new FileStandardItem(provider.icon(QFileIconProvider::File), QFileInfo(filename)),
                      });
 }
 
 void BackupWidget::on_restoreLocalPushButton_clicked()
 {
-    if(selectedFileInfo == nullptr) {
+    if(selectedRows.isEmpty()) {
         INFO(tr("No backup selected"));
         return;
     }
 
-    QString backup_name = AStandardPaths::asChildOfDir(
-                AStandardPaths::Backup,
-                selectedFileInfo->data(Qt::DisplayRole).toString()
-                );
+    const QString file_name = model->item(selectedRows.first(), 5)->data(Qt::DisplayRole).toString();
+    const QString backup_name = OPL::Paths::filePath(OPL::Paths::Backup, file_name);
 
     QMessageBox confirm(this);
     confirm.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
@@ -153,21 +153,22 @@ void BackupWidget::on_restoreLocalPushButton_clicked()
     }
 
     view->clearSelection();
-    selectedFileInfo = nullptr;
+    selectedRows.clear();
 }
 
 void BackupWidget::on_deleteSelectedPushButton_clicked()
 {
-    if(selectedFileInfo == nullptr) {
+    if(selectedRows.isEmpty()) {
         INFO(tr("No backup was selected"));
         return;
     }
 
-    const QFileInfo& filename = selectedFileInfo->info();
-    QFile file(filename.absoluteFilePath());
+    const QString file_name = model->item(selectedRows.first(), 5)->data(Qt::DisplayRole).toString();
+    const QString backup_name = OPL::Paths::filePath(OPL::Paths::Backup, file_name);
+    QFile file(OPL::Paths::filePath(OPL::Paths::Backup, file_name));
 
     if(!file.exists()) {
-        WARN(tr("Selected backup file (<tt>%1</tt>) does not exist.").arg(filename.absolutePath()));
+        WARN(tr("Selected backup file (<tt>%1</tt>) does not exist.").arg(file_name));
         return;
     }
 
@@ -176,24 +177,24 @@ void BackupWidget::on_deleteSelectedPushButton_clicked()
     confirm.setDefaultButton(QMessageBox::No);
     confirm.setIcon(QMessageBox::Question);
     confirm.setWindowTitle(tr("Delete Backup"));
-    confirm.setText(tr("The following backup will be deleted:<br><br><b><tt>"
-                       "%1</b></tt><br><br>"
-                       "Are you sure?"
-                       ).arg(filename.fileName()));
+    confirm.setText(tr("The following backup will be deleted:<br><br><i>%1</i><br><br><b><tt>"
+                       "%2</b></tt><br><br>"
+                       "<b>This action is irreversible.</b><br><br>Continue?"
+                       ).arg(file_name, OPL::DbSummary::summaryString(backup_name)));
     if (confirm.exec() == QMessageBox::No)
         return;
 
-    LOG << "Deleting backup:" << filename;
+    LOG << "Deleting backup:" << file_name;
     if(!file.remove()) {
-        WARN(tr("Unable to remove file %1\nError: %2").arg(filename.fileName(),file.errorString()));
+        WARN(tr("Unable to remove file %1\nError: %2").arg(file_name,file.errorString()));
         return;
     } else {
         INFO(tr("Backup successfully deleted."));
     }
 
-    model->removeRow(selectedFileInfo->row());
+    model->removeRow(selectedRows.first());
     view->clearSelection();
-    selectedFileInfo = nullptr;
+    selectedRows.clear();
 }
 
 void BackupWidget::on_createExternalPushButton_clicked()

+ 6 - 9
src/gui/widgets/backupwidget.h

@@ -18,7 +18,7 @@
 #ifndef BACKUPWIDGET_H
 #define BACKUPWIDGET_H
 
-#include "src/classes/astandardpaths.h"
+#include "src/classes/paths.h"
 
 #include <QWidget>
 #include <QStandardItemModel>
@@ -37,20 +37,19 @@ class BackupWidget;
  * the absolute path in the fileInfo attribute for further use while
  * displaying only the base name.
  */
-class AFileStandardItem : public QStandardItem {
+class FileStandardItem : public QStandardItem {
 private:
     QFileInfo fileInfo;
 public:
-    AFileStandardItem(const QIcon& icon, const QString& filename, const AStandardPaths::Directories dir)
+    FileStandardItem(const QIcon& icon, const QString& filename, const OPL::Paths::Directories dir)
         : QStandardItem(icon, filename),
-          fileInfo(QFileInfo(AStandardPaths::asChildOfDir(dir, filename)))
+          fileInfo(QFileInfo(OPL::Paths::filePath(dir, filename)))
     {}
-    AFileStandardItem(const QIcon& icon, const QFileInfo file_info)
+    FileStandardItem(const QIcon& icon, const QFileInfo file_info)
         : QStandardItem(icon, file_info.baseName()),
           fileInfo(QFileInfo(file_info))
     {}
 
-
     const QFileInfo& info() const
     {
         return fileInfo;
@@ -121,9 +120,7 @@ private:
 
     QStandardItemModel *model;
     QTableView *view;
-    AFileStandardItem *selectedFileInfo = nullptr;  // Only the first column is necessary for
-                                                    // any operation and it is encapsulated in the
-                                                    // AFileStandardItem class
+    QList<int> selectedRows;
     void refresh();
 
 protected:

+ 0 - 1
src/gui/widgets/settingswidget.cpp

@@ -17,7 +17,6 @@
  */
 #include "settingswidget.h"
 #include "ui_settingswidget.h"
-#include "src/functions/alog.h"
 #include "src/classes/astyle.h"
 #include "src/classes/asettings.h"
 #include "src/database/database.h"