Browse Source

Merge remote-tracking branch 'origin/develop-firstrundialog-gk' into develop-firstrundialog-rework

Felix 4 years ago
parent
commit
71a6dfbbd6

+ 1 - 0
main.cpp

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

+ 8 - 5
src/astandardpaths.cpp

@@ -1,23 +1,26 @@
 #include "src/astandardpaths.h"
 
-QMap<QStandardPaths::StandardLocation, QString> AStandardPaths::paths;
+//QMap<QStandardPaths::StandardLocation, QString> AStandardPaths::paths;
+QMap<AStandardPaths::Dirs, QString> AStandardPaths::paths;
 
 void AStandardPaths::setup()
 {
     auto settings_location = QStandardPaths::AppConfigLocation;
     auto data_location = QStandardPaths::AppDataLocation;
     paths = {
-        {settings_location, QStandardPaths::writableLocation(settings_location)},
-        {data_location, QStandardPaths::writableLocation(data_location)},
+        {Database, QStandardPaths::writableLocation(data_location)},
+        {Templates, QDir(QStandardPaths::writableLocation(data_location)).filePath("templates")},
+        {Settings, QStandardPaths::writableLocation(settings_location)},
     };
+    DEB << "Paths created: " << paths;
 }
 
-QString AStandardPaths::getPath(QStandardPaths::StandardLocation loc)
+QString AStandardPaths::getPath(Dirs loc)
 {
     return paths[loc];
 }
 
-QMap<QStandardPaths::StandardLocation, QString> AStandardPaths::getPaths()
+QMap<AStandardPaths::Dirs, QString> AStandardPaths::getPaths()
 {
     return paths;
 }

+ 9 - 3
src/astandardpaths.h

@@ -13,15 +13,21 @@
  * `QCoreApplication::setWhateverName`.
  */
 class AStandardPaths{
+public:
+    enum Dirs {
+        Database,
+        Templates,
+        Settings,
+    };
 private:
     static
-    QMap<QStandardPaths::StandardLocation, QString> paths;
+    QMap<Dirs, QString> paths;
 public:
     /// Initialise paths with corresponding StandardLocation paths
     static void setup();
 
-    static QString getPath(QStandardPaths::StandardLocation loc);
-    static QMap<QStandardPaths::StandardLocation, QString> getPaths();
+    static QString getPath(Dirs loc);
+    static QMap<Dirs, QString> getPaths();
 
     /// Ensure standard app paths exist, if not mkdir them.
     static void scan_paths();

+ 6 - 5
src/database/adatabase.cpp

@@ -50,6 +50,11 @@ ADatabase* ADatabase::getInstance()
     return instance;
 }
 
+ADatabase::ADatabase()
+    : databaseDir(QDir(AStandardPaths::getPath(AStandardPaths::Database))),
+      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.

+ 21 - 29
src/database/adatabasesetup.cpp

@@ -281,46 +281,38 @@ bool ADataBaseSetup::createDatabase()
     return true;
 }
 
-/*!
- * \brief create template directory or verify exists
- */
-bool ADataBaseSetup::setupTemplateDir()
-{
-    QDir dir(AStandardPaths::getPath(QStandardPaths::AppDataLocation)
-             % QStringLiteral("/templates"));
-    if (dir.exists()) {
-        return true;
-    } else {
-        if(dir.mkpath(".")) {
-            return true;
-        } else {
-            return false;
-        }
-    }
-}
-/*!
- * \brief Download up-to-date database templates
- */
 bool ADataBaseSetup::downloadTemplates()
 {
-    DEB << "Downloading templates...";
+    QStringList templateTables = {"aircraft", "airports", "changelog"};
+    QString linkStub = TEMPLATE_URL;
+    QDir template_dir(AStandardPaths::getPath(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(AStandardPaths::getPath(QStandardPaths::AppDataLocation)
-                        % QStringLiteral("/templates/")
-                        % table % QStringLiteral(".csv"));
+        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(); // schedule dl object for deletion
+        dl->deleteLater();
     }
-    DEB << "Downloading templates complete";
     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()
 {

+ 4 - 2
src/database/adatabasesetup.h

@@ -22,6 +22,8 @@
 #include <QStringBuilder>
 #include <QEventLoop>
 
+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
@@ -35,10 +37,10 @@ public:
 
     static bool createDatabase();
 
-    static bool setupTemplateDir();
-
     static bool downloadTemplates();
 
+    static bool backupOldData();
+
     static bool fillTemplates();
 
     static bool importDefaultData();

+ 70 - 105
src/gui/dialogues/firstrundialog.cpp

@@ -7,6 +7,7 @@
 #include "src/classes/adownload.h"
 #include "src/classes/asettings.h"
 #include "src/astandardpaths.h"
+//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)
@@ -87,122 +88,86 @@ 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(QStringLiteral("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);
+    // [G]: Extremely suspect behaviour. Too much control for something that runs once.
+    // Main should handle the qApp since we dont have a dedicated "application" class
+    if (aDB()->commit(pilot)) {
+        qApp->quit();
+        QProcess::startDetached(qApp->arguments()[0], qApp->arguments());
     } else {
-        ASettings::write("userdata/lastname", ui->lastnameLineEdit->text());
-        ASettings::write(QStringLiteral("userdata/firstname"), ui->firstnameLineEdit->text());
-        ASettings::write(QStringLiteral("userdata/employeeid"), ui->employeeidLineEdit->text());
-        ASettings::write(QStringLiteral("userdata/phone"), ui->phoneLineEdit->text());
-        ASettings::write(QStringLiteral("userdata/email"), ui->emailLineEdit->text());
-
-        ASettings::write(QStringLiteral("flightlogging/function"), ui->functionComboBox->currentText());
-        ASettings::write(QStringLiteral("flightlogging/approach"), ui->approachComboBox->currentText());
-        ASettings::write(QStringLiteral("flightlogging/nightlogging"), ui->nightComboBox->currentIndex());
-        ASettings::write(QStringLiteral("flightlogging/logIfr"), ui->rulesComboBox->currentIndex());
-        ASettings::write(QStringLiteral("flightlogging/flightnumberPrefix"), ui->prefixLineEdit->text());
-
-        ASettings::write(QStringLiteral("flightlogging/numberTakeoffs"), 1);
-        ASettings::write(QStringLiteral("flightlogging/numberLandings"), 1);
-        ASettings::write(QStringLiteral("flightlogging/popupCalendar"), true);
-        ASettings::write(QStringLiteral("flightlogging/pilotFlying"), true);
-
-
-        QMap<QString, QVariant> data;
-        switch (ui->aliasComboBox->currentIndex()) {
-        case 0:
-            ASettings::write(QStringLiteral("userdata/displayselfas"), ui->aliasComboBox->currentIndex());
-            break;
-        case 1:
-            ASettings::write(QStringLiteral("userdata/displayselfas"), ui->aliasComboBox->currentIndex());
-            break;
-        case 2:
-            ASettings::write(QStringLiteral("userdata/displayselfas"), ui->aliasComboBox->currentIndex());
-            break;
-        default:
-            break;
-        }
-        data.insert(QStringLiteral("lastname"), ui->lastnameLineEdit->text());
-        data.insert(QStringLiteral("firstname"), ui->firstnameLineEdit->text());
-        data.insert(QStringLiteral("alias"), QStringLiteral("self"));
-        data.insert(QStringLiteral("employeeid"), ui->employeeidLineEdit->text());
-        data.insert(QStringLiteral("phone"), ui->phoneLineEdit->text());
-        data.insert(QStringLiteral("email"), ui->emailLineEdit->text());
-
-        if (!setupDatabase()) {
-            QMessageBox message_box(this);
-            message_box.setText(QStringLiteral("Errors have ocurred creating the database. "
-                                               "Without a working database The application will "
-                                               "not be usable."));
-        }
-        ASettings::write(QStringLiteral("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)) {
-            QDialog::accept();
-        } else {
-            QMessageBox message_box(this);
-            message_box.setText(QStringLiteral("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::setupDatabase()
 {
-    //check if template dir exists and create if needed.
-
     QMessageBox confirm;
     confirm.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
     confirm.setDefaultButton(QMessageBox::No);
     confirm.setIcon(QMessageBox::Question);
-    confirm.setWindowTitle(QStringLiteral("Create Database"));
-    confirm.setText(QStringLiteral("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) {// to do: else use local copy that shipped with the release
-        //close database connection
-        aDB()->disconnect();
-        // back up old database if exists
-        auto oldDatabase = QFile(AStandardPaths::getPath(QStandardPaths::AppDataLocation)
-                                 % QStringLiteral("/logbook.db"));
-
-        if (oldDatabase.exists()) {
-            auto dateString = QDateTime::currentDateTime().toString(Qt::ISODate);
-            DEB << "Backing up old database as: " << "logbook-backup-" + dateString;
-            if (!oldDatabase.rename(AStandardPaths::getPath(QStandardPaths::AppDataLocation)
-                                    % QStringLiteral("/logbook-backup-") % dateString
-                                    % QStringLiteral(".db"))) {
-                DEB << "Warning: Creating backup of old database has failed.";
-            }
-        }
-        // re-connect and create new database
-        aDB()->connect();
+    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");
 
-        if(! ADataBaseSetup::createDatabase()) {
-            DEB << "Error creating Database";
-            return false;
-        }
-        if(! ADataBaseSetup::setupTemplateDir()) {
-                    DEB << "Error setting up template dir";
-                    return false;
-                }
-        if(! ADataBaseSetup::downloadTemplates()) {
-                    DEB << "Download Error";
-                    return false;
-                }
-        if(! ADataBaseSetup::importDefaultData()) {
-                    DEB << "Error importing default data";
-                    return false;
-                }
-        ASettings::write(QStringLiteral("setup/setup_complete"), true);
-    }
+    int reply = confirm.exec();
+    if (reply == QMessageBox::Yes)
+        ADataBaseSetup::downloadTemplates();
+
+    aDB()->disconnect();
+    ADataBaseSetup::backupOldData();
+    aDB()->connect();
+
+    if(!ADataBaseSetup::createDatabase())
+        return false;
+    if(!ADataBaseSetup::importDefaultData())
+        return false;
+    ASettings::write("setup/setup_complete", true);
     return true;
 }
 

+ 1 - 1
src/gui/widgets/debugwidget.cpp

@@ -169,7 +169,7 @@ void DebugWidget::on_importCsvPushButton_clicked()
 
 void DebugWidget::on_debugPushButton_clicked()
 {
-    DEB << AStandardPaths::getPaths()[QStandardPaths::AppDataLocation];
+    DEB << AStandardPaths::getPaths()[AStandardPaths::Database	];
 }
 
 /* //Comparing two functions template