aboutsummaryrefslogtreecommitdiff
path: root/src/qt/addresstablemodel.cpp
blob: 25500764086fe1476a95ba7ac20b1e96ec0c0aa1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include "addresstablemodel.h"
#include "guiutil.h"

#include "headers.h"

#include <QFont>
#include <QColor>

const QString AddressTableModel::Send = "S";
const QString AddressTableModel::Receive = "R";

struct AddressTableEntry
{
    enum Type {
        Sending,
        Receiving
    };

    Type type;
    QString label;
    QString address;

    AddressTableEntry() {}
    AddressTableEntry(Type type, const QString &label, const QString &address):
        type(type), label(label), address(address) {}
};

// Private implementation
struct AddressTablePriv
{
    CWallet *wallet;
    QList<AddressTableEntry> cachedAddressTable;

    AddressTablePriv(CWallet *wallet):
            wallet(wallet) {}

    void refreshAddressTable()
    {
        cachedAddressTable.clear();

        CRITICAL_BLOCK(wallet->cs_mapKeys)
        CRITICAL_BLOCK(wallet->cs_mapAddressBook)
        {
            BOOST_FOREACH(const PAIRTYPE(std::string, std::string)& item, wallet->mapAddressBook)
            {
                std::string strAddress = item.first;
                std::string strName = item.second;
                uint160 hash160;
                bool fMine = (AddressToHash160(strAddress, hash160) && mapPubKeys.count(hash160));
                cachedAddressTable.append(AddressTableEntry(fMine ? AddressTableEntry::Receiving : AddressTableEntry::Sending,
                                  QString::fromStdString(strName),
                                  QString::fromStdString(strAddress)));
            }
        }
    }

    int size()
    {
        return cachedAddressTable.size();
    }

    AddressTableEntry *index(int idx)
    {
        if(idx >= 0 && idx < cachedAddressTable.size())
        {
            return &cachedAddressTable[idx];
        }
        else
        {
            return 0;
        }
    }

    bool isDefaultAddress(const AddressTableEntry *rec)
    {
        return rec->address == QString::fromStdString(wallet->GetDefaultAddress());
    }
};

AddressTableModel::AddressTableModel(CWallet *wallet, QObject *parent) :
    QAbstractTableModel(parent),wallet(wallet),priv(0)
{
    columns << tr("Label") << tr("Address");
    priv = new AddressTablePriv(wallet);
    priv->refreshAddressTable();
}

AddressTableModel::~AddressTableModel()
{
    delete priv;
}

int AddressTableModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return priv->size();
}

int AddressTableModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return columns.length();
}

QVariant AddressTableModel::data(const QModelIndex &index, int role) const
{
    if(!index.isValid())
        return QVariant();

    AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());

    if(role == Qt::DisplayRole || role == Qt::EditRole)
    {
        switch(index.column())
        {
        case Label:
            if(rec->label.isEmpty())
            {
                return tr("(no label)");
            }
            else
            {
                return rec->label;
            }
        case Address:
            return rec->address;
        case IsDefaultAddress:
            return priv->isDefaultAddress(rec);
        }
    }
    else if (role == Qt::FontRole)
    {
        QFont font;
        if(index.column() == Address)
        {
            font = GUIUtil::bitcoinAddressFont();
        }
        if(priv->isDefaultAddress(rec))
        {
            font.setBold(true);
        }
        return font;
    }
    else if (role == Qt::BackgroundRole)
    {
        // Show default address in alternative color
        if(priv->isDefaultAddress(rec))
        {
            return QColor(255,255,128);
        }
    }
    else if (role == Qt::ToolTipRole)
    {
        if(priv->isDefaultAddress(rec))
        {
            return tr("Default receiving address");
        }
    }
    else if (role == TypeRole)
    {
        switch(rec->type)
        {
        case AddressTableEntry::Sending:
            return Send;
        case AddressTableEntry::Receiving:
            return Receive;
        default: break;
        }
    }
    return QVariant();
}

bool AddressTableModel::setData(const QModelIndex & index, const QVariant & value, int role)
{
    if(!index.isValid())
        return false;
    AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());

    if(role == Qt::EditRole)
    {
        switch(index.column())
        {
        case Label:
            wallet->SetAddressBookName(rec->address.toStdString(), value.toString().toStdString());
            rec->label = value.toString();
            break;
        case Address:
            // Double-check that we're not overwriting receiving address
            if(rec->type == AddressTableEntry::Sending)
            {
                // Remove old entry
                wallet->EraseAddressBookName(rec->address.toStdString());
                // Add new entry with new address
                wallet->SetAddressBookName(value.toString().toStdString(), rec->label.toStdString());

                rec->address = value.toString();
            }
            break;
        case IsDefaultAddress:
            if(value.toBool())
            {
                setDefaultAddress(rec->address);
            }
            break;
        }
        emit dataChanged(index, index);

        return true;
    }
    return false;
}

QVariant AddressTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if(orientation == Qt::Horizontal)
    {
        if(role == Qt::DisplayRole)
        {
            return columns[section];
        }
    }
    return QVariant();
}

QModelIndex AddressTableModel::index(int row, int column, const QModelIndex & parent) const
{
    Q_UNUSED(parent);
    AddressTableEntry *data = priv->index(row);
    if(data)
    {
        return createIndex(row, column, priv->index(row));
    }
    else
    {
        return QModelIndex();
    }
}

void AddressTableModel::updateList()
{
    // Update internal model from Bitcoin core
    beginResetModel();
    priv->refreshAddressTable();
    endResetModel();
}

QString AddressTableModel::addRow(const QString &type, const QString &label, const QString &address, bool setAsDefault)
{
    std::string strLabel = label.toStdString();
    std::string strAddress = address.toStdString();

    if(type == Send)
    {
        // Check for duplicate
        CRITICAL_BLOCK(wallet->cs_mapAddressBook)
        {
            if(wallet->mapAddressBook.count(strAddress))
            {
                return QString();
            }
        }
    }
    else if(type == Receive)
    {
        // Generate a new address to associate with given label, optionally
        // set as default receiving address.
        strAddress = PubKeyToAddress(wallet->GetKeyFromKeyPool());
        if(setAsDefault)
        {
            setDefaultAddress(QString::fromStdString(strAddress));
        }
    }
    else
    {
        return QString();
    }
    // Add entry and update list
    wallet->SetAddressBookName(strAddress, strLabel);
    updateList();
    return QString::fromStdString(strAddress);
}

bool AddressTableModel::removeRows(int row, int count, const QModelIndex & parent)
{
    Q_UNUSED(parent);
    AddressTableEntry *rec = priv->index(row);
    if(count != 1 || !rec || rec->type == AddressTableEntry::Receiving)
    {
        // Can only remove one row at a time, and cannot remove rows not in model.
        // Also refuse to remove receiving addresses.
        return false;
    }
    wallet->EraseAddressBookName(rec->address.toStdString());
    updateList();
    return true;
}

QString AddressTableModel::getDefaultAddress() const
{
    return QString::fromStdString(wallet->GetDefaultAddress());
}

void AddressTableModel::setDefaultAddress(const QString &defaultAddress)
{
    wallet->SetDefaultAddress(defaultAddress.toStdString());
}

void AddressTableModel::update()
{
    emit defaultAddressChanged(getDefaultAddress());
}