Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

optionsmodel.cpp 9.3KB

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