1 #include "addresstablemodel.h"
8 const QString AddressTableModel::Send = "S";
9 const QString AddressTableModel::Receive = "R";
11 struct AddressTableEntry
22 AddressTableEntry() {}
23 AddressTableEntry(Type type, const QString &label, const QString &address):
24 type(type), label(label), address(address) {}
26 bool isDefaultAddress() const
28 std::vector<unsigned char> vchPubKey;
29 if (CWalletDB("r").ReadDefaultKey(vchPubKey))
31 return address == QString::fromStdString(PubKeyToAddress(vchPubKey));
37 // Private implementation
38 struct AddressTablePriv
40 QList<AddressTableEntry> cachedAddressTable;
42 void refreshAddressTable()
44 cachedAddressTable.clear();
46 CRITICAL_BLOCK(cs_mapKeys)
47 CRITICAL_BLOCK(cs_mapAddressBook)
49 BOOST_FOREACH(const PAIRTYPE(std::string, std::string)& item, mapAddressBook)
51 std::string strAddress = item.first;
52 std::string strName = item.second;
54 bool fMine = (AddressToHash160(strAddress, hash160) && mapPubKeys.count(hash160));
55 cachedAddressTable.append(AddressTableEntry(fMine ? AddressTableEntry::Receiving : AddressTableEntry::Sending,
56 QString::fromStdString(strName),
57 QString::fromStdString(strAddress)));
64 return cachedAddressTable.size();
67 AddressTableEntry *index(int idx)
69 if(idx >= 0 && idx < cachedAddressTable.size())
71 return &cachedAddressTable[idx];
80 AddressTableModel::AddressTableModel(QObject *parent) :
81 QAbstractTableModel(parent),priv(0)
83 columns << tr("Label") << tr("Address");
84 priv = new AddressTablePriv();
85 priv->refreshAddressTable();
88 AddressTableModel::~AddressTableModel()
93 int AddressTableModel::rowCount(const QModelIndex &parent) const
99 int AddressTableModel::columnCount(const QModelIndex &parent) const
102 return columns.length();
105 QVariant AddressTableModel::data(const QModelIndex &index, int role) const
110 AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
112 if(role == Qt::DisplayRole || role == Qt::EditRole)
114 switch(index.column())
122 else if (role == Qt::FontRole)
125 if(index.column() == Address)
127 font = GUIUtil::bitcoinAddressFont();
129 if(rec->isDefaultAddress())
135 else if (role == Qt::ForegroundRole)
137 // Show default address in alternative color
138 if(rec->isDefaultAddress())
140 return QColor(0,0,255);
143 else if (role == Qt::ToolTipRole)
145 if(rec->isDefaultAddress())
147 return tr("Default receiving address");
150 else if (role == TypeRole)
154 case AddressTableEntry::Sending:
156 case AddressTableEntry::Receiving:
164 bool AddressTableModel::setData(const QModelIndex & index, const QVariant & value, int role)
168 AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
170 if(role == Qt::EditRole)
172 switch(index.column())
175 SetAddressBookName(rec->address.toStdString(), value.toString().toStdString());
176 rec->label = value.toString();
179 // Double-check that we're not overwriting receiving address
180 if(rec->type == AddressTableEntry::Sending)
183 CWalletDB().EraseName(rec->address.toStdString());
184 // Add new entry with new address
185 SetAddressBookName(value.toString().toStdString(), rec->label.toStdString());
187 rec->address = value.toString();
191 emit dataChanged(index, index);
198 QVariant AddressTableModel::headerData(int section, Qt::Orientation orientation, int role) const
200 if(orientation == Qt::Horizontal)
202 if(role == Qt::DisplayRole)
204 return columns[section];
210 QModelIndex AddressTableModel::index(int row, int column, const QModelIndex & parent) const
213 AddressTableEntry *data = priv->index(row);
216 return createIndex(row, column, priv->index(row));
220 return QModelIndex();
224 void AddressTableModel::updateList()
226 // Update internal model from Bitcoin core
228 priv->refreshAddressTable();
232 QString AddressTableModel::addRow(const QString &type, const QString &label, const QString &address)
234 std::string strLabel = label.toStdString();
235 std::string strAddress = address.toStdString();
239 // Check for duplicate
240 CRITICAL_BLOCK(cs_mapAddressBook)
242 if(mapAddressBook.count(strAddress))
248 else if(type == Receive)
250 // Generate a new address to associate with given label
251 strAddress = PubKeyToAddress(GetKeyFromKeyPool());
257 // Add entry and update list
258 SetAddressBookName(strAddress, strLabel);
260 return QString::fromStdString(strAddress);
263 bool AddressTableModel::removeRows(int row, int count, const QModelIndex & parent)
266 AddressTableEntry *rec = priv->index(row);
267 if(count != 1 || !rec || rec->type == AddressTableEntry::Receiving)
269 // Can only remove one row at a time, and cannot remove rows not in model.
270 // Also refuse to remove receiving addresses.
273 CWalletDB().EraseName(rec->address.toStdString());