Skip to content

Commit 5088478

Browse files
committed
Remove dupiclate code. Create updateAddressBookLabels function in the wallet model
1 parent 93c5f9f commit 5088478

File tree

5 files changed

+44
-26
lines changed

5 files changed

+44
-26
lines changed

src/qt/multisenddialog.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,15 @@ void MultiSendDialog::on_addButton_clicked()
140140
strMultiSendPrint += "% \n";
141141
}
142142

143-
// update the address book with the label given or no label if none was given.
144-
CBitcoinAddress address(strAddress);
145-
std::string userInputLabel = ui->labelAddressLabelEdit->text().toStdString();
146-
if (!userInputLabel.empty())
147-
pwalletMain->SetAddressBook(address.Get(), userInputLabel, "send");
148-
else
149-
pwalletMain->SetAddressBook(address.Get(), "(no label)", "send");
143+
if (model && model->getAddressTableModel()) {
144+
// update the address book with the label given or no label if none was given.
145+
CBitcoinAddress address(strAddress);
146+
std::string userInputLabel = ui->labelAddressLabelEdit->text().toStdString();
147+
if (!userInputLabel.empty())
148+
model->updateAddressBookLabels(address.Get(), userInputLabel, "send");
149+
else
150+
model->updateAddressBookLabels(address.Get(), "(no label)", "send");
151+
}
150152

151153
CWalletDB walletdb(pwalletMain->strWalletFile);
152154
if(!walletdb.WriteMultiSend(pwalletMain->vMultiSend)) {

src/qt/privacydialog.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,9 @@ void PrivacyDialog::on_pasteButton_clicked()
144144

145145
void PrivacyDialog::on_addressBookButton_clicked()
146146
{
147-
if (!walletModel)
147+
if (!walletModel || !walletModel->getOptionsModel())
148148
return;
149+
149150
AddressBookPage dlg(AddressBookPage::ForSelection, AddressBookPage::SendingTab, this);
150151
dlg.setModel(walletModel->getAddressTableModel());
151152
if (dlg.exec()) {
@@ -235,9 +236,6 @@ void PrivacyDialog::on_pushButtonMintzPIV_clicked()
235236

236237
void PrivacyDialog::on_pushButtonMintReset_clicked()
237238
{
238-
if (!walletModel || !walletModel->getOptionsModel())
239-
return;
240-
241239
ui->TEMintStatus->setPlainText(tr("Starting ResetMintZerocoin: rescanning complete blockchain, this will need up to 30 minutes depending on your hardware. \nPlease be patient..."));
242240
ui->TEMintStatus->repaint ();
243241

@@ -252,9 +250,6 @@ void PrivacyDialog::on_pushButtonMintReset_clicked()
252250

253251
void PrivacyDialog::on_pushButtonSpentReset_clicked()
254252
{
255-
if (!walletModel || !walletModel->getOptionsModel())
256-
return;
257-
258253
ui->TEMintStatus->setPlainText(tr("Starting ResetSpentZerocoin: "));
259254
ui->TEMintStatus->repaint ();
260255
int64_t nTime = GetTimeMillis();
@@ -296,6 +291,9 @@ void PrivacyDialog::on_pushButtonSpendzPIV_clicked()
296291

297292
void PrivacyDialog::on_pushButtonZPivControl_clicked()
298293
{
294+
if (!walletModel || !walletModel->getOptionsModel())
295+
return;
296+
299297
ZPivControlDialog* zPivControl = new ZPivControlDialog(this);
300298
zPivControl->setModel(walletModel);
301299
zPivControl->exec();
@@ -447,6 +445,15 @@ void PrivacyDialog::sendzPIV()
447445
return;
448446
}
449447

448+
if (walletModel && walletModel->getAddressTableModel()) {
449+
// If zPiv was spent successfully update the addressbook with the label
450+
std::string labelText = ui->addAsLabel->text().toStdString();
451+
if (!labelText.empty())
452+
walletModel->updateAddressBookLabels(address.Get(), labelText, "send");
453+
else
454+
walletModel->updateAddressBookLabels(address.Get(), "(no label)", "send");
455+
}
456+
450457
// Clear zpiv selector in case it was used
451458
ZPivControlDialog::listSelectedMints.clear();
452459
ui->labelzPivSelected_int->setText(QString("0"));
@@ -513,6 +520,9 @@ void PrivacyDialog::coinControlClipboardAmount()
513520
// Coin Control: button inputs -> show actual coin control dialog
514521
void PrivacyDialog::coinControlButtonClicked()
515522
{
523+
if (!walletModel || !walletModel->getOptionsModel())
524+
return;
525+
516526
CoinControlDialog dlg;
517527
dlg.setModel(walletModel);
518528
dlg.exec();

src/qt/walletmodel.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,20 @@ bool WalletModel::validateAddress(const QString& address)
242242
return addressParsed.IsValid();
243243
}
244244

245+
void WalletModel::updateAddressBookLabels(const CTxDestination& dest, const string& strName, const string& strPurpose)
246+
{
247+
LOCK(wallet->cs_wallet);
248+
249+
std::map<CTxDestination, CAddressBookData>::iterator mi = wallet->mapAddressBook.find(dest);
250+
251+
// Check if we have a new address or an updated label
252+
if (mi == wallet->mapAddressBook.end()) {
253+
wallet->SetAddressBook(dest, strName, strPurpose);
254+
} else if (mi->second.name != strName) {
255+
wallet->SetAddressBook(dest, strName, ""); // "" means don't change purpose
256+
}
257+
}
258+
245259
WalletModel::SendCoinsReturn WalletModel::prepareTransaction(WalletModelTransaction& transaction, const CCoinControl* coinControl)
246260
{
247261
CAmount total = 0;
@@ -389,21 +403,12 @@ WalletModel::SendCoinsReturn WalletModel::sendCoins(WalletModelTransaction& tran
389403
foreach (const SendCoinsRecipient& rcp, transaction.getRecipients()) {
390404
// Don't touch the address book when we have a payment request
391405
if (!rcp.paymentRequest.IsInitialized()) {
406+
392407
std::string strAddress = rcp.address.toStdString();
393408
CTxDestination dest = CBitcoinAddress(strAddress).Get();
394409
std::string strLabel = rcp.label.toStdString();
395-
{
396-
LOCK(wallet->cs_wallet);
397410

398-
std::map<CTxDestination, CAddressBookData>::iterator mi = wallet->mapAddressBook.find(dest);
399-
400-
// Check if we have a new address or an updated label
401-
if (mi == wallet->mapAddressBook.end()) {
402-
wallet->SetAddressBook(dest, strLabel, "send");
403-
} else if (mi->second.name != strLabel) {
404-
wallet->SetAddressBook(dest, strLabel, ""); // "" means don't change purpose
405-
}
406-
}
411+
updateAddressBookLabels(dest, strLabel, "send");
407412
}
408413
emit coinsSent(wallet, rcp, transaction_array);
409414
}

src/qt/walletmodel.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ public slots:
293293
void updateMultiSigFlag(bool fHaveMultiSig);
294294
/* Current, immature or unconfirmed balance might have changed - emit 'balanceChanged' if so */
295295
void pollBalanceChanged();
296+
/* Update address book labels in teh database */
297+
void updateAddressBookLabels(const CTxDestination& address, const string& strName, const string& strPurpose);
296298
};
297299

298300
#endif // BITCOIN_QT_WALLETMODEL_H

src/wallet.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3328,7 +3328,6 @@ DBErrors CWallet::ZapWalletTx(std::vector<CWalletTx>& vWtx)
33283328
return DB_LOAD_OK;
33293329
}
33303330

3331-
33323331
bool CWallet::SetAddressBook(const CTxDestination& address, const string& strName, const string& strPurpose)
33333332
{
33343333
bool fUpdated = false;

0 commit comments

Comments
 (0)