Browse Source

Refactored FirstRunDIalog finish functionality

George 4 years ago
parent
commit
029eedd3e8

+ 1 - 0
main.cpp

@@ -49,6 +49,7 @@ int main(int argc, char *argv[])
 
     ASettings::setup();
 
+    FirstRunDialog().exec();
     aDB()->connect();
     if (!ASettings::read("setup/setup_complete").toBool()) {
         FirstRunDialog dialog;

+ 6 - 5
src/database/adatabase.cpp

@@ -50,6 +50,11 @@ ADatabase* ADatabase::getInstance()
     return instance;
 }
 
+ADatabase::ADatabase()
+    : databaseDir(QDir(AStandardPaths::getPath(QStandardPaths::AppDataLocation))),
+      databasePath(databaseDir.filePath(QStringLiteral("logbook.db")))
+{}
+
 /*!
  * \brief ADatabase::sqliteVersion returns database sqlite version.
  * \return sqlite version string
@@ -68,12 +73,8 @@ bool ADatabase::connect()
     if (!QSqlDatabase::isDriverAvailable(SQL_DRIVER))
         return false;
 
-    QString path = AStandardPaths::getPath(QStandardPaths::AppDataLocation);
-    QDir directory(path);
-    // [G]: Where would it make sense to define the database file?. In the class perhaps?
-    QString databaseLocation = directory.filePath(QStringLiteral("logbook.db"));
     QSqlDatabase db = QSqlDatabase::addDatabase(SQL_DRIVER);
-    db.setDatabaseName(databaseLocation);
+    db.setDatabaseName(databasePath);
 
     if (!db.open())
         return false;

+ 4 - 1
src/database/adatabase.h

@@ -37,6 +37,7 @@
 #include "src/classes/atailentry.h"
 #include "src/classes/aaircraftentry.h"
 #include "src/classes/aflightentry.h"
+#include "src/astandardpaths.h"
 
 /*!
  * \brief The DBTarget enum lists database items that are
@@ -79,7 +80,7 @@ private:
     TableNames tableNames;
     TableColumns tableColumns;
     static ADatabase* instance;
-    ADatabase() = default;
+    ADatabase();
 public:
     // Ensure DB is not copiable or assignable
     ADatabase(const ADatabase&) = delete;
@@ -90,6 +91,8 @@ public:
     const QString sqliteVersion();
 
     ADatabaseError lastError;
+    const QDir databaseDir;
+    const QString databasePath;
 
     /*!
      * \brief Connect to the database and populate database information.

+ 36 - 0
src/database/adatabasesetup.cpp

@@ -20,6 +20,7 @@
 #include "src/testing/adebug.h"
 #include "src/functions/areadcsv.h"
 
+#include "src/classes/adownload.h"
 
 // Statements for creation of database tables, Revision 15
 
@@ -278,6 +279,41 @@ bool ADataBaseSetup::createDatabase()
     return true;
 }
 
+bool ADataBaseSetup::downloadTemplates()
+{
+    QStringList templateTables = {"aircraft", "airports", "changelog"};
+    QString linkStub = TEMPLATE_URL;
+    // [G]: could be moved to AStandard paths. Feels clunky here.
+    // Might aswell be created upfront and have a more strict access.
+    aDB()->databaseDir.mkdir("templates");
+    // ---
+    QDir template_dir(aDB()->databaseDir.filePath("templates"));
+    for (const auto& table : templateTables) {
+        QEventLoop loop;
+        ADownload* dl = new ADownload;
+        QObject::connect(dl, &ADownload::done, &loop, &QEventLoop::quit );
+        dl->setTarget(QUrl(linkStub + table + ".csv"));
+        dl->setFileName(template_dir.filePath(table + ".csv"));
+        dl->download();
+        loop.exec(); // event loop waits for download done signal before allowing loop to continue
+        dl->deleteLater();
+    }
+    return true;
+}
+
+bool ADataBaseSetup::backupOldData()
+{
+    // back up old database
+    auto oldDatabase = QFile(aDB()->databasePath);
+    if (oldDatabase.exists()) {
+        auto dateString = QDateTime::currentDateTime().toString(Qt::ISODate);
+        DEB << "Backing up old database as: " << "logbook-backup-" + dateString;
+        if (!oldDatabase.rename("data/logbook-backup-" + dateString)) {
+            DEB << "Warning: Creating backup of old database has failed.";
+        }
+    }
+    return true;
+}
 
 bool ADataBaseSetup::importDefaultData()
 {

+ 6 - 0
src/database/adatabasesetup.h

@@ -20,6 +20,8 @@
 
 #include <QCoreApplication>
 
+const auto TEMPLATE_URL = QStringLiteral("https://raw.githubusercontent.com/fiffty-50/openpilotlog/develop/assets/database/templates/");
+
 /*!
  * \brief The ADataBaseSetup class is responsible for the inital setup of the database when
  * the application is first launched. It creates the database in the specified default
@@ -33,6 +35,10 @@ public:
 
     static bool createDatabase();
 
+    static bool downloadTemplates();
+
+    static bool backupOldData();
+
     static bool fillTemplates();
 
     static bool importDefaultData();

+ 62 - 104
src/gui/dialogues/firstrundialog.cpp

@@ -6,7 +6,7 @@
 #include "src/classes/apilotentry.h"
 #include "src/classes/adownload.h"
 #include "src/classes/asettings.h"
-const auto TEMPLATE_URL = QStringLiteral("https://raw.githubusercontent.com/fiffty-50/openpilotlog/develop/assets/database/templates/");
+//const auto TEMPLATE_URL = QStringLiteral("https://raw.githubusercontent.com/fiffty-50/openpilotlog/develop/assets/database/templates/");
 
 static inline
 void prompt_error_box(QString title, QString text, QWidget* parent = nullptr)
@@ -86,127 +86,85 @@ void FirstRunDialog::on_themeGroup_toggled(int id)
 
 void FirstRunDialog::finish()
 {
-    if(ui->lastnameLineEdit->text().isEmpty() || ui->firstnameLineEdit->text().isEmpty()){
-        auto mb = QMessageBox(this);
-        mb.setText("You have to enter a valid first and last name for the logbook.");
-        mb.show();
+    ASettings::write("userdata/lastname", ui->lastnameLineEdit->text());
+    ASettings::write("userdata/firstname", ui->firstnameLineEdit->text());
+    ASettings::write("userdata/employeeid", ui->employeeidLineEdit->text());
+    ASettings::write("userdata/phone", ui->phoneLineEdit->text());
+    ASettings::write("userdata/email", ui->emailLineEdit->text());
+
+    ASettings::write("flightlogging/function", ui->functionComboBox->currentText());
+    ASettings::write("flightlogging/approach", ui->approachComboBox->currentText());
+    ASettings::write("flightlogging/nightlogging", ui->nightComboBox->currentIndex());
+    ASettings::write("flightlogging/logIfr", ui->rulesComboBox->currentIndex());
+    ASettings::write("flightlogging/flightnumberPrefix", ui->prefixLineEdit->text());
+
+    ASettings::write("flightlogging/numberTakeoffs", 1);
+    ASettings::write("flightlogging/numberLandings", 1);
+    ASettings::write("flightlogging/popupCalendar", true);
+    ASettings::write("flightlogging/pilotFlying", true);
+
+    QMap<QString, QVariant> data;
+    switch (ui->aliasComboBox->currentIndex()) {
+    case 0:
+        ASettings::write("userdata/displayselfas", ui->aliasComboBox->currentIndex());
+        break;
+    case 1:
+        ASettings::write("userdata/displayselfas", ui->aliasComboBox->currentIndex());
+        break;
+    case 2:
+        ASettings::write("userdata/displayselfas", ui->aliasComboBox->currentIndex());
+        break;
+    default:
+        break;
+    }
+    data.insert("lastname", ui->lastnameLineEdit->text());
+    data.insert("firstname", ui->firstnameLineEdit->text());
+    data.insert("alias", "self");
+    data.insert("employeeid", ui->employeeidLineEdit->text());
+    data.insert("phone", ui->phoneLineEdit->text());
+    data.insert("email", ui->emailLineEdit->text());
+
+    if (!finishSetup()) {
+        QMessageBox message_box(this);
+        message_box.setText("Errors have ocurred creating the database. Without a working database The application will not be usable.");
+    }
+    ASettings::write("setup/setup_complete", true);
+    aDB()->disconnect(); // reset db connection to refresh layout after initial setup.
+    aDB()->connect();
+    auto pilot = APilotEntry(1);
+    pilot.setData(data);
+    if (aDB()->commit(pilot)) {
+        qApp->quit();
+        QProcess::startDetached(qApp->arguments()[0], qApp->arguments());
     } else {
-        ASettings::write("userdata/lastname", ui->lastnameLineEdit->text());
-        ASettings::write("userdata/firstname", ui->firstnameLineEdit->text());
-        ASettings::write("userdata/employeeid", ui->employeeidLineEdit->text());
-        ASettings::write("userdata/phone", ui->phoneLineEdit->text());
-        ASettings::write("userdata/email", ui->emailLineEdit->text());
-
-        ASettings::write("flightlogging/function", ui->functionComboBox->currentText());
-        ASettings::write("flightlogging/approach", ui->approachComboBox->currentText());
-        ASettings::write("flightlogging/nightlogging", ui->nightComboBox->currentIndex());
-        ASettings::write("flightlogging/logIfr", ui->rulesComboBox->currentIndex());
-        ASettings::write("flightlogging/flightnumberPrefix", ui->prefixLineEdit->text());
-
-        ASettings::write("flightlogging/numberTakeoffs", 1);
-        ASettings::write("flightlogging/numberLandings", 1);
-        ASettings::write("flightlogging/popupCalendar", true);
-        ASettings::write("flightlogging/pilotFlying", true);
-
-
-        QMap<QString, QVariant> data;
-        switch (ui->aliasComboBox->currentIndex()) {
-        case 0:
-            ASettings::write("userdata/displayselfas", ui->aliasComboBox->currentIndex());
-            break;
-        case 1:
-            ASettings::write("userdata/displayselfas", ui->aliasComboBox->currentIndex());
-            break;
-        case 2:
-            ASettings::write("userdata/displayselfas", ui->aliasComboBox->currentIndex());
-            break;
-        default:
-            break;
-        }
-        data.insert("lastname", ui->lastnameLineEdit->text());
-        data.insert("firstname", ui->firstnameLineEdit->text());
-        data.insert("alias", "self");
-        data.insert("employeeid", ui->employeeidLineEdit->text());
-        data.insert("phone", ui->phoneLineEdit->text());
-        data.insert("email", ui->emailLineEdit->text());
-
-        if (!finishSetup()) {
-            QMessageBox message_box(this);
-            message_box.setText("Errors have ocurred creating the database. Without a working database The application will not be usable.");
-        }
-        ASettings::write("setup/setup_complete", true);
-        aDB()->disconnect(); // reset db connection to refresh layout after initial setup.
-        aDB()->connect();
-        auto pilot = APilotEntry(1);
-        pilot.setData(data);
-        if (aDB()->commit(pilot)) {
-            qApp->quit();
-            QProcess::startDetached(qApp->arguments()[0], qApp->arguments());
-        } else {
-            QMessageBox message_box(this);
-            message_box.setText("Errors have ocurred creating the database. Without a working database The application will not be usable.");
-        }
-
+        QMessageBox message_box(this);
+        message_box.setText("Errors have ocurred creating the database. Without a working database The application will not be usable.");
     }
 }
 
 bool FirstRunDialog::finishSetup()
 {
-
-    //check if template dir exists and create if needed.
-    QDir dir("data/templates");
-    DEB << dir.path();
-    if (!dir.exists())
-        dir.mkpath(".");
-
     QMessageBox confirm;
     confirm.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
     confirm.setDefaultButton(QMessageBox::No);
     confirm.setIcon(QMessageBox::Question);
     confirm.setWindowTitle("Create Database");
     confirm.setText("We are now going to create the database. Would you like to download the latest database information?\n(Recommended, Internet connection required)\n");
+
     int reply = confirm.exec();
-    if (reply == QMessageBox::Yes) {
-        // download latest csv
-        QStringList templateTables = {"aircraft", "airports", "changelog"};
-        QString linkStub = TEMPLATE_URL;
-        for (const auto& table : templateTables) {
-            QEventLoop loop;
-            ADownload* dl = new ADownload;
-            connect(dl, &ADownload::done, &loop, &QEventLoop::quit );
-            dl->setTarget(QUrl(linkStub + table + ".csv"));
-            dl->setFileName("data/templates/" + table + ".csv");
-            dl->download();
-            loop.exec(); // event loop waits for download done signal before allowing loop to continue
-            dl->deleteLater();
-        }
-    }
+    if (reply == QMessageBox::Yes)
+        ADataBaseSetup::downloadTemplates();
 
-    //close database connection
     aDB()->disconnect();
-
-    // back up old database
-    auto oldDatabase = QFile("data/logbook.db");
-    if (oldDatabase.exists()) {
-        auto dateString = QDateTime::currentDateTime().toString(Qt::ISODate);
-        DEB << "Backing up old database as: " << "logbook-backup-" + dateString;
-        if (!oldDatabase.rename("data/logbook-backup-" + dateString)) {
-            DEB << "Warning: Creating backup of old database has failed.";
-        }
-    }
-    // re-connect and create new database
+    ADataBaseSetup::backupOldData();
     aDB()->connect();
 
-    if (ADataBaseSetup::createDatabase()) {
-        if (ADataBaseSetup::importDefaultData()) {
-            ASettings::write("setup/setup_complete", true);
-            return true;
-        } else {
-            return false;
-        }
-    } else {
+    if(!ADataBaseSetup::createDatabase())
         return false;
-    }
+    if(!ADataBaseSetup::importDefaultData())
+        return false;
+    ASettings::write("setup/setup_complete", true);
+    return true;
 }