Browse Source

Guards against unsuccessful Downloads in FirstRunDialog

This commit adresses #52.

The templates that are used to create the initial database are now included in the project itself. This enables the user to choose not to download the latest revision should there be connectivity issues.

Even if the user chooses to download the latest templates, there is now a fallback mechanism to create the database from local data, in case the download fails. This leaves the user with a working application instead of being stuck at the FirstRunDialog.

Creating the database layout has been decoupled from filling in the template data.
Felix 4 years ago
parent
commit
1e6531f42a

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

+ 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 += \

+ 2 - 2
src/classes/adownload.cpp

@@ -28,7 +28,7 @@ ADownload::ADownload() : QObject(nullptr)
 
 ADownload::~ADownload()
 {
-    DEB << "Deleting Download 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();
 }

+ 25 - 13
src/database/adatabasesetup.cpp

@@ -270,12 +270,6 @@ bool ADataBaseSetup::createDatabase()
 
     aDB->updateLayout();
 
-    DEB << "Populating tables...";
-    if (!importDefaultData()) {
-        DEB << "Populating tables failed.";
-        return false;
-    }
-
     DEB << "Database successfully created!";
     return true;
 }
@@ -292,6 +286,11 @@ bool ADataBaseSetup::downloadTemplates()
         dl->setFileName(template_dir.filePath(table % QStringLiteral(".csv")));
         dl->download();
         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
+
         dl->deleteLater();
     }
     return true;
@@ -318,7 +317,7 @@ bool ADataBaseSetup::backupOldData()
     return true;
 }
 
-bool ADataBaseSetup::importDefaultData()
+bool ADataBaseSetup::importDefaultData(bool use_local_data)
 {
     QSqlQuery query;
     // reset template tables
@@ -328,13 +327,26 @@ bool ADataBaseSetup::importDefaultData()
         if (!query.exec()) {
             DEB << "Error: " << query.lastError().text();
         }
+
         //fill with data from csv
-        if (!commitData(aReadCsv(AStandardPaths::absPathOf(AStandardPaths::Templates)
-                                 % QLatin1Char('/')
-                                 % table % QStringLiteral(".csv")),
-                        table)) {
-            DEB << "Error importing data.";
-            return false;
+        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;
+            }
         }
     }
     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();
 

+ 11 - 17
src/gui/dialogues/firstrundialog.cpp

@@ -23,14 +23,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 +50,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,11 +75,6 @@ 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()
 {
 
@@ -140,8 +126,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 +147,7 @@ bool FirstRunDialog::setupDatabase()
 
     aDB->updateLayout();
 
-    if(!ADataBaseSetup::importDefaultData())
+    if(!ADataBaseSetup::importDefaultData(useLocalTemplates))
         return false;
     return true;
 }

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

@@ -24,17 +24,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 - 1
src/gui/widgets/debugwidget.cpp

@@ -75,7 +75,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();
@@ -168,6 +168,7 @@ void DebugWidget::on_importCsvPushButton_clicked()
 void DebugWidget::on_debugPushButton_clicked()
 {
     // debug space
+    ASettings::write(ASettings::Setup::SetupComplete, false);
 }
 
 /* //Comparing two functions template