ソースを参照

Merge branch 'develop-firstrundialog-rework' into develop-firstrundialog-gk

Felix Turowsky 4 年 前
コミット
145c354fb7

+ 14 - 7
main.cpp

@@ -48,13 +48,20 @@ int main(int argc, char *argv[])
     }
 
     ASettings::setup();
-    DEB << "HELLO???????????";
-    FirstRunDialog().exec();
+    //Debug firstrundialog
+    ASettings::write(ASettings::Setup::SetupComplete, false);
+
     aDB()->connect();
-//    if (!ASettings::read("setup/setup_complete").toBool()) {
-//        FirstRunDialog dialog;
-//        dialog.exec();
-//    }
+    if (!ASettings::read(ASettings::Setup::SetupComplete).toBool()) {
+        FirstRunDialog dialog;
+        if(dialog.exec() == QDialog::Accepted) {
+            qApp->quit();
+            QProcess::startDetached(qApp->arguments()[0], qApp->arguments());
+        } else {
+            DEB "First run not accepted. Exiting.";
+            return 0;
+        }
+    }
 
     //Theming
     switch (ASettings::read(ASettings::Main::Theme).toInt()) {
@@ -79,7 +86,7 @@ int main(int argc, char *argv[])
     }
 
     //sqlite does not deal well with multiple connections, ensure only one instance is running
-    ARunGuard guard("opl_single_key");
+    ARunGuard guard(QStringLiteral("opl_single_key"));
         if ( !guard.tryToRun() ){
             DEB << "Another Instance is already running. Exiting.";
             return 0;

+ 7 - 6
src/database/adatabasesetup.cpp

@@ -19,7 +19,7 @@
 #include "src/database/adatabase.h"
 #include "src/testing/adebug.h"
 #include "src/functions/areadcsv.h"
-
+#include "src/astandardpaths.h"
 #include "src/classes/adownload.h"
 
 // Statements for creation of database tables, Revision 15
@@ -281,16 +281,14 @@ bool ADataBaseSetup::createDatabase()
 
 bool ADataBaseSetup::downloadTemplates()
 {
-    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(linkStub + table + ".csv"));
-        dl->setFileName(template_dir.filePath(table + ".csv"));
+        dl->setTarget(QUrl(TEMPLATE_URL % table % QStringLiteral(".csv")));
+        dl->setFileName(template_dir.filePath(table % QStringLiteral(".csv")));
         dl->download();
         loop.exec(); // event loop waits for download done signal before allowing loop to continue
         dl->deleteLater();
@@ -323,7 +321,10 @@ bool ADataBaseSetup::importDefaultData()
             DEB << "Error: " << query.lastError().text();
         }
         //fill with data from csv
-        if (!commitData(aReadCsv("data/templates/" + table + ".csv"), table)) {
+        if (!commitData(aReadCsv(AStandardPaths::getPath(AStandardPaths::Templates)
+                                 % QLatin1Char('/')
+                                 % table % QStringLiteral(".csv")),
+                        table)) {
             DEB << "Error importing data.";
             return false;
         }

+ 2 - 0
src/database/adatabasesetup.h

@@ -19,6 +19,8 @@
 #define DBSETUP_H
 
 #include <QCoreApplication>
+#include <QStringBuilder>
+#include <QEventLoop>
 
 const auto TEMPLATE_URL = QStringLiteral("https://raw.githubusercontent.com/fiffty-50/openpilotlog/develop/assets/database/templates/");
 

+ 34 - 15
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/");
+#include "src/astandardpaths.h"
 
 static inline
 void prompt_error_box(QString title, QString text, QWidget* parent = nullptr)
@@ -64,7 +64,8 @@ void FirstRunDialog::on_nextPushButton_clicked()
         if(ui->firstnameLineEdit->text().isEmpty()
            || ui->lastnameLineEdit->text().isEmpty())
         {
-            prompt_error_box(QStringLiteral("Error"), QStringLiteral("Please enter first and last name"));
+            prompt_error_box(QStringLiteral("Error"),
+                             QStringLiteral("Please enter first and last name"));
             return;
         }
         ui->previousPushButton->setEnabled(true);
@@ -86,7 +87,6 @@ void FirstRunDialog::on_themeGroup_toggled(int id)
 
 void FirstRunDialog::finish()
 {
-
     ASettings::write(ASettings::UserData::LastName, ui->lastnameLineEdit->text());
     ASettings::write(ASettings::UserData::FirstName, ui->firstnameLineEdit->text());
     ASettings::write(ASettings::UserData::EmployeeID, ui->employeeidLineEdit->text());
@@ -112,29 +112,31 @@ void FirstRunDialog::finish()
     data.insert(ASettings::stringOfKey(ASettings::UserData::Phone), ui->phoneLineEdit->text());
     data.insert(ASettings::stringOfKey(ASettings::UserData::Email), ui->emailLineEdit->text());
 
-    if (!finishSetup()) {
+    if (!setupDatabase()) {
         QMessageBox message_box(this);
-        message_box.setText("Errors have ocurred creating the database. Without a working database The application will not be usable.");
+        message_box.setText(QStringLiteral("Errors have ocurred creating the database. Without a working database The application will not be usable."));
         message_box.exec();
     }
     ASettings::write(ASettings::Setup::SetupComplete, 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());
+        ASettings::write(QStringLiteral("setup/setup_complete"), true);
+        QDialog::accept();
     } else {
         QMessageBox message_box(this);
-        message_box.setText("Errors have ocurred creating the database. Without a working database The application will not be usable.");
-        message_box.exec();
+        message_box.setText(QStringLiteral("Errors have ocurred creating the database. "
+                                           "Without a working database The application will "
+                                           "not be usable."));
+        QDialog::reject();
     }
 }
 
-bool FirstRunDialog::finishSetup()
+bool FirstRunDialog::setupDatabase()
 {
 
     QMessageBox confirm;
@@ -142,8 +144,10 @@ bool FirstRunDialog::finishSetup()
     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");
+    confirm.setWindowTitle(QStringLiteral("Create Database"));
+    confirm.setText(QStringLiteral("We are now going to create the database.<br>"
+                                   "Would you like to download the latest database information?"
+                                   "<br>(Recommended, Internet connection required)\n"));
 
     int reply = confirm.exec();
     if (reply == QMessageBox::Yes)
@@ -153,6 +157,7 @@ bool FirstRunDialog::finishSetup()
     ADataBaseSetup::backupOldData();
     aDB()->connect();
 
+    ///[F]: to do: handle unsuccessful steps
     if(!ADataBaseSetup::createDatabase())
         return false;
     if(!ADataBaseSetup::importDefaultData())
@@ -161,4 +166,18 @@ bool FirstRunDialog::finishSetup()
     return true;
 }
 
-
+void FirstRunDialog::reject()
+{
+    auto confirm = QMessageBox(this);
+    confirm.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
+    confirm.setDefaultButton(QMessageBox::No);
+    confirm.setIcon(QMessageBox::Critical);
+    confirm.setWindowTitle(QStringLiteral("Setup incomplete"));
+    confirm.setText(QStringLiteral("Without completing the initial setup"
+                                   " you cannot use the application.<br><br>"
+                                   "Quit anyway?"));
+    if (confirm.exec() == QMessageBox::Yes) {
+        DEB << "rejected.";
+        QDialog::reject();
+        }
+}

+ 4 - 3
src/gui/dialogues/firstrundialog.h

@@ -4,6 +4,7 @@
 #include <QDialog>
 #include <QButtonGroup>
 #include <QMessageBox>
+#include <QStringBuilder>
 
 namespace Ui {
 class FirstRunDialog;
@@ -25,14 +26,14 @@ private slots:
 
     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
-    bool finishSetup();
+
+    void reject() override;
+    bool setupDatabase();
     void finish();
 
 };