1 #include "addresstablemodel.h"
9 const QString AddressTableModel::Send = "S";
10 const QString AddressTableModel::Receive = "R";
12 struct AddressTableEntry
23 AddressTableEntry() {}
24 AddressTableEntry(Type type, const QString &label, const QString &address):
25 type(type), label(label), address(address) {}
28 // Private implementation
29 struct AddressTablePriv
32 QList<AddressTableEntry> cachedAddressTable;
34 AddressTablePriv(CWallet *wallet):
37 void refreshAddressTable()
39 cachedAddressTable.clear();
41 CRITICAL_BLOCK(wallet->cs_mapKeys)
42 CRITICAL_BLOCK(wallet->cs_mapAddressBook)
44 BOOST_FOREACH(const PAIRTYPE(std::string, std::string)& item, wallet->mapAddressBook)
46 std::string strAddress = item.first;
47 std::string strName = item.second;
49 bool fMine = (AddressToHash160(strAddress, hash160) && mapPubKeys.count(hash160));
50 cachedAddressTable.append(AddressTableEntry(fMine ? AddressTableEntry::Receiving : AddressTableEntry::Sending,
51 QString::fromStdString(strName),
52 QString::fromStdString(strAddress)));
59 return cachedAddressTable.size();
62 AddressTableEntry *index(int idx)
64 if(idx >= 0 && idx < cachedAddressTable.size())
66 return &cachedAddressTable[idx];
74 bool isDefaultAddress(const AddressTableEntry *rec)
76 return rec->address == QString::fromStdString(wallet->GetDefaultAddress());
80 AddressTableModel::AddressTableModel(CWallet *wallet, QObject *parent) :
81 QAbstractTableModel(parent),wallet(wallet),priv(0)
83 columns << tr("Label") << tr("Address");
84 priv = new AddressTablePriv(wallet);
85 priv->refreshAddressTable();
88 AddressTableModel::~AddressTableModel()
93 int AddressTableModel::rowCount(const QModelIndex &parent) const
99 int AddressTableModel::columnCount(const QModelIndex &parent) const
102 return columns.length();
105 QVariant AddressTableModel::data(const QModelIndex &index, int role) const
110 AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
112 if(role == Qt::DisplayRole || role == Qt::EditRole)
114 switch(index.column())
120 case IsDefaultAddress:
121 return priv->isDefaultAddress(rec);
124 else if (role == Qt::FontRole)
127 if(index.column() == Address)
129 font = GUIUtil::bitcoinAddressFont();
131 if(priv->isDefaultAddress(rec))
137 else if (role == Qt::ForegroundRole)
139 // Show default address in alternative color
140 if(priv->isDefaultAddress(rec))
142 return QColor(0,0,255);
145 else if (role == Qt::ToolTipRole)
147 if(priv->isDefaultAddress(rec))
149 return tr("Default receiving address");
152 else if (role == TypeRole)
156 case AddressTableEntry::Sending:
158 case AddressTableEntry::Receiving:
166 bool AddressTableModel::setData(const QModelIndex & index, const QVariant & value, int role)
170 AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
172 if(role == Qt::EditRole)
174 switch(index.column())
177 wallet->SetAddressBookName(rec->address.toStdString(), value.toString().toStdString());
178 rec->label = value.toString();
181 // Double-check that we're not overwriting receiving address
182 if(rec->type == AddressTableEntry::Sending)
185 wallet->EraseAddressBookName(rec->address.toStdString());
186 // Add new entry with new address
187 wallet->SetAddressBookName(value.toString().toStdString(), rec->label.toStdString());
189 rec->address = value.toString();
192 case IsDefaultAddress:
195 setDefaultAddress(rec->address);
199 emit dataChanged(index, index);
206 QVariant AddressTableModel::headerData(int section, Qt::Orientation orientation, int role) const
208 if(orientation == Qt::Horizontal)
210 if(role == Qt::DisplayRole)
212 return columns[section];
218 QModelIndex AddressTableModel::index(int row, int column, const QModelIndex & parent) const
221 AddressTableEntry *data = priv->index(row);
224 return createIndex(row, column, priv->index(row));
228 return QModelIndex();
232 void AddressTableModel::updateList()
234 // Update internal model from Bitcoin core
236 priv->refreshAddressTable();
240 QString AddressTableModel::addRow(const QString &type, const QString &label, const QString &address, bool setAsDefault)
242 std::string strLabel = label.toStdString();
243 std::string strAddress = address.toStdString();
247 // Check for duplicate
248 CRITICAL_BLOCK(wallet->cs_mapAddressBook)
250 if(wallet->mapAddressBook.count(strAddress))
256 else if(type == Receive)
258 // Generate a new address to associate with given label, optionally
259 // set as default receiving address.
260 strAddress = PubKeyToAddress(wallet->GetKeyFromKeyPool());
263 setDefaultAddress(QString::fromStdString(strAddress));
270 // Add entry and update list
271 wallet->SetAddressBookName(strAddress, strLabel);
273 return QString::fromStdString(strAddress);
276 bool AddressTableModel::removeRows(int row, int count, const QModelIndex & parent)
279 AddressTableEntry *rec = priv->index(row);
280 if(count != 1 || !rec || rec->type == AddressTableEntry::Receiving)
282 // Can only remove one row at a time, and cannot remove rows not in model.
283 // Also refuse to remove receiving addresses.
286 wallet->EraseAddressBookName(rec->address.toStdString());
291 QString AddressTableModel::getDefaultAddress() const
293 return QString::fromStdString(wallet->GetDefaultAddress());
296 void AddressTableModel::setDefaultAddress(const QString &defaultAddress)
298 wallet->SetDefaultAddress(defaultAddress.toStdString());
301 void AddressTableModel::update()
303 emit defaultAddressChanged(getDefaultAddress());