123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- /*
- *openPilotLog - A FOSS Pilot Logbook Application
- *Copyright (C) 2020-2021 Felix Turowsky
- *
- *This program is free software: you can redistribute it and/or modify
- *it under the terms of the GNU General Public License as published by
- *the Free Software Foundation, either version 3 of the License, or
- *(at your option) any later version.
- *
- *This program is distributed in the hope that it will be useful,
- *but WITHOUT ANY WARRANTY; without even the implied warranty of
- *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- *GNU General Public License for more details.
- *
- *You should have received a copy of the GNU General Public License
- *along with this program. If not, see <https://www.gnu.org/licenses/>.
- */
- #include "firstrundialog.h"
- #include "ui_firstrundialog.h"
- #include "src/functions/alog.h"
- #include "src/database/adatabase.h"
- #include "src/database/adatabasesetup.h"
- #include "src/classes/apilotentry.h"
- #include "src/classes/adownload.h"
- #include "src/classes/asettings.h"
- #include "src/opl.h"
- #include <QErrorMessage>
- #include "src/classes/astyle.h"
- FirstRunDialog::FirstRunDialog(QWidget *parent) :
- QDialog(parent),
- ui(new Ui::FirstRunDialog)
- {
- ui->setupUi(this);
- ui->stackedWidget->setCurrentIndex(0);
- ui->lastnameLineEdit->setFocus();
- ui->previousPushButton->setEnabled(false);
- ui->logoLabel->setPixmap(QPixmap(Opl::Assets::LOGO));
- // approach Combo Box
- for (const auto &approach : Opl::ApproachTypes){
- ui->approachComboBox->addItem(approach);
- }
- // Style combo box
- const QSignalBlocker blocker_style(ui->styleComboBox);
- ui->styleComboBox->addItems(AStyle::styles);
- for (const auto &style_sheet : AStyle::styleSheets) {
- ui->styleComboBox->addItem(style_sheet.styleSheetName);
- }
- ui->styleComboBox->addItem(QStringLiteral("Dark-Palette"));
- ui->styleComboBox->model()->sort(0);
- ui->styleComboBox->setCurrentText(AStyle::defaultStyle);
- // Set Date Edits for currencies
- for (const auto date_edit : this->findChildren<QDateEdit *>())
- date_edit->setDate(QDate::currentDate());
- }
- FirstRunDialog::~FirstRunDialog()
- {
- delete ui;
- }
- void FirstRunDialog::on_previousPushButton_clicked()
- {
- auto current_index = ui->stackedWidget->currentIndex();
- switch (current_index) {
- case 0:
- return;
- case 1:
- ui->previousPushButton->setEnabled(false);
- break;
- case 2:
- ui->nextPushButton->setText(tr("Next"));
- break;
- }
- ui->stackedWidget->setCurrentIndex(current_index - 1);
- }
- void FirstRunDialog::on_nextPushButton_clicked()
- {
- auto current_index = ui->stackedWidget->currentIndex();
- switch (current_index) {
- case 0:
- if(ui->firstnameLineEdit->text().isEmpty()
- || ui->lastnameLineEdit->text().isEmpty())
- {
- QMessageBox(QMessageBox::Warning, tr("Error"),
- tr("Please enter first and last name")
- ).exec();
- return;
- }
- ui->previousPushButton->setEnabled(true);
- break;
- case 3:
- ui->nextPushButton->setText(tr("Done"));
- break;
- case 4:
- if(!finishSetup())
- QDialog::reject();
- else
- QDialog::accept();
- return;
- }
- ui->stackedWidget->setCurrentIndex(current_index + 1);
- }
- bool FirstRunDialog::finishSetup()
- {
- writeSettings();
- QFileInfo database_file(AStandardPaths::directory(AStandardPaths::Database).
- absoluteFilePath(QStringLiteral("logbook.db")));
- if (database_file.exists()) {
- QMessageBox message_box(QMessageBox::Critical, tr("Database found"),
- tr("Warning."
- "An existing database file has been detected on your system.<br>"
- "A backup copy of the existing database will be created at this location:<br>"
- "%1").arg(
- QDir::cleanPath(AStandardPaths::directory(AStandardPaths::Backup).canonicalPath())));
- message_box.exec();
- ADataBaseSetup::backupOldData();
- }
- if (!aDB->connect()) {
- QMessageBox message_box(QMessageBox::Critical, tr("Database setup failed"),
- tr("Errors have ocurred creating the database."
- "Without a working database The application will not be usable.<br>"
- "The following error has ocurred:<br>"
- "Database: Unable to connect"));
- message_box.exec();
- return false;
- }
- if (!setupDatabase()) {
- QMessageBox message_box(QMessageBox::Critical, tr("Database setup failed"),
- tr("Errors have ocurred creating the database."
- "Without a working database The application will not be usable.<br>"
- "The following error has ocurred:<br>%1"
- ).arg(aDB->lastError.text()));
- message_box.exec();
- return false;
- }
- if (!createUserEntry()) {
- QMessageBox message_box(QMessageBox::Critical, tr("Database setup failed"),
- tr("Unable to execute database query<br>"
- "The following error has occured:<br>%1"
- ).arg(aDB->lastError.text()));
- message_box.exec();
- return false;
- }
- if (!writeCurrencies()) {
- QMessageBox message_box(QMessageBox::Critical, tr("Database setup failed"),
- tr("Unable to execute database query<br>"
- "The following error has occured:<br>%1"
- ).arg(aDB->lastError.text()));
- message_box.exec();
- return false;
- }
- aDB->disconnect(); // connection will be re-established by main()
- return true;
- }
- void FirstRunDialog::writeSettings()
- {
- ASettings::resetToDefaults();
- ASettings::write(ASettings::FlightLogging::Function, ui->functionComboBox->currentText());
- ASettings::write(ASettings::FlightLogging::Approach, ui->approachComboBox->currentIndex());
- switch (ui->nightComboBox->currentIndex()) {
- case 0:
- ASettings::write(ASettings::FlightLogging::NightLoggingEnabled, true);
- break;
- case 1:
- ASettings::write(ASettings::FlightLogging::NightLoggingEnabled, false);
- break;
- default:
- ASettings::write(ASettings::FlightLogging::NightLoggingEnabled, true);
- break;
- }
- switch (ui->nightRulesComboBox->currentIndex()) {
- case 0:
- ASettings::write(ASettings::FlightLogging::NightAngle, 6);
- break;
- case 1:
- ASettings::write(ASettings::FlightLogging::NightAngle, 0);
- break;
- }
- ASettings::write(ASettings::FlightLogging::LogIFR, ui->rulesComboBox->currentIndex());
- ASettings::write(ASettings::FlightLogging::FlightNumberPrefix, ui->prefixLineEdit->text());
- ASettings::write(ASettings::FlightLogging::FlightTimeFormat, Opl::Time::Default);
- ASettings::write(ASettings::UserData::DisplaySelfAs, ui->aliasComboBox->currentIndex());
- ASettings::write(ASettings::Main::LogbookView, ui->logbookViewComboBox->currentIndex());
- switch (ui->currWarningCheckBox->checkState()) {
- case Qt::CheckState::Checked:
- ASettings::write(ASettings::UserData::CurrWarningEnabled, true);
- break;
- case Qt::CheckState::Unchecked:
- ASettings::write(ASettings::UserData::CurrWarningEnabled, false);
- break;
- default:
- break;
- }
- ASettings::write(ASettings::UserData::CurrWarningThreshold, ui->currWarningThresholdSpinBox->value());
- ASettings::write(ASettings::Main::Style, ui->styleComboBox->currentText());
- QSettings settings;
- settings.sync();
- }
- bool FirstRunDialog::setupDatabase()
- {
- QMessageBox confirm(QMessageBox::Question, tr("Create Database"),
- tr("We are now going to create the database.<br>"
- "Would you like to download the latest database information?"
- "<br>(Recommended, Internet connection required)"),
- QMessageBox::Yes | QMessageBox::No, this);
- confirm.setDefaultButton(QMessageBox::No);
- if (confirm.exec() == QMessageBox::Yes) {
- useRessourceData = false;
- if (!ADataBaseSetup::downloadTemplates()) {
- QMessageBox message_box(this);
- message_box.setText(tr("Downloading latest data has failed.<br><br>Using local data instead."));
- message_box.exec();
- useRessourceData = true; // fall back
- }
- } else {
- useRessourceData = true;
- }
- if(!ADataBaseSetup::createDatabase())
- return false;
- aDB->updateLayout();
- if(!ADataBaseSetup::importDefaultData(useRessourceData))
- return false;
- aDB->updateLayout();
- return true;
- }
- bool FirstRunDialog::createUserEntry()
- {
- QMap<QString, QVariant> data;
- data.insert(Opl::Db::PILOTS_LASTNAME, ui->lastnameLineEdit->text());
- data.insert(Opl::Db::PILOTS_FIRSTNAME, ui->firstnameLineEdit->text());
- data.insert(Opl::Db::PILOTS_ALIAS, QStringLiteral("self"));
- data.insert(Opl::Db::PILOTS_EMPLOYEEID, ui->employeeidLineEdit->text());
- data.insert(Opl::Db::PILOTS_PHONE, ui->phoneLineEdit->text());
- data.insert(Opl::Db::PILOTS_EMAIL, ui->emailLineEdit->text());
- auto pilot = APilotEntry(1);
- pilot.setData(data);
- return aDB->commit(pilot);
- }
- bool FirstRunDialog::writeCurrencies()
- {
- const QList<QPair<ACurrencyEntry::CurrencyName, QDateEdit*>> currencies_list = {
- {ACurrencyEntry::CurrencyName::Licence, ui->currLicDateEdit},
- {ACurrencyEntry::CurrencyName::TypeRating, ui->currTrDateEdit},
- {ACurrencyEntry::CurrencyName::LineCheck, ui->currLckDateEdit},
- {ACurrencyEntry::CurrencyName::Medical, ui->currMedDateEdit},
- {ACurrencyEntry::CurrencyName::Custom1, ui->currCustom1DateEdit},
- {ACurrencyEntry::CurrencyName::Custom2, ui->currCustom1DateEdit},
- };
- QDate today = QDate::currentDate();
- for (const auto &pair : currencies_list) {
- // only write dates that have been edited
- if (pair.second->date() != today) {
- ACurrencyEntry entry(pair.first, pair.second->date());
- if (!aDB->commit(entry))
- return false;
- }
- }
- return true;
- }
- void FirstRunDialog::reject()
- {
- QMessageBox confirm(QMessageBox::Critical,
- tr("Setup incomplete"),
- tr("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();
- }
- }
- void FirstRunDialog::on_styleComboBox_currentTextChanged(const QString &new_style_setting)
- {
- DEB << "style selected:"<<new_style_setting;
- if (new_style_setting == QLatin1String("Dark-Palette")) {
- //DEB << "Palette";
- AStyle::setStyle(AStyle::darkPalette());
- return;
- }
- for (const auto &style_name : AStyle::styles) {
- if (new_style_setting == style_name) {
- //DEB << "style";
- AStyle::setStyle(style_name);
- return;
- }
- }
- for (const auto &style_sheet : AStyle::styleSheets) {
- if (new_style_setting == style_sheet.styleSheetName) {
- //DEB << "stylesheet";
- AStyle::setStyle(style_sheet);
- return;
- }
- }
- }
- void FirstRunDialog::on_currWarningCheckBox_stateChanged(int arg1)
- {
- switch (arg1) {
- case Qt::CheckState::Checked:
- ASettings::write(ASettings::UserData::CurrWarningEnabled, true);
- break;
- case Qt::CheckState::Unchecked:
- ASettings::write(ASettings::UserData::CurrWarningEnabled, false);
- break;
- default:
- break;
- }
- ASettings::write(ASettings::UserData::CurrWarningThreshold, arg1);
- }
- void FirstRunDialog::on_currWarningThresholdSpinBox_valueChanged(int arg1)
- {
- ASettings::write(ASettings::UserData::CurrWarningThreshold, arg1);
- }
- void FirstRunDialog::on_currCustom1LineEdit_editingFinished()
- {
- ASettings::write(ASettings::UserData::Custom1CurrencyName, ui->currCustom1LineEdit->text());
- }
- void FirstRunDialog::on_currCustom2LineEdit_editingFinished()
- {
- ASettings::write(ASettings::UserData::Custom2CurrencyName, ui->currCustom2LineEdit->text());
- }
|