You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

optionsmodel.cpp 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include "optionsmodel.h"
  2. #include "bitcoinunits.h"
  3. #include "headers.h"
  4. #include <QDebug>
  5. OptionsModel::OptionsModel(CWallet *wallet, QObject *parent) :
  6. QAbstractListModel(parent),
  7. wallet(wallet),
  8. nDisplayUnit(BitcoinUnits::BTC),
  9. bDisplayAddresses(false)
  10. {
  11. // Read our specific settings from the wallet db
  12. CWalletDB walletdb(wallet->strWalletFile);
  13. walletdb.ReadSetting("nDisplayUnit", nDisplayUnit);
  14. walletdb.ReadSetting("bDisplayAddresses", bDisplayAddresses);
  15. }
  16. int OptionsModel::rowCount(const QModelIndex & parent) const
  17. {
  18. return OptionIDRowCount;
  19. }
  20. QVariant OptionsModel::data(const QModelIndex & index, int role) const
  21. {
  22. if(role == Qt::EditRole)
  23. {
  24. switch(index.row())
  25. {
  26. case StartAtStartup:
  27. return QVariant();
  28. case MinimizeToTray:
  29. return QVariant(fMinimizeToTray);
  30. case MapPortUPnP:
  31. return QVariant(fUseUPnP);
  32. case MinimizeOnClose:
  33. return QVariant(fMinimizeOnClose);
  34. case ConnectSOCKS4:
  35. return QVariant(fUseProxy);
  36. case ProxyIP:
  37. return QVariant(QString::fromStdString(addrProxy.ToStringIP()));
  38. case ProxyPort:
  39. return QVariant(QString::fromStdString(addrProxy.ToStringPort()));
  40. case Fee:
  41. return QVariant(nTransactionFee);
  42. case DisplayUnit:
  43. return QVariant(nDisplayUnit);
  44. case DisplayAddresses:
  45. return QVariant(bDisplayAddresses);
  46. default:
  47. return QVariant();
  48. }
  49. }
  50. return QVariant();
  51. }
  52. bool OptionsModel::setData(const QModelIndex & index, const QVariant & value, int role)
  53. {
  54. bool successful = true; /* set to false on parse error */
  55. if(role == Qt::EditRole)
  56. {
  57. CWalletDB walletdb(wallet->strWalletFile);
  58. switch(index.row())
  59. {
  60. case StartAtStartup:
  61. successful = false; /*TODO*/
  62. break;
  63. case MinimizeToTray:
  64. fMinimizeToTray = value.toBool();
  65. walletdb.WriteSetting("fMinimizeToTray", fMinimizeToTray);
  66. break;
  67. case MapPortUPnP:
  68. fUseUPnP = value.toBool();
  69. walletdb.WriteSetting("fUseUPnP", fUseUPnP);
  70. #ifdef USE_UPNP
  71. MapPort(fUseUPnP);
  72. #endif
  73. break;
  74. case MinimizeOnClose:
  75. fMinimizeOnClose = value.toBool();
  76. walletdb.WriteSetting("fMinimizeOnClose", fMinimizeOnClose);
  77. break;
  78. case ConnectSOCKS4:
  79. fUseProxy = value.toBool();
  80. walletdb.WriteSetting("fUseProxy", fUseProxy);
  81. break;
  82. case ProxyIP:
  83. {
  84. // Use CAddress to parse and check IP
  85. CAddress addr(value.toString().toStdString() + ":1");
  86. if (addr.ip != INADDR_NONE)
  87. {
  88. addrProxy.ip = addr.ip;
  89. walletdb.WriteSetting("addrProxy", addrProxy);
  90. }
  91. else
  92. {
  93. successful = false;
  94. }
  95. }
  96. break;
  97. case ProxyPort:
  98. {
  99. int nPort = atoi(value.toString().toAscii().data());
  100. if (nPort > 0 && nPort < USHRT_MAX)
  101. {
  102. addrProxy.port = htons(nPort);
  103. walletdb.WriteSetting("addrProxy", addrProxy);
  104. }
  105. else
  106. {
  107. successful = false;
  108. }
  109. }
  110. break;
  111. case Fee: {
  112. nTransactionFee = value.toLongLong();
  113. walletdb.WriteSetting("nTransactionFee", nTransactionFee);
  114. }
  115. break;
  116. case DisplayUnit: {
  117. int unit = value.toInt();
  118. nDisplayUnit = unit;
  119. walletdb.WriteSetting("nDisplayUnit", nDisplayUnit);
  120. emit displayUnitChanged(unit);
  121. }
  122. case DisplayAddresses: {
  123. bDisplayAddresses = value.toBool();
  124. walletdb.WriteSetting("bDisplayAddresses", bDisplayAddresses);
  125. }
  126. default:
  127. break;
  128. }
  129. }
  130. emit dataChanged(index, index);
  131. return successful;
  132. }
  133. qint64 OptionsModel::getTransactionFee()
  134. {
  135. return nTransactionFee;
  136. }
  137. bool OptionsModel::getMinimizeToTray()
  138. {
  139. return fMinimizeToTray;
  140. }
  141. bool OptionsModel::getMinimizeOnClose()
  142. {
  143. return fMinimizeOnClose;
  144. }
  145. int OptionsModel::getDisplayUnit()
  146. {
  147. return nDisplayUnit;
  148. }
  149. bool OptionsModel::getDisplayAddresses()
  150. {
  151. return bDisplayAddresses;
  152. }