1 #include "addresstablemodel.h"
3 #include "walletmodel.h"
11 const QString AddressTableModel::Send = "S";
12 const QString AddressTableModel::Receive = "R";
14 struct AddressTableEntry
25 AddressTableEntry() {}
26 AddressTableEntry(Type type, const QString &label, const QString &address):
27 type(type), label(label), address(address) {}
30 struct AddressTableEntryLessThan
32 bool operator()(const AddressTableEntry &a, const AddressTableEntry &b) const
34 return a.address < b.address;
36 bool operator()(const AddressTableEntry &a, const QString &b) const
40 bool operator()(const QString &a, const AddressTableEntry &b) const
46 // Private implementation
47 class AddressTablePriv
51 QList<AddressTableEntry> cachedAddressTable;
52 AddressTableModel *parent;
54 AddressTablePriv(CWallet *wallet, AddressTableModel *parent):
55 wallet(wallet), parent(parent) {}
57 void refreshAddressTable()
59 cachedAddressTable.clear();
61 LOCK(wallet->cs_wallet);
62 BOOST_FOREACH(const PAIRTYPE(CTxDestination, std::string)& item, wallet->mapAddressBook)
64 const CBitcoinAddress& address = item.first;
65 const std::string& strName = item.second;
66 bool fMine = IsMine(*wallet, address.Get());
67 cachedAddressTable.append(AddressTableEntry(fMine ? AddressTableEntry::Receiving : AddressTableEntry::Sending,
68 QString::fromStdString(strName),
69 QString::fromStdString(address.ToString())));
74 void updateEntry(const QString &address, const QString &label, bool isMine, int status)
76 // Find address / label in model
77 QList<AddressTableEntry>::iterator lower = qLowerBound(
78 cachedAddressTable.begin(), cachedAddressTable.end(), address, AddressTableEntryLessThan());
79 QList<AddressTableEntry>::iterator upper = qUpperBound(
80 cachedAddressTable.begin(), cachedAddressTable.end(), address, AddressTableEntryLessThan());
81 int lowerIndex = (lower - cachedAddressTable.begin());
82 int upperIndex = (upper - cachedAddressTable.begin());
83 bool inModel = (lower != upper);
84 AddressTableEntry::Type newEntryType = isMine ? AddressTableEntry::Receiving : AddressTableEntry::Sending;
91 OutputDebugStringF("Warning: AddressTablePriv::updateEntry: Got CT_NOW, but entry is already in model\n");
94 parent->beginInsertRows(QModelIndex(), lowerIndex, lowerIndex);
95 cachedAddressTable.insert(lowerIndex, AddressTableEntry(newEntryType, label, address));
96 parent->endInsertRows();
101 OutputDebugStringF("Warning: AddressTablePriv::updateEntry: Got CT_UPDATED, but entry is not in model\n");
104 lower->type = newEntryType;
105 lower->label = label;
106 parent->emitDataChanged(lowerIndex);
111 OutputDebugStringF("Warning: AddressTablePriv::updateEntry: Got CT_DELETED, but entry is not in model\n");
114 parent->beginRemoveRows(QModelIndex(), lowerIndex, upperIndex-1);
115 cachedAddressTable.erase(lower, upper);
116 parent->endRemoveRows();
123 return cachedAddressTable.size();
126 AddressTableEntry *index(int idx)
128 if(idx >= 0 && idx < cachedAddressTable.size())
130 return &cachedAddressTable[idx];
139 AddressTableModel::AddressTableModel(CWallet *wallet, WalletModel *parent) :
140 QAbstractTableModel(parent),walletModel(parent),wallet(wallet),priv(0)
142 columns << tr("Label") << tr("Address");
143 priv = new AddressTablePriv(wallet, this);
144 priv->refreshAddressTable();
147 AddressTableModel::~AddressTableModel()
152 int AddressTableModel::rowCount(const QModelIndex &parent) const
158 int AddressTableModel::columnCount(const QModelIndex &parent) const
161 return columns.length();
164 QVariant AddressTableModel::data(const QModelIndex &index, int role) const
169 AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
171 if(role == Qt::DisplayRole || role == Qt::EditRole)
173 switch(index.column())
176 if(rec->label.isEmpty() && role == Qt::DisplayRole)
178 return tr("(no label)");
188 else if (role == Qt::FontRole)
191 if(index.column() == Address)
193 font = GUIUtil::bitcoinAddressFont();
197 else if (role == TypeRole)
201 case AddressTableEntry::Sending:
203 case AddressTableEntry::Receiving:
211 bool AddressTableModel::setData(const QModelIndex & index, const QVariant & value, int role)
215 AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
219 if(role == Qt::EditRole)
221 switch(index.column())
224 wallet->SetAddressBookName(CBitcoinAddress(rec->address.toStdString()).Get(), value.toString().toStdString());
225 rec->label = value.toString();
228 // Refuse to set invalid address, set error status and return false
229 if(!walletModel->validateAddress(value.toString()))
231 editStatus = INVALID_ADDRESS;
234 // Double-check that we're not overwriting a receiving address
235 if(rec->type == AddressTableEntry::Sending)
238 LOCK(wallet->cs_wallet);
240 wallet->DelAddressBookName(CBitcoinAddress(rec->address.toStdString()).Get());
241 // Add new entry with new address
242 wallet->SetAddressBookName(CBitcoinAddress(value.toString().toStdString()).Get(), rec->label.toStdString());
253 QVariant AddressTableModel::headerData(int section, Qt::Orientation orientation, int role) const
255 if(orientation == Qt::Horizontal)
257 if(role == Qt::DisplayRole)
259 return columns[section];
265 Qt::ItemFlags AddressTableModel::flags(const QModelIndex & index) const
269 AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
271 Qt::ItemFlags retval = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
272 // Can edit address and label for sending addresses,
273 // and only label for receiving addresses.
274 if(rec->type == AddressTableEntry::Sending ||
275 (rec->type == AddressTableEntry::Receiving && index.column()==Label))
277 retval |= Qt::ItemIsEditable;
282 QModelIndex AddressTableModel::index(int row, int column, const QModelIndex & parent) const
285 AddressTableEntry *data = priv->index(row);
288 return createIndex(row, column, priv->index(row));
292 return QModelIndex();
296 void AddressTableModel::updateEntry(const QString &address, const QString &label, bool isMine, int status)
298 // Update address book model from Bitcoin core
299 priv->updateEntry(address, label, isMine, status);
302 QString AddressTableModel::addRow(const QString &type, const QString &label, const QString &address)
304 std::string strLabel = label.toStdString();
305 std::string strAddress = address.toStdString();
311 if(!walletModel->validateAddress(address))
313 editStatus = INVALID_ADDRESS;
316 // Check for duplicate addresses
318 LOCK(wallet->cs_wallet);
319 if(wallet->mapAddressBook.count(CBitcoinAddress(strAddress).Get()))
321 editStatus = DUPLICATE_ADDRESS;
326 else if(type == Receive)
328 // Generate a new address to associate with given label
329 WalletModel::UnlockContext ctx(walletModel->requestUnlock());
332 // Unlock wallet failed or was cancelled
333 editStatus = WALLET_UNLOCK_FAILURE;
337 if(!wallet->GetKeyFromPool(newKey, true))
339 editStatus = KEY_GENERATION_FAILURE;
342 strAddress = CBitcoinAddress(newKey.GetID()).ToString();
350 LOCK(wallet->cs_wallet);
351 wallet->SetAddressBookName(CBitcoinAddress(strAddress).Get(), strLabel);
353 return QString::fromStdString(strAddress);
356 bool AddressTableModel::removeRows(int row, int count, const QModelIndex & parent)
359 AddressTableEntry *rec = priv->index(row);
360 if(count != 1 || !rec || rec->type == AddressTableEntry::Receiving)
362 // Can only remove one row at a time, and cannot remove rows not in model.
363 // Also refuse to remove receiving addresses.
367 LOCK(wallet->cs_wallet);
368 wallet->DelAddressBookName(CBitcoinAddress(rec->address.toStdString()).Get());
373 /* Look up label for address in address book, if not found return empty string.
375 QString AddressTableModel::labelForAddress(const QString &address) const
378 LOCK(wallet->cs_wallet);
379 CBitcoinAddress address_parsed(address.toStdString());
380 std::map<CTxDestination, std::string>::iterator mi = wallet->mapAddressBook.find(address_parsed.Get());
381 if (mi != wallet->mapAddressBook.end())
383 return QString::fromStdString(mi->second);
389 int AddressTableModel::lookupAddress(const QString &address) const
391 QModelIndexList lst = match(index(0, Address, QModelIndex()),
392 Qt::EditRole, address, 1, Qt::MatchExactly);
399 return lst.at(0).row();
403 void AddressTableModel::emitDataChanged(int idx)
405 emit dataChanged(index(idx, 0, QModelIndex()), index(idx, columns.length()-1, QModelIndex()));