-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathbitcoin.cpp
316 lines (265 loc) · 11.1 KB
/
bitcoin.cpp
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Copyright (c) 2021 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <qml/bitcoin.h>
#include <chainparams.h>
#include <common/args.h>
#include <common/system.h>
#include <init.h>
#include <interfaces/chain.h>
#include <interfaces/init.h>
#include <interfaces/node.h>
#include <logging.h>
#include <node/interface_ui.h>
#include <noui.h>
#include <qml/appmode.h>
#ifdef __ANDROID__
#include <qml/androidnotifier.h>
#endif
#include <qml/components/blockclockdial.h>
#include <qml/controls/linegraph.h>
#include <qml/models/chainmodel.h>
#include <qml/models/networktraffictower.h>
#include <qml/models/nodemodel.h>
#include <qml/models/options_model.h>
#include <qml/models/peerlistsortproxy.h>
#include <qml/imageprovider.h>
#include <qml/thememanager.h>
#include <qml/util.h>
#include <qt/guiconstants.h>
#include <qt/guiutil.h>
#include <qt/initexecutor.h>
#include <qt/networkstyle.h>
#include <qt/peertablemodel.h>
#include <util/threadnames.h>
#include <util/translation.h>
#include <boost/signals2/connection.hpp>
#include <cassert>
#include <memory>
#include <tuple>
#include <QDebug>
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QQuickWindow>
#include <QString>
#include <QStyleHints>
#include <QUrl>
QT_BEGIN_NAMESPACE
class QMessageLogContext;
QT_END_NAMESPACE
#if defined(QT_STATICPLUGIN)
#include <QtPlugin>
Q_IMPORT_PLUGIN(QtQmlPlugin)
Q_IMPORT_PLUGIN(QtQmlModelsPlugin)
Q_IMPORT_PLUGIN(QtQuick2Plugin)
Q_IMPORT_PLUGIN(QtQuick2WindowPlugin)
Q_IMPORT_PLUGIN(QtQuickControls1Plugin)
Q_IMPORT_PLUGIN(QmlSettingsPlugin)
Q_IMPORT_PLUGIN(QtQuickLayoutsPlugin)
Q_IMPORT_PLUGIN(QtQuickControls2Plugin)
Q_IMPORT_PLUGIN(QtQuickTemplates2Plugin)
#endif
namespace {
void SetupUIArgs(ArgsManager& argsman)
{
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);
}
bool InitErrorMessageBox(
const bilingual_str& message,
[[maybe_unused]] const std::string& caption,
[[maybe_unused]] unsigned int style)
{
QQmlApplicationEngine engine;
#ifdef __ANDROID__
AppMode app_mode(AppMode::MOBILE);
#else
AppMode app_mode(AppMode::DESKTOP);
#endif // __ANDROID__
qmlRegisterSingletonInstance<AppMode>("org.bitcoincore.qt", 1, 0, "AppMode", &app_mode);
engine.rootContext()->setContextProperty("message", QString::fromStdString(message.translated));
engine.load(QUrl(QStringLiteral("qrc:///qml/pages/initerrormessage.qml")));
if (engine.rootObjects().isEmpty()) {
return EXIT_FAILURE;
}
qGuiApp->exec();
return false;
}
/* qDebug() message handler --> debug.log */
void DebugMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& msg)
{
Q_UNUSED(context);
if (type == QtDebugMsg) {
LogPrint(BCLog::QT, "GUI: %s\n", msg.toStdString());
} else {
LogPrintf("GUI: %s\n", msg.toStdString());
}
}
bool ConfigurationFileExists(ArgsManager& argsman)
{
fs::path settings_path;
if (!argsman.GetSettingsPath(&settings_path)) {
// settings file is disabled
return true;
}
if (fs::exists(settings_path)) {
return true;
}
const fs::path rel_config_path = argsman.GetPathArg("-conf", BITCOIN_CONF_FILENAME);
const fs::path abs_config_path = AbsPathForConfigVal(argsman, rel_config_path, true);
if (fs::exists(abs_config_path)) {
return true;
}
return false;
}
void setupChainQSettings(QGuiApplication* app, QString chain)
{
if (chain.compare("MAIN") == 0) {
app->setApplicationName(QAPP_APP_NAME_DEFAULT);
} else if (chain.compare("TEST") == 0) {
app->setApplicationName(QAPP_APP_NAME_TESTNET);
} else if (chain.compare("SIGNET") == 0) {
app->setApplicationName(QAPP_APP_NAME_SIGNET);
} else if (chain.compare("REGTEST") == 0) {
app->setApplicationName(QAPP_APP_NAME_REGTEST);
}
}
} // namespace
int QmlGuiMain(int argc, char* argv[])
{
#ifdef WIN32
common::WinCmdLineArgs winArgs;
std::tie(argc, argv) = winArgs.get();
#endif // WIN32
Q_INIT_RESOURCE(bitcoin_qml);
qRegisterMetaType<interfaces::BlockAndHeaderTipInfo>("interfaces::BlockAndHeaderTipInfo");
QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication::styleHints()->setTabFocusBehavior(Qt::TabFocusAllControls);
QGuiApplication app(argc, argv);
auto handler_message_box = ::uiInterface.ThreadSafeMessageBox_connect(InitErrorMessageBox);
std::unique_ptr<interfaces::Init> init = interfaces::MakeGuiInit(argc, argv);
SetupEnvironment();
util::ThreadSetInternalName("main");
/// Parse command-line options. We do this after qt in order to show an error if there are problems parsing these.
SetupServerArgs(gArgs);
SetupUIArgs(gArgs);
std::string error;
if (!gArgs.ParseParameters(argc, argv, error)) {
InitError(strprintf(Untranslated("Cannot parse command line arguments: %s\n"), error));
return EXIT_FAILURE;
}
// must be set before OptionsModel is initialized or translations are loaded,
// as it is used to locate QSettings
app.setOrganizationName(QAPP_ORG_NAME);
app.setOrganizationDomain(QAPP_ORG_DOMAIN);
app.setApplicationName(QAPP_APP_NAME_DEFAULT);
/// Determine availability of data directory.
if (!CheckDataDirOption(gArgs)) {
InitError(strprintf(Untranslated("Specified data directory \"%s\" does not exist.\n"), gArgs.GetArg("-datadir", "")));
return EXIT_FAILURE;
}
/// Read and parse bitcoin.conf file.
if (!gArgs.ReadConfigFiles(error, true)) {
InitError(strprintf(Untranslated("Cannot parse configuration file: %s\n"), error));
return EXIT_FAILURE;
}
/// Check for chain settings (Params() calls are only valid after this clause).
try {
SelectParams(gArgs.GetChainType());
} catch(std::exception &e) {
InitError(Untranslated(strprintf("%s\n", e.what())));
return EXIT_FAILURE;
}
/// Read and parse settings.json file.
std::vector<std::string> errors;
if (!gArgs.ReadSettingsFile(&errors)) {
error = strprintf("Failed loading settings file:\n%s\n", MakeUnorderedList(errors));
InitError(Untranslated(error));
return EXIT_FAILURE;
}
QVariant need_onboarding(true);
if (gArgs.IsArgSet("-datadir") && !gArgs.GetPathArg("-datadir").empty()) {
need_onboarding.setValue(false);
} else if (ConfigurationFileExists(gArgs)) {
need_onboarding.setValue(false);
}
if (gArgs.IsArgSet("-resetguisettings")) {
need_onboarding.setValue(true);
}
// Default printtoconsole to false for the GUI. GUI programs should not
// print to the console unnecessarily.
gArgs.SoftSetBoolArg("-printtoconsole", false);
InitLogging(gArgs);
InitParameterInteraction(gArgs);
GUIUtil::LogQtInfo();
std::unique_ptr<interfaces::Node> node = init->makeNode();
std::unique_ptr<interfaces::Chain> chain = init->makeChain();
if (!node->baseInitialize()) {
// A dialog with detailed error will have been shown by InitError().
return EXIT_FAILURE;
}
handler_message_box.disconnect();
NodeModel node_model{*node};
InitExecutor init_executor{*node};
QObject::connect(&node_model, &NodeModel::requestedInitialize, &init_executor, &InitExecutor::initialize);
QObject::connect(&node_model, &NodeModel::requestedShutdown, &init_executor, &InitExecutor::shutdown);
QObject::connect(&init_executor, &InitExecutor::initializeResult, &node_model, &NodeModel::initializeResult);
QObject::connect(&init_executor, &InitExecutor::shutdownResult, qGuiApp, &QGuiApplication::quit, Qt::QueuedConnection);
// QObject::connect(&init_executor, &InitExecutor::runawayException, &node_model, &NodeModel::handleRunawayException);
ThemeManager theme_manager{};
NetworkTrafficTower network_traffic_tower{node_model};
#ifdef __ANDROID__
AndroidNotifier android_notifier{node_model};
#endif
ChainModel chain_model{*chain};
chain_model.setCurrentNetworkName(QString::fromStdString(ChainTypeToString(gArgs.GetChainType())));
setupChainQSettings(&app, chain_model.currentNetworkName());
QObject::connect(&node_model, &NodeModel::setTimeRatioList, &chain_model, &ChainModel::setTimeRatioList);
QObject::connect(&node_model, &NodeModel::setTimeRatioListInitial, &chain_model, &ChainModel::setTimeRatioListInitial);
qGuiApp->setQuitOnLastWindowClosed(false);
QObject::connect(qGuiApp, &QGuiApplication::lastWindowClosed, [&] {
node->startShutdown();
});
PeerTableModel peer_model{*node, nullptr};
PeerListSortProxy peer_model_sort_proxy{nullptr};
peer_model_sort_proxy.setSourceModel(&peer_model);
GUIUtil::LoadFont(":/fonts/inter/regular");
GUIUtil::LoadFont(":/fonts/inter/semibold");
QQmlApplicationEngine engine;
QScopedPointer<const NetworkStyle> network_style{NetworkStyle::instantiate(Params().GetChainType())};
assert(!network_style.isNull());
engine.addImageProvider(QStringLiteral("images"), new ImageProvider{network_style.data()});
engine.rootContext()->setContextProperty("networkTrafficTower", &network_traffic_tower);
engine.rootContext()->setContextProperty("nodeModel", &node_model);
engine.rootContext()->setContextProperty("chainModel", &chain_model);
engine.rootContext()->setContextProperty("peerTableModel", &peer_model);
engine.rootContext()->setContextProperty("peerListModelProxy", &peer_model_sort_proxy);
engine.rootContext()->setContextProperty("themeManager", &theme_manager);
OptionsQmlModel options_model{*node};
engine.rootContext()->setContextProperty("optionsModel", &options_model);
engine.rootContext()->setContextProperty("needOnboarding", need_onboarding);
#ifdef __ANDROID__
AppMode app_mode(AppMode::MOBILE);
#else
AppMode app_mode(AppMode::DESKTOP);
#endif // __ANDROID__
qmlRegisterSingletonInstance<AppMode>("org.bitcoincore.qt", 1, 0, "AppMode", &app_mode);
qmlRegisterType<BlockClockDial>("org.bitcoincore.qt", 1, 0, "BlockClockDial");
qmlRegisterType<LineGraph>("org.bitcoincore.qt", 1, 0, "LineGraph");
engine.load(QUrl(QStringLiteral("qrc:///qml/pages/main.qml")));
if (engine.rootObjects().isEmpty()) {
return EXIT_FAILURE;
}
auto window = qobject_cast<QQuickWindow*>(engine.rootObjects().first());
if (!window) {
return EXIT_FAILURE;
}
// Install qDebug() message handler to route to debug.log
qInstallMessageHandler(DebugMessageHandler);
qInfo() << "Graphics API in use:" << QmlUtil::GraphicsApi(window);
node_model.startShutdownPolling();
return qGuiApp->exec();
}