2021-12-30 19:36:57 +02:00
// Copyright (c) 2011-2021 The Bitcoin Core developers
2014-12-13 12:09:33 +08:00
// Distributed under the MIT software license, see the accompanying
2013-11-04 16:20:43 +01:00
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2013-01-23 21:51:02 +01:00
2014-01-11 18:56:24 +01:00
# if defined(HAVE_CONFIG_H)
2017-11-10 13:57:53 +13:00
# include <config/bitcoin-config.h>
2014-01-11 18:56:24 +01:00
# endif
2018-05-18 16:41:56 -04:00
# include <qt/bitcoin.h>
2017-11-10 13:57:53 +13:00
# include <chainparams.h>
2021-07-14 12:03:25 +03:00
# include <init.h>
# include <interfaces/handler.h>
2017-12-05 15:57:12 -05:00
# include <interfaces/init.h>
2021-07-14 12:03:25 +03:00
# include <interfaces/node.h>
# include <node/ui_interface.h>
# include <noui.h>
# include <qt/bitcoingui.h>
2019-07-10 08:06:57 -04:00
# include <qt/clientmodel.h>
2017-11-10 13:57:53 +13:00
# include <qt/guiconstants.h>
# include <qt/guiutil.h>
2021-07-14 12:03:25 +03:00
# include <qt/initexecutor.h>
2017-11-10 13:57:53 +13:00
# 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>
2021-07-14 12:03:25 +03:00
# include <uint256.h>
2021-08-06 21:51:57 +03:00
# include <util/string.h>
2021-07-14 12:03:25 +03:00
# include <util/system.h>
# include <util/threadnames.h>
# include <util/translation.h>
# include <validation.h>
2014-09-05 13:18:35 +02:00
2013-12-11 15:00:56 +01:00
# ifdef ENABLE_WALLET
2017-11-10 13:57:53 +13:00
# include <qt/paymentserver.h>
2019-01-04 18:49:48 +00:00
# include <qt/walletcontroller.h>
2019-07-23 15:38:10 +03:00
# include <qt/walletmodel.h>
# endif // ENABLE_WALLET
2013-04-13 00:13:08 -05:00
2018-08-23 13:42:31 -04:00
# include <boost/signals2/connection.hpp>
2022-01-06 18:35:53 +02:00
# include <chrono>
2018-04-02 18:31:40 +00:00
# include <memory>
2014-09-05 13:18:35 +02:00
2013-07-23 08:52:24 +02:00
# include <QApplication>
2014-07-15 10:23:28 +02:00
# include <QDebug>
2021-03-27 18:52:22 +02:00
# include <QLatin1String>
2013-04-13 00:13:08 -05:00
# include <QLibraryInfo>
# include <QLocale>
2011-06-05 16:03:29 +02:00
# include <QMessageBox>
2013-04-13 00:13:08 -05:00
# include <QSettings>
2014-09-05 13:18:35 +02:00
# include <QThread>
2013-04-13 00:13:08 -05:00
# include <QTimer>
# include <QTranslator>
2021-06-07 18:45:04 +03:00
# include <QWindow>
2013-04-13 00:13:08 -05:00
2013-12-02 16:54:19 -05:00
# if defined(QT_STATICPLUGIN)
2012-03-24 16:52:43 +01: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 16:54:19 -05:00
Q_IMPORT_PLUGIN ( QWindowsIntegrationPlugin ) ;
2021-06-02 20:59:58 +03:00
Q_IMPORT_PLUGIN ( QWindowsVistaStylePlugin ) ;
2014-06-03 23:42:53 -04:00
# elif defined(QT_QPA_PLATFORM_COCOA)
Q_IMPORT_PLUGIN ( QCocoaIntegrationPlugin ) ;
2021-03-04 13:28:55 +02:00
Q_IMPORT_PLUGIN ( QMacStylePlugin ) ;
2021-12-12 08:15:06 +01:00
# elif defined(QT_QPA_PLATFORM_ANDROID)
Q_IMPORT_PLUGIN ( QAndroidPlatformIntegrationPlugin )
2014-06-03 23:42:53 -04:00
# endif
2013-12-02 16:54:19 -05:00
# endif
2012-03-24 16:52:43 +01:00
2013-04-14 18:50:40 +02:00
// Declare meta types used for QMetaObject::invokeMethod
Q_DECLARE_METATYPE ( bool * )
2014-10-01 10:09:08 +02:00
Q_DECLARE_METATYPE ( CAmount )
2020-03-04 22:12:07 +02:00
Q_DECLARE_METATYPE ( SynchronizationState )
2018-03-01 10:40:36 +01:00
Q_DECLARE_METATYPE ( uint256 )
2013-04-14 18:50:40 +02:00
2021-11-12 10:06:00 -05:00
using node : : NodeContext ;
2019-11-12 23:26:31 +00:00
static void RegisterMetaTypes ( )
{
// Register meta types used for QMetaObject::invokeMethod and Qt::QueuedConnection
qRegisterMetaType < bool * > ( ) ;
qRegisterMetaType < SynchronizationState > ( ) ;
# ifdef ENABLE_WALLET
qRegisterMetaType < WalletModel * > ( ) ;
# endif
2020-12-30 21:49:12 -05:00
// Register typedefs (see https://doc.qt.io/qt-5/qmetatype.html#qRegisterMetaType)
2019-11-12 23:26:31 +00:00
// 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 " ) ;
qRegisterMetaType < size_t > ( " size_t " ) ;
qRegisterMetaType < std : : function < void ( ) > > ( " std::function<void()> " ) ;
qRegisterMetaType < QMessageBox : : Icon > ( " QMessageBox::Icon " ) ;
2020-05-19 15:15:00 +02:00
qRegisterMetaType < interfaces : : BlockAndHeaderTipInfo > ( " interfaces::BlockAndHeaderTipInfo " ) ;
2021-01-21 22:27:28 +02:00
qRegisterMetaTypeStreamOperators < BitcoinUnit > ( " BitcoinUnit " ) ;
2019-11-12 23:26:31 +00:00
}
2015-01-16 16:03:48 -05: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 21:17:40 +02:00
lang_territory = QString : : fromStdString ( gArgs . GetArg ( " -lang " , lang_territory . toStdString ( ) ) ) ;
2015-01-16 16:03:48 -05:00
return lang_territory ;
}
2013-05-19 17:36:01 +02:00
/** Set up translations */
static void initTranslations ( QTranslator & qtTranslatorBase , QTranslator & qtTranslator , QTranslator & translatorBase , QTranslator & translator )
{
2014-03-11 08:32:07 +01:00
// Remove old translators
QApplication : : removeTranslator ( & qtTranslatorBase ) ;
QApplication : : removeTranslator ( & qtTranslator ) ;
QApplication : : removeTranslator ( & translatorBase ) ;
QApplication : : removeTranslator ( & translator ) ;
2013-05-19 17:36:01 +02:00
// Get desired locale (e.g. "de_DE")
// 1) System default language
2015-01-16 16:03:48 -05:00
QString lang_territory = GetLangTerritory ( ) ;
2013-05-19 17:36:01 +02: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 ) ;
}
2021-07-08 14:57:03 -04:00
static bool InitSettings ( )
{
if ( ! gArgs . GetSettingsPath ( ) ) {
return true ; // Do nothing if settings file disabled.
}
std : : vector < std : : string > errors ;
if ( ! gArgs . ReadSettingsFile ( & errors ) ) {
2021-10-13 18:03:15 +02:00
std : : string error = QT_TRANSLATE_NOOP ( " bitcoin-core " , " Settings file could not be read " ) ;
std : : string error_translated = QCoreApplication : : translate ( " bitcoin-core " , error . c_str ( ) ) . toStdString ( ) ;
InitError ( Untranslated ( strprintf ( " %s: \n %s \n " , error , MakeUnorderedList ( errors ) ) ) ) ;
2021-07-08 14:57:03 -04:00
2021-10-13 18:03:15 +02:00
QMessageBox messagebox ( QMessageBox : : Critical , PACKAGE_NAME , QString : : fromStdString ( strprintf ( " %s. " , error_translated ) ) , QMessageBox : : Reset | QMessageBox : : Abort ) ;
2021-07-08 14:57:03 -04:00
/*: Explanatory text shown on startup when the settings file cannot be read.
Prompts user to make a choice between resetting or aborting . */
messagebox . setInformativeText ( QObject : : tr ( " Do you want to reset settings to default values, or to abort without making changes? " ) ) ;
2021-08-06 21:49:14 +03:00
messagebox . setDetailedText ( QString : : fromStdString ( MakeUnorderedList ( errors ) ) ) ;
2021-07-08 14:57:03 -04:00
messagebox . setTextFormat ( Qt : : PlainText ) ;
messagebox . setDefaultButton ( QMessageBox : : Reset ) ;
switch ( messagebox . exec ( ) ) {
case QMessageBox : : Reset :
break ;
case QMessageBox : : Abort :
return false ;
default :
assert ( false ) ;
}
}
errors . clear ( ) ;
if ( ! gArgs . WriteSettingsFile ( & errors ) ) {
2021-10-13 18:03:15 +02:00
std : : string error = QT_TRANSLATE_NOOP ( " bitcoin-core " , " Settings file could not be written " ) ;
std : : string error_translated = QCoreApplication : : translate ( " bitcoin-core " , error . c_str ( ) ) . toStdString ( ) ;
InitError ( Untranslated ( strprintf ( " %s: \n %s \n " , error , MakeUnorderedList ( errors ) ) ) ) ;
2021-07-08 14:57:03 -04:00
2021-10-13 18:03:15 +02:00
QMessageBox messagebox ( QMessageBox : : Critical , PACKAGE_NAME , QString : : fromStdString ( strprintf ( " %s. " , error_translated ) ) , QMessageBox : : Ok ) ;
2021-07-08 14:57:03 -04:00
/*: Explanatory text shown on startup when the settings file could not be written.
Prompts user to check that we have the ability to write to the file .
Explains that the user has the option of running without a settings file . */
2021-08-06 21:51:57 +03:00
messagebox . setInformativeText ( QObject : : tr ( " A fatal error occurred. Check that settings file is writable, or try running with -nosettings. " ) ) ;
2021-08-06 21:49:14 +03:00
messagebox . setDetailedText ( QString : : fromStdString ( MakeUnorderedList ( errors ) ) ) ;
2021-07-08 14:57:03 -04:00
messagebox . setTextFormat ( Qt : : PlainText ) ;
messagebox . setDefaultButton ( QMessageBox : : Ok ) ;
messagebox . exec ( ) ;
return false ;
}
return true ;
}
2013-07-23 17:01:08 +10:00
/* qDebug() message handler --> debug.log */
void DebugMessageHandler ( QtMsgType type , const QMessageLogContext & context , const QString & msg )
{
2013-09-28 19:29:44 +02:00
Q_UNUSED ( context ) ;
2016-12-25 20:19:40 +00:00
if ( type = = QtDebugMsg ) {
LogPrint ( BCLog : : QT , " GUI: %s \n " , msg . toStdString ( ) ) ;
} else {
LogPrintf ( " GUI: %s \n " , msg . toStdString ( ) ) ;
}
2013-07-23 17:01:08 +10:00
}
2019-08-09 15:39:30 -04:00
static int qt_argc = 1 ;
static const char * qt_argv = " bitcoin-qt " ;
2018-08-23 13:42:31 -04:00
BitcoinApplication : : BitcoinApplication ( ) :
2019-08-09 15:39:30 -04:00
QApplication ( qt_argc , const_cast < char * * > ( & qt_argv ) ) ,
2018-07-30 12:37:09 +02:00
optionsModel ( nullptr ) ,
clientModel ( nullptr ) ,
window ( nullptr ) ,
pollShutdownTimer ( nullptr ) ,
2018-04-28 19:40:51 -04:00
returnValue ( 0 ) ,
2018-07-30 12:37:09 +02:00
platformStyle ( nullptr )
2014-01-07 11:30:17 +01:00
{
2020-04-29 15:01:02 +00:00
// Qt runs setlocale(LC_ALL, "") on initialization.
2019-11-12 23:26:31 +00:00
RegisterMetaTypes ( ) ;
2014-01-07 11:30:17 +01:00
setQuitOnLastWindowClosed ( false ) ;
2018-04-28 19:40:51 -04:00
}
2015-07-28 15:20:14 +02:00
2018-04-28 19:40:51 -04:00
void BitcoinApplication : : setupPlatformStyle ( )
{
2015-07-28 15:20:14 +02: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 21:44:55 +00:00
std : : string platformName ;
2017-08-01 21:17:40 +02:00
platformName = gArgs . GetArg ( " -uiplatform " , BitcoinGUI : : DEFAULT_UIPLATFORM ) ;
2015-07-28 15:20:14 +02: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 11:30:17 +01:00
}
BitcoinApplication : : ~ BitcoinApplication ( )
{
2021-07-14 11:48:21 +03:00
m_executor . reset ( ) ;
2014-01-08 08:59:24 +01:00
delete window ;
2018-07-30 12:37:09 +02:00
window = nullptr ;
2015-07-28 15:20:14 +02:00
delete platformStyle ;
2018-07-30 12:37:09 +02:00
platformStyle = nullptr ;
2014-01-07 11:30:17 +01:00
}
2013-12-11 15:00:56 +01:00
# ifdef ENABLE_WALLET
2014-01-07 11:30:17 +01:00
void BitcoinApplication : : createPaymentServer ( )
{
paymentServer = new PaymentServer ( this ) ;
}
2013-12-11 15:00:56 +01:00
# endif
2014-01-07 11:30:17 +01:00
2019-04-29 15:29:00 -04:00
bool BitcoinApplication : : createOptionsModel ( bool resetSettings )
2014-01-07 11:30:17 +01:00
{
2019-04-29 15:29:00 -04:00
optionsModel = new OptionsModel ( node ( ) , this ) ;
if ( resetSettings ) {
optionsModel - > Reset ( ) ;
}
bilingual_str error ;
if ( ! optionsModel - > Init ( error ) ) {
fs : : path settings_path ;
if ( gArgs . GetSettingsPath ( & settings_path ) ) {
error + = Untranslated ( " \n " ) ;
std : : string quoted_path = strprintf ( " %s " , fs : : quoted ( fs : : PathToString ( settings_path ) ) ) ;
error . original + = strprintf ( " Settings file %s might be corrupt or invalid. " , quoted_path ) ;
error . translated + = tr ( " Settings file %1 might be corrupt or invalid. " ) . arg ( QString : : fromStdString ( quoted_path ) ) . toStdString ( ) ;
}
InitError ( error ) ;
QMessageBox : : critical ( nullptr , PACKAGE_NAME , QString : : fromStdString ( error . translated ) ) ;
return false ;
}
return true ;
2014-01-07 11:30:17 +01:00
}
2014-10-09 11:04:49 +02:00
void BitcoinApplication : : createWindow ( const NetworkStyle * networkStyle )
2014-01-07 11:30:17 +01:00
{
2018-08-23 13:42:31 -04:00
window = new BitcoinGUI ( node ( ) , platformStyle , networkStyle , nullptr ) ;
2021-06-07 18:45:04 +03:00
connect ( window , & BitcoinGUI : : quitRequested , this , & BitcoinApplication : : requestShutdown ) ;
2014-01-07 11:30:17 +01:00
2014-01-08 10:45:00 +01:00
pollShutdownTimer = new QTimer ( window ) ;
2022-02-08 17:25:39 +02:00
connect ( pollShutdownTimer , & QTimer : : timeout , [ this ] {
if ( ! QApplication : : activeModalWidget ( ) ) {
window - > detectShutdown ( ) ;
}
} ) ;
2014-01-07 11:30:17 +01:00
}
2014-10-09 11:04:49 +02:00
void BitcoinApplication : : createSplashScreen ( const NetworkStyle * networkStyle )
2014-01-08 08:59:24 +01:00
{
2018-08-23 13:42:31 -04:00
assert ( ! m_splash ) ;
2020-08-09 18:33:54 +03:00
m_splash = new SplashScreen ( networkStyle ) ;
2016-11-19 11:08:19 +01:00
// We don't hold a direct pointer to the splash screen after creation, but the splash
2018-12-02 01:26:28 +02:00
// screen will take care of deleting itself when finish() happens.
2018-08-23 13:42:31 -04:00
m_splash - > show ( ) ;
connect ( this , & BitcoinApplication : : splashFinished , m_splash , & SplashScreen : : finish ) ;
connect ( this , & BitcoinApplication : : requestedShutdown , m_splash , & QWidget : : close ) ;
}
2017-12-05 15:57:12 -05:00
void BitcoinApplication : : createNode ( interfaces : : Init & init )
2018-08-23 13:42:31 -04:00
{
assert ( ! m_node ) ;
2017-12-05 15:57:12 -05:00
m_node = init . makeNode ( ) ;
2020-07-06 10:36:15 -04:00
if ( m_splash ) m_splash - > setNode ( * m_node ) ;
2014-01-08 08:59:24 +01:00
}
2017-11-06 20:11:43 -05:00
bool BitcoinApplication : : baseInitialize ( )
{
2018-08-23 13:42:31 -04:00
return node ( ) . baseInitialize ( ) ;
2017-11-06 20:11:43 -05:00
}
2014-01-07 11:30:17 +01:00
void BitcoinApplication : : startThread ( )
{
2021-07-14 11:48:21 +03:00
assert ( ! m_executor ) ;
m_executor . emplace ( node ( ) ) ;
2014-01-07 11:30:17 +01:00
/* communication to and from thread */
2021-07-14 19:03:42 +03:00
connect ( & m_executor . value ( ) , & InitExecutor : : initializeResult , this , & BitcoinApplication : : initializeResult ) ;
2022-04-16 15:26:45 +02:00
connect ( & m_executor . value ( ) , & InitExecutor : : shutdownResult , this , [ ] {
QCoreApplication : : exit ( 0 ) ;
} ) ;
2021-07-14 19:03:42 +03:00
connect ( & m_executor . value ( ) , & InitExecutor : : runawayException , this , & BitcoinApplication : : handleRunawayException ) ;
connect ( this , & BitcoinApplication : : requestedInitialize , & m_executor . value ( ) , & InitExecutor : : initialize ) ;
connect ( this , & BitcoinApplication : : requestedShutdown , & m_executor . value ( ) , & InitExecutor : : shutdown ) ;
2014-01-07 11:30:17 +01:00
}
2015-10-08 10:01:29 +02:00
void BitcoinApplication : : parameterSetup ( )
{
2018-04-23 09:25:34 +02:00
// Default printtoconsole to false for the GUI. GUI programs should not
// print to the console unnecessarily.
gArgs . SoftSetBoolArg ( " -printtoconsole " , false ) ;
2018-08-23 13:42:31 -04:00
InitLogging ( gArgs ) ;
InitParameterInteraction ( gArgs ) ;
2015-10-08 10:01:29 +02:00
}
2020-04-20 14:42:52 +00:00
void BitcoinApplication : : InitPruneSetting ( int64_t prune_MiB )
2019-11-12 12:09:10 +02:00
{
2020-04-20 14:42:52 +00:00
optionsModel - > SetPruneTargetGB ( PruneMiBtoGB ( prune_MiB ) , true ) ;
2019-08-24 22:31:14 +02:00
}
2014-01-07 11:30:17 +01:00
void BitcoinApplication : : requestInitialize ( )
{
2014-07-15 10:23:28 +02:00
qDebug ( ) < < __func__ < < " : Requesting initialize " ;
2014-07-15 16:26:16 +02:00
startThread ( ) ;
2015-07-14 13:59:05 +02:00
Q_EMIT requestedInitialize ( ) ;
2014-01-07 11:30:17 +01:00
}
void BitcoinApplication : : requestShutdown ( )
{
2021-06-07 18:45:04 +03:00
for ( const auto w : QGuiApplication : : topLevelWindows ( ) ) {
w - > hide ( ) ;
}
2016-11-18 16:35:14 +01: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 14:28:55 +01:00
shutdownWindow . reset ( ShutdownWindow : : showShutdownWindow ( window ) ) ;
2016-11-18 16:35:14 +01:00
2014-07-15 10:23:28 +02:00
qDebug ( ) < < __func__ < < " : Requesting shutdown " ;
2021-06-07 18:45:04 +03:00
2019-01-29 00:08:48 +00: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.
2018-08-23 13:42:31 -04:00
node ( ) . startShutdown ( ) ;
2019-01-29 00:08:48 +00:00
// Unsetting the client model can cause the current thread to wait for node
2019-11-04 04:22:53 -05:00
// to complete an operation, like wait for a RPC execution to complete.
2018-07-30 12:37:09 +02:00
window - > setClientModel ( nullptr ) ;
2014-01-08 10:45:00 +01:00
pollShutdownTimer - > stop ( ) ;
2014-01-07 16:04:47 +01:00
2017-12-05 15:57:12 -05:00
# ifdef ENABLE_WALLET
// Delete wallet controller here manually, instead of relying on Qt object
// tracking (https://doc.qt.io/qt-5/objecttrees.html). This makes sure
// walletmodel m_handle_* notification handlers are deleted before wallets
// are unloaded, which can simplify wallet implementations. It also avoids
// these notifications having to be handled while GUI objects are being
// destroyed, making GUI code less fragile as well.
delete m_wallet_controller ;
m_wallet_controller = nullptr ;
# endif // ENABLE_WALLET
2014-01-08 08:59:24 +01:00
delete clientModel ;
2018-07-30 12:37:09 +02:00
clientModel = nullptr ;
2014-01-07 16:04:47 +01:00
// Request shutdown from core thread
2015-07-14 13:59:05 +02:00
Q_EMIT requestedShutdown ( ) ;
2014-01-07 11:30:17 +01:00
}
2020-05-19 15:15:00 +02:00
void BitcoinApplication : : initializeResult ( bool success , interfaces : : BlockAndHeaderTipInfo tip_info )
2014-01-07 11:30:17 +01:00
{
2017-02-23 17:12:36 +09:00
qDebug ( ) < < __func__ < < " : Initialization result: " < < success ;
// Set exit result.
returnValue = success ? EXIT_SUCCESS : EXIT_FAILURE ;
if ( success )
2014-01-07 11:30:17 +01:00
{
2017-02-23 16:38:41 +09:00
// Log this only after AppInitMain finishes, as then logging setup is guaranteed complete
2019-06-21 20:22:13 +03:00
qInfo ( ) < < " Platform customization: " < < platformStyle - > getName ( ) ;
2018-08-23 13:42:31 -04:00
clientModel = new ClientModel ( node ( ) , optionsModel ) ;
2020-05-19 15:15:00 +02:00
window - > setClientModel ( clientModel , & tip_info ) ;
2013-12-11 15:00:56 +01:00
# ifdef ENABLE_WALLET
2019-07-23 15:38:10 +03:00
if ( WalletModel : : isWalletEnabled ( ) ) {
2018-08-01 13:38:45 -04:00
m_wallet_controller = new WalletController ( * clientModel , platformStyle , this ) ;
2019-07-23 15:38:10 +03:00
window - > setWalletController ( m_wallet_controller ) ;
if ( paymentServer ) {
paymentServer - > setOptionsModel ( optionsModel ) ;
}
}
# endif // ENABLE_WALLET
2014-01-07 11:30:17 +01:00
2018-10-19 10:39:25 +03:00
// If -min option passed, start window minimized (iconified) or minimized to tray
if ( ! gArgs . GetBoolArg ( " -min " , false ) ) {
2014-01-07 11:30:17 +01:00
window - > show ( ) ;
2018-10-19 10: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 11:30:17 +01:00
}
2018-12-02 01:26:28 +02:00
Q_EMIT splashFinished ( ) ;
2017-11-06 20:11:43 -05:00
Q_EMIT windowShown ( window ) ;
2014-09-18 13:14:38 +02:00
2013-12-11 15:00:56 +01:00
# ifdef ENABLE_WALLET
2014-01-07 11:30:17 +01:00
// Now that initialization/startup is done, process any command-line
// bitcoin: URIs or payment requests:
2017-11-06 20:11:43 -05:00
if ( paymentServer ) {
connect ( paymentServer , & PaymentServer : : receivedPaymentRequest , window , & BitcoinGUI : : handlePaymentRequest ) ;
connect ( window , & BitcoinGUI : : receivedURI , paymentServer , & PaymentServer : : handleURIOrFile ) ;
2020-01-19 09:37:34 +02:00
connect ( paymentServer , & PaymentServer : : message , [ this ] ( const QString & title , const QString & message , unsigned int style ) {
window - > message ( title , message , style ) ;
} ) ;
2022-01-06 18:35:53 +02:00
QTimer : : singleShot ( 100 ms , paymentServer , & PaymentServer : : uiReady ) ;
2017-11-06 20:11:43 -05:00
}
2013-12-11 15:00:56 +01:00
# endif
2022-01-06 18:41:36 +02:00
pollShutdownTimer - > start ( SHUTDOWN_POLLING_DELAY ) ;
2014-01-07 11:30:17 +01:00
} else {
2018-12-02 01:26:28 +02:00
Q_EMIT splashFinished ( ) ; // Make sure splash screen doesn't stick around during shutdown
2021-06-07 18:45:04 +03:00
requestShutdown ( ) ;
2014-01-07 11:30:17 +01:00
}
}
void BitcoinApplication : : handleRunawayException ( const QString & message )
{
2021-03-27 18:52:22 +02:00
QMessageBox : : critical (
nullptr , tr ( " Runaway exception " ) ,
2021-05-03 12:55:00 +03:00
tr ( " A fatal error occurred. %1 can no longer continue safely and will quit. " ) . arg ( PACKAGE_NAME ) +
QLatin1String ( " <br><br> " ) + GUIUtil : : MakeHtmlLink ( message , PACKAGE_BUGREPORT ) ) ;
2016-11-07 21:31:38 +03:00
: : exit ( EXIT_FAILURE ) ;
2014-01-07 11:30:17 +01:00
}
2021-03-27 18:52:22 +02:00
void BitcoinApplication : : handleNonFatalException ( const QString & message )
{
assert ( QThread : : currentThread ( ) = = thread ( ) ) ;
QMessageBox : : warning (
nullptr , tr ( " Internal error " ) ,
tr ( " An internal error occurred. %1 will attempt to continue safely. This is "
2021-05-03 12:55:00 +03:00
" an unexpected bug which can be reported as described below. " ) . arg ( PACKAGE_NAME ) +
QLatin1String ( " <br><br> " ) + GUIUtil : : MakeHtmlLink ( message , PACKAGE_BUGREPORT ) ) ;
2021-03-27 18:52:22 +02:00
}
2014-04-10 08:19:58 +02:00
WId BitcoinApplication : : getMainWinId ( ) const
{
if ( ! window )
return 0 ;
return window - > winId ( ) ;
}
2022-02-12 15:37:43 +02:00
bool BitcoinApplication : : event ( QEvent * e )
{
if ( e - > type ( ) = = QEvent : : Quit ) {
requestShutdown ( ) ;
return true ;
}
return QApplication : : event ( e ) ;
}
2020-07-19 14:31:51 +07:00
static void SetupUIArgs ( ArgsManager & argsman )
2018-04-28 16:54:58 -04:00
{
2020-07-19 14:47:05 +07:00
argsman . AddArg ( " -choosedatadir " , strprintf ( " Choose data directory on startup (default: %u) " , DEFAULT_CHOOSE_DATADIR ) , ArgsManager : : ALLOW_ANY , OptionsCategory : : GUI ) ;
argsman . AddArg ( " -lang=<lang> " , " Set language, for example \" de_DE \" (default: system locale) " , ArgsManager : : ALLOW_ANY , OptionsCategory : : GUI ) ;
argsman . AddArg ( " -min " , " Start minimized " , ArgsManager : : ALLOW_ANY , OptionsCategory : : GUI ) ;
argsman . AddArg ( " -resetguisettings " , " Reset all settings changed in the GUI " , ArgsManager : : ALLOW_ANY , OptionsCategory : : GUI ) ;
argsman . AddArg ( " -splash " , strprintf ( " Show splash screen on startup (default: %u) " , DEFAULT_SPLASHSCREEN ) , ArgsManager : : ALLOW_ANY , OptionsCategory : : GUI ) ;
argsman . 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 16:54:58 -04:00
}
2017-11-06 20:11:43 -05:00
int GuiMain ( int argc , char * argv [ ] )
2011-05-07 22:13:39 +02:00
{
2018-08-05 16:38:25 +00:00
# ifdef WIN32
util : : WinCmdLineArgs winArgs ;
std : : tie ( argc , argv ) = winArgs . get ( ) ;
# endif
2014-05-13 10:15:00 +00:00
2017-12-05 15:57:12 -05:00
std : : unique_ptr < interfaces : : Init > init = interfaces : : MakeGuiInit ( argc , argv ) ;
2017-12-05 15:57:12 -05:00
SetupEnvironment ( ) ;
util : : ThreadSetInternalName ( " main " ) ;
2017-04-17 13:55:43 -04:00
2018-11-23 16:06:32 +00:00
// Subscribe to global signals from core
2018-08-23 13:42:31 -04:00
boost : : signals2 : : scoped_connection handler_message_box = : : uiInterface . ThreadSafeMessageBox_connect ( noui_ThreadSafeMessageBox ) ;
boost : : signals2 : : scoped_connection handler_question = : : uiInterface . ThreadSafeQuestion_connect ( noui_ThreadSafeQuestion ) ;
boost : : signals2 : : scoped_connection handler_init_message = : : uiInterface . InitMessage_connect ( noui_InitMessage ) ;
2018-11-23 16:06:32 +00:00
2014-01-05 12:37:51 +01:00
// Do not refer to data directory yet, this can be overridden by Intro::pickDataDirectory
2013-01-03 22:06:18 +01:00
2018-04-28 19:40:51 -04:00
/// 1. Basic Qt initialization (not dependent on parameters or configuration)
2011-06-18 14:25:24 +02: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 17:59:31 +01:00
// Generate high-dpi pixmaps
QApplication : : setAttribute ( Qt : : AA_UseHighDpiPixmaps ) ;
2019-06-20 21:24:22 +03:00
QCoreApplication : : setAttribute ( Qt : : AA_EnableHighDpiScaling ) ;
2011-06-23 22:35:30 +02:00
2020-08-28 11:09:58 +02:00
# if defined(QT_QPA_PLATFORM_ANDROID)
QApplication : : setAttribute ( Qt : : AA_DontUseNativeMenuBar ) ;
QApplication : : setAttribute ( Qt : : AA_DontCreateNativeWidgetSiblings ) ;
QApplication : : setAttribute ( Qt : : AA_DontUseNativeDialogs ) ;
# endif
2018-08-23 13:42:31 -04:00
BitcoinApplication app ;
2021-10-06 15:06:40 +01:00
GUIUtil : : LoadFont ( QStringLiteral ( " :/fonts/monospace " ) ) ;
2019-06-20 21:24:22 +03:00
2018-04-28 19:40:51 -04: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:
2017-12-05 15:57:12 -05:00
SetupServerArgs ( gArgs ) ;
2020-07-19 14:31:51 +07:00
SetupUIArgs ( gArgs ) ;
2018-04-28 19:40:51 -04:00
std : : string error ;
2018-08-23 13:42:31 -04:00
if ( ! gArgs . ParseParameters ( argc , argv , error ) ) {
InitError ( strprintf ( Untranslated ( " 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 ,
2022-02-17 12:54:39 +01:00
// message cannot be translated because translations have not been initialized
2019-07-10 08:06:57 -04:00
QString : : fromStdString ( " Error parsing command line arguments: %1. " ) . arg ( QString : : fromStdString ( error ) ) ) ;
2018-04-28 19:40:51 -04: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 12:37:51 +01: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 08:32:07 +01:00
QApplication : : setOrganizationName ( QAPP_ORG_NAME ) ;
QApplication : : setOrganizationDomain ( QAPP_ORG_DOMAIN ) ;
QApplication : : setApplicationName ( QAPP_APP_NAME_DEFAULT ) ;
2013-05-19 17:36:01 +02:00
2014-01-05 12:37:51 +01:00
/// 4. Initialization of translations, so that intro dialog is in user's language
2013-05-19 17:36:01 +02:00
// Now that QSettings are accessible, initialize translations
QTranslator qtTranslatorBase , qtTranslator , translatorBase , translator ;
initTranslations ( qtTranslatorBase , qtTranslator , translatorBase , translator ) ;
2014-01-05 12:37:51 +01:00
// Show help message immediately after parsing command-line options (for "-lang") and setting locale,
// but before showing splash screen.
2018-03-30 13:47:36 -07:00
if ( HelpRequested ( gArgs ) | | gArgs . IsArgSet ( " -version " ) ) {
2018-08-23 13:42:31 -04:00
HelpMessageDialog help ( nullptr , gArgs . IsArgSet ( " -version " ) ) ;
2014-01-05 12:37:51 +01:00
help . showOrPrint ( ) ;
2016-11-07 21:31:38 +03:00
return EXIT_SUCCESS ;
2013-06-23 18:04:44 +02:00
}
2020-07-02 08:27:47 +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-01-05 12:37:51 +01: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 22:34:59 +02:00
bool did_show_intro = false ;
2020-04-20 14:42:52 +00:00
int64_t prune_MiB = 0 ; // Intro dialog prune configuration
2019-08-24 22:34:59 +02:00
// Gracefully exit if the user cancels
2020-04-20 14:42:52 +00:00
if ( ! Intro : : showIfNeeded ( did_show_intro , prune_MiB ) ) return EXIT_SUCCESS ;
2014-01-05 12:37:51 +01:00
2019-07-24 18:54:32 +03:00
/// 6. Determine availability of data directory and parse bitcoin.conf
2021-05-04 13:31:18 +02:00
/// - Do not call gArgs.GetDataDirNet() before this step finishes
2019-07-24 18:54:32 +03:00
if ( ! CheckDataDirOption ( ) ) {
2018-08-23 13:42:31 -04:00
InitError ( strprintf ( Untranslated ( " 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 21:31:38 +03:00
return EXIT_FAILURE ;
2014-01-05 12:37:51 +01:00
}
2018-08-23 13:42:31 -04:00
if ( ! gArgs . ReadConfigFiles ( error , true ) ) {
InitError ( strprintf ( Untranslated ( " Error reading configuration file: %s \n " ) , error ) ) ;
2019-06-26 10:28:13 -04:00
QMessageBox : : critical ( nullptr , PACKAGE_NAME ,
2018-04-28 19:40:51 -04:00
QObject : : tr ( " Error: Cannot parse configuration file: %1. " ) . arg ( QString : : fromStdString ( error ) ) ) ;
2016-11-07 21:31:38 +03:00
return EXIT_FAILURE ;
2014-04-07 10:10:01 +02:00
}
2014-01-05 12:37:51 +01:00
2014-03-11 08:32:07 +01: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
2020-09-22 15:57:30 +02:00
// Check for chain settings (Params() calls are only valid after this clause)
2015-05-25 09:00:17 +02:00
try {
2018-08-23 13:42:31 -04:00
SelectParams ( gArgs . GetChainName ( ) ) ;
2015-05-25 09:00:17 +02:00
} catch ( std : : exception & e ) {
2018-08-23 13:42:31 -04:00
InitError ( Untranslated ( 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 21:31:38 +03:00
return EXIT_FAILURE ;
2014-03-11 08:32:07 +01:00
}
# ifdef ENABLE_WALLET
2022-03-08 19:41:38 +01:00
// Parse URIs on command line
2018-08-23 13:42:31 -04:00
PaymentServer : : ipcParseCommandLine ( argc , argv ) ;
2014-03-11 08:32:07 +01:00
# endif
2021-07-08 14:57:03 -04:00
if ( ! InitSettings ( ) ) {
2019-04-28 19:08:26 -04:00
return EXIT_FAILURE ;
}
2014-11-05 11:42:51 +01:00
2016-10-13 23:24:21 +02:00
QScopedPointer < const NetworkStyle > networkStyle ( NetworkStyle : : instantiate ( Params ( ) . NetworkIDString ( ) ) ) ;
2014-10-09 11:04:49 +02:00
assert ( ! networkStyle . isNull ( ) ) ;
2014-03-11 08:32:07 +01:00
// Allow for separate UI settings for testnets
2014-10-09 11:04:49 +02:00
QApplication : : setApplicationName ( networkStyle - > getAppName ( ) ) ;
2014-03-11 08:32:07 +01:00
// Re-initialize translations after changing application name (language in network-specific settings can be different)
initTranslations ( qtTranslatorBase , qtTranslator , translatorBase , translator ) ;
2013-12-11 15:00:56 +01:00
# ifdef ENABLE_WALLET
2014-03-11 08:32:07 +01:00
/// 8. URI IPC sending
2014-01-05 12:37:51 +01: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 21:31:38 +03:00
exit ( EXIT_SUCCESS ) ;
2014-01-05 12:37:51 +01:00
2013-07-18 16:50:17 +10: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 15:38:10 +03:00
if ( WalletModel : : isWalletEnabled ( ) ) {
app . createPaymentServer ( ) ;
}
# endif // ENABLE_WALLET
2013-07-18 16:50:17 +10:00
2014-03-11 08:32:07 +01:00
/// 9. Main GUI initialization
2020-06-08 16:48:45 +03:00
// Install global event filter that makes sure that out-of-focus labels do not contain text cursor.
app . installEventFilter ( new GUIUtil : : LabelOutOfFocusEventFilter ( & app ) ) ;
2014-04-10 08:19:58 +02: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 15:32:35 +02:00
qInstallMessageHandler ( DebugMessageHandler ) ;
2015-10-08 10:01:29 +02:00
// Allow parameter interaction before we create the options model
app . parameterSetup ( ) ;
2019-12-29 17:29:45 +02:00
GUIUtil : : LogQtInfo ( ) ;
2019-04-29 15:29:00 -04:00
if ( gArgs . GetBoolArg ( " -splash " , DEFAULT_SPLASHSCREEN ) & & ! gArgs . GetBoolArg ( " -min " , false ) )
app . createSplashScreen ( networkStyle . data ( ) ) ;
app . createNode ( * init ) ;
2014-01-05 12:37:51 +01:00
// Load GUI settings from QSettings
2019-04-29 15:29:00 -04:00
if ( ! app . createOptionsModel ( gArgs . GetBoolArg ( " -resetguisettings " , false ) ) ) {
return EXIT_FAILURE ;
}
2012-02-16 21:09:41 -05:00
2019-08-24 19:13:04 +02:00
if ( did_show_intro ) {
// Store intro dialog settings other than datadir (network specific)
2020-04-20 14:42:52 +00:00
app . InitPruneSetting ( prune_MiB ) ;
2019-08-24 19:13:04 +02:00
}
2017-07-15 10:46:06 +02:00
int rv = EXIT_SUCCESS ;
2011-06-07 18:59:01 +02:00
try
{
2014-10-09 11:04:49 +02:00
app . createWindow ( networkStyle . data ( ) ) ;
2017-07-15 10:46:06 +02: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 20:11:43 -05:00
if ( app . baseInitialize ( ) ) {
2017-07-15 10:46:06 +02:00
app . requestInitialize ( ) ;
2018-06-13 16:02:39 +02:00
# if defined(Q_OS_WIN)
2021-05-02 21:53:36 +03:00
WinShutdownMonitor : : registerShutdownBlockReason ( QObject : : tr ( " %1 didn't yet exit safely… " ) . arg ( PACKAGE_NAME ) , ( HWND ) app . getMainWinId ( ) ) ;
2014-04-10 08:19:58 +02:00
# endif
2017-07-15 10:46:06 +02:00
app . exec ( ) ;
rv = app . getReturnValue ( ) ;
} else {
// A dialog with detailed error will have been shown by InitError()
rv = EXIT_FAILURE ;
}
2014-12-07 13:29:06 +01:00
} catch ( const std : : exception & e ) {
2014-01-07 11:30:17 +01:00
PrintExceptionContinue ( & e , " Runaway exception " ) ;
2018-08-23 13:42:31 -04:00
app . handleRunawayException ( QString : : fromStdString ( app . node ( ) . getWarnings ( ) . translated ) ) ;
2011-05-22 17:19:43 +02:00
} catch ( . . . ) {
2017-08-07 07:36:37 +02:00
PrintExceptionContinue ( nullptr , " Runaway exception " ) ;
2018-08-23 13:42:31 -04:00
app . handleRunawayException ( QString : : fromStdString ( app . node ( ) . getWarnings ( ) . translated ) ) ;
2011-05-22 17:19:43 +02:00
}
2017-07-15 10:46:06 +02:00
return rv ;
2011-05-07 22:13:39 +02:00
}