Browse Source

Cleaned Up main()

Cleaned up main.cpp, moved DB check and connection to mainwindow.cpp
Felix Turo 3 năm trước cách đây
mục cha
commit
8e922598ad
2 tập tin đã thay đổi với 50 bổ sung33 xóa
  1. 40 33
      main.cpp
  2. 10 0
      mainwindow.cpp

+ 40 - 33
main.cpp

@@ -37,53 +37,60 @@
 #define ORGNAME QStringLiteral("opl")
 #define ORGDOMAIN QStringLiteral("https://github.com/fiffty-50/openpilotlog")
 
-int main(int argc, char *argv[])
-{
-    QApplication openPilotLog(argc, argv);
-    QCoreApplication::setOrganizationName(ORGNAME);
-    QCoreApplication::setOrganizationDomain(ORGDOMAIN);
-    QCoreApplication::setApplicationName(APPNAME);
 
-    if(!AStandardPaths::setup()){
-        LOG << "Unable to create directories.\n";
-        return 1;
+void init()
+{
+    LOG << "Setting up / verifying Application Directories...";
+    if(AStandardPaths::setup()) {
+        LOG << "Application Directories... verified";
+    } else {
+        LOG << "Unable to create directories.";
     }
-
-    ALog::init(true); // Initialise logging and message handling
-
+    LOG << "Setting up logging facilities...";
+    if(ALog::init(true)) {
+        LOG << "Logging enabled.";
+    } else {
+        LOG << "Unable to initalise logging.";
+    }
+    LOG << "Reading Settings...";
     ASettings::setup();
-
+    LOG << "Setting up application style...";
     AStyle::setup();
+}
 
-    if (ASettings::read(ASettings::Main::SetupComplete).toBool()) {
-        QFileInfo database_file(AStandardPaths::directory(AStandardPaths::Database).
-                                     absoluteFilePath(QStringLiteral("logbook.db")));
-        if (!database_file.exists()) {
-            LOG << "Error: Database file not found\n";
-            return 2;
-        }
-    } else {
-        if(FirstRunDialog().exec() == QDialog::Rejected){
-            LOG << "Initial setup incomplete or unsuccessfull. Exiting.\n";
-            return 3;
-        }
-        ASettings::write(ASettings::Main::SetupComplete, true);
-        DEB << "Wrote setup_commplete";
+void firstRun()
+{
+    if(FirstRunDialog().exec() == QDialog::Rejected){
+        LOG << "Initial setup incomplete or unsuccessfull.";
+        return;
     }
+    ASettings::write(ASettings::Main::SetupComplete, true);
+    LOG << "Initial Setup Completed successfully";
+}
 
-    if (!aDB->connect()) {
-        LOG << "Error establishing database connection\n";
-        return 4;
-    }
+int main(int argc, char *argv[])
+{
+    QApplication openPilotLog(argc, argv);
+    QCoreApplication::setOrganizationName(ORGNAME);
+    QCoreApplication::setOrganizationDomain(ORGDOMAIN);
+    QCoreApplication::setApplicationName(APPNAME);
 
+    // Check for another instance already running
     ARunGuard guard(QStringLiteral("opl_single_key"));
     if ( !guard.tryToRun() ){
-        LOG << "Another Instance of openPilotLog is already running. Exiting.\n";
+        LOG << "Another Instance of openPilotLog is already running. Exiting.";
         return 0;
     }
 
-    MainWindow w;
+    // Set Up the Application
+    init();
+
+    // Check for First Run and launch Setup Wizard
+    if (!ASettings::read(ASettings::Main::SetupComplete).toBool())
+        firstRun();
 
+    // Create Main Window and set Window Icon acc. to Platform
+    MainWindow w;
 #ifdef __linux__
     w.setWindowIcon(QIcon(Opl::Assets::ICON_APPICON_LINUX));
 #elif defined(_WIN32) || defined(_WIN64)

+ 10 - 0
mainwindow.cpp

@@ -26,6 +26,16 @@ MainWindow::MainWindow(QWidget *parent)
     , ui(new Ui::MainWindow)
 {
     ui->setupUi(this);
+    // connect to the Database
+    TODO << "Create more verbose warning about DB and offer instructions how to fix it.";
+    QFileInfo database_file(AStandardPaths::directory(AStandardPaths::Database).
+                                         absoluteFilePath(QStringLiteral("logbook.db")));
+            if (!database_file.exists()) {
+                WARN(tr("Error: Database file not found."));
+            }
+    if(!aDB->connect()){
+        WARN(tr("Error establishing database connection."));
+    }
 
     // Create a spacer for the toolbar to separate left and right parts
     auto *spacer = new QWidget();