Skip to content

Commit

Permalink
Add separate field for entering e-mail address of recipient.
Browse files Browse the repository at this point in the history
  • Loading branch information
mtl1979 committed May 26, 2022
1 parent 08d8d07 commit 58f03a7
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 42 deletions.
67 changes: 62 additions & 5 deletions src/Gui/Send/TransferFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include <QClipboard>
#include <QCompleter>
#include <QJsonDocument>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QStyle>
#include <QWheelEvent>
Expand Down Expand Up @@ -66,25 +65,49 @@ TransferFrame::TransferFrame(QWidget* _parent) : QFrame(_parent), m_ui(new Ui::T
m_ui->setupUi(this);
m_ui->m_sendAmountSpin->installEventFilter(this);
setStyleSheet(Settings::instance().getCurrentStyle().makeStyleSheet(TRANSFER_FRAME_STYLE_SHEET_TEMPLATE));
m_manager = new QNetworkAccessManager(this);
}

TransferFrame::~TransferFrame() {
}

void TransferFrame::resolveAddress(const QString& _email) {
QNetworkAccessManager *mgr = new QNetworkAccessManager(this);
auto iter = m_resolvedAddresses.find(_email);
if (iter != m_resolvedAddresses.end()) {
m_lastResolvedAddress = iter.value();
if (m_lastResolvedAddress != getAddress()) {
setAddress(m_lastResolvedAddress);
}
setEmailError(false);
return;
}
QUrl url(QStringLiteral("https://wallet.talleo.org/getaddress.php?email=") + _email);
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
QNetworkReply *reply = mgr->post(request, QByteArray());
QNetworkReply *reply = m_manager->post(request, QByteArray());

QObject::connect(reply, &QNetworkReply::finished, [=](){
if(reply->error() == QNetworkReply::NoError){
QByteArray contents = reply->readAll();
QJsonDocument doc = QJsonDocument::fromJson(contents);
if (doc.object().value("address") != QJsonValue::Undefined) {
setAddress(doc.object().value("address").toString());
m_lastResolvedAddress = doc.object().value("address").toString();
m_resolvedAddresses.insert(_email, m_lastResolvedAddress);
if (m_resolvedEmails.find(m_lastResolvedAddress) == m_resolvedEmails.end()) {
// Make sure two e-mail addresses don't map to same wallet address
m_resolvedEmails.insert(m_lastResolvedAddress, _email);
if (m_lastResolvedAddress != getAddress()) {
setAddress(m_lastResolvedAddress);
}
setEmailError(false);
} else {
setEmailError(true);
}
} else {
setEmailError(true);
}
} else {
setEmailError(true);
}
reply->deleteLater();
});
Expand All @@ -103,6 +126,10 @@ QString TransferFrame::getAmountString() const {
return m_ui->m_sendAmountSpin->cleanText();
}

QString TransferFrame::getEmail() const {
return m_ui->m_sendEmailEdit->text().trimmed();
}

QString TransferFrame::getLabel() const {
return m_ui->m_sendLabelEdit->text().trimmed();
}
Expand All @@ -115,6 +142,10 @@ void TransferFrame::setAmount(qreal _amount) {
m_ui->m_sendAmountSpin->setValue(_amount);
}

void TransferFrame::setEmail(const QString& _email) {
m_ui->m_sendEmailEdit->setText(_email);
}

void TransferFrame::setLabel(const QString& _label) {
m_ui->m_sendLabelEdit->setText(_label);
}
Expand Down Expand Up @@ -147,6 +178,14 @@ void TransferFrame::setAddressError(bool _error) {
m_ui->m_payToTextLabel->update();
}

void TransferFrame::setEmailError(bool _error) {
m_ui->m_sendEmailEdit->setProperty("errorState", _error);

m_ui->m_sendEmailEdit->style()->unpolish(m_ui->m_sendEmailEdit);
m_ui->m_sendEmailEdit->style()->polish(m_ui->m_sendEmailEdit);
m_ui->m_sendEmailEdit->update();
}

void TransferFrame::setDuplicationError(bool _error) {
m_ui->m_sendLabelEdit->setProperty("errorState", _error);
m_ui->m_labelTextLabel->setProperty("errorState", _error);
Expand Down Expand Up @@ -286,17 +325,35 @@ void TransferFrame::pasteClicked() {

void TransferFrame::addressChanged(const QString& _address) {
if (_address.contains("@")) {
resolveAddress(_address);
setEmail(_address);
return;
}
setAddressError(m_addressCompleter->currentCompletion().isEmpty() && !_address.isEmpty() &&
!m_cryptoNoteAdapter->isValidAddress(_address));
Q_EMIT addressChangedSignal(_address);
}

void TransferFrame::emailChanged(const QString& _email) {
if (_email.contains("@")) {
resolveAddress(_email);
} else if (_email.isEmpty()) {
setEmailError(false);
} else {
setEmailError(true);
}
}

void TransferFrame::labelOrAddressChanged(const QString& _text) {
QString label = getLabel().trimmed();
QString address = getAddress().trimmed();
if (address != m_lastResolvedAddress || getEmail().isEmpty()) {
auto iter = m_resolvedEmails.find(address);
if (iter != m_resolvedEmails.end()) {
setEmail(iter.value());
} else {
m_ui->m_sendEmailEdit->clear();
}
}
if (!label.isEmpty() && (m_addressBookManager->findAddressByAddress(address) != std::numeric_limits<quintptr>::max() ||
m_addressBookManager->findAddressByLabel(label) != std::numeric_limits<quintptr>::max())) {
setDuplicationError(true);
Expand Down
9 changes: 9 additions & 0 deletions src/Gui/Send/TransferFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#pragma once

#include <QFrame>
#include <QNetworkAccessManager>

#include "Application/IWalletUiItem.h"

Expand All @@ -45,16 +46,19 @@ class TransferFrame : public QFrame, public IWalletUiItem {
bool readyToSend() const;
QString getAddress() const;
QString getAmountString() const;
QString getEmail() const;
QString getLabel() const;

void setAddress(const QString& _address);
void setAmount(qreal _amount);
void setEmail(const QString& _email);
void setLabel(const QString& _label);

void disableRemoveButton(bool _disable);
void hideBorder();

void setAddressError(bool _error = true);
void setEmailError(bool _error = true);
void setDuplicationError(bool _error = true);
void setAmountFormatError(bool _error = true);
void setInsufficientFundsError(bool _error = true);
Expand All @@ -77,12 +81,17 @@ class TransferFrame : public QFrame, public IWalletUiItem {
QWidget* m_mainWindow;
QAbstractItemModel* m_addressBookModel;
QCompleter* m_addressCompleter;
QNetworkAccessManager* m_manager;
QHash<QString, QString> m_resolvedAddresses;
QHash<QString, QString> m_resolvedEmails;
QString m_lastResolvedAddress;

void resolveAddress(const QString& _email);
void validateAmount(double _amount);

Q_SLOT void amountStringChanged(const QString& _amountString);
Q_SLOT void addressChanged(const QString& _address);
Q_SLOT void emailChanged(const QString& _email);
Q_SLOT void labelOrAddressChanged(const QString& _text);
Q_SLOT void addressBookClicked();
Q_SLOT void pasteClicked();
Expand Down
Loading

0 comments on commit 58f03a7

Please sign in to comment.