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];
75 AddressTableModel::AddressTableModel(CWallet *wallet, QObject *parent) :
76 QAbstractTableModel(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());
153 if(role == Qt::EditRole)
155 switch(index.column())
158 wallet->SetAddressBookName(rec->address.toStdString(), value.toString().toStdString());
159 rec->label = value.toString();
162 // Refuse to set invalid address
163 if(!validateAddress(value.toString()))
165 // Double-check that we're not overwriting receiving address
166 if(rec->type == AddressTableEntry::Sending)
168 CRITICAL_BLOCK(wallet->cs_mapAddressBook)
171 wallet->DelAddressBookName(rec->address.toStdString());
172 // Add new entry with new address
173 wallet->SetAddressBookName(value.toString().toStdString(), rec->label.toStdString());
176 rec->address = value.toString();
180 emit dataChanged(index, index);
187 QVariant AddressTableModel::headerData(int section, Qt::Orientation orientation, int role) const
189 if(orientation == Qt::Horizontal)
191 if(role == Qt::DisplayRole)
193 return columns[section];
199 Qt::ItemFlags AddressTableModel::flags(const QModelIndex & index) const
203 AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
205 Qt::ItemFlags retval = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
206 // Can edit address and label for sending addresses,
207 // and only label for receiving addresses.
208 if(rec->type == AddressTableEntry::Sending ||
209 (rec->type == AddressTableEntry::Receiving && index.column()==Label))
211 retval |= Qt::ItemIsEditable;
216 QModelIndex AddressTableModel::index(int row, int column, const QModelIndex & parent) const
219 AddressTableEntry *data = priv->index(row);
222 return createIndex(row, column, priv->index(row));
226 return QModelIndex();
230 void AddressTableModel::updateList()
232 // Update internal model from Bitcoin core
234 priv->refreshAddressTable();
238 QString AddressTableModel::addRow(const QString &type, const QString &label, const QString &address)
240 std::string strLabel = label.toStdString();
241 std::string strAddress = address.toStdString();
245 // Check for duplicate
246 CRITICAL_BLOCK(wallet->cs_mapAddressBook)
248 if(wallet->mapAddressBook.count(strAddress))
254 else if(type == Receive)
256 // Generate a new address to associate with given label, optionally
257 // set as default receiving address.
258 strAddress = PubKeyToAddress(wallet->GetKeyFromKeyPool());
264 // Add entry and update list
265 wallet->SetAddressBookName(strAddress, strLabel);
267 return QString::fromStdString(strAddress);
270 bool AddressTableModel::removeRows(int row, int count, const QModelIndex & parent)
273 AddressTableEntry *rec = priv->index(row);
274 if(count != 1 || !rec || rec->type == AddressTableEntry::Receiving)
276 // Can only remove one row at a time, and cannot remove rows not in model.
277 // Also refuse to remove receiving addresses.
280 CRITICAL_BLOCK(wallet->cs_mapAddressBook)
282 wallet->DelAddressBookName(rec->address.toStdString());
288 void AddressTableModel::update()
293 bool AddressTableModel::validateAddress(const QString &address)
297 return AddressToHash160(address.toStdString(), hash160);
300 /* Look up label for address in address book, if not found return empty string.
302 QString AddressTableModel::labelForAddress(const QString &address) const
304 CRITICAL_BLOCK(wallet->cs_mapAddressBook)
306 std::map<std::string, std::string>::iterator mi = wallet->mapAddressBook.find(address.toStdString());
307 if (mi != wallet->mapAddressBook.end())
309 return QString::fromStdString(mi->second);
315 int AddressTableModel::lookupAddress(const QString &address) const
317 QModelIndexList lst = match(index(0, Address, QModelIndex()),
318 Qt::EditRole, address, 1, Qt::MatchExactly);
325 return lst.at(0).row();