Browse Source

Bug fixes

- Added a ; in the SQL file used to create the database
- Adjusted control flow in main.cpp to force a re-start of the Application after initial set-up
- Added a null-check for a String containing the user name in the SettingsWidget which could cause crashes
- Added DB functions explicitly designed to set and get the logbook owner.
- set the correct default button for choices in the FirstRunDialog and added a method to initialise the previous experience table
- Added an option in the DebugDialog to trigger the set-up dialog on next application launch
Felix Turowsky 1 year ago
parent
commit
72d2f1dd30

+ 1 - 2
assets/database/database_schema.sql

@@ -132,8 +132,7 @@ CREATE TABLE 'previousExperience' (
         'ldgDay'	INTEGER,
         'ldgNight'	INTEGER,
         'autoland'	INTEGER
-)
-
+);
 DROP VIEW IF EXISTS 'viewDefault';
 CREATE VIEW viewDefault AS  
 SELECT 	flight_id,

+ 17 - 11
main.cpp

@@ -34,10 +34,9 @@
 #include <QTranslator>
 
 /*!
- *  Helper functions that prepare and set up the application
+ * \brief init - Sets up the logging facilities, loads the user settings and sets
+ * up the application style before the MainWindow is instantiated
  */
-namespace Main {
-
 void init()
 {
     LOG << "Setting up / verifying Application Directories...";
@@ -61,17 +60,24 @@ void init()
     //ATranslator::installTranslator(OPL::Translations::English);
 }
 
-bool firstRun()
+/*!
+ * \brief firstRun - is run if the application is run for the first time and launches
+ * the FirstRunDialog which guides the user through the initial set-up process.
+ */
+int firstRun()
 {
     if(FirstRunDialog().exec() == QDialog::Rejected){
         LOG << "Initial setup incomplete or unsuccessfull.";
-        return false;
+        return 1;
     }
+
     Settings::write(Settings::Main::SetupComplete, true);
     LOG << "Initial Setup Completed successfully";
-    return true;
+    QMessageBox mb;
+    mb.setText("Initial set-up has been completed successfully.<br><br>Please re-start the application.");
+    mb.exec();
+    return 0;
 }
-} // namespace Main
 
 int main(int argc, char *argv[])
 {
@@ -80,7 +86,8 @@ int main(int argc, char *argv[])
     QCoreApplication::setOrganizationDomain(ORGDOMAIN);
     QCoreApplication::setApplicationName(APPNAME);
 
-    // Check for another instance already running
+    // Check of another instance of the application is already running, we don't want
+    // different processes writing to the same database
     RunGuard guard(QStringLiteral("opl_single_key"));
     if ( !guard.tryToRun() ){
         LOG << "Another Instance of openPilotLog is already running. Exiting.";
@@ -88,12 +95,11 @@ int main(int argc, char *argv[])
     }
 
     // Set Up the Application
-    Main::init();
+    init();
 
     // Check for First Run and launch Setup Wizard
     if (!Settings::read(Settings::Main::SetupComplete).toBool())
-        if(!Main::firstRun())
-            return 0;
+        return firstRun();
 
     // Create Main Window and set Window Icon acc. to Platform
     MainWindow w;

+ 1 - 1
src/database/database.cpp

@@ -798,7 +798,7 @@ bool Database::resetUserData()
     for (const auto& table : DB->getUserTables()) {
         query.prepare(QLatin1String("DELETE FROM ") + OPL::GLOBALS->getDbTableName(table));
         if (!query.exec()) {
-            DEB << "Error: " << query.lastError().text();
+            lastError = query.lastError();
             return false;
         }
     }

+ 24 - 0
src/database/database.h

@@ -214,6 +214,30 @@ public:
         return OPL::PilotEntry(row_id, data);
     }
 
+    /*!
+     * \brief get the database entry for the logbook owner (self)
+     */
+    inline OPL::PilotEntry getLogbookOwner()
+    {
+        auto data = getRowData(OPL::DbTable::Pilots, 1);
+        data.insert(OPL::PilotEntry::ROWID, 1);
+        return OPL::PilotEntry(1, data);
+    }
+
+    /*!
+     * \brief Set the database entry for the logbook owner (self)
+     */
+    inline bool setLogbookOwner(RowData_T &ownerData)
+    {
+        if(ownerData.value(OPL::PilotEntry::LASTNAME).isNull()) {
+            lastError = QSqlError("Logbook owners last name is mandatory.");
+            return false;
+        }
+
+        ownerData.insert(OPL::PilotEntry::ROWID, 1);
+        return commit(OPL::PilotEntry(1, ownerData));
+    }
+
     /*!
      * \brief retreives a TailEntry from the database. See row class for details.
      */

+ 20 - 4
src/gui/dialogues/firstrundialog.cpp

@@ -16,6 +16,7 @@
  *along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 #include "firstrundialog.h"
+#include "src/database/previousexperienceentry.h"
 #include "ui_firstrundialog.h"
 #include "src/opl.h"
 #include "src/database/database.h"
@@ -177,6 +178,15 @@ bool FirstRunDialog::finishSetup()
         return false;
     }
 
+    if (!setupPreviousExperienceEntry()) {
+        QMessageBox message_box(QMessageBox::Critical, tr("Database setup failed"),
+                                tr("Unable to execute database query<br>"
+                                   "The following error has occured:<br>%1"
+                                   ).arg(DB->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>"
@@ -291,7 +301,7 @@ bool FirstRunDialog::setupDatabase()
                                               "Would you like to download the latest database information?"
                                               "<br>(Recommended, Internet connection required)"),
                                QMessageBox::Yes | QMessageBox::No, this);
-    confirm.setDefaultButton(QMessageBox::No);
+    confirm.setDefaultButton(QMessageBox::Yes);
 
     if (confirm.exec() == QMessageBox::Yes) {
         useRessourceData = false;
@@ -321,7 +331,7 @@ bool FirstRunDialog::setupDatabase()
 
 bool FirstRunDialog::createUserEntry()
 {
-    QHash<QString, QVariant> data;
+    OPL::RowData_T data;
     data.insert(OPL::PilotEntry::LASTNAME,   ui->lastnameLineEdit->text());
     data.insert(OPL::PilotEntry::FIRSTNAME,  ui->firstnameLineEdit->text());
     data.insert(OPL::PilotEntry::ALIAS,      QStringLiteral("self"));
@@ -329,9 +339,15 @@ bool FirstRunDialog::createUserEntry()
     data.insert(OPL::PilotEntry::PHONE,      ui->phoneLineEdit->text());
     data.insert(OPL::PilotEntry::EMAIL,      ui->emailLineEdit->text());
 
-    auto pilot = OPL::PilotEntry(1, data);
+    return DB->setLogbookOwner(data);
+}
 
-    return DB->commit(pilot);
+bool FirstRunDialog::setupPreviousExperienceEntry()
+{
+    OPL::RowData_T prevData;
+    prevData.insert(OPL::PreviousExperienceEntry::TBLK, 0);
+    auto pXpEntry = OPL::PreviousExperienceEntry(1, prevData);
+    return DB->commit(pXpEntry);
 }
 
 bool FirstRunDialog::writeCurrencies()

+ 5 - 0
src/gui/dialogues/firstrundialog.h

@@ -117,6 +117,11 @@ private:
      */
     bool createUserEntry();
 
+    /*!
+     * \brief setupPreviousExperienceEntry - set up a stub database entry for previous experience
+     */
+    bool setupPreviousExperienceEntry();
+
     /*!
      * \brief writeCurrencies - submits the user input to the currencies table in the database
      */

+ 10 - 2
src/gui/widgets/debugwidget.cpp

@@ -16,10 +16,8 @@
  *along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 #include "debugwidget.h"
-#include "src/gui/verification/airportinput.h"
 #include "src/gui/verification/completerprovider.h"
 #include "src/gui/verification/pilotinput.h"
-#include "src/gui/verification/timeinput.h"
 #include "src/testing/importCrewlounge/processaircraft.h"
 #include "src/testing/importCrewlounge/processflights.h"
 #include "src/testing/importCrewlounge/processpilots.h"
@@ -29,6 +27,8 @@
 #include "src/functions/readcsv.h"
 #include "src/database/database.h"
 #include "src/testing/atimer.h"
+#include "src/classes/settings.h"
+
 void DebugWidget::on_debugPushButton_clicked()
 {
     auto rawCsvData = CSV::readCsvAsRows("/home/felix/git/importMCC/assets/data/felix.csv");
@@ -75,6 +75,7 @@ void DebugWidget::on_resetUserTablesPushButton_clicked()
         emit DB->dataBaseUpdated(OPL::DbTable::Any);
     } else
         LOG <<"Errors have occurred. Check console for Debug output. ";
+    Settings::resetToDefaults();
 }
 
 void DebugWidget::on_resetDatabasePushButton_clicked()
@@ -295,3 +296,10 @@ void DebugWidget::on_debug2LineEdit_editingFinished()
 
 }
 
+
+void DebugWidget::on_pushButton_clicked()
+{
+    Settings::resetToDefaults();
+    Settings::write(Settings::Main::SetupComplete, false);
+}
+

+ 2 - 0
src/gui/widgets/debugwidget.h

@@ -57,6 +57,8 @@ private slots:
 
     void on_debug2LineEdit_editingFinished();
 
+    void on_pushButton_clicked();
+
 private:
     Ui::DebugWidget *ui;
 

+ 66 - 59
src/gui/widgets/debugwidget.ui

@@ -24,38 +24,38 @@
        <string>Database</string>
       </attribute>
       <layout class="QGridLayout" name="gridLayout_2">
-       <item row="2" column="3">
-        <widget class="QLabel" name="label_3">
-         <property name="text">
-          <string>Fill a new database with sample entries</string>
-         </property>
-        </widget>
-       </item>
-       <item row="3" column="3">
-        <widget class="QLineEdit" name="importCsvLineEdit">
+       <item row="0" column="0">
+        <widget class="QPushButton" name="resetDatabasePushButton">
          <property name="minimumSize">
           <size>
-           <width>110</width>
+           <width>220</width>
            <height>0</height>
           </size>
          </property>
-         <property name="placeholderText">
-          <string>/path/to/file.csv</string>
+         <property name="text">
+          <string>Reset Database</string>
          </property>
         </widget>
        </item>
-       <item row="4" column="3">
-        <widget class="QLineEdit" name="debugLineEdit"/>
+       <item row="0" column="2">
+        <widget class="QLineEdit" name="branchLineEdit">
+         <property name="text">
+          <string>develop</string>
+         </property>
+         <property name="placeholderText">
+          <string>develop</string>
+         </property>
+        </widget>
        </item>
-       <item row="2" column="0">
-        <widget class="QPushButton" name="fillUserDataPushButton">
+       <item row="5" column="0">
+        <widget class="QPushButton" name="debugPushButton">
          <property name="text">
-          <string>Fill User Table with test data</string>
+          <string>Do Debug Stuff!</string>
          </property>
         </widget>
        </item>
-       <item row="3" column="0">
-        <widget class="QPushButton" name="importCsvPushButton">
+       <item row="4" column="4">
+        <widget class="QPushButton" name="selectCsvPushButton">
          <property name="minimumSize">
           <size>
            <width>110</width>
@@ -63,39 +63,40 @@
           </size>
          </property>
          <property name="text">
-          <string>Import CSV</string>
+          <string>Select File</string>
          </property>
         </widget>
        </item>
-       <item row="4" column="0">
-        <widget class="QPushButton" name="debugPushButton">
+       <item row="0" column="1">
+        <widget class="QLabel" name="branchLabel">
          <property name="text">
-          <string>Do Debug Stuff!</string>
+          <string>branch:</string>
          </property>
         </widget>
        </item>
-       <item row="1" column="3">
-        <widget class="QLabel" name="label_2">
+       <item row="3" column="3">
+        <widget class="QLabel" name="label_3">
          <property name="text">
-          <string>Keep current database but delete entries in pilots, aircraft and flights</string>
+          <string>Fill a new database with sample entries</string>
          </property>
         </widget>
        </item>
-       <item row="1" column="0">
-        <widget class="QPushButton" name="resetUserTablesPushButton">
-         <property name="minimumSize">
-          <size>
-           <width>220</width>
-           <height>0</height>
-          </size>
+       <item row="3" column="0">
+        <widget class="QPushButton" name="fillUserDataPushButton">
+         <property name="text">
+          <string>Fill User Table with test data</string>
          </property>
+        </widget>
+       </item>
+       <item row="0" column="3">
+        <widget class="QLabel" name="label">
          <property name="text">
-          <string>Reset User Tables</string>
+          <string>Backup current database (if exists) and create a new one from scratch.</string>
          </property>
         </widget>
        </item>
-       <item row="3" column="4">
-        <widget class="QPushButton" name="selectCsvPushButton">
+       <item row="4" column="0">
+        <widget class="QPushButton" name="importCsvPushButton">
          <property name="minimumSize">
           <size>
            <width>110</width>
@@ -103,19 +104,12 @@
           </size>
          </property>
          <property name="text">
-          <string>Select File</string>
-         </property>
-        </widget>
-       </item>
-       <item row="0" column="3">
-        <widget class="QLabel" name="label">
-         <property name="text">
-          <string>Backup current database (if exists) and create a new one from scratch.</string>
+          <string>Import CSV</string>
          </property>
         </widget>
        </item>
-       <item row="0" column="0">
-        <widget class="QPushButton" name="resetDatabasePushButton">
+       <item row="1" column="0">
+        <widget class="QPushButton" name="resetUserTablesPushButton">
          <property name="minimumSize">
           <size>
            <width>220</width>
@@ -123,11 +117,14 @@
           </size>
          </property>
          <property name="text">
-          <string>Reset Database</string>
+          <string>Reset User Tables and Settings</string>
          </property>
         </widget>
        </item>
-       <item row="3" column="1" colspan="2">
+       <item row="6" column="3">
+        <widget class="QLineEdit" name="debug2LineEdit"/>
+       </item>
+       <item row="4" column="1" colspan="2">
         <widget class="QComboBox" name="tableComboBox">
          <property name="minimumSize">
           <size>
@@ -142,23 +139,33 @@
          </item>
         </widget>
        </item>
-       <item row="5" column="3">
-        <widget class="QLineEdit" name="debug2LineEdit"/>
+       <item row="4" column="3">
+        <widget class="QLineEdit" name="importCsvLineEdit">
+         <property name="minimumSize">
+          <size>
+           <width>110</width>
+           <height>0</height>
+          </size>
+         </property>
+         <property name="placeholderText">
+          <string>/path/to/file.csv</string>
+         </property>
+        </widget>
        </item>
-       <item row="0" column="1">
-        <widget class="QLabel" name="branchLabel">
+       <item row="1" column="3">
+        <widget class="QLabel" name="label_2">
          <property name="text">
-          <string>branch:</string>
+          <string>Keep current database but delete entries in pilots, aircraft and flights</string>
          </property>
         </widget>
        </item>
-       <item row="0" column="2">
-        <widget class="QLineEdit" name="branchLineEdit">
+       <item row="5" column="3">
+        <widget class="QLineEdit" name="debugLineEdit"/>
+       </item>
+       <item row="2" column="0">
+        <widget class="QPushButton" name="pushButton">
          <property name="text">
-          <string>develop</string>
-         </property>
-         <property name="placeholderText">
-          <string>develop</string>
+          <string>Trigger Setup Dialog on Next Launch</string>
          </property>
         </widget>
        </item>

+ 13 - 2
src/gui/widgets/homewidget.cpp

@@ -43,7 +43,7 @@ HomeWidget::HomeWidget(QWidget *parent) :
     currWarningThreshold = Settings::read(Settings::UserData::CurrWarningThreshold).toInt();
     auto logo = QPixmap(OPL::Assets::LOGO);
     ui->logoLabel->setPixmap(logo);
-    ui->welcomeLabel->setText(tr("Welcome to openPilotLog, %1!").arg(userName()));
+    ui->welcomeLabel->setText(tr("Welcome to openPilotLog, %1!").arg(getLogbookOwnerName()));
 
 
     limitationDisplayLabels = {
@@ -84,7 +84,7 @@ void HomeWidget::onPilotsDatabaseChanged(const OPL::DbTable table)
 {
     // maybe logbook owner name has changed, redraw
     if (table == OPL::DbTable::Pilots)
-        ui->welcomeLabel->setText(tr("Welcome to openPilotLog, %1!").arg(userName()));
+        ui->welcomeLabel->setText(tr("Welcome to openPilotLog, %1!").arg(getLogbookOwnerName()));
 }
 
 void HomeWidget::changeEvent(QEvent *event)
@@ -224,3 +224,14 @@ void HomeWidget::fillLimitations()
         setLabelColour(ui->FlightTimeCalYearDisplayLabel, Colour::Orange);
     }
 }
+
+const QString HomeWidget::getLogbookOwnerName()
+{
+    OPL::PilotEntry owner = DB->getLogbookOwner();
+    QString name = owner.getFirstName();
+    if(name.isEmpty()) {
+        name = owner.getLastName();
+    }
+    DEB << "owner name: " << name;
+    return name;
+}

+ 1 - 1
src/gui/widgets/homewidget.h

@@ -94,7 +94,7 @@ private:
     /*!
      * \brief Retreives the users first name from the database.
      */
-    const inline QString userName() { return DB->getPilotEntry(1).getFirstName(); }
+    const QString getLogbookOwnerName();
 
 public slots:
     void refresh();

+ 12 - 13
src/gui/widgets/settingswidget.cpp

@@ -122,8 +122,12 @@ void SettingsWidget::readSettings()
     //const QSignalBlocker blocker(this); // don't emit editing finished for setting these values
 
     // Personal Data Tab
-    auto user_data = DB->getPilotEntry(1).getData();
-    ui->lastnameLineEdit->setText(user_data.value(OPL::PilotEntry::LASTNAME).toString());
+    const auto user_data = DB->getLogbookOwner().getData();
+    QString lastName = user_data.value(OPL::PilotEntry::LASTNAME).toString();
+    if(lastName.isEmpty()) {
+        lastName = "Please enter your last name.";
+    }
+    ui->lastnameLineEdit->setText(lastName);
     ui->firstnameLineEdit->setText(user_data.value(OPL::PilotEntry::FIRSTNAME).toString());
     ui->companyLineEdit->setText(user_data.value(OPL::PilotEntry::COMPANY).toString());
     ui->employeeidLineEdit->setText(user_data.value(OPL::PilotEntry::EMPLOYEEID).toString());
@@ -194,9 +198,11 @@ void SettingsWidget::updatePersonalDetails()
     case 2:{
         QString name;
         name.append(ui->lastnameLineEdit->text());
-        name.append(QLatin1String(", "));
-        name.append(ui->firstnameLineEdit->text().at(0));
-        name.append(QLatin1Char('.'));
+        if(ui->firstnameLineEdit->text().size() > 0) {
+            name.append(QLatin1String(", "));
+            name.append(ui->firstnameLineEdit->text().at(0));
+            name.append(QLatin1Char('.'));
+        }
         user_data.insert(OPL::PilotEntry::ALIAS, name);
     }
         break;
@@ -210,17 +216,10 @@ void SettingsWidget::updatePersonalDetails()
     user_data.insert(OPL::PilotEntry::PHONE, ui->phoneLineEdit->text());
     user_data.insert(OPL::PilotEntry::EMAIL, ui->emailLineEdit->text());
 
-    auto user = OPL::PilotEntry(1, user_data);
-
-    TODO << "Changing DB does not currently refresh logbook view";
-    TODO << "Check for empty line edits (First, last name should not be empty...validators not a good way because it gives no user feedback)";
-
-    if(!DB->commit(user))
+    if(!DB->setLogbookOwner(user_data))
         WARN(tr("Unable to update Database:<br>") + DB->lastError.text());
     else
         LOG << "User updated successfully.";
-
-
 }
 
 /*

+ 2 - 0
src/gui/widgets/settingswidget.h

@@ -111,6 +111,8 @@ private:
 
     bool usingStylesheet();
 
+    const static int SELF_ROW_ID = 1;
+
 signals:
 
     /*!

+ 0 - 3
src/gui/widgets/totalswidget.cpp

@@ -90,8 +90,6 @@ void TotalsWidget::fillTotals(const WidgetType widgetType)
         break;
     case PreviousExperienceWidget:
         time_data = DB->getRowData(OPL::DbTable::PreviousExperience, ROW_ID);
-        LOG << "Previous row data:";
-        LOG << time_data;
     }
 
     // fill the line edits with the data obtained
@@ -105,7 +103,6 @@ void TotalsWidget::fillTotals(const WidgetType widgetType)
             const QString &le_name = line_edit->objectName();
             if(le_name.contains("to") || le_name.contains("ldg")) {
                 // line edits for take offs and landings
-                DEB << "Setting to/ldg: " << le_name << " -> " + field.toString();
                 line_edit->setText(field.toString());
             } else {
                 // line edits for total time

+ 1 - 1
src/gui/widgets/totalswidget.h

@@ -46,7 +46,7 @@ private:
      */
     OPL::RowData_T m_rowData;
     /*!
-     * \brief ROW_ID the row ID for previous experience entries (0)
+     * \brief ROW_ID the row ID for previous experience entries (1)
      */
     const static int ROW_ID = 1;
     void fillTotals(const WidgetType widgetType);