2018-11-22 08:36:07 -03:00
// Copyright (c) 2011-2019 The Bitcoin Core developers
2014-12-13 01:09:33 -03:00
// Distributed under the MIT software license, see the accompanying
2013-11-04 12:20:43 -03:00
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2013-01-23 17:51:02 -03:00
2014-01-11 14:56:24 -03:00
# if defined(HAVE_CONFIG_H)
2017-11-09 21:57:53 -03:00
# include <config/bitcoin-config.h>
2014-01-11 14:56:24 -03:00
# endif
2018-05-18 16:41:56 -04:00
# include <qt/bitcoin.h>
2017-11-09 21:57:53 -03:00
# include <qt/bitcoingui.h>
# include <chainparams.h>
2019-07-10 08:06:57 -04:00
# include <qt/clientmodel.h>
2017-11-09 21:57:53 -03:00
# include <qt/guiconstants.h>
# include <qt/guiutil.h>
# include <qt/intro.h>
# include <qt/networkstyle.h>
# include <qt/optionsmodel.h>
# include <qt/platformstyle.h>
# include <qt/splashscreen.h>
# include <qt/utilitydialog.h>
# include <qt/winshutdownmonitor.h>
2014-09-05 07:18:35 -04:00
2013-12-11 11:00:56 -03:00
# ifdef ENABLE_WALLET
2017-11-09 21:57:53 -03:00
# include <qt/paymentserver.h>
2019-01-04 15:49:48 -03:00
# include <qt/walletcontroller.h>
2019-07-23 08:38:10 -04:00
# include <qt/walletmodel.h>
# endif // ENABLE_WALLET
2013-04-13 02:13:08 -03:00
2018-04-07 04:42:02 -03:00
# include <interfaces/handler.h>
# include <interfaces/node.h>
2018-09-06 16:21:00 -03:00
# include <noui.h>
2017-11-09 21:57:53 -03:00
# include <ui_interface.h>
2018-03-01 06:40:36 -03:00
# include <uint256.h>
2018-10-22 19:51:11 -03:00
# include <util/system.h>
2019-07-10 08:06:57 -04:00
# include <util/threadnames.h>
2011-05-07 17:13:39 -03:00
2018-04-02 15:31:40 -03:00
# include <memory>
2014-09-05 07:18:35 -04:00
2013-07-23 02:52:24 -04:00
# include <QApplication>
2014-07-15 04:23:28 -04:00
# include <QDebug>
2013-04-13 02:13:08 -03:00
# include <QLibraryInfo>
# include <QLocale>
2011-06-05 10:03:29 -04:00
# include <QMessageBox>
2013-04-13 02:13:08 -03:00
# include <QSettings>
2014-09-05 07:18:35 -04:00
# include <QThread>
2013-04-13 02:13:08 -03:00
# include <QTimer>
# include <QTranslator>
2013-12-02 18:54:19 -03:00
# if defined(QT_STATICPLUGIN)
2012-03-24 12:52:43 -03:00
# include <QtPlugin>
2014-06-03 23:42:53 -04:00
# if defined(QT_QPA_PLATFORM_XCB)
Q_IMPORT_PLUGIN ( QXcbIntegrationPlugin ) ;
# elif defined(QT_QPA_PLATFORM_WINDOWS)
2013-12-02 18:54:19 -03:00
Q_IMPORT_PLUGIN ( QWindowsIntegrationPlugin ) ;
2014-06-03 23:42:53 -04:00
# elif defined(QT_QPA_PLATFORM_COCOA)
Q_IMPORT_PLUGIN ( QCocoaIntegrationPlugin ) ;
# endif
2013-12-02 18:54:19 -03:00
# endif
2012-03-24 12:52:43 -03:00
2013-04-14 13:50:40 -03:00
// Declare meta types used for QMetaObject::invokeMethod
Q_DECLARE_METATYPE ( bool * )
2014-10-01 05:09:08 -03:00
Q_DECLARE_METATYPE ( CAmount )
2018-03-01 06:40:36 -03:00
Q_DECLARE_METATYPE ( uint256 )
2013-04-14 13:50:40 -03:00
2015-01-16 18:03:48 -03:00
static QString GetLangTerritory ( )
{
QSettings settings ;
// Get desired locale (e.g. "de_DE")
// 1) System default language
QString lang_territory = QLocale : : system ( ) . name ( ) ;
// 2) Language from QSettings
QString lang_territory_qsettings = settings . value ( " language " , " " ) . toString ( ) ;
if ( ! lang_territory_qsettings . isEmpty ( ) )
lang_territory = lang_territory_qsettings ;
// 3) -lang command line argument
2017-08-01 15:17:40 -04:00
lang_territory = QString : : fromStdString ( gArgs . GetArg ( " -lang " , lang_territory . toStdString ( ) ) ) ;
2015-01-16 18:03:48 -03:00
return lang_territory ;
}
2013-05-19 11:36:01 -04:00
/** Set up translations */
static void initTranslations ( QTranslator & qtTranslatorBase , QTranslator & qtTranslator , QTranslator & translatorBase , QTranslator & translator )
{
2014-03-11 04:32:07 -03:00
// Remove old translators
QApplication : : removeTranslator ( & qtTranslatorBase ) ;
QApplication : : removeTranslator ( & qtTranslator ) ;
QApplication : : removeTranslator ( & translatorBase ) ;
QApplication : : removeTranslator ( & translator ) ;
2013-05-19 11:36:01 -04:00
// Get desired locale (e.g. "de_DE")
// 1) System default language
2015-01-16 18:03:48 -03:00
QString lang_territory = GetLangTerritory ( ) ;
2013-05-19 11:36:01 -04:00
// Convert to "de" only by truncating "_DE"
QString lang = lang_territory ;
lang . truncate ( lang_territory . lastIndexOf ( ' _ ' ) ) ;
// Load language files for configured locale:
// - First load the translator for the base language, without territory
// - Then load the more specific locale translator
// Load e.g. qt_de.qm
if ( qtTranslatorBase . load ( " qt_ " + lang , QLibraryInfo : : location ( QLibraryInfo : : TranslationsPath ) ) )
QApplication : : installTranslator ( & qtTranslatorBase ) ;
// Load e.g. qt_de_DE.qm
if ( qtTranslator . load ( " qt_ " + lang_territory , QLibraryInfo : : location ( QLibraryInfo : : TranslationsPath ) ) )
QApplication : : installTranslator ( & qtTranslator ) ;
// Load e.g. bitcoin_de.qm (shortcut "de" needs to be defined in bitcoin.qrc)
if ( translatorBase . load ( lang , " :/translations/ " ) )
QApplication : : installTranslator ( & translatorBase ) ;
// Load e.g. bitcoin_de_DE.qm (shortcut "de_DE" needs to be defined in bitcoin.qrc)
if ( translator . load ( lang_territory , " :/translations/ " ) )
QApplication : : installTranslator ( & translator ) ;
}
2013-07-23 03:01:08 -04:00
/* qDebug() message handler --> debug.log */
void DebugMessageHandler ( QtMsgType type , const QMessageLogContext & context , const QString & msg )
{
2013-09-28 14:29:44 -03:00
Q_UNUSED ( context ) ;
2016-12-25 17:19:40 -03:00
if ( type = = QtDebugMsg ) {
LogPrint ( BCLog : : QT , " GUI: %s \n " , msg . toStdString ( ) ) ;
} else {
LogPrintf ( " GUI: %s \n " , msg . toStdString ( ) ) ;
}
2013-07-23 03:01:08 -04:00
}
2018-04-07 04:42:02 -03:00
BitcoinCore : : BitcoinCore ( interfaces : : Node & node ) :
2017-04-17 14:55:43 -03:00
QObject ( ) , m_node ( node )
2014-01-07 07:30:17 -03:00
{
}
2014-12-07 09:29:06 -03:00
void BitcoinCore : : handleRunawayException ( const std : : exception * e )
2014-01-07 07:30:17 -03:00
{
PrintExceptionContinue ( e , " Runaway exception " ) ;
2017-04-17 14:55:43 -03:00
Q_EMIT runawayException ( QString : : fromStdString ( m_node . getWarnings ( " gui " ) ) ) ;
2017-07-15 04:46:06 -04:00
}
2014-01-07 07:30:17 -03:00
void BitcoinCore : : initialize ( )
{
try
{
2017-02-23 04:38:41 -03:00
qDebug ( ) < < __func__ < < " : Running initialization in thread " ;
2018-06-13 14:50:59 -04:00
util : : ThreadRename ( " qt-init " ) ;
2017-04-17 14:55:43 -03:00
bool rv = m_node . appInitMain ( ) ;
2015-07-14 08:59:05 -03:00
Q_EMIT initializeResult ( rv ) ;
2014-12-07 09:29:06 -03:00
} catch ( const std : : exception & e ) {
2014-01-07 07:30:17 -03:00
handleRunawayException ( & e ) ;
} catch ( . . . ) {
2017-08-07 01:36:37 -04:00
handleRunawayException ( nullptr ) ;
2014-01-07 07:30:17 -03:00
}
}
void BitcoinCore : : shutdown ( )
{
try
{
2014-07-15 04:23:28 -04:00
qDebug ( ) < < __func__ < < " : Running Shutdown in thread " ;
2017-04-17 14:55:43 -03:00
m_node . appShutdown ( ) ;
2014-07-15 04:23:28 -04:00
qDebug ( ) < < __func__ < < " : Shutdown finished " ;
2017-02-23 05:12:36 -03:00
Q_EMIT shutdownResult ( ) ;
2014-12-07 09:29:06 -03:00
} catch ( const std : : exception & e ) {
2014-01-07 07:30:17 -03:00
handleRunawayException ( & e ) ;
} catch ( . . . ) {
2017-08-07 01:36:37 -04:00
handleRunawayException ( nullptr ) ;
2014-01-07 07:30:17 -03:00
}
}
2019-08-09 15:39:30 -04:00
static int qt_argc = 1 ;
static const char * qt_argv = " bitcoin-qt " ;
BitcoinApplication : : BitcoinApplication ( interfaces : : Node & node ) :
QApplication ( qt_argc , const_cast < char * * > ( & qt_argv ) ) ,
2018-07-30 06:37:09 -04:00
coreThread ( nullptr ) ,
2017-04-17 14:55:43 -03:00
m_node ( node ) ,
2018-07-30 06:37:09 -04:00
optionsModel ( nullptr ) ,
clientModel ( nullptr ) ,
window ( nullptr ) ,
pollShutdownTimer ( nullptr ) ,
2018-04-28 20:40:51 -03:00
returnValue ( 0 ) ,
2018-07-30 06:37:09 -04:00
platformStyle ( nullptr )
2014-01-07 07:30:17 -03:00
{
setQuitOnLastWindowClosed ( false ) ;
2018-04-28 20:40:51 -03:00
}
2015-07-28 10:20:14 -03:00
2018-04-28 20:40:51 -03:00
void BitcoinApplication : : setupPlatformStyle ( )
{
2015-07-28 10:20:14 -03:00
// UI per-platform customization
// This must be done inside the BitcoinApplication constructor, or after it, because
// PlatformStyle::instantiate requires a QApplication
2015-11-28 18:44:55 -03:00
std : : string platformName ;
2017-08-01 15:17:40 -04:00
platformName = gArgs . GetArg ( " -uiplatform " , BitcoinGUI : : DEFAULT_UIPLATFORM ) ;
2015-07-28 10:20:14 -03:00
platformStyle = PlatformStyle : : instantiate ( QString : : fromStdString ( platformName ) ) ;
if ( ! platformStyle ) // Fall back to "other" if specified name not found
platformStyle = PlatformStyle : : instantiate ( " other " ) ;
assert ( platformStyle ) ;
2014-01-07 07:30:17 -03:00
}
BitcoinApplication : : ~ BitcoinApplication ( )
{
2014-07-15 10:26:16 -04:00
if ( coreThread )
{
qDebug ( ) < < __func__ < < " : Stopping thread " ;
2018-09-17 19:17:22 -03:00
coreThread - > quit ( ) ;
2014-07-15 10:26:16 -04:00
coreThread - > wait ( ) ;
qDebug ( ) < < __func__ < < " : Stopped thread " ;
}
2014-01-08 04:59:24 -03:00
delete window ;
2018-07-30 06:37:09 -04:00
window = nullptr ;
2014-01-08 04:59:24 -03:00
delete optionsModel ;
2018-07-30 06:37:09 -04:00
optionsModel = nullptr ;
2015-07-28 10:20:14 -03:00
delete platformStyle ;
2018-07-30 06:37:09 -04:00
platformStyle = nullptr ;
2014-01-07 07:30:17 -03:00
}
2013-12-11 11:00:56 -03:00
# ifdef ENABLE_WALLET
2014-01-07 07:30:17 -03:00
void BitcoinApplication : : createPaymentServer ( )
{
paymentServer = new PaymentServer ( this ) ;
}
2013-12-11 11:00:56 -03:00
# endif
2014-01-07 07:30:17 -03:00
2015-11-13 12:27:42 -03:00
void BitcoinApplication : : createOptionsModel ( bool resetSettings )
2014-01-07 07:30:17 -03:00
{
2017-04-17 15:23:14 -03:00
optionsModel = new OptionsModel ( m_node , nullptr , resetSettings ) ;
2014-01-07 07:30:17 -03:00
}
2014-10-09 06:04:49 -03:00
void BitcoinApplication : : createWindow ( const NetworkStyle * networkStyle )
2014-01-07 07:30:17 -03:00
{
2018-07-31 14:02:34 -04:00
window = new BitcoinGUI ( m_node , platformStyle , networkStyle , nullptr ) ;
2014-01-07 07:30:17 -03:00
2014-01-08 06:45:00 -03:00
pollShutdownTimer = new QTimer ( window ) ;
2018-06-24 11:18:22 -04:00
connect ( pollShutdownTimer , & QTimer : : timeout , window , & BitcoinGUI : : detectShutdown ) ;
2014-01-07 07:30:17 -03:00
}
2014-10-09 06:04:49 -03:00
void BitcoinApplication : : createSplashScreen ( const NetworkStyle * networkStyle )
2014-01-08 04:59:24 -03:00
{
2018-07-31 14:02:34 -04:00
SplashScreen * splash = new SplashScreen ( m_node , nullptr , networkStyle ) ;
2016-11-19 07:08:19 -03:00
// We don't hold a direct pointer to the splash screen after creation, but the splash
2018-12-01 20:26:28 -03:00
// screen will take care of deleting itself when finish() happens.
2014-01-08 04:59:24 -03:00
splash - > show ( ) ;
2018-12-01 20:26:28 -03:00
connect ( this , & BitcoinApplication : : splashFinished , splash , & SplashScreen : : finish ) ;
2018-06-24 11:18:22 -04:00
connect ( this , & BitcoinApplication : : requestedShutdown , splash , & QWidget : : close ) ;
2014-01-08 04:59:24 -03:00
}
2017-11-06 22:11:43 -03:00
bool BitcoinApplication : : baseInitialize ( )
{
return m_node . baseInitialize ( ) ;
}
2014-01-07 07:30:17 -03:00
void BitcoinApplication : : startThread ( )
{
2014-07-15 10:26:16 -04:00
if ( coreThread )
return ;
2014-01-07 07:30:17 -03:00
coreThread = new QThread ( this ) ;
2017-04-17 14:55:43 -03:00
BitcoinCore * executor = new BitcoinCore ( m_node ) ;
2014-01-07 07:30:17 -03:00
executor - > moveToThread ( coreThread ) ;
/* communication to and from thread */
2018-06-24 11:18:22 -04:00
connect ( executor , & BitcoinCore : : initializeResult , this , & BitcoinApplication : : initializeResult ) ;
connect ( executor , & BitcoinCore : : shutdownResult , this , & BitcoinApplication : : shutdownResult ) ;
connect ( executor , & BitcoinCore : : runawayException , this , & BitcoinApplication : : handleRunawayException ) ;
connect ( this , & BitcoinApplication : : requestedInitialize , executor , & BitcoinCore : : initialize ) ;
connect ( this , & BitcoinApplication : : requestedShutdown , executor , & BitcoinCore : : shutdown ) ;
2014-01-07 07:30:17 -03:00
/* make sure executor object is deleted in its own thread */
2018-09-17 19:17:22 -03:00
connect ( coreThread , & QThread : : finished , executor , & QObject : : deleteLater ) ;
2014-01-07 07:30:17 -03:00
coreThread - > start ( ) ;
}
2015-10-08 05:01:29 -03:00
void BitcoinApplication : : parameterSetup ( )
{
2018-04-23 04:25:34 -03:00
// Default printtoconsole to false for the GUI. GUI programs should not
// print to the console unnecessarily.
gArgs . SoftSetBoolArg ( " -printtoconsole " , false ) ;
2017-04-17 14:55:43 -03:00
m_node . initLogging ( ) ;
m_node . initParameterInteraction ( ) ;
2015-10-08 05:01:29 -03:00
}
2019-11-12 07:09:10 -03:00
void BitcoinApplication : : SetPrune ( bool prune , bool force )
{
// If prune is set, intentionally override existing prune size with
// the default size since this is called when choosing a new datadir.
optionsModel - > SetPruneTargetGB ( prune ? DEFAULT_PRUNE_TARGET_GB : 0 , force ) ;
2019-08-24 16:31:14 -04:00
}
2014-01-07 07:30:17 -03:00
void BitcoinApplication : : requestInitialize ( )
{
2014-07-15 04:23:28 -04:00
qDebug ( ) < < __func__ < < " : Requesting initialize " ;
2014-07-15 10:26:16 -04:00
startThread ( ) ;
2015-07-14 08:59:05 -03:00
Q_EMIT requestedInitialize ( ) ;
2014-01-07 07:30:17 -03:00
}
void BitcoinApplication : : requestShutdown ( )
{
2016-11-18 12:35:14 -03:00
// Show a simple window indicating shutdown status
// Do this first as some of the steps may take some time below,
// for example the RPC console may still be executing a command.
2016-11-19 10:28:55 -03:00
shutdownWindow . reset ( ShutdownWindow : : showShutdownWindow ( window ) ) ;
2016-11-18 12:35:14 -03:00
2014-07-15 04:23:28 -04:00
qDebug ( ) < < __func__ < < " : Requesting shutdown " ;
2014-07-15 10:26:16 -04:00
startThread ( ) ;
2014-01-07 07:30:17 -03:00
window - > hide ( ) ;
2019-01-28 21:08:48 -03:00
// Must disconnect node signals otherwise current thread can deadlock since
// no event loop is running.
window - > unsubscribeFromCoreSignals ( ) ;
// Request node shutdown, which can interrupt long operations, like
// rescanning a wallet.
m_node . startShutdown ( ) ;
// Unsetting the client model can cause the current thread to wait for node
2019-11-04 06:22:53 -03:00
// to complete an operation, like wait for a RPC execution to complete.
2018-07-30 06:37:09 -04:00
window - > setClientModel ( nullptr ) ;
2014-01-08 06:45:00 -03:00
pollShutdownTimer - > stop ( ) ;
2014-01-07 12:04:47 -03:00
2014-01-08 04:59:24 -03:00
delete clientModel ;
2018-07-30 06:37:09 -04:00
clientModel = nullptr ;
2014-01-07 12:04:47 -03:00
// Request shutdown from core thread
2015-07-14 08:59:05 -03:00
Q_EMIT requestedShutdown ( ) ;
2014-01-07 07:30:17 -03:00
}
2017-02-23 05:12:36 -03:00
void BitcoinApplication : : initializeResult ( bool success )
2014-01-07 07:30:17 -03:00
{
2017-02-23 05:12:36 -03:00
qDebug ( ) < < __func__ < < " : Initialization result: " < < success ;
// Set exit result.
returnValue = success ? EXIT_SUCCESS : EXIT_FAILURE ;
if ( success )
2014-01-07 07:30:17 -03:00
{
2017-02-23 04:38:41 -03:00
// Log this only after AppInitMain finishes, as then logging setup is guaranteed complete
2019-06-21 13:22:13 -04:00
qInfo ( ) < < " Platform customization: " < < platformStyle - > getName ( ) ;
2017-04-17 16:37:36 -03:00
clientModel = new ClientModel ( m_node , optionsModel ) ;
2014-01-07 07:30:17 -03:00
window - > setClientModel ( clientModel ) ;
2013-12-11 11:00:56 -03:00
# ifdef ENABLE_WALLET
2019-07-23 08:38:10 -04:00
if ( WalletModel : : isWalletEnabled ( ) ) {
m_wallet_controller = new WalletController ( m_node , platformStyle , optionsModel , this ) ;
window - > setWalletController ( m_wallet_controller ) ;
if ( paymentServer ) {
paymentServer - > setOptionsModel ( optionsModel ) ;
}
}
# endif // ENABLE_WALLET
2014-01-07 07:30:17 -03:00
2018-10-19 04:39:25 -03:00
// If -min option passed, start window minimized (iconified) or minimized to tray
if ( ! gArgs . GetBoolArg ( " -min " , false ) ) {
2014-01-07 07:30:17 -03:00
window - > show ( ) ;
2018-10-19 04:39:25 -03:00
} else if ( clientModel - > getOptionsModel ( ) - > getMinimizeToTray ( ) & & window - > hasTrayIcon ( ) ) {
// do nothing as the window is managed by the tray icon
} else {
window - > showMinimized ( ) ;
2014-01-07 07:30:17 -03:00
}
2018-12-01 20:26:28 -03:00
Q_EMIT splashFinished ( ) ;
2017-11-06 22:11:43 -03:00
Q_EMIT windowShown ( window ) ;
2014-09-18 08:14:38 -03:00
2013-12-11 11:00:56 -03:00
# ifdef ENABLE_WALLET
2014-01-07 07:30:17 -03:00
// Now that initialization/startup is done, process any command-line
// bitcoin: URIs or payment requests:
2017-11-06 22:11:43 -03:00
if ( paymentServer ) {
connect ( paymentServer , & PaymentServer : : receivedPaymentRequest , window , & BitcoinGUI : : handlePaymentRequest ) ;
connect ( window , & BitcoinGUI : : receivedURI , paymentServer , & PaymentServer : : handleURIOrFile ) ;
connect ( paymentServer , & PaymentServer : : message , [ this ] ( const QString & title , const QString & message , unsigned int style ) {
window - > message ( title , message , style ) ;
} ) ;
QTimer : : singleShot ( 100 , paymentServer , & PaymentServer : : uiReady ) ;
}
2013-12-11 11:00:56 -03:00
# endif
2018-02-07 17:18:54 -03:00
pollShutdownTimer - > start ( 200 ) ;
2014-01-07 07:30:17 -03:00
} else {
2018-12-01 20:26:28 -03:00
Q_EMIT splashFinished ( ) ; // Make sure splash screen doesn't stick around during shutdown
2018-02-07 06:37:19 -03:00
quit ( ) ; // Exit first main loop invocation
2014-01-07 07:30:17 -03:00
}
}
2017-02-23 05:12:36 -03:00
void BitcoinApplication : : shutdownResult ( )
2014-01-07 07:30:17 -03:00
{
2018-02-07 06:37:19 -03:00
quit ( ) ; // Exit second main loop invocation after shutdown finished
2014-01-07 07:30:17 -03:00
}
void BitcoinApplication : : handleRunawayException ( const QString & message )
{
2018-07-31 14:02:34 -04:00
QMessageBox : : critical ( nullptr , " Runaway exception " , BitcoinGUI : : tr ( " A fatal error occurred. Bitcoin can no longer continue safely and will quit. " ) + QString ( " \n \n " ) + message ) ;
2016-11-07 15:31:38 -03:00
: : exit ( EXIT_FAILURE ) ;
2014-01-07 07:30:17 -03:00
}
2014-04-10 03:19:58 -03:00
WId BitcoinApplication : : getMainWinId ( ) const
{
if ( ! window )
return 0 ;
return window - > winId ( ) ;
}
2018-04-28 17:54:58 -03:00
static void SetupUIArgs ( )
{
scripted-diff: Use ArgsManager::DEBUG_ONLY flag
-BEGIN VERIFY SCRIPT-
sed -i 's/unsigned int flags, const bool debug_only,/unsigned int flags,/' src/util/system.h src/util/system.cpp
sed -i 's/ArgsManager::NONE, debug_only/flags, false/' src/util/system.cpp
sed -i 's/arg.second.m_debug_only/(arg.second.m_flags \& ArgsManager::DEBUG_ONLY)/' src/util/system.cpp
sed -i 's/ArgsManager::ALLOW_ANY, true, OptionsCategory::/ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::/' $(git grep --files-with-matches 'AddArg(' src)
sed -i 's/ArgsManager::ALLOW_ANY, false, OptionsCategory::/ArgsManager::ALLOW_ANY, OptionsCategory::/' $(git grep --files-with-matches 'AddArg(' src)
-END VERIFY SCRIPT-
2019-07-27 05:06:32 -04:00
gArgs . AddArg ( " -choosedatadir " , strprintf ( " Choose data directory on startup (default: %u) " , DEFAULT_CHOOSE_DATADIR ) , ArgsManager : : ALLOW_ANY , OptionsCategory : : GUI ) ;
gArgs . AddArg ( " -lang=<lang> " , " Set language, for example \" de_DE \" (default: system locale) " , ArgsManager : : ALLOW_ANY , OptionsCategory : : GUI ) ;
gArgs . AddArg ( " -min " , " Start minimized " , ArgsManager : : ALLOW_ANY , OptionsCategory : : GUI ) ;
gArgs . AddArg ( " -resetguisettings " , " Reset all settings changed in the GUI " , ArgsManager : : ALLOW_ANY , OptionsCategory : : GUI ) ;
gArgs . AddArg ( " -splash " , strprintf ( " Show splash screen on startup (default: %u) " , DEFAULT_SPLASHSCREEN ) , ArgsManager : : ALLOW_ANY , OptionsCategory : : GUI ) ;
gArgs . AddArg ( " -uiplatform " , strprintf ( " Select platform to customize UI for (one of windows, macosx, other; default: %s) " , BitcoinGUI : : DEFAULT_UIPLATFORM ) , ArgsManager : : ALLOW_ANY | ArgsManager : : DEBUG_ONLY , OptionsCategory : : GUI ) ;
2018-04-28 17:54:58 -03:00
}
2017-11-06 22:11:43 -03:00
int GuiMain ( int argc , char * argv [ ] )
2011-05-07 17:13:39 -03:00
{
2018-08-05 12:38:25 -04:00
# ifdef WIN32
util : : WinCmdLineArgs winArgs ;
std : : tie ( argc , argv ) = winArgs . get ( ) ;
# endif
2014-05-13 06:15:00 -04:00
SetupEnvironment ( ) ;
2019-10-03 01:08:52 -03:00
util : : ThreadSetInternalName ( " main " ) ;
2014-05-13 06:15:00 -04:00
2018-04-07 04:42:02 -03:00
std : : unique_ptr < interfaces : : Node > node = interfaces : : MakeNode ( ) ;
2017-04-17 14:55:43 -03:00
2018-11-23 13:06:32 -03:00
// Subscribe to global signals from core
std : : unique_ptr < interfaces : : Handler > handler_message_box = node - > handleMessageBox ( noui_ThreadSafeMessageBox ) ;
std : : unique_ptr < interfaces : : Handler > handler_question = node - > handleQuestion ( noui_ThreadSafeQuestion ) ;
std : : unique_ptr < interfaces : : Handler > handler_init_message = node - > handleInitMessage ( noui_InitMessage ) ;
2014-01-05 08:37:51 -03:00
// Do not refer to data directory yet, this can be overridden by Intro::pickDataDirectory
2013-01-03 18:06:18 -03:00
2018-04-28 20:40:51 -03:00
/// 1. Basic Qt initialization (not dependent on parameters or configuration)
2011-06-18 08:25:24 -04:00
Q_INIT_RESOURCE ( bitcoin ) ;
2014-06-10 15:43:02 -04:00
Q_INIT_RESOURCE ( bitcoin_locale ) ;
2014-08-06 15:06:40 -04:00
2013-11-21 13:59:31 -03:00
// Generate high-dpi pixmaps
QApplication : : setAttribute ( Qt : : AA_UseHighDpiPixmaps ) ;
2016-06-20 04:17:01 -04:00
# if QT_VERSION >= 0x050600
2019-06-20 14:24:22 -04:00
QCoreApplication : : setAttribute ( Qt : : AA_EnableHighDpiScaling ) ;
2016-06-20 04:17:01 -04:00
# endif
2011-06-23 16:35:30 -04:00
2019-08-09 15:39:30 -04:00
BitcoinApplication app ( * node ) ;
2019-06-20 14:24:22 -04:00
2019-11-10 07:10:36 -03:00
// Register meta types used for QMetaObject::invokeMethod and Qt::QueuedConnection
qRegisterMetaType < bool * > ( ) ;
2019-07-08 16:30:15 -04:00
# ifdef ENABLE_WALLET
qRegisterMetaType < WalletModel * > ( ) ;
# endif
2019-11-10 07:10:36 -03:00
// Register typedefs (see http://qt-project.org/doc/qt-5/qmetatype.html#qRegisterMetaType)
// IMPORTANT: if CAmount is no longer a typedef use the normal variant above (see https://doc.qt.io/qt-5/qmetatype.html#qRegisterMetaType-1)
qRegisterMetaType < CAmount > ( " CAmount " ) ;
2019-11-10 07:03:53 -03:00
qRegisterMetaType < size_t > ( " size_t " ) ;
2019-11-10 07:10:36 -03:00
qRegisterMetaType < std : : function < void ( ) > > ( " std::function<void()> " ) ;
2019-01-21 13:58:20 -03:00
qRegisterMetaType < QMessageBox : : Icon > ( " QMessageBox::Icon " ) ;
2019-11-10 07:10:36 -03:00
2018-04-28 20:40:51 -03:00
/// 2. Parse command-line options. We do this after qt in order to show an error if there are problems parsing these
// Command-line options take precedence:
node - > setupServerArgs ( ) ;
SetupUIArgs ( ) ;
std : : string error ;
if ( ! node - > parseParameters ( argc , argv , error ) ) {
2019-07-11 18:20:44 -04:00
node - > initError ( strprintf ( " Error parsing command line arguments: %s \n " , error ) ) ;
2019-07-10 08:06:57 -04:00
// Create a message box, because the gui has neither been created nor has subscribed to core signals
2019-06-26 10:28:13 -04:00
QMessageBox : : critical ( nullptr , PACKAGE_NAME ,
2019-07-10 08:06:57 -04:00
// message can not be translated because translations have not been initialized
QString : : fromStdString ( " Error parsing command line arguments: %1. " ) . arg ( QString : : fromStdString ( error ) ) ) ;
2018-04-28 20:40:51 -03:00
return EXIT_FAILURE ;
}
// Now that the QApplication is setup and we have parsed our parameters, we can set the platform style
app . setupPlatformStyle ( ) ;
2014-01-05 08:37:51 -03:00
/// 3. Application identification
// must be set before OptionsModel is initialized or translations are loaded,
// as it is used to locate QSettings
2014-03-11 04:32:07 -03:00
QApplication : : setOrganizationName ( QAPP_ORG_NAME ) ;
QApplication : : setOrganizationDomain ( QAPP_ORG_DOMAIN ) ;
QApplication : : setApplicationName ( QAPP_APP_NAME_DEFAULT ) ;
2013-05-19 11:36:01 -04:00
2014-01-05 08:37:51 -03:00
/// 4. Initialization of translations, so that intro dialog is in user's language
2013-05-19 11:36:01 -04:00
// Now that QSettings are accessible, initialize translations
QTranslator qtTranslatorBase , qtTranslator , translatorBase , translator ;
initTranslations ( qtTranslatorBase , qtTranslator , translatorBase , translator ) ;
2014-01-05 08:37:51 -03:00
// Show help message immediately after parsing command-line options (for "-lang") and setting locale,
// but before showing splash screen.
2018-03-30 17:47:36 -03:00
if ( HelpRequested ( gArgs ) | | gArgs . IsArgSet ( " -version " ) ) {
2017-04-17 15:40:41 -03:00
HelpMessageDialog help ( * node , nullptr , gArgs . IsArgSet ( " -version " ) ) ;
2014-01-05 08:37:51 -03:00
help . showOrPrint ( ) ;
2016-11-07 15:31:38 -03:00
return EXIT_SUCCESS ;
2013-06-23 12:04:44 -04:00
}
2014-01-05 08:37:51 -03:00
/// 5. Now that settings and translations are available, ask user for data directory
// User language is set up: pick a data directory
2019-08-24 16:34:59 -04:00
bool did_show_intro = false ;
2019-08-24 13:13:04 -04:00
bool prune = false ; // Intro dialog prune check box
2019-08-24 16:34:59 -04:00
// Gracefully exit if the user cancels
2019-08-24 13:13:04 -04:00
if ( ! Intro : : showIfNeeded ( * node , did_show_intro , prune ) ) return EXIT_SUCCESS ;
2014-01-05 08:37:51 -03:00
2019-07-24 11:54:32 -04:00
/// 6. Determine availability of data directory and parse bitcoin.conf
2014-03-11 04:32:07 -03:00
/// - Do not call GetDataDir(true) before this step finishes
2019-07-24 11:54:32 -04:00
if ( ! CheckDataDirOption ( ) ) {
2019-07-11 18:20:44 -04:00
node - > initError ( strprintf ( " Specified data directory \" %s \" does not exist. \n " , gArgs . GetArg ( " -datadir " , " " ) ) ) ;
2019-06-26 10:28:13 -04:00
QMessageBox : : critical ( nullptr , PACKAGE_NAME ,
2018-07-31 14:02:34 -04:00
QObject : : tr ( " Error: Specified data directory \" %1 \" does not exist. " ) . arg ( QString : : fromStdString ( gArgs . GetArg ( " -datadir " , " " ) ) ) ) ;
2016-11-07 15:31:38 -03:00
return EXIT_FAILURE ;
2014-01-05 08:37:51 -03:00
}
2018-04-28 20:40:51 -03:00
if ( ! node - > readConfigFiles ( error ) ) {
2019-07-11 18:20:44 -04:00
node - > initError ( strprintf ( " Error reading configuration file: %s \n " , error ) ) ;
2019-06-26 10:28:13 -04:00
QMessageBox : : critical ( nullptr , PACKAGE_NAME ,
2018-04-28 20:40:51 -03:00
QObject : : tr ( " Error: Cannot parse configuration file: %1. " ) . arg ( QString : : fromStdString ( error ) ) ) ;
2016-11-07 15:31:38 -03:00
return EXIT_FAILURE ;
2014-04-07 05:10:01 -03:00
}
2014-01-05 08:37:51 -03:00
2014-03-11 04:32:07 -03:00
/// 7. Determine network (and switch to network specific options)
// - Do not call Params() before this step
// - Do this after parsing the configuration file, as the network can be switched there
// - QSettings() will use the new application name after this, resulting in network-specific settings
// - Needs to be done before createOptionsModel
2016-10-13 17:38:10 -03:00
// Check for -chain, -testnet or -regtest parameter (Params() calls are only valid after this clause)
2015-05-25 04:00:17 -03:00
try {
2018-03-29 02:00:00 -03:00
node - > selectParams ( gArgs . GetChainName ( ) ) ;
2015-05-25 04:00:17 -03:00
} catch ( std : : exception & e ) {
2019-07-11 18:20:44 -04:00
node - > initError ( strprintf ( " %s \n " , e . what ( ) ) ) ;
2019-06-26 10:28:13 -04:00
QMessageBox : : critical ( nullptr , PACKAGE_NAME , QObject : : tr ( " Error: %1 " ) . arg ( e . what ( ) ) ) ;
2016-11-07 15:31:38 -03:00
return EXIT_FAILURE ;
2014-03-11 04:32:07 -03:00
}
# ifdef ENABLE_WALLET
// Parse URIs on command line -- this can affect Params()
2018-03-23 18:14:39 -03:00
PaymentServer : : ipcParseCommandLine ( * node , argc , argv ) ;
2014-03-11 04:32:07 -03:00
# endif
2014-11-05 07:42:51 -03:00
2016-10-13 18:24:21 -03:00
QScopedPointer < const NetworkStyle > networkStyle ( NetworkStyle : : instantiate ( Params ( ) . NetworkIDString ( ) ) ) ;
2014-10-09 06:04:49 -03:00
assert ( ! networkStyle . isNull ( ) ) ;
2014-03-11 04:32:07 -03:00
// Allow for separate UI settings for testnets
2014-10-09 06:04:49 -03:00
QApplication : : setApplicationName ( networkStyle - > getAppName ( ) ) ;
2014-03-11 04:32:07 -03:00
// Re-initialize translations after changing application name (language in network-specific settings can be different)
initTranslations ( qtTranslatorBase , qtTranslator , translatorBase , translator ) ;
2013-12-11 11:00:56 -03:00
# ifdef ENABLE_WALLET
2014-03-11 04:32:07 -03:00
/// 8. URI IPC sending
2014-01-05 08:37:51 -03:00
// - Do this early as we don't want to bother initializing if we are just calling IPC
// - Do this *after* setting up the data directory, as the data directory hash is used in the name
// of the server.
// - Do this after creating app and setting up translations, so errors are
// translated properly.
if ( PaymentServer : : ipcSendCommandLine ( ) )
2016-11-07 15:31:38 -03:00
exit ( EXIT_SUCCESS ) ;
2014-01-05 08:37:51 -03:00
2013-07-18 02:50:17 -04:00
// Start up the payment server early, too, so impatient users that click on
// bitcoin: links repeatedly have their payment requests routed to this process:
2019-07-23 08:38:10 -04:00
if ( WalletModel : : isWalletEnabled ( ) ) {
app . createPaymentServer ( ) ;
}
# endif // ENABLE_WALLET
2013-07-18 02:50:17 -04:00
2014-03-11 04:32:07 -03:00
/// 9. Main GUI initialization
2012-04-13 12:10:50 -03:00
// Install global event filter that makes sure that long tooltips can be word-wrapped
app . installEventFilter ( new GUIUtil : : ToolTipToRichTextFilter ( TOOLTIP_WRAP_THRESHOLD , & app ) ) ;
2014-04-10 03:19:58 -03:00
# if defined(Q_OS_WIN)
// Install global event filter for processing Windows session related Windows messages (WM_QUERYENDSESSION and WM_ENDSESSION)
qApp - > installNativeEventFilter ( new WinShutdownMonitor ( ) ) ;
# endif
// Install qDebug() message handler to route to debug.log
2013-08-31 09:32:35 -04:00
qInstallMessageHandler ( DebugMessageHandler ) ;
2015-10-08 05:01:29 -03:00
// Allow parameter interaction before we create the options model
app . parameterSetup ( ) ;
2014-01-05 08:37:51 -03:00
// Load GUI settings from QSettings
2018-03-26 16:13:33 -03:00
app . createOptionsModel ( gArgs . GetBoolArg ( " -resetguisettings " , false ) ) ;
2012-02-16 23:09:41 -03:00
2019-08-24 13:13:04 -04:00
if ( did_show_intro ) {
// Store intro dialog settings other than datadir (network specific)
app . SetPrune ( prune , true ) ;
}
2017-08-01 15:17:40 -04:00
if ( gArgs . GetBoolArg ( " -splash " , DEFAULT_SPLASHSCREEN ) & & ! gArgs . GetBoolArg ( " -min " , false ) )
2014-10-09 06:04:49 -03:00
app . createSplashScreen ( networkStyle . data ( ) ) ;
2011-05-07 17:13:39 -03:00
2017-07-15 04:46:06 -04:00
int rv = EXIT_SUCCESS ;
2011-06-07 12:59:01 -04:00
try
{
2014-10-09 06:04:49 -03:00
app . createWindow ( networkStyle . data ( ) ) ;
2017-07-15 04:46:06 -04:00
// Perform base initialization before spinning up initialization/shutdown thread
// This is acceptable because this function only contains steps that are quick to execute,
// so the GUI thread won't be held up.
2017-11-06 22:11:43 -03:00
if ( app . baseInitialize ( ) ) {
2017-07-15 04:46:06 -04:00
app . requestInitialize ( ) ;
2018-06-13 10:02:39 -04:00
# if defined(Q_OS_WIN)
2019-06-26 10:28:13 -04:00
WinShutdownMonitor : : registerShutdownBlockReason ( QObject : : tr ( " %1 didn't yet exit safely... " ) . arg ( PACKAGE_NAME ) , ( HWND ) app . getMainWinId ( ) ) ;
2014-04-10 03:19:58 -03:00
# endif
2017-07-15 04:46:06 -04:00
app . exec ( ) ;
app . requestShutdown ( ) ;
app . exec ( ) ;
rv = app . getReturnValue ( ) ;
} else {
// A dialog with detailed error will have been shown by InitError()
rv = EXIT_FAILURE ;
}
2014-12-07 09:29:06 -03:00
} catch ( const std : : exception & e ) {
2014-01-07 07:30:17 -03:00
PrintExceptionContinue ( & e , " Runaway exception " ) ;
2017-04-17 14:55:43 -03:00
app . handleRunawayException ( QString : : fromStdString ( node - > getWarnings ( " gui " ) ) ) ;
2011-05-22 11:19:43 -04:00
} catch ( . . . ) {
2017-08-07 01:36:37 -04:00
PrintExceptionContinue ( nullptr , " Runaway exception " ) ;
2017-04-17 14:55:43 -03:00
app . handleRunawayException ( QString : : fromStdString ( node - > getWarnings ( " gui " ) ) ) ;
2011-05-22 11:19:43 -04:00
}
2017-07-15 04:46:06 -04:00
return rv ;
2011-05-07 17:13:39 -03:00
}