1cd82b76265da07eeea7a497acfa99846c9179b5
[novacoin.git] / src / qt / addresstablemodel.cpp
1 #include "addresstablemodel.h"
2 #include "guiutil.h"
3 #include "main.h"
4
5 #include <QFont>
6 #include <QColor>
7
8 const QString AddressTableModel::Send = "S";
9 const QString AddressTableModel::Receive = "R";
10
11 struct AddressTableEntry
12 {
13     enum Type {
14         Sending,
15         Receiving
16     };
17
18     Type type;
19     QString label;
20     QString address;
21
22     AddressTableEntry() {}
23     AddressTableEntry(Type type, const QString &label, const QString &address):
24         type(type), label(label), address(address) {}
25
26     bool isDefaultAddress() const
27     {
28         std::vector<unsigned char> vchPubKey;
29         if (CWalletDB("r").ReadDefaultKey(vchPubKey))
30         {
31             return address == QString::fromStdString(PubKeyToAddress(vchPubKey));
32         }
33         return false;
34     }
35 };
36
37 // Private implementation
38 struct AddressTablePriv
39 {
40     QList<AddressTableEntry> cachedAddressTable;
41
42     void refreshAddressTable()
43     {
44         cachedAddressTable.clear();
45
46         CRITICAL_BLOCK(cs_mapKeys)
47         CRITICAL_BLOCK(cs_mapAddressBook)
48         {
49             BOOST_FOREACH(const PAIRTYPE(std::string, std::string)& item, mapAddressBook)
50             {
51                 std::string strAddress = item.first;
52                 std::string strName = item.second;
53                 uint160 hash160;
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)));
58             }
59         }
60     }
61
62     int size()
63     {
64         return cachedAddressTable.size();
65     }
66
67     AddressTableEntry *index(int idx)
68     {
69         if(idx >= 0 && idx < cachedAddressTable.size())
70         {
71             return &cachedAddressTable[idx];
72         }
73         else
74         {
75             return 0;
76         }
77     }
78 };
79
80 AddressTableModel::AddressTableModel(QObject *parent) :
81     QAbstractTableModel(parent),priv(0)
82 {
83     columns << tr("Label") << tr("Address");
84     priv = new AddressTablePriv();
85     priv->refreshAddressTable();
86 }
87
88 AddressTableModel::~AddressTableModel()
89 {
90     delete priv;
91 }
92
93 int AddressTableModel::rowCount(const QModelIndex &parent) const
94 {
95     Q_UNUSED(parent);
96     return priv->size();
97 }
98
99 int AddressTableModel::columnCount(const QModelIndex &parent) const
100 {
101     Q_UNUSED(parent);
102     return columns.length();
103 }
104
105 QVariant AddressTableModel::data(const QModelIndex &index, int role) const
106 {
107     if(!index.isValid())
108         return QVariant();
109
110     AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
111
112     if(role == Qt::DisplayRole || role == Qt::EditRole)
113     {
114         switch(index.column())
115         {
116         case Label:
117             return rec->label;
118         case Address:
119             return rec->address;
120         case IsDefaultAddress:
121             return rec->isDefaultAddress();
122         }
123     }
124     else if (role == Qt::FontRole)
125     {
126         QFont font;
127         if(index.column() == Address)
128         {
129             font = GUIUtil::bitcoinAddressFont();
130         }
131         if(rec->isDefaultAddress())
132         {
133             font.setBold(true);
134         }
135         return font;
136     }
137     else if (role == Qt::ForegroundRole)
138     {
139         // Show default address in alternative color
140         if(rec->isDefaultAddress())
141         {
142             return QColor(0,0,255);
143         }
144     }
145     else if (role == Qt::ToolTipRole)
146     {
147         if(rec->isDefaultAddress())
148         {
149             return tr("Default receiving address");
150         }
151     }
152     else if (role == TypeRole)
153     {
154         switch(rec->type)
155         {
156         case AddressTableEntry::Sending:
157             return Send;
158         case AddressTableEntry::Receiving:
159             return Receive;
160         default: break;
161         }
162     }
163     return QVariant();
164 }
165
166 bool AddressTableModel::setData(const QModelIndex & index, const QVariant & value, int role)
167 {
168     if(!index.isValid())
169         return false;
170     AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
171
172     if(role == Qt::EditRole)
173     {
174         switch(index.column())
175         {
176         case Label:
177             SetAddressBookName(rec->address.toStdString(), value.toString().toStdString());
178             rec->label = value.toString();
179             break;
180         case Address:
181             // Double-check that we're not overwriting receiving address
182             if(rec->type == AddressTableEntry::Sending)
183             {
184                 // Remove old entry
185                 CWalletDB().EraseName(rec->address.toStdString());
186                 // Add new entry with new address
187                 SetAddressBookName(value.toString().toStdString(), rec->label.toStdString());
188
189                 rec->address = value.toString();
190             }
191             break;
192         case IsDefaultAddress:
193             if(value.toBool())
194             {
195                 setDefaultAddress(rec->address);
196             }
197             break;
198         }
199         emit dataChanged(index, index);
200
201         return true;
202     }
203     return false;
204 }
205
206 QVariant AddressTableModel::headerData(int section, Qt::Orientation orientation, int role) const
207 {
208     if(orientation == Qt::Horizontal)
209     {
210         if(role == Qt::DisplayRole)
211         {
212             return columns[section];
213         }
214     }
215     return QVariant();
216 }
217
218 QModelIndex AddressTableModel::index(int row, int column, const QModelIndex & parent) const
219 {
220     Q_UNUSED(parent);
221     AddressTableEntry *data = priv->index(row);
222     if(data)
223     {
224         return createIndex(row, column, priv->index(row));
225     }
226     else
227     {
228         return QModelIndex();
229     }
230 }
231
232 void AddressTableModel::updateList()
233 {
234     // Update internal model from Bitcoin core
235     beginResetModel();
236     priv->refreshAddressTable();
237     endResetModel();
238 }
239
240 QString AddressTableModel::addRow(const QString &type, const QString &label, const QString &address, bool setAsDefault)
241 {
242     std::string strLabel = label.toStdString();
243     std::string strAddress = address.toStdString();
244
245     if(type == Send)
246     {
247         // Check for duplicate
248         CRITICAL_BLOCK(cs_mapAddressBook)
249         {
250             if(mapAddressBook.count(strAddress))
251             {
252                 return QString();
253             }
254         }
255     }
256     else if(type == Receive)
257     {
258         // Generate a new address to associate with given label, optionally
259         // set as default receiving address.
260         strAddress = PubKeyToAddress(GetKeyFromKeyPool());
261         if(setAsDefault)
262         {
263             setDefaultAddress(QString::fromStdString(strAddress));
264         }
265     }
266     else
267     {
268         return QString();
269     }
270     // Add entry and update list
271     SetAddressBookName(strAddress, strLabel);
272     updateList();
273     return QString::fromStdString(strAddress);
274 }
275
276 bool AddressTableModel::removeRows(int row, int count, const QModelIndex & parent)
277 {
278     Q_UNUSED(parent);
279     AddressTableEntry *rec = priv->index(row);
280     if(count != 1 || !rec || rec->type == AddressTableEntry::Receiving)
281     {
282         // Can only remove one row at a time, and cannot remove rows not in model.
283         // Also refuse to remove receiving addresses.
284         return false;
285     }
286     CWalletDB().EraseName(rec->address.toStdString());
287     updateList();
288     return true;
289 }
290
291 QString AddressTableModel::getDefaultAddress() const
292 {
293     std::vector<unsigned char> vchPubKey;
294     if (CWalletDB("r").ReadDefaultKey(vchPubKey))
295     {
296         return QString::fromStdString(PubKeyToAddress(vchPubKey));
297     }
298     else
299     {
300         return QString();
301     }
302 }
303
304 void AddressTableModel::setDefaultAddress(const QString &defaultAddress)
305 {
306     uint160 hash160;
307     std::string strAddress = defaultAddress.toStdString();
308     if (!AddressToHash160(strAddress, hash160))
309         return;
310     if (!mapPubKeys.count(hash160))
311         return;
312     CWalletDB().WriteDefaultKey(mapPubKeys[hash160]);
313 }
314
315 void AddressTableModel::update()
316 {
317     emit defaultAddressChanged(getDefaultAddress());
318 }