From 213d92b88a48a09fcc7edf96191b3ed1d2a07baf Mon Sep 17 00:00:00 2001 From: Daniel Kraft Date: Sun, 7 Jul 2013 18:41:13 +0200 Subject: [PATCH] Add UI to test namecoin connection. Add a test button to namecoin UI settings, which tries out the connection and reports its result back. Also use namecoin.conf as config file to load default RPC user/pass combination from. --- src/bitmessageqt/__init__.py | 13 ++++++++++ src/bitmessageqt/settings.py | 12 +++++++-- src/bitmessageqt/settings.ui | 16 +++++++++++- src/namecoin.py | 48 ++++++++++++++++++++++++++++++------ 4 files changed, 79 insertions(+), 10 deletions(-) diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index 88512a21..ba967bb7 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -2759,6 +2759,8 @@ class settingsDialog(QtGui.QDialog): shared.config.get('bitmessagesettings', 'namecoinrpcuser'))) self.ui.lineEditNamecoinPassword.setText(str( shared.config.get('bitmessagesettings', 'namecoinrpcpassword'))) + QtCore.QObject.connect(self.ui.pushButtonNamecoinTest, QtCore.SIGNAL( + "clicked()"), self.click_pushButtonNamecoinTest) #'System' tab removed for now. """try: @@ -2797,6 +2799,17 @@ class settingsDialog(QtGui.QDialog): self.ui.lineEditSocksPassword.setEnabled(True) self.ui.lineEditTCPPort.setEnabled(False) + # Test the namecoin settings specified in the settings dialog. + def click_pushButtonNamecoinTest(self): + options = {} + options["host"] = self.ui.lineEditNamecoinHost.text() + options["port"] = self.ui.lineEditNamecoinPort.text() + options["user"] = self.ui.lineEditNamecoinUser.text() + options["password"] = self.ui.lineEditNamecoinPassword.text() + nc = namecoinConnection(options) + res = nc.test() + self.ui.labelNamecoinTestResult.setText(res) + class SpecialAddressBehaviorDialog(QtGui.QDialog): diff --git a/src/bitmessageqt/settings.py b/src/bitmessageqt/settings.py index d11859b2..2f93ce88 100644 --- a/src/bitmessageqt/settings.py +++ b/src/bitmessageqt/settings.py @@ -2,7 +2,7 @@ # Form implementation generated from reading ui file 'settings.ui' # -# Created: Sun Jul 7 17:25:43 2013 +# Created: Sun Jul 7 18:40:01 2013 # by: PyQt4 UI code generator 4.9.3 # # WARNING! All changes made in this file will be lost! @@ -242,7 +242,7 @@ class Ui_settingsDialog(object): self.lineEditNamecoinPort.setObjectName(_fromUtf8("lineEditNamecoinPort")) self.gridLayout_8.addWidget(self.lineEditNamecoinPort, 2, 2, 1, 1) spacerItem10 = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding) - self.gridLayout_8.addItem(spacerItem10, 5, 1, 1, 1) + self.gridLayout_8.addItem(spacerItem10, 7, 1, 1, 1) spacerItem11 = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum) self.gridLayout_8.addItem(spacerItem11, 3, 0, 1, 1) self.label_19 = QtGui.QLabel(self.tabNamecoin) @@ -263,6 +263,13 @@ class Ui_settingsDialog(object): self.lineEditNamecoinPassword.setEchoMode(QtGui.QLineEdit.Password) self.lineEditNamecoinPassword.setObjectName(_fromUtf8("lineEditNamecoinPassword")) self.gridLayout_8.addWidget(self.lineEditNamecoinPassword, 4, 2, 1, 1) + self.labelNamecoinTestResult = QtGui.QLabel(self.tabNamecoin) + self.labelNamecoinTestResult.setText(_fromUtf8("")) + self.labelNamecoinTestResult.setObjectName(_fromUtf8("labelNamecoinTestResult")) + self.gridLayout_8.addWidget(self.labelNamecoinTestResult, 6, 0, 1, 2) + self.pushButtonNamecoinTest = QtGui.QPushButton(self.tabNamecoin) + self.pushButtonNamecoinTest.setObjectName(_fromUtf8("pushButtonNamecoinTest")) + self.gridLayout_8.addWidget(self.pushButtonNamecoinTest, 6, 2, 1, 1) self.tabWidgetSettings.addTab(self.tabNamecoin, _fromUtf8("")) self.gridLayout.addWidget(self.tabWidgetSettings, 0, 0, 1, 1) @@ -323,5 +330,6 @@ class Ui_settingsDialog(object): self.label_18.setText(QtGui.QApplication.translate("settingsDialog", "Port:", None, QtGui.QApplication.UnicodeUTF8)) self.label_19.setText(QtGui.QApplication.translate("settingsDialog", "Username:", None, QtGui.QApplication.UnicodeUTF8)) self.label_20.setText(QtGui.QApplication.translate("settingsDialog", "Password:", None, QtGui.QApplication.UnicodeUTF8)) + self.pushButtonNamecoinTest.setText(QtGui.QApplication.translate("settingsDialog", "Test", None, QtGui.QApplication.UnicodeUTF8)) self.tabWidgetSettings.setTabText(self.tabWidgetSettings.indexOf(self.tabNamecoin), QtGui.QApplication.translate("settingsDialog", "Namecoin integration", None, QtGui.QApplication.UnicodeUTF8)) diff --git a/src/bitmessageqt/settings.ui b/src/bitmessageqt/settings.ui index 24132635..4c1e14eb 100644 --- a/src/bitmessageqt/settings.ui +++ b/src/bitmessageqt/settings.ui @@ -558,7 +558,7 @@ - + Qt::Vertical @@ -630,6 +630,20 @@ + + + + + + + + + + + Test + + + diff --git a/src/namecoin.py b/src/namecoin.py index 184545f9..9e98c2d0 100644 --- a/src/namecoin.py +++ b/src/namecoin.py @@ -44,12 +44,23 @@ class namecoinConnection (object): bufsize = 4096 queryid = 1 - def __init__ (self): - ensureNamecoinOptions () - self.user = shared.config.get (configSection, "namecoinrpcuser") - self.password = shared.config.get (configSection, "namecoinrpcpassword") - self.host = shared.config.get (configSection, "namecoinrpchost") - self.port = shared.config.get (configSection, "namecoinrpcport") + # Initialise. If options are given, take the connection settings from + # them instead of loading from the configs. This can be used to test + # currently entered connection settings in the config dialog without + # actually changing the values (yet). + def __init__ (self, options = None): + if options is None: + ensureNamecoinOptions () + self.host = shared.config.get (configSection, "namecoinrpchost") + self.port = shared.config.get (configSection, "namecoinrpcport") + self.user = shared.config.get (configSection, "namecoinrpcuser") + self.password = shared.config.get (configSection, + "namecoinrpcpassword") + else: + self.host = options["host"] + self.port = options["port"] + self.user = options["user"] + self.password = options["password"] # Query for the bitmessage address corresponding to the given identity # string. If it doesn't contain a slash, id/ is prepended. We return @@ -83,6 +94,29 @@ class namecoinConnection (object): return ("The name '%s' has no associated Bitmessage address." % string, None) + # Test the connection settings. This routine tries to query a "getinfo" + # command, and builds either an error message or a success message with + # some info from it. + def test (self): + try: + res = self.callRPC ("getinfo", []) + vers = res["version"] + + v3 = vers % 100 + vers = vers / 100 + v2 = vers % 100 + vers = vers / 100 + v1 = vers + if v3 == 0: + versStr = "0.%d.%d" % (v1, v2) + else: + versStr = "0.%d.%d.%d" % (v1, v2, v3) + + return "Success! Namecoind version %s running." % versStr + + except: + return "The connection to namecoind failed." + # Helper routine that actually performs an JSON RPC call. def callRPC (self, method, params): data = {"method": method, "params": params, "id": self.queryid} @@ -181,7 +215,7 @@ def ensureNamecoinOptions (): if (not hasUser) or (not hasPass): try: nmcFolder = lookupNamecoinFolder () - nmcConfig = nmcFolder + "bitcoin.conf" + nmcConfig = nmcFolder + "namecoin.conf" nmc = open (nmcConfig, "r") while True: