Skip to content

Commit 01237b0

Browse files
committed
[GUI] Translator class abstracted to be able to reuse the ProcessSendCoinsReturn method in the masternodeswizard class.
[Utils] ProcessSendCoinsReturn, requestUnlock moved inside the the !fPrepare block.
1 parent fc9946c commit 01237b0

File tree

7 files changed

+80
-122
lines changed

7 files changed

+80
-122
lines changed

src/qt/pivx/coldstakingwidget.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ void ColdStakingWidget::onSendClicked(){
450450
WalletModel::SendCoinsReturn prepareStatus = walletModel->prepareTransaction(currentTransaction, CoinControlDialog::coinControl);
451451

452452
// process prepareStatus and on error generate message shown to user
453-
GuiTransactionsUtils::ProcessSendCoinsReturn(
453+
GuiTransactionsUtils::ProcessSendCoinsReturnAndInform(
454454
this,
455455
prepareStatus,
456456
walletModel,
@@ -474,7 +474,7 @@ void ColdStakingWidget::onSendClicked(){
474474
// now send the prepared transaction
475475
WalletModel::SendCoinsReturn sendStatus = dialog->getStatus();
476476
// process sendStatus and on error generate message shown to user
477-
GuiTransactionsUtils::ProcessSendCoinsReturn(
477+
GuiTransactionsUtils::ProcessSendCoinsReturnAndInform(
478478
this,
479479
sendStatus,
480480
walletModel

src/qt/pivx/guitransactionsutils.cpp

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,77 +7,74 @@
77
#include "optionsmodel.h"
88

99
namespace GuiTransactionsUtils {
10-
void ProcessSendCoinsReturn(PWidget *parent, const WalletModel::SendCoinsReturn &sendCoinsReturn,
11-
WalletModel *walletModel, const QString &msgArg, bool fPrepare) {
12-
bool fAskForUnlock = false;
13-
14-
QPair<QString, CClientUIInterface::MessageBoxFlags> msgParams;
15-
// Default to a warning message, override if error message is needed
16-
msgParams.second = CClientUIInterface::MSG_WARNING;
1710

11+
QString ProcessSendCoinsReturn(PWidget::Translator *parent, const WalletModel::SendCoinsReturn &sendCoinsReturn,
12+
WalletModel *walletModel, CClientUIInterface::MessageBoxFlags& informType, const QString &msgArg,
13+
bool fPrepare) {
14+
QString retStr;
15+
informType = CClientUIInterface::MSG_WARNING;
1816
// This comment is specific to SendCoinsDialog usage of WalletModel::SendCoinsReturn.
1917
// WalletModel::TransactionCommitFailed is used only in WalletModel::sendCoins()
2018
// all others are used only in WalletModel::prepareTransaction()
2119
switch (sendCoinsReturn.status) {
2220
case WalletModel::InvalidAddress:
23-
msgParams.first = parent->translate("The recipient address is not valid, please recheck.");
21+
retStr = parent->translate("The recipient address is not valid, please recheck.");
2422
break;
2523
case WalletModel::InvalidAmount:
26-
msgParams.first = parent->translate("The amount to pay must be larger than 0.");
24+
retStr = parent->translate("The amount to pay must be larger than 0.");
2725
break;
2826
case WalletModel::AmountExceedsBalance:
29-
msgParams.first = parent->translate("The amount exceeds your balance.");
27+
retStr = parent->translate("The amount exceeds your balance.");
3028
break;
3129
case WalletModel::AmountWithFeeExceedsBalance:
32-
msgParams.first = parent->translate(
30+
retStr = parent->translate(
3331
"The total exceeds your balance when the %1 transaction fee is included.").arg(msgArg);
3432
break;
3533
case WalletModel::DuplicateAddress:
36-
msgParams.first = parent->translate(
34+
retStr = parent->translate(
3735
"Duplicate address found, can only send to each address once per send operation.");
3836
break;
3937
case WalletModel::TransactionCreationFailed:
40-
msgParams.first = parent->translate("Transaction creation failed!");
41-
msgParams.second = CClientUIInterface::MSG_ERROR;
38+
retStr = parent->translate("Transaction creation failed!");
39+
informType = CClientUIInterface::MSG_ERROR;
4240
break;
4341
case WalletModel::TransactionCommitFailed:
44-
msgParams.first = parent->translate(
42+
retStr = parent->translate(
4543
"The transaction was rejected! This might happen if some of the coins in your wallet were already spent, such as if you used a copy of wallet.dat and coins were spent in the copy but not marked as spent here.");
46-
msgParams.second = CClientUIInterface::MSG_ERROR;
44+
informType = CClientUIInterface::MSG_ERROR;
4745
break;
4846
case WalletModel::AnonymizeOnlyUnlocked:
4947
// Unlock is only need when the coins are send
50-
if (!fPrepare)
51-
fAskForUnlock = true;
52-
else
53-
msgParams.first = parent->translate("Error: The wallet is unlocked for staking only. Fully unlock the wallet to send the transaction.");
48+
if (!fPrepare) {
49+
// Unlock wallet if it wasn't fully unlocked already
50+
walletModel->requestUnlock(AskPassphraseDialog::Context::Unlock_Full, false);
51+
if (walletModel->getEncryptionStatus() != WalletModel::Unlocked) {
52+
retStr = parent->translate(
53+
"Error: The wallet was unlocked for staking only. Unlock canceled.");
54+
}
55+
} else
56+
retStr = parent->translate("Error: The wallet is unlocked for staking only. Fully unlock the wallet to send the transaction.");
5457
break;
55-
5658
case WalletModel::InsaneFee:
57-
msgParams.first = parent->translate(
59+
retStr = parent->translate(
5860
"A fee %1 times higher than %2 per kB is considered an insanely high fee.").arg(10000).arg(
5961
BitcoinUnits::formatWithUnit(walletModel->getOptionsModel()->getDisplayUnit(),
6062
::minRelayTxFee.GetFeePerK()));
6163
break;
62-
// included to prevent a compiler warning.
64+
// included to prevent a compiler warning.
6365
case WalletModel::OK:
6466
default:
65-
return;
67+
return retStr; // No issue
6668
}
6769

68-
// Unlock wallet if it wasn't fully unlocked already
69-
if (fAskForUnlock) {
70-
walletModel->requestUnlock(AskPassphraseDialog::Context::Unlock_Full, false);
71-
if (walletModel->getEncryptionStatus() != WalletModel::Unlocked) {
72-
msgParams.first = parent->translate(
73-
"Error: The wallet was unlocked only to anonymize coins. Unlock canceled.");
74-
} else {
75-
// Wallet unlocked
76-
return;
77-
}
78-
}
70+
return retStr;
71+
}
7972

80-
parent->emitMessage(parent->translate("Send Coins"), msgParams.first, msgParams.second, 0);
73+
void ProcessSendCoinsReturnAndInform(PWidget* parent, const WalletModel::SendCoinsReturn& sendCoinsReturn, WalletModel* walletModel, const QString& msgArg, bool fPrepare) {
74+
CClientUIInterface::MessageBoxFlags informType;
75+
QString informMsg = ProcessSendCoinsReturn(parent, sendCoinsReturn, walletModel, informType, msgArg, fPrepare);
76+
if (!informMsg.isEmpty()) parent->emitMessage(parent->translate("Send Coins"), informMsg, informType, 0);
8177
}
8278

79+
8380
}

src/qt/pivx/guitransactionsutils.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,22 @@ namespace GuiTransactionsUtils {
1414
// Process WalletModel::SendCoinsReturn and generate a pair consisting
1515
// of a message and message flags for use in emit message().
1616
// Additional parameter msgArg can be used via .arg(msgArg).
17-
void ProcessSendCoinsReturn(PWidget* parent, const WalletModel::SendCoinsReturn& sendCoinsReturn, WalletModel* walletModel, const QString& msgArg = QString(), bool fPrepare = false);
17+
QString ProcessSendCoinsReturn(
18+
PWidget::Translator* parent,
19+
const WalletModel::SendCoinsReturn& sendCoinsReturn,
20+
WalletModel* walletModel,
21+
CClientUIInterface::MessageBoxFlags& informType,
22+
const QString& msgArg = QString(),
23+
bool fPrepare = false
24+
);
25+
26+
void ProcessSendCoinsReturnAndInform(PWidget* parent,
27+
const WalletModel::SendCoinsReturn& sendCoinsReturn,
28+
WalletModel* walletModel,
29+
const QString& msgArg = QString(),
30+
bool fPrepare = false
31+
);
32+
1833

1934
}
2035

src/qt/pivx/masternodewizarddialog.cpp

Lines changed: 16 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "optionsmodel.h"
99
#include "pairresult.h"
1010
#include "activemasternode.h"
11+
#include "qt/pivx/guitransactionsutils.h"
1112
#include <QFile>
1213
#include <QIntValidator>
1314
#include <QHostAddress>
@@ -208,10 +209,15 @@ bool MasterNodeWizardDialog::createMN(){
208209

209210
QString returnMsg = "Unknown error";
210211
// process prepareStatus and on error generate message shown to user
211-
returnMsg = processSendCoinsReturn(prepareStatus,
212-
BitcoinUnits::formatWithUnit(walletModel->getOptionsModel()->getDisplayUnit(),
213-
currentTransaction.getTransactionFee()),
214-
true
212+
CClientUIInterface::MessageBoxFlags informType;
213+
returnMsg = GuiTransactionsUtils::ProcessSendCoinsReturn(
214+
this,
215+
prepareStatus,
216+
walletModel,
217+
informType, // this flag is not needed
218+
BitcoinUnits::formatWithUnit(walletModel->getOptionsModel()->getDisplayUnit(),
219+
currentTransaction.getTransactionFee()),
220+
true
215221
);
216222

217223
if (prepareStatus.status != WalletModel::OK) {
@@ -221,7 +227,12 @@ bool MasterNodeWizardDialog::createMN(){
221227

222228
WalletModel::SendCoinsReturn sendStatus = walletModel->sendCoins(currentTransaction);
223229
// process sendStatus and on error generate message shown to user
224-
returnMsg = processSendCoinsReturn(sendStatus);
230+
returnMsg = GuiTransactionsUtils::ProcessSendCoinsReturn(
231+
this,
232+
sendStatus,
233+
walletModel,
234+
informType
235+
);
225236

226237
if (sendStatus.status == WalletModel::OK) {
227238
// now change the conf
@@ -362,73 +373,6 @@ void MasterNodeWizardDialog::onBackClicked(){
362373
}
363374
}
364375

365-
QString MasterNodeWizardDialog::processSendCoinsReturn(const WalletModel::SendCoinsReturn& sendCoinsReturn, const QString& msgArg, bool fPrepare)
366-
{
367-
bool fAskForUnlock = false;
368-
369-
QPair<QString, CClientUIInterface::MessageBoxFlags> msgParams;
370-
// Default to a warning message, override if error message is needed
371-
msgParams.second = CClientUIInterface::MSG_WARNING;
372-
373-
// This comment is specific to SendCoinsDialog usage of WalletModel::SendCoinsReturn.
374-
// WalletModel::TransactionCommitFailed is used only in WalletModel::sendCoins()
375-
// all others are used only in WalletModel::prepareTransaction()
376-
switch (sendCoinsReturn.status) {
377-
case WalletModel::InvalidAddress:
378-
msgParams.first = tr("The recipient address is not valid, please recheck.");
379-
break;
380-
case WalletModel::InvalidAmount:
381-
msgParams.first = tr("The amount to pay must be larger than 0.");
382-
break;
383-
case WalletModel::AmountExceedsBalance:
384-
msgParams.first = tr("The amount exceeds your balance.");
385-
break;
386-
case WalletModel::AmountWithFeeExceedsBalance:
387-
msgParams.first = tr("The total exceeds your balance when the %1 transaction fee is included.").arg(msgArg);
388-
break;
389-
case WalletModel::DuplicateAddress:
390-
msgParams.first = tr("Duplicate address found, can only send to each address once per send operation.");
391-
break;
392-
case WalletModel::TransactionCreationFailed:
393-
msgParams.first = tr("Transaction creation failed!");
394-
msgParams.second = CClientUIInterface::MSG_ERROR;
395-
break;
396-
case WalletModel::TransactionCommitFailed:
397-
msgParams.first = tr("The transaction was rejected! This might happen if some of the coins in your wallet were already spent, such as if you used a copy of wallet.dat and coins were spent in the copy but not marked as spent here.");
398-
msgParams.second = CClientUIInterface::MSG_ERROR;
399-
break;
400-
case WalletModel::AnonymizeOnlyUnlocked:
401-
// Unlock is only need when the coins are send
402-
if(!fPrepare)
403-
fAskForUnlock = true;
404-
else
405-
msgParams.first = tr("Error: The wallet was unlocked only to anonymize coins.");
406-
break;
407-
408-
case WalletModel::InsaneFee:
409-
msgParams.first = tr("A fee %1 times higher than %2 per kB is considered an insanely high fee.").arg(10000).arg(BitcoinUnits::formatWithUnit(walletModel->getOptionsModel()->getDisplayUnit(), ::minRelayTxFee.GetFeePerK()));
410-
break;
411-
// included to prevent a compiler warning.
412-
case WalletModel::OK:
413-
default:
414-
return msgParams.first;
415-
}
416-
417-
// Unlock wallet if it wasn't fully unlocked already
418-
if(fAskForUnlock) {
419-
walletModel->requestUnlock(AskPassphraseDialog::Context::Unlock_Full, false);
420-
if(walletModel->getEncryptionStatus () != WalletModel::Unlocked) {
421-
msgParams.first = tr("Error: The wallet was unlocked only to anonymize coins. Unlock canceled.");
422-
}
423-
else {
424-
// Wallet unlocked
425-
return msgParams.first;
426-
}
427-
}
428-
429-
return msgParams.first;
430-
}
431-
432376
void MasterNodeWizardDialog::inform(QString text){
433377
if (!snackBar)
434378
snackBar = new SnackBar(nullptr, this);

src/qt/pivx/masternodewizarddialog.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "walletmodel.h"
1010
#include "qt/pivx/snackbar.h"
1111
#include "masternodeconfig.h"
12+
#include "qt/pivx/pwidget.h"
1213

1314
class WalletModel;
1415

@@ -17,14 +18,15 @@ class MasterNodeWizardDialog;
1718
class QPushButton;
1819
}
1920

20-
class MasterNodeWizardDialog : public QDialog
21+
class MasterNodeWizardDialog : public QDialog, public PWidget::Translator
2122
{
2223
Q_OBJECT
2324

2425
public:
2526
explicit MasterNodeWizardDialog(WalletModel *walletMode, QWidget *parent = nullptr);
2627
~MasterNodeWizardDialog();
2728
void showEvent(QShowEvent *event) override;
29+
QString translate(const char *msg) override { return tr(msg); }
2830

2931
QString returnStr = "";
3032
bool isOk = false;
@@ -43,10 +45,6 @@ private slots:
4345

4446
WalletModel *walletModel = nullptr;
4547
bool createMN();
46-
// Process WalletModel::SendCoinsReturn and generate a pair consisting
47-
// of a message and message flags for use in emit message().
48-
// Additional parameter msgArg can be used via .arg(msgArg).
49-
QString processSendCoinsReturn(const WalletModel::SendCoinsReturn& sendCoinsReturn, const QString& msgArg = QString(), bool fPrepare = false);
5048
void inform(QString text);
5149
void initBtn(std::initializer_list<QPushButton*> args);
5250
};

src/qt/pivx/pwidget.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ namespace Ui {
1919
class PWidget;
2020
}
2121

22-
class PWidget : public QWidget, public Runnable
22+
class Translator
23+
{
24+
public:
25+
virtual QString translate(const char *msg) = 0;
26+
};
27+
28+
class PWidget : public QWidget, public Runnable, public Translator
2329
{
2430
Q_OBJECT
2531
public:
@@ -37,9 +43,7 @@ class PWidget : public QWidget, public Runnable
3743
void inform(const QString& message);
3844
void emitMessage(const QString& title, const QString& message, unsigned int style, bool* ret = nullptr);
3945

40-
QString translate(const char *msg) {
41-
return tr(msg);
42-
}
46+
QString translate(const char *msg) override { return tr(msg); }
4347

4448
signals:
4549
void message(const QString& title, const QString& body, unsigned int style, bool* ret = nullptr);

src/qt/pivx/send.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ bool SendWidget::send(QList<SendCoinsRecipient> recipients){
344344
prepareStatus = walletModel->prepareTransaction(currentTransaction, CoinControlDialog::coinControl);
345345

346346
// process prepareStatus and on error generate message shown to user
347-
GuiTransactionsUtils::ProcessSendCoinsReturn(
347+
GuiTransactionsUtils::ProcessSendCoinsReturnAndInform(
348348
this,
349349
prepareStatus,
350350
walletModel,
@@ -373,7 +373,7 @@ bool SendWidget::send(QList<SendCoinsRecipient> recipients){
373373
// now send the prepared transaction
374374
WalletModel::SendCoinsReturn sendStatus = dialog->getStatus();
375375
// process sendStatus and on error generate message shown to user
376-
GuiTransactionsUtils::ProcessSendCoinsReturn(
376+
GuiTransactionsUtils::ProcessSendCoinsReturnAndInform(
377377
this,
378378
sendStatus,
379379
walletModel

0 commit comments

Comments
 (0)