Skip to content

Commit

Permalink
Namecoin: Add Renew to Qt GUI context menu
Browse files Browse the repository at this point in the history
Fixes namecoin#377

Based on namecoin#187 by Brandon
Roberts.
  • Loading branch information
JeremyRand committed Dec 14, 2020
1 parent 008a447 commit baa62b2
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/qt/forms/managenamespage.ui
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ to the network and creates a pending name_firstupdate transaction.</string>
<bool>true</bool>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::SingleSelection</enum>
<enum>QAbstractItemView::ExtendedSelection</enum>
</property>
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum>
Expand Down
69 changes: 67 additions & 2 deletions src/qt/managenamespage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,20 @@ ManageNamesPage::ManageNamesPage(const PlatformStyle *platformStyle, QWidget *pa
ui->setupUi(this);

// Context menu actions
QAction *copyNameAction = new QAction(tr("Copy &Name"), this);
QAction *copyValueAction = new QAction(tr("Copy &Value"), this);
copyNameAction = new QAction(tr("Copy &Name"), this);
copyValueAction = new QAction(tr("Copy &Value"), this);
renewNameAction = new QAction(tr("&Renew Names"), this);

// Build context menu
contextMenu = new QMenu();
contextMenu->addAction(copyNameAction);
contextMenu->addAction(copyValueAction);
contextMenu->addAction(renewNameAction);

// Connect signals for context menu actions
connect(copyNameAction, &QAction::triggered, this, &ManageNamesPage::onCopyNameAction);
connect(copyValueAction, &QAction::triggered, this, &ManageNamesPage::onCopyValueAction);
connect(renewNameAction, &QAction::triggered, this, &ManageNamesPage::onRenewNameAction);

connect(ui->tableView, &QTableView::customContextMenuRequested, this, &ManageNamesPage::contextualMenu);
ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
Expand Down Expand Up @@ -79,6 +82,20 @@ bool ManageNamesPage::eventFilter(QObject *object, QEvent *event)

void ManageNamesPage::selectionChanged()
{
// Enable/disable UI elements based on number of names selected.
QTableView *table = ui->tableView;
if (!table->selectionModel())
return;

QModelIndexList indexes = GUIUtil::getEntryData(ui->tableView, NameTableModel::Name);

bool singleNameSelected = indexes.size() == 1;
bool anyNamesSelected = indexes.size() >= 1;

// Context menu
copyNameAction->setEnabled(singleNameSelected);
copyValueAction->setEnabled(singleNameSelected);
renewNameAction->setEnabled(anyNamesSelected);
}

void ManageNamesPage::contextualMenu(const QPoint &point)
Expand All @@ -98,6 +115,54 @@ void ManageNamesPage::onCopyValueAction()
GUIUtil::copyEntryData(ui->tableView, NameTableModel::Value);
}

void ManageNamesPage::onRenewNameAction()
{
QModelIndexList indexes = GUIUtil::getEntryData(ui->tableView, NameTableModel::Name);

if (indexes.isEmpty())
return;

QString msg;
QString title;

if (indexes.size() == 1)
{
const QString &name = indexes.at(0).data(Qt::EditRole).toString();

msg = tr ("Are you sure you want to renew the name <b>%1</b>?")
.arg (GUIUtil::HtmlEscape (name));
title = tr ("Confirm name renewal");
}
else
{
msg = tr ("Are you sure you want to renew multiple names simultaneously? This will reveal common ownership of the renewed names (bad for anonymity).");
title = tr ("Confirm multiple name renewal");
}

QMessageBox::StandardButton res;
res = QMessageBox::question (this, title, msg,
QMessageBox::Yes | QMessageBox::Cancel,
QMessageBox::Cancel);
if (res != QMessageBox::Yes)
return;

WalletModel::UnlockContext ctx(walletModel->requestUnlock ());
if (!ctx.isValid ())
return;

for (QModelIndexList::iterator i = indexes.begin(); i != indexes.end(); i++)
{
const QString &name = i->data(Qt::EditRole).toString();

const QString err_msg = model->renew(name);
if (!err_msg.isEmpty() && err_msg != "ABORTED")
{
QMessageBox::critical(this, tr("Name renew error"), err_msg);
return;
}
}
}

void ManageNamesPage::exportClicked()
{
// CSV is currently the only supported format
Expand Down
4 changes: 4 additions & 0 deletions src/qt/managenamespage.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ class ManageNamesPage : public QWidget
WalletModel *walletModel;
QSortFilterProxyModel *proxyModel;
QMenu *contextMenu;
QAction *copyNameAction;
QAction *copyValueAction;
QAction *renewNameAction;

public Q_SLOTS:
void exportClicked();
Expand All @@ -51,6 +54,7 @@ private Q_SLOTS:

void onCopyNameAction();
void onCopyValueAction();
void onRenewNameAction();
};

#endif // MANAGENAMESPAGE_H
23 changes: 23 additions & 0 deletions src/qt/nametablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,26 @@ NameTableModel::emitDataChanged(int idx)
//emit
dataChanged(index(idx, 0), index(idx, columns.length()-1));
}

QString NameTableModel::renew(const QString &name) const
{
std::string strName = name.toStdString();
LogPrintf ("wallet attempting name_update: name=%s\n", strName);

UniValue params(UniValue::VOBJ);
params.pushKV ("name", strName);

std::string walletURI = ("/wallet/" + walletModel->getWalletName()).toStdString();

UniValue res;
try {
res = walletModel->node().executeRpc("name_update", params, walletURI);
}
catch (const UniValue& e) {
UniValue message = find_value( e, "message");
std::string errorStr = message.get_str();
LogPrintf ("name_update error: %s\n", errorStr);
return QString::fromStdString(errorStr);
}
return tr ("");
}
2 changes: 2 additions & 0 deletions src/qt/nametablemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class NameTableModel : public QAbstractTableModel
Qt::ItemFlags flags(const QModelIndex &index) const;
/*@}*/

QString renew(const QString &name) const;

private:
WalletModel *walletModel;
std::unique_ptr<interfaces::Handler> m_handler_transaction_changed;
Expand Down

0 comments on commit baa62b2

Please sign in to comment.