-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathCoinsWidget.cpp
More file actions
380 lines (308 loc) · 12.1 KB
/
CoinsWidget.cpp
File metadata and controls
380 lines (308 loc) · 12.1 KB
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// SPDX-License-Identifier: BSD-3-Clause
// SPDX-FileCopyrightText: The Monero Project
#include "CoinsWidget.h"
#include "ui_CoinsWidget.h"
#include <QMessageBox>
#include "dialog/OutputInfoDialog.h"
#include "dialog/OutputSweepDialog.h"
#include "utils/Icons.h"
#include "utils/Utils.h"
#ifdef WITH_SCANNER
#include "wizard/offline_tx_signing/OfflineTxSigningWizard.h"
#endif
CoinsWidget::CoinsWidget(Wallet *wallet, QWidget *parent)
: QWidget(parent)
, ui(new Ui::CoinsWidget)
, m_wallet(wallet)
, m_headerMenu(new QMenu(this))
, m_copyMenu(new QMenu("Copy",this))
{
ui->setupUi(this);
// header context menu
ui->coins->header()->setContextMenuPolicy(Qt::CustomContextMenu);
m_showSpentAction = m_headerMenu->addAction("Show spent outputs", this, &CoinsWidget::setShowSpent);
m_showSpentAction->setCheckable(true);
connect(ui->coins->header(), &QHeaderView::customContextMenuRequested, this, &CoinsWidget::showHeaderMenu);
ui->btn_options->setMenu(m_headerMenu);
// copy menu
m_copyMenu->addAction("Public Key", this, [this]{copy(copyField::PubKey);});
m_copyMenu->addAction("Key Image", this, [this]{copy(copyField::KeyImage);});
m_copyMenu->addAction("Transaction ID", this, [this]{copy(copyField::TxID);});
m_copyMenu->addAction("Address", this, [this]{copy(copyField::Address);});
m_copyMenu->addAction("Label", this, [this]{copy(copyField::Label);});
m_copyMenu->addAction("Height", this, [this]{copy(copyField::Height);});
m_copyMenu->addAction("Amount", this, [this]{copy(copyField::Amount);});
// context menu
ui->coins->setContextMenuPolicy(Qt::CustomContextMenu);
m_editLabelAction = new QAction("Edit Label", this);
connect(m_editLabelAction, &QAction::triggered, this, &CoinsWidget::editLabel);
m_thawOutputAction = new QAction("Thaw output", this);
m_freezeOutputAction = new QAction("Freeze output", this);
m_freezeAllSelectedAction = new QAction("Freeze selected", this);
m_thawAllSelectedAction = new QAction("Thaw selected", this);
m_spendAction = new QAction("Spend", this);
m_viewOutputAction = new QAction("Details", this);
m_sweepOutputAction = new QAction("Sweep output", this);
m_sweepOutputsAction = new QAction("Sweep selected outputs", this);
connect(m_freezeOutputAction, &QAction::triggered, this, &CoinsWidget::freezeAllSelected);
connect(m_thawOutputAction, &QAction::triggered, this, &CoinsWidget::thawAllSelected);
connect(m_spendAction, &QAction::triggered, this, &CoinsWidget::spendSelected);
connect(m_viewOutputAction, &QAction::triggered, this, &CoinsWidget::viewOutput);
connect(m_sweepOutputAction, &QAction::triggered, this, &CoinsWidget::onSweepOutputs);
connect(m_sweepOutputsAction, &QAction::triggered, this, &CoinsWidget::onSweepOutputs);
connect(m_freezeAllSelectedAction, &QAction::triggered, this, &CoinsWidget::freezeAllSelected);
connect(m_thawAllSelectedAction, &QAction::triggered, this, &CoinsWidget::thawAllSelected);
connect(ui->coins, &QTreeView::customContextMenuRequested, this, &CoinsWidget::showContextMenu);
connect(ui->coins, &QTreeView::doubleClicked, [this](QModelIndex index){
if (!m_model) return;
if (!(m_model->flags(index) & Qt::ItemIsEditable)) {
this->viewOutput();
}
});
connect(ui->search, &QLineEdit::textChanged, this, &CoinsWidget::setSearchFilter);
connect(m_wallet, &Wallet::selectedInputsChanged, this, &CoinsWidget::selectCoins);
}
void CoinsWidget::setModel(CoinsModel * model, Coins * coins) {
m_coins = coins;
m_model = model;
m_proxyModel = new CoinsProxyModel(this, m_coins);
m_proxyModel->setSourceModel(m_model);
ui->coins->setModel(m_proxyModel);
ui->coins->setColumnHidden(CoinsModel::Spent, true);
ui->coins->setColumnHidden(CoinsModel::SpentHeight, true);
ui->coins->setColumnHidden(CoinsModel::Frozen, true);
if (!m_wallet->viewOnly()) {
ui->coins->setColumnHidden(CoinsModel::KeyImageKnown, true);
} else {
ui->coins->setColumnHidden(CoinsModel::KeyImageKnown, false);
}
ui->coins->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
ui->coins->header()->setSectionResizeMode(CoinsModel::Label, QHeaderView::Stretch);
ui->coins->header()->setSortIndicator(CoinsModel::BlockHeight, Qt::DescendingOrder);
ui->coins->setSortingEnabled(true);
}
void CoinsWidget::setSearchbarVisible(bool visible) {
ui->frame_search->setVisible(visible);
}
void CoinsWidget::focusSearchbar() {
ui->search->setFocusPolicy(Qt::StrongFocus);
ui->search->setFocus();
}
void CoinsWidget::showContextMenu(const QPoint &point) {
QModelIndexList list = ui->coins->selectionModel()->selectedRows();
auto *menu = new QMenu(ui->coins);
if (list.size() > 1) {
menu->addAction(m_spendAction);
menu->addAction(m_freezeAllSelectedAction);
menu->addAction(m_thawAllSelectedAction);
menu->addAction(m_sweepOutputsAction);
}
else {
auto index = this->getCurrentIndex();
if (!index.isValid()) {
return;
}
const CoinsInfo& c = m_model->entryFromIndex(index);
bool isSpent = c.spent;
bool isFrozen = c.frozen;
bool isUnlocked = c.unlocked;
menu->addAction(m_spendAction);
menu->addMenu(m_copyMenu);
menu->addAction(m_editLabelAction);
if (!isSpent) {
isFrozen ? menu->addAction(m_thawOutputAction) : menu->addAction(m_freezeOutputAction);
menu->addAction(m_sweepOutputAction);
if (isFrozen || !isUnlocked) {
m_sweepOutputAction->setDisabled(true);
} else {
m_sweepOutputAction->setEnabled(true);
}
}
menu->addAction(m_viewOutputAction);
}
menu->popup(ui->coins->viewport()->mapToGlobal(point));
}
void CoinsWidget::showHeaderMenu(const QPoint& position)
{
m_headerMenu->popup(QCursor::pos());
}
void CoinsWidget::setShowSpent(bool show)
{
if(!m_proxyModel) return;
m_proxyModel->setShowSpent(show);
}
void CoinsWidget::setSearchFilter(const QString &filter) {
if (!m_proxyModel) return;
m_proxyModel->setSearchFilter(filter);
}
QStringList CoinsWidget::selectedPubkeys() {
QModelIndexList list = ui->coins->selectionModel()->selectedRows();
QStringList pubkeys;
for (QModelIndex index: list) {
pubkeys << m_model->entryFromIndex(m_proxyModel->mapToSource(index)).pubKey;
}
return pubkeys;
}
void CoinsWidget::freezeAllSelected() {
QStringList pubkeys = this->selectedPubkeys();
this->freezeCoins(pubkeys);
}
void CoinsWidget::thawAllSelected() {
QStringList pubkeys = this->selectedPubkeys();
this->thawCoins(pubkeys);
}
void CoinsWidget::spendSelected() {
QModelIndexList selectedRows = ui->coins->selectionModel()->selectedRows();
QStringList keyimages;
for (const auto index : selectedRows) {
if (!index.isValid()) {
return;
}
const CoinsInfo& coin = m_model->entryFromIndex(m_proxyModel->mapToSource(index));
bool spendable = this->isCoinSpendable(coin);
if (!spendable) return;
QString keyImage = coin.keyImage;
keyimages << keyImage;
}
m_wallet->setSelectedInputs(keyimages);
this->selectCoins(keyimages);
}
void CoinsWidget::viewOutput() {
auto index = this->getCurrentIndex();
if (!index.isValid()) {
return;
}
const CoinsInfo& c = m_model->entryFromIndex(index);
auto * dialog = new OutputInfoDialog(c, this);
dialog->show();
}
void CoinsWidget::onSweepOutputs() {
if (!m_wallet->isConnected()) {
Utils::showError(this, "Unable to create transaction", "Wallet is not connected to a node.",
{"Wait for the wallet to automatically connect to a node.", "Go to File -> Settings -> Network -> Node to manually connect to a node."},
"nodes");
return;
}
if (!m_wallet->isSynchronized()) {
Utils::showError(this, "Unable to create transaction", "Wallet is not synchronized", {"Wait for wallet synchronization to complete"}, "synchronization");
return;
}
QModelIndexList selectedRows = ui->coins->selectionModel()->selectedRows();
QVector<QString> keyImages;
quint64 totalAmount = 0;
for (const auto index : selectedRows) {
if (!index.isValid()) {
return;
}
const CoinsInfo& coin = m_model->entryFromIndex(m_proxyModel->mapToSource(index));
bool spendable = this->isCoinSpendable(coin);
if (!spendable) return;
QString keyImage = coin.keyImage;
keyImages.push_back(keyImage);
totalAmount += coin.amount;
}
OutputSweepDialog dialog{this, totalAmount};
int ret = dialog.exec();
if (!ret) return;
if (m_wallet->keyImageSyncNeeded(totalAmount, false)) {
#if defined(WITH_SCANNER)
OfflineTxSigningWizard wizard(this, m_wallet);
auto r = wizard.exec();
if (r == QDialog::Rejected) {
return;
}
#else
Utils::showError(this, "Can't open offline transaction signing wizard", "Feather was built without webcam QR scanner support");
return;
#endif
}
QString address = dialog.address();
bool churn = dialog.churn();
int outputs = dialog.outputs();
QtFuture::connect(m_wallet, &Wallet::preTransactionChecksComplete)
.then([this, keyImages, address, churn, outputs](int feeLevel){
m_wallet->sweepOutputs(keyImages, address, churn, outputs, feeLevel);
});
m_wallet->preTransactionChecks(dialog.feeLevel());
}
void CoinsWidget::copy(copyField field) {
auto index = this->getCurrentIndex();
if (!index.isValid()) {
return;
}
const CoinsInfo& c = m_model->entryFromIndex(index);
QString data;
switch (field) {
case PubKey:
data = c.pubKey;
break;
case KeyImage:
data = c.keyImage;
break;
case TxID:
data = c.hash;
break;
case Address:
data = c.address;
break;
case Label: {
if (!c.description.isEmpty())
data = c.description;
else
data = c.getAddressLabel();
break;
}
case Height:
data = QString::number(c.blockHeight);
break;
case Amount:
data = c.displayAmount();
break;
}
Utils::copyToClipboard(data);
}
void CoinsWidget::freezeCoins(QStringList &pubkeys) {
m_wallet->coins()->freeze(pubkeys);
m_wallet->updateBalance();
}
void CoinsWidget::thawCoins(QStringList &pubkeys) {
m_wallet->coins()->thaw(pubkeys);
m_wallet->updateBalance();
}
void CoinsWidget::selectCoins(const QStringList &keyimages) {
m_model->setSelected(keyimages);
ui->coins->clearSelection();
}
void CoinsWidget::editLabel() {
QModelIndex index = ui->coins->currentIndex().siblingAtColumn(m_model->ModelColumn::Label);
ui->coins->setCurrentIndex(index);
ui->coins->edit(index);
}
bool CoinsWidget::isCoinSpendable(const CoinsInfo &coin) {
if (!coin.keyImageKnown) {
Utils::showError(this, "Unable to spend outputs", "Selected output has unknown key image");
return false;
}
if (coin.spent) {
Utils::showError(this, "Unable to spend outputs", "Selected output was already spent");
return false;
}
if (coin.frozen) {
Utils::showError(this, "Unable to spend outputs", "Selected output is frozen", {"Thaw the selected output(s) before spending"}, "freeze_thaw_outputs");
return false;
}
if (!coin.unlocked) {
Utils::showError(this, "Unable to spend outputs", "Selected output is locked", {"Wait until the output has reached the required number of confirmation before spending."});
return false;
}
return true;
}
QModelIndex CoinsWidget::getCurrentIndex()
{
QModelIndexList list = ui->coins->selectionModel()->selectedRows();
if (list.length() < 1) {
return {};
}
return m_proxyModel->mapToSource(list.first());
}
CoinsWidget::~CoinsWidget() = default;