From 45f3a80885bce88338bf47a2de5cb29d839d33ec Mon Sep 17 00:00:00 2001 From: DenioD <41270280+DenioD@users.noreply.github.com> Date: Wed, 29 Apr 2020 22:53:04 +0200 Subject: [PATCH] add contact request flag to headermemo - work in progress --- src/chatmodel.cpp | 180 +++++++++++++++++++++++++++++++++++++++++---- src/mainwindow.cpp | 2 +- src/mainwindow.h | 5 +- src/mainwindow.ui | 2 +- 4 files changed, 173 insertions(+), 16 deletions(-) diff --git a/src/chatmodel.cpp b/src/chatmodel.cpp index d412bc4..53f94a2 100644 --- a/src/chatmodel.cpp +++ b/src/chatmodel.cpp @@ -83,11 +83,7 @@ void ChatModel::renderChatBox(QListWidget *view) { view->takeItem(0); } - - //QModelIndex index = parent->listContactWidget->currentIndex(); - // QString itemText = index.data(Qt::DisplayRole).toString(); - - + for(auto &c : this->chatItems) { @@ -109,7 +105,7 @@ void ChatModel::renderChatBox(QListWidget *view) } } -QString MainWindow::createHeaderMemo(QString cid, QString zaddr, int version=0, int headerNumber=1) +QString MainWindow::createHeaderMemo(QString safeContact, QString cid, QString zaddr, int version=0, int headerNumber=1) { QString header=""; @@ -121,6 +117,7 @@ QString MainWindow::createHeaderMemo(QString cid, QString zaddr, int version=0, h["v"] = version; // HushChat version h["z"] = zaddr; // zaddr to respond to h["cid"] = cid; // conversation id + h["c"] = safeContact; // Is this a safe Contact request? j.setObject(h); header = j.toJson(); @@ -135,13 +132,7 @@ Tx MainWindow::createTxFromChatPage() { CAmount totalAmt; // For each addr/amt in the Chat tab { - - - - - - QString addr = ui->ContactZaddr->text().trimmed(); // We need to set the reply Address for our Contact here // Remove label if it exists addr = AddressBook::addressFromAddressLabel(addr); @@ -162,9 +153,11 @@ Tx MainWindow::createTxFromChatPage() { QString cid = c.getCid(); QString myAddr = c.getMyAddress(); + QString safeContact = "false"; QString addr = c.getPartnerAddress(); + - QString hmemo= createHeaderMemo(cid,myAddr); + QString hmemo= createHeaderMemo(safeContact,cid,myAddr); QString memo = ui->memoTxtChat->toPlainText().trimmed(); // ui->memoSizeChat->setLenDisplayLabel(); @@ -291,5 +284,166 @@ QString MainWindow::doSendChatTxValidations(Tx tx) { .arg(available.toDecimalhushString(), total.toDecimalhushString()); } + return ""; +} + +// Create a Safe Contact Request. +Tx MainWindow::createTxForSafeContactRequest() { + Tx tx; + CAmount totalAmt; + // For each addr/amt in the Chat tab + { + + QString addr = ui->ContactZaddr->text().trimmed(); // We need to set the reply Address for our Contact here + // Remove label if it exists + addr = AddressBook::addressFromAddressLabel(addr); + + QString amtStr = "0"; + + // bool ok; + CAmount amt; + + + amt = CAmount::fromDecimalString("0"); + totalAmt = totalAmt + amt; + + + for(auto &c : AddressBook::getInstance()->getAllAddressLabels()) + + if (ui->ContactZaddr->text().trimmed() == c.getName()) { + + // QString cid = c.getCid(); // This has to be a new cid for the contact + // QString myAddr = c.getMyAddress(); // this should be a new HushChat zaddr + // QString addr = c.getPartnerAddress(); // this address will be insert by the user + QString safeContact = "true"; + + + QString hmemo= createHeaderMemo(safeContact,cid,myAddr); + QString memo = ui->memoTxtChat->toPlainText().trimmed(); + // ui->memoSizeChat->setLenDisplayLabel(); + + + tx.toAddrs.push_back(ToFields{addr, amt, hmemo}) ; + qDebug()<text()))) { + // QMessageBox msg(QMessageBox::Critical, tr("Memos can only be used with z-addresses"), + // tr("The memo field can only be used with a z-address.\n") + addr->text() + tr("\ndoesn't look like a z-address"), + // QMessageBox::Ok, this); + + // msg.exec(); + //return; + //} + + Tx tx = createTxForSafeContactRequest(); + + QString error = doSendChatTxValidations(tx); + + if (!error.isEmpty()) { + // Something went wrong, so show an error and exit + QMessageBox msg(QMessageBox::Critical, tr("Message Error"), error, + QMessageBox::Ok, this); + + msg.exec(); + + // abort the Tx + return; + qDebug() << "Tx aborted"; + } + + // Create a new Dialog to show that we are computing/sending the Tx + auto d = new QDialog(this); + auto connD = new Ui_ConnectionDialog(); + connD->setupUi(d); + QPixmap logo(":/img/res/logobig.gif"); + connD->topIcon->setBasePixmap(logo.scaled(256, 256, Qt::KeepAspectRatio, Qt::SmoothTransformation)); + + connD->status->setText(tr("Please wait...")); + connD->statusDetail->setText(tr("Your Safe Contact Request will be send")); + + d->show(); + + // And send the Tx + rpc->executeTransaction(tx, + [=] (QString txid) { + ui->statusBar->showMessage(Settings::txidStatusMessage + " " + txid); + + connD->status->setText(tr("Done!")); + connD->statusDetail->setText(txid); + + QTimer::singleShot(1000, [=]() { + d->accept(); + d->close(); + delete connD; + delete d; + + }); + + // Force a UI update so we get the unconfirmed Tx + rpc->refresh(true); + + }, + // Errored out + [=] (QString opid, QString errStr) { + ui->statusBar->showMessage(QObject::tr(" Tx ") % opid % QObject::tr(" failed"), 15 * 1000); + + d->accept(); + d->close(); + delete connD; + delete d; + + if (!opid.isEmpty()) + errStr = QObject::tr("The transaction with id ") % opid % QObject::tr(" failed. The error was") + ":\n\n" + errStr; + + QMessageBox::critical(this, QObject::tr("Transaction Error"), errStr, QMessageBox::Ok); + } + ); + } + + +QString MainWindow::doSendRequestTxValidations(Tx tx) { + // Check to see if we have enough verified funds to send the Tx. + + CAmount total; + for (auto toAddr : tx.toAddrs) { + if (!Settings::isValidAddress(toAddr.addr)) { + QString addr = (toAddr.addr.length() > 100 ? toAddr.addr.left(100) + "..." : toAddr.addr); + return QString(tr("Recipient Address ")) % addr % tr(" is Invalid"); + } + + // This technically shouldn't be possible, but issue #62 seems to have discovered a bug + // somewhere, so just add a check to make sure. + if (toAddr.amount.toqint64() < 0) { + return QString(tr("Amount for address '%1' is invalid!").arg(toAddr.addr)); + } + + total = total + toAddr.amount; + } + total = total + tx.fee; + + auto available = rpc->getModel()->getAvailableBalance(); + + if (available < total) { + return tr("Not enough available funds to send this transaction\n\nHave: %1\nNeed: %2\n\nNote: Funds need 5 confirmations before they can be spent") + .arg(available.toDecimalhushString(), total.toDecimalhushString()); + } + return ""; } \ No newline at end of file diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index cc60938..88a4d5f 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -987,7 +987,7 @@ void MainWindow::setupchatTab() { // Send button QObject::connect(ui->sendChatButton, &QPushButton::clicked, this, &MainWindow::sendChatButton); - + QObject::connect(ui->safeContactRequest, &QPushButton::clicked, this, &MainWindow::safeContactRequest); ///////// Set selected Zaddr for Chat with Doubleklick diff --git a/src/mainwindow.h b/src/mainwindow.h index ad83f73..f9f781b 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -48,6 +48,7 @@ public: QString doSendTxValidations(Tx tx); QString doSendChatTxValidations(Tx tx); + QString doSendRequestTxValidations(Tx tx); void replaceWormholeClient(WormholeClient* newClient); bool isWebsocketListening(); @@ -76,7 +77,7 @@ public: Logger* logger; void doClose(); - QString createHeaderMemo(QString cid, QString zaddr, int version, int headerNumnber); + QString createHeaderMemo(QString safeContact, QString cid, QString zaddr, int version, int headerNumber); public slots: void slot_change_theme(const QString& themeName); @@ -103,6 +104,7 @@ private: bool confirmTx(Tx tx, RecurringPaymentInfo* rpi); Tx createTxFromChatPage(); + Tx createTxForSafeContactRequest(); void encryptWallet(); void removeWalletEncryption(); @@ -110,6 +112,7 @@ private: void cancelButton(); void sendButton(); void sendChatButton(); + void safeContactRequest(); void addAddressSection(); void maxAmountChecked(int checked); diff --git a/src/mainwindow.ui b/src/mainwindow.ui index 59d3e78..4c8fa7b 100644 --- a/src/mainwindow.ui +++ b/src/mainwindow.ui @@ -1502,7 +1502,7 @@ - + 943