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())
120 case IsDefaultAddress:
121 return rec->isDefaultAddress();
124 else if (role == Qt::FontRole)
127 if(index.column() == Address)
129 font = GUIUtil::bitcoinAddressFont();
131 if(rec->isDefaultAddress())
137 else if (role == Qt::ForegroundRole)
139 // Show default address in alternative color
140 if(rec->isDefaultAddress())
142 return QColor(0,0,255);
145 else if (role == Qt::ToolTipRole)
147 if(rec->isDefaultAddress())
149 return tr("Default receiving address");
152 else if (role == TypeRole)
156 case AddressTableEntry::Sending:
158 case AddressTableEntry::Receiving:
166 bool AddressTableModel::setData(const QModelIndex & index, const QVariant & value, int role)
170 AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
172 if(role == Qt::EditRole)
174 switch(index.column())
177 SetAddressBookName(rec->address.toStdString(), value.toString().toStdString());
178 rec->label = value.toString();
181 // Double-check that we're not overwriting receiving address
182 if(rec->type == AddressTableEntry::Sending)
185 CWalletDB().EraseName(rec->address.toStdString());
186 // Add new entry with new address
187 SetAddressBookName(value.toString().toStdString(), rec->label.toStdString());
189 rec->address = value.toString();
192 case IsDefaultAddress:
195 setDefaultAddress(rec->address);
199 emit dataChanged(index, index);
206 QVariant AddressTableModel::headerData(int section, Qt::Orientation orientation, int role) const
208 if(orientation == Qt::Horizontal)
210 if(role == Qt::DisplayRole)
212 return columns[section];
218 QModelIndex AddressTableModel::index(int row, int column, const QModelIndex & parent) const
221 AddressTableEntry *data = priv->index(row);
224 return createIndex(row, column, priv->index(row));
228 return QModelIndex();
232 void AddressTableModel::updateList()
234 // Update internal model from Bitcoin core
236 priv->refreshAddressTable();
240 QString AddressTableModel::addRow(const QString &type, const QString &label, const QString &address, bool setAsDefault)
242 std::string strLabel = label.toStdString();
243 std::string strAddress = address.toStdString();
247 // Check for duplicate
248 CRITICAL_BLOCK(cs_mapAddressBook)
250 if(mapAddressBook.count(strAddress))
256 else if(type == Receive)
258 // Generate a new address to associate with given label, optionally
259 // set as default receiving address.
260 strAddress = PubKeyToAddress(GetKeyFromKeyPool());
263 setDefaultAddress(QString::fromStdString(strAddress));
270 // Add entry and update list
271 SetAddressBookName(strAddress, strLabel);
273 return QString::fromStdString(strAddress);
276 bool AddressTableModel::removeRows(int row, int count, const QModelIndex & parent)
279 AddressTableEntry *rec = priv->index(row);
280 if(count != 1 || !rec || rec->type == AddressTableEntry::Receiving)
282 // Can only remove one row at a time, and cannot remove rows not in model.
283 // Also refuse to remove receiving addresses.
286 CWalletDB().EraseName(rec->address.toStdString());
291 QString AddressTableModel::getDefaultAddress() const
293 std::vector<unsigned char> vchPubKey;
294 if (CWalletDB("r").ReadDefaultKey(vchPubKey))
296 return QString::fromStdString(PubKeyToAddress(vchPubKey));
304 void AddressTableModel::setDefaultAddress(const QString &defaultAddress)
307 std::string strAddress = defaultAddress.toStdString();
308 if (!AddressToHash160(strAddress, hash160))
310 if (!mapPubKeys.count(hash160))
312 CWalletDB().WriteDefaultKey(mapPubKeys[hash160]);
315 void AddressTableModel::update()
317 emit defaultAddressChanged(getDefaultAddress());