lbrycrd/src/qt/optionsmodel.cpp

324 lines
9.9 KiB
C++
Raw Normal View History

// Copyright (c) 2011-2013 The Bitcoin developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#if defined(HAVE_CONFIG_H)
#include "bitcoin-config.h"
#endif
#include "optionsmodel.h"
#include "bitcoinunits.h"
#include "guiutil.h"
#include "init.h"
#include "main.h"
#include "net.h"
#include "walletdb.h"
#include <QSettings>
2012-02-17 03:09:41 +01:00
OptionsModel::OptionsModel(QObject *parent) :
QAbstractListModel(parent)
{
2012-02-17 03:09:41 +01:00
Init();
}
2012-05-24 19:02:21 +02:00
bool static ApplyProxySettings()
{
QSettings settings;
CService addrProxy(settings.value("addrProxy", "127.0.0.1:9050").toString().toStdString());
int nSocksVersion(settings.value("nSocksVersion", 5).toInt());
if (!settings.value("fUseProxy", false).toBool()) {
addrProxy = CService();
nSocksVersion = 0;
return false;
2012-05-24 19:02:21 +02:00
}
if (nSocksVersion && !addrProxy.IsValid())
return false;
if (!IsLimited(NET_IPV4))
SetProxy(NET_IPV4, addrProxy, nSocksVersion);
if (nSocksVersion > 4) {
#ifdef USE_IPV6
if (!IsLimited(NET_IPV6))
SetProxy(NET_IPV6, addrProxy, nSocksVersion);
#endif
SetNameProxy(addrProxy, nSocksVersion);
}
return true;
}
2012-02-17 03:09:41 +01:00
void OptionsModel::Init()
{
QSettings settings;
2012-07-07 16:35:29 +02:00
// These are Qt-only settings:
2012-02-17 03:09:41 +01:00
nDisplayUnit = settings.value("nDisplayUnit", BitcoinUnits::BTC).toInt();
bDisplayAddresses = settings.value("bDisplayAddresses", false).toBool();
fMinimizeToTray = settings.value("fMinimizeToTray", false).toBool();
fMinimizeOnClose = settings.value("fMinimizeOnClose", false).toBool();
nTransactionFee = settings.value("nTransactionFee").toLongLong();
language = settings.value("language", "").toString();
2013-08-12 17:03:03 +02:00
fCoinControlFeatures = settings.value("fCoinControlFeatures", false).toBool();
2012-02-17 03:09:41 +01:00
// These are shared with core Bitcoin; we want
2012-02-17 03:09:41 +01:00
// command-line options to override the GUI settings:
if (settings.contains("fUseUPnP"))
SoftSetBoolArg("-upnp", settings.value("fUseUPnP").toBool());
if (settings.contains("addrProxy") && settings.value("fUseProxy").toBool())
SoftSetArg("-proxy", settings.value("addrProxy").toString().toStdString());
if (settings.contains("nSocksVersion") && settings.value("fUseProxy").toBool())
SoftSetArg("-socks", settings.value("nSocksVersion").toString().toStdString());
if (!language.isEmpty())
SoftSetArg("-lang", language.toStdString());
2012-02-17 03:09:41 +01:00
}
void OptionsModel::Reset()
{
QSettings settings;
// Remove all entries in this QSettings object
settings.clear();
// default setting for OptionsModel::StartAtStartup - disabled
if (GUIUtil::GetStartOnSystemStartup())
GUIUtil::SetStartOnSystemStartup(false);
// Re-Init to get default values
Init();
// Ensure Upgrade() is not running again by setting the bImportFinished flag
settings.setValue("bImportFinished", true);
}
2012-02-17 03:09:41 +01:00
bool OptionsModel::Upgrade()
{
QSettings settings;
if (settings.contains("bImportFinished"))
return false; // Already upgraded
settings.setValue("bImportFinished", true);
// Move settings from old wallet.dat (if any):
CWalletDB walletdb(strWalletFile);
2012-02-17 03:09:41 +01:00
QList<QString> intOptions;
intOptions << "nDisplayUnit" << "nTransactionFee";
foreach(QString key, intOptions)
{
int value = 0;
if (walletdb.ReadSetting(key.toStdString(), value))
{
settings.setValue(key, value);
walletdb.EraseSetting(key.toStdString());
}
}
QList<QString> boolOptions;
boolOptions << "bDisplayAddresses" << "fMinimizeToTray" << "fMinimizeOnClose" << "fUseProxy" << "fUseUPnP";
foreach(QString key, boolOptions)
{
bool value = false;
if (walletdb.ReadSetting(key.toStdString(), value))
{
settings.setValue(key, value);
walletdb.EraseSetting(key.toStdString());
}
}
try
{
CAddress addrProxyAddress;
if (walletdb.ReadSetting("addrProxy", addrProxyAddress))
{
2012-05-24 19:02:21 +02:00
settings.setValue("addrProxy", addrProxyAddress.ToStringIPPort().c_str());
2012-02-17 03:09:41 +01:00
walletdb.EraseSetting("addrProxy");
}
}
catch (std::ios_base::failure &e)
{
// 0.6.0rc1 saved this as a CService, which causes failure when parsing as a CAddress
2012-05-24 19:02:21 +02:00
CService addrProxy;
2012-02-17 03:09:41 +01:00
if (walletdb.ReadSetting("addrProxy", addrProxy))
{
2012-02-26 23:39:32 +01:00
settings.setValue("addrProxy", addrProxy.ToStringIPPort().c_str());
2012-02-17 03:09:41 +01:00
walletdb.EraseSetting("addrProxy");
}
}
2012-05-24 19:02:21 +02:00
ApplyProxySettings();
2012-02-17 03:09:41 +01:00
Init();
return true;
}
int OptionsModel::rowCount(const QModelIndex & parent) const
{
return OptionIDRowCount;
}
QVariant OptionsModel::data(const QModelIndex & index, int role) const
{
if(role == Qt::EditRole)
{
2012-02-17 03:09:41 +01:00
QSettings settings;
switch(index.row())
{
case StartAtStartup:
return QVariant(GUIUtil::GetStartOnSystemStartup());
case MinimizeToTray:
return QVariant(fMinimizeToTray);
case MapPortUPnP:
#ifdef USE_UPNP
2012-02-17 03:09:41 +01:00
return settings.value("fUseUPnP", GetBoolArg("-upnp", true));
#else
return QVariant(false);
#endif
case MinimizeOnClose:
return QVariant(fMinimizeOnClose);
case ProxyUse: {
proxyType proxy;
return QVariant(GetProxy(NET_IPV4, proxy));
}
2012-05-24 19:02:21 +02:00
case ProxyIP: {
proxyType proxy;
if (GetProxy(NET_IPV4, proxy))
return QVariant(QString::fromStdString(proxy.first.ToStringIP()));
2012-05-24 19:02:21 +02:00
else
return QVariant(QString::fromStdString("127.0.0.1"));
2012-05-24 19:02:21 +02:00
}
case ProxyPort: {
proxyType proxy;
if (GetProxy(NET_IPV4, proxy))
return QVariant(proxy.first.GetPort());
2012-05-24 19:02:21 +02:00
else
return QVariant(9050);
2012-05-24 19:02:21 +02:00
}
case ProxySocksVersion: {
proxyType proxy;
if (GetProxy(NET_IPV4, proxy))
return QVariant(proxy.second);
else
return QVariant(5);
}
case Fee:
return QVariant((qint64) nTransactionFee);
case DisplayUnit:
return QVariant(nDisplayUnit);
case DisplayAddresses:
return QVariant(bDisplayAddresses);
case Language:
return settings.value("language", "");
2013-08-12 17:03:03 +02:00
case CoinControlFeatures:
return QVariant(fCoinControlFeatures);
default:
return QVariant();
}
}
return QVariant();
}
bool OptionsModel::setData(const QModelIndex & index, const QVariant & value, int role)
{
bool successful = true; /* set to false on parse error */
if(role == Qt::EditRole)
{
2012-02-17 03:09:41 +01:00
QSettings settings;
switch(index.row())
{
case StartAtStartup:
successful = GUIUtil::SetStartOnSystemStartup(value.toBool());
break;
case MinimizeToTray:
fMinimizeToTray = value.toBool();
2012-02-17 03:09:41 +01:00
settings.setValue("fMinimizeToTray", fMinimizeToTray);
break;
case MapPortUPnP:
settings.setValue("fUseUPnP", value.toBool());
MapPort(value.toBool());
break;
case MinimizeOnClose:
fMinimizeOnClose = value.toBool();
2012-02-17 03:09:41 +01:00
settings.setValue("fMinimizeOnClose", fMinimizeOnClose);
break;
2012-05-24 19:02:21 +02:00
case ProxyUse:
settings.setValue("fUseProxy", value.toBool());
successful = ApplyProxySettings();
break;
case ProxyIP: {
proxyType proxy;
proxy.first = CService("127.0.0.1", 9050);
GetProxy(NET_IPV4, proxy);
CNetAddr addr(value.toString().toStdString());
proxy.first.SetIP(addr);
settings.setValue("addrProxy", proxy.first.ToStringIPPort().c_str());
successful = ApplyProxySettings();
}
break;
case ProxyPort: {
proxyType proxy;
proxy.first = CService("127.0.0.1", 9050);
GetProxy(NET_IPV4, proxy);
proxy.first.SetPort(value.toInt());
settings.setValue("addrProxy", proxy.first.ToStringIPPort().c_str());
successful = ApplyProxySettings();
}
break;
case ProxySocksVersion: {
proxyType proxy;
proxy.second = 5;
GetProxy(NET_IPV4, proxy);
proxy.second = value.toInt();
settings.setValue("nSocksVersion", proxy.second);
successful = ApplyProxySettings();
}
break;
case Fee:
nTransactionFee = value.toLongLong();
settings.setValue("nTransactionFee", (qint64) nTransactionFee);
2013-08-12 17:03:03 +02:00
emit transactionFeeChanged(nTransactionFee);
break;
case DisplayUnit:
nDisplayUnit = value.toInt();
2012-02-17 03:09:41 +01:00
settings.setValue("nDisplayUnit", nDisplayUnit);
emit displayUnitChanged(nDisplayUnit);
break;
case DisplayAddresses:
bDisplayAddresses = value.toBool();
2012-02-17 03:09:41 +01:00
settings.setValue("bDisplayAddresses", bDisplayAddresses);
break;
case Language:
settings.setValue("language", value);
break;
case CoinControlFeatures:
2013-08-12 17:03:03 +02:00
fCoinControlFeatures = value.toBool();
settings.setValue("fCoinControlFeatures", fCoinControlFeatures);
emit coinControlFeaturesChanged(fCoinControlFeatures);
break;
default:
break;
}
}
emit dataChanged(index, index);
return successful;
}
2011-06-01 09:34:12 +02:00
qint64 OptionsModel::getTransactionFee()
{
return (qint64) nTransactionFee;
2011-06-01 09:34:12 +02:00
}
bool OptionsModel::getProxySettings(QString& proxyIP, quint16 &proxyPort) const
{
std::string proxy = GetArg("-proxy", "");
if (proxy.empty()) return false;
CService addrProxy(proxy);
proxyIP = QString(addrProxy.ToStringIP().c_str());
proxyPort = addrProxy.GetPort();
return true;
}