You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

optionsmodel.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. // Copyright (c) 2011-2014 The Bitcoin developers
  2. // Distributed under the MIT/X11 software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #if defined(HAVE_CONFIG_H)
  5. #include "bitcoin-config.h"
  6. #endif
  7. #include "optionsmodel.h"
  8. #include "bitcoinunits.h"
  9. #include "guiutil.h"
  10. #include "init.h"
  11. #include "main.h"
  12. #include "net.h"
  13. #include "txdb.h" // for -dbcache defaults
  14. #ifdef ENABLE_WALLET
  15. #include "wallet.h"
  16. #include "walletdb.h"
  17. #endif
  18. #include <QNetworkProxy>
  19. #include <QSettings>
  20. #include <QStringList>
  21. OptionsModel::OptionsModel(QObject *parent) :
  22. QAbstractListModel(parent)
  23. {
  24. Init();
  25. }
  26. void OptionsModel::addOverriddenOption(const std::string &option)
  27. {
  28. strOverriddenByCommandLine += QString::fromStdString(option) + "=" + QString::fromStdString(mapArgs[option]) + " ";
  29. }
  30. // Writes all missing QSettings with their default values
  31. void OptionsModel::Init()
  32. {
  33. QSettings settings;
  34. // Ensure restart flag is unset on client startup
  35. setRestartRequired(false);
  36. // These are Qt-only settings:
  37. // Window
  38. if (!settings.contains("fMinimizeToTray"))
  39. settings.setValue("fMinimizeToTray", false);
  40. fMinimizeToTray = settings.value("fMinimizeToTray").toBool();
  41. if (!settings.contains("fMinimizeOnClose"))
  42. settings.setValue("fMinimizeOnClose", false);
  43. fMinimizeOnClose = settings.value("fMinimizeOnClose").toBool();
  44. // Display
  45. if (!settings.contains("nDisplayUnit"))
  46. settings.setValue("nDisplayUnit", BitcoinUnits::BTC);
  47. nDisplayUnit = settings.value("nDisplayUnit").toInt();
  48. if (!settings.contains("bDisplayAddresses"))
  49. settings.setValue("bDisplayAddresses", false);
  50. bDisplayAddresses = settings.value("bDisplayAddresses", false).toBool();
  51. if (!settings.contains("strThirdPartyTxUrls"))
  52. settings.setValue("strThirdPartyTxUrls", "");
  53. strThirdPartyTxUrls = settings.value("strThirdPartyTxUrls", "").toString();
  54. if (!settings.contains("fCoinControlFeatures"))
  55. settings.setValue("fCoinControlFeatures", false);
  56. fCoinControlFeatures = settings.value("fCoinControlFeatures", false).toBool();
  57. // These are shared with the core or have a command-line parameter
  58. // and we want command-line parameters to overwrite the GUI settings.
  59. //
  60. // If setting doesn't exist create it with defaults.
  61. //
  62. // If SoftSetArg() or SoftSetBoolArg() return false we were overridden
  63. // by command-line and show this in the UI.
  64. // Main
  65. if (!settings.contains("nDatabaseCache"))
  66. settings.setValue("nDatabaseCache", (qint64)nDefaultDbCache);
  67. if (!SoftSetArg("-dbcache", settings.value("nDatabaseCache").toString().toStdString()))
  68. addOverriddenOption("-dbcache");
  69. if (!settings.contains("nThreadsScriptVerif"))
  70. settings.setValue("nThreadsScriptVerif", DEFAULT_SCRIPTCHECK_THREADS);
  71. if (!SoftSetArg("-par", settings.value("nThreadsScriptVerif").toString().toStdString()))
  72. addOverriddenOption("-par");
  73. // Wallet
  74. #ifdef ENABLE_WALLET
  75. if (!settings.contains("nTransactionFee"))
  76. settings.setValue("nTransactionFee", 0);
  77. nTransactionFee = settings.value("nTransactionFee").toLongLong(); // if -paytxfee is set, this will be overridden later in init.cpp
  78. if (mapArgs.count("-paytxfee"))
  79. addOverriddenOption("-paytxfee");
  80. if (!settings.contains("bSpendZeroConfChange"))
  81. settings.setValue("bSpendZeroConfChange", true);
  82. if (!SoftSetBoolArg("-spendzeroconfchange", settings.value("bSpendZeroConfChange").toBool()))
  83. addOverriddenOption("-spendzeroconfchange");
  84. #endif
  85. // Network
  86. if (!settings.contains("fUseUPnP"))
  87. #ifdef USE_UPNP
  88. settings.setValue("fUseUPnP", true);
  89. #else
  90. settings.setValue("fUseUPnP", false);
  91. #endif
  92. if (!SoftSetBoolArg("-upnp", settings.value("fUseUPnP").toBool()))
  93. addOverriddenOption("-upnp");
  94. if (!settings.contains("fUseProxy"))
  95. settings.setValue("fUseProxy", false);
  96. if (!settings.contains("addrProxy"))
  97. settings.setValue("addrProxy", "127.0.0.1:9050");
  98. // Only try to set -proxy, if user has enabled fUseProxy
  99. if (settings.value("fUseProxy").toBool() && !SoftSetArg("-proxy", settings.value("addrProxy").toString().toStdString()))
  100. addOverriddenOption("-proxy");
  101. if (!settings.contains("nSocksVersion"))
  102. settings.setValue("nSocksVersion", 5);
  103. // Only try to set -socks, if user has enabled fUseProxy
  104. if (settings.value("fUseProxy").toBool() && !SoftSetArg("-socks", settings.value("nSocksVersion").toString().toStdString()))
  105. addOverriddenOption("-socks");
  106. // Display
  107. if (!settings.contains("language"))
  108. settings.setValue("language", "");
  109. if (!SoftSetArg("-lang", settings.value("language").toString().toStdString()))
  110. addOverriddenOption("-lang");
  111. language = settings.value("language").toString();
  112. }
  113. void OptionsModel::Reset()
  114. {
  115. QSettings settings;
  116. // Remove all entries from our QSettings object
  117. settings.clear();
  118. // default setting for OptionsModel::StartAtStartup - disabled
  119. if (GUIUtil::GetStartOnSystemStartup())
  120. GUIUtil::SetStartOnSystemStartup(false);
  121. }
  122. int OptionsModel::rowCount(const QModelIndex & parent) const
  123. {
  124. return OptionIDRowCount;
  125. }
  126. // read QSettings values and return them
  127. QVariant OptionsModel::data(const QModelIndex & index, int role) const
  128. {
  129. if(role == Qt::EditRole)
  130. {
  131. QSettings settings;
  132. switch(index.row())
  133. {
  134. case StartAtStartup:
  135. return GUIUtil::GetStartOnSystemStartup();
  136. case MinimizeToTray:
  137. return fMinimizeToTray;
  138. case MapPortUPnP:
  139. #ifdef USE_UPNP
  140. return settings.value("fUseUPnP");
  141. #else
  142. return false;
  143. #endif
  144. case MinimizeOnClose:
  145. return fMinimizeOnClose;
  146. // default proxy
  147. case ProxyUse:
  148. return settings.value("fUseProxy", false);
  149. case ProxyIP: {
  150. // contains IP at index 0 and port at index 1
  151. QStringList strlIpPort = settings.value("addrProxy").toString().split(":", QString::SkipEmptyParts);
  152. return strlIpPort.at(0);
  153. }
  154. case ProxyPort: {
  155. // contains IP at index 0 and port at index 1
  156. QStringList strlIpPort = settings.value("addrProxy").toString().split(":", QString::SkipEmptyParts);
  157. return strlIpPort.at(1);
  158. }
  159. case ProxySocksVersion:
  160. return settings.value("nSocksVersion", 5);
  161. #ifdef ENABLE_WALLET
  162. case Fee:
  163. // Attention: Init() is called before nTransactionFee is set in AppInit2()!
  164. // To ensure we can change the fee on-the-fly update our QSetting when
  165. // opening OptionsDialog, which queries Fee via the mapper.
  166. if (nTransactionFee != settings.value("nTransactionFee").toLongLong())
  167. settings.setValue("nTransactionFee", (qint64)nTransactionFee);
  168. // Todo: Consider to revert back to use just nTransactionFee here, if we don't want
  169. // -paytxfee to update our QSettings!
  170. return settings.value("nTransactionFee");
  171. case SpendZeroConfChange:
  172. return settings.value("bSpendZeroConfChange");
  173. #endif
  174. case DisplayUnit:
  175. return nDisplayUnit;
  176. case DisplayAddresses:
  177. return bDisplayAddresses;
  178. case ThirdPartyTxUrls:
  179. return strThirdPartyTxUrls;
  180. case Language:
  181. return settings.value("language");
  182. case CoinControlFeatures:
  183. return fCoinControlFeatures;
  184. case DatabaseCache:
  185. return settings.value("nDatabaseCache");
  186. case ThreadsScriptVerif:
  187. return settings.value("nThreadsScriptVerif");
  188. default:
  189. return QVariant();
  190. }
  191. }
  192. return QVariant();
  193. }
  194. // write QSettings values
  195. bool OptionsModel::setData(const QModelIndex & index, const QVariant & value, int role)
  196. {
  197. bool successful = true; /* set to false on parse error */
  198. if(role == Qt::EditRole)
  199. {
  200. QSettings settings;
  201. switch(index.row())
  202. {
  203. case StartAtStartup:
  204. successful = GUIUtil::SetStartOnSystemStartup(value.toBool());
  205. break;
  206. case MinimizeToTray:
  207. fMinimizeToTray = value.toBool();
  208. settings.setValue("fMinimizeToTray", fMinimizeToTray);
  209. break;
  210. case MapPortUPnP: // core option - can be changed on-the-fly
  211. settings.setValue("fUseUPnP", value.toBool());
  212. MapPort(value.toBool());
  213. break;
  214. case MinimizeOnClose:
  215. fMinimizeOnClose = value.toBool();
  216. settings.setValue("fMinimizeOnClose", fMinimizeOnClose);
  217. break;
  218. // default proxy
  219. case ProxyUse:
  220. if (settings.value("fUseProxy") != value) {
  221. settings.setValue("fUseProxy", value.toBool());
  222. setRestartRequired(true);
  223. }
  224. break;
  225. case ProxyIP: {
  226. // contains current IP at index 0 and current port at index 1
  227. QStringList strlIpPort = settings.value("addrProxy").toString().split(":", QString::SkipEmptyParts);
  228. // if that key doesn't exist or has a changed IP
  229. if (!settings.contains("addrProxy") || strlIpPort.at(0) != value.toString()) {
  230. // construct new value from new IP and current port
  231. QString strNewValue = value.toString() + ":" + strlIpPort.at(1);
  232. settings.setValue("addrProxy", strNewValue);
  233. setRestartRequired(true);
  234. }
  235. }
  236. break;
  237. case ProxyPort: {
  238. // contains current IP at index 0 and current port at index 1
  239. QStringList strlIpPort = settings.value("addrProxy").toString().split(":", QString::SkipEmptyParts);
  240. // if that key doesn't exist or has a changed port
  241. if (!settings.contains("addrProxy") || strlIpPort.at(1) != value.toString()) {
  242. // construct new value from current IP and new port
  243. QString strNewValue = strlIpPort.at(0) + ":" + value.toString();
  244. settings.setValue("addrProxy", strNewValue);
  245. setRestartRequired(true);
  246. }
  247. }
  248. break;
  249. case ProxySocksVersion: {
  250. if (settings.value("nSocksVersion") != value) {
  251. settings.setValue("nSocksVersion", value.toInt());
  252. setRestartRequired(true);
  253. }
  254. }
  255. break;
  256. #ifdef ENABLE_WALLET
  257. case Fee: // core option - can be changed on-the-fly
  258. // Todo: Add is valid check and warn via message, if not
  259. nTransactionFee = value.toLongLong();
  260. settings.setValue("nTransactionFee", (qint64)nTransactionFee);
  261. emit transactionFeeChanged(nTransactionFee);
  262. break;
  263. case SpendZeroConfChange:
  264. if (settings.value("bSpendZeroConfChange") != value) {
  265. settings.setValue("bSpendZeroConfChange", value);
  266. setRestartRequired(true);
  267. }
  268. break;
  269. #endif
  270. case DisplayUnit:
  271. nDisplayUnit = value.toInt();
  272. settings.setValue("nDisplayUnit", nDisplayUnit);
  273. emit displayUnitChanged(nDisplayUnit);
  274. break;
  275. case DisplayAddresses:
  276. bDisplayAddresses = value.toBool();
  277. settings.setValue("bDisplayAddresses", bDisplayAddresses);
  278. break;
  279. case ThirdPartyTxUrls:
  280. if (strThirdPartyTxUrls != value.toString()) {
  281. strThirdPartyTxUrls = value.toString();
  282. settings.setValue("strThirdPartyTxUrls", strThirdPartyTxUrls);
  283. setRestartRequired(true);
  284. }
  285. break;
  286. case Language:
  287. if (settings.value("language") != value) {
  288. settings.setValue("language", value);
  289. setRestartRequired(true);
  290. }
  291. break;
  292. case CoinControlFeatures:
  293. fCoinControlFeatures = value.toBool();
  294. settings.setValue("fCoinControlFeatures", fCoinControlFeatures);
  295. emit coinControlFeaturesChanged(fCoinControlFeatures);
  296. break;
  297. case DatabaseCache:
  298. if (settings.value("nDatabaseCache") != value) {
  299. settings.setValue("nDatabaseCache", value);
  300. setRestartRequired(true);
  301. }
  302. break;
  303. case ThreadsScriptVerif:
  304. if (settings.value("nThreadsScriptVerif") != value) {
  305. settings.setValue("nThreadsScriptVerif", value);
  306. setRestartRequired(true);
  307. }
  308. break;
  309. default:
  310. break;
  311. }
  312. }
  313. emit dataChanged(index, index);
  314. return successful;
  315. }
  316. bool OptionsModel::getProxySettings(QNetworkProxy& proxy) const
  317. {
  318. // Directly query current base proxy, because
  319. // GUI settings can be overridden with -proxy.
  320. proxyType curProxy;
  321. if (GetProxy(NET_IPV4, curProxy)) {
  322. if (curProxy.second == 5) {
  323. proxy.setType(QNetworkProxy::Socks5Proxy);
  324. proxy.setHostName(QString::fromStdString(curProxy.first.ToStringIP()));
  325. proxy.setPort(curProxy.first.GetPort());
  326. return true;
  327. }
  328. else
  329. return false;
  330. }
  331. else
  332. proxy.setType(QNetworkProxy::NoProxy);
  333. return true;
  334. }
  335. void OptionsModel::setRestartRequired(bool fRequired)
  336. {
  337. QSettings settings;
  338. return settings.setValue("fRestartRequired", fRequired);
  339. }
  340. bool OptionsModel::isRestartRequired()
  341. {
  342. QSettings settings;
  343. return settings.value("fRestartRequired", false).toBool();
  344. }