@ -18,6 +18,7 @@
# include "walletdb.h"
# include "walletdb.h"
# include <QSettings>
# include <QSettings>
# include <QStringList>
OptionsModel : : OptionsModel ( QObject * parent ) :
OptionsModel : : OptionsModel ( QObject * parent ) :
QAbstractListModel ( parent )
QAbstractListModel ( parent )
@ -25,79 +26,114 @@ OptionsModel::OptionsModel(QObject *parent) :
Init ( ) ;
Init ( ) ;
}
}
bool static ApplyProxySettings ( )
// Writes all missing QSettings with their default values
{
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 ;
}
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 ;
}
void OptionsModel : : Init ( )
void OptionsModel : : Init ( )
{
{
QSettings settings ;
QSettings settings ;
// Ensure restart flag is unset on client startup
setRestartRequired ( false ) ;
// These are Qt-only settings:
// These are Qt-only settings:
nDisplayUnit = settings . value ( " nDisplayUnit " , BitcoinUnits : : BTC ) . toInt ( ) ;
// Window
if ( ! settings . contains ( " fMinimizeToTray " ) )
settings . setValue ( " fMinimizeToTray " , false ) ;
fMinimizeToTray = settings . value ( " fMinimizeToTray " ) . toBool ( ) ;
if ( ! settings . contains ( " fMinimizeOnClose " ) )
settings . setValue ( " fMinimizeOnClose " , false ) ;
fMinimizeOnClose = settings . value ( " fMinimizeOnClose " ) . toBool ( ) ;
// Display
if ( ! settings . contains ( " nDisplayUnit " ) )
settings . setValue ( " nDisplayUnit " , BitcoinUnits : : BTC ) ;
nDisplayUnit = settings . value ( " nDisplayUnit " ) . toInt ( ) ;
if ( ! settings . contains ( " bDisplayAddresses " ) )
settings . setValue ( " bDisplayAddresses " , false ) ;
bDisplayAddresses = settings . value ( " bDisplayAddresses " , false ) . toBool ( ) ;
bDisplayAddresses = settings . value ( " bDisplayAddresses " , false ) . toBool ( ) ;
fMinimizeToTray = settings . value ( " fMinimizeToTray " , false ) . toBool ( ) ;
fMinimizeOnClose = settings . value ( " fMinimizeOnClose " , false ) . toBool ( ) ;
if ( ! settings . contains ( " fCoinControlFeatures " ) )
nTransactionFee = settings . value ( " nTransactionFee " ) . toLongLong ( ) ;
settings . setValue ( " fCoinControlFeatures " , false ) ;
language = settings . value ( " language " , " " ) . toString ( ) ;
fCoinControlFeatures = settings . value ( " fCoinControlFeatures " , false ) . toBool ( ) ;
fCoinControlFeatures = settings . value ( " fCoinControlFeatures " , false ) . toBool ( ) ;
// These are shared with core Bitcoin; we want
// These are shared with the core or have a command-line parameter
// command-line options to override the GUI settings:
// and we want command-line parameters to overwrite the GUI settings.
if ( settings . contains ( " fUseUPnP " ) )
//
SoftSetBoolArg ( " -upnp " , settings . value ( " fUseUPnP " ) . toBool ( ) ) ;
// If setting doesn't exist create it with defaults.
if ( settings . contains ( " addrProxy " ) & & settings . value ( " fUseProxy " ) . toBool ( ) )
//
SoftSetArg ( " -proxy " , settings . value ( " addrProxy " ) . toString ( ) . toStdString ( ) ) ;
// If SoftSetArg() or SoftSetBoolArg() return false we were overridden
if ( settings . contains ( " nSocksVersion " ) & & settings . value ( " fUseProxy " ) . toBool ( ) )
// by command-line and show this in the UI.
SoftSetArg ( " -socks " , settings . value ( " nSocksVersion " ) . toString ( ) . toStdString ( ) ) ;
if ( ! language . isEmpty ( ) )
// Main
SoftSetArg ( " -lang " , language . toStdString ( ) ) ;
if ( ! settings . contains ( " nTransactionFee " ) )
settings . setValue ( " nTransactionFee " , 0 ) ;
if ( ! settings . contains ( " nDatabaseCache " ) )
settings . setValue ( " nDatabaseCache " , 25 ) ;
if ( ! SoftSetArg ( " -dbcache " , settings . value ( " nDatabaseCache " ) . toString ( ) . toStdString ( ) ) )
strOverriddenByCommandLine + = " -dbcache " ;
if ( ! settings . contains ( " nThreadsScriptVerif " ) )
settings . setValue ( " nThreadsScriptVerif " , 0 ) ;
if ( ! SoftSetArg ( " -par " , settings . value ( " nThreadsScriptVerif " ) . toString ( ) . toStdString ( ) ) )
strOverriddenByCommandLine + = " -par " ;
// Network
if ( ! settings . contains ( " fUseUPnP " ) )
# ifdef USE_UPNP
settings . setValue ( " fUseUPnP " , true ) ;
# else
settings . setValue ( " fUseUPnP " , false ) ;
# endif
if ( ! SoftSetBoolArg ( " -upnp " , settings . value ( " fUseUPnP " ) . toBool ( ) ) )
strOverriddenByCommandLine + = " -upnp " ;
if ( ! settings . contains ( " fUseProxy " ) )
settings . setValue ( " fUseProxy " , false ) ;
if ( ! settings . contains ( " addrProxy " ) )
settings . setValue ( " addrProxy " , " 127.0.0.1:9050 " ) ;
// Only try to set -proxy, if user has enabled fUseProxy
if ( settings . value ( " fUseProxy " ) . toBool ( ) & & ! SoftSetArg ( " -proxy " , settings . value ( " addrProxy " ) . toString ( ) . toStdString ( ) ) )
strOverriddenByCommandLine + = " -proxy " ;
if ( ! settings . contains ( " nSocksVersion " ) )
settings . setValue ( " nSocksVersion " , 5 ) ;
// Only try to set -socks, if user has enabled fUseProxy
if ( settings . value ( " fUseProxy " ) . toBool ( ) & & ! SoftSetArg ( " -socks " , settings . value ( " nSocksVersion " ) . toString ( ) . toStdString ( ) ) )
strOverriddenByCommandLine + = " -socks " ;
// Display
if ( ! settings . contains ( " language " ) )
settings . setValue ( " language " , " " ) ;
if ( ! SoftSetArg ( " -lang " , settings . value ( " language " ) . toString ( ) . toStdString ( ) ) )
strOverriddenByCommandLine + = " -lang " ;
language = settings . value ( " language " ) . toString ( ) ;
}
}
void OptionsModel : : Reset ( )
void OptionsModel : : Reset ( )
{
{
QSettings settings ;
QSettings settings ;
// Remove all entries in this QSettings object
// Remove all entries from our QSettings object
settings . clear ( ) ;
settings . clear ( ) ;
// default setting for OptionsModel::StartAtStartup - disabled
// default setting for OptionsModel::StartAtStartup - disabled
if ( GUIUtil : : GetStartOnSystemStartup ( ) )
if ( GUIUtil : : GetStartOnSystemStartup ( ) )
GUIUtil : : SetStartOnSystemStartup ( false ) ;
GUIUtil : : SetStartOnSystemStartup ( false ) ;
// Re-Init to get default values
Init ( ) ;
// Ensure Upgrade() is not running again by setting the bImportFinished flag
// Ensure Upgrade() is not running again by setting the bImportFinished flag
settings . setValue ( " bImportFinished " , true ) ;
settings . setValue ( " bImportFinished " , true ) ;
}
}
bool OptionsModel : : Upgrade ( )
void OptionsModel : : Upgrade ( )
{
{
QSettings settings ;
QSettings settings ;
// Already upgraded
if ( settings . contains ( " bImportFinished " ) )
if ( settings . contains ( " bImportFinished " ) )
return false ; // Already upgraded
return ;
settings . setValue ( " bImportFinished " , true ) ;
settings . setValue ( " bImportFinished " , true ) ;
@ -145,18 +181,16 @@ bool OptionsModel::Upgrade()
walletdb . EraseSetting ( " addrProxy " ) ;
walletdb . EraseSetting ( " addrProxy " ) ;
}
}
}
}
ApplyProxySettings ( ) ;
Init ( ) ;
return true ;
Init ( ) ;
}
}
int OptionsModel : : rowCount ( const QModelIndex & parent ) const
int OptionsModel : : rowCount ( const QModelIndex & parent ) const
{
{
return OptionIDRowCount ;
return OptionIDRowCount ;
}
}
// read QSettings values and return them
QVariant OptionsModel : : data ( const QModelIndex & index , int role ) const
QVariant OptionsModel : : data ( const QModelIndex & index , int role ) const
{
{
if ( role = = Qt : : EditRole )
if ( role = = Qt : : EditRole )
@ -165,52 +199,55 @@ QVariant OptionsModel::data(const QModelIndex & index, int role) const
switch ( index . row ( ) )
switch ( index . row ( ) )
{
{
case StartAtStartup :
case StartAtStartup :
return QVariant ( GUIUtil : : GetStartOnSystemStartup ( ) ) ;
return GUIUtil : : GetStartOnSystemStartup ( ) ;
case MinimizeToTray :
case MinimizeToTray :
return QVariant ( fMinimizeToTray ) ;
return fMinimizeToTray ;
case MapPortUPnP :
case MapPortUPnP :
# ifdef USE_UPNP
# ifdef USE_UPNP
return settings . value ( " fUseUPnP " , GetBoolArg ( " -upnp " , true ) ) ;
return settings . value ( " fUseUPnP " ) ;
# else
# else
return QVariant ( false ) ;
return false ;
# endif
# endif
case MinimizeOnClose :
case MinimizeOnClose :
return QVariant ( fMinimizeOnClose ) ;
return fMinimizeOnClose ;
case ProxyUse : {
proxyType proxy ;
// default proxy
return QVariant ( GetProxy ( NET_IPV4 , proxy ) ) ;
case ProxyUse :
}
return settings . value ( " fUseProxy " , false ) ;
case ProxyIP : {
case ProxyIP : {
proxyType proxy ;
// contains IP at index 0 and port at index 1
if ( GetProxy ( NET_IPV4 , proxy ) )
QStringList strlIpPort = settings . value ( " addrProxy " ) . toString ( ) . split ( " : " , QString : : SkipEmptyParts ) ;
return QVariant ( QString : : fromStdString ( proxy . first . ToStringIP ( ) ) ) ;
return strlIpPort . at ( 0 ) ;
else
return QVariant ( QString : : fromStdString ( " 127.0.0.1 " ) ) ;
}
}
case ProxyPort : {
case ProxyPort : {
proxyType proxy ;
// contains IP at index 0 and port at index 1
if ( GetProxy ( NET_IPV4 , proxy ) )
QStringList strlIpPort = settings . value ( " addrProxy " ) . toString ( ) . split ( " : " , QString : : SkipEmptyParts ) ;
return QVariant ( proxy . first . GetPort ( ) ) ;
return strlIpPort . at ( 1 ) ;
else
return QVariant ( 9050 ) ;
}
case ProxySocksVersion : {
proxyType proxy ;
if ( GetProxy ( NET_IPV4 , proxy ) )
return QVariant ( proxy . second ) ;
else
return QVariant ( 5 ) ;
}
}
case ProxySocksVersion :
return settings . value ( " nSocksVersion " , 5 ) ;
case Fee :
case Fee :
return QVariant ( ( qint64 ) nTransactionFee ) ;
// Attention: Init() is called before nTransactionFee is set in AppInit2()!
// To ensure we can change the fee on-the-fly update our QSetting when
// opening OptionsDialog, which queries Fee via the mapper.
if ( nTransactionFee ! = settings . value ( " nTransactionFee " ) . toLongLong ( ) )
settings . setValue ( " nTransactionFee " , ( qint64 ) nTransactionFee ) ;
// Todo: Consider to revert back to use just nTransactionFee here, if we don't want
// -paytxfee to update our QSettings!
return settings . value ( " nTransactionFee " ) ;
case DisplayUnit :
case DisplayUnit :
return QVariant ( nDisplayUnit ) ;
return nDisplayUnit ;
case DisplayAddresses :
case DisplayAddresses :
return QVariant ( bDisplayAddresses ) ;
return bDisplayAddresses ;
case Language :
case Language :
return settings . value ( " language " , " " ) ;
return settings . value ( " language " ) ;
case CoinControlFeatures :
case CoinControlFeatures :
return QVariant ( fCoinControlFeatures ) ;
return fCoinControlFeatures ;
case DatabaseCache :
return settings . value ( " nDatabaseCache " ) ;
case ThreadsScriptVerif :
return settings . value ( " nThreadsScriptVerif " ) ;
default :
default :
return QVariant ( ) ;
return QVariant ( ) ;
}
}
@ -218,6 +255,7 @@ QVariant OptionsModel::data(const QModelIndex & index, int role) const
return QVariant ( ) ;
return QVariant ( ) ;
}
}
// write QSettings values
bool OptionsModel : : setData ( const QModelIndex & index , const QVariant & value , int role )
bool OptionsModel : : setData ( const QModelIndex & index , const QVariant & value , int role )
{
{
bool successful = true ; /* set to false on parse error */
bool successful = true ; /* set to false on parse error */
@ -233,7 +271,7 @@ bool OptionsModel::setData(const QModelIndex & index, const QVariant & value, in
fMinimizeToTray = value . toBool ( ) ;
fMinimizeToTray = value . toBool ( ) ;
settings . setValue ( " fMinimizeToTray " , fMinimizeToTray ) ;
settings . setValue ( " fMinimizeToTray " , fMinimizeToTray ) ;
break ;
break ;
case MapPortUPnP :
case MapPortUPnP : // core option - can be changed on-the-fly
settings . setValue ( " fUseUPnP " , value . toBool ( ) ) ;
settings . setValue ( " fUseUPnP " , value . toBool ( ) ) ;
MapPort ( value . toBool ( ) ) ;
MapPort ( value . toBool ( ) ) ;
break ;
break ;
@ -241,42 +279,48 @@ bool OptionsModel::setData(const QModelIndex & index, const QVariant & value, in
fMinimizeOnClose = value . toBool ( ) ;
fMinimizeOnClose = value . toBool ( ) ;
settings . setValue ( " fMinimizeOnClose " , fMinimizeOnClose ) ;
settings . setValue ( " fMinimizeOnClose " , fMinimizeOnClose ) ;
break ;
break ;
// default proxy
case ProxyUse :
case ProxyUse :
if ( settings . value ( " fUseProxy " ) ! = value ) {
settings . setValue ( " fUseProxy " , value . toBool ( ) ) ;
settings . setValue ( " fUseProxy " , value . toBool ( ) ) ;
successful = ApplyProxySettings ( ) ;
setRestartRequired ( true ) ;
}
break ;
break ;
case ProxyIP : {
case ProxyIP : {
proxyType proxy ;
// contains current IP at index 0 and current port at index 1
proxy . first = CService ( " 127.0.0.1 " , 9050 ) ;
QStringList strlIpPort = settings . value ( " addrProxy " ) . toString ( ) . split ( " : " , QString : : SkipEmptyParts ) ;
GetProxy ( NET_IPV4 , proxy ) ;
// if that key doesn't exist or has a changed IP
if ( ! settings . contains ( " addrProxy " ) | | strlIpPort . at ( 0 ) ! = value . toString ( ) ) {
CNetAddr addr ( value . toString ( ) . toStdString ( ) ) ;
// construct new value from new IP and current port
proxy . first . SetIP ( addr ) ;
QString strNewValue = value . toString ( ) + " : " + strlIpPort . at ( 1 ) ;
settings . setValue ( " addrProxy " , proxy . first . ToStringIPPort ( ) . c_str ( ) ) ;
settings . setValue ( " addrProxy " , strNewValue ) ;
successful = ApplyProxySettings ( ) ;
setRestartRequired ( true ) ;
}
}
}
break ;
break ;
case ProxyPort : {
case ProxyPort : {
proxyType proxy ;
// contains current IP at index 0 and current port at index 1
proxy . first = CService ( " 127.0.0.1 " , 9050 ) ;
QStringList strlIpPort = settings . value ( " addrProxy " ) . toString ( ) . split ( " : " , QString : : SkipEmptyParts ) ;
GetProxy ( NET_IPV4 , proxy ) ;
// if that key doesn't exist or has a changed port
if ( ! settings . contains ( " addrProxy " ) | | strlIpPort . at ( 1 ) ! = value . toString ( ) ) {
proxy . first . SetPort ( value . toInt ( ) ) ;
// construct new value from current IP and new port
settings . setValue ( " addrProxy " , proxy . first . ToStringIPPort ( ) . c_str ( ) ) ;
QString strNewValue = strlIpPort . at ( 0 ) + " : " + value . toString ( ) ;
successful = ApplyProxySettings ( ) ;
settings . setValue ( " addrProxy " , strNewValue ) ;
setRestartRequired ( true ) ;
}
}
}
break ;
break ;
case ProxySocksVersion : {
case ProxySocksVersion : {
proxyType proxy ;
if ( settings . value ( " nSocksVersion " ) ! = value ) {
proxy . second = 5 ;
settings . setValue ( " nSocksVersion " , value . toInt ( ) ) ;
GetProxy ( NET_IPV4 , proxy ) ;
setRestartRequired ( true ) ;
}
proxy . second = value . toInt ( ) ;
settings . setValue ( " nSocksVersion " , proxy . second ) ;
successful = ApplyProxySettings ( ) ;
}
}
break ;
break ;
case Fee :
case Fee : // core option - can be changed on-the-fly
// Todo: Add is valid check and warn via message, if not
nTransactionFee = value . toLongLong ( ) ;
nTransactionFee = value . toLongLong ( ) ;
settings . setValue ( " nTransactionFee " , ( qint64 ) nTransactionFee ) ;
settings . setValue ( " nTransactionFee " , ( qint64 ) nTransactionFee ) ;
emit transactionFeeChanged ( nTransactionFee ) ;
emit transactionFeeChanged ( nTransactionFee ) ;
@ -291,13 +335,28 @@ bool OptionsModel::setData(const QModelIndex & index, const QVariant & value, in
settings . setValue ( " bDisplayAddresses " , bDisplayAddresses ) ;
settings . setValue ( " bDisplayAddresses " , bDisplayAddresses ) ;
break ;
break ;
case Language :
case Language :
if ( settings . value ( " language " ) ! = value ) {
settings . setValue ( " language " , value ) ;
settings . setValue ( " language " , value ) ;
setRestartRequired ( true ) ;
}
break ;
break ;
case CoinControlFeatures :
case CoinControlFeatures :
fCoinControlFeatures = value . toBool ( ) ;
fCoinControlFeatures = value . toBool ( ) ;
settings . setValue ( " fCoinControlFeatures " , fCoinControlFeatures ) ;
settings . setValue ( " fCoinControlFeatures " , fCoinControlFeatures ) ;
emit coinControlFeaturesChanged ( fCoinControlFeatures ) ;
emit coinControlFeaturesChanged ( fCoinControlFeatures ) ;
break ;
break ;
case DatabaseCache :
if ( settings . value ( " nDatabaseCache " ) ! = value ) {
settings . setValue ( " nDatabaseCache " , value ) ;
setRestartRequired ( true ) ;
}
break ;
case ThreadsScriptVerif :
if ( settings . value ( " nThreadsScriptVerif " ) ! = value ) {
settings . setValue ( " nThreadsScriptVerif " , value ) ;
setRestartRequired ( true ) ;
}
break ;
default :
default :
break ;
break ;
}
}
@ -317,3 +376,15 @@ bool OptionsModel::getProxySettings(QString& proxyIP, quint16 &proxyPort) const
proxyPort = addrProxy . GetPort ( ) ;
proxyPort = addrProxy . GetPort ( ) ;
return true ;
return true ;
}
}
void OptionsModel : : setRestartRequired ( bool fRequired )
{
QSettings settings ;
return settings . setValue ( " fRestartRequired " , fRequired ) ;
}
bool OptionsModel : : isRestartRequired ( )
{
QSettings settings ;
return settings . value ( " fRestartRequired " , false ) . toBool ( ) ;
}