Skip to content

Commit

Permalink
Namecoin: Add Configure Name dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyRand committed Oct 6, 2018
1 parent ddc903a commit 5924dc3
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 4 deletions.
128 changes: 128 additions & 0 deletions electrum_nmc/gui/qt/configure_name_dialog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#!/usr/bin/env python
#
# Electrum-NMC - lightweight Namecoin client
# Copyright (C) 2012-2018 Namecoin Developers, Electrum Developers
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

from electrum_nmc.bitcoin import TYPE_ADDRESS
from electrum_nmc.i18n import _
from electrum_nmc.names import format_name_identifier

from .paytoedit import PayToEdit

dialogs = [] # Otherwise python randomly garbage collects the dialogs...


def show_configure_name(identifier, value, parent):
d = ConfigureNameDialog(identifier, value, parent)

dialogs.append(d)
d.show()


class ConfigureNameDialog(QDialog):
def __init__(self, identifier, value, parent):
# We want to be a top-level window
QDialog.__init__(self, parent=None)

self.main_window = parent

self.setMinimumWidth(545)
self.setMinimumHeight(245)
self.setWindowTitle(_("Configure Name"))

form_layout = QFormLayout()

self.identifier = identifier
formatted_name = format_name_identifier(identifier)
form_layout.addRow(QLabel(formatted_name))

self.dataEdit = QLineEdit()
# TODO: support non-ASCII encodings
self.dataEdit.setText(value.decode('ascii'))
form_layout.addRow(_("Data:"), self.dataEdit)

self.transferTo = PayToEdit(self.main_window)
form_layout.addRow(_("Transfer to:"), self.transferTo)

form = QWidget()
form.setLayout(form_layout)

self.buttons_box = QDialogButtonBox()
self.buttons_box.setStandardButtons(QDialogButtonBox.Cancel|QDialogButtonBox.Ok)

buttons_hbox = QHBoxLayout()
buttons_hbox.addStretch()
buttons_hbox.addWidget(self.buttons_box)
buttons = QWidget()
buttons.setLayout(buttons_hbox)

vbox = QVBoxLayout()
vbox.addWidget(form)
vbox.addWidget(buttons)
self.setLayout(vbox)

self.buttons_box.accepted.connect(self.accept)
self.buttons_box.rejected.connect(self.reject)

# TODO: handle non-ASCII encodings
self.accepted.connect(lambda: self.update_and_broadcast(self.identifier, self.dataEdit.text().encode('ascii'), self.transferTo))

def update_and_broadcast(self, identifier, value, transfer_to):
if transfer_to.toPlainText() == "":
# User left the recipient blank, so this isn't a transfer.
recipient_address = None
else:
# The user entered something into the recipient text box.

recipient = transfer_to.get_recipient()

if recipient is None:
recipient_type, recipient_address = None, transfer_to.toPlainText()
else:
recipient_type, recipient_address = recipient

if recipient_type != TYPE_ADDRESS:
self.main_window.show_error(_("Invalid address ") + recipient_address)
return

name_update = self.main_window.console.namespace.get('name_update')
broadcast = self.main_window.console.namespace.get('broadcast')

# TODO: support non-ASCII encodings
tx = name_update(identifier.decode('ascii'), value.decode('ascii'), recipient_address)['hex']

status, msg = broadcast(tx)
if not status:
formatted_name = format_name_identifier(identifier)
self.main_window.show_error(_("Error broadcasting update for ") + formatted_name + ": " + str(msg))
return

# As far as I can tell, we don't need to explicitly add the transaction
# to the wallet, because we're only issuing a single transaction, so
# there's not much risk of accidental double-spends from subsequent
# transactions.

4 changes: 2 additions & 2 deletions electrum_nmc/gui/qt/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -3270,14 +3270,14 @@ def create_names_tab(self):
self.names_configure_button.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
self.names_configure_button.setMinimumSize(150, 0)
self.names_configure_button.setToolTip(_('Change the value of the selected name or transfer ownership'))
self.names_configure_button.clicked.connect(l.configure_selected_item)
self.names_renew_button = QPushButton(_('Renew Name'))
self.names_renew_button.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
self.names_renew_button.setMinimumSize(150, 0)
self.names_renew_button.setToolTip(_('Renew the selected name with its current value (nothing will happen if the name was updated in the last 12 blocks)'))
self.names_renew_button.clicked.connect(l.renew_selected_items)

# TODO: enable name configure button
#self.names_actions_hbox.addWidget(self.names_configure_button)
self.names_actions_hbox.addWidget(self.names_configure_button)
self.names_actions_hbox.addWidget(self.names_renew_button)

self.names_actions = QWidget()
Expand Down
15 changes: 13 additions & 2 deletions electrum_nmc/gui/qt/uno_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from electrum_nmc.i18n import _
from electrum_nmc.names import format_name_identifier, format_name_value, name_expires_in

from .configure_name_dialog import show_configure_name
from .util import *
from .utxo_list import UTXOList

Expand Down Expand Up @@ -101,8 +102,7 @@ def create_menu(self, position):
txid = selected[0].split(':')[0]
tx = self.wallet.transactions.get(txid)
if tx:
# TODO: implement Configure
#menu.addAction(_("Configure"), lambda: self.parent.show_transaction(tx))
menu.addAction(_("Configure"), lambda: self.configure_selected_item())
menu.addAction(_("Transaction Details"), lambda: self.parent.show_transaction(tx))

menu.exec_(self.viewport().mapToGlobal(position))
Expand Down Expand Up @@ -145,3 +145,14 @@ def renew_selected_items(self):
self.parent.show_error(_("Error adding renewal for ") + formatted_name + _(" to wallet"))
continue

def configure_selected_item(self):
selected = [(x.data(0, Qt.UserRole + USER_ROLE_NAME), x.data(0, Qt.UserRole + USER_ROLE_VALUE)) for x in self.selectedItems()]
if not selected:
return
if len(selected) != 1:
return

identifier, initial_value = selected[0]

show_configure_name(identifier, initial_value, self.parent)

0 comments on commit 5924dc3

Please sign in to comment.