mirror of
https://github.com/kvazar-network/kevacoin.git
synced 2025-03-13 06:01:45 +00:00
WIP: implemented adding key value.
This commit is contained in:
parent
c799f67673
commit
41d9bc8301
@ -6,17 +6,20 @@
|
||||
#include <qt/forms/ui_kevaaddkeydialog.h>
|
||||
|
||||
#include <qt/kevatablemodel.h>
|
||||
#include <qt/kevadialog.h>
|
||||
|
||||
#include <QPushButton>
|
||||
|
||||
KevaAddKeyDialog::KevaAddKeyDialog(QWidget *parent) :
|
||||
KevaAddKeyDialog::KevaAddKeyDialog(QWidget *parent, QString &nameSpace) :
|
||||
QDialog(parent),
|
||||
ui(new Ui::KevaAddKeyDialog)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
this->nameSpace = nameSpace;
|
||||
connect(ui->buttonBox->button(QDialogButtonBox::Cancel), SIGNAL(clicked()), this, SLOT(cancel()));
|
||||
connect(ui->buttonBox->button(QDialogButtonBox::Save), SIGNAL(clicked()), this, SLOT(create()));
|
||||
connect(ui->keyText, SIGNAL(textChanged(const QString &)), this, SLOT(onKeyChanged(const QString &)));
|
||||
connect(ui->valueText, SIGNAL(textChanged()), this, SLOT(onValueChanged()));
|
||||
ui->buttonBox->button(QDialogButtonBox::Save)->setEnabled(false);
|
||||
}
|
||||
|
||||
@ -27,6 +30,15 @@ KevaAddKeyDialog::~KevaAddKeyDialog()
|
||||
|
||||
void KevaAddKeyDialog::create()
|
||||
{
|
||||
KevaDialog* dialog = (KevaDialog*)this->parentWidget();
|
||||
std::string keyText = ui->keyText->text().toStdString();
|
||||
std::string valueText = ui->valueText->toPlainText().toStdString();
|
||||
std::string ns = nameSpace.toStdString();
|
||||
if (!dialog->addKeyValue(ns, keyText, valueText)) {
|
||||
QDialog::close();
|
||||
return;
|
||||
}
|
||||
dialog->showNamespace(nameSpace);
|
||||
QDialog::close();
|
||||
}
|
||||
|
||||
@ -37,6 +49,12 @@ void KevaAddKeyDialog::cancel()
|
||||
|
||||
void KevaAddKeyDialog::onKeyChanged(const QString& key)
|
||||
{
|
||||
bool enabled = key.length() > 0;
|
||||
bool enabled = key.length() > 0 && ui->valueText->toPlainText().length() > 0;
|
||||
ui->buttonBox->button(QDialogButtonBox::Save)->setEnabled(enabled);
|
||||
}
|
||||
|
||||
void KevaAddKeyDialog::onValueChanged()
|
||||
{
|
||||
bool enabled = ui->valueText->toPlainText().length() > 0 && ui->keyText->text().length() > 0;
|
||||
ui->buttonBox->button(QDialogButtonBox::Save)->setEnabled(enabled);
|
||||
}
|
||||
|
@ -24,16 +24,18 @@ class KevaAddKeyDialog : public QDialog
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit KevaAddKeyDialog(QWidget *parent = 0);
|
||||
explicit KevaAddKeyDialog(QWidget *parent, QString &nameSpace);
|
||||
~KevaAddKeyDialog();
|
||||
|
||||
private:
|
||||
Ui::KevaAddKeyDialog *ui;
|
||||
QString nameSpace;
|
||||
|
||||
public Q_SLOTS:
|
||||
void create();
|
||||
void cancel();
|
||||
void onKeyChanged(const QString& key);
|
||||
void onValueChanged();
|
||||
};
|
||||
|
||||
#endif // BITCOIN_QT_KEVAADDKEYDIALOG_H
|
||||
|
@ -278,7 +278,8 @@ void KevaDialog::on_removeButton_clicked()
|
||||
|
||||
void KevaDialog::on_addKVButton_clicked()
|
||||
{
|
||||
KevaAddKeyDialog *dialog = new KevaAddKeyDialog(this);
|
||||
QString ns = ui->nameSpace->text();
|
||||
KevaAddKeyDialog *dialog = new KevaAddKeyDialog(this, ns);
|
||||
dialog->setAttribute(Qt::WA_DeleteOnClose);
|
||||
dialog->show();
|
||||
}
|
||||
@ -393,3 +394,31 @@ int KevaDialog::createNamespace(std::string displayName, std::string& namespaceI
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int KevaDialog::addKeyValue(std::string& namespaceId, std::string& key, std::string& value)
|
||||
{
|
||||
if (!this->model) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ret = this->model->addKeyValue(namespaceId, key, value);
|
||||
if (ret > 0) {
|
||||
QString msg;
|
||||
switch (ret) {
|
||||
case WalletModel::CannotUpdate:
|
||||
msg = tr("Cannot add key-value. Make sure you own this namespace.");
|
||||
break;
|
||||
case WalletModel::KeyTooLong:
|
||||
msg = tr("Key too long.");
|
||||
break;
|
||||
case WalletModel::ValueTooLong:
|
||||
msg = tr("Value too long.");
|
||||
break;
|
||||
default:
|
||||
msg = tr("Unknown error.");
|
||||
}
|
||||
QMessageBox::critical(this, tr("Error"), msg, QMessageBox::Ok);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
@ -45,6 +45,7 @@ public:
|
||||
void setModel(WalletModel *model);
|
||||
void showNamespace(QString ns);
|
||||
int createNamespace(std::string displayName, std::string& namespaceId);
|
||||
int addKeyValue(std::string& namespaceId, std::string& key, std::string& Value);
|
||||
|
||||
public Q_SLOTS:
|
||||
void clear();
|
||||
|
@ -788,7 +788,7 @@ void WalletModel::getKevaEntries(std::vector<KevaEntry>& vKevaEntries, std::stri
|
||||
entry.key = ValtypeToString(std::get<1>(e));
|
||||
entry.value = ValtypeToString(std::get<2>(e));
|
||||
entry.block = -1; // Unconfirmed.
|
||||
entry.date = QDateTime();
|
||||
entry.date = QDateTime::currentDateTime();
|
||||
vKevaEntries.push_back(std::move(entry));
|
||||
}
|
||||
}
|
||||
@ -803,8 +803,6 @@ void WalletModel::getKevaEntries(std::vector<KevaEntry>& vKevaEntries, std::stri
|
||||
entry.key = ValtypeToString(key);
|
||||
entry.value = ValtypeToString(data.getValue());
|
||||
entry.block = data.getHeight();
|
||||
// TODO: figure out how to get the date time from block.
|
||||
|
||||
CBlockIndex* pblockindex = chainActive[entry.block];
|
||||
if (pblockindex) {
|
||||
entry.date.setTime_t(pblockindex->nTime);
|
||||
@ -985,4 +983,49 @@ int WalletModel::deleteKevaEntry(std::string namespaceStr, std::string keyStr)
|
||||
|
||||
keyName.KeepKey();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int WalletModel::addKeyValue(std::string& namespaceStr, std::string& keyStr, std::string& valueStr)
|
||||
{
|
||||
valtype nameSpace;
|
||||
if (!DecodeKevaNamespace(namespaceStr, Params(), nameSpace)) {
|
||||
return InvalidNamespace;
|
||||
}
|
||||
|
||||
const valtype key = ValtypeFromString(keyStr);
|
||||
if (keyStr.size() > MAX_KEY_LENGTH) {
|
||||
return KeyTooLong;
|
||||
}
|
||||
|
||||
const valtype value = ValtypeFromString(valueStr);
|
||||
if (value.size() > MAX_VALUE_LENGTH) {
|
||||
return ValueTooLong;
|
||||
}
|
||||
|
||||
COutput output;
|
||||
if (!wallet->FindKevaCoin(output, namespaceStr)) {
|
||||
return CannotUpdate;
|
||||
}
|
||||
const COutPoint outp(output.tx->GetHash(), output.i);
|
||||
const CTxIn txIn(outp);
|
||||
|
||||
CReserveKey keyName(wallet);
|
||||
CPubKey pubKeyReserve;
|
||||
const bool ok = keyName.GetReservedKey(pubKeyReserve, true);
|
||||
assert(ok);
|
||||
CKeyID keyId = pubKeyReserve.GetID();
|
||||
CScript redeemScript = GetScriptForDestination(WitnessV0KeyHash(keyId));
|
||||
CScriptID scriptHash = CScriptID(redeemScript);
|
||||
CScript addrName = GetScriptForDestination(scriptHash);
|
||||
|
||||
const CScript kevaScript = CKevaScript::buildKevaPut(addrName, nameSpace, key, value);
|
||||
|
||||
CCoinControl coinControl;
|
||||
CWalletTx wtx;
|
||||
valtype empty;
|
||||
SendMoneyToScript(wallet, kevaScript, &txIn, empty,
|
||||
KEVA_LOCKED_AMOUNT, false, wtx, coinControl);
|
||||
|
||||
keyName.KeepKey();
|
||||
return 0;
|
||||
}
|
||||
|
@ -128,7 +128,9 @@ public:
|
||||
InvalidNamespace,
|
||||
KeyTooLong,
|
||||
NamespaceTooLong,
|
||||
KeyNotFound
|
||||
KeyNotFound,
|
||||
ValueTooLong,
|
||||
CannotUpdate,
|
||||
};
|
||||
|
||||
enum EncryptionStatus
|
||||
@ -240,6 +242,7 @@ public:
|
||||
void getNamespaceEntries(std::vector<NamespaceEntry>& vNamespaceEntries);
|
||||
int createNamespace(std::string displayName, std::string& namespaceId);
|
||||
int deleteKevaEntry(std::string nameSpace, std::string key);
|
||||
int addKeyValue(std::string& namespaceStr, std::string& keyStr, std::string& valueStr);
|
||||
|
||||
private:
|
||||
CWallet *wallet;
|
||||
|
Loading…
x
Reference in New Issue
Block a user