Anoncoin  0.9.4
P2P Digital Currency
optionsmodel.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2014 The Bitcoin developers
2 // Copyright (c) 2013-2015 The Anoncoin Core developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "optionsmodel.h"
7 // Anoncoin-config.h has been loaded...
8 
9 #include "anoncoinunits.h"
10 #include "guiutil.h"
11 
12 #include "init.h"
13 #include "main.h"
14 #include "net.h"
15 #include "txdb.h" // for -dbcache defaults
16 #ifdef ENABLE_WALLET
17 #include "wallet.h"
18 #include "walletdb.h"
19 #endif
20 #ifdef ENABLE_I2PSAM
21 #include "i2pwrapper.h"
22 #endif
23 
24 #include <QNetworkProxy>
25 #include <QSettings>
26 #include <QStringList>
27 
28 // Only needed because we have a temporary readonly on the i2p settings
29 #ifdef NOTYET_ENABLE_I2PSAM
30 #include <QMessageBox>
31  QMessageBox::warning( NULL, "Notice - Upgrade in progress",
32  "Values are now for read-only purposes only",
33  QMessageBox::Ok, QMessageBox::Ok );
34 #endif
35 
36 OptionsModel::OptionsModel(QObject *parent) :
37  QAbstractListModel(parent)
38 {
39  Init();
40 }
41 
42 void OptionsModel::addOverriddenOption(const std::string &option)
43 {
44  strOverriddenByCommandLine += QString::fromStdString(option) + "=" + QString::fromStdString(mapArgs[option]) + " ";
45 }
46 
47 // Writes all missing QSettings with their default values
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("fMinimizeToTray"))
59  settings.setValue("fMinimizeToTray", false);
60  fMinimizeToTray = settings.value("fMinimizeToTray").toBool();
61 
62  if (!settings.contains("fMinimizeOnClose"))
63  settings.setValue("fMinimizeOnClose", false);
64  fMinimizeOnClose = settings.value("fMinimizeOnClose").toBool();
65 
66  // Display
67  if (!settings.contains("nDisplayUnit"))
68  settings.setValue("nDisplayUnit", AnoncoinUnits::ANC);
69  nDisplayUnit = settings.value("nDisplayUnit").toInt();
70 
71  if (!settings.contains("strThirdPartyTxUrls"))
72  settings.setValue("strThirdPartyTxUrls", "");
73  strThirdPartyTxUrls = settings.value("strThirdPartyTxUrls", "").toString();
74 
75  if (!settings.contains("fCoinControlFeatures"))
76  settings.setValue("fCoinControlFeatures", false);
77  fCoinControlFeatures = settings.value("fCoinControlFeatures", false).toBool();
78 
79  // These are shared with the core or have a command-line parameter
80  // and we want command-line parameters to overwrite the GUI settings.
81  //
82  // If setting doesn't exist create it with defaults.
83  //
84  // If SoftSetArg() or SoftSetBoolArg() return false we were overridden
85  // by command-line and show this in the UI.
86 
87  // Main
88  if (!settings.contains("nDatabaseCache"))
89  settings.setValue("nDatabaseCache", (qint64)nDefaultDbCache);
90  if (!SoftSetArg("-dbcache", settings.value("nDatabaseCache").toString().toStdString()))
91  addOverriddenOption("-dbcache");
92 
93  if (!settings.contains("nThreadsScriptVerif"))
94  settings.setValue("nThreadsScriptVerif", DEFAULT_SCRIPTCHECK_THREADS);
95  if (!SoftSetArg("-par", settings.value("nThreadsScriptVerif").toString().toStdString()))
96  addOverriddenOption("-par");
97 
98  // Wallet
99 #ifdef ENABLE_WALLET
100  if (!settings.contains("nTransactionFee"))
101  settings.setValue("nTransactionFee", (qint64)DEFAULT_TRANSACTION_FEE);
102  nTransactionFee = settings.value("nTransactionFee").toLongLong(); // if -paytxfee is set, this will be overridden later in init.cpp
103  if (mapArgs.count("-paytxfee"))
104  addOverriddenOption("-paytxfee");
105 
106  if (!settings.contains("bSpendZeroConfChange"))
107  settings.setValue("bSpendZeroConfChange", true);
108  if (!SoftSetBoolArg("-spendzeroconfchange", settings.value("bSpendZeroConfChange").toBool()))
109  addOverriddenOption("-spendzeroconfchange");
110 #endif
111 
112  // Network
113  if (!settings.contains("fUseUPnP"))
114 #ifdef USE_UPNP
115  settings.setValue("fUseUPnP", true);
116 #else
117  settings.setValue("fUseUPnP", false);
118 #endif
119  if (!SoftSetBoolArg("-upnp", settings.value("fUseUPnP").toBool()))
120  addOverriddenOption("-upnp");
121 
122  if (!settings.contains("fUseProxy"))
123  settings.setValue("fUseProxy", false);
124  if (!settings.contains("addrProxy"))
125  settings.setValue("addrProxy", "127.0.0.1:9050");
126  // Only try to set -proxy, if user has enabled fUseProxy
127  if (settings.value("fUseProxy").toBool() && !SoftSetArg("-proxy", settings.value("addrProxy").toString().toStdString()))
128  addOverriddenOption("-proxy");
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 #ifdef ENABLE_I2PSAM
138  // Initialize our options model parameters for I2P session variables, from what has already been set, primarily from the variables in anoncoin.conf
139  // ToDo: These have become read-only, yet the interface still needs to be changed.
140  I2PUseI2POnly = IsI2POnly();
141  I2PSAMHost = QString::fromStdString( GetArg( "-i2p.options.samhost", SAM_DEFAULT_ADDRESS ) );
142  I2PSAMPort = (int)GetArg( "-i2p.options.samport", SAM_DEFAULT_PORT );
143  I2PSessionName = QString::fromStdString( GetArg( "-i2p.options.sessionname", I2P_SESSION_NAME_DEFAULT ) );
144 
145  i2pInboundQuantity = (int)GetArg( "-i2p.options.inbound.quantity" , SAM_DEFAULT_INBOUND_QUANTITY );
146  i2pInboundLength = (int)GetArg( "-i2p.options.inbound.length" , SAM_DEFAULT_INBOUND_LENGTH );
147  i2pInboundLengthVariance = (int)GetArg( "-i2p.options.inbound.lengthvariance" , SAM_DEFAULT_INBOUND_LENGTHVARIANCE );
148  i2pInboundBackupQuantity = (int)GetArg( "-i2p.options.inbound.backupquantity" , SAM_DEFAULT_INBOUND_BACKUPQUANTITY );
149  i2pInboundAllowZeroHop = GetBoolArg( "-i2p.options.inbound.allowzerohop" , SAM_DEFAULT_INBOUND_ALLOWZEROHOP );
150  i2pInboundIPRestriction = (int)GetArg( "-i2p.options.inbound.iprestriction" , SAM_DEFAULT_INBOUND_IPRESTRICTION );
151  i2pOutboundQuantity = (int)GetArg( "-i2p.options.outbound.quantity" , SAM_DEFAULT_OUTBOUND_QUANTITY );
152  i2pOutboundLength = (int)GetArg( "-i2p.options.outbound.length" , SAM_DEFAULT_OUTBOUND_LENGTH );
153  i2pOutboundLengthVariance = (int)GetArg( "-i2p.options.outbound.lengthvariance" , SAM_DEFAULT_OUTBOUND_LENGTHVARIANCE );
154  i2pOutboundBackupQuantity = (int)GetArg( "-i2p.options.outbound.backupquantity" , SAM_DEFAULT_OUTBOUND_BACKUPQUANTITY );
155  i2pOutboundAllowZeroHop = GetBoolArg( "-i2p.options.outbound.allowzerohop" , SAM_DEFAULT_OUTBOUND_ALLOWZEROHOP );
156  i2pOutboundIPRestriction = (int)GetArg( "-i2p.options.outbound.iprestriction" , SAM_DEFAULT_OUTBOUND_IPRESTRICTION );
157  i2pOutboundPriority = (int)GetArg( "-i2p.options.outbound.priority" , SAM_DEFAULT_OUTBOUND_PRIORITY );
158 #endif // ENABLE_I2PSAM
159 }
160 
162 {
163  QSettings settings;
164 
165  // Remove all entries from our QSettings object
166  settings.clear();
167 
168  // default setting for OptionsModel::StartAtStartup - disabled
171 }
172 
173 int OptionsModel::rowCount(const QModelIndex & parent) const
174 {
175  return OptionIDRowCount;
176 }
177 
178 // read QSettings values and return them
179 QVariant OptionsModel::data(const QModelIndex & index, int role) const
180 {
181  if(role == Qt::EditRole)
182  {
183  QSettings settings;
184  switch(index.row())
185  {
186  case StartAtStartup:
188  case MinimizeToTray:
189  return fMinimizeToTray;
190  case MapPortUPnP:
191 #ifdef USE_UPNP
192  return settings.value("fUseUPnP");
193 #else
194  return false;
195 #endif
196  case MinimizeOnClose:
197  return fMinimizeOnClose;
198 
199  // default proxy
200  case ProxyUse:
201  return settings.value("fUseProxy", false);
202  case ProxyIP: {
203  // contains IP at index 0 and port at index 1
204  QStringList strlIpPort = settings.value("addrProxy").toString().split(":", QString::SkipEmptyParts);
205  return strlIpPort.at(0);
206  }
207  case ProxyPort: {
208  // contains IP at index 0 and port at index 1
209  QStringList strlIpPort = settings.value("addrProxy").toString().split(":", QString::SkipEmptyParts);
210  return strlIpPort.at(1);
211  }
212 
213 #ifdef ENABLE_WALLET
214  case Fee:
215  // Attention: Init() is called before nTransactionFee is set in AppInit2()!
216  // To ensure we can change the fee on-the-fly update our QSetting when
217  // opening OptionsDialog, which queries Fee via the mapper.
218  if (nTransactionFee != settings.value("nTransactionFee").toLongLong())
219  settings.setValue("nTransactionFee", (qint64)nTransactionFee);
220  // Todo: Consider to revert back to use just nTransactionFee here, if we don't want
221  // -paytxfee to update our QSettings!
222  return settings.value("nTransactionFee");
223  case SpendZeroConfChange:
224  return settings.value("bSpendZeroConfChange");
225 #endif
226  case DisplayUnit:
227  return nDisplayUnit;
228  case ThirdPartyTxUrls:
229  return strThirdPartyTxUrls;
230  case Language:
231  return settings.value("language");
232  case CoinControlFeatures:
233  return fCoinControlFeatures;
234  case DatabaseCache:
235  return settings.value("nDatabaseCache");
236  case ThreadsScriptVerif:
237  return settings.value("nThreadsScriptVerif");
238 #ifdef ENABLE_I2PSAM
239  case eI2PUseI2POnly:
240  return QVariant( I2PUseI2POnly );
241  case eI2PSAMHost:
242  return QVariant( I2PSAMHost );
243  case eI2PSAMPort:
244  return QVariant( I2PSAMPort );
245  case eI2PSessionName:
246  return QVariant( I2PSessionName );
247  case I2PInboundQuantity:
248  return QVariant(i2pInboundQuantity);
249  case I2PInboundLength:
250  return QVariant(i2pInboundLength);
251  case I2PInboundLengthVariance:
252  return QVariant(i2pInboundLengthVariance);
253  case I2PInboundBackupQuantity:
254  return QVariant(i2pInboundBackupQuantity);
255  case I2PInboundAllowZeroHop:
256  return QVariant(i2pInboundAllowZeroHop);
257  case I2PInboundIPRestriction:
258  return QVariant(i2pInboundIPRestriction);
259  case I2POutboundQuantity:
260  return QVariant(i2pOutboundQuantity);
261  case I2POutboundLength:
262  return QVariant(i2pOutboundLength);
263  case I2POutboundLengthVariance:
264  return QVariant(i2pOutboundLengthVariance);
265  case I2POutboundBackupQuantity:
266  return QVariant(i2pOutboundBackupQuantity);
267  case I2POutboundAllowZeroHop:
268  return QVariant(i2pOutboundAllowZeroHop);
269  case I2POutboundIPRestriction:
270  return QVariant(i2pOutboundIPRestriction);
271  case I2POutboundPriority:
272  return QVariant(i2pOutboundPriority);
273 #endif // ENABLE_I2PSAM
274  default:
275  return QVariant();
276  }
277  }
278  return QVariant();
279 }
280 
281 // write QSettings values
282 bool OptionsModel::setData(const QModelIndex & index, const QVariant & value, int role)
283 {
284  bool successful = true; /* set to false on parse error */
285  if(role == Qt::EditRole)
286  {
287  QSettings settings;
288  switch(index.row())
289  {
290  case StartAtStartup:
291  successful = GUIUtil::SetStartOnSystemStartup(value.toBool());
292  break;
293  case MinimizeToTray:
294  fMinimizeToTray = value.toBool();
295  settings.setValue("fMinimizeToTray", fMinimizeToTray);
296  break;
297  case MapPortUPnP: // core option - can be changed on-the-fly
298  settings.setValue("fUseUPnP", value.toBool());
299  MapPort(value.toBool());
300  break;
301  case MinimizeOnClose:
302  fMinimizeOnClose = value.toBool();
303  settings.setValue("fMinimizeOnClose", fMinimizeOnClose);
304  break;
305 
306  // default proxy
307  case ProxyUse:
308  if (settings.value("fUseProxy") != value) {
309  settings.setValue("fUseProxy", value.toBool());
310  setRestartRequired(true);
311  }
312  break;
313  case ProxyIP: {
314  // contains current IP at index 0 and current port at index 1
315  QStringList strlIpPort = settings.value("addrProxy").toString().split(":", QString::SkipEmptyParts);
316  // if that key doesn't exist or has a changed IP
317  if (!settings.contains("addrProxy") || strlIpPort.at(0) != value.toString()) {
318  // construct new value from new IP and current port
319  QString strNewValue = value.toString() + ":" + strlIpPort.at(1);
320  settings.setValue("addrProxy", strNewValue);
321  setRestartRequired(true);
322  }
323  }
324  break;
325  case ProxyPort: {
326  // contains current IP at index 0 and current port at index 1
327  QStringList strlIpPort = settings.value("addrProxy").toString().split(":", QString::SkipEmptyParts);
328  // if that key doesn't exist or has a changed port
329  if (!settings.contains("addrProxy") || strlIpPort.at(1) != value.toString()) {
330  // construct new value from current IP and new port
331  QString strNewValue = strlIpPort.at(0) + ":" + value.toString();
332  settings.setValue("addrProxy", strNewValue);
333  setRestartRequired(true);
334  }
335  }
336  break;
337 #ifdef ENABLE_WALLET
338  case Fee: // core option - can be changed on-the-fly
339  // Todo: Add is valid check and warn via message, if not
340  nTransactionFee = value.toLongLong();
341  settings.setValue("nTransactionFee", (qint64)nTransactionFee);
342  emit transactionFeeChanged(nTransactionFee);
343  break;
344  case SpendZeroConfChange:
345  if (settings.value("bSpendZeroConfChange") != value) {
346  settings.setValue("bSpendZeroConfChange", value);
347  setRestartRequired(true);
348  }
349  break;
350 #endif
351  case DisplayUnit:
352  setDisplayUnit(value);
353  break;
354  case ThirdPartyTxUrls:
355  if (strThirdPartyTxUrls != value.toString()) {
356  strThirdPartyTxUrls = value.toString();
357  settings.setValue("strThirdPartyTxUrls", strThirdPartyTxUrls);
358  setRestartRequired(true);
359  }
360  break;
361  case Language:
362  if (settings.value("language") != value) {
363  settings.setValue("language", value);
364  setRestartRequired(true);
365  }
366  break;
367  case CoinControlFeatures:
368  fCoinControlFeatures = value.toBool();
369  settings.setValue("fCoinControlFeatures", fCoinControlFeatures);
371  break;
372  case DatabaseCache:
373  if (settings.value("nDatabaseCache") != value) {
374  settings.setValue("nDatabaseCache", value);
375  setRestartRequired(true);
376  }
377  break;
378  case ThreadsScriptVerif:
379  if (settings.value("nThreadsScriptVerif") != value) {
380  settings.setValue("nThreadsScriptVerif", value);
381  setRestartRequired(true);
382  }
383  break;
384 #ifdef ENABLE_I2PSAM
385  case eI2PUseI2POnly:
386  case eI2PSAMHost:
387  case eI2PSAMPort:
388  case eI2PSessionName:
389  case I2PInboundQuantity:
390  case I2PInboundLength:
391  case I2PInboundLengthVariance:
392  case I2PInboundBackupQuantity:
393  case I2PInboundAllowZeroHop:
394  case I2PInboundIPRestriction:
395  case I2POutboundQuantity:
396  case I2POutboundLength:
397  case I2POutboundLengthVariance:
398  case I2POutboundBackupQuantity:
399  case I2POutboundAllowZeroHop:
400  case I2POutboundIPRestriction:
401  case I2POutboundPriority:
402  break;
403 #endif // ENABLE_I2PSAM
404  default:
405  break;
406  }
407  }
408 // ToDo: Finish fixing this so that it is readonly or removed
409 #ifdef ENABLE_I2PSAM
410  if( index.row() < eI2PUseI2POnly )
411 #endif
412  emit dataChanged(index, index);
413 
414  return successful;
415 }
416 
418 void OptionsModel::setDisplayUnit(const QVariant &value)
419 {
420  if (!value.isNull())
421  {
422  QSettings settings;
423  nDisplayUnit = value.toInt();
424  settings.setValue("nDisplayUnit", nDisplayUnit);
426  }
427 }
428 
429 bool OptionsModel::getProxySettings(QNetworkProxy& proxy) const
430 {
431  // Directly query current base proxy, because
432  // GUI settings can be overridden with -proxy.
433  proxyType curProxy;
434  if (GetProxy(NET_IPV4, curProxy)) {
435  proxy.setType(QNetworkProxy::Socks5Proxy);
436  proxy.setHostName(QString::fromStdString(curProxy.ToStringIP()));
437  proxy.setPort(curProxy.GetPort());
438 
439  return true;
440  }
441  else
442  proxy.setType(QNetworkProxy::NoProxy);
443 
444  return false;
445 }
446 
448 {
449  QSettings settings;
450  return settings.setValue("fRestartRequired", fRequired);
451 }
452 
454 {
455  QSettings settings;
456  return settings.value("fRestartRequired", false).toBool();
457 }
#define SAM_DEFAULT_OUTBOUND_IPRESTRICTION
Definition: i2psam.h:59
#define SAM_DEFAULT_ADDRESS
Definition: i2psam.h:28
#define SAM_DEFAULT_INBOUND_IPRESTRICTION
Definition: i2psam.h:47
unsigned short GetPort() const
Definition: netbase.cpp:1216
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.
std::string ToStringIP() const
Definition: netbase.cpp:882
bool SoftSetBoolArg(const std::string &strArg, bool fValue)
Set a boolean argument if it doesn't already have a value.
Definition: util.cpp:539
#define USE_UPNP
void transactionFeeChanged(qint64)
#define SAM_DEFAULT_INBOUND_BACKUPQUANTITY
Definition: i2psam.h:43
QString language
Definition: optionsmodel.h:101
bool GetStartOnSystemStartup()
Definition: guiutil.cpp:733
void coinControlFeaturesChanged(bool)
OptionsModel(QObject *parent=0)
bool fMinimizeOnClose
Definition: optionsmodel.h:100
#define SAM_DEFAULT_OUTBOUND_ALLOWZEROHOP
Definition: i2psam.h:57
#define I2P_SESSION_NAME_DEFAULT
Definition: i2pwrapper.h:12
void MapPort(bool)
Definition: net.cpp:1327
#define SAM_DEFAULT_OUTBOUND_LENGTH
Definition: i2psam.h:51
bool GetBoolArg(const std::string &strArg, bool fDefault)
Return boolean argument or default value.
Definition: util.cpp:520
bool SoftSetArg(const std::string &strArg, const std::string &strValue)
Set an argument if it doesn't already have a value.
Definition: util.cpp:531
void setDisplayUnit(const QVariant &value)
Updates current unit in memory, settings and emits displayUnitChanged(newUnit) signal.
int64_t nTransactionFee
Definition: wallet.cpp:20
#define SAM_DEFAULT_INBOUND_QUANTITY
Definition: i2psam.h:37
int rowCount(const QModelIndex &parent=QModelIndex()) const
bool getProxySettings(QNetworkProxy &proxy) const
#define SAM_DEFAULT_OUTBOUND_LENGTHVARIANCE
Definition: i2psam.h:53
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netbase.h:109
#define SAM_DEFAULT_OUTBOUND_PRIORITY
Definition: i2psam.h:61
bool fMinimizeToTray
Definition: optionsmodel.h:99
#define SAM_DEFAULT_INBOUND_ALLOWZEROHOP
Definition: i2psam.h:45
void displayUnitChanged(int unit)
#define SAM_DEFAULT_INBOUND_LENGTH
Definition: i2psam.h:39
bool fCoinControlFeatures
Definition: optionsmodel.h:104
#define SAM_DEFAULT_INBOUND_LENGTHVARIANCE
Definition: i2psam.h:41
#define SAM_DEFAULT_PORT
Definition: i2psam.h:29
void setRestartRequired(bool fRequired)
QString strThirdPartyTxUrls
Definition: optionsmodel.h:103
#define SAM_DEFAULT_OUTBOUND_BACKUPQUANTITY
Definition: i2psam.h:55
bool GetProxy(enum Network net, proxyType &proxyInfoOut)
Definition: netbase.cpp:476
bool SetStartOnSystemStartup(bool fAutoStart)
Definition: guiutil.cpp:734
std::string GetArg(const std::string &strArg, const std::string &strDefault)
Return string argument or default value.
Definition: util.cpp:506
bool isRestartRequired()
#define SAM_DEFAULT_OUTBOUND_QUANTITY
Definition: i2psam.h:49
map< string, string > mapArgs
Definition: util.cpp:89
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
QString strOverriddenByCommandLine
Definition: optionsmodel.h:106