firstrundialog.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. *openPilotLog - A FOSS Pilot Logbook Application
  3. *Copyright (C) 2020-2021 Felix Turowsky
  4. *
  5. *This program is free software: you can redistribute it and/or modify
  6. *it under the terms of the GNU General Public License as published by
  7. *the Free Software Foundation, either version 3 of the License, or
  8. *(at your option) any later version.
  9. *
  10. *This program is distributed in the hope that it will be useful,
  11. *but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. *GNU General Public License for more details.
  14. *
  15. *You should have received a copy of the GNU General Public License
  16. *along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. #include "firstrundialog.h"
  19. #include "ui_firstrundialog.h"
  20. #include "src/testing/adebug.h"
  21. #include "src/database/adatabase.h"
  22. #include "src/database/adatabasesetup.h"
  23. #include "src/classes/apilotentry.h"
  24. #include "src/classes/adownload.h"
  25. #include "src/classes/asettings.h"
  26. #include "src/oplconstants.h"
  27. #include <QErrorMessage>
  28. FirstRunDialog::FirstRunDialog(QWidget *parent) :
  29. QDialog(parent),
  30. ui(new Ui::FirstRunDialog)
  31. {
  32. ui->setupUi(this);
  33. ui->stackedWidget->setCurrentIndex(0);
  34. ui->lastnameLineEdit->setFocus();
  35. ui->previousPushButton->setEnabled(false);
  36. ui->nightComboBox->setCurrentIndex(1);
  37. for (const auto &approach : Opl::ApproachTypes){
  38. ui->approachComboBox->addItem(approach);
  39. }
  40. }
  41. FirstRunDialog::~FirstRunDialog()
  42. {
  43. delete ui;
  44. }
  45. void FirstRunDialog::on_previousPushButton_clicked()
  46. {
  47. auto current_idx = ui->stackedWidget->currentIndex();
  48. switch (current_idx) {
  49. case 0:
  50. return;
  51. case 1:
  52. ui->previousPushButton->setEnabled(false);
  53. break;
  54. case 2:
  55. ui->nextPushButton->setText(tr("Next"));
  56. break;
  57. }
  58. ui->stackedWidget->setCurrentIndex(current_idx - 1);
  59. }
  60. void FirstRunDialog::on_nextPushButton_clicked()
  61. {
  62. auto current_idx = ui->stackedWidget->currentIndex();
  63. switch (current_idx) {
  64. case 0:
  65. if(ui->firstnameLineEdit->text().isEmpty()
  66. || ui->lastnameLineEdit->text().isEmpty())
  67. {
  68. QMessageBox(QMessageBox::Warning, tr("Error"),
  69. tr("Please enter first and last name")
  70. ).exec();
  71. return;
  72. }
  73. ui->previousPushButton->setEnabled(true);
  74. break;
  75. case 1:
  76. ui->nextPushButton->setText(tr("Done"));
  77. break;
  78. case 2:
  79. if(!finishSetup())
  80. QDialog::reject();
  81. else
  82. QDialog::accept();
  83. return;
  84. }
  85. ui->stackedWidget->setCurrentIndex(current_idx + 1);
  86. }
  87. bool FirstRunDialog::finishSetup()
  88. {
  89. writeSettings();
  90. if (!setupDatabase()) {
  91. QMessageBox message_box(QMessageBox::Critical, tr("Database setup failed"),
  92. tr("Errors have ocurred creating the database."
  93. "Without a working database The application will not be usable.<br>"
  94. "The following error has ocurred:<br>%1"
  95. ).arg(aDB->lastError.text()));
  96. message_box.exec();
  97. return false;
  98. }
  99. if (!createUserEntry()) {
  100. QMessageBox message_box(QMessageBox::Critical, tr("Database setup failed"),
  101. tr("Unable to execute database query<br>"
  102. "The following error has occured:<br>%1"
  103. ).arg(aDB->lastError.text()));
  104. message_box.exec();
  105. return false;
  106. }
  107. return true;
  108. }
  109. void FirstRunDialog::writeSettings()
  110. {
  111. ASettings::write(ASettings::Main::UseSystemFont, true);
  112. ASettings::write(ASettings::Main::LogbookView, 0);
  113. ASettings::write(ASettings::FlightLogging::Function, ui->functionComboBox->currentText());
  114. ASettings::write(ASettings::FlightLogging::Approach, ui->approachComboBox->currentIndex());
  115. ASettings::write(ASettings::FlightLogging::NightLogging, ui->nightComboBox->currentIndex());
  116. ASettings::write(ASettings::FlightLogging::LogIFR, ui->rulesComboBox->currentIndex());
  117. ASettings::write(ASettings::FlightLogging::FlightNumberPrefix, ui->prefixLineEdit->text());
  118. ASettings::write(ASettings::FlightLogging::NumberTakeoffs, 1);
  119. ASettings::write(ASettings::FlightLogging::NumberLandings, 1);
  120. ASettings::write(ASettings::FlightLogging::PopupCalendar, true);
  121. ASettings::write(ASettings::FlightLogging::PilotFlying, true);
  122. ASettings::write(ASettings::FlightLogging::FlightTimeFormat, Opl::Time::Default);
  123. ASettings::write(ASettings::UserData::DisplaySelfAs, ui->aliasComboBox->currentIndex());
  124. ASettings::write(ASettings::UserData::ShowToLgdCurrency, true);
  125. // To Do: UI option
  126. ASettings::write(ASettings::UserData::FtlWarningThreshold, 0.8);
  127. ASettings::write(ASettings::UserData::CurrWarningEnabled, true);
  128. ASettings::write(ASettings::UserData::CurrWarningThreshold, 30);
  129. }
  130. bool FirstRunDialog::setupDatabase()
  131. {
  132. QMessageBox confirm(QMessageBox::Question, tr("Create Database"),
  133. tr("We are now going to create the database.<br>"
  134. "Would you like to download the latest database information?"
  135. "<br>(Recommended, Internet connection required)"),
  136. QMessageBox::Yes | QMessageBox::No, this);
  137. confirm.setDefaultButton(QMessageBox::No);
  138. if (confirm.exec() == QMessageBox::Yes) {
  139. useLocalTemplates = false;
  140. if (!ADataBaseSetup::downloadTemplates()) {
  141. QMessageBox message_box(this);
  142. message_box.setText(tr("Downloading latest data has failed.<br><br>Using local data instead."));
  143. message_box.exec();
  144. useLocalTemplates = true; // fall back
  145. } else {
  146. useLocalTemplates = true;
  147. }
  148. }
  149. aDB->disconnect();
  150. ADataBaseSetup::backupOldData();
  151. aDB->connect();
  152. // [F]: todo: handle unsuccessful steps
  153. if(!ADataBaseSetup::createDatabase())
  154. return false;
  155. aDB->updateLayout();
  156. if(!ADataBaseSetup::importDefaultData(useLocalTemplates))
  157. return false;
  158. aDB->updateLayout();
  159. return true;
  160. }
  161. bool FirstRunDialog::createUserEntry()
  162. {
  163. QMap<QString, QVariant> data;
  164. data.insert(Opl::Db::PILOTS_LASTNAME, ui->lastnameLineEdit->text());
  165. data.insert(Opl::Db::PILOTS_FIRSTNAME, ui->firstnameLineEdit->text());
  166. data.insert(Opl::Db::PILOTS_ALIAS, QStringLiteral("self"));
  167. data.insert(Opl::Db::PILOTS_EMPLOYEEID, ui->employeeidLineEdit->text());
  168. data.insert(Opl::Db::PILOTS_PHONE, ui->phoneLineEdit->text());
  169. data.insert(Opl::Db::PILOTS_EMAIL, ui->emailLineEdit->text());
  170. auto pilot = APilotEntry(1);
  171. pilot.setData(data);
  172. return aDB->commit(pilot);
  173. }
  174. void FirstRunDialog::reject()
  175. {
  176. QMessageBox confirm(QMessageBox::Critical,
  177. tr("Setup incomplete"),
  178. tr("Without completing the initial setup"
  179. " you cannot use the application.<br><br>"
  180. "Quit anyway?"),
  181. QMessageBox::Yes | QMessageBox::No, this);
  182. confirm.setDefaultButton(QMessageBox::No);
  183. if (confirm.exec() == QMessageBox::Yes) {
  184. DEB << "rejected.";
  185. QDialog::reject();
  186. }
  187. }