1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
/******************************************************************************
*
* Copyright (C) 2002 Hugo PEREIRA <mailto: hugo.pereira@free.fr>
*
* This 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 2 of the License, or (at your option) any later
* version.
*
* This software 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 <http://www.gnu.org/licenses/>.
*
*******************************************************************************/
#include "BaseCoreApplication.h"
#include "Debug.h"
#include "ErrorHandler.h"
#include "InterruptionHandler.h"
#include "XmlOptions.h"
#include <signal.h>
//____________________________________________
BaseCoreApplication::BaseCoreApplication( QObject* parent, CommandLineArguments arguments ) :
QObject( parent ),
arguments_( arguments )
{
Debug::Throw( "BaseCoreApplication::BaseCoreApplication.\n" );
// install interuption handler
InterruptionHandler::initialize();
// install error handler
ErrorHandler::initialize();
// configuration
connect( this, SIGNAL(configurationChanged()), SLOT(_updateConfiguration()) );
}
//____________________________________________
BaseCoreApplication::~BaseCoreApplication()
{
Debug::Throw( "BaseCoreApplication::~BaseCoreApplication.\n" );
emit saveConfiguration();
XmlOptions::write();
if( _hasApplicationManager() )
{
delete applicationManager_;
applicationManager_ = 0;
}
ErrorHandler::get().exit();
}
//____________________________________________
bool BaseCoreApplication::initApplicationManager()
{
Debug::Throw() << "BaseCoreApplication::initApplicationManager - arguments: " << arguments_.get().join( " " ) << endl;
// check if already initialized
if( _hasApplicationManager() ) return true;
// assign application name to qapplication
qApp->setApplicationName( applicationName() );
// parse arguments
auto parser( commandLineParser( arguments_ ) );
if( parser.hasFlag( "--help" ) )
{
usage();
return false;
} else if( parser.hasFlag( "--version" ) ) {
Debug::Throw(0) << "Qt: " << qVersion() << endl;
Debug::Throw(0) << applicationName() << ": " << applicationVersion();
Debug::Throw(0) << "" << endl;
return false;
} else if( parser.hasFlag( "--no-server" ) ) {
realizeWidget();
return true;
}
// create application manager
applicationManager_ = new Server::ApplicationManager( this );
applicationManager_->setApplicationName( applicationName() );
// connections
connect( applicationManager_, SIGNAL(commandRecieved(Server::ServerCommand)), SLOT(_processCommand(Server::ServerCommand)) );
// initialization
applicationManager_->initialize( arguments_ );
return true;
}
//____________________________________________
bool BaseCoreApplication::realizeWidget()
{
Debug::Throw( "BaseCoreApplication::realizeWidget.\n" );
//* check if the method has already been called.
if( realized_ ) return false;
realized_ = true;
return true;
}
//_______________________________________________
CommandLineParser BaseCoreApplication::commandLineParser( CommandLineArguments arguments, bool ignoreWarnings ) const
{
Debug::Throw() << "BaseCoreApplication::commandLineParser" << endl;
CommandLineParser out( Server::ApplicationManager::commandLineParser() );
out.setGroup( CommandLineParser::applicationGroupName );
out.registerFlag( CommandLineParser::Tag( "--help", "-h" ), QObject::tr( "print this help and exit" ) );
out.registerFlag( CommandLineParser::Tag( "--version", "-v" ), QObject::tr( "print application version and exits" ) );
if( !arguments.isEmpty() )
{ out.parse( arguments, ignoreWarnings ); }
return out;
}
//__________________________________________________________________
void BaseCoreApplication::sendServerCommand( Server::ServerCommand::CommandType type )
{
Debug::Throw( "BaseCoreApplication::sendServerCommand.\n" );
{ if( _hasApplicationManager() ) sendServerCommand( Server::ServerCommand( _applicationManager().id(), type ) ); }
}
//__________________________________________________________________
void BaseCoreApplication::sendServerCommand( Server::ServerCommand command )
{
Debug::Throw( "BaseCoreApplication::sendServerCommand.\n" );
if( _hasApplicationManager() ) _applicationManager().client().sendCommand( command );
}
//________________________________________________
bool BaseCoreApplication::_processCommand( Server::ServerCommand command )
{
Debug::Throw() << "BaseCoreApplication::_processCommand: " << command.commandName() << endl;
switch( command.command() )
{
case Server::ServerCommand::Accepted:
realizeWidget();
return true;
case Server::ServerCommand::Abort:
case Server::ServerCommand::Denied:
qApp->quit();
return true;
default:
break;
}
return false;
}
//_______________________________________________
void BaseCoreApplication::_updateConfiguration()
{
Debug::Throw( "BaseCoreApplication::_updateConfiguration.\n" );
// debug
Debug::setLevel( XmlOptions::get().get<int>( "DEBUG_LEVEL" ) );
// emit signal to propagate changes to other widgets
Debug::Throw( "BaseCoreApplication::_updateConfiguration - done.\n" );
}
//_______________________________________________
void BaseCoreApplication::_usage( QString application, QString options ) const
{
QTextStream( stdout ) << tr( "Usage: " ) << endl;
QTextStream( stdout ) << " " << application << " " << options << endl;
}
|