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 struct AddressTablePriv
33 QList<AddressTableEntry> cachedAddressTable;
35 AddressTablePriv(CWallet *wallet):
38 void refreshAddressTable()
40 cachedAddressTable.clear();
43 LOCK(wallet->cs_wallet);
44 BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, std::string)& item, wallet->mapAddressBook)
46 const CBitcoinAddress& address = item.first;
47 const std::string& strName = item.second;
48 bool fMine = wallet->HaveKey(address);
49 cachedAddressTable.append(AddressTableEntry(fMine ? AddressTableEntry::Receiving : AddressTableEntry::Sending,
50 QString::fromStdString(strName),
51 QString::fromStdString(address.ToString())));
58 return cachedAddressTable.size();
61 AddressTableEntry *index(int idx)
63 if(idx >= 0 && idx < cachedAddressTable.size())
65 return &cachedAddressTable[idx];
74 AddressTableModel::AddressTableModel(CWallet *wallet, WalletModel *parent) :
75 QAbstractTableModel(parent),walletModel(parent),wallet(wallet),priv(0)
77 columns << tr("Label") << tr("Address");
78 priv = new AddressTablePriv(wallet);
79 priv->refreshAddressTable();
82 AddressTableModel::~AddressTableModel()
87 int AddressTableModel::rowCount(const QModelIndex &parent) const
93 int AddressTableModel::columnCount(const QModelIndex &parent) const
96 return columns.length();
99 QVariant AddressTableModel::data(const QModelIndex &index, int role) const
104 AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
106 if(role == Qt::DisplayRole || role == Qt::EditRole)
108 switch(index.column())
111 if(rec->label.isEmpty() && role == Qt::DisplayRole)
113 return tr("(no label)");
123 else if (role == Qt::FontRole)
126 if(index.column() == Address)
128 font = GUIUtil::bitcoinAddressFont();
132 else if (role == TypeRole)
136 case AddressTableEntry::Sending:
138 case AddressTableEntry::Receiving:
146 bool AddressTableModel::setData(const QModelIndex & index, const QVariant & value, int role)
150 AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
154 if(role == Qt::EditRole)
156 switch(index.column())
159 wallet->SetAddressBookName(rec->address.toStdString(), value.toString().toStdString());
160 rec->label = value.toString();
163 // Refuse to set invalid address, set error status and return false
164 if(!walletModel->validateAddress(value.toString()))
166 editStatus = INVALID_ADDRESS;
169 // Double-check that we're not overwriting a receiving address
170 if(rec->type == AddressTableEntry::Sending)
173 LOCK(wallet->cs_wallet);
175 wallet->DelAddressBookName(rec->address.toStdString());
176 // Add new entry with new address
177 wallet->SetAddressBookName(value.toString().toStdString(), rec->label.toStdString());
180 rec->address = value.toString();
184 emit dataChanged(index, index);
191 QVariant AddressTableModel::headerData(int section, Qt::Orientation orientation, int role) const
193 if(orientation == Qt::Horizontal)
195 if(role == Qt::DisplayRole)
197 return columns[section];
203 Qt::ItemFlags AddressTableModel::flags(const QModelIndex & index) const
207 AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
209 Qt::ItemFlags retval = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
210 // Can edit address and label for sending addresses,
211 // and only label for receiving addresses.
212 if(rec->type == AddressTableEntry::Sending ||
213 (rec->type == AddressTableEntry::Receiving && index.column()==Label))
215 retval |= Qt::ItemIsEditable;
220 QModelIndex AddressTableModel::index(int row, int column, const QModelIndex & parent) const
223 AddressTableEntry *data = priv->index(row);
226 return createIndex(row, column, priv->index(row));
230 return QModelIndex();
234 void AddressTableModel::update()
236 // Update address book model from Bitcoin core
238 priv->refreshAddressTable();
242 QString AddressTableModel::addRow(const QString &type, const QString &label, const QString &address)
244 std::string strLabel = label.toStdString();
245 std::string strAddress = address.toStdString();
251 if(!walletModel->validateAddress(address))
253 editStatus = INVALID_ADDRESS;
256 // Check for duplicate addresses
258 LOCK(wallet->cs_wallet);
259 if(wallet->mapAddressBook.count(strAddress))
261 editStatus = DUPLICATE_ADDRESS;
266 else if(type == Receive)
268 // Generate a new address to associate with given label
269 WalletModel::UnlockContext ctx(walletModel->requestUnlock());
272 // Unlock wallet failed or was cancelled
273 editStatus = WALLET_UNLOCK_FAILURE;
276 std::vector<unsigned char> newKey;
277 if(!wallet->GetKeyFromPool(newKey, true))
279 editStatus = KEY_GENERATION_FAILURE;
282 strAddress = CBitcoinAddress(newKey).ToString();
290 LOCK(wallet->cs_wallet);
291 wallet->SetAddressBookName(strAddress, strLabel);
293 return QString::fromStdString(strAddress);
296 bool AddressTableModel::removeRows(int row, int count, const QModelIndex & parent)
299 AddressTableEntry *rec = priv->index(row);
300 if(count != 1 || !rec || rec->type == AddressTableEntry::Receiving)
302 // Can only remove one row at a time, and cannot remove rows not in model.
303 // Also refuse to remove receiving addresses.
307 LOCK(wallet->cs_wallet);
308 wallet->DelAddressBookName(rec->address.toStdString());
313 /* Look up label for address in address book, if not found return empty string.
315 QString AddressTableModel::labelForAddress(const QString &address) const
318 LOCK(wallet->cs_wallet);
319 CBitcoinAddress address_parsed(address.toStdString());
320 std::map<CBitcoinAddress, std::string>::iterator mi = wallet->mapAddressBook.find(address_parsed);
321 if (mi != wallet->mapAddressBook.end())
323 return QString::fromStdString(mi->second);
329 int AddressTableModel::lookupAddress(const QString &address) const
331 QModelIndexList lst = match(index(0, Address, QModelIndex()),
332 Qt::EditRole, address, 1, Qt::MatchExactly);
339 return lst.at(0).row();