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)
169 wallet->EraseAddressBookName(rec->address.toStdString());
170 // Add new entry with new address
171 wallet->SetAddressBookName(value.toString().toStdString(), rec->label.toStdString());
173 rec->address = value.toString();
177 emit dataChanged(index, index);
184 QVariant AddressTableModel::headerData(int section, Qt::Orientation orientation, int role) const
186 if(orientation == Qt::Horizontal)
188 if(role == Qt::DisplayRole)
190 return columns[section];
196 Qt::ItemFlags AddressTableModel::flags(const QModelIndex & index) const
200 AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
202 Qt::ItemFlags retval = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
203 // Can edit address and label for sending addresses,
204 // and only label for receiving addresses.
205 if(rec->type == AddressTableEntry::Sending ||
206 (rec->type == AddressTableEntry::Receiving && index.column()==Label))
208 retval |= Qt::ItemIsEditable;
213 QModelIndex AddressTableModel::index(int row, int column, const QModelIndex & parent) const
216 AddressTableEntry *data = priv->index(row);
219 return createIndex(row, column, priv->index(row));
223 return QModelIndex();
227 void AddressTableModel::updateList()
229 // Update internal model from Bitcoin core
231 priv->refreshAddressTable();
235 QString AddressTableModel::addRow(const QString &type, const QString &label, const QString &address)
237 std::string strLabel = label.toStdString();
238 std::string strAddress = address.toStdString();
242 // Check for duplicate
243 CRITICAL_BLOCK(wallet->cs_mapAddressBook)
245 if(wallet->mapAddressBook.count(strAddress))
251 else if(type == Receive)
253 // Generate a new address to associate with given label, optionally
254 // set as default receiving address.
255 strAddress = PubKeyToAddress(wallet->GetKeyFromKeyPool());
261 // Add entry and update list
262 wallet->SetAddressBookName(strAddress, strLabel);
264 return QString::fromStdString(strAddress);
267 bool AddressTableModel::removeRows(int row, int count, const QModelIndex & parent)
270 AddressTableEntry *rec = priv->index(row);
271 if(count != 1 || !rec || rec->type == AddressTableEntry::Receiving)
273 // Can only remove one row at a time, and cannot remove rows not in model.
274 // Also refuse to remove receiving addresses.
277 wallet->EraseAddressBookName(rec->address.toStdString());
282 void AddressTableModel::update()
287 bool AddressTableModel::validateAddress(const QString &address)
291 return AddressToHash160(address.toStdString(), hash160);