update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
[novacoin.git] / src / qt / addresstablemodel.cpp
1 #include "addresstablemodel.h"
2 #include "guiutil.h"
3
4 #include "headers.h"
5
6 #include <QFont>
7 #include <QColor>
8
9 const QString AddressTableModel::Send = "S";
10 const QString AddressTableModel::Receive = "R";
11
12 struct AddressTableEntry
13 {
14     enum Type {
15         Sending,
16         Receiving
17     };
18
19     Type type;
20     QString label;
21     QString address;
22
23     AddressTableEntry() {}
24     AddressTableEntry(Type type, const QString &label, const QString &address):
25         type(type), label(label), address(address) {}
26 };
27
28 // Private implementation
29 struct AddressTablePriv
30 {
31     CWallet *wallet;
32     QList<AddressTableEntry> cachedAddressTable;
33
34     AddressTablePriv(CWallet *wallet):
35             wallet(wallet) {}
36
37     void refreshAddressTable()
38     {
39         cachedAddressTable.clear();
40
41         CRITICAL_BLOCK(wallet->cs_mapKeys)
42         CRITICAL_BLOCK(wallet->cs_mapAddressBook)
43         {
44             BOOST_FOREACH(const PAIRTYPE(std::string, std::string)& item, wallet->mapAddressBook)
45             {
46                 std::string strAddress = item.first;
47                 std::string strName = item.second;
48                 uint160 hash160;
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)));
53             }
54         }
55     }
56
57     int size()
58     {
59         return cachedAddressTable.size();
60     }
61
62     AddressTableEntry *index(int idx)
63     {
64         if(idx >= 0 && idx < cachedAddressTable.size())
65         {
66             return &cachedAddressTable[idx];
67         }
68         else
69         {
70             return 0;
71         }
72     }
73
74     bool isDefaultAddress(const AddressTableEntry *rec)
75     {
76         return rec->address == QString::fromStdString(wallet->GetDefaultAddress());
77     }
78 };
79
80 AddressTableModel::AddressTableModel(CWallet *wallet, QObject *parent) :
81     QAbstractTableModel(parent),wallet(wallet),priv(0)
82 {
83     columns << tr("Label") << tr("Address");
84     priv = new AddressTablePriv(wallet);
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 priv->isDefaultAddress(rec);
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(priv->isDefaultAddress(rec))
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(priv->isDefaultAddress(rec))
141         {
142             return QColor(0,0,255);
143         }
144     }
145     else if (role == Qt::ToolTipRole)
146     {
147         if(priv->isDefaultAddress(rec))
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             wallet->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                 wallet->EraseAddressBookName(rec->address.toStdString());
186                 // Add new entry with new address
187                 wallet->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(wallet->cs_mapAddressBook)
249         {
250             if(wallet->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(wallet->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     wallet->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     wallet->EraseAddressBookName(rec->address.toStdString());
287     updateList();
288     return true;
289 }
290
291 QString AddressTableModel::getDefaultAddress() const
292 {
293     return QString::fromStdString(wallet->GetDefaultAddress());
294 }
295
296 void AddressTableModel::setDefaultAddress(const QString &defaultAddress)
297 {
298     wallet->SetDefaultAddress(defaultAddress.toStdString());
299 }
300
301 void AddressTableModel::update()
302 {
303     emit defaultAddressChanged(getDefaultAddress());
304 }