Skip to content

Commit ea38e91

Browse files
committed
Fixing namecoin button again (broken in c7d3784):
* separate method MyForm.resetNamecoinConnection() - sets MyForm.namecoin to fresh instance of namecoin.namecoinConnection, tests it and shows or hides "Fetch Namecoin ID" button; * that method is called when MyForm initializes and when settingsDialog instance is accepted; * namecoin.namecoinConnection.query() checks found address and always prepends it with display name, if query result doesn't contain "name" field it will be the query string.
1 parent 8ad064d commit ea38e91

File tree

2 files changed

+59
-43
lines changed

2 files changed

+59
-43
lines changed

src/bitmessageqt/__init__.py

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -798,26 +798,14 @@ def __init__(self, parent=None):
798798
"valueChanged(int)"), self.updateTTL)
799799

800800
self.initSettings()
801-
802-
namecoin.ensureNamecoinOptions()
803-
self.namecoin = namecoin.namecoinConnection()
804-
805-
# Check to see whether we can connect to namecoin.
806-
# Hide the 'Fetch Namecoin ID' button if we can't.
807-
if BMConfigParser().safeGetBoolean(
808-
'bitmessagesettings', 'dontconnect'
809-
) or self.namecoin.test()[0] == 'failed':
810-
logger.warning(
811-
'There was a problem testing for a Namecoin daemon. Hiding the'
812-
' Fetch Namecoin ID button')
813-
self.ui.pushButtonFetchNamecoinID.hide()
801+
self.resetNamecoinConnection()
814802

815803
def updateTTL(self, sliderPosition):
816804
TTL = int(sliderPosition ** 3.199 + 3600)
817805
self.updateHumanFriendlyTTLDescription(TTL)
818806
BMConfigParser().set('bitmessagesettings', 'ttl', str(TTL))
819807
BMConfigParser().save()
820-
808+
821809
def updateHumanFriendlyTTLDescription(self, TTL):
822810
numberOfHours = int(round(TTL / (60*60)))
823811
font = QtGui.QFont()
@@ -2160,9 +2148,8 @@ def click_pushButtonLoadFromAddressBook(self):
21602148
))
21612149

21622150
def click_pushButtonFetchNamecoinID(self):
2163-
nc = namecoinConnection()
21642151
identities = str(self.ui.lineEditTo.text().toUtf8()).split(";")
2165-
err, addr = nc.query(identities[-1].strip())
2152+
err, addr = self.namecoin.query(identities[-1].strip())
21662153
if err is not None:
21672154
self.updateStatusBar(
21682155
_translate("MainWindow", "Error: %1").arg(err))
@@ -2481,7 +2468,8 @@ def click_actionSettings(self):
24812468
self.settingsDialogInstance.ui.lineEditNamecoinUser.text()))
24822469
BMConfigParser().set('bitmessagesettings', 'namecoinrpcpassword', str(
24832470
self.settingsDialogInstance.ui.lineEditNamecoinPassword.text()))
2484-
2471+
self.resetNamecoinConnection()
2472+
24852473
# Demanded difficulty tab
24862474
if float(self.settingsDialogInstance.ui.lineEditTotalDifficulty.text()) >= 1:
24872475
BMConfigParser().set('bitmessagesettings', 'defaultnoncetrialsperbyte', str(int(float(
@@ -4129,6 +4117,22 @@ def updateStatusBar(self, data):
41294117
else:
41304118
self.statusbar.showMessage(message, 10000)
41314119

4120+
def resetNamecoinConnection(self):
4121+
namecoin.ensureNamecoinOptions()
4122+
self.namecoin = namecoin.namecoinConnection()
4123+
4124+
# Check to see whether we can connect to namecoin.
4125+
# Hide the 'Fetch Namecoin ID' button if we can't.
4126+
if BMConfigParser().safeGetBoolean(
4127+
'bitmessagesettings', 'dontconnect'
4128+
) or self.namecoin.test()[0] == 'failed':
4129+
logger.warning(
4130+
'There was a problem testing for a Namecoin daemon. Hiding the'
4131+
' Fetch Namecoin ID button')
4132+
self.ui.pushButtonFetchNamecoinID.hide()
4133+
else:
4134+
self.ui.pushButtonFetchNamecoinID.show()
4135+
41324136
def initSettings(self):
41334137
QtCore.QCoreApplication.setOrganizationName("PyBitmessage")
41344138
QtCore.QCoreApplication.setOrganizationDomain("bitmessage.org")
@@ -4344,7 +4348,7 @@ def getNamecoinType(self):
43444348
def namecoinTypeChanged(self, checked):
43454349
nmctype = self.getNamecoinType()
43464350
assert nmctype == "namecoind" or nmctype == "nmcontrol"
4347-
4351+
43484352
isNamecoind = (nmctype == "namecoind")
43494353
self.ui.lineEditNamecoinUser.setEnabled(isNamecoind)
43504354
self.ui.labelNamecoinUser.setEnabled(isNamecoind)
@@ -4356,23 +4360,21 @@ def namecoinTypeChanged(self, checked):
43564360
else:
43574361
self.ui.lineEditNamecoinPort.setText("9000")
43584362

4359-
# Test the namecoin settings specified in the settings dialog.
43604363
def click_pushButtonNamecoinTest(self):
4364+
"""Test the namecoin settings specified in the settings dialog."""
43614365
self.ui.labelNamecoinTestResult.setText(_translate(
4362-
"MainWindow", "Testing..."))
4366+
"MainWindow", "Testing..."))
43634367
options = {}
43644368
options["type"] = self.getNamecoinType()
43654369
options["host"] = str(self.ui.lineEditNamecoinHost.text().toUtf8())
43664370
options["port"] = str(self.ui.lineEditNamecoinPort.text().toUtf8())
43674371
options["user"] = str(self.ui.lineEditNamecoinUser.text().toUtf8())
43684372
options["password"] = str(self.ui.lineEditNamecoinPassword.text().toUtf8())
4369-
nc = namecoinConnection(options)
4370-
response = nc.test()
4371-
responseStatus = response[0]
4372-
responseText = response[1]
4373-
self.ui.labelNamecoinTestResult.setText(responseText)
4374-
if responseStatus== 'success':
4375-
self.parent.ui.pushButtonFetchNamecoinID.show()
4373+
nc = namecoin.namecoinConnection(options)
4374+
status, text = nc.test()
4375+
self.ui.labelNamecoinTestResult.setText(text)
4376+
if status == 'success':
4377+
self.parent.namecoin = nc
43764378

43774379

43784380
# In order for the time columns on the Inbox and Sent tabs to be sorted

src/namecoin.py

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import socket
3535
import sys
3636

37+
from addresses import decodeAddress
3738
from debug import logger
3839
import defaults
3940
import tr # translate
@@ -102,7 +103,10 @@ def query(self, string):
102103
"""
103104
slashPos = string.find("/")
104105
if slashPos < 0:
106+
display_name = string
105107
string = "id/" + string
108+
else:
109+
display_name = string.split("/")[1]
106110

107111
try:
108112
if self.nmctype == "namecoind":
@@ -112,7 +116,9 @@ def query(self, string):
112116
res = self.callRPC("data", ["getValue", string])
113117
res = res["reply"]
114118
if not res:
115-
return (tr._translate("MainWindow", 'The name %1 was not found.').arg(unicode(string)), None)
119+
return (tr._translate(
120+
"MainWindow", 'The name %1 was not found.'
121+
).arg(unicode(string)), None)
116122
else:
117123
assert False
118124
except RPCError as exc:
@@ -121,29 +127,37 @@ def query(self, string):
121127
errmsg = exc.error["message"]
122128
else:
123129
errmsg = exc.error
124-
return (tr._translate("MainWindow", 'The namecoin query failed (%1)').arg(unicode(errmsg)), None)
130+
return (tr._translate(
131+
"MainWindow", 'The namecoin query failed (%1)'
132+
).arg(unicode(errmsg)), None)
133+
except AssertionError:
134+
return (tr._translate(
135+
"MainWindow", 'Unknown namecoin interface type: %1'
136+
).arg(unicode(self.nmctype)), None)
125137
except Exception:
126138
logger.exception("Namecoin query exception")
127-
return (tr._translate("MainWindow", 'The namecoin query failed.'), None)
139+
return (tr._translate(
140+
"MainWindow", 'The namecoin query failed.'), None)
128141

129142
try:
130-
val = json.loads(res)
131-
except:
132-
logger.exception("Namecoin query json exception")
133-
return (tr._translate("MainWindow", 'The name %1 has no valid JSON data.').arg(unicode(string)), None)
143+
res = json.loads(res)
144+
except ValueError:
145+
pass
146+
else:
147+
try:
148+
display_name = res["name"]
149+
except KeyError:
150+
pass
151+
res = res.get("bitmessage")
134152

135-
if "bitmessage" in val:
136-
if "name" in val:
137-
ret = "%s <%s>" % (val["name"], val["bitmessage"])
138-
else:
139-
ret = val["bitmessage"]
140-
return (None, ret)
153+
valid = decodeAddress(res)[0] == 'success'
141154
return (
155+
None, "%s <%s>" % (display_name, res)
156+
) if valid else (
142157
tr._translate(
143158
"MainWindow",
144-
'The name %1 has no associated Bitmessage address.').arg(
145-
unicode(string)),
146-
None)
159+
'The name %1 has no associated Bitmessage address.'
160+
).arg(unicode(string)), None)
147161

148162
def test(self):
149163
"""

0 commit comments

Comments
 (0)