Allow user to select wallet file (-waller=walletfilename.dat)
[novacoin.git] / src / qt / optionsmodel.cpp
1 #include "optionsmodel.h"
2 #include "bitcoinunits.h"
3 #include <QSettings>
4
5 #include "init.h"
6 #include "walletdb.h"
7 #include "guiutil.h"
8
9 OptionsModel::OptionsModel(QObject *parent) :
10     QAbstractListModel(parent)
11 {
12     Init();
13 }
14
15 bool static ApplyProxySettings()
16 {
17     QSettings settings;
18     CService addrProxy(settings.value("addrProxy", "127.0.0.1:9050").toString().toStdString());
19     int nSocksVersion(settings.value("nSocksVersion", 5).toInt());
20     if (!settings.value("fUseProxy", false).toBool()) {
21         addrProxy = CService();
22         nSocksVersion = 0;
23         return false;
24     }
25     if (nSocksVersion && !addrProxy.IsValid())
26         return false;
27     if (!IsLimited(NET_IPV4))
28         SetProxy(NET_IPV4, addrProxy, nSocksVersion);
29     if (nSocksVersion > 4) {
30 #ifdef USE_IPV6
31         if (!IsLimited(NET_IPV6))
32             SetProxy(NET_IPV6, addrProxy, nSocksVersion);
33 #endif
34         SetNameProxy(addrProxy, nSocksVersion);
35     }
36     return true;
37 }
38
39 void OptionsModel::Init()
40 {
41     QSettings settings;
42
43     // These are Qt-only settings:
44     nDisplayUnit = settings.value("nDisplayUnit", BitcoinUnits::BTC).toInt();
45     bDisplayAddresses = settings.value("bDisplayAddresses", false).toBool();
46     fMinimizeToTray = settings.value("fMinimizeToTray", false).toBool();
47     fMinimizeOnClose = settings.value("fMinimizeOnClose", false).toBool();
48     nTransactionFee = settings.value("nTransactionFee").toLongLong();
49     language = settings.value("language", "").toString();
50
51     // These are shared with core Bitcoin; we want
52     // command-line options to override the GUI settings:
53     if (settings.contains("fUseUPnP"))
54         SoftSetBoolArg("-upnp", settings.value("fUseUPnP").toBool());
55     if (settings.contains("addrProxy") && settings.value("fUseProxy").toBool())
56         SoftSetArg("-proxy", settings.value("addrProxy").toString().toStdString());
57     if (settings.contains("nSocksVersion") && settings.value("fUseProxy").toBool())
58         SoftSetArg("-socks", settings.value("nSocksVersion").toString().toStdString());
59     if (settings.contains("detachDB"))
60         SoftSetBoolArg("-detachdb", settings.value("detachDB").toBool());
61     if (!language.isEmpty())
62         SoftSetArg("-lang", language.toStdString());
63 }
64
65 bool OptionsModel::Upgrade()
66 {
67     QSettings settings;
68
69     if (settings.contains("bImportFinished"))
70         return false; // Already upgraded
71
72     settings.setValue("bImportFinished", true);
73
74     // Move settings from old wallet.dat (if any):
75     CWalletDB walletdb(strWalletFileName);
76
77     QList<QString> intOptions;
78     intOptions << "nDisplayUnit" << "nTransactionFee";
79     foreach(QString key, intOptions)
80     {
81         int value = 0;
82         if (walletdb.ReadSetting(key.toStdString(), value))
83         {
84             settings.setValue(key, value);
85             walletdb.EraseSetting(key.toStdString());
86         }
87     }
88     QList<QString> boolOptions;
89     boolOptions << "bDisplayAddresses" << "fMinimizeToTray" << "fMinimizeOnClose" << "fUseProxy" << "fUseUPnP";
90     foreach(QString key, boolOptions)
91     {
92         bool value = false;
93         if (walletdb.ReadSetting(key.toStdString(), value))
94         {
95             settings.setValue(key, value);
96             walletdb.EraseSetting(key.toStdString());
97         }
98     }
99     try
100     {
101         CAddress addrProxyAddress;
102         if (walletdb.ReadSetting("addrProxy", addrProxyAddress))
103         {
104             settings.setValue("addrProxy", addrProxyAddress.ToStringIPPort().c_str());
105             walletdb.EraseSetting("addrProxy");
106         }
107     }
108     catch (std::ios_base::failure &e)
109     {
110         // 0.6.0rc1 saved this as a CService, which causes failure when parsing as a CAddress
111         CService addrProxy;
112         if (walletdb.ReadSetting("addrProxy", addrProxy))
113         {
114             settings.setValue("addrProxy", addrProxy.ToStringIPPort().c_str());
115             walletdb.EraseSetting("addrProxy");
116         }
117     }
118     ApplyProxySettings();
119     Init();
120
121     return true;
122 }
123
124
125 int OptionsModel::rowCount(const QModelIndex & parent) const
126 {
127     return OptionIDRowCount;
128 }
129
130 QVariant OptionsModel::data(const QModelIndex & index, int role) const
131 {
132     if(role == Qt::EditRole)
133     {
134         QSettings settings;
135         switch(index.row())
136         {
137         case StartAtStartup:
138             return QVariant(GUIUtil::GetStartOnSystemStartup());
139         case MinimizeToTray:
140             return QVariant(fMinimizeToTray);
141         case MapPortUPnP:
142             return settings.value("fUseUPnP", GetBoolArg("-upnp", true));
143         case MinimizeOnClose:
144             return QVariant(fMinimizeOnClose);
145         case ProxyUse:
146             return settings.value("fUseProxy", false);
147         case ProxyIP: {
148             proxyType proxy;
149             if (GetProxy(NET_IPV4, proxy))
150                 return QVariant(QString::fromStdString(proxy.first.ToStringIP()));
151             else
152                 return QVariant(QString::fromStdString("127.0.0.1"));
153         }
154         case ProxyPort: {
155             proxyType proxy;
156             if (GetProxy(NET_IPV4, proxy))
157                 return QVariant(proxy.first.GetPort());
158             else
159                 return QVariant(9050);
160         }
161         case ProxySocksVersion:
162             return settings.value("nSocksVersion", 5);
163         case Fee:
164             return QVariant(nTransactionFee);
165         case DisplayUnit:
166             return QVariant(nDisplayUnit);
167         case DisplayAddresses:
168             return QVariant(bDisplayAddresses);
169         case DetachDatabases:
170             return QVariant(bitdb.GetDetach());
171         case Language:
172             return settings.value("language", "");
173         default:
174             return QVariant();
175         }
176     }
177     return QVariant();
178 }
179
180 bool OptionsModel::setData(const QModelIndex & index, const QVariant & value, int role)
181 {
182     bool successful = true; /* set to false on parse error */
183     if(role == Qt::EditRole)
184     {
185         QSettings settings;
186         switch(index.row())
187         {
188         case StartAtStartup:
189             successful = GUIUtil::SetStartOnSystemStartup(value.toBool());
190             break;
191         case MinimizeToTray:
192             fMinimizeToTray = value.toBool();
193             settings.setValue("fMinimizeToTray", fMinimizeToTray);
194             break;
195         case MapPortUPnP:
196             fUseUPnP = value.toBool();
197             settings.setValue("fUseUPnP", fUseUPnP);
198             MapPort();
199             break;
200         case MinimizeOnClose:
201             fMinimizeOnClose = value.toBool();
202             settings.setValue("fMinimizeOnClose", fMinimizeOnClose);
203             break;
204         case ProxyUse:
205             settings.setValue("fUseProxy", value.toBool());
206             ApplyProxySettings();
207             break;
208         case ProxyIP: {
209             proxyType proxy;
210             proxy.first = CService("127.0.0.1", 9050);
211             GetProxy(NET_IPV4, proxy);
212
213             CNetAddr addr(value.toString().toStdString());
214             proxy.first.SetIP(addr);
215             settings.setValue("addrProxy", proxy.first.ToStringIPPort().c_str());
216             successful = ApplyProxySettings();
217         }
218         break;
219         case ProxyPort: {
220             proxyType proxy;
221             proxy.first = CService("127.0.0.1", 9050);
222             GetProxy(NET_IPV4, proxy);
223
224             proxy.first.SetPort(value.toInt());
225             settings.setValue("addrProxy", proxy.first.ToStringIPPort().c_str());
226             successful = ApplyProxySettings();
227         }
228         break;
229         case ProxySocksVersion: {
230             proxyType proxy;
231             proxy.second = 5;
232             GetProxy(NET_IPV4, proxy);
233
234             proxy.second = value.toInt();
235             settings.setValue("nSocksVersion", proxy.second);
236             successful = ApplyProxySettings();
237         }
238         break;
239         case Fee:
240             nTransactionFee = value.toLongLong();
241             settings.setValue("nTransactionFee", nTransactionFee);
242             break;
243         case DisplayUnit:
244             nDisplayUnit = value.toInt();
245             settings.setValue("nDisplayUnit", nDisplayUnit);
246             emit displayUnitChanged(nDisplayUnit);
247             break;
248         case DisplayAddresses:
249             bDisplayAddresses = value.toBool();
250             settings.setValue("bDisplayAddresses", bDisplayAddresses);
251             break;
252         case DetachDatabases: {
253             bool fDetachDB = value.toBool();
254             bitdb.SetDetach(fDetachDB);
255             settings.setValue("detachDB", fDetachDB);
256             }
257             break;
258         case Language:
259             settings.setValue("language", value);
260             break;
261         default:
262             break;
263         }
264     }
265     emit dataChanged(index, index);
266
267     return successful;
268 }
269
270 qint64 OptionsModel::getTransactionFee()
271 {
272     return nTransactionFee;
273 }
274
275 bool OptionsModel::getMinimizeToTray()
276 {
277     return fMinimizeToTray;
278 }
279
280 bool OptionsModel::getMinimizeOnClose()
281 {
282     return fMinimizeOnClose;
283 }
284
285 int OptionsModel::getDisplayUnit()
286 {
287     return nDisplayUnit;
288 }
289
290 bool OptionsModel::getDisplayAddresses()
291 {
292     return bDisplayAddresses;
293 }