AusweisApp2/src/CommandLineParser.cpp

128 lines
2.9 KiB
C++
Raw Normal View History

2017-07-03 09:30:10 +02:00
/*
2019-05-22 10:08:38 +02:00
* \copyright Copyright (c) 2014-2019 Governikus GmbH & Co. KG, Germany
2017-07-03 09:30:10 +02:00
*/
#include "CommandLineParser.h"
2019-01-03 15:06:22 +01:00
#include "controller/AppController.h"
#include "DatagramHandlerImpl.h"
2017-07-03 09:30:10 +02:00
#include "LogHandler.h"
#include "NetworkManager.h"
2019-01-03 15:06:22 +01:00
#include "PortFile.h"
2017-07-03 09:30:10 +02:00
#include "SingletonHelper.h"
2019-01-03 15:06:22 +01:00
#include "UILoader.h"
2017-07-03 09:30:10 +02:00
#if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS)
2017-07-03 09:33:28 +02:00
#include "HttpServer.h"
2017-07-03 09:30:10 +02:00
#endif
#include <QCoreApplication>
using namespace governikus;
2019-01-03 15:06:22 +01:00
defineSingleton(CommandLineParser)
2017-07-03 09:30:10 +02:00
CommandLineParser::CommandLineParser()
: mParser()
2017-12-20 14:54:05 +01:00
, mOptionKeepLog(QStringLiteral("keep"), QStringLiteral("Keep log file."))
2019-01-03 15:06:22 +01:00
, mOptionNoLogFile(QStringLiteral("no-logfile"), QStringLiteral("Disable log file."))
2019-05-22 10:08:38 +02:00
, mOptionNoLogHandler(QStringLiteral("no-loghandler"), QStringLiteral("Disable default log handler."))
2017-12-20 14:54:05 +01:00
, mOptionShowWindow(QStringLiteral("show"), QStringLiteral("Show window on startup."))
, mOptionProxy(QStringLiteral("no-proxy"), QStringLiteral("Disable system proxy."))
2019-01-03 15:06:22 +01:00
, mOptionUi(QStringLiteral("ui"), QStringLiteral("Use given UI plugin."), UILoader::getInstance().getDefault().join(QLatin1Char(',')))
, mOptionPort(QStringLiteral("port"), QStringLiteral("Use listening port."), QString::number(PortFile::cDefaultPort))
2017-07-03 09:30:10 +02:00
{
addOptions();
}
CommandLineParser& CommandLineParser::getInstance()
{
return *Instance;
}
void CommandLineParser::addOptions()
{
mParser.addHelpOption();
mParser.addVersionOption();
mParser.addOption(mOptionKeepLog);
2019-01-03 15:06:22 +01:00
mParser.addOption(mOptionNoLogFile);
2019-05-22 10:08:38 +02:00
mParser.addOption(mOptionNoLogHandler);
2017-07-03 09:30:10 +02:00
2017-07-03 09:33:28 +02:00
#if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS) && !defined(Q_OS_WINRT)
2017-07-03 09:30:10 +02:00
mParser.addOption(mOptionShowWindow);
#endif
mParser.addOption(mOptionProxy);
mParser.addOption(mOptionUi);
mParser.addOption(mOptionPort);
}
void CommandLineParser::parse(QCoreApplication* pApp)
{
2019-09-30 17:22:19 +02:00
if (pApp == nullptr)
2017-07-03 09:30:10 +02:00
{
return;
}
mParser.process(*pApp);
parseUiPlugin();
2019-01-03 15:06:22 +01:00
const auto& logHandler = Env::getSingleton<LogHandler>();
2017-07-03 09:30:10 +02:00
if (mParser.isSet(mOptionKeepLog))
{
2019-01-03 15:06:22 +01:00
logHandler->setAutoRemove(false);
}
if (mParser.isSet(mOptionNoLogFile))
{
logHandler->setLogfile(false);
2017-07-03 09:30:10 +02:00
}
2019-05-22 10:08:38 +02:00
if (mParser.isSet(mOptionNoLogHandler))
{
logHandler->setUseHandler(false);
}
2017-07-03 09:33:28 +02:00
#if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS) && !defined(Q_OS_WINRT)
2017-07-03 09:30:10 +02:00
if (mParser.isSet(mOptionShowWindow))
{
2019-01-03 15:06:22 +01:00
AppController::cShowUi = true;
2017-07-03 09:30:10 +02:00
}
#endif
if (mParser.isSet(mOptionProxy))
{
NetworkManager::lockProxy(true);
}
if (mParser.isSet(mOptionPort))
{
bool converted = false;
const uint port = mParser.value(mOptionPort).toUInt(&converted);
2019-01-03 15:06:22 +01:00
if (converted && port <= std::numeric_limits<quint16>::max())
2017-07-03 09:30:10 +02:00
{
2019-01-03 15:06:22 +01:00
DatagramHandlerImpl::cPort = static_cast<quint16>(port);
2017-07-03 09:30:10 +02:00
2019-01-03 15:06:22 +01:00
#if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS)
HttpServer::cPort = DatagramHandlerImpl::cPort;
#endif
2017-07-03 09:30:10 +02:00
}
}
}
void CommandLineParser::parseUiPlugin()
{
if (mParser.isSet(mOptionUi))
{
2019-01-03 15:06:22 +01:00
UILoader::getInstance().setDefault(mParser.values(mOptionUi));
2017-07-03 09:30:10 +02:00
}
}