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())
117 if(rec->label.isEmpty())
119 return tr("(no label)");
127 case IsDefaultAddress:
128 return priv->isDefaultAddress(rec);
131 else if (role == Qt::FontRole)
134 if(index.column() == Address)
136 font = GUIUtil::bitcoinAddressFont();
138 if(priv->isDefaultAddress(rec))
144 else if (role == Qt::BackgroundRole)
146 // Show default address in alternative color
147 if(priv->isDefaultAddress(rec))
149 return QColor(255,255,128);
152 else if (role == Qt::ToolTipRole)
154 if(priv->isDefaultAddress(rec))
156 return tr("Default receiving address");
159 else if (role == TypeRole)
163 case AddressTableEntry::Sending:
165 case AddressTableEntry::Receiving:
173 bool AddressTableModel::setData(const QModelIndex & index, const QVariant & value, int role)
177 AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
179 if(role == Qt::EditRole)
181 switch(index.column())
184 wallet->SetAddressBookName(rec->address.toStdString(), value.toString().toStdString());
185 rec->label = value.toString();
188 // Double-check that we're not overwriting receiving address
189 if(rec->type == AddressTableEntry::Sending)
192 wallet->EraseAddressBookName(rec->address.toStdString());
193 // Add new entry with new address
194 wallet->SetAddressBookName(value.toString().toStdString(), rec->label.toStdString());
196 rec->address = value.toString();
199 case IsDefaultAddress:
202 setDefaultAddress(rec->address);
206 emit dataChanged(index, index);
213 QVariant AddressTableModel::headerData(int section, Qt::Orientation orientation, int role) const
215 if(orientation == Qt::Horizontal)
217 if(role == Qt::DisplayRole)
219 return columns[section];
225 QModelIndex AddressTableModel::index(int row, int column, const QModelIndex & parent) const
228 AddressTableEntry *data = priv->index(row);
231 return createIndex(row, column, priv->index(row));
235 return QModelIndex();
239 void AddressTableModel::updateList()
241 // Update internal model from Bitcoin core
243 priv->refreshAddressTable();
247 QString AddressTableModel::addRow(const QString &type, const QString &label, const QString &address, bool setAsDefault)
249 std::string strLabel = label.toStdString();
250 std::string strAddress = address.toStdString();
254 // Check for duplicate
255 CRITICAL_BLOCK(wallet->cs_mapAddressBook)
257 if(wallet->mapAddressBook.count(strAddress))
263 else if(type == Receive)
265 // Generate a new address to associate with given label, optionally
266 // set as default receiving address.
267 strAddress = PubKeyToAddress(wallet->GetKeyFromKeyPool());
270 setDefaultAddress(QString::fromStdString(strAddress));
277 // Add entry and update list
278 wallet->SetAddressBookName(strAddress, strLabel);
280 return QString::fromStdString(strAddress);
283 bool AddressTableModel::removeRows(int row, int count, const QModelIndex & parent)
286 AddressTableEntry *rec = priv->index(row);
287 if(count != 1 || !rec || rec->type == AddressTableEntry::Receiving)
289 // Can only remove one row at a time, and cannot remove rows not in model.
290 // Also refuse to remove receiving addresses.
293 wallet->EraseAddressBookName(rec->address.toStdString());
298 QString AddressTableModel::getDefaultAddress() const
300 return QString::fromStdString(wallet->GetDefaultAddress());
303 void AddressTableModel::setDefaultAddress(const QString &defaultAddress)
305 wallet->SetDefaultAddress(defaultAddress.toStdString());
308 void AddressTableModel::update()
310 emit defaultAddressChanged(getDefaultAddress());