Auto detect testnet and mainnet ports
This commit is contained in:
100
src/settings.cpp
100
src/settings.cpp
@@ -2,9 +2,24 @@
|
||||
|
||||
#include "settings.h"
|
||||
|
||||
Settings::Settings()
|
||||
{
|
||||
refresh();
|
||||
Settings* Settings::instance = nullptr;
|
||||
|
||||
Settings* Settings::init() {
|
||||
if (instance != nullptr) return instance;
|
||||
|
||||
instance = new Settings();
|
||||
|
||||
// Load from settings first, because if they are redefined in the zcash.conf file,
|
||||
// we'll overwrite them.
|
||||
instance->loadFromSettings();
|
||||
// Overwrite if any are defined in the zcash.conf
|
||||
instance->loadFromFile();
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
Settings* Settings::getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
QString Settings::getHost() {
|
||||
@@ -25,43 +40,56 @@ QString Settings::getUsernamePassword() {
|
||||
return username % ":" % password;
|
||||
}
|
||||
|
||||
void Settings::refresh() {
|
||||
// First step is to try and load from the QT Settings
|
||||
void Settings::loadFromSettings() {
|
||||
// First step is to try and load from the QT Settings. These are loaded first, because
|
||||
// they could be overridden by whats in the zcash.conf, which will take precedence.
|
||||
QSettings s;
|
||||
|
||||
host = s.value("connection/host", "127.0.0.1" ).toString();
|
||||
port = s.value("connection/port", "8232" ).toString();
|
||||
username = s.value("connection/rpcuser", "" ).toString();
|
||||
password = s.value("connection/rpcpassword", "" ).toString();
|
||||
|
||||
if (username == "") {
|
||||
// Nothing in QT Settings, so try to read from file.
|
||||
QString zcashdconf = QStandardPaths::locate(QStandardPaths::HomeLocation, ".zcash/zcash.conf");
|
||||
if (zcashdconf.isNull()) {
|
||||
// No zcash file, just return with nothing
|
||||
return;
|
||||
}
|
||||
password = s.value("connection/rpcpassword", "" ).toString();
|
||||
}
|
||||
|
||||
QFile file(zcashdconf);
|
||||
if (!file.open(QIODevice::ReadOnly)) {
|
||||
qDebug() << file.errorString();
|
||||
return;
|
||||
}
|
||||
|
||||
QTextStream in(&file);
|
||||
|
||||
while (!in.atEnd()) {
|
||||
QString line = in.readLine();
|
||||
QStringList fields = line.split("=");
|
||||
|
||||
if (fields[0].trimmed().toLower() == "rpcuser") {
|
||||
username = fields[1].trimmed();
|
||||
}
|
||||
if (fields[0].trimmed().toLower() == "rpcpassword") {
|
||||
password = fields[1].trimmed();
|
||||
}
|
||||
}
|
||||
|
||||
file.close();
|
||||
void Settings::loadFromFile() {
|
||||
// Nothing in QT Settings, so try to read from file.
|
||||
QString zcashdconf = QStandardPaths::locate(QStandardPaths::HomeLocation, ".zcash/zcash.conf");
|
||||
if (zcashdconf.isNull()) {
|
||||
// No zcash file, just return with nothing
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
QFile file(zcashdconf);
|
||||
if (!file.open(QIODevice::ReadOnly)) {
|
||||
qDebug() << file.errorString();
|
||||
return;
|
||||
}
|
||||
|
||||
// If file was found, then setup some defaults
|
||||
overridePort = "8232";
|
||||
|
||||
QTextStream in(&file);
|
||||
|
||||
while (!in.atEnd()) {
|
||||
QString line = in.readLine();
|
||||
QStringList fields = line.split("=");
|
||||
|
||||
if (fields[0].trimmed().toLower() == "rpcuser") {
|
||||
username = fields[1].trimmed();
|
||||
}
|
||||
if (fields[0].trimmed().toLower() == "rpcpassword") {
|
||||
password = fields[1].trimmed();
|
||||
}
|
||||
if (fields[0].trimmed().toLower() == "rpcport") {
|
||||
overridePort = fields[1].trimmed();
|
||||
}
|
||||
if (fields[0].trimmed().toLower() == "testnet" &&
|
||||
fields[1].trimmed() == "1" &&
|
||||
overridePort.isEmpty()) {
|
||||
overridePort = "18232";
|
||||
}
|
||||
}
|
||||
|
||||
file.close();
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user