Remove headers.h
[VerusCoin.git] / src / qt / optionsmodel.cpp
CommitLineData
92f20d53 1#include "optionsmodel.h"
ee014e5b 2#include "bitcoinunits.h"
3f8cb2c5 3#include <QSettings>
e8ef3da7 4
f18a119a 5#include "init.h"
ed6d0b5f 6#include "db.h"
92f20d53 7
3f8cb2c5
GA
8OptionsModel::OptionsModel(QObject *parent) :
9 QAbstractListModel(parent)
92f20d53 10{
3f8cb2c5 11 Init();
92f20d53
WL
12}
13
3f8cb2c5
GA
14void OptionsModel::Init()
15{
16 QSettings settings;
17
18 // These are QT-only settings:
19 nDisplayUnit = settings.value("nDisplayUnit", BitcoinUnits::BTC).toInt();
20 bDisplayAddresses = settings.value("bDisplayAddresses", false).toBool();
21 fMinimizeToTray = settings.value("fMinimizeToTray", false).toBool();
22 fMinimizeOnClose = settings.value("fMinimizeOnClose", false).toBool();
23 nTransactionFee = settings.value("nTransactionFee").toLongLong();
24
25 // These are shared with core bitcoin; we want
26 // command-line options to override the GUI settings:
27 if (settings.contains("fUseUPnP"))
28 SoftSetBoolArg("-upnp", settings.value("fUseUPnP").toBool());
29 if (settings.contains("addrProxy") && settings.value("fUseProxy").toBool())
30 SoftSetArg("-proxy", settings.value("addrProxy").toString().toStdString());
31}
32
33bool OptionsModel::Upgrade()
34{
35 QSettings settings;
36
37 if (settings.contains("bImportFinished"))
38 return false; // Already upgraded
39
40 settings.setValue("bImportFinished", true);
41
42 // Move settings from old wallet.dat (if any):
43 CWalletDB walletdb("wallet.dat");
44
45 QList<QString> intOptions;
46 intOptions << "nDisplayUnit" << "nTransactionFee";
47 foreach(QString key, intOptions)
48 {
49 int value = 0;
50 if (walletdb.ReadSetting(key.toStdString(), value))
51 {
52 settings.setValue(key, value);
53 walletdb.EraseSetting(key.toStdString());
54 }
55 }
56 QList<QString> boolOptions;
57 boolOptions << "bDisplayAddresses" << "fMinimizeToTray" << "fMinimizeOnClose" << "fUseProxy" << "fUseUPnP";
58 foreach(QString key, boolOptions)
59 {
60 bool value = false;
61 if (walletdb.ReadSetting(key.toStdString(), value))
62 {
63 settings.setValue(key, value);
64 walletdb.EraseSetting(key.toStdString());
65 }
66 }
67 try
68 {
69 CAddress addrProxyAddress;
70 if (walletdb.ReadSetting("addrProxy", addrProxyAddress))
71 {
72 addrProxy = addrProxyAddress;
42c8b56f 73 settings.setValue("addrProxy", addrProxy.ToStringIPPort().c_str());
3f8cb2c5
GA
74 walletdb.EraseSetting("addrProxy");
75 }
76 }
77 catch (std::ios_base::failure &e)
78 {
79 // 0.6.0rc1 saved this as a CService, which causes failure when parsing as a CAddress
80 if (walletdb.ReadSetting("addrProxy", addrProxy))
81 {
42c8b56f 82 settings.setValue("addrProxy", addrProxy.ToStringIPPort().c_str());
3f8cb2c5
GA
83 walletdb.EraseSetting("addrProxy");
84 }
85 }
86 Init();
87
88 return true;
89}
90
91
92f20d53
WL
92int OptionsModel::rowCount(const QModelIndex & parent) const
93{
94 return OptionIDRowCount;
95}
96
97QVariant OptionsModel::data(const QModelIndex & index, int role) const
98{
92f20d53
WL
99 if(role == Qt::EditRole)
100 {
3f8cb2c5 101 QSettings settings;
92f20d53
WL
102 switch(index.row())
103 {
104 case StartAtStartup:
f18a119a 105 return QVariant(GetStartOnSystemStartup());
92f20d53
WL
106 case MinimizeToTray:
107 return QVariant(fMinimizeToTray);
108 case MapPortUPnP:
3f8cb2c5 109 return settings.value("fUseUPnP", GetBoolArg("-upnp", true));
92f20d53
WL
110 case MinimizeOnClose:
111 return QVariant(fMinimizeOnClose);
112 case ConnectSOCKS4:
3f8cb2c5 113 return settings.value("fUseProxy", false);
92f20d53
WL
114 case ProxyIP:
115 return QVariant(QString::fromStdString(addrProxy.ToStringIP()));
116 case ProxyPort:
06706ab8 117 return QVariant(addrProxy.GetPort());
92f20d53 118 case Fee:
587e5285 119 return QVariant(nTransactionFee);
ee014e5b
WL
120 case DisplayUnit:
121 return QVariant(nDisplayUnit);
2f5d3809
WL
122 case DisplayAddresses:
123 return QVariant(bDisplayAddresses);
92f20d53
WL
124 default:
125 return QVariant();
126 }
127 }
128 return QVariant();
129}
130
131bool OptionsModel::setData(const QModelIndex & index, const QVariant & value, int role)
132{
c3e0734d
WL
133 bool successful = true; /* set to false on parse error */
134 if(role == Qt::EditRole)
135 {
3f8cb2c5 136 QSettings settings;
c3e0734d
WL
137 switch(index.row())
138 {
139 case StartAtStartup:
f18a119a 140 successful = SetStartOnSystemStartup(value.toBool());
c3e0734d
WL
141 break;
142 case MinimizeToTray:
143 fMinimizeToTray = value.toBool();
3f8cb2c5 144 settings.setValue("fMinimizeToTray", fMinimizeToTray);
c3e0734d
WL
145 break;
146 case MapPortUPnP:
3f8cb2c5
GA
147 {
148 bool bUseUPnP = value.toBool();
149 settings.setValue("fUseUPnP", bUseUPnP);
150 MapPort(bUseUPnP);
151 }
c3e0734d
WL
152 break;
153 case MinimizeOnClose:
154 fMinimizeOnClose = value.toBool();
3f8cb2c5 155 settings.setValue("fMinimizeOnClose", fMinimizeOnClose);
c3e0734d
WL
156 break;
157 case ConnectSOCKS4:
158 fUseProxy = value.toBool();
3f8cb2c5 159 settings.setValue("fUseProxy", fUseProxy);
c3e0734d
WL
160 break;
161 case ProxyIP:
162 {
0f3981be 163 // Use CAddress to parse and check IP
be4d08b2 164 CNetAddr addr(value.toString().toStdString());
67a42f92 165 if (addr.IsValid())
c3e0734d 166 {
67a42f92 167 addrProxy.SetIP(addr);
42c8b56f 168 settings.setValue("addrProxy", addrProxy.ToStringIPPort().c_str());
8e86dca2
WL
169 }
170 else
171 {
c3e0734d
WL
172 successful = false;
173 }
174 }
175 break;
176 case ProxyPort:
177 {
178 int nPort = atoi(value.toString().toAscii().data());
26ce92b3 179 if (nPort > 0 && nPort < std::numeric_limits<unsigned short>::max())
c3e0734d 180 {
67a42f92 181 addrProxy.SetPort(nPort);
42c8b56f 182 settings.setValue("addrProxy", addrProxy.ToStringIPPort().c_str());
8e86dca2
WL
183 }
184 else
185 {
c3e0734d
WL
186 successful = false;
187 }
188 }
189 break;
190 case Fee: {
587e5285 191 nTransactionFee = value.toLongLong();
3f8cb2c5 192 settings.setValue("nTransactionFee", nTransactionFee);
c3e0734d
WL
193 }
194 break;
ee014e5b
WL
195 case DisplayUnit: {
196 int unit = value.toInt();
197 nDisplayUnit = unit;
3f8cb2c5 198 settings.setValue("nDisplayUnit", nDisplayUnit);
ee014e5b
WL
199 emit displayUnitChanged(unit);
200 }
2f5d3809
WL
201 case DisplayAddresses: {
202 bDisplayAddresses = value.toBool();
3f8cb2c5 203 settings.setValue("bDisplayAddresses", bDisplayAddresses);
2f5d3809 204 }
c3e0734d
WL
205 default:
206 break;
207 }
208 }
92f20d53 209 emit dataChanged(index, index);
c3e0734d
WL
210
211 return successful;
92f20d53 212}
968d55aa
WL
213
214qint64 OptionsModel::getTransactionFee()
215{
216 return nTransactionFee;
217}
c3e0734d 218
352083cb 219bool OptionsModel::getMinimizeToTray()
c3e0734d
WL
220{
221 return fMinimizeToTray;
222}
223
352083cb 224bool OptionsModel::getMinimizeOnClose()
c3e0734d
WL
225{
226 return fMinimizeOnClose;
227}
ee014e5b
WL
228
229int OptionsModel::getDisplayUnit()
230{
231 return nDisplayUnit;
232}
2f5d3809
WL
233
234bool OptionsModel::getDisplayAddresses()
235{
236 return bDisplayAddresses;
237}
This page took 0.136279 seconds and 4 git commands to generate.