#include "addresstablemodel.h"
#include "guiutil.h"
-#include "main.h"
+#include "walletmodel.h"
+
+#include "wallet.h"
#include <QFont>
#include <QColor>
AddressTableEntry() {}
AddressTableEntry(Type type, const QString &label, const QString &address):
type(type), label(label), address(address) {}
-
- bool isDefaultAddress() const
- {
- std::vector<unsigned char> vchPubKey;
- if (CWalletDB("r").ReadDefaultKey(vchPubKey))
- {
- return address == QString::fromStdString(PubKeyToAddress(vchPubKey));
- }
- return false;
- }
};
// Private implementation
-struct AddressTablePriv
+class AddressTablePriv
{
+public:
+ CWallet *wallet;
QList<AddressTableEntry> cachedAddressTable;
+ AddressTablePriv(CWallet *wallet):
+ wallet(wallet) {}
+
void refreshAddressTable()
{
cachedAddressTable.clear();
- CRITICAL_BLOCK(cs_mapKeys)
- CRITICAL_BLOCK(cs_mapAddressBook)
{
- BOOST_FOREACH(const PAIRTYPE(std::string, std::string)& item, mapAddressBook)
+ LOCK(wallet->cs_wallet);
+ BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, std::string)& item, wallet->mapAddressBook)
{
- std::string strAddress = item.first;
- std::string strName = item.second;
- uint160 hash160;
- bool fMine = (AddressToHash160(strAddress, hash160) && mapPubKeys.count(hash160));
+ const CBitcoinAddress& address = item.first;
+ const std::string& strName = item.second;
+ bool fMine = wallet->HaveKey(address);
cachedAddressTable.append(AddressTableEntry(fMine ? AddressTableEntry::Receiving : AddressTableEntry::Sending,
QString::fromStdString(strName),
- QString::fromStdString(strAddress)));
+ QString::fromStdString(address.ToString())));
}
}
}
}
};
-AddressTableModel::AddressTableModel(QObject *parent) :
- QAbstractTableModel(parent),priv(0)
+AddressTableModel::AddressTableModel(CWallet *wallet, WalletModel *parent) :
+ QAbstractTableModel(parent),walletModel(parent),wallet(wallet),priv(0)
{
columns << tr("Label") << tr("Address");
- priv = new AddressTablePriv();
+ priv = new AddressTablePriv(wallet);
priv->refreshAddressTable();
}
switch(index.column())
{
case Label:
- return rec->label;
+ if(rec->label.isEmpty() && role == Qt::DisplayRole)
+ {
+ return tr("(no label)");
+ }
+ else
+ {
+ return rec->label;
+ }
case Address:
return rec->address;
- case IsDefaultAddress:
- return rec->isDefaultAddress();
}
}
else if (role == Qt::FontRole)
{
font = GUIUtil::bitcoinAddressFont();
}
- if(rec->isDefaultAddress())
- {
- font.setBold(true);
- }
return font;
}
- else if (role == Qt::ForegroundRole)
- {
- // Show default address in alternative color
- if(rec->isDefaultAddress())
- {
- return QColor(0,0,255);
- }
- }
- else if (role == Qt::ToolTipRole)
- {
- if(rec->isDefaultAddress())
- {
- return tr("Default receiving address");
- }
- }
else if (role == TypeRole)
{
switch(rec->type)
return false;
AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
+ editStatus = OK;
+
if(role == Qt::EditRole)
{
switch(index.column())
{
case Label:
- SetAddressBookName(rec->address.toStdString(), value.toString().toStdString());
+ wallet->SetAddressBookName(rec->address.toStdString(), value.toString().toStdString());
rec->label = value.toString();
break;
case Address:
- // Double-check that we're not overwriting receiving address
+ // Refuse to set invalid address, set error status and return false
+ if(!walletModel->validateAddress(value.toString()))
+ {
+ editStatus = INVALID_ADDRESS;
+ return false;
+ }
+ // Double-check that we're not overwriting a receiving address
if(rec->type == AddressTableEntry::Sending)
{
- // Remove old entry
- CWalletDB().EraseName(rec->address.toStdString());
- // Add new entry with new address
- SetAddressBookName(value.toString().toStdString(), rec->label.toStdString());
+ {
+ LOCK(wallet->cs_wallet);
+ // Remove old entry
+ wallet->DelAddressBookName(rec->address.toStdString());
+ // Add new entry with new address
+ wallet->SetAddressBookName(value.toString().toStdString(), rec->label.toStdString());
+ }
rec->address = value.toString();
}
break;
- case IsDefaultAddress:
- if(value.toBool())
- {
- setDefaultAddress(rec->address);
- }
- break;
}
emit dataChanged(index, index);
return QVariant();
}
+Qt::ItemFlags AddressTableModel::flags(const QModelIndex & index) const
+{
+ if(!index.isValid())
+ return 0;
+ AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
+
+ Qt::ItemFlags retval = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
+ // Can edit address and label for sending addresses,
+ // and only label for receiving addresses.
+ if(rec->type == AddressTableEntry::Sending ||
+ (rec->type == AddressTableEntry::Receiving && index.column()==Label))
+ {
+ retval |= Qt::ItemIsEditable;
+ }
+ return retval;
+}
+
QModelIndex AddressTableModel::index(int row, int column, const QModelIndex & parent) const
{
Q_UNUSED(parent);
}
}
-void AddressTableModel::updateList()
+void AddressTableModel::update()
{
- // Update internal model from Bitcoin core
+ // Update address book model from Bitcoin core
beginResetModel();
priv->refreshAddressTable();
endResetModel();
}
-QString AddressTableModel::addRow(const QString &type, const QString &label, const QString &address, bool setAsDefault)
+QString AddressTableModel::addRow(const QString &type, const QString &label, const QString &address)
{
std::string strLabel = label.toStdString();
std::string strAddress = address.toStdString();
+ editStatus = OK;
+
if(type == Send)
{
- // Check for duplicate
- CRITICAL_BLOCK(cs_mapAddressBook)
+ if(!walletModel->validateAddress(address))
{
- if(mapAddressBook.count(strAddress))
+ editStatus = INVALID_ADDRESS;
+ return QString();
+ }
+ // Check for duplicate addresses
+ {
+ LOCK(wallet->cs_wallet);
+ if(wallet->mapAddressBook.count(strAddress))
{
+ editStatus = DUPLICATE_ADDRESS;
return QString();
}
}
}
else if(type == Receive)
{
- // Generate a new address to associate with given label, optionally
- // set as default receiving address.
- strAddress = PubKeyToAddress(GetKeyFromKeyPool());
- if(setAsDefault)
+ // Generate a new address to associate with given label
+ WalletModel::UnlockContext ctx(walletModel->requestUnlock());
+ if(!ctx.isValid())
+ {
+ // Unlock wallet failed or was cancelled
+ editStatus = WALLET_UNLOCK_FAILURE;
+ return QString();
+ }
+ std::vector<unsigned char> newKey;
+ if(!wallet->GetKeyFromPool(newKey, true))
{
- setDefaultAddress(QString::fromStdString(strAddress));
+ editStatus = KEY_GENERATION_FAILURE;
+ return QString();
}
+ strAddress = CBitcoinAddress(newKey).ToString();
}
else
{
return QString();
}
- // Add entry and update list
- SetAddressBookName(strAddress, strLabel);
- updateList();
+ // Add entry
+ {
+ LOCK(wallet->cs_wallet);
+ wallet->SetAddressBookName(strAddress, strLabel);
+ }
return QString::fromStdString(strAddress);
}
// Also refuse to remove receiving addresses.
return false;
}
- CWalletDB().EraseName(rec->address.toStdString());
- updateList();
+ {
+ LOCK(wallet->cs_wallet);
+ wallet->DelAddressBookName(rec->address.toStdString());
+ }
return true;
}
-QString AddressTableModel::getDefaultAddress() const
+/* Look up label for address in address book, if not found return empty string.
+ */
+QString AddressTableModel::labelForAddress(const QString &address) const
{
- std::vector<unsigned char> vchPubKey;
- if (CWalletDB("r").ReadDefaultKey(vchPubKey))
- {
- return QString::fromStdString(PubKeyToAddress(vchPubKey));
- }
- else
{
- return QString();
+ LOCK(wallet->cs_wallet);
+ CBitcoinAddress address_parsed(address.toStdString());
+ std::map<CBitcoinAddress, std::string>::iterator mi = wallet->mapAddressBook.find(address_parsed);
+ if (mi != wallet->mapAddressBook.end())
+ {
+ return QString::fromStdString(mi->second);
+ }
}
+ return QString();
}
-void AddressTableModel::setDefaultAddress(const QString &defaultAddress)
+int AddressTableModel::lookupAddress(const QString &address) const
{
- uint160 hash160;
- std::string strAddress = defaultAddress.toStdString();
- if (!AddressToHash160(strAddress, hash160))
- return;
- if (!mapPubKeys.count(hash160))
- return;
- CWalletDB().WriteDefaultKey(mapPubKeys[hash160]);
+ QModelIndexList lst = match(index(0, Address, QModelIndex()),
+ Qt::EditRole, address, 1, Qt::MatchExactly);
+ if(lst.isEmpty())
+ {
+ return -1;
+ }
+ else
+ {
+ return lst.at(0).row();
+ }
}
-void AddressTableModel::update()
-{
- emit defaultAddressChanged(getDefaultAddress());
-}