Feathercoin  0.5.0
P2P Digital Currency
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
optionsmodel.cpp
Go to the documentation of this file.
1 #include "optionsmodel.h"
2 
3 #include "bitcoinunits.h"
4 #include "init.h"
5 #include "walletdb.h"
6 #include "guiutil.h"
7 
8 #include <QSettings>
9 
10 OptionsModel::OptionsModel(QObject *parent) :
11  QAbstractListModel(parent)
12 {
13  Init();
14 }
15 
16 bool static ApplyProxySettings()
17 {
18  QSettings settings;
19  CService addrProxy(settings.value("addrProxy", "127.0.0.1:9050").toString().toStdString());
20  int nSocksVersion(settings.value("nSocksVersion", 5).toInt());
21  if (!settings.value("fUseProxy", false).toBool()) {
22  addrProxy = CService();
23  nSocksVersion = 0;
24  return false;
25  }
26  if (nSocksVersion && !addrProxy.IsValid())
27  return false;
28  if (!IsLimited(NET_IPV4))
29  SetProxy(NET_IPV4, addrProxy, nSocksVersion);
30  if (nSocksVersion > 4) {
31 #ifdef USE_IPV6
32  if (!IsLimited(NET_IPV6))
33  SetProxy(NET_IPV6, addrProxy, nSocksVersion);
34 #endif
35  SetNameProxy(addrProxy, nSocksVersion);
36  }
37  return true;
38 }
39 
41 {
42  QSettings settings;
43 
44  // These are Qt-only settings:
45  nDisplayUnit = settings.value("nDisplayUnit", BitcoinUnits::BTC).toInt();
46  bDisplayAddresses = settings.value("bDisplayAddresses", false).toBool();
47  fMinimizeToTray = settings.value("fMinimizeToTray", false).toBool();
48  fMinimizeOnClose = settings.value("fMinimizeOnClose", false).toBool();
49  nTransactionFee = settings.value("nTransactionFee").toLongLong();
50  language = settings.value("language", "").toString();
51  fCoinControlFeatures = settings.value("fCoinControlFeatures", false).toBool();
52 
53  // These are shared with core Bitcoin; we want
54  // command-line options to override the GUI settings:
55  if (settings.contains("fUseUPnP"))
56  SoftSetBoolArg("-upnp", settings.value("fUseUPnP").toBool());
57  if (settings.contains("addrProxy") && settings.value("fUseProxy").toBool())
58  SoftSetArg("-proxy", settings.value("addrProxy").toString().toStdString());
59  if (settings.contains("nSocksVersion") && settings.value("fUseProxy").toBool())
60  SoftSetArg("-socks", settings.value("nSocksVersion").toString().toStdString());
61  if (!language.isEmpty())
62  SoftSetArg("-lang", language.toStdString());
63 }
64 
66 {
67  QSettings settings;
68 
69  // Remove all entries in this QSettings object
70  settings.clear();
71 
72  // default setting for OptionsModel::StartAtStartup - disabled
75 
76  // Re-Init to get default values
77  Init();
78 
79  // Ensure Upgrade() is not running again by setting the bImportFinished flag
80  settings.setValue("bImportFinished", true);
81 }
82 
84 {
85  QSettings settings;
86 
87  if (settings.contains("bImportFinished"))
88  return false; // Already upgraded
89 
90  settings.setValue("bImportFinished", true);
91 
92  // Move settings from old wallet.dat (if any):
93  CWalletDB walletdb("wallet.dat");
94 
95  QList<QString> intOptions;
96  intOptions << "nDisplayUnit" << "nTransactionFee";
97  foreach(QString key, intOptions)
98  {
99  int value = 0;
100  if (walletdb.ReadSetting(key.toStdString(), value))
101  {
102  settings.setValue(key, value);
103  walletdb.EraseSetting(key.toStdString());
104  }
105  }
106  QList<QString> boolOptions;
107  boolOptions << "bDisplayAddresses" << "fMinimizeToTray" << "fMinimizeOnClose" << "fUseProxy" << "fUseUPnP";
108  foreach(QString key, boolOptions)
109  {
110  bool value = false;
111  if (walletdb.ReadSetting(key.toStdString(), value))
112  {
113  settings.setValue(key, value);
114  walletdb.EraseSetting(key.toStdString());
115  }
116  }
117  try
118  {
119  CAddress addrProxyAddress;
120  if (walletdb.ReadSetting("addrProxy", addrProxyAddress))
121  {
122  settings.setValue("addrProxy", addrProxyAddress.ToStringIPPort().c_str());
123  walletdb.EraseSetting("addrProxy");
124  }
125  }
126  catch (std::ios_base::failure &e)
127  {
128  // 0.6.0rc1 saved this as a CService, which causes failure when parsing as a CAddress
129  CService addrProxy;
130  if (walletdb.ReadSetting("addrProxy", addrProxy))
131  {
132  settings.setValue("addrProxy", addrProxy.ToStringIPPort().c_str());
133  walletdb.EraseSetting("addrProxy");
134  }
135  }
136  ApplyProxySettings();
137  Init();
138 
139  return true;
140 }
141 
142 
143 int OptionsModel::rowCount(const QModelIndex & parent) const
144 {
145  return OptionIDRowCount;
146 }
147 
148 QVariant OptionsModel::data(const QModelIndex & index, int role) const
149 {
150  if(role == Qt::EditRole)
151  {
152  QSettings settings;
153  switch(index.row())
154  {
155  case StartAtStartup:
156  return QVariant(GUIUtil::GetStartOnSystemStartup());
157  case MinimizeToTray:
158  return QVariant(fMinimizeToTray);
159  case MapPortUPnP:
160 #ifdef USE_UPNP
161  return settings.value("fUseUPnP", GetBoolArg("-upnp", true));
162 #else
163  return QVariant(false);
164 #endif
165  case MinimizeOnClose:
166  return QVariant(fMinimizeOnClose);
167  case ProxyUse: {
168  proxyType proxy;
169  return QVariant(GetProxy(NET_IPV4, proxy));
170  }
171  case ProxyIP: {
172  proxyType proxy;
173  if (GetProxy(NET_IPV4, proxy))
174  return QVariant(QString::fromStdString(proxy.first.ToStringIP()));
175  else
176  return QVariant(QString::fromStdString("127.0.0.1"));
177  }
178  case ProxyPort: {
179  proxyType proxy;
180  if (GetProxy(NET_IPV4, proxy))
181  return QVariant(proxy.first.GetPort());
182  else
183  return QVariant(9050);
184  }
185  case ProxySocksVersion: {
186  proxyType proxy;
187  if (GetProxy(NET_IPV4, proxy))
188  return QVariant(proxy.second);
189  else
190  return QVariant(5);
191  }
192  case Fee:
193  return QVariant(nTransactionFee);
194  case DisplayUnit:
195  return QVariant(nDisplayUnit);
196  case DisplayAddresses:
197  return QVariant(bDisplayAddresses);
198  case Language:
199  return settings.value("language", "");
200  case CoinControlFeatures:
201  return QVariant(fCoinControlFeatures);
202  default:
203  return QVariant();
204  }
205  }
206  return QVariant();
207 }
208 
209 bool OptionsModel::setData(const QModelIndex & index, const QVariant & value, int role)
210 {
211  bool successful = true; /* set to false on parse error */
212  if(role == Qt::EditRole)
213  {
214  QSettings settings;
215  switch(index.row())
216  {
217  case StartAtStartup:
218  successful = GUIUtil::SetStartOnSystemStartup(value.toBool());
219  break;
220  case MinimizeToTray:
221  fMinimizeToTray = value.toBool();
222  settings.setValue("fMinimizeToTray", fMinimizeToTray);
223  break;
224  case MapPortUPnP:
225  settings.setValue("fUseUPnP", value.toBool());
226  MapPort(value.toBool());
227  break;
228  case MinimizeOnClose:
229  fMinimizeOnClose = value.toBool();
230  settings.setValue("fMinimizeOnClose", fMinimizeOnClose);
231  break;
232  case ProxyUse:
233  settings.setValue("fUseProxy", value.toBool());
234  successful = ApplyProxySettings();
235  break;
236  case ProxyIP: {
237  proxyType proxy;
238  proxy.first = CService("127.0.0.1", 9050);
239  GetProxy(NET_IPV4, proxy);
240 
241  CNetAddr addr(value.toString().toStdString());
242  proxy.first.SetIP(addr);
243  settings.setValue("addrProxy", proxy.first.ToStringIPPort().c_str());
244  successful = ApplyProxySettings();
245  }
246  break;
247  case ProxyPort: {
248  proxyType proxy;
249  proxy.first = CService("127.0.0.1", 9050);
250  GetProxy(NET_IPV4, proxy);
251 
252  proxy.first.SetPort(value.toInt());
253  settings.setValue("addrProxy", proxy.first.ToStringIPPort().c_str());
254  successful = ApplyProxySettings();
255  }
256  break;
257  case ProxySocksVersion: {
258  proxyType proxy;
259  proxy.second = 5;
260  GetProxy(NET_IPV4, proxy);
261 
262  proxy.second = value.toInt();
263  settings.setValue("nSocksVersion", proxy.second);
264  successful = ApplyProxySettings();
265  }
266  break;
267  case Fee:
268  nTransactionFee = value.toLongLong();
269  settings.setValue("nTransactionFee", nTransactionFee);
271  break;
272  case DisplayUnit:
273  nDisplayUnit = value.toInt();
274  settings.setValue("nDisplayUnit", nDisplayUnit);
276  break;
277  case DisplayAddresses:
278  bDisplayAddresses = value.toBool();
279  settings.setValue("bDisplayAddresses", bDisplayAddresses);
280  break;
281  case Language:
282  settings.setValue("language", value);
283  break;
284  case CoinControlFeatures: {
285  fCoinControlFeatures = value.toBool();
286  settings.setValue("fCoinControlFeatures", fCoinControlFeatures);
288  }
289  break;
290  default:
291  break;
292  }
293  }
294  emit dataChanged(index, index);
295 
296  return successful;
297 }
298 
300 {
301  return nTransactionFee;
302 }
303 
305 {
306  return fCoinControlFeatures;
307 }
308 
std::string * value
Definition: version_set.cc:270
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
void SetIP(const CNetAddr &ip)
Definition: netbase.cpp:545
bool SoftSetBoolArg(const std::string &strArg, bool fValue)
Set a boolean argument if it doesn't already have a value.
Definition: util.cpp:619
std::string ToStringIPPort() const
Definition: netbase.cpp:1117
void transactionFeeChanged(qint64)
QString language
Definition: optionsmodel.h:60
bool bDisplayAddresses
Definition: optionsmodel.h:57
bool GetStartOnSystemStartup()
Definition: guiutil.cpp:490
void coinControlFeaturesChanged(bool)
OptionsModel(QObject *parent=0)
int64 nTransactionFee
Definition: main.cpp:84
bool fMinimizeOnClose
Definition: optionsmodel.h:59
void MapPort(bool)
Definition: net.cpp:1176
bool getCoinControlFeatures()
bool GetBoolArg(const std::string &strArg, bool fDefault)
Return boolean argument or default value.
Definition: util.cpp:600
bool SoftSetArg(const std::string &strArg, const std::string &strValue)
Set an argument if it doesn't already have a value.
Definition: util.cpp:611
int rowCount(const QModelIndex &parent=QModelIndex()) const
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netbase.h:90
bool fMinimizeToTray
Definition: optionsmodel.h:58
A CService with information about it as peer.
Definition: protocol.h:76
qint64 getTransactionFee()
bool SetNameProxy(CService addrProxy, int nSocksVersion)
Definition: netbase.cpp:441
bool EraseSetting(const std::string &strKey)
Definition: walletdb.h:137
void displayUnitChanged(int unit)
Access to the wallet database (wallet.dat)
Definition: walletdb.h:27
bool fCoinControlFeatures
Definition: optionsmodel.h:61
IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96))
Definition: netbase.h:34
std::pair< CService, int > proxyType
Definition: netbase.h:134
bool GetProxy(enum Network net, proxyType &proxyInfoOut)
Definition: netbase.cpp:432
bool SetStartOnSystemStartup(bool fAutoStart)
Definition: guiutil.cpp:491
bool ReadSetting(const std::string &strKey, T &value)
Definition: walletdb.h:127
bool IsLimited(enum Network net)
Definition: net.cpp:256
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
bool SetProxy(enum Network net, CService addrProxy, int nSocksVersion)
Definition: netbase.cpp:421