0558bfa45356838a2c26b9ffda50b56632b508a8
[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         }
121     }
122     else if (role == Qt::FontRole)
123     {
124         QFont font;
125         if(index.column() == Address)
126         {
127             font = GUIUtil::bitcoinAddressFont();
128         }
129         if(rec->isDefaultAddress())
130         {
131             font.setBold(true);
132         }
133         return font;
134     }
135     else if (role == Qt::ForegroundRole)
136     {
137         // Show default address in alternative color
138         if(rec->isDefaultAddress())
139         {
140             return QColor(0,0,255);
141         }
142     }
143     else if (role == Qt::ToolTipRole)
144     {
145         if(rec->isDefaultAddress())
146         {
147             return tr("Default receiving address");
148         }
149     }
150     else if (role == TypeRole)
151     {
152         switch(rec->type)
153         {
154         case AddressTableEntry::Sending:
155             return Send;
156         case AddressTableEntry::Receiving:
157             return Receive;
158         default: break;
159         }
160     }
161     return QVariant();
162 }
163
164 bool AddressTableModel::setData(const QModelIndex & index, const QVariant & value, int role)
165 {
166     if(!index.isValid())
167         return false;
168     AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
169
170     if(role == Qt::EditRole)
171     {
172         switch(index.column())
173         {
174         case Label:
175             SetAddressBookName(rec->address.toStdString(), value.toString().toStdString());
176             rec->label = value.toString();
177             break;
178         case Address:
179             // Double-check that we're not overwriting receiving address
180             if(rec->type == AddressTableEntry::Sending)
181             {
182                 // Remove old entry
183                 CWalletDB().EraseName(rec->address.toStdString());
184                 // Add new entry with new address
185                 SetAddressBookName(value.toString().toStdString(), rec->label.toStdString());
186
187                 rec->address = value.toString();
188             }
189             break;
190         }
191         emit dataChanged(index, index);
192
193         return true;
194     }
195     return false;
196 }
197
198 QVariant AddressTableModel::headerData(int section, Qt::Orientation orientation, int role) const
199 {
200     if(orientation == Qt::Horizontal)
201     {
202         if(role == Qt::DisplayRole)
203         {
204             return columns[section];
205         }
206     }
207     return QVariant();
208 }
209
210 QModelIndex AddressTableModel::index(int row, int column, const QModelIndex & parent) const
211 {
212     Q_UNUSED(parent);
213     AddressTableEntry *data = priv->index(row);
214     if(data)
215     {
216         return createIndex(row, column, priv->index(row));
217     }
218     else
219     {
220         return QModelIndex();
221     }
222 }
223
224 void AddressTableModel::updateList()
225 {
226     // Update internal model from Bitcoin core
227     beginResetModel();
228     priv->refreshAddressTable();
229     endResetModel();
230 }
231
232 QString AddressTableModel::addRow(const QString &type, const QString &label, const QString &address)
233 {
234     std::string strLabel = label.toStdString();
235     std::string strAddress = address.toStdString();
236
237     if(type == Send)
238     {
239         // Check for duplicate
240         CRITICAL_BLOCK(cs_mapAddressBook)
241         {
242             if(mapAddressBook.count(strAddress))
243             {
244                 return QString();
245             }
246         }
247     }
248     else if(type == Receive)
249     {
250         // Generate a new address to associate with given label
251         strAddress = PubKeyToAddress(GetKeyFromKeyPool());
252     }
253     else
254     {
255         return QString();
256     }
257     // Add entry and update list
258     SetAddressBookName(strAddress, strLabel);
259     updateList();
260     return QString::fromStdString(strAddress);
261 }
262
263 bool AddressTableModel::removeRows(int row, int count, const QModelIndex & parent)
264 {
265     Q_UNUSED(parent);
266     AddressTableEntry *rec = priv->index(row);
267     if(count != 1 || !rec || rec->type == AddressTableEntry::Receiving)
268     {
269         // Can only remove one row at a time, and cannot remove rows not in model.
270         // Also refuse to remove receiving addresses.
271         return false;
272     }
273     CWalletDB().EraseName(rec->address.toStdString());
274     updateList();
275     return true;
276 }