Skip to content

Commit 4c01ba6

Browse files
committed
Merge bitcoin#500: [Qt] Remove duplicate code for updating address book labels.
5088478 Remove dupiclate code. Create updateAddressBookLabels function in the wallet model (blondfrogs) Tree-SHA512: c8ff4407a55d50e257acf7f97d1aca32c61c1e7f5659f9cbcd2edeb8489d5461d14b22114d5d399966e649e35a6fd366e403c8fedba6c972c587c67d0d48e916
2 parents f21e445 + 5088478 commit 4c01ba6

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()) {
@@ -237,9 +238,6 @@ void PrivacyDialog::on_pushButtonMintzPIV_clicked()
237238

238239
void PrivacyDialog::on_pushButtonMintReset_clicked()
239240
{
240-
if (!walletModel || !walletModel->getOptionsModel())
241-
return;
242-
243241
ui->TEMintStatus->setPlainText(tr("Starting ResetMintZerocoin: rescanning complete blockchain, this will need up to 30 minutes depending on your hardware. \nPlease be patient..."));
244242
ui->TEMintStatus->repaint ();
245243

@@ -255,9 +253,6 @@ void PrivacyDialog::on_pushButtonMintReset_clicked()
255253

256254
void PrivacyDialog::on_pushButtonSpentReset_clicked()
257255
{
258-
if (!walletModel || !walletModel->getOptionsModel())
259-
return;
260-
261256
ui->TEMintStatus->setPlainText(tr("Starting ResetSpentZerocoin: "));
262257
ui->TEMintStatus->repaint ();
263258
int64_t nTime = GetTimeMillis();
@@ -300,6 +295,9 @@ void PrivacyDialog::on_pushButtonSpendzPIV_clicked()
300295

301296
void PrivacyDialog::on_pushButtonZPivControl_clicked()
302297
{
298+
if (!walletModel || !walletModel->getOptionsModel())
299+
return;
300+
303301
ZPivControlDialog* zPivControl = new ZPivControlDialog(this);
304302
zPivControl->setModel(walletModel);
305303
zPivControl->exec();
@@ -452,6 +450,15 @@ void PrivacyDialog::sendzPIV()
452450
return;
453451
}
454452

453+
if (walletModel && walletModel->getAddressTableModel()) {
454+
// If zPiv was spent successfully update the addressbook with the label
455+
std::string labelText = ui->addAsLabel->text().toStdString();
456+
if (!labelText.empty())
457+
walletModel->updateAddressBookLabels(address.Get(), labelText, "send");
458+
else
459+
walletModel->updateAddressBookLabels(address.Get(), "(no label)", "send");
460+
}
461+
455462
// Clear zpiv selector in case it was used
456463
ZPivControlDialog::listSelectedMints.clear();
457464
ui->labelzPivSelected_int->setText(QString("0"));
@@ -519,6 +526,9 @@ void PrivacyDialog::coinControlClipboardAmount()
519526
// Coin Control: button inputs -> show actual coin control dialog
520527
void PrivacyDialog::coinControlButtonClicked()
521528
{
529+
if (!walletModel || !walletModel->getOptionsModel())
530+
return;
531+
522532
CoinControlDialog dlg;
523533
dlg.setModel(walletModel);
524534
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
@@ -3280,7 +3280,6 @@ DBErrors CWallet::ZapWalletTx(std::vector<CWalletTx>& vWtx)
32803280
return DB_LOAD_OK;
32813281
}
32823282

3283-
32843283
bool CWallet::SetAddressBook(const CTxDestination& address, const string& strName, const string& strPurpose)
32853284
{
32863285
bool fUpdated = false;

0 commit comments

Comments
 (0)