Browse Source

render incoming and outgoing memos in a chat conversation

pull/130/head
DenioD 4 years ago
parent
commit
ab1e525860
  1. 2
      src/addressbook.cpp
  2. 106
      src/chatmodel.cpp
  3. 4
      src/chatmodel.h
  4. 108
      src/controller.cpp
  5. 4
      src/mainwindow.cpp
  6. 31
      src/mainwindow.ui

2
src/addressbook.cpp

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

106
src/chatmodel.cpp

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

4
src/chatmodel.h

@ -118,8 +118,8 @@ class ChatModel
void setItems(std::map<long, ChatItem> items);
QString zaddr();
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 clear();
void addMessage(ChatItem item);

108
src/controller.cpp

@ -840,48 +840,47 @@ void Controller::refreshBalances()
});
}
void Controller::refreshTransactions()
{
void Controller::refreshTransactions() {
if (!zrpc->haveConnection())
return noConnection();
zrpc->fetchTransactions([=] (json reply) {
QList<TransactionItem> txdata;
for (auto& it : reply.get<json::array_t>())
{
for (auto& it : reply.get<json::array_t>()) {
QString address;
CAmount total_amount;
QList<TransactionItemDetail> items;
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;
else
} else {
confirmations = model->getLatestBlock() - it["block_height"].get<json::number_integer_t>() + 1;
}
auto txid = QString::fromStdString(it["txid"]);
auto datetime = it["datetime"].get<json::number_integer_t>();
// First, check if there's outgoing metadata
if (!it["outgoing_metadata"].is_null())
{
for (auto o: it["outgoing_metadata"].get<json::array_t>())
{
QString address;
if (!it["outgoing_metadata"].is_null()) {
for (auto o: it["outgoing_metadata"].get<json::array_t>()) {
QString address;
address = QString::fromStdString(o["address"]);
// Sent items are -ve
CAmount amount = CAmount::fromqint64(-1* o["value"].get<json::number_unsigned_t>());
// Check for Memos
QString memo;
if (!o["memo"].is_null())
{
if (!o["memo"].is_null()) {
memo = QString::fromStdString(o["memo"]);
ChatItem item = ChatItem(
}
ChatItem item = ChatItem(
datetime,
address,
QString(""),
@ -889,9 +888,6 @@ void Controller::refreshTransactions()
true // is an outgoing message
);
chatModel->addMessage(item);
}
items.push_back(TransactionItemDetail{address, amount, memo});
total_amount = total_amount + amount;
@ -901,62 +897,46 @@ void Controller::refreshTransactions()
// Concat all the addresses
QList<QString> addresses;
for (auto item : items)
{
if (item.amount == 0 )
{
}
else
{
addresses.push_back(item.address);
address = addresses.join(",");
}
for (auto item : items) {
addresses.push_back(item.address);
address = addresses.join(",");
}
}
}
txdata.push_back(
TransactionItem{"send", datetime, address, txid,confirmations, items}
);
}
else
{
txdata.push_back(TransactionItem{
"send", datetime, address, txid,confirmations, items
});
} else {
// Incoming Transaction
address = (it["address"].is_null() ? "" : QString::fromStdString(it["address"]));
model->markAddressUsed(address);
QString memo;
if (!it["memo"].is_null())
{
if (!it["memo"].is_null()) {
memo = QString::fromStdString(it["memo"]);
ChatItem item = ChatItem(
}
ChatItem item = ChatItem(
datetime,
address,
QString(""),
memo
);
chatModel->addMessage(item);
}
items.push_back(
TransactionItemDetail{
address,
CAmount::fromqint64(it["amount"].get<json::number_integer_t>()),
memo
}
);
items.push_back(TransactionItemDetail{
address,
CAmount::fromqint64(it["amount"].get<json::number_integer_t>()),
memo
});
TransactionItem tx{
"Receive",
datetime,
address,
txid,
confirmations,
items
"Receive", datetime, address, txid,confirmations, items
};
txdata.push_back(tx);
@ -967,29 +947,25 @@ void Controller::refreshTransactions()
// 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
CAmount totalPending;
for (auto txitem : txdata)
{
if (txitem.confirmations == 0)
{
for (auto item: txitem.items)
{
for (auto txitem : txdata) {
if (txitem.confirmations == 0) {
for (auto item: txitem.items) {
totalPending = totalPending + item.amount;
}
}
}
getModel()->setTotalPending(totalPending);
getModel()->setTotalPending(totalPending);
// Update UI Balance
updateUIBalances();
// Update model data, which updates the table view
// Update model data, which updates the table view
transactionsTableModel->replaceData(txdata);
chatModel->renderChatBox(ui, ui->listChatMemo);
refreshContacts(
ui->listContactWidget
);
});
});
}
void Controller::refreshChat(QListWidget *listWidget)

4
src/mainwindow.cpp

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

31
src/mainwindow.ui

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

Loading…
Cancel
Save