1 #include "addresstablemodel.h"
4 #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())));
72 // qLowerBound() and qUpperBound() require our cachedAddressTable list to be sorted in asc order
73 qSort(cachedAddressTable.begin(), cachedAddressTable.end(), AddressTableEntryLessThan());
76 void updateEntry(const QString &address, const QString &label, bool isMine, int status)
78 // Find address / label in model
79 QList<AddressTableEntry>::iterator lower = qLowerBound(
80 cachedAddressTable.begin(), cachedAddressTable.end(), address, AddressTableEntryLessThan());
81 QList<AddressTableEntry>::iterator upper = qUpperBound(
82 cachedAddressTable.begin(), cachedAddressTable.end(), address, AddressTableEntryLessThan());
83 int lowerIndex = (lower - cachedAddressTable.begin());
84 int upperIndex = (upper - cachedAddressTable.begin());
85 bool inModel = (lower != upper);
86 AddressTableEntry::Type newEntryType = isMine ? AddressTableEntry::Receiving : AddressTableEntry::Sending;
93 OutputDebugStringF("Warning: AddressTablePriv::updateEntry: Got CT_NOW, but entry is already in model\n");
96 parent->beginInsertRows(QModelIndex(), lowerIndex, lowerIndex);
97 cachedAddressTable.insert(lowerIndex, AddressTableEntry(newEntryType, label, address));
98 parent->endInsertRows();
103 OutputDebugStringF("Warning: AddressTablePriv::updateEntry: Got CT_UPDATED, but entry is not in model\n");
106 lower->type = newEntryType;
107 lower->label = label;
108 parent->emitDataChanged(lowerIndex);
113 OutputDebugStringF("Warning: AddressTablePriv::updateEntry: Got CT_DELETED, but entry is not in model\n");
116 parent->beginRemoveRows(QModelIndex(), lowerIndex, upperIndex-1);
117 cachedAddressTable.erase(lower, upper);
118 parent->endRemoveRows();
125 return cachedAddressTable.size();
128 AddressTableEntry *index(int idx)
130 if(idx >= 0 && idx < cachedAddressTable.size())
132 return &cachedAddressTable[idx];
141 AddressTableModel::AddressTableModel(CWallet *wallet, WalletModel *parent) :
142 QAbstractTableModel(parent),walletModel(parent),wallet(wallet),priv(0)
144 columns << tr("Label") << tr("Address");
145 priv = new AddressTablePriv(wallet, this);
146 priv->refreshAddressTable();
149 AddressTableModel::~AddressTableModel()
154 int AddressTableModel::rowCount(const QModelIndex &parent) const
160 int AddressTableModel::columnCount(const QModelIndex &parent) const
163 return columns.length();
166 QVariant AddressTableModel::data(const QModelIndex &index, int role) const
171 AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
173 if(role == Qt::DisplayRole || role == Qt::EditRole)
175 switch(index.column())
178 if(rec->label.isEmpty() && role == Qt::DisplayRole)
180 return tr("(no label)");
190 else if (role == Qt::FontRole)
193 if(index.column() == Address)
195 font = GUIUtil::bitcoinAddressFont();
199 else if (role == TypeRole)
203 case AddressTableEntry::Sending:
205 case AddressTableEntry::Receiving:
213 bool AddressTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
217 AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
221 if(role == Qt::EditRole)
223 switch(index.column())
226 // Do nothing, if old label == new label
227 if(rec->label == value.toString())
229 editStatus = NO_CHANGES;
232 wallet->SetAddressBookName(CBitcoinAddress(rec->address.toStdString()).Get(), value.toString().toStdString());
235 // Do nothing, if old address == new address
236 if(CBitcoinAddress(rec->address.toStdString()) == CBitcoinAddress(value.toString().toStdString()))
238 editStatus = NO_CHANGES;
241 // Refuse to set invalid address, set error status and return false
242 else if(!walletModel->validateAddress(value.toString()))
244 editStatus = INVALID_ADDRESS;
247 // Check for duplicate addresses to prevent accidental deletion of addresses, if you try
248 // to paste an existing address over another address (with a different label)
249 else if(wallet->mapAddressBook.count(CBitcoinAddress(value.toString().toStdString()).Get()))
251 editStatus = DUPLICATE_ADDRESS;
254 // Double-check that we're not overwriting a receiving address
255 else if(rec->type == AddressTableEntry::Sending)
258 LOCK(wallet->cs_wallet);
260 wallet->DelAddressBookName(CBitcoinAddress(rec->address.toStdString()).Get());
261 // Add new entry with new address
262 wallet->SetAddressBookName(CBitcoinAddress(value.toString().toStdString()).Get(), rec->label.toStdString());
272 QVariant AddressTableModel::headerData(int section, Qt::Orientation orientation, int role) const
274 if(orientation == Qt::Horizontal)
276 if(role == Qt::DisplayRole)
278 return columns[section];
284 Qt::ItemFlags AddressTableModel::flags(const QModelIndex &index) const
288 AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
290 Qt::ItemFlags retval = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
291 // Can edit address and label for sending addresses,
292 // and only label for receiving addresses.
293 if(rec->type == AddressTableEntry::Sending ||
294 (rec->type == AddressTableEntry::Receiving && index.column()==Label))
296 retval |= Qt::ItemIsEditable;
301 QModelIndex AddressTableModel::index(int row, int column, const QModelIndex &parent) const
304 AddressTableEntry *data = priv->index(row);
307 return createIndex(row, column, priv->index(row));
311 return QModelIndex();
315 void AddressTableModel::updateEntry(const QString &address, const QString &label, bool isMine, int status)
317 // Update address book model from Bitcoin core
318 priv->updateEntry(address, label, isMine, status);
321 QString AddressTableModel::addRow(const QString &type, const QString &label, const QString &address)
323 std::string strLabel = label.toStdString();
324 std::string strAddress = address.toStdString();
330 if(!walletModel->validateAddress(address))
332 editStatus = INVALID_ADDRESS;
335 // Check for duplicate addresses
337 LOCK(wallet->cs_wallet);
338 if(wallet->mapAddressBook.count(CBitcoinAddress(strAddress).Get()))
340 editStatus = DUPLICATE_ADDRESS;
345 else if(type == Receive)
347 // Generate a new address to associate with given label
348 WalletModel::UnlockContext ctx(walletModel->requestUnlock());
351 // Unlock wallet failed or was cancelled
352 editStatus = WALLET_UNLOCK_FAILURE;
356 if(!wallet->GetKeyFromPool(newKey, true))
358 editStatus = KEY_GENERATION_FAILURE;
361 strAddress = CBitcoinAddress(newKey.GetID()).ToString();
370 LOCK(wallet->cs_wallet);
371 wallet->SetAddressBookName(CBitcoinAddress(strAddress).Get(), strLabel);
373 return QString::fromStdString(strAddress);
376 bool AddressTableModel::removeRows(int row, int count, const QModelIndex &parent)
379 AddressTableEntry *rec = priv->index(row);
380 if(count != 1 || !rec || rec->type == AddressTableEntry::Receiving)
382 // Can only remove one row at a time, and cannot remove rows not in model.
383 // Also refuse to remove receiving addresses.
387 LOCK(wallet->cs_wallet);
388 wallet->DelAddressBookName(CBitcoinAddress(rec->address.toStdString()).Get());
393 /* Look up label for address in address book, if not found return empty string.
395 QString AddressTableModel::labelForAddress(const QString &address) const
398 LOCK(wallet->cs_wallet);
399 CBitcoinAddress address_parsed(address.toStdString());
400 std::map<CTxDestination, std::string>::iterator mi = wallet->mapAddressBook.find(address_parsed.Get());
401 if (mi != wallet->mapAddressBook.end())
403 return QString::fromStdString(mi->second);
409 int AddressTableModel::lookupAddress(const QString &address) const
411 QModelIndexList lst = match(index(0, Address, QModelIndex()),
412 Qt::EditRole, address, 1, Qt::MatchExactly);
419 return lst.at(0).row();
423 void AddressTableModel::emitDataChanged(int idx)
425 emit dataChanged(index(idx, 0, QModelIndex()), index(idx, columns.length()-1, QModelIndex()));