render incoming and outgoing memos in a chat conversation

This commit is contained in:
DenioD
2020-04-30 21:18:37 +02:00
parent 8f3bc21844
commit ab1e525860
6 changed files with 124 additions and 129 deletions

View File

@@ -194,6 +194,8 @@ void AddressBook::open(MainWindow* parent, QLineEdit* target)
message, //todo translate this message, //todo translate this
QMessageBox::Ok QMessageBox::Ok
); );
parent->ui->listReceiveAddresses->insertItem(0, addr);
parent->ui->listReceiveAddresses->setCurrentIndex(0);
// ab.addr_chat->setText(myAddr); // ab.addr_chat->setText(myAddr);
qDebug() << "new generated myAddr" << myAddr; qDebug() << "new generated myAddr" << myAddr;
QString cid = QUuid::createUuid().toString(QUuid::WithoutBraces); // QString cid = QUuid::createUuid().toString(QUuid::WithoutBraces); //

View File

@@ -62,9 +62,7 @@ void ChatModel::showMessages()
} }
} }
void ChatModel::renderChatBox(Ui::MainWindow* ui, QListWidget &view)
void ChatModel::renderChatBox(Ui::MainWindow* ui, QListWidget &view)
{ {
/*for(auto &c : this->chatItems) /*for(auto &c : this->chatItems)
{ {
@@ -90,14 +88,22 @@ void ChatModel::renderChatBox(Ui::MainWindow* ui, QListWidget *view)
myDateTime.setTime_t(c.second.getTimestamp()); myDateTime.setTime_t(c.second.getTimestamp());
////// //////Render only Memos for selected contacts. Do not render empty Memos
if ((ui->ContactZaddr->text().trimmed() == c.second.getAddress()) && (c.second.getMemo().startsWith("{") == false)) { if ((ui->ContactZaddr->text().trimmed() == c.second.getAddress()) && (c.second.getMemo().startsWith("{") == false) && (c.second.getMemo().isEmpty() == false)) {
line += QString("[") + myDateTime.toString("dd.MM.yyyy hh:mm:ss ") + QString("] "); line += QString("[") + myDateTime.toString("dd.MM.yyyy hh:mm:ss ") + QString("] ");
line += QString("<") + QString(c.second.getContact()) + QString("> :\n"); line += QString("<") + QString(c.second.getContact()) + QString("> :\n");
line += QString(c.second.getMemo()) + QString("\n"); line += QString(c.second.getMemo()) + QString("\n");
view->addItem(line); view->addItem(line);
line =""; line ="";
}else {} }
if ((ui->MyZaddr->text().trimmed() == c.second.getAddress()) && (c.second.getMemo().startsWith("{") == false) && (c.second.getMemo().isEmpty() == false)){
line += QString("[") + myDateTime.toString("dd.MM.yyyy hh:mm:ss ") + QString("] ");
line += QString("<") + QString(c.second.getContact()) + QString("> :\n");
line += QString(c.second.getMemo()) + QString("\n");
view->addItem(line);
line ="";
}else{}
} }
@@ -131,13 +137,7 @@ Tx MainWindow::createTxFromChatPage() {
// For each addr/amt in the Chat tab // 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"; QString amtStr = "0";
// bool ok;
CAmount amt; CAmount amt;
@@ -151,17 +151,16 @@ Tx MainWindow::createTxFromChatPage() {
QString cid = c.getCid(); QString cid = c.getCid();
QString myAddr = c.getMyAddress(); QString myAddr = c.getMyAddress();
QString type = "memo"; QString type = "Memo";
QString addr = c.getPartnerAddress(); QString addr = c.getPartnerAddress();
QString hmemo= createHeaderMemo(type,cid,myAddr); QString hmemo= createHeaderMemo(type,cid,myAddr);
QString memo = ui->memoTxtChat->toPlainText().trimmed(); QString memo = ui->memoTxtChat->toPlainText().trimmed();
// ui->memoSizeChat->setLenDisplayLabel(); // ui->memoSizeChat->setLenDisplayLabel();// Todo -> activate lendisplay for chat
tx.toAddrs.push_back(ToFields{addr, amt, hmemo}) ; tx.toAddrs.push_back(ToFields{addr, amt, hmemo}) ;
qDebug()<<hmemo;
tx.toAddrs.push_back( ToFields{addr, amt, memo}); tx.toAddrs.push_back( ToFields{addr, amt, memo});
qDebug() << "pushback chattx"; qDebug() << "pushback chattx";
@@ -175,19 +174,25 @@ Tx MainWindow::createTxFromChatPage() {
} }
void MainWindow::sendChatButton() { void MainWindow::sendChatButton() {
////////////////////////////Todo: Check if its a zaddr//////////
// Create a Tx from the values on the send tab. Note that this Tx object // Create a Tx from the values on the send tab. Note that this Tx object
// might not be valid yet. // might not be valid yet.
// Memos can only be used with zAddrs. So check that first // Memos can only be used with zAddrs. So check that first
//auto addr = "zs1fllv4hgrjddnz2yz5dng9kchcg3wkhs0j2v5v3nc89w3r3kntkgq2sefcz2a9k2ycc8f6t0gm2q"; // for(auto &c : AddressBook::getInstance()->getAllAddressLabels())
// if (! Settings::isZAddress(AddressBook::addressFromAddressLabel(addr->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(); // if (ui->ContactZaddr->text().trimmed() == c.getName()) {
//return;
//} // auto addr = "";
// if (! Settings::isZAddress(AddressBook::addressFromAddressLabel(addr->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 = createTxFromChatPage(); Tx tx = createTxFromChatPage();
@@ -287,20 +292,13 @@ QString MainWindow::doSendChatTxValidations(Tx tx) {
return ""; return "";
} }
// Create a Safe Contact Request. // Create a Contact Request.
Tx MainWindow::createTxForSafeContactRequest() { Tx MainWindow::createTxForSafeContactRequest() {
Tx tx; Tx tx;
CAmount totalAmt; 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"; QString amtStr = "0";
// bool ok;
CAmount amt; CAmount amt;
@@ -318,20 +316,17 @@ Tx MainWindow::createTxForSafeContactRequest() {
QString cid = ""; QString cid = "";
QString myAddr = ""; QString myAddr = "";
QString addr = ""; QString addr = "";
QString safeContact = "true"; QString type = "Request";
QString hmemo= createHeaderMemo(safeContact,cid,myAddr); QString hmemo= createHeaderMemo(type,cid,myAddr);
QString memo = ui->memoTxtChat->toPlainText().trimmed();
// ui->memoSizeChat->setLenDisplayLabel();
tx.toAddrs.push_back(ToFields{addr, amt, hmemo}) ; tx.toAddrs.push_back(ToFields{addr, amt, hmemo}) ;
qDebug()<<hmemo;
// tx.toAddrs.push_back( ToFields{addr, amt, memo});
qDebug() << "pushback chattx"; qDebug() << "pushback chattx";
} } }
}
tx.fee = Settings::getMinerFee(); tx.fee = Settings::getMinerFee();
@@ -341,19 +336,26 @@ Tx MainWindow::createTxForSafeContactRequest() {
} }
void MainWindow::safeContactRequest() { void MainWindow::safeContactRequest() {
////////////////////////////Todo: Check if its a zaddr//////////
// Create a Tx from the values on the send tab. Note that this Tx object // Create a Tx from the values on the send tab. Note that this Tx object
// might not be valid yet. // might not be valid yet.
// Memos can only be used with zAddrs. So check that first // Memos can only be used with zAddrs. So check that first
//auto addr = "zs1fllv4hgrjddnz2yz5dng9kchcg3wkhs0j2v5v3nc89w3r3kntkgq2sefcz2a9k2ycc8f6t0gm2q"; // for(auto &c : AddressBook::getInstance()->getAllAddressLabels())
// if (! Settings::isZAddress(AddressBook::addressFromAddressLabel(addr->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(); // if (ui->ContactZaddr->text().trimmed() == c.getName()) {
//return;
//} // auto addr = "";
// if (! Settings::isZAddress(AddressBook::addressFromAddressLabel(addr->text()))) {
// QMessageBox msg(QMessageBox::Critical, tr("Contact requests 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(); Tx tx = createTxForSafeContactRequest();

View File

@@ -118,8 +118,8 @@ class ChatModel
void setItems(std::map<long, ChatItem> items); void setItems(std::map<long, ChatItem> items);
QString zaddr(); QString zaddr();
void setItems(std::vector<ChatItem> items); void setItems(std::vector<ChatItem> items);
void renderChatBox(Ui::MainWindow* ui, QListWidget &view); void renderChatBox(Ui::MainWindow* ui, QListWidget &view);
void renderChatBox(Ui::MainWindow* ui, QListWidget *view); void renderChatBox(Ui::MainWindow* ui, QListWidget *view);
void showMessages(); void showMessages();
void clear(); void clear();
void addMessage(ChatItem item); void addMessage(ChatItem item);

View File

@@ -840,35 +840,35 @@ void Controller::refreshBalances()
}); });
} }
void Controller::refreshTransactions() void Controller::refreshTransactions() {
{
if (!zrpc->haveConnection()) if (!zrpc->haveConnection())
return noConnection(); return noConnection();
zrpc->fetchTransactions([=] (json reply) { zrpc->fetchTransactions([=] (json reply) {
QList<TransactionItem> txdata; QList<TransactionItem> txdata;
for (auto& it : reply.get<json::array_t>()) for (auto& it : reply.get<json::array_t>()) {
{
QString address; QString address;
CAmount total_amount; CAmount total_amount;
QList<TransactionItemDetail> items; QList<TransactionItemDetail> items;
long confirmations; long confirmations;
if (it.find("unconfirmed") != it.end() && it["unconfirmed"].get<json::boolean_t>()) if (it.find("unconfirmed") != it.end() && it["unconfirmed"].get<json::boolean_t>()) {
confirmations = 0; confirmations = 0;
else } else {
confirmations = model->getLatestBlock() - it["block_height"].get<json::number_integer_t>() + 1; confirmations = model->getLatestBlock() - it["block_height"].get<json::number_integer_t>() + 1;
}
auto txid = QString::fromStdString(it["txid"]); auto txid = QString::fromStdString(it["txid"]);
auto datetime = it["datetime"].get<json::number_integer_t>(); auto datetime = it["datetime"].get<json::number_integer_t>();
// First, check if there's outgoing metadata // First, check if there's outgoing metadata
if (!it["outgoing_metadata"].is_null()) if (!it["outgoing_metadata"].is_null()) {
{
for (auto o: it["outgoing_metadata"].get<json::array_t>()) for (auto o: it["outgoing_metadata"].get<json::array_t>()) {
{
QString address; QString address;
address = QString::fromStdString(o["address"]); address = QString::fromStdString(o["address"]);
// Sent items are -ve // Sent items are -ve
@@ -877,11 +877,10 @@ void Controller::refreshTransactions()
// Check for Memos // Check for Memos
QString memo; QString memo;
if (!o["memo"].is_null()) if (!o["memo"].is_null()) {
{
memo = QString::fromStdString(o["memo"]); memo = QString::fromStdString(o["memo"]);
}
ChatItem item = ChatItem( ChatItem item = ChatItem(
datetime, datetime,
address, address,
QString(""), QString(""),
@@ -890,9 +889,6 @@ void Controller::refreshTransactions()
); );
chatModel->addMessage(item); chatModel->addMessage(item);
}
items.push_back(TransactionItemDetail{address, amount, memo}); items.push_back(TransactionItemDetail{address, amount, memo});
total_amount = total_amount + amount; total_amount = total_amount + amount;
} }
@@ -901,62 +897,46 @@ void Controller::refreshTransactions()
// Concat all the addresses // Concat all the addresses
QList<QString> addresses; QList<QString> addresses;
for (auto item : items) for (auto item : items) {
{
if (item.amount == 0 )
{
}
else
{
addresses.push_back(item.address);
address = addresses.join(",");
}
} addresses.push_back(item.address);
address = addresses.join(",");
}
} }
txdata.push_back( txdata.push_back(TransactionItem{
TransactionItem{"send", datetime, address, txid,confirmations, items} "send", datetime, address, txid,confirmations, items
); });
} else {
}
else
{
// Incoming Transaction // Incoming Transaction
address = (it["address"].is_null() ? "" : QString::fromStdString(it["address"])); address = (it["address"].is_null() ? "" : QString::fromStdString(it["address"]));
model->markAddressUsed(address); model->markAddressUsed(address);
QString memo;
if (!it["memo"].is_null())
{
memo = QString::fromStdString(it["memo"]);
ChatItem item = ChatItem( QString memo;
if (!it["memo"].is_null()) {
memo = QString::fromStdString(it["memo"]);
}
ChatItem item = ChatItem(
datetime, datetime,
address, address,
QString(""), QString(""),
memo memo
); );
chatModel->addMessage(item); chatModel->addMessage(item);
}
items.push_back(TransactionItemDetail{
items.push_back( address,
TransactionItemDetail{ CAmount::fromqint64(it["amount"].get<json::number_integer_t>()),
address, memo
CAmount::fromqint64(it["amount"].get<json::number_integer_t>()), });
memo
}
);
TransactionItem tx{ TransactionItem tx{
"Receive", "Receive", datetime, address, txid,confirmations, items
datetime,
address,
txid,
confirmations,
items
}; };
txdata.push_back(tx); txdata.push_back(tx);
@@ -967,29 +947,25 @@ void Controller::refreshTransactions()
// Calculate the total unspent amount that's pending. This will need to be // Calculate the total unspent amount that's pending. This will need to be
// shown in the UI so the user can keep track of pending funds // shown in the UI so the user can keep track of pending funds
CAmount totalPending; CAmount totalPending;
for (auto txitem : txdata) for (auto txitem : txdata) {
{ if (txitem.confirmations == 0) {
if (txitem.confirmations == 0) for (auto item: txitem.items) {
{
for (auto item: txitem.items)
{
totalPending = totalPending + item.amount; totalPending = totalPending + item.amount;
} }
} }
} }
getModel()->setTotalPending(totalPending);
getModel()->setTotalPending(totalPending);
// Update UI Balance // Update UI Balance
updateUIBalances(); updateUIBalances();
// Update model data, which updates the table view // Update model data, which updates the table view
transactionsTableModel->replaceData(txdata); transactionsTableModel->replaceData(txdata);
chatModel->renderChatBox(ui, ui->listChatMemo); chatModel->renderChatBox(ui, ui->listChatMemo);
refreshContacts( refreshContacts(
ui->listContactWidget ui->listContactWidget
); );
}); });
} }
void Controller::refreshChat(QListWidget *listWidget) void Controller::refreshChat(QListWidget *listWidget)

View File

@@ -997,10 +997,12 @@ void MainWindow::setupchatTab() {
QString label_contact = index.data(Qt::DisplayRole).toString(); QString label_contact = index.data(Qt::DisplayRole).toString();
for(auto &p : AddressBook::getInstance()->getAllAddressLabels()) for(auto &p : AddressBook::getInstance()->getAllAddressLabels())
if (label_contact == p.getName()) if (label_contact == p.getName()) {
ui->ContactZaddr->setText(p.getPartnerAddress()); ui->ContactZaddr->setText(p.getPartnerAddress());
ui->MyZaddr->setText(p.getMyAddress());
rpc->refresh(true); rpc->refresh(true);
}
}); });
} }

View File

@@ -1354,8 +1354,8 @@
<widget class="QLabel" name="label_40"> <widget class="QLabel" name="label_40">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>340</x> <x>400</x>
<y>490</y> <y>410</y>
<width>331</width> <width>331</width>
<height>17</height> <height>17</height>
</rect> </rect>
@@ -1453,8 +1453,8 @@
<widget class="QLabel" name="label_42"> <widget class="QLabel" name="label_42">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>520</x> <x>340</x>
<y>490</y> <y>460</y>
<width>67</width> <width>67</width>
<height>17</height> <height>17</height>
</rect> </rect>
@@ -1466,21 +1466,21 @@
<widget class="QLabel" name="ContactName"> <widget class="QLabel" name="ContactName">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>520</x> <x>340</x>
<y>490</y> <y>490</y>
<width>67</width> <width>161</width>
<height>17</height> <height>17</height>
</rect> </rect>
</property> </property>
<property name="text"> <property name="text">
<string/> <string>Your HushChat zaddr:</string>
</property> </property>
</widget> </widget>
<widget class="QLabel" name="ContactZaddr"> <widget class="QLabel" name="ContactZaddr">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>550</x> <x>370</x>
<y>490</y> <y>460</y>
<width>691</width> <width>691</width>
<height>20</height> <height>20</height>
</rect> </rect>
@@ -1511,6 +1511,19 @@
<bool>false</bool> <bool>false</bool>
</property> </property>
</widget> </widget>
<widget class="QLabel" name="MyZaddr">
<property name="geometry">
<rect>
<x>490</x>
<y>490</y>
<width>691</width>
<height>20</height>
</rect>
</property>
<property name="text">
<string/>
</property>
</widget>
</widget> </widget>
</widget> </widget>
</item> </item>