neobytes/src/qt/optionsmodel.cpp

259 lines
7.5 KiB
C++
Raw Normal View History

#include "optionsmodel.h"
#include "bitcoinunits.h"
2012-02-17 03:09:41 +01:00
#include <QSettings>
#include "init.h"
#include "walletdb.h"
#include "guiutil.h"
2012-02-17 03:09:41 +01:00
OptionsModel::OptionsModel(QObject *parent) :
QAbstractListModel(parent)
{
2012-02-17 03:09:41 +01:00
Init();
}
2012-02-17 03:09:41 +01:00
void OptionsModel::Init()
{
QSettings settings;
// These are QT-only settings:
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();
2012-02-17 03:09:41 +01:00
// These are shared with core bitcoin; we want
// 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("detachDB"))
SoftSetBoolArg("-detachdb", settings.value("detachDB").toBool());
if (!language.isEmpty())
SoftSetArg("-lang", language.toStdString());
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("wallet.dat");
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))
{
addrProxy = addrProxyAddress;
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");
}
}
catch (std::ios_base::failure &e)
{
// 0.6.0rc1 saved this as a CService, which causes failure when parsing as a CAddress
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");
}
}
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:
2012-02-17 03:09:41 +01:00
return settings.value("fUseUPnP", GetBoolArg("-upnp", true));
case MinimizeOnClose:
return QVariant(fMinimizeOnClose);
case ConnectSOCKS4:
2012-02-17 03:09:41 +01:00
return settings.value("fUseProxy", false);
case ProxyIP:
return QVariant(QString::fromStdString(addrProxy.ToStringIP()));
case ProxyPort:
return QVariant(addrProxy.GetPort());
case Fee:
return QVariant(nTransactionFee);
case DisplayUnit:
return QVariant(nDisplayUnit);
case DisplayAddresses:
return QVariant(bDisplayAddresses);
case DetachDatabases:
return QVariant(fDetachDB);
case Language:
return settings.value("language", "");
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:
2012-02-17 03:09:41 +01:00
{
bool bUseUPnP = value.toBool();
settings.setValue("fUseUPnP", bUseUPnP);
MapPort(bUseUPnP);
}
break;
case MinimizeOnClose:
fMinimizeOnClose = value.toBool();
2012-02-17 03:09:41 +01:00
settings.setValue("fMinimizeOnClose", fMinimizeOnClose);
break;
case ConnectSOCKS4:
fUseProxy = value.toBool();
2012-02-17 03:09:41 +01:00
settings.setValue("fUseProxy", fUseProxy);
break;
case ProxyIP:
{
// Use CAddress to parse and check IP
CNetAddr addr(value.toString().toStdString());
if (addr.IsValid())
{
addrProxy.SetIP(addr);
2012-02-26 23:39:32 +01:00
settings.setValue("addrProxy", addrProxy.ToStringIPPort().c_str());
2011-06-07 18:59:01 +02:00
}
else
{
successful = false;
}
}
break;
case ProxyPort:
{
int nPort = atoi(value.toString().toAscii().data());
if (nPort > 0 && nPort < std::numeric_limits<unsigned short>::max())
{
addrProxy.SetPort(nPort);
2012-02-26 23:39:32 +01:00
settings.setValue("addrProxy", addrProxy.ToStringIPPort().c_str());
2011-06-07 18:59:01 +02:00
}
else
{
successful = false;
}
}
break;
case Fee: {
nTransactionFee = value.toLongLong();
2012-02-17 03:09:41 +01:00
settings.setValue("nTransactionFee", nTransactionFee);
}
break;
case DisplayUnit: {
int unit = value.toInt();
nDisplayUnit = unit;
2012-02-17 03:09:41 +01:00
settings.setValue("nDisplayUnit", nDisplayUnit);
emit displayUnitChanged(unit);
}
break;
case DisplayAddresses: {
bDisplayAddresses = value.toBool();
2012-02-17 03:09:41 +01:00
settings.setValue("bDisplayAddresses", bDisplayAddresses);
}
break;
case DetachDatabases: {
fDetachDB = value.toBool();
settings.setValue("detachDB", fDetachDB);
}
break;
case Language: {
settings.setValue("language", value);
}
break;
default:
break;
}
}
emit dataChanged(index, index);
return successful;
}
2011-06-01 09:34:12 +02:00
qint64 OptionsModel::getTransactionFee()
{
return nTransactionFee;
}
bool OptionsModel::getMinimizeToTray()
{
return fMinimizeToTray;
}
bool OptionsModel::getMinimizeOnClose()
{
return fMinimizeOnClose;
}
int OptionsModel::getDisplayUnit()
{
return nDisplayUnit;
}
bool OptionsModel::getDisplayAddresses()
{
return bDisplayAddresses;
}