MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / getOrCreateWallet

Method getOrCreateWallet

src/qt/walletcontroller.cpp:117–175  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

115}
116
117WalletModel* WalletController::getOrCreateWallet(std::unique_ptr<interfaces::Wallet> wallet)
118{
119 QMutexLocker locker(&m_mutex);
120
121 // Return model instance if exists.
122 if (!m_wallets.empty()) {
123 std::string name = wallet->getWalletName();
124 for (WalletModel* wallet_model : m_wallets) {
125 if (wallet_model->wallet().getWalletName() == name) {
126 return wallet_model;
127 }
128 }
129 }
130
131 // Instantiate model and register it.
132 WalletModel* wallet_model = new WalletModel(std::move(wallet), m_client_model, m_platform_style,
133 nullptr /* required for the following moveToThread() call */);
134
135 // Move WalletModel object to the thread that created the WalletController
136 // object (GUI main thread), instead of the current thread, which could be
137 // an outside wallet thread or RPC thread sending a LoadWallet notification.
138 // This ensures queued signals sent to the WalletModel object will be
139 // handled on the GUI event loop.
140 wallet_model->moveToThread(thread());
141 // setParent(parent) must be called in the thread which created the parent object. More details in #18948.
142 QMetaObject::invokeMethod(this, [wallet_model, this] {
143 wallet_model->setParent(this);
144 }, GUIUtil::blockingGUIThreadConnection());
145
146 m_wallets.push_back(wallet_model);
147
148 // WalletModel::startPollBalance needs to be called in a thread managed by
149 // Qt because of startTimer. Considering the current thread can be a RPC
150 // thread, better delegate the calling to Qt with Qt::AutoConnection.
151 const bool called = QMetaObject::invokeMethod(wallet_model, "startPollBalance");
152 assert(called);
153
154 connect(wallet_model, &WalletModel::unload, this, [this, wallet_model] {
155 // Defer removeAndDeleteWallet when no modal widget is actively waiting for an action.
156 // TODO: remove this workaround by removing usage of QDialog::exec.
157 QWidget* active_dialog = QApplication::activeModalWidget();
158 if (active_dialog && dynamic_cast<QProgressDialog*>(active_dialog) == nullptr) {
159 connect(qApp, &QApplication::focusWindowChanged, wallet_model, [this, wallet_model]() {
160 if (!QApplication::activeModalWidget()) {
161 removeAndDeleteWallet(wallet_model);
162 }
163 }, Qt::QueuedConnection);
164 } else {
165 removeAndDeleteWallet(wallet_model);
166 }
167 }, Qt::QueuedConnection);
168
169 // Re-emit coinsSent signal from wallet model.
170 connect(wallet_model, &WalletModel::coinsSent, this, &WalletController::coinsSent);
171
172 Q_EMIT walletAdded(wallet_model);
173
174 return wallet_model;

Callers 5

createWalletMethod · 0.80
openMethod · 0.80
loadMethod · 0.80
restoreMethod · 0.80
do_migrateMethod · 0.80

Calls 5

emptyMethod · 0.45
getWalletNameMethod · 0.45
walletMethod · 0.45
push_backMethod · 0.45

Tested by

no test coverage detected