8
8
#include < qt/walletmodel.h>
9
9
10
10
#include < key_io.h>
11
+ #include < wallet/types.h>
11
12
#include < wallet/wallet.h>
12
13
13
14
#include < algorithm>
@@ -52,17 +53,16 @@ struct AddressTableEntryLessThan
52
53
};
53
54
54
55
/* Determine address type from address purpose */
55
- static AddressTableEntry::Type translateTransactionType (const QString &strPurpose , bool isMine)
56
+ static AddressTableEntry::Type translateTransactionType (wallet::AddressPurpose purpose , bool isMine)
56
57
{
57
- AddressTableEntry::Type addressType = AddressTableEntry::Hidden;
58
58
// "refund" addresses aren't shown, and change addresses aren't returned by getAddresses at all.
59
- if (strPurpose == " send " )
60
- addressType = AddressTableEntry::Sending;
61
- else if (strPurpose == " receive " )
62
- addressType = AddressTableEntry::Receiving ;
63
- else if (strPurpose == " unknown " || strPurpose == " " ) // if purpose not set, guess
64
- addressType = (isMine ? AddressTableEntry::Receiving : AddressTableEntry::Sending);
65
- return addressType ;
59
+ switch (purpose) {
60
+ case wallet::AddressPurpose::SEND: return AddressTableEntry::Sending;
61
+ case wallet::AddressPurpose::RECEIVE: return AddressTableEntry::Receiving;
62
+ case wallet::AddressPurpose::REFUND: return AddressTableEntry::Hidden ;
63
+ // No default case to allow for compiler to warn
64
+ }
65
+ assert ( false ) ;
66
66
}
67
67
68
68
// Private implementation
@@ -85,7 +85,7 @@ class AddressTablePriv
85
85
continue ;
86
86
}
87
87
AddressTableEntry::Type addressType = translateTransactionType (
88
- QString::fromStdString ( address.purpose ) , address.is_mine );
88
+ address.purpose , address.is_mine );
89
89
cachedAddressTable.append (AddressTableEntry (addressType,
90
90
QString::fromStdString (address.name ),
91
91
QString::fromStdString (EncodeDestination (address.dest ))));
@@ -97,7 +97,7 @@ class AddressTablePriv
97
97
std::sort (cachedAddressTable.begin (), cachedAddressTable.end (), AddressTableEntryLessThan ());
98
98
}
99
99
100
- void updateEntry (const QString &address, const QString &label, bool isMine, const QString & purpose, int status)
100
+ void updateEntry (const QString &address, const QString &label, bool isMine, wallet::AddressPurpose purpose, int status)
101
101
{
102
102
// Find address / label in model
103
103
QList<AddressTableEntry>::iterator lower = std::lower_bound (
@@ -239,7 +239,7 @@ bool AddressTableModel::setData(const QModelIndex &index, const QVariant &value,
239
239
if (!index .isValid ())
240
240
return false ;
241
241
AddressTableEntry *rec = static_cast <AddressTableEntry*>(index .internalPointer ());
242
- std::string strPurpose = ( rec->type == AddressTableEntry::Sending ? " send " : " receive " ) ;
242
+ wallet::AddressPurpose purpose = rec->type == AddressTableEntry::Sending ? wallet::AddressPurpose::SEND : wallet::AddressPurpose::RECEIVE ;
243
243
editStatus = OK;
244
244
245
245
if (role == Qt::EditRole)
@@ -253,7 +253,7 @@ bool AddressTableModel::setData(const QModelIndex &index, const QVariant &value,
253
253
editStatus = NO_CHANGES;
254
254
return false ;
255
255
}
256
- walletModel->wallet ().setAddressBook (curAddress, value.toString ().toStdString (), strPurpose );
256
+ walletModel->wallet ().setAddressBook (curAddress, value.toString ().toStdString (), purpose );
257
257
} else if (index .column () == Address) {
258
258
CTxDestination newAddress = DecodeDestination (value.toString ().toStdString ());
259
259
// Refuse to set invalid address, set error status and return false
@@ -282,7 +282,7 @@ bool AddressTableModel::setData(const QModelIndex &index, const QVariant &value,
282
282
// Remove old entry
283
283
walletModel->wallet ().delAddressBook (curAddress);
284
284
// Add new entry with new address
285
- walletModel->wallet ().setAddressBook (newAddress, value.toString ().toStdString (), strPurpose );
285
+ walletModel->wallet ().setAddressBook (newAddress, value.toString ().toStdString (), purpose );
286
286
}
287
287
}
288
288
return true ;
@@ -334,7 +334,7 @@ QModelIndex AddressTableModel::index(int row, int column, const QModelIndex &par
334
334
}
335
335
336
336
void AddressTableModel::updateEntry (const QString &address,
337
- const QString &label, bool isMine, const QString & purpose, int status)
337
+ const QString &label, bool isMine, wallet::AddressPurpose purpose, int status)
338
338
{
339
339
// Update address book model from Bitcoin core
340
340
priv->updateEntry (address, label, isMine, purpose, status);
@@ -365,7 +365,7 @@ QString AddressTableModel::addRow(const QString &type, const QString &label, con
365
365
}
366
366
367
367
// Add entry
368
- walletModel->wallet ().setAddressBook (DecodeDestination (strAddress), strLabel, " send " );
368
+ walletModel->wallet ().setAddressBook (DecodeDestination (strAddress), strLabel, wallet::AddressPurpose::SEND );
369
369
}
370
370
else if (type == Receive)
371
371
{
@@ -416,18 +416,18 @@ QString AddressTableModel::labelForAddress(const QString &address) const
416
416
return QString ();
417
417
}
418
418
419
- QString AddressTableModel::purposeForAddress (const QString &address) const
419
+ std::optional<wallet::AddressPurpose> AddressTableModel::purposeForAddress (const QString &address) const
420
420
{
421
- std::string purpose;
421
+ wallet::AddressPurpose purpose;
422
422
if (getAddressData (address, /* name= */ nullptr , &purpose)) {
423
- return QString::fromStdString ( purpose) ;
423
+ return purpose;
424
424
}
425
- return QString () ;
425
+ return std::nullopt ;
426
426
}
427
427
428
428
bool AddressTableModel::getAddressData (const QString &address,
429
429
std::string* name,
430
- std::string * purpose) const {
430
+ wallet::AddressPurpose * purpose) const {
431
431
CTxDestination destination = DecodeDestination (address.toStdString ());
432
432
return walletModel->wallet ().getAddress (destination, name, /* is_mine= */ nullptr , purpose);
433
433
}
0 commit comments