55#include < qml/models/sendrecipient.h>
66
77#include < qml/bitcoinamount.h>
8+ #include < qml/models/walletqmlmodel.h>
89
9- SendRecipient::SendRecipient (QObject* parent)
10- : QObject(parent), m_amount(new BitcoinAmount(this ))
10+ #include < key_io.h>
11+
12+ SendRecipient::SendRecipient (WalletQmlModel* wallet, QObject* parent)
13+ : QObject(parent), m_wallet(wallet), m_amount(new BitcoinAmount(this ))
1114{
15+ connect (m_amount, &BitcoinAmount::amountChanged, this , &SendRecipient::validateAmount);
1216}
1317
1418QString SendRecipient::address () const
@@ -21,6 +25,20 @@ void SendRecipient::setAddress(const QString& address)
2125 if (m_address != address) {
2226 m_address = address;
2327 Q_EMIT addressChanged ();
28+ validateAddress ();
29+ }
30+ }
31+
32+ QString SendRecipient::addressError () const
33+ {
34+ return m_addressError;
35+ }
36+
37+ void SendRecipient::setAddressError (const QString& error)
38+ {
39+ if (m_addressError != error) {
40+ m_addressError = error;
41+ Q_EMIT addressErrorChanged ();
2442 }
2543}
2644
@@ -42,6 +60,19 @@ BitcoinAmount* SendRecipient::amount() const
4260 return m_amount;
4361}
4462
63+ QString SendRecipient::amountError () const
64+ {
65+ return m_amountError;
66+ }
67+
68+ void SendRecipient::setAmountError (const QString& error)
69+ {
70+ if (m_amountError != error) {
71+ m_amountError = error;
72+ Q_EMIT amountErrorChanged ();
73+ }
74+ }
75+
4576QString SendRecipient::message () const
4677{
4778 return m_message;
@@ -77,3 +108,36 @@ void SendRecipient::clear()
77108 Q_EMIT messageChanged ();
78109 Q_EMIT amount ()->amountChanged ();
79110}
111+
112+ void SendRecipient::validateAddress ()
113+ {
114+ setAddressError (" " );
115+
116+ if (!m_address.isEmpty () && !IsValidDestinationString (m_address.toStdString ())) {
117+ setAddressError (tr (" Invalid address" ));
118+ }
119+
120+ Q_EMIT isValidChanged ();
121+ }
122+
123+ void SendRecipient::validateAmount ()
124+ {
125+ setAmountError (" " );
126+
127+ if (m_amount->isSet ()) {
128+ if (m_amount->satoshi () <= 0 ) {
129+ setAmountError (tr (" Amount must be greater than zero" ));
130+ } else if (m_amount->satoshi () > MAX_MONEY) {
131+ setAmountError (tr (" Amount exceeds maximum limit" ));
132+ } else if (m_amount->satoshi () > m_wallet->balanceSatoshi ()) {
133+ setAmountError (tr (" Amount exceeds available balance" ));
134+ }
135+ }
136+
137+ Q_EMIT isValidChanged ();
138+ }
139+
140+ bool SendRecipient::isValid () const
141+ {
142+ return m_addressError.isEmpty () && m_amountError.isEmpty () && m_amount->satoshi () > 0 && !m_address.isEmpty ();
143+ }
0 commit comments