Browse Source

Merge pull request #57 from fiffty-50/develop-adownload-rework

Guards against unsuccessful Downloads in FirstRunDialog - closes #52
Felix Turowsky 4 years ago
parent
commit
142427cf94

+ 1 - 0
CMakeLists.txt

@@ -97,6 +97,7 @@ set(PROJECT_SOURCES
     src/gui/widgets/totalswidget.ui
     assets/icons.qrc
     assets/themes/breeze.qrc
+    assets/templates.qrc
 )
 
 if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)

+ 7 - 0
assets/templates.qrc

@@ -0,0 +1,7 @@
+<RCC>
+    <qresource prefix="/templates">
+        <file>database/templates/aircraft.csv</file>
+        <file>database/templates/airports.csv</file>
+        <file>database/templates/changelog.csv</file>
+    </qresource>
+</RCC>

+ 11 - 9
main.cpp

@@ -41,9 +41,7 @@ int main(int argc, char *argv[])
     QCoreApplication::setOrganizationDomain(ORGDOMAIN);
     QCoreApplication::setApplicationName(APPNAME);
 
-    AStandardPaths::setup();
-    AStandardPaths::scan_dirs();
-    if(!AStandardPaths::validate_dirs()){
+    if(!AStandardPaths::setup()){
         DEB << "Standard paths not valid.";
         return 1;
     }
@@ -51,21 +49,25 @@ int main(int argc, char *argv[])
     ASettings::setup();
 
     AStyle::setup();
-    aDB->connect();
+
+    if (!aDB->connect()) {
+        DEB << "Error establishing database connection";
+        return 2;
+    }
+
     if (!ASettings::read(ASettings::Setup::SetupComplete).toBool()) {
         if(FirstRunDialog().exec() == QDialog::Rejected){
-            DEB "First run not accepted. Exiting.";
-            return 1;
+            DEB << "First run not accepted. Exiting.";
+            return 3;
         }
         ASettings::write(ASettings::Setup::SetupComplete, true);
         DEB << "Wrote setup_commplete?";
     }
 
-    //sqlite does not deal well with multiple connections, ensure only one instance is running
     ARunGuard guard(QStringLiteral("opl_single_key"));
     if ( !guard.tryToRun() ){
-        DEB << "Another Instance is already running. Exiting.";
-        return 2;
+        DEB << "Another Instance of openPilotLog is already running. Exiting.";
+        return 0;
     }
 
 

+ 0 - 1
mainwindow.cpp

@@ -24,7 +24,6 @@ MainWindow::MainWindow(QWidget *parent)
     : QMainWindow(parent)
     , ui(new Ui::MainWindow)
 {
-    ATimer timer(this);
     ui->setupUi(this);
 
     // Set up Toolbar

+ 1 - 0
openPilotLog.pro

@@ -108,6 +108,7 @@ else: unix:!android: target.path = /opt/$${TARGET}/bin
 
 RESOURCES += \
     assets/icons.qrc \
+    assets/templates.qrc \
     assets/themes/breeze.qrc
 
 DISTFILES += \

+ 17 - 0
src/classes/aaircraftentry.h

@@ -1,3 +1,20 @@
+/*
+ *openPilot Log - A FOSS Pilot Logbook Application
+ *Copyright (C) 2020  Felix Turowsky
+ *
+ *This program is free software: you can redistribute it and/or modify
+ *it under the terms of the GNU General Public License as published by
+ *the Free Software Foundation, either version 3 of the License, or
+ *(at your option) any later version.
+ *
+ *This program is distributed in the hope that it will be useful,
+ *but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *GNU General Public License for more details.
+ *
+ *You should have received a copy of the GNU General Public License
+ *along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
 #ifndef AAIRCRAFTENTRY_H
 #define AAIRCRAFTENTRY_H
 

+ 2 - 2
src/classes/adownload.cpp

@@ -28,7 +28,7 @@ ADownload::ADownload() : QObject(nullptr)
 
 ADownload::~ADownload()
 {
-    DEB << "Deleting Download object" ;
+    DEB << "Deleting ADownload Object";
 }
 
 void ADownload::setTarget(const QUrl &value)
@@ -65,7 +65,7 @@ void ADownload::downloadFinished(QNetworkReply *data)
     const QByteArray sdata = data->readAll();
     localFile.write(sdata);
     localFile.close();
-    DEB << "Download finished. Output file: " << fileName;
+    DEB << "Download finished. Output file: " << fileName << "size: " << localFile.size();
 
     emit done();
 }

+ 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

+ 39 - 24
src/classes/astandardpaths.cpp

@@ -1,43 +1,58 @@
+/*
+ *openPilot Log - A FOSS Pilot Logbook Application
+ *Copyright (C) 2020  Felix Turowsky
+ *
+ *This program is free software: you can redistribute it and/or modify
+ *it under the terms of the GNU General Public License as published by
+ *the Free Software Foundation, either version 3 of the License, or
+ *(at your option) any later version.
+ *
+ *This program is distributed in the hope that it will be useful,
+ *but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *GNU General Public License for more details.
+ *
+ *You should have received a copy of the GNU General Public License
+ *along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
 #include "src/classes/astandardpaths.h"
 
-QMap<AStandardPaths::Dirs, QString> AStandardPaths::dirs;
+QMap<AStandardPaths::Directories, QDir> AStandardPaths::directories;
 
-void AStandardPaths::setup()
+bool 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 = {
+        {Database, QDir(QStandardPaths::writableLocation(data_location))},
+        {Templates, QDir(QStandardPaths::writableLocation(data_location)
+         + QStringLiteral("/templates"))},
+        {Backup, QDir(QStandardPaths::writableLocation(data_location)
+         + QStringLiteral("/backup"))}
     };
-}
+    if (scan_directories())
+        return true;
 
-const QString& AStandardPaths::absPathOf(Dirs loc)
-{
-    return dirs[loc];
+    return false;
 }
 
-const QMap<AStandardPaths::Dirs, QString>& AStandardPaths::allPaths()
+const QDir& AStandardPaths::directory(Directories location)
 {
-    return dirs;
+    return directories[location];
 }
 
-void AStandardPaths::scan_dirs()
+const QMap<AStandardPaths::Directories, QDir>& AStandardPaths::allDirectories()
 {
-    for(auto& dir : dirs){
-        auto d = QDir(dir);
-        if(!d.exists()) {
-            d.mkpath(dir);
-        }
-    }
+    return directories;
 }
 
-bool AStandardPaths::validate_dirs()
+bool AStandardPaths::scan_directories()
 {
-    for(auto& dir : dirs){
-        //DEB << "Validating " << dir;
-        if(false)  // determine path as valid (scan contents and parse for correctness)
-            return false;
+    for(const auto& dir : directories){
+        if(!dir.exists()) {
+            DEB << dir << "Does not exist. Creating:" << dir.absolutePath();
+            if (!dir.mkpath(dir.absolutePath()))
+                return false;
+        }
     }
     return true;
 }

+ 42 - 19
src/classes/astandardpaths.h

@@ -1,3 +1,20 @@
+/*
+ *openPilot Log - A FOSS Pilot Logbook Application
+ *Copyright (C) 2020  Felix Turowsky
+ *
+ *This program is free software: you can redistribute it and/or modify
+ *it under the terms of the GNU General Public License as published by
+ *the Free Software Foundation, either version 3 of the License, or
+ *(at your option) any later version.
+ *
+ *This program is distributed in the hope that it will be useful,
+ *but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *GNU General Public License for more details.
+ *
+ *You should have received a copy of the GNU General Public License
+ *along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
 #ifndef ASTANDARDPATHS_H
 #define ASTANDARDPATHS_H
 
@@ -9,35 +26,41 @@
 
 /*!
  * \brief The AStandardAppPaths class encapsulates a static QMap holding
- * the standard paths of the application. Setup should be called after
- * `QCoreApplication::setWhateverName`.
+ * the standard paths of the application.
  */
 class AStandardPaths{
 public:
-    enum Dirs {
+    enum Directories {
         Database,
         Templates,
-        Settings,
-        DatabaseBackup
+        Backup
     };
 private:
-    static QMap<Dirs, QString> dirs;
-public:
-    /// Initialise paths with corresponding StandardLocation paths
-    static void setup();
+    static QMap<Directories, QDir> directories;
 
-    // [G]: Subjective opinion:
-    // 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();
+    /*!
+     * \brief Ensures the standard app directories exists and creates
+     * them if neccessary.
+     */
+    static bool scan_directories();
+
+public:
+    /*!
+     * \brief Creates and verifies a static map of the standard paths used in the app.
+     */
+    static bool setup();
 
-    /// Ensure standard app directories exist, if not mkpath them.
-    static void scan_dirs();
+    /*!
+     * \brief Returns the QDir for the standard directory referenced
+     * by the Directories enum 'loc'
+     */
+    static const QDir &directory(Directories location);
 
-    /// Validate standard app directories are valid in structure and contents.
-    static bool validate_dirs();
+    /*!
+     * \brief returns the static map of all standard directories
+     * \return static const QMap<Directories, QDir>
+     */
+    static const QMap<Directories, QDir> &allDirectories();
 };
 
 

+ 17 - 0
src/classes/astyle.cpp

@@ -1,3 +1,20 @@
+/*
+ *openPilot Log - A FOSS Pilot Logbook Application
+ *Copyright (C) 2020  Felix Turowsky
+ *
+ *This program is free software: you can redistribute it and/or modify
+ *it under the terms of the GNU General Public License as published by
+ *the Free Software Foundation, either version 3 of the License, or
+ *(at your option) any later version.
+ *
+ *This program is distributed in the hope that it will be useful,
+ *but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *GNU General Public License for more details.
+ *
+ *You should have received a copy of the GNU General Public License
+ *along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
 #include "astyle.h"
 #include <QStyle>
 #include <QStyleFactory>

+ 17 - 0
src/classes/astyle.h

@@ -1,3 +1,20 @@
+/*
+ *openPilot Log - A FOSS Pilot Logbook Application
+ *Copyright (C) 2020  Felix Turowsky
+ *
+ *This program is free software: you can redistribute it and/or modify
+ *it under the terms of the GNU General Public License as published by
+ *the Free Software Foundation, either version 3 of the License, or
+ *(at your option) any later version.
+ *
+ *This program is distributed in the hope that it will be useful,
+ *but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *GNU General Public License for more details.
+ *
+ *You should have received a copy of the GNU General Public License
+ *along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
 #ifndef ASTYLE_H
 #define ASTYLE_H
 #include <QString>

+ 2 - 2
src/database/adatabase.cpp

@@ -81,8 +81,8 @@ ADatabase* ADatabase::instance()
 }
 
 ADatabase::ADatabase()
-    : databaseDir(QDir(AStandardPaths::absPathOf(AStandardPaths::Database))),
-      databaseFile(QFileInfo(databaseDir.filePath(QStringLiteral("logbook.db"))))
+    : databaseFile(QFileInfo(AStandardPaths::directory(AStandardPaths::Database).
+                             absoluteFilePath(QStringLiteral("logbook.db"))))
 {}
 
 /*!

+ 2 - 1
src/database/adatabase.h

@@ -105,9 +105,10 @@ public:
     const QString sqliteVersion();
 
     ADatabaseError lastError;
-    const QDir databaseDir;
+    //const QDir databaseDir;
     const QFileInfo databaseFile;
 
+
     /*!
      * \brief Connect to the database and populate database information.
      */

+ 32 - 19
src/database/adatabasesetup.cpp

@@ -270,29 +270,28 @@ bool ADataBaseSetup::createDatabase()
 
     aDB->updateLayout();
 
-    DEB << "Populating tables...";
-    if (!importDefaultData()) {
-        DEB << "Populating tables failed.";
-        return false;
-    }
-
     DEB << "Database successfully created!";
     return true;
 }
 
 bool ADataBaseSetup::downloadTemplates()
 {
-    QDir template_dir(AStandardPaths::absPathOf(AStandardPaths::Templates));
+    QDir template_dir(AStandardPaths::directory(AStandardPaths::Templates));
     DEB << template_dir;
     for (const auto& table : templateTables) {
         QEventLoop loop;
         ADownload* dl = new ADownload;
         QObject::connect(dl, &ADownload::done, &loop, &QEventLoop::quit );
         dl->setTarget(QUrl(TEMPLATE_URL % table % QStringLiteral(".csv")));
-        dl->setFileName(template_dir.filePath(table % QStringLiteral(".csv")));
+        dl->setFileName(template_dir.absoluteFilePath(table % QStringLiteral(".csv")));
         dl->download();
-        loop.exec(); // event loop waits for download done signal before allowing loop to continue
         dl->deleteLater();
+        loop.exec(); // event loop waits for download done signal before allowing loop to continue
+
+        QFileInfo downloaded_file(template_dir.filePath(table % QStringLiteral(".csv")));
+        if (downloaded_file.size() == 0)
+            return false; // ssl/network error
+
     }
     return true;
 }
@@ -306,11 +305,11 @@ bool ADataBaseSetup::backupOldData()
 
     auto date_string = ADateTime::toString(QDateTime::currentDateTime(),
                                            Opl::Datetime::Backup);
-    auto backup_dir = QDir(AStandardPaths::absPathOf(AStandardPaths::DatabaseBackup));
+    auto backup_dir = AStandardPaths::directory(AStandardPaths::Backup);
     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.absoluteFilePath(backup_name))) {
         DEB << "Unable to backup old database.";
         return false;
     }
@@ -318,25 +317,39 @@ bool ADataBaseSetup::backupOldData()
     return true;
 }
 
-bool ADataBaseSetup::importDefaultData()
+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::directory(
+                                          AStandardPaths::Templates).absoluteFilePath(
+                                          table_name + QStringLiteral(".csv")));
+            error_message.append(" (remote) ");
+        }
+
         //fill with data from csv
-        if (!commitData(aReadCsv(AStandardPaths::absPathOf(AStandardPaths::Templates)
-                                 % QLatin1Char('/')
-                                 % table % QStringLiteral(".csv")),
-                        table)) {
-            DEB << "Error importing data.";
+        if (!commitData(data_to_commit, table_name)) {
+            DEB << error_message;
             return false;
         }
     }
+
     return true;
 };
 

+ 1 - 1
src/database/adatabasesetup.h

@@ -43,7 +43,7 @@ public:
 
     static bool fillTemplates();
 
-    static bool importDefaultData();
+    static bool importDefaultData(bool use_local_data);
 
     static bool resetToDefault();
 

+ 17 - 0
src/database/declarations.h

@@ -1,3 +1,20 @@
+/*
+ *openPilot Log - A FOSS Pilot Logbook Application
+ *Copyright (C) 2020  Felix Turowsky
+ *
+ *This program is free software: you can redistribute it and/or modify
+ *it under the terms of the GNU General Public License as published by
+ *the Free Software Foundation, either version 3 of the License, or
+ *(at your option) any later version.
+ *
+ *This program is distributed in the hope that it will be useful,
+ *but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *GNU General Public License for more details.
+ *
+ *You should have received a copy of the GNU General Public License
+ *along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
 #ifndef DECLARATIONS_H
 #define DECLARATIONS_H
 

+ 17 - 0
src/functions/acalc.cpp

@@ -1,3 +1,20 @@
+/*
+ *openPilot Log - A FOSS Pilot Logbook Application
+ *Copyright (C) 2020  Felix Turowsky
+ *
+ *This program is free software: you can redistribute it and/or modify
+ *it under the terms of the GNU General Public License as published by
+ *the Free Software Foundation, either version 3 of the License, or
+ *(at your option) any later version.
+ *
+ *This program is distributed in the hope that it will be useful,
+ *but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *GNU General Public License for more details.
+ *
+ *You should have received a copy of the GNU General Public License
+ *along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
 #include "acalc.h"
 #include "src/testing/adebug.h"
 #include "src/database/adatabase.h"

+ 17 - 0
src/functions/acalc.h

@@ -1,3 +1,20 @@
+/*
+ *openPilot Log - A FOSS Pilot Logbook Application
+ *Copyright (C) 2020  Felix Turowsky
+ *
+ *This program is free software: you can redistribute it and/or modify
+ *it under the terms of the GNU General Public License as published by
+ *the Free Software Foundation, either version 3 of the License, or
+ *(at your option) any later version.
+ *
+ *This program is distributed in the hope that it will be useful,
+ *but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *GNU General Public License for more details.
+ *
+ *You should have received a copy of the GNU General Public License
+ *along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
 #ifndef ACALC_H
 #define ACALC_H
 

+ 17 - 0
src/functions/adatetime.h

@@ -1,3 +1,20 @@
+/*
+ *openPilot Log - A FOSS Pilot Logbook Application
+ *Copyright (C) 2020  Felix Turowsky
+ *
+ *This program is free software: you can redistribute it and/or modify
+ *it under the terms of the GNU General Public License as published by
+ *the Free Software Foundation, either version 3 of the License, or
+ *(at your option) any later version.
+ *
+ *This program is distributed in the hope that it will be useful,
+ *but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *GNU General Public License for more details.
+ *
+ *You should have received a copy of the GNU General Public License
+ *along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
 #ifndef ADATETIME_H
 #define ADATETIME_H
 #include <QtCore>

+ 17 - 0
src/functions/areadcsv.cpp

@@ -1,3 +1,20 @@
+/*
+ *openPilot Log - A FOSS Pilot Logbook Application
+ *Copyright (C) 2020  Felix Turowsky
+ *
+ *This program is free software: you can redistribute it and/or modify
+ *it under the terms of the GNU General Public License as published by
+ *the Free Software Foundation, either version 3 of the License, or
+ *(at your option) any later version.
+ *
+ *This program is distributed in the hope that it will be useful,
+ *but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *GNU General Public License for more details.
+ *
+ *You should have received a copy of the GNU General Public License
+ *along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
 #include "areadcsv.h"
 
 /*!

+ 17 - 0
src/functions/areadcsv.h

@@ -1,3 +1,20 @@
+/*
+ *openPilot Log - A FOSS Pilot Logbook Application
+ *Copyright (C) 2020  Felix Turowsky
+ *
+ *This program is free software: you can redistribute it and/or modify
+ *it under the terms of the GNU General Public License as published by
+ *the Free Software Foundation, either version 3 of the License, or
+ *(at your option) any later version.
+ *
+ *This program is distributed in the hope that it will be useful,
+ *but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *GNU General Public License for more details.
+ *
+ *You should have received a copy of the GNU General Public License
+ *along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
 #ifndef AREADCSV_H
 #define AREADCSV_H
 

+ 17 - 0
src/functions/atime.h

@@ -1,3 +1,20 @@
+/*
+ *openPilot Log - A FOSS Pilot Logbook Application
+ *Copyright (C) 2020  Felix Turowsky
+ *
+ *This program is free software: you can redistribute it and/or modify
+ *it under the terms of the GNU General Public License as published by
+ *the Free Software Foundation, either version 3 of the License, or
+ *(at your option) any later version.
+ *
+ *This program is distributed in the hope that it will be useful,
+ *but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *GNU General Public License for more details.
+ *
+ *You should have received a copy of the GNU General Public License
+ *along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
 #ifndef ATIME_H
 #define ATIME_H
 

+ 29 - 19
src/gui/dialogues/firstrundialog.cpp

@@ -1,3 +1,20 @@
+/*
+ *openPilot Log - A FOSS Pilot Logbook Application
+ *Copyright (C) 2020  Felix Turowsky
+ *
+ *This program is free software: you can redistribute it and/or modify
+ *it under the terms of the GNU General Public License as published by
+ *the Free Software Foundation, either version 3 of the License, or
+ *(at your option) any later version.
+ *
+ *This program is distributed in the hope that it will be useful,
+ *but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *GNU General Public License for more details.
+ *
+ *You should have received a copy of the GNU General Public License
+ *along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
 #include "firstrundialog.h"
 #include "ui_firstrundialog.h"
 #include "src/testing/adebug.h"
@@ -6,7 +23,6 @@
 #include "src/classes/apilotentry.h"
 #include "src/classes/adownload.h"
 #include "src/classes/asettings.h"
-#include "src/classes/astandardpaths.h"
 #include "src/oplconstants.h"
 #include <QErrorMessage>
 
@@ -23,14 +39,6 @@ FirstRunDialog::FirstRunDialog(QWidget *parent) :
     for (const auto &approach : Opl::ApproachTypes){
         ui->approachComboBox->addItem(approach);
     }
-
-//    auto *themeGroup = new QButtonGroup;
-//    themeGroup->addButton(ui->systemThemeCheckBox, 0);
-//    themeGroup->addButton(ui->lightThemeCheckBox, 1);
-//    themeGroup->addButton(ui->darkThemeCheckBox, 2);
-
-//    QObject::connect(themeGroup, QOverload<int>::of(&QButtonGroup::buttonClicked),
-//                     this, &FirstRunDialog::on_themeGroup_toggled);
 }
 
 FirstRunDialog::~FirstRunDialog()
@@ -58,7 +66,6 @@ void FirstRunDialog::on_previousPushButton_clicked()
 void FirstRunDialog::on_nextPushButton_clicked()
 {
     auto current_idx = ui->stackedWidget->currentIndex();
-    // [G]: per index do appropriate error handling
     switch (current_idx) {
     case 0:
         if(ui->firstnameLineEdit->text().isEmpty()
@@ -84,16 +91,11 @@ void FirstRunDialog::on_nextPushButton_clicked()
     ui->stackedWidget->setCurrentIndex(current_idx + 1);
 }
 
-//void FirstRunDialog::on_themeGroup_toggled(int id)
-//{
-//    ASettings::write(ASettings::Main::Theme, id);
-//}
-
 bool FirstRunDialog::finish()
 {
 
     ASettings::write(ASettings::FlightLogging::Function, ui->functionComboBox->currentText());
-    ASettings::write(ASettings::FlightLogging::Approach, ui->approachComboBox->currentText());
+    ASettings::write(ASettings::FlightLogging::Approach, ui->approachComboBox->currentIndex());
     ASettings::write(ASettings::FlightLogging::NightLogging, ui->nightComboBox->currentIndex());
     ASettings::write(ASettings::FlightLogging::LogIFR, ui->rulesComboBox->currentIndex());
     ASettings::write(ASettings::FlightLogging::FlightNumberPrefix, ui->prefixLineEdit->text());
@@ -140,8 +142,16 @@ bool FirstRunDialog::setupDatabase()
                                QMessageBox::Yes | QMessageBox::No, this);
     confirm.setDefaultButton(QMessageBox::No);
 
-    if (confirm.exec() == QMessageBox::Yes)
-        ADataBaseSetup::downloadTemplates();
+    if (confirm.exec() == QMessageBox::Yes) {
+        useLocalTemplates = false;
+        if (!ADataBaseSetup::downloadTemplates()) { // To do: return true only if size of dl != 0
+            QMessageBox message_box(this);
+            message_box.setText(QStringLiteral("Downloading latest data has failed.<br><br>Using local data instead."));
+            useLocalTemplates = true; // fall back
+        } else {
+        useLocalTemplates = true;
+    }
+    }
 
     aDB->disconnect();
     ADataBaseSetup::backupOldData();
@@ -153,7 +163,7 @@ bool FirstRunDialog::setupDatabase()
 
     aDB->updateLayout();
 
-    if(!ADataBaseSetup::importDefaultData())
+    if(!ADataBaseSetup::importDefaultData(useLocalTemplates))
         return false;
     return true;
 }

+ 18 - 5
src/gui/dialogues/firstrundialog.h

@@ -1,3 +1,20 @@
+/*
+ *openPilot Log - A FOSS Pilot Logbook Application
+ *Copyright (C) 2020  Felix Turowsky
+ *
+ *This program is free software: you can redistribute it and/or modify
+ *it under the terms of the GNU General Public License as published by
+ *the Free Software Foundation, either version 3 of the License, or
+ *(at your option) any later version.
+ *
+ *This program is distributed in the hope that it will be useful,
+ *but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *GNU General Public License for more details.
+ *
+ *You should have received a copy of the GNU General Public License
+ *along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
 #ifndef FIRSTRUNDIALOG_H
 #define FIRSTRUNDIALOG_H
 
@@ -24,17 +41,13 @@ private slots:
 
     void on_nextPushButton_clicked();
 
-//    void on_themeGroup_toggled(int id);
-
 private:
     Ui::FirstRunDialog *ui;
-    // [G]: finish is the old signal.
-    // finishSetup does something with template of database which
-    // goes over my head but everything works for now. Better naming needed
 
     void reject() override;
     bool setupDatabase();
     bool finish();
+    bool useLocalTemplates;
 
 };
 

+ 2 - 2
src/gui/dialogues/newflightdialog.cpp

@@ -155,7 +155,7 @@ void NewFlightDialog::readSettings()
     DEB << "Reading Settings...";
     QSettings settings;
     ui->FunctionComboBox->setCurrentText(ASettings::read(ASettings::FlightLogging::Function).toString());
-    ui->ApproachComboBox->setCurrentText(ASettings::read(ASettings::FlightLogging::Approach).toString());
+    ui->ApproachComboBox->setCurrentIndex(ASettings::read(ASettings::FlightLogging::Approach).toInt());
 
     ASettings::read(ASettings::FlightLogging::PilotFlying).toBool() ? ui->PilotFlyingCheckBox->setChecked(true)
                                                           : ui->PilotMonitoringCheckBox->setChecked(true);
@@ -189,7 +189,7 @@ void NewFlightDialog::writeSettings()
     DEB << "Writing Settings...";
 
     ASettings::write(ASettings::FlightLogging::Function, ui->FunctionComboBox->currentText());
-    ASettings::write(ASettings::FlightLogging::Approach, ui->ApproachComboBox->currentText());
+    ASettings::write(ASettings::FlightLogging::Approach, ui->ApproachComboBox->currentIndex());
     ASettings::write(ASettings::FlightLogging::PilotFlying, ui->PilotFlyingCheckBox->isChecked());
     ASettings::write(ASettings::FlightLogging::NumberTakeoffs, ui->TakeoffSpinBox->value());
     ASettings::write(ASettings::FlightLogging::NumberLandings, ui->LandingSpinBox->value());

+ 26 - 7
src/gui/widgets/debugwidget.cpp

@@ -1,3 +1,20 @@
+/*
+ *openPilot Log - A FOSS Pilot Logbook Application
+ *Copyright (C) 2020  Felix Turowsky
+ *
+ *This program is free software: you can redistribute it and/or modify
+ *it under the terms of the GNU General Public License as published by
+ *the Free Software Foundation, either version 3 of the License, or
+ *(at your option) any later version.
+ *
+ *This program is distributed in the hope that it will be useful,
+ *but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *GNU General Public License for more details.
+ *
+ *You should have received a copy of the GNU General Public License
+ *along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
 #include "debugwidget.h"
 #include "ui_debugwidget.h"
 #include "src/classes/astandardpaths.h"
@@ -50,7 +67,7 @@ void DebugWidget::on_resetDatabasePushButton_clicked()
     link_stub.append("/assets/database/templates/");
 
     QStringList template_tables = {"aircraft", "airports", "changelog"};
-    QDir template_dir(AStandardPaths::absPathOf(AStandardPaths::Templates));
+    QDir template_dir(AStandardPaths::directory(AStandardPaths::Templates));
     for (const auto& table : template_tables) {
         QEventLoop loop;
         ADownload* dl = new ADownload;
@@ -75,7 +92,7 @@ void DebugWidget::on_resetDatabasePushButton_clicked()
                             "Check console for details.");
         message_box.exec();
     }
-    if (ADataBaseSetup::importDefaultData()) {
+    if (ADataBaseSetup::importDefaultData(false)) {
         message_box.setText("Database has been successfully reset.");
         emit aDB->dataBaseUpdated();
         message_box.exec();
@@ -100,7 +117,7 @@ void DebugWidget::on_fillUserDataPushButton_clicked()
     QString linkStub = "https://raw.githubusercontent.com/fiffty-50/openpilotlog/";
     linkStub.append(ui->branchLineEdit->text());
     linkStub.append("/assets/database/templates/sample_");
-    QDir template_dir(AStandardPaths::absPathOf(AStandardPaths::Templates));
+    QDir template_dir(AStandardPaths::directory(AStandardPaths::Templates));
 
     for (const auto& table : userTables) {
         QEventLoop loop;
@@ -116,8 +133,8 @@ void DebugWidget::on_fillUserDataPushButton_clicked()
     allGood.resize(userTables.size());
 
     for (const auto& table : userTables) {
-        auto data = aReadCsv(AStandardPaths::absPathOf(AStandardPaths::Templates)
-                             + "/sample_" + table + ".csv");
+        auto data = aReadCsv(AStandardPaths::directory(AStandardPaths::Templates).absoluteFilePath(
+                             + "sample_" + table + ".csv"));
         allGood.setBit(userTables.indexOf(table), ADataBaseSetup::commitData(data, table));
     }
 
@@ -136,7 +153,7 @@ void DebugWidget::on_selectCsvPushButton_clicked()
 {
     auto fileName = QFileDialog::getOpenFileName(this,
                                                  tr("Open CSV File for import"),
-                                                 AStandardPaths::absPathOf(AStandardPaths::Templates),
+                                                 AStandardPaths::directory(AStandardPaths::Templates).absolutePath(),
                                                  tr("CSV files (*.csv)"));
     ui->importCsvLineEdit->setText(fileName);
 }
@@ -145,7 +162,7 @@ void DebugWidget::on_importCsvPushButton_clicked()
 {
     ATimer timer(this);
     auto file = QFileInfo(ui->importCsvLineEdit->text());
-    DEB << "File exists/is file: " << file.exists() << file.isFile() << " Path: " << file.absoluteFilePath();
+    DEB << "File exists/is file:" << file.exists() << file.isFile() << " Path:" << file.absoluteFilePath();
 
     if (file.exists() && file.isFile()) {
 
@@ -168,6 +185,8 @@ void DebugWidget::on_importCsvPushButton_clicked()
 void DebugWidget::on_debugPushButton_clicked()
 {
     // debug space
+    //ASettings::write(ASettings::Setup::SetupComplete, false);
+
 }
 
 /* //Comparing two functions template

+ 17 - 0
src/gui/widgets/debugwidget.h

@@ -1,3 +1,20 @@
+/*
+ *openPilot Log - A FOSS Pilot Logbook Application
+ *Copyright (C) 2020  Felix Turowsky
+ *
+ *This program is free software: you can redistribute it and/or modify
+ *it under the terms of the GNU General Public License as published by
+ *the Free Software Foundation, either version 3 of the License, or
+ *(at your option) any later version.
+ *
+ *This program is distributed in the hope that it will be useful,
+ *but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *GNU General Public License for more details.
+ *
+ *You should have received a copy of the GNU General Public License
+ *along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
 #ifndef DEBUGWIDGET_H
 #define DEBUGWIDGET_H
 

+ 17 - 0
src/gui/widgets/totalswidget.cpp

@@ -1,3 +1,20 @@
+/*
+ *openPilot Log - A FOSS Pilot Logbook Application
+ *Copyright (C) 2020  Felix Turowsky
+ *
+ *This program is free software: you can redistribute it and/or modify
+ *it under the terms of the GNU General Public License as published by
+ *the Free Software Foundation, either version 3 of the License, or
+ *(at your option) any later version.
+ *
+ *This program is distributed in the hope that it will be useful,
+ *but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *GNU General Public License for more details.
+ *
+ *You should have received a copy of the GNU General Public License
+ *along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
 #include "totalswidget.h"
 #include "ui_totalswidget.h"
 #include "src/testing/adebug.h"

+ 17 - 0
src/gui/widgets/totalswidget.h

@@ -1,3 +1,20 @@
+/*
+ *openPilot Log - A FOSS Pilot Logbook Application
+ *Copyright (C) 2020  Felix Turowsky
+ *
+ *This program is free software: you can redistribute it and/or modify
+ *it under the terms of the GNU General Public License as published by
+ *the Free Software Foundation, either version 3 of the License, or
+ *(at your option) any later version.
+ *
+ *This program is distributed in the hope that it will be useful,
+ *but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *GNU General Public License for more details.
+ *
+ *You should have received a copy of the GNU General Public License
+ *along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
 #ifndef TOTALSWIDGET_H
 #define TOTALSWIDGET_H
 

+ 17 - 0
src/oplconstants.h

@@ -1,3 +1,20 @@
+/*
+ *openPilot Log - A FOSS Pilot Logbook Application
+ *Copyright (C) 2020  Felix Turowsky
+ *
+ *This program is free software: you can redistribute it and/or modify
+ *it under the terms of the GNU General Public License as published by
+ *the Free Software Foundation, either version 3 of the License, or
+ *(at your option) any later version.
+ *
+ *This program is distributed in the hope that it will be useful,
+ *but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *GNU General Public License for more details.
+ *
+ *You should have received a copy of the GNU General Public License
+ *along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
 #ifndef OPLCONSTANTS_H
 #define OPLCONSTANTS_H