1 #include "addresstablemodel.h"
3 #include "walletmodel.h"
10 const QString AddressTableModel::Send = "S";
11 const QString AddressTableModel::Receive = "R";
13 struct AddressTableEntry
24 AddressTableEntry() {}
25 AddressTableEntry(Type type, const QString &label, const QString &address):
26 type(type), label(label), address(address) {}
29 // Private implementation
30 class AddressTablePriv
34 QList<AddressTableEntry> cachedAddressTable;
36 AddressTablePriv(CWallet *wallet):
39 void refreshAddressTable()
41 cachedAddressTable.clear();
44 LOCK(wallet->cs_wallet);
45 BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, std::string)& item, wallet->mapAddressBook)
47 const CBitcoinAddress& address = item.first;
48 const std::string& strName = item.second;
49 bool fMine = wallet->HaveKey(address);
50 cachedAddressTable.append(AddressTableEntry(fMine ? AddressTableEntry::Receiving : AddressTableEntry::Sending,
51 QString::fromStdString(strName),
52 QString::fromStdString(address.ToString())));
59 return cachedAddressTable.size();
62 AddressTableEntry *index(int idx)
64 if(idx >= 0 && idx < cachedAddressTable.size())
66 return &cachedAddressTable[idx];
75 AddressTableModel::AddressTableModel(CWallet *wallet, WalletModel *parent) :
76 QAbstractTableModel(parent),walletModel(parent),wallet(wallet),priv(0)
78 columns << tr("Label") << tr("Address");
79 priv = new AddressTablePriv(wallet);
80 priv->refreshAddressTable();
83 AddressTableModel::~AddressTableModel()
88 int AddressTableModel::rowCount(const QModelIndex &parent) const
94 int AddressTableModel::columnCount(const QModelIndex &parent) const
97 return columns.length();
100 QVariant AddressTableModel::data(const QModelIndex &index, int role) const
105 AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
107 if(role == Qt::DisplayRole || role == Qt::EditRole)
109 switch(index.column())
112 if(rec->label.isEmpty() && role == Qt::DisplayRole)
114 return tr("(no label)");
124 else if (role == Qt::FontRole)
127 if(index.column() == Address)
129 font = GUIUtil::bitcoinAddressFont();
133 else if (role == TypeRole)
137 case AddressTableEntry::Sending:
139 case AddressTableEntry::Receiving:
147 bool AddressTableModel::setData(const QModelIndex & index, const QVariant & value, int role)
151 AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
155 if(role == Qt::EditRole)
157 switch(index.column())
160 wallet->SetAddressBookName(rec->address.toStdString(), value.toString().toStdString());
161 rec->label = value.toString();
164 // Refuse to set invalid address, set error status and return false
165 if(!walletModel->validateAddress(value.toString()))
167 editStatus = INVALID_ADDRESS;
170 // Double-check that we're not overwriting a receiving address
171 if(rec->type == AddressTableEntry::Sending)
174 LOCK(wallet->cs_wallet);
176 wallet->DelAddressBookName(rec->address.toStdString());
177 // Add new entry with new address
178 wallet->SetAddressBookName(value.toString().toStdString(), rec->label.toStdString());
181 rec->address = value.toString();
185 emit dataChanged(index, index);
192 QVariant AddressTableModel::headerData(int section, Qt::Orientation orientation, int role) const
194 if(orientation == Qt::Horizontal)
196 if(role == Qt::DisplayRole)
198 return columns[section];
204 Qt::ItemFlags AddressTableModel::flags(const QModelIndex & index) const
208 AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
210 Qt::ItemFlags retval = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
211 // Can edit address and label for sending addresses,
212 // and only label for receiving addresses.
213 if(rec->type == AddressTableEntry::Sending ||
214 (rec->type == AddressTableEntry::Receiving && index.column()==Label))
216 retval |= Qt::ItemIsEditable;
221 QModelIndex AddressTableModel::index(int row, int column, const QModelIndex & parent) const
224 AddressTableEntry *data = priv->index(row);
227 return createIndex(row, column, priv->index(row));
231 return QModelIndex();
235 void AddressTableModel::update()
237 // Update address book model from Bitcoin core
239 priv->refreshAddressTable();
243 QString AddressTableModel::addRow(const QString &type, const QString &label, const QString &address)
245 std::string strLabel = label.toStdString();
246 std::string strAddress = address.toStdString();
252 if(!walletModel->validateAddress(address))
254 editStatus = INVALID_ADDRESS;
257 // Check for duplicate addresses
259 LOCK(wallet->cs_wallet);
260 if(wallet->mapAddressBook.count(strAddress))
262 editStatus = DUPLICATE_ADDRESS;
267 else if(type == Receive)
269 // Generate a new address to associate with given label
270 WalletModel::UnlockContext ctx(walletModel->requestUnlock());
273 // Unlock wallet failed or was cancelled
274 editStatus = WALLET_UNLOCK_FAILURE;
277 std::vector<unsigned char> newKey;
278 if(!wallet->GetKeyFromPool(newKey, true))
280 editStatus = KEY_GENERATION_FAILURE;
283 strAddress = CBitcoinAddress(newKey).ToString();
291 LOCK(wallet->cs_wallet);
292 wallet->SetAddressBookName(strAddress, strLabel);
294 return QString::fromStdString(strAddress);
297 bool AddressTableModel::removeRows(int row, int count, const QModelIndex & parent)
300 AddressTableEntry *rec = priv->index(row);
301 if(count != 1 || !rec || rec->type == AddressTableEntry::Receiving)
303 // Can only remove one row at a time, and cannot remove rows not in model.
304 // Also refuse to remove receiving addresses.
308 LOCK(wallet->cs_wallet);
309 wallet->DelAddressBookName(rec->address.toStdString());
314 /* Look up label for address in address book, if not found return empty string.
316 QString AddressTableModel::labelForAddress(const QString &address) const
319 LOCK(wallet->cs_wallet);
320 CBitcoinAddress address_parsed(address.toStdString());
321 std::map<CBitcoinAddress, std::string>::iterator mi = wallet->mapAddressBook.find(address_parsed);
322 if (mi != wallet->mapAddressBook.end())
324 return QString::fromStdString(mi->second);
330 int AddressTableModel::lookupAddress(const QString &address) const
332 QModelIndexList lst = match(index(0, Address, QModelIndex()),
333 Qt::EditRole, address, 1, Qt::MatchExactly);
340 return lst.at(0).row();