-
Notifications
You must be signed in to change notification settings - Fork 95
/
addressfield.cpp
186 lines (159 loc) · 5.21 KB
/
addressfield.cpp
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
// Copyright (c) 2016-2017 The Qtum Core developers
// Copyright (c) 2018 The Luxcore developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "addressfield.h"
#include "wallet.h"
//#include "validation.h"
#include "base58.h"
#include "qvalidatedlineedit.h"
#include "bitcoinaddressvalidator.h"
#include "../main.h"
#include <QLineEdit>
#include <QCompleter>
using namespace std;
AddressField::AddressField(QWidget *parent) :
QComboBox(parent),
m_addressType(AddressField::UTXO),
m_addressTableModel(0),
m_addressColumn(0),
m_typeRole(Qt::UserRole),
m_receive("R")
{
// Set editable state
setComboBoxEditable(false);
// Connect signals and slots
connect(this, SIGNAL(addressTypeChanged(AddressType)), SLOT(on_addressTypeChanged()));
}
QString AddressField::currentText() const
{
if(isEditable())
{
return lineEdit()->text();
}
int index = currentIndex();
if(index == -1)
{
return QString();
}
return itemText(index);
}
bool AddressField::isValidAddress()
{
if(!isEditable())
{
if(currentIndex() != -1)
return true;
else
return false;
}
((QValidatedLineEdit*)lineEdit())->checkValidity();
return ((QValidatedLineEdit*)lineEdit())->isValid();
}
void AddressField::setComboBoxEditable(bool editable)
{
QValidatedLineEdit *validatedLineEdit = new QValidatedLineEdit(this);
setLineEdit(validatedLineEdit);
setEditable(editable);
if(editable)
{
QValidatedLineEdit *validatedLineEdit = (QValidatedLineEdit*)lineEdit();
validatedLineEdit->setCheckValidator(new BitcoinAddressCheckValidator(parent()));
completer()->setCompletionMode(QCompleter::InlineCompletion);
connect(validatedLineEdit, SIGNAL(editingFinished()), this, SLOT(on_editingFinished()));
}
}
void AddressField::on_refresh()
{
// Initialize variables
QString currentAddress = currentText();
m_stringList.clear();
if (GetTime() < lastRefreshTime + 30) return; //only do this once every 30 secs
lastRefreshTime = GetTime();
vector<COutput> vecOutputs;
assert(pwalletMain != NULL);
// Fill the list with address
if(m_addressType == AddressField::UTXO)
{
// Fill the list with UTXO
LOCK2(cs_main, pwalletMain->cs_wallet);
// Add all available addresses if 0 address ballance for token is enabled
if(m_addressTableModel)
{
// Fill the list with user defined address
for(int row = 0; row < m_addressTableModel->rowCount(); row++)
{
QModelIndex index = m_addressTableModel->index(row, m_addressColumn);
QString strAddress = m_addressTableModel->data(index).toString();
QString type = m_addressTableModel->data(index, m_typeRole).toString();
if(type == m_receive)
{
appendAddress(strAddress);
}
}
// Include zero or unconfirmed coins too
pwalletMain->AvailableCoins(vecOutputs, false, NULL, true);
}
else
{
// List only the spendable coins
pwalletMain->AvailableCoins(vecOutputs);
}
for (const COutput& out : vecOutputs) {
CTxDestination address;
const CScript& scriptPubKey = out.tx->vout[out.i].scriptPubKey;
bool fValidAddress = ExtractDestination(scriptPubKey, address);
if (fValidAddress)
{
QString strAddress = QString::fromStdString(EncodeDestination(address));
appendAddress(strAddress);
}
}
}
// Update the current index
int index = m_stringList.indexOf(currentAddress);
m_stringModel.setStringList(m_stringList);
setModel(&m_stringModel);
setCurrentIndex(index);
}
void AddressField::on_addressTypeChanged()
{
m_stringList.clear();
on_refresh();
}
void AddressField::on_editingFinished()
{
Q_EMIT editTextChanged(QComboBox::currentText());
}
void AddressField::appendAddress(const QString &strAddress)
{
CTxDestination dest = DecodeDestination(strAddress.toStdString());
if(!m_stringList.contains(strAddress) && IsMine(*pwalletMain, dest))
{
m_stringList.append(strAddress);
}
}
void AddressField::setReceive(const QString &receive)
{
m_receive = receive;
}
void AddressField::setTypeRole(int typeRole)
{
m_typeRole = typeRole;
}
void AddressField::setAddressColumn(int addressColumn)
{
m_addressColumn = addressColumn;
}
void AddressField::setAddressTableModel(QAbstractItemModel *addressTableModel)
{
if(m_addressTableModel)
{
disconnect(m_addressTableModel, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(on_refresh()));
disconnect(m_addressTableModel, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(on_refresh()));
}
m_addressTableModel = addressTableModel;
connect(m_addressTableModel, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(on_refresh()));
connect(m_addressTableModel, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(on_refresh()));
on_refresh();
}