|
@@ -6,15 +6,17 @@
|
|
|
#include "src/classes/apilotentry.h"
|
|
|
#include "src/classes/adownload.h"
|
|
|
#include "src/classes/asettings.h"
|
|
|
-const auto TEMPLATE_URL = QLatin1String("https://raw.githubusercontent.com/fiffty-50/openpilotlog/develop/assets/database/templates/");
|
|
|
+#include "src/classes/astandardpaths.h"
|
|
|
+#include <QErrorMessage>
|
|
|
|
|
|
FirstRunDialog::FirstRunDialog(QWidget *parent) :
|
|
|
QDialog(parent),
|
|
|
ui(new Ui::FirstRunDialog)
|
|
|
{
|
|
|
ui->setupUi(this);
|
|
|
- ui->tabWidget->setCurrentIndex(0);
|
|
|
+ ui->stackedWidget->setCurrentIndex(0);
|
|
|
ui->lastnameLineEdit->setFocus();
|
|
|
+ ui->previousPushButton->setEnabled(false);
|
|
|
ui->nightComboBox->setCurrentIndex(1);
|
|
|
|
|
|
auto *themeGroup = new QButtonGroup;
|
|
@@ -33,149 +35,141 @@ FirstRunDialog::~FirstRunDialog()
|
|
|
|
|
|
void FirstRunDialog::on_previousPushButton_clicked()
|
|
|
{
|
|
|
- if(ui->tabWidget->currentIndex()>0)
|
|
|
- ui->tabWidget->setCurrentIndex(ui->tabWidget->currentIndex()-1);
|
|
|
+ auto current_idx = ui->stackedWidget->currentIndex();
|
|
|
+ switch (current_idx) {
|
|
|
+ case 0:
|
|
|
+ return;
|
|
|
+ case 1:
|
|
|
+ ui->previousPushButton->setEnabled(false);
|
|
|
+ break;
|
|
|
+ case 2:
|
|
|
+ ui->nextPushButton->setText(QStringLiteral("Next"));
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ ui->stackedWidget->setCurrentIndex(current_idx - 1);
|
|
|
+
|
|
|
}
|
|
|
|
|
|
void FirstRunDialog::on_nextPushButton_clicked()
|
|
|
{
|
|
|
- if(ui->tabWidget->currentIndex()<2){
|
|
|
- ui->tabWidget->setCurrentIndex(ui->tabWidget->currentIndex()+1);
|
|
|
- } else {
|
|
|
- emit ui->finishButton->clicked();
|
|
|
+ auto current_idx = ui->stackedWidget->currentIndex();
|
|
|
+ // [G]: per index do appropriate error handling
|
|
|
+ switch (current_idx) {
|
|
|
+ case 0:
|
|
|
+ if(ui->firstnameLineEdit->text().isEmpty()
|
|
|
+ || ui->lastnameLineEdit->text().isEmpty())
|
|
|
+ {
|
|
|
+ QMessageBox(QMessageBox::Warning, QStringLiteral("Error"),
|
|
|
+ QStringLiteral("Please enter first and last name")
|
|
|
+ ).exec();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ ui->previousPushButton->setEnabled(true);
|
|
|
+ break;
|
|
|
+ case 1:
|
|
|
+ ui->nextPushButton->setText(QStringLiteral("Done"));
|
|
|
+ break;
|
|
|
+ case 2:
|
|
|
+ if(!finish())
|
|
|
+ QDialog::reject();
|
|
|
+ else
|
|
|
+ QDialog::accept();
|
|
|
+ return;
|
|
|
}
|
|
|
-
|
|
|
+ ui->stackedWidget->setCurrentIndex(current_idx + 1);
|
|
|
}
|
|
|
|
|
|
void FirstRunDialog::on_themeGroup_toggled(int id)
|
|
|
{
|
|
|
- ASettings::write("main/theme", id);
|
|
|
+ ASettings::write(ASettings::Main::Theme, id);
|
|
|
}
|
|
|
|
|
|
-void FirstRunDialog::on_finishButton_clicked()
|
|
|
+bool FirstRunDialog::finish()
|
|
|
{
|
|
|
- if(ui->lastnameLineEdit->text().isEmpty() || ui->firstnameLineEdit->text().isEmpty()){
|
|
|
- auto mb = new QMessageBox(this);
|
|
|
- mb->setText("You have to enter a valid first and last name for the logbook.");
|
|
|
- mb->show();
|
|
|
- } 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.");
|
|
|
- }
|
|
|
+ ASettings::write(ASettings::UserData::LastName, ui->lastnameLineEdit->text());
|
|
|
+ ASettings::write(ASettings::UserData::FirstName, ui->firstnameLineEdit->text());
|
|
|
+ ASettings::write(ASettings::UserData::EmployeeID, ui->employeeidLineEdit->text());
|
|
|
+ ASettings::write(ASettings::UserData::Phone, ui->phoneLineEdit->text());
|
|
|
+ ASettings::write(ASettings::UserData::Email, ui->emailLineEdit->text());
|
|
|
+
|
|
|
+ ASettings::write(ASettings::FlightLogging::Function, ui->functionComboBox->currentText());
|
|
|
+ ASettings::write(ASettings::FlightLogging::Approach, ui->approachComboBox->currentText());
|
|
|
+ ASettings::write(ASettings::FlightLogging::NightLogging, ui->nightComboBox->currentIndex());
|
|
|
+ ASettings::write(ASettings::FlightLogging::LogIFR, ui->rulesComboBox->currentIndex());
|
|
|
+ ASettings::write(ASettings::FlightLogging::FlightNumberPrefix, ui->prefixLineEdit->text());
|
|
|
+ ASettings::write(ASettings::FlightLogging::NumberTakeoffs, 1);
|
|
|
+ ASettings::write(ASettings::FlightLogging::NumberLandings, 1);
|
|
|
+ ASettings::write(ASettings::FlightLogging::PopupCalendar, true);
|
|
|
+ ASettings::write(ASettings::FlightLogging::PilotFlying, true);
|
|
|
+
|
|
|
+ QMap<QString, QVariant> data;
|
|
|
+ ASettings::write(ASettings::UserData::DisplaySelfAs, ui->aliasComboBox->currentIndex());
|
|
|
+ data.insert(ASettings::stringOfKey(ASettings::UserData::LastName), ui->lastnameLineEdit->text());
|
|
|
+ data.insert(ASettings::stringOfKey(ASettings::UserData::FirstName), ui->firstnameLineEdit->text());
|
|
|
+ data.insert(ASettings::stringOfKey(ASettings::UserData::Alias), "self");
|
|
|
+ data.insert(ASettings::stringOfKey(ASettings::UserData::EmployeeID), ui->employeeidLineEdit->text());
|
|
|
+ data.insert(ASettings::stringOfKey(ASettings::UserData::Phone), ui->phoneLineEdit->text());
|
|
|
+ data.insert(ASettings::stringOfKey(ASettings::UserData::Email), ui->emailLineEdit->text());
|
|
|
+
|
|
|
+ auto db_fail_msg_box = QMessageBox(QMessageBox::Critical, QStringLiteral("Database setup failed"),
|
|
|
+ QStringLiteral("Errors have ocurred creating the database."
|
|
|
+ "Without a working database The application will not be usable."));
|
|
|
+ // [G]: Im abit confused on the logic here
|
|
|
+ // why do you write setup complete twice?
|
|
|
+ if (!setupDatabase()) {
|
|
|
+ db_fail_msg_box.exec();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ aDB()->disconnect(); // reset db connection to refresh layout after initial setup.
|
|
|
+ aDB()->connect();
|
|
|
|
|
|
+ auto pilot = APilotEntry(1);
|
|
|
+ pilot.setData(data);
|
|
|
+ if(!aDB()->commit(pilot)){
|
|
|
+ db_fail_msg_box.exec();
|
|
|
+ return false;
|
|
|
}
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
-bool FirstRunDialog::finishSetup()
|
|
|
+bool FirstRunDialog::setupDatabase()
|
|
|
{
|
|
|
-
|
|
|
- //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);
|
|
|
+ auto confirm = QMessageBox(QMessageBox::Question, QStringLiteral("Create Database"),
|
|
|
+ QStringLiteral("We are now going to create the database.<br>" // [G]: Why both <br> and \n ?
|
|
|
+ "Would you like to download the latest database information?"
|
|
|
+ "<br>(Recommended, Internet connection required)"),
|
|
|
+ QMessageBox::Yes | QMessageBox::No, this);
|
|
|
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();
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
- //close database connection
|
|
|
- aDB()->disconnect();
|
|
|
+ if (confirm.exec() == QMessageBox::Yes)
|
|
|
+ ADataBaseSetup::downloadTemplates();
|
|
|
|
|
|
- // 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
|
|
|
+ aDB()->disconnect();
|
|
|
+ ADataBaseSetup::backupOldData();
|
|
|
aDB()->connect();
|
|
|
|
|
|
- if (ADataBaseSetup::createDatabase()) {
|
|
|
- if (ADataBaseSetup::importDefaultData()) {
|
|
|
- ASettings::write("setup/setup_complete", true);
|
|
|
- return true;
|
|
|
- } else {
|
|
|
- return false;
|
|
|
- }
|
|
|
- } else {
|
|
|
+ // [F]: todo: handle unsuccessful steps
|
|
|
+ // [G]: only two / for comments
|
|
|
+ // three are shorthand for documentation
|
|
|
+ if(!ADataBaseSetup::createDatabase())
|
|
|
return false;
|
|
|
- }
|
|
|
+ if(!ADataBaseSetup::importDefaultData())
|
|
|
+ return false;
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
+void FirstRunDialog::reject()
|
|
|
+{
|
|
|
+ auto confirm = QMessageBox(QMessageBox::Critical,
|
|
|
+ QStringLiteral("Setup incomplete"),
|
|
|
+ QStringLiteral("Without completing the initial setup"
|
|
|
+ " you cannot use the application.<br><br>"
|
|
|
+ "Quit anyway?"),
|
|
|
+ QMessageBox::Yes | QMessageBox::No, this);
|
|
|
+ confirm.setDefaultButton(QMessageBox::No);
|
|
|
|
|
|
+ if (confirm.exec() == QMessageBox::Yes) {
|
|
|
+ DEB << "rejected.";
|
|
|
+ QDialog::reject();
|
|
|
+ }
|
|
|
+}
|