# HG changeset patch # User Michiel Broek # Date 1644265706 -3600 # Node ID a1e435907f3aadb4678e84089dfa63dafec03cd0 # Parent d38df7b580263fe80bc752c1779086ed9a553a2e Added commandline parameters. Added initial app functions. Added user configuration read/write/create. diff -r d38df7b58026 -r a1e435907f3a CMakeLists.txt --- a/CMakeLists.txt Sun Feb 06 16:52:20 2022 +0100 +++ b/CMakeLists.txt Mon Feb 07 21:28:26 2022 +0100 @@ -6,7 +6,7 @@ SET(bmsapp_EXECUTABLE "bmsapp") MESSAGE( STATUS "Building bmsapp" ) -# Version +# ===== Set application version ===== SET( bmsapp_VERSION_MAJOR 0 ) SET( bmsapp_VERSION_MINOR 1 ) @@ -37,20 +37,36 @@ # SET(CMAKE_BUILD_TYPE "Debug") #ENDIF() -#============================Directories======================================= +# ===== Directories ===== + +IF( NOT EXEC_PREFIX ) + SET( EXEC_PREFIX ${CMAKE_INSTALL_PREFIX} ) +ENDIF() + +SET( DATAROOTDIR "${EXEC_PREFIX}/share" ) +SET( BINDIR "${EXEC_PREFIX}/bin" ) +IF( NOT DOCDIR ) + SET( DOCDIR "${DATAROOTDIR}/doc/${CMAKE_PROJECT_NAME}" ) +ENDIF() + +SET( DATAPATH "${DATAROOTDIR}/${CMAKE_PROJECT_NAME}" ) +SET( TARGETPATH ${BINDIR} ) +SET( DOCPATH ${DOCDIR} ) + SET(ROOTDIR "${CMAKE_CURRENT_SOURCE_DIR}") SET(SRCDIR "${ROOTDIR}/src") #SET(UIDIR "${ROOTDIR}/ui") -#SET(DATADIR "${ROOTDIR}/data") +SET(DATADIR "${ROOTDIR}/data") #SET(TRANSLATIONSDIR "${ROOTDIR}/translations") INCLUDE_DIRECTORIES(${SRCDIR}) INCLUDE_DIRECTORIES("${CMAKE_BINARY_DIR}/src") # In case of out-of-source build. -#==================================Find Qt5==================================== +# ===== Find Qt5 ===== -find_package(Qt5 REQUIRED COMPONENTS Core Widgets) +# Minimum versio 5.13 for debug messages. +find_package(Qt5 5.13 REQUIRED COMPONENTS Core Widgets) # Network PrintSupport Sql Xml LinguistTools @@ -63,7 +79,10 @@ CMakeCache.txt ) -#==============================Setup the config.h============================== +# ===== Setup the config.h ===== + +SET( CONFIGDATADIR "${DATAPATH}/" ) +SET( CONFIGDOCDIR "${DOCPATH}/" ) # Tell cmake where the configure file is and where # to put the output. All variables in config.in written as "${VAR}$ @@ -71,10 +90,20 @@ # Outputs only in the build directory. CONFIGURE_FILE( src/config.in src/config.h ) -# ===== Process other CMakeList.txt's ===== +# ===== All sources ===== + +set( SRCS + ${SRCDIR}/main.cpp + ${SRCDIR}/bmsapp.cpp +) + +set( HDRS + ${SRCDIR}/bmsapp.h +) set( SOURCE_FILES - ${SRCDIR}/main.cpp + ${SRCS} + ${HDRS} ) # ===== Build the application ===== diff -r d38df7b58026 -r a1e435907f3a src/bmsapp.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/bmsapp.cpp Mon Feb 07 21:28:26 2022 +0100 @@ -0,0 +1,170 @@ +/** + * bmsapp.cpp is part of bmsapp. + * + * bmsapp 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. + * + * bmsapp 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 . + */ +#include "bmsapp.h" +#include "config.h" + + + +bool Bmsapp::ensureDirectoriesExist() +{ + QDir resourceDir = Bmsapp::getResourceDir(); + bool resourceDirSuccess = resourceDir.exists(); + if (!resourceDirSuccess) { + QString errMsg{ + QObject::tr("Resource directory \"%1\" is missing. Some features will be unavailable.").arg(resourceDir.path()) + }; + qCritical() << Q_FUNC_INFO << errMsg; + } + + return resourceDirSuccess; +// return resourceDirSuccess && createDir(PersistentSettings::getUserDir()); +} + + +QDir Bmsapp::getResourceDir() +{ + QString dir = qApp->applicationDirPath(); + + dir = QString(CONFIGDATADIR); + + qDebug() << "Resource dir" << dir; + return dir; +} + + + +bool Bmsapp::initialize() +{ + ensureDirectoriesExist(); + readsettings(); + + qDebug() << "Loading Database..."; + return true; +} + + + +void Bmsapp::cleanup() +{ + qDebug() << "BMSapp is cleaning up."; + writesettings(); +} + + + +void Bmsapp::readsettings() +{ + QSettings settings(QSettings::IniFormat, QSettings::UserScope, "mbse", "bmsapp"); + + settings.beginGroup("dbprod"); + dbProd.host = settings.value("host").toString(); + if (dbProd.host.isEmpty()) { + dbProd.host = "localhost"; + dbProd.port = "3306"; + dbProd.name = "bms_prod"; + dbProd.user = "nobody"; + dbProd.pass = "secret"; + dbProd.charset = "utf8"; + settings.setValue("host", dbProd.host); + settings.setValue("port", dbProd.port); + settings.setValue("name", dbProd.name); + settings.setValue("user", dbProd.user); + settings.setValue("pass", dbProd.pass); + settings.setValue("charset", dbProd.charset); + } else { + dbProd.port = settings.value("port").toString(); + dbProd.name = settings.value("name").toString(); + dbProd.user = settings.value("user").toString(); + dbProd.pass = settings.value("pass").toString(); + dbProd.charset = settings.value("charset").toString(); + } + settings.endGroup(); + qDebug() << dbProd.host << dbProd.port << dbProd.name << dbProd.pass; + + settings.beginGroup("dbdev"); + dbDev.host = settings.value("host").toString(); + if (dbDev.host.isEmpty()) { + dbDev.host = "localhost"; + dbDev.port = "3306"; + dbDev.name = "bms_dev"; + dbDev.user = "nobody"; + dbDev.pass = "secret"; + dbDev.charset = "utf8"; + settings.setValue("host", dbDev.host); + settings.setValue("port", dbDev.port); + settings.setValue("name", dbDev.name); + settings.setValue("user", dbDev.user); + settings.setValue("pass", dbDev.pass); + settings.setValue("charset", dbDev.charset); + } else { + dbDev.port = settings.value("port").toString(); + dbDev.name = settings.value("name").toString(); + dbDev.user = settings.value("user").toString(); + dbDev.pass = settings.value("pass").toString(); + dbDev.charset = settings.value("charset").toString(); + } + settings.endGroup(); + qDebug() << dbDev.host << dbDev.port << dbDev.name << dbDev.pass; +} + + + +void Bmsapp::writesettings() +{ + QSettings settings(QSettings::IniFormat, QSettings::UserScope, "mbse", "bmsapp"); + + settings.beginGroup("dbprod"); + settings.setValue("host", dbProd.host); + settings.setValue("port", dbProd.port); + settings.setValue("name", dbProd.name); + settings.setValue("user", dbProd.user); + settings.setValue("pass", dbProd.pass); + settings.setValue("charset", dbProd.charset); + settings.endGroup(); + + settings.beginGroup("dbdev"); + settings.setValue("host", dbDev.host); + settings.setValue("port", dbDev.port); + settings.setValue("name", dbDev.name); + settings.setValue("user", dbDev.user); + settings.setValue("pass", dbDev.pass); + settings.setValue("charset", dbDev.charset); + settings.endGroup(); + qDebug() << "writesettings() done."; +} + + + +int Bmsapp::run() { + + int rc = 0; + + if (! initialize()) { + cleanup(); + return 1; + } + + rc = qApp->exec(); + + cleanup(); + + qDebug() << Q_FUNC_INFO << "Cleaned up. Returning " << rc; + return rc; +} + + + diff -r d38df7b58026 -r a1e435907f3a src/bmsapp.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/bmsapp.h Mon Feb 07 21:28:26 2022 +0100 @@ -0,0 +1,86 @@ +#ifndef _BMSAPP_H +#define _BMSAPP_H + +#include +#include +#include +#include +// #include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + +typedef struct IniMySQL +{ + QString host; ///< MySQL host + QString port; ///< MySQL port + QString name; ///< MySQL database + QString user; ///< MySQL username + QString pass; ///< MySQL password + QString charset; ///< MySQL character encoding +} IniMySQL; + +static IniMySQL dbProd; +static IniMySQL dbDev; + +class MainWindow; + + +class Bmsapp : public QObject +{ + Q_OBJECT + + friend class MainWindow; + +public: + Bmsapp(); + + static QDir getResourceDir(); /* System wide resources */ + + /** + * @brief Blocking call that executes the application. + * @return Exit code from the application. + */ + static int run(); + +private: + static MainWindow* m_mainWindow; + + /** + * @brief Ensure some directories and files exist. + * @return true if all is ok. + */ + static bool ensureDirectoriesExist(); + + /** + * @brief Run before showing MainWindow, does all system setup. + * @return false if anything goes awry, true if it's ok to start MainWindow. + */ + static bool initialize(); + + /** + * @brief Run after QApplication exits to clean up shit, close database, etc. + */ + static void cleanup(); + + /** + * @brief Read user settings for the MySQL connection. + */ + static void readsettings(); + + /** + * @brief Write or update the user settings for the MySQL connection. + */ + static void writesettings(); +}; + + +#endif diff -r d38df7b58026 -r a1e435907f3a src/config.in --- a/src/config.in Sun Feb 06 16:52:20 2022 +0100 +++ b/src/config.in Mon Feb 07 21:28:26 2022 +0100 @@ -1,6 +1,9 @@ #ifndef CONFIG_IN #define CONFIG_IN +#define CONFIGDATADIR "${CONFIGDATADIR}" +#define CONFIGDOCDIR "${CONFIGDOCDIR}" + #define VERSIONSTRING "${bmsapp_VERSION_MAJOR}.${bmsapp_VERSION_MINOR}.${bmsapp_VERSION_PATCH}" #endif diff -r d38df7b58026 -r a1e435907f3a src/main.cpp --- a/src/main.cpp Sun Feb 06 16:52:20 2022 +0100 +++ b/src/main.cpp Mon Feb 07 21:28:26 2022 +0100 @@ -16,9 +16,14 @@ */ #include #include +#include +#include +#include + //#include //#include +#include "bmsapp.h" #include "config.h" @@ -33,6 +38,36 @@ #endif ); app.setApplicationVersion(VERSIONSTRING); + app.setOrganizationName("mbse"); - return app.exec(); + /* Setup user ini format */ + QSettings::setDefaultFormat(QSettings::IniFormat); + + /* Setup commandline parser */ + QCommandLineParser parser; + parser.setApplicationDescription("Brewery Management System Application."); + parser.addOptions({ + // A boolean option with multiple names (-f, --force) + {{"c", "config"}, + QCoreApplication::translate("main", "Start the configuration editor.")}, + {{"d", "develop"}, + QCoreApplication::translate("main", "Use the development database.")}, + }); + parser.addHelpOption(); + parser.addVersionOption(); + parser.process(app); + + qDebug().noquote() << "Starting" << app.applicationName() << app.applicationVersion() << QDateTime::currentDateTime().toString(); + + try { + auto mainAppReturnValue = Bmsapp::run(); + qDebug().noquote() << "Finished" << app.applicationName() << app.applicationVersion() << QDateTime::currentDateTime().toString(); + return mainAppReturnValue; + } + catch (const QString &error) { + QMessageBox::critical(0, + QApplication::tr("Application terminates"), + QApplication::tr("The application encountered a fatal error.\nError message:\n%1").arg(error)); + } + return EXIT_FAILURE; }