Bitcoin Core  21.99.0
P2P Digital Currency
optionsmodel.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2020 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #if defined(HAVE_CONFIG_H)
7 #endif
8 
9 #include <qt/optionsmodel.h>
10 
11 #include <qt/bitcoinunits.h>
12 #include <qt/guiconstants.h>
13 #include <qt/guiutil.h>
14 
15 #include <interfaces/node.h>
16 #include <mapport.h>
17 #include <net.h>
18 #include <netbase.h>
19 #include <txdb.h> // for -dbcache defaults
20 #include <util/string.h>
21 #include <validation.h> // For DEFAULT_SCRIPTCHECK_THREADS
22 
23 #include <QDebug>
24 #include <QSettings>
25 #include <QStringList>
26 
27 const char *DEFAULT_GUI_PROXY_HOST = "127.0.0.1";
28 
29 static const QString GetDefaultProxyAddress();
30 
31 OptionsModel::OptionsModel(QObject *parent, bool resetSettings) :
32  QAbstractListModel(parent)
33 {
34  Init(resetSettings);
35 }
36 
37 void OptionsModel::addOverriddenOption(const std::string &option)
38 {
39  strOverriddenByCommandLine += QString::fromStdString(option) + "=" + QString::fromStdString(gArgs.GetArg(option, "")) + " ";
40 }
41 
42 // Writes all missing QSettings with their default values
43 void OptionsModel::Init(bool resetSettings)
44 {
45  if (resetSettings)
46  Reset();
47 
49 
50  QSettings settings;
51 
52  // Ensure restart flag is unset on client startup
53  setRestartRequired(false);
54 
55  // These are Qt-only settings:
56 
57  // Window
58  if (!settings.contains("fHideTrayIcon")) {
59  settings.setValue("fHideTrayIcon", false);
60  }
61  m_show_tray_icon = !settings.value("fHideTrayIcon").toBool();
63 
64  if (!settings.contains("fMinimizeToTray"))
65  settings.setValue("fMinimizeToTray", false);
66  fMinimizeToTray = settings.value("fMinimizeToTray").toBool() && m_show_tray_icon;
67 
68  if (!settings.contains("fMinimizeOnClose"))
69  settings.setValue("fMinimizeOnClose", false);
70  fMinimizeOnClose = settings.value("fMinimizeOnClose").toBool();
71 
72  // Display
73  if (!settings.contains("nDisplayUnit"))
74  settings.setValue("nDisplayUnit", BitcoinUnits::BTC);
75  nDisplayUnit = settings.value("nDisplayUnit").toInt();
76 
77  if (!settings.contains("strThirdPartyTxUrls"))
78  settings.setValue("strThirdPartyTxUrls", "");
79  strThirdPartyTxUrls = settings.value("strThirdPartyTxUrls", "").toString();
80 
81  if (!settings.contains("fCoinControlFeatures"))
82  settings.setValue("fCoinControlFeatures", false);
83  fCoinControlFeatures = settings.value("fCoinControlFeatures", false).toBool();
84 
85  // These are shared with the core or have a command-line parameter
86  // and we want command-line parameters to overwrite the GUI settings.
87  //
88  // If setting doesn't exist create it with defaults.
89  //
90  // If gArgs.SoftSetArg() or gArgs.SoftSetBoolArg() return false we were overridden
91  // by command-line and show this in the UI.
92 
93  // Main
94  if (!settings.contains("bPrune"))
95  settings.setValue("bPrune", false);
96  if (!settings.contains("nPruneSize"))
97  settings.setValue("nPruneSize", DEFAULT_PRUNE_TARGET_GB);
98  SetPruneEnabled(settings.value("bPrune").toBool());
99 
100  if (!settings.contains("nDatabaseCache"))
101  settings.setValue("nDatabaseCache", (qint64)nDefaultDbCache);
102  if (!gArgs.SoftSetArg("-dbcache", settings.value("nDatabaseCache").toString().toStdString()))
103  addOverriddenOption("-dbcache");
104 
105  if (!settings.contains("nThreadsScriptVerif"))
106  settings.setValue("nThreadsScriptVerif", DEFAULT_SCRIPTCHECK_THREADS);
107  if (!gArgs.SoftSetArg("-par", settings.value("nThreadsScriptVerif").toString().toStdString()))
108  addOverriddenOption("-par");
109 
110  if (!settings.contains("strDataDir"))
111  settings.setValue("strDataDir", GUIUtil::getDefaultDataDirectory());
112 
113  // Wallet
114 #ifdef ENABLE_WALLET
115  if (!settings.contains("bSpendZeroConfChange"))
116  settings.setValue("bSpendZeroConfChange", true);
117  if (!gArgs.SoftSetBoolArg("-spendzeroconfchange", settings.value("bSpendZeroConfChange").toBool()))
118  addOverriddenOption("-spendzeroconfchange");
119 #endif
120 
121  // Network
122  if (!settings.contains("fUseUPnP"))
123  settings.setValue("fUseUPnP", DEFAULT_UPNP);
124  if (!gArgs.SoftSetBoolArg("-upnp", settings.value("fUseUPnP").toBool()))
125  addOverriddenOption("-upnp");
126 
127  if (!settings.contains("fUseNatpmp")) {
128  settings.setValue("fUseNatpmp", DEFAULT_NATPMP);
129  }
130  if (!gArgs.SoftSetBoolArg("-natpmp", settings.value("fUseNatpmp").toBool())) {
131  addOverriddenOption("-natpmp");
132  }
133 
134  if (!settings.contains("fListen"))
135  settings.setValue("fListen", DEFAULT_LISTEN);
136  if (!gArgs.SoftSetBoolArg("-listen", settings.value("fListen").toBool()))
137  addOverriddenOption("-listen");
138 
139  if (!settings.contains("fUseProxy"))
140  settings.setValue("fUseProxy", false);
141  if (!settings.contains("addrProxy"))
142  settings.setValue("addrProxy", GetDefaultProxyAddress());
143  // Only try to set -proxy, if user has enabled fUseProxy
144  if ((settings.value("fUseProxy").toBool() && !gArgs.SoftSetArg("-proxy", settings.value("addrProxy").toString().toStdString())))
145  addOverriddenOption("-proxy");
146  else if(!settings.value("fUseProxy").toBool() && !gArgs.GetArg("-proxy", "").empty())
147  addOverriddenOption("-proxy");
148 
149  if (!settings.contains("fUseSeparateProxyTor"))
150  settings.setValue("fUseSeparateProxyTor", false);
151  if (!settings.contains("addrSeparateProxyTor"))
152  settings.setValue("addrSeparateProxyTor", GetDefaultProxyAddress());
153  // Only try to set -onion, if user has enabled fUseSeparateProxyTor
154  if ((settings.value("fUseSeparateProxyTor").toBool() && !gArgs.SoftSetArg("-onion", settings.value("addrSeparateProxyTor").toString().toStdString())))
155  addOverriddenOption("-onion");
156  else if(!settings.value("fUseSeparateProxyTor").toBool() && !gArgs.GetArg("-onion", "").empty())
157  addOverriddenOption("-onion");
158 
159  // Display
160  if (!settings.contains("language"))
161  settings.setValue("language", "");
162  if (!gArgs.SoftSetArg("-lang", settings.value("language").toString().toStdString()))
163  addOverriddenOption("-lang");
164 
165  language = settings.value("language").toString();
166 }
167 
171 static void CopySettings(QSettings& dst, const QSettings& src)
172 {
173  for (const QString& key : src.allKeys()) {
174  dst.setValue(key, src.value(key));
175  }
176 }
177 
179 static void BackupSettings(const fs::path& filename, const QSettings& src)
180 {
181  qInfo() << "Backing up GUI settings to" << GUIUtil::boostPathToQString(filename);
182  QSettings dst(GUIUtil::boostPathToQString(filename), QSettings::IniFormat);
183  dst.clear();
184  CopySettings(dst, src);
185 }
186 
188 {
189  QSettings settings;
190 
191  // Backup old settings to chain-specific datadir for troubleshooting
192  BackupSettings(GetDataDir(true) / "guisettings.ini.bak", settings);
193 
194  // Save the strDataDir setting
195  QString dataDir = GUIUtil::getDefaultDataDirectory();
196  dataDir = settings.value("strDataDir", dataDir).toString();
197 
198  // Remove all entries from our QSettings object
199  settings.clear();
200 
201  // Set strDataDir
202  settings.setValue("strDataDir", dataDir);
203 
204  // Set that this was reset
205  settings.setValue("fReset", true);
206 
207  // default setting for OptionsModel::StartAtStartup - disabled
210 }
211 
212 int OptionsModel::rowCount(const QModelIndex & parent) const
213 {
214  return OptionIDRowCount;
215 }
216 
217 struct ProxySetting {
218  bool is_set;
219  QString ip;
220  QString port;
221 };
222 
223 static ProxySetting GetProxySetting(QSettings &settings, const QString &name)
224 {
225  static const ProxySetting default_val = {false, DEFAULT_GUI_PROXY_HOST, QString("%1").arg(DEFAULT_GUI_PROXY_PORT)};
226  // Handle the case that the setting is not set at all
227  if (!settings.contains(name)) {
228  return default_val;
229  }
230  // contains IP at index 0 and port at index 1
231  QStringList ip_port = GUIUtil::SplitSkipEmptyParts(settings.value(name).toString(), ":");
232  if (ip_port.size() == 2) {
233  return {true, ip_port.at(0), ip_port.at(1)};
234  } else { // Invalid: return default
235  return default_val;
236  }
237 }
238 
239 static void SetProxySetting(QSettings &settings, const QString &name, const ProxySetting &ip_port)
240 {
241  settings.setValue(name, ip_port.ip + ":" + ip_port.port);
242 }
243 
244 static const QString GetDefaultProxyAddress()
245 {
246  return QString("%1:%2").arg(DEFAULT_GUI_PROXY_HOST).arg(DEFAULT_GUI_PROXY_PORT);
247 }
248 
249 void OptionsModel::SetPruneEnabled(bool prune, bool force)
250 {
251  QSettings settings;
252  settings.setValue("bPrune", prune);
253  const int64_t prune_target_mib = PruneGBtoMiB(settings.value("nPruneSize").toInt());
254  std::string prune_val = prune ? ToString(prune_target_mib) : "0";
255  if (force) {
256  gArgs.ForceSetArg("-prune", prune_val);
257  return;
258  }
259  if (!gArgs.SoftSetArg("-prune", prune_val)) {
260  addOverriddenOption("-prune");
261  }
262 }
263 
264 void OptionsModel::SetPruneTargetGB(int prune_target_gb, bool force)
265 {
266  const bool prune = prune_target_gb > 0;
267  if (prune) {
268  QSettings settings;
269  settings.setValue("nPruneSize", prune_target_gb);
270  }
271  SetPruneEnabled(prune, force);
272 }
273 
274 // read QSettings values and return them
275 QVariant OptionsModel::data(const QModelIndex & index, int role) const
276 {
277  if(role == Qt::EditRole)
278  {
279  QSettings settings;
280  switch(index.row())
281  {
282  case StartAtStartup:
284  case ShowTrayIcon:
285  return m_show_tray_icon;
286  case MinimizeToTray:
287  return fMinimizeToTray;
288  case MapPortUPnP:
289 #ifdef USE_UPNP
290  return settings.value("fUseUPnP");
291 #else
292  return false;
293 #endif // USE_UPNP
294  case MapPortNatpmp:
295 #ifdef USE_NATPMP
296  return settings.value("fUseNatpmp");
297 #else
298  return false;
299 #endif // USE_NATPMP
300  case MinimizeOnClose:
301  return fMinimizeOnClose;
302 
303  // default proxy
304  case ProxyUse:
305  return settings.value("fUseProxy", false);
306  case ProxyIP:
307  return GetProxySetting(settings, "addrProxy").ip;
308  case ProxyPort:
309  return GetProxySetting(settings, "addrProxy").port;
310 
311  // separate Tor proxy
312  case ProxyUseTor:
313  return settings.value("fUseSeparateProxyTor", false);
314  case ProxyIPTor:
315  return GetProxySetting(settings, "addrSeparateProxyTor").ip;
316  case ProxyPortTor:
317  return GetProxySetting(settings, "addrSeparateProxyTor").port;
318 
319 #ifdef ENABLE_WALLET
320  case SpendZeroConfChange:
321  return settings.value("bSpendZeroConfChange");
322 #endif
323  case DisplayUnit:
324  return nDisplayUnit;
325  case ThirdPartyTxUrls:
326  return strThirdPartyTxUrls;
327  case Language:
328  return settings.value("language");
329  case CoinControlFeatures:
330  return fCoinControlFeatures;
331  case Prune:
332  return settings.value("bPrune");
333  case PruneSize:
334  return settings.value("nPruneSize");
335  case DatabaseCache:
336  return settings.value("nDatabaseCache");
337  case ThreadsScriptVerif:
338  return settings.value("nThreadsScriptVerif");
339  case Listen:
340  return settings.value("fListen");
341  default:
342  return QVariant();
343  }
344  }
345  return QVariant();
346 }
347 
348 // write QSettings values
349 bool OptionsModel::setData(const QModelIndex & index, const QVariant & value, int role)
350 {
351  bool successful = true; /* set to false on parse error */
352  if(role == Qt::EditRole)
353  {
354  QSettings settings;
355  switch(index.row())
356  {
357  case StartAtStartup:
358  successful = GUIUtil::SetStartOnSystemStartup(value.toBool());
359  break;
360  case ShowTrayIcon:
361  m_show_tray_icon = value.toBool();
362  settings.setValue("fHideTrayIcon", !m_show_tray_icon);
364  break;
365  case MinimizeToTray:
366  fMinimizeToTray = value.toBool();
367  settings.setValue("fMinimizeToTray", fMinimizeToTray);
368  break;
369  case MapPortUPnP: // core option - can be changed on-the-fly
370  settings.setValue("fUseUPnP", value.toBool());
371  break;
372  case MapPortNatpmp: // core option - can be changed on-the-fly
373  settings.setValue("fUseNatpmp", value.toBool());
374  break;
375  case MinimizeOnClose:
376  fMinimizeOnClose = value.toBool();
377  settings.setValue("fMinimizeOnClose", fMinimizeOnClose);
378  break;
379 
380  // default proxy
381  case ProxyUse:
382  if (settings.value("fUseProxy") != value) {
383  settings.setValue("fUseProxy", value.toBool());
384  setRestartRequired(true);
385  }
386  break;
387  case ProxyIP: {
388  auto ip_port = GetProxySetting(settings, "addrProxy");
389  if (!ip_port.is_set || ip_port.ip != value.toString()) {
390  ip_port.ip = value.toString();
391  SetProxySetting(settings, "addrProxy", ip_port);
392  setRestartRequired(true);
393  }
394  }
395  break;
396  case ProxyPort: {
397  auto ip_port = GetProxySetting(settings, "addrProxy");
398  if (!ip_port.is_set || ip_port.port != value.toString()) {
399  ip_port.port = value.toString();
400  SetProxySetting(settings, "addrProxy", ip_port);
401  setRestartRequired(true);
402  }
403  }
404  break;
405 
406  // separate Tor proxy
407  case ProxyUseTor:
408  if (settings.value("fUseSeparateProxyTor") != value) {
409  settings.setValue("fUseSeparateProxyTor", value.toBool());
410  setRestartRequired(true);
411  }
412  break;
413  case ProxyIPTor: {
414  auto ip_port = GetProxySetting(settings, "addrSeparateProxyTor");
415  if (!ip_port.is_set || ip_port.ip != value.toString()) {
416  ip_port.ip = value.toString();
417  SetProxySetting(settings, "addrSeparateProxyTor", ip_port);
418  setRestartRequired(true);
419  }
420  }
421  break;
422  case ProxyPortTor: {
423  auto ip_port = GetProxySetting(settings, "addrSeparateProxyTor");
424  if (!ip_port.is_set || ip_port.port != value.toString()) {
425  ip_port.port = value.toString();
426  SetProxySetting(settings, "addrSeparateProxyTor", ip_port);
427  setRestartRequired(true);
428  }
429  }
430  break;
431 
432 #ifdef ENABLE_WALLET
433  case SpendZeroConfChange:
434  if (settings.value("bSpendZeroConfChange") != value) {
435  settings.setValue("bSpendZeroConfChange", value);
436  setRestartRequired(true);
437  }
438  break;
439 #endif
440  case DisplayUnit:
441  setDisplayUnit(value);
442  break;
443  case ThirdPartyTxUrls:
444  if (strThirdPartyTxUrls != value.toString()) {
445  strThirdPartyTxUrls = value.toString();
446  settings.setValue("strThirdPartyTxUrls", strThirdPartyTxUrls);
447  setRestartRequired(true);
448  }
449  break;
450  case Language:
451  if (settings.value("language") != value) {
452  settings.setValue("language", value);
453  setRestartRequired(true);
454  }
455  break;
456  case CoinControlFeatures:
457  fCoinControlFeatures = value.toBool();
458  settings.setValue("fCoinControlFeatures", fCoinControlFeatures);
460  break;
461  case Prune:
462  if (settings.value("bPrune") != value) {
463  settings.setValue("bPrune", value);
464  setRestartRequired(true);
465  }
466  break;
467  case PruneSize:
468  if (settings.value("nPruneSize") != value) {
469  settings.setValue("nPruneSize", value);
470  setRestartRequired(true);
471  }
472  break;
473  case DatabaseCache:
474  if (settings.value("nDatabaseCache") != value) {
475  settings.setValue("nDatabaseCache", value);
476  setRestartRequired(true);
477  }
478  break;
479  case ThreadsScriptVerif:
480  if (settings.value("nThreadsScriptVerif") != value) {
481  settings.setValue("nThreadsScriptVerif", value);
482  setRestartRequired(true);
483  }
484  break;
485  case Listen:
486  if (settings.value("fListen") != value) {
487  settings.setValue("fListen", value);
488  setRestartRequired(true);
489  }
490  break;
491  default:
492  break;
493  }
494  }
495 
496  Q_EMIT dataChanged(index, index);
497 
498  return successful;
499 }
500 
502 void OptionsModel::setDisplayUnit(const QVariant &value)
503 {
504  if (!value.isNull())
505  {
506  QSettings settings;
507  nDisplayUnit = value.toInt();
508  settings.setValue("nDisplayUnit", nDisplayUnit);
510  }
511 }
512 
514 {
515  QSettings settings;
516  return settings.setValue("fRestartRequired", fRequired);
517 }
518 
520 {
521  QSettings settings;
522  return settings.value("fRestartRequired", false).toBool();
523 }
524 
526 {
527  // Migration of default values
528  // Check if the QSettings container was already loaded with this client version
529  QSettings settings;
530  static const char strSettingsVersionKey[] = "nSettingsVersion";
531  int settingsVersion = settings.contains(strSettingsVersionKey) ? settings.value(strSettingsVersionKey).toInt() : 0;
532  if (settingsVersion < CLIENT_VERSION)
533  {
534  // -dbcache was bumped from 100 to 300 in 0.13
535  // see https://github.com/bitcoin/bitcoin/pull/8273
536  // force people to upgrade to the new value if they are using 100MB
537  if (settingsVersion < 130000 && settings.contains("nDatabaseCache") && settings.value("nDatabaseCache").toLongLong() == 100)
538  settings.setValue("nDatabaseCache", (qint64)nDefaultDbCache);
539 
540  settings.setValue(strSettingsVersionKey, CLIENT_VERSION);
541  }
542 
543  // Overwrite the 'addrProxy' setting in case it has been set to an illegal
544  // default value (see issue #12623; PR #12650).
545  if (settings.contains("addrProxy") && settings.value("addrProxy").toString().endsWith("%2")) {
546  settings.setValue("addrProxy", GetDefaultProxyAddress());
547  }
548 
549  // Overwrite the 'addrSeparateProxyTor' setting in case it has been set to an illegal
550  // default value (see issue #12623; PR #12650).
551  if (settings.contains("addrSeparateProxyTor") && settings.value("addrSeparateProxyTor").toString().endsWith("%2")) {
552  settings.setValue("addrSeparateProxyTor", GetDefaultProxyAddress());
553  }
554 }
static const int DEFAULT_SCRIPTCHECK_THREADS
-par default (number of script-checking threads, 0 = auto)
Definition: validation.h:72
static constexpr int DEFAULT_PRUNE_TARGET_GB
Definition: guiconstants.h:56
static void BackupSettings(const fs::path &filename, const QSettings &src)
Back up a QSettings to an ini-formatted file.
void addOverriddenOption(const std::string &option)
bool SoftSetBoolArg(const std::string &strArg, bool fValue)
Set a boolean argument if it doesn&#39;t already have a value.
Definition: system.cpp:493
static void CopySettings(QSettings &dst, const QSettings &src)
Helper function to copy contents from one QSettings to another.
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
void SetPruneTargetGB(int prune_target_gb, bool force=false)
static const bool DEFAULT_LISTEN
-listen default
Definition: net.h:69
QString language
Definition: optionsmodel.h:107
bool GetStartOnSystemStartup()
Definition: guiutil.cpp:731
void coinControlFeaturesChanged(bool)
void ForceSetArg(const std::string &strArg, const std::string &strValue)
Definition: system.cpp:501
static int64_t PruneGBtoMiB(int gb)
Convert displayed prune target GB to configured MiB.
Definition: optionsmodel.h:31
bool SoftSetArg(const std::string &strArg, const std::string &strValue)
Set an argument if it doesn&#39;t already have a value.
Definition: system.cpp:485
bool fMinimizeOnClose
Definition: optionsmodel.h:106
bool isRestartRequired() const
static constexpr bool DEFAULT_UPNP
Definition: mapport.h:11
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:71
int rowCount(const QModelIndex &parent=QModelIndex()) const override
void setDisplayUnit(const QVariant &value)
Updates current unit in memory, settings and emits displayUnitChanged(newUnit) signal.
OptionsModel(QObject *parent=nullptr, bool resetSettings=false)
void SetPruneEnabled(bool prune, bool force=false)
const char * name
Definition: rest.cpp:41
bool fMinimizeToTray
Definition: optionsmodel.h:105
const fs::path & GetDataDir(bool fNetSpecific)
Definition: system.cpp:720
const char * DEFAULT_GUI_PROXY_HOST
static const int64_t nDefaultDbCache
-dbcache default (MiB)
Definition: txdb.h:24
bool m_show_tray_icon
Definition: optionsmodel.h:104
void Init(bool resetSettings=false)
void displayUnitChanged(int unit)
bool fCoinControlFeatures
Definition: optionsmodel.h:110
static constexpr uint16_t DEFAULT_GUI_PROXY_PORT
Definition: optionsmodel.h:21
void checkAndMigrate()
QStringList SplitSkipEmptyParts(const QString &string, const SeparatorType &separator)
Splits the string into substrings wherever separator occurs, and returns the list of those strings...
Definition: guiutil.h:342
static constexpr bool DEFAULT_NATPMP
Definition: mapport.h:17
void setRestartRequired(bool fRequired)
QString strThirdPartyTxUrls
Definition: optionsmodel.h:109
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: system.cpp:467
ArgsManager gArgs
Definition: system.cpp:77
bool SetStartOnSystemStartup(bool fAutoStart)
Definition: guiutil.cpp:732
QString getDefaultDataDirectory()
Determine default data directory for operating system.
Definition: guiutil.cpp:258
QString boostPathToQString(const fs::path &path)
Convert OS specific boost path to QString through UTF-8.
Definition: guiutil.cpp:747
static ProxySetting GetProxySetting(QSettings &settings, const QString &name)
static const int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
Definition: clientversion.h:38
static const QString GetDefaultProxyAddress()
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
QString strOverriddenByCommandLine
Definition: optionsmodel.h:112
static void SetProxySetting(QSettings &settings, const QString &name, const ProxySetting &ip_port)
void showTrayIconChanged(bool)