#include "addresstablemodel.h"
#include "guiutil.h"
-#include "main.h"
+
+#include "headers.h"
#include <QFont>
+#include <QColor>
const QString AddressTableModel::Send = "S";
const QString AddressTableModel::Receive = "R";
type(type), label(label), address(address) {}
};
-/* Private implementation */
+// Private implementation
struct AddressTablePriv
{
+ CWallet *wallet;
QList<AddressTableEntry> cachedAddressTable;
+ AddressTablePriv(CWallet *wallet):
+ wallet(wallet) {}
+
void refreshAddressTable()
{
cachedAddressTable.clear();
- CRITICAL_BLOCK(cs_mapKeys)
- CRITICAL_BLOCK(cs_mapAddressBook)
+ CRITICAL_BLOCK(wallet->cs_mapKeys)
+ CRITICAL_BLOCK(wallet->cs_mapAddressBook)
{
- BOOST_FOREACH(const PAIRTYPE(std::string, std::string)& item, mapAddressBook)
+ BOOST_FOREACH(const PAIRTYPE(std::string, std::string)& item, wallet->mapAddressBook)
{
std::string strAddress = item.first;
std::string strName = item.second;
return 0;
}
}
+
+ bool isDefaultAddress(const AddressTableEntry *rec)
+ {
+ return rec->address == QString::fromStdString(wallet->GetDefaultAddress());
+ }
};
-AddressTableModel::AddressTableModel(QObject *parent) :
- QAbstractTableModel(parent),priv(0)
+AddressTableModel::AddressTableModel(CWallet *wallet, QObject *parent) :
+ QAbstractTableModel(parent),wallet(wallet),priv(0)
{
columns << tr("Label") << tr("Address");
- priv = new AddressTablePriv();
+ priv = new AddressTablePriv(wallet);
priv->refreshAddressTable();
}
return rec->label;
case Address:
return rec->address;
+ case IsDefaultAddress:
+ return priv->isDefaultAddress(rec);
}
}
else if (role == Qt::FontRole)
{
+ QFont font;
if(index.column() == Address)
{
- return GUIUtil::bitcoinAddressFont();
+ font = GUIUtil::bitcoinAddressFont();
+ }
+ if(priv->isDefaultAddress(rec))
+ {
+ font.setBold(true);
+ }
+ return font;
+ }
+ else if (role == Qt::ForegroundRole)
+ {
+ // Show default address in alternative color
+ if(priv->isDefaultAddress(rec))
+ {
+ return QColor(0,0,255);
+ }
+ }
+ else if (role == Qt::ToolTipRole)
+ {
+ if(priv->isDefaultAddress(rec))
+ {
+ return tr("Default receiving address");
}
}
else if (role == TypeRole)
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 */
+ // Double-check that we're not overwriting 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());
+ // Remove old entry
+ wallet->EraseAddressBookName(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);
void AddressTableModel::updateList()
{
- /* Update internal model from Bitcoin core */
+ // Update internal model from Bitcoin core
beginResetModel();
priv->refreshAddressTable();
endResetModel();
}
-QString AddressTableModel::addRow(const QString &type, const QString &label, const QString &address)
+QString AddressTableModel::addRow(const QString &type, const QString &label, const QString &address, bool setAsDefault)
{
std::string strLabel = label.toStdString();
std::string strAddress = address.toStdString();
if(type == Send)
{
- /* Check for duplicate */
- CRITICAL_BLOCK(cs_mapAddressBook)
+ // Check for duplicate
+ CRITICAL_BLOCK(wallet->cs_mapAddressBook)
{
- if(mapAddressBook.count(strAddress))
+ if(wallet->mapAddressBook.count(strAddress))
{
return QString();
}
}
else if(type == Receive)
{
- /* Generate a new address to associate with given label */
- strAddress = PubKeyToAddress(GetKeyFromKeyPool());
+ // Generate a new address to associate with given label, optionally
+ // set as default receiving address.
+ strAddress = PubKeyToAddress(wallet->GetKeyFromKeyPool());
+ if(setAsDefault)
+ {
+ setDefaultAddress(QString::fromStdString(strAddress));
+ }
}
else
{
return QString();
}
- /* Add entry and update list */
- SetAddressBookName(strAddress, strLabel);
+ // Add entry and update list
+ wallet->SetAddressBookName(strAddress, strLabel);
updateList();
return QString::fromStdString(strAddress);
}
AddressTableEntry *rec = priv->index(row);
if(count != 1 || !rec || rec->type == AddressTableEntry::Receiving)
{
- /* Can only remove one row at a time, and cannot remove rows not in model.
- Also refuse to remove receiving addresses.
- */
+ // Can only remove one row at a time, and cannot remove rows not in model.
+ // Also refuse to remove receiving addresses.
return false;
}
- CWalletDB().EraseName(rec->address.toStdString());
+ wallet->EraseAddressBookName(rec->address.toStdString());
updateList();
return true;
}
+
+QString AddressTableModel::getDefaultAddress() const
+{
+ return QString::fromStdString(wallet->GetDefaultAddress());
+}
+
+void AddressTableModel::setDefaultAddress(const QString &defaultAddress)
+{
+ wallet->SetDefaultAddress(defaultAddress.toStdString());
+}
+
+void AddressTableModel::update()
+{
+ emit defaultAddressChanged(getDefaultAddress());
+}