/** * @RDE_HEADER@ * DBUpdate * $Id$ * $HeadURL$ * $Author$ * Created by Iuliana Ojog, 2021-02-17 */ #include "db_update.h" #include #include #include #include #include #include class CmdLineParser : public LtsCommandLineParser::LtsCommandLineParser { public: CmdLineParser() : LtsCommandLineParser(ProvideCommandLineParameterOptions::None) { addOption({{QStringLiteral("l"), QStringLiteral("lang")}, QStringLiteral("Language iso code to use (de_DE, en_GB, en_US)"), QStringLiteral("") }); addOption({{QStringLiteral("u"), QStringLiteral("update")}, QStringLiteral("Update file (*.upd)"), QStringLiteral("") }); addOption({{QStringLiteral("p"), QStringLiteral("pass")}, QStringLiteral("Password"), QStringLiteral("") }); addOption({{QStringLiteral("g"), QStringLiteral("only generate sql")}, QStringLiteral("Only generate sql (do no execute)") }); addOption({{QStringLiteral("b"), QStringLiteral("Backup first")}, QStringLiteral("Make a database backup first") }); } bool onlyGenerate() const { return isSet(QStringLiteral("g")); } bool withBackup() const { return isSet(QStringLiteral("b")); } QString langIsoCode() const { return value(QStringLiteral("l")); } QString updateFile() const { return value(QStringLiteral("u")); } QString pass() const { return value(QStringLiteral("p")); } }; int main(int argc, char *argv[]) { int retval = -1; bool bHeadless = false; for (int i = 1; i < argc; ++i) { if (strcmp(argv[i], "-u") == 0) // given the update file on commando line { bHeadless = true; break; } } QScopedPointer app; app.reset(bHeadless ? dynamic_cast(new NGLCoreApplication(argc, argv)) : dynamic_cast(new NGLApplication(argc, argv))); QCoreApplication::setApplicationName(QLatin1String("DBUpdate")); QCoreApplication::setOrganizationName(QLatin1String("RME")); if (!bHeadless) app->setAttribute(Qt::AA_UseHighDpiPixmaps); NGL::LoggerHandler *lh = dynamic_cast(app.data()); lh->createLogger("dbu", "log4cxx_dbu.props"); NGL::INGLLogger &logger = lh->baseLogger(); // Parse Options CmdLineParser options; if (!options.init()) return retval; // Set Translation QString langIsoCode = options.langIsoCode(); if (langIsoCode.isEmpty()) langIsoCode = QLocale::system().name(); NGL::setTranslationLocale(langIsoCode.toLatin1()); STARTUP_LOG(logger); if (bHeadless) { QString pwd = options.pass(); if (pwd.isEmpty() || pwd != QStringLiteral("geheim")) { ERROR_LOG(logger) << "Incorrect password !!!"; return -3; } DEBUG_LOG(logger) << "Running headless, update file: " << options.updateFile() << ", Generate only: " << options.onlyGenerate(); DBUpdate::DBUpdateRun *up = new DBUpdate::DBUpdateRun(options.updateFile(), options.onlyGenerate(), options.withBackup()); if (!up->init()) { ERROR_LOG(logger) << "Failed to init DBUpdateHeadless"; return -2; } if (!up->runUpdate()) { ERROR_LOG(logger) << "Failed to run update: " << options.updateFile(); return -1; } retval = 0; } else { bool ok; QString pass = QInputDialog::getText(nullptr, QCoreApplication::tr("Password"), QCoreApplication::tr("Please enter the password:"), QLineEdit::Password, QString(), &ok); // blocking, no event loop needed if (!ok || pass.isEmpty() || pass.toLower() != QStringLiteral("geheim")) { ERROR_LOG(logger) << "Incorrect password !!!"; return -3; } QScopedPointer mainWindow(new DBUpdate::DBUpdate); if (!mainWindow->init()) { ERROR_LOG(logger) << "Failed to init DBUpdate"; return -2; } mainWindow->show(); QGuiApplication::setQuitOnLastWindowClosed(true); retval = QCoreApplication::exec(); if (retval < 0) ERROR_LOG(logger) << "Failed: " << retval; } SHUTDOWN_LOG(logger); return retval; }