2017-07-03 09:30:10 +02:00
/*
2018-03-28 15:10:51 +02:00
* \ copyright Copyright ( c ) 2014 - 2018 Governikus GmbH & Co . KG , Germany
2017-07-03 09:30:10 +02:00
*/
# include "CommandLineParser.h"
# include "LogHandler.h"
# include "NetworkManager.h"
# include "SingletonHelper.h"
# include "view/UILoader.h"
# if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS)
2017-07-03 09:33:28 +02:00
# include "HttpServer.h"
# ifndef Q_OS_WINRT
# include "GuiProfile.h"
# endif
2017-07-03 09:30:10 +02:00
# endif
# ifndef QT_NO_DEBUG
# include "UIPlugInWebSocket.h"
# endif
# include <QCoreApplication>
# include <QLoggingCategory>
using namespace governikus ;
defineSingleton ( CommandLineParser )
Q_DECLARE_LOGGING_CATEGORY ( cmdline )
namespace
{
2017-12-20 14:54:05 +01:00
QString getPrefixUi ( )
{
return QStringLiteral ( " UIPlugIn " ) ;
}
2017-07-03 09:30:10 +02:00
QString defaultUi ( const QVector < UIPlugInName > & pPlugins )
{
QStringList list ;
for ( auto entry : pPlugins )
{
2017-12-20 14:54:05 +01:00
list < < QString ( getEnumName ( entry ) ) . remove ( getPrefixUi ( ) ) ;
2017-07-03 09:30:10 +02:00
}
2017-12-20 14:54:05 +01:00
return list . join ( QLatin1Char ( ' , ' ) ) ;
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. " ) )
, mOptionShowWindow ( QStringLiteral ( " show " ) , QStringLiteral ( " Show window on startup. " ) )
, mOptionProxy ( QStringLiteral ( " no-proxy " ) , QStringLiteral ( " Disable system proxy. " ) )
, mOptionUi ( QStringLiteral ( " ui " ) , QStringLiteral ( " Use given UI plugin. " ) , defaultUi ( UILoader : : getInstance ( ) . getDefault ( ) ) )
, mOptionPort ( QStringLiteral ( " port " ) , QStringLiteral ( " Use listening port. " ) , QStringLiteral ( " 24727 " ) )
2017-07-03 09:30:10 +02:00
# ifndef QT_NO_DEBUG
2017-12-20 14:54:05 +01:00
, mOptionPortWebSocket ( QStringLiteral ( " port-websocket " ) , QStringLiteral ( " Use listening port for websocket. " ) , QString : : number ( UIPlugInWebSocket : : WEBSOCKET_DEFAULT_PORT ) )
2017-07-03 09:30:10 +02:00
# endif
{
addOptions ( ) ;
}
CommandLineParser : : ~ CommandLineParser ( )
{
}
CommandLineParser & CommandLineParser : : getInstance ( )
{
return * Instance ;
}
void CommandLineParser : : addOptions ( )
{
mParser . addHelpOption ( ) ;
mParser . addVersionOption ( ) ;
mParser . addOption ( mOptionKeepLog ) ;
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 ) ;
# ifndef QT_NO_DEBUG
mParser . addOption ( mOptionPortWebSocket ) ;
# endif
}
void CommandLineParser : : parse ( QCoreApplication * pApp )
{
if ( ! pApp )
{
qCWarning ( cmdline ) < < " QCoreApplication is undefined " ;
return ;
}
mParser . process ( * pApp ) ;
parseUiPlugin ( ) ;
if ( mParser . isSet ( mOptionKeepLog ) )
{
LogHandler : : getInstance ( ) . setAutoRemove ( 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 ) )
{
GuiProfile : : getProfile ( ) . setShowWindow ( true ) ;
}
# endif
if ( mParser . isSet ( mOptionProxy ) )
{
NetworkManager : : lockProxy ( true ) ;
}
# if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS)
if ( mParser . isSet ( mOptionPort ) )
{
bool converted = false ;
const uint port = mParser . value ( mOptionPort ) . toUInt ( & converted ) ;
2017-07-03 09:33:28 +02:00
if ( converted & & port < USHRT_MAX )
2017-07-03 09:30:10 +02:00
{
2017-07-03 09:33:28 +02:00
HttpServer : : cPort = static_cast < ushort > ( port ) ;
2017-07-03 09:30:10 +02:00
}
else
{
qCWarning ( cmdline ) < < " Cannot use value as port: " < < mParser . value ( mOptionPort ) ;
}
}
# endif
# ifndef QT_NO_DEBUG
if ( mParser . isSet ( mOptionPortWebSocket ) )
{
bool converted = false ;
const uint port = mParser . value ( mOptionPortWebSocket ) . toUInt ( & converted ) ;
2017-07-03 09:33:28 +02:00
if ( converted & & port < USHRT_MAX )
2017-07-03 09:30:10 +02:00
{
2017-07-03 09:33:28 +02:00
UIPlugInWebSocket : : setPort ( static_cast < ushort > ( port ) ) ;
2017-07-03 09:30:10 +02:00
}
else
{
qCWarning ( cmdline ) < < " Cannot use value as websocket port: " < < mParser . value ( mOptionPortWebSocket ) ;
}
}
# endif
}
void CommandLineParser : : parseUiPlugin ( )
{
if ( mParser . isSet ( mOptionUi ) )
{
QVector < UIPlugInName > selectedPlugins ;
2017-07-03 09:33:28 +02:00
const auto & availablePlugins = Enum < UIPlugInName > : : getList ( ) ;
const auto & requestedUis = mParser . values ( mOptionUi ) ;
for ( const auto & parsedUiOption : requestedUis )
2017-07-03 09:30:10 +02:00
{
for ( auto availablePluginEntry : availablePlugins )
{
2017-12-20 14:54:05 +01:00
if ( parsedUiOption . compare ( QString ( getEnumName ( availablePluginEntry ) ) . remove ( getPrefixUi ( ) ) , Qt : : CaseInsensitive ) = = 0 )
2017-07-03 09:30:10 +02:00
{
selectedPlugins < < availablePluginEntry ;
}
}
}
if ( ! selectedPlugins . isEmpty ( ) )
{
UILoader : : getInstance ( ) . setDefault ( selectedPlugins ) ;
}
}
}