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