Refactor settings to work with windows

This commit is contained in:
adityapk
2018-10-17 22:31:41 -07:00
parent 87b8087a6f
commit 1fe42d93af
8 changed files with 155 additions and 80 deletions

View File

@@ -16,8 +16,6 @@ int main(int argc, char *argv[])
qApp->setFont(QFont("Ubuntu", 11, QFont::Normal, false)); qApp->setFont(QFont("Ubuntu", 11, QFont::Normal, false));
#endif #endif
QCoreApplication::setOrganizationName("adityapk");
QCoreApplication::setOrganizationDomain("adityapk.com");
QCoreApplication::setApplicationName("zcash-qt-wallet"); QCoreApplication::setApplicationName("zcash-qt-wallet");
Settings::init(); Settings::init();

View File

@@ -73,25 +73,44 @@ MainWindow::MainWindow(QWidget *parent) :
QIntValidator validator(0, 65535); QIntValidator validator(0, 65535);
settings.port->setValidator(&validator); settings.port->setValidator(&validator);
// Load previous values into the dialog
settings.hostname ->setText(Settings::getInstance()->getHost()); // If values are coming from zcash.conf, then disable all the fields
settings.port ->setText(Settings::getInstance()->getPort()); auto zcashConfLocation = Settings::getInstance()->getZcashdConfLocation();
settings.rpcuser ->setText(Settings::getInstance()->getUsernamePassword().split(":")[0]); if (!zcashConfLocation.isEmpty()) {
settings.rpcpassword->setText(Settings::getInstance()->getUsernamePassword().split(":")[1]); settings.confMsg->setText("Values are configured from\n" + zcashConfLocation);
settings.hostname->setEnabled(false);
settings.port->setEnabled(false);
settings.rpcuser->setEnabled(false);
settings.rpcpassword->setEnabled(false);
}
else {
settings.hostname->setEnabled(true);
settings.port->setEnabled(true);
settings.rpcuser->setEnabled(true);
settings.rpcpassword->setEnabled(true);
// Load previous values into the dialog
settings.hostname->setText(Settings::getInstance()->getHost());
settings.port->setText(Settings::getInstance()->getPort());
settings.rpcuser->setText(Settings::getInstance()->getUsernamePassword().split(":")[0]);
settings.rpcpassword->setText(Settings::getInstance()->getUsernamePassword().split(":")[1]);
}
if (settingsDialog.exec() == QDialog::Accepted) { if (settingsDialog.exec() == QDialog::Accepted) {
// Save settings if (zcashConfLocation.isEmpty()) {
QSettings s; // Save settings
s.setValue("connection/host", settings.hostname->text()); QSettings s;
s.setValue("connection/port", settings.port->text()); s.setValue("connection/host", settings.hostname->text());
s.setValue("connection/rpcuser", settings.rpcuser->text()); s.setValue("connection/port", settings.port->text());
s.setValue("connection/rpcpassword", settings.rpcpassword->text()); s.setValue("connection/rpcuser", settings.rpcuser->text());
s.setValue("connection/rpcpassword", settings.rpcpassword->text());
s.sync(); s.sync();
// Then refresh everything. // Then refresh everything.
this->rpc->reloadConnectionInfo(); this->rpc->reloadConnectionInfo();
this->rpc->refresh(); this->rpc->refresh();
}
}; };
}); });

View File

@@ -15,6 +15,7 @@
#include <QPainter> #include <QPainter>
#include <QMovie> #include <QMovie>
#include <QPair> #include <QPair>
#include <QDir>
#include <QDateTime> #include <QDateTime>
#include <QTimer> #include <QTimer>
#include <QSettings> #include <QSettings>

View File

@@ -231,11 +231,18 @@ void RPC::handleConnectionError(const QString& error) {
% "rpcpassword=<somepassword>\n" % "rpcpassword=<somepassword>\n"
% "\nIf you're connecting to a remote note, you can change the username/password in the " % "\nIf you're connecting to a remote note, you can change the username/password in the "
% "File->Settings menu."; % "File->Settings menu.";
} else if (error.contains("connection", Qt::CaseInsensitive)) { } else if (error.contains("connection refused", Qt::CaseInsensitive)) {
explanation = QString() auto confLocation = Settings::getInstance()->getZcashdConfLocation();
% "\n\nThis is most likely because we couldn't connect to zcashd. Is zcashd running and " if (confLocation.isEmpty()) {
% "accepting connections from this machine? \nIf you need to change the host/port, you can set that in the " explanation = QString()
% "File->Settings menu."; % "\n\nA zcash.conf was not found on this machine. If you are connecting to a remote/non-standard node "
% "please set the host/port and user/password in the File->Settings menu.";
}
else {
explanation = QString()
% "\n\nA zcash.conf was found at\n" % confLocation
% "\nbut we can't connect to zcashd. Is rpcuser=<user> and rpcpassword=<pass> set in the zcash.conf file?";
}
} else if (error.contains("bad request", Qt::CaseInsensitive)) { } else if (error.contains("bad request", Qt::CaseInsensitive)) {
explanation = QString() explanation = QString()
% "\n\nThis is most likely an internal error. Are you using zcashd v2.0 or higher? You might " % "\n\nThis is most likely an internal error. Are you using zcashd v2.0 or higher? You might "

View File

@@ -4,18 +4,38 @@
Settings* Settings::instance = nullptr; Settings* Settings::instance = nullptr;
Settings::~Settings() {
delete defaults;
delete zcashconf;
delete uisettings;
}
Settings* Settings::init() { Settings* Settings::init() {
if (instance != nullptr) return instance; if (instance != nullptr) return instance;
instance = new Settings(); instance = new Settings();
// Load from settings first, because if they are redefined in the zcash.conf file, // There are 3 possible configurations
// we'll overwrite them. // 1. The defaults
instance->loadFromSettings(); instance->defaults = new Config{ "127.0.0.1", "8232", "", "" };
#ifdef Q_OS_LINUX
// Overwrite if any are defined in the zcash.conf // 2. From the UI settings
instance->loadFromFile(); auto settingsFound = instance->loadFromSettings();
#endif
// 3. From the zcash.conf file
auto confFound = instance->loadFromFile();
// zcash.conf (#3) is first priority if it exists
if (confFound) {
instance->currentConfig = instance->zcashconf;
}
else if (settingsFound) {
instance->currentConfig = instance->uisettings;
}
else {
instance->currentConfig = instance->defaults;
}
return instance; return instance;
} }
@@ -23,75 +43,92 @@ Settings* Settings::getInstance() {
return instance; return instance;
} }
QString Settings::getHost() { QString Settings::getHost() {
if (host.isNull() || host == "") return "127.0.0.1"; return currentConfig->host;
return host;
} }
QString Settings::getPort() { QString Settings::getPort() {
// If the override port is set, we'll always return it return currentConfig->port;
if (!overridePort.isEmpty()) return overridePort;
if (port.isNull() || port == "") return "8232";
return port;
} }
QString Settings::getUsernamePassword() { QString Settings::getUsernamePassword() {
return username % ":" % password; return currentConfig->rpcuser % ":" % currentConfig->rpcpassword;
} }
void Settings::loadFromSettings() { bool Settings::loadFromSettings() {
// First step is to try and load from the QT Settings. These are loaded first, because delete uisettings;
// they could be overridden by whats in the zcash.conf, which will take precedence.
// Load from the QT Settings.
QSettings s; QSettings s;
host = s.value("connection/host", "127.0.0.1" ).toString(); auto host = s.value("connection/host").toString();
port = s.value("connection/port", "8232" ).toString(); auto port = s.value("connection/port").toString();
username = s.value("connection/rpcuser", "" ).toString(); auto username = s.value("connection/rpcuser").toString();
password = s.value("connection/rpcpassword", "" ).toString(); auto password = s.value("connection/rpcpassword").toString();
uisettings = new Config{host, port, username, password};
if (username.isEmpty()) return false;
return true;
} }
void Settings::loadFromFile() { bool Settings::loadFromFile() {
// Nothing in QT Settings, so try to read from file. delete zcashconf;
QString zcashdconf = QStandardPaths::locate(QStandardPaths::HomeLocation, ".zcash/zcash.conf");
if (zcashdconf.isNull()) { #ifdef Q_OS_LINUX
confLocation = QStandardPaths::locate(QStandardPaths::HomeLocation, ".zcash/zcash.conf");
#else
confLocation = QStandardPaths::locate(QStandardPaths::AppDataLocation, "../Zcash/zcash.conf");
#endif
confLocation = QDir::cleanPath(confLocation);
if (confLocation.isNull()) {
// No zcash file, just return with nothing // No zcash file, just return with nothing
return; return false;
} }
QFile file(zcashdconf); QFile file(confLocation);
if (!file.open(QIODevice::ReadOnly)) { if (!file.open(QIODevice::ReadOnly)) {
qDebug() << file.errorString(); qDebug() << file.errorString();
return; return false;
} }
QTextStream in(&file); QTextStream in(&file);
zcashconf = new Config();
zcashconf->host = defaults->host;
while (!in.atEnd()) { while (!in.atEnd()) {
QString line = in.readLine(); QString line = in.readLine();
QStringList fields = line.split("="); auto s = line.indexOf("=");
QString name = line.left(s).trimmed().toLower();
QString value = line.right(line.length() - s - 1).trimmed();
if (fields[0].trimmed().toLower() == "rpcuser") { if (name == "rpcuser") {
fields.removeFirst(); zcashconf->rpcuser = value;
username = fields.join("").trimmed();
} }
if (fields[0].trimmed().toLower() == "rpcpassword") { if (name == "rpcpassword") {
fields.removeFirst(); zcashconf->rpcpassword = value;
password = fields.join("").trimmed();
} }
if (fields[0].trimmed().toLower() == "rpcport") { if (name == "rpcport") {
overridePort = fields[1].trimmed(); zcashconf->port = value;
} }
if (fields[0].trimmed().toLower() == "testnet" && if (name == "testnet" &&
fields[1].trimmed() == "1" && value == "1" &&
overridePort.isEmpty()) { zcashconf->port.isEmpty()) {
overridePort = "18232"; zcashconf->port = "18232";
} }
} }
// If rpcport is not in the file, and it was not set by the testnet=1 flag, then go to default
if (zcashconf->port.isEmpty()) zcashconf->port = defaults->port;
file.close(); file.close();
return true;
} }
bool Settings::isTestnet() { bool Settings::isTestnet() {

View File

@@ -3,6 +3,13 @@
#include "precompiled.h" #include "precompiled.h"
struct Config {
QString host;
QString port;
QString rpcuser;
QString rpcpassword;
};
class Settings class Settings
{ {
public: public:
@@ -13,11 +20,9 @@ public:
QString getHost(); QString getHost();
QString getPort(); QString getPort();
void setDefaultPort(int port) {overridePort = QString::number(port);}
double fees() { return 0.0001; } double fees() { return 0.0001; }
void loadFromSettings(); bool loadFromSettings();
void loadFromFile(); bool loadFromFile();
bool isTestnet(); bool isTestnet();
void setTestnet(bool isTestnet); void setTestnet(bool isTestnet);
@@ -25,18 +30,22 @@ public:
bool isSyncing(); bool isSyncing();
void setSyncing(bool syncing); void setSyncing(bool syncing);
const QString& getZcashdConfLocation() { return confLocation; }
private: private:
// This class can only be accessed through Settings::getInstance() // This class can only be accessed through Settings::getInstance()
Settings() = default; Settings() = default;
~Settings();
static Settings* instance; static Settings* instance;
QString host; Config* currentConfig;
QString port;
QString username;
QString password;
QString overridePort; Config* defaults = nullptr;
Config* zcashconf = nullptr;
Config* uisettings = nullptr;
QString confLocation;
bool _isTestnet = false; bool _isTestnet = false;
bool _isSyncing = false; bool _isSyncing = false;

View File

@@ -111,9 +111,12 @@
</widget> </widget>
</item> </item>
<item row="0" column="0"> <item row="0" column="0">
<widget class="QLabel" name="label_5"> <widget class="QLabel" name="confMsg">
<property name="text"> <property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Values configured in ~/.zcash/zcash.conf &lt;br/&gt;will overwrite these values&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string> <string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;zcash msg&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="textInteractionFlags">
<set>Qt::LinksAccessibleByMouse|Qt::TextSelectableByMouse</set>
</property> </property>
</widget> </widget>
</item> </item>

View File

@@ -43,7 +43,7 @@ public:
QLabel *label_4; QLabel *label_4;
QLineEdit *rpcpassword; QLineEdit *rpcpassword;
QLabel *label_2; QLabel *label_2;
QLabel *label_5; QLabel *confMsg;
QFrame *line; QFrame *line;
QSpacerItem *verticalSpacer; QSpacerItem *verticalSpacer;
QDialogButtonBox *buttonBox; QDialogButtonBox *buttonBox;
@@ -113,10 +113,11 @@ public:
gridLayout->addWidget(label_2, 4, 0, 1, 1); gridLayout->addWidget(label_2, 4, 0, 1, 1);
label_5 = new QLabel(groupBox); confMsg = new QLabel(groupBox);
label_5->setObjectName(QStringLiteral("label_5")); confMsg->setObjectName(QStringLiteral("confMsg"));
confMsg->setTextInteractionFlags(Qt::LinksAccessibleByMouse|Qt::TextSelectableByMouse);
gridLayout->addWidget(label_5, 0, 0, 1, 1); gridLayout->addWidget(confMsg, 0, 0, 1, 1);
line = new QFrame(groupBox); line = new QFrame(groupBox);
line->setObjectName(QStringLiteral("line")); line->setObjectName(QStringLiteral("line"));
@@ -171,7 +172,7 @@ public:
port->setPlaceholderText(QApplication::translate("Settings", "8232", nullptr)); port->setPlaceholderText(QApplication::translate("Settings", "8232", nullptr));
label_4->setText(QApplication::translate("Settings", "RPC Password", nullptr)); label_4->setText(QApplication::translate("Settings", "RPC Password", nullptr));
label_2->setText(QApplication::translate("Settings", "Port", nullptr)); label_2->setText(QApplication::translate("Settings", "Port", nullptr));
label_5->setText(QApplication::translate("Settings", "<html><head/><body><p>Values configured in ~/.zcash/zcash.conf <br/>will overwrite these values</p></body></html>", nullptr)); confMsg->setText(QApplication::translate("Settings", "<html><head/><body><p>zcash msg</p></body></html>", nullptr));
tabWidget->setTabText(tabWidget->indexOf(tab), QApplication::translate("Settings", "Connection", nullptr)); tabWidget->setTabText(tabWidget->indexOf(tab), QApplication::translate("Settings", "Connection", nullptr));
} // retranslateUi } // retranslateUi