clientmodel.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // ECOin - Copyright (c) - 2014/2024 - GPLv3 - epsylon@riseup.net (https://03c8.net)
  2. #include "clientmodel.h"
  3. #include "guiconstants.h"
  4. #include "optionsmodel.h"
  5. #include "addresstablemodel.h"
  6. #include "transactiontablemodel.h"
  7. #include "alert.h"
  8. #include "main.h"
  9. #include "ui_interface.h"
  10. #include <QDateTime>
  11. #include <QTimer>
  12. static const int64 nClientStartupTime = GetTime();
  13. ClientModel::ClientModel(OptionsModel *optionsModel, QObject *parent) :
  14. QObject(parent), optionsModel(optionsModel),
  15. cachedNumBlocks(0), cachedNumBlocksOfPeers(0), pollTimer(0)
  16. {
  17. numBlocksAtStartup = -1;
  18. pollTimer = new QTimer(this);
  19. pollTimer->setInterval(MODEL_UPDATE_DELAY);
  20. pollTimer->start();
  21. connect(pollTimer, SIGNAL(timeout()), this, SLOT(updateTimer()));
  22. subscribeToCoreSignals();
  23. }
  24. ClientModel::~ClientModel()
  25. {
  26. unsubscribeFromCoreSignals();
  27. }
  28. int ClientModel::getNumConnections() const
  29. {
  30. return vNodes.size();
  31. }
  32. int ClientModel::getNumBlocks() const
  33. {
  34. return nBestHeight;
  35. }
  36. int ClientModel::getNumBlocksAtStartup()
  37. {
  38. if (numBlocksAtStartup == -1) numBlocksAtStartup = getNumBlocks();
  39. return numBlocksAtStartup;
  40. }
  41. QDateTime ClientModel::getLastBlockDate() const
  42. {
  43. if (pindexBest)
  44. return QDateTime::fromTime_t(pindexBest->GetBlockTime());
  45. else
  46. return QDateTime::fromTime_t(1360105017); // Genesis block's time
  47. }
  48. void ClientModel::updateTimer()
  49. {
  50. int newNumBlocks = getNumBlocks();
  51. int newNumBlocksOfPeers = getNumBlocksOfPeers();
  52. if(cachedNumBlocks != newNumBlocks || cachedNumBlocksOfPeers != newNumBlocksOfPeers)
  53. {
  54. cachedNumBlocks = newNumBlocks;
  55. cachedNumBlocksOfPeers = newNumBlocksOfPeers;
  56. emit numBlocksChanged(newNumBlocks, newNumBlocksOfPeers);
  57. }
  58. }
  59. void ClientModel::updateNumConnections(int numConnections)
  60. {
  61. emit numConnectionsChanged(numConnections);
  62. }
  63. void ClientModel::updateAlert(const QString &hash, int status)
  64. {
  65. // Show error message notification for new alert
  66. if(status == CT_NEW)
  67. {
  68. uint256 hash_256;
  69. hash_256.SetHex(hash.toStdString());
  70. CAlert alert = CAlert::getAlertByHash(hash_256);
  71. if(!alert.IsNull())
  72. {
  73. emit error(tr("Network Alert"), QString::fromStdString(alert.strStatusBar), false);
  74. }
  75. }
  76. emit numBlocksChanged(getNumBlocks(), getNumBlocksOfPeers());
  77. }
  78. bool ClientModel::isTestNet() const
  79. {
  80. return fTestNet;
  81. }
  82. bool ClientModel::inInitialBlockDownload() const
  83. {
  84. return IsInitialBlockDownload();
  85. }
  86. int ClientModel::getNumBlocksOfPeers() const
  87. {
  88. return GetNumBlocksOfPeers();
  89. }
  90. QString ClientModel::getStatusBarWarnings() const
  91. {
  92. return QString::fromStdString(GetWarnings("statusbar"));
  93. }
  94. OptionsModel *ClientModel::getOptionsModel()
  95. {
  96. return optionsModel;
  97. }
  98. QString ClientModel::formatFullVersion() const
  99. {
  100. return QString::fromStdString(FormatFullVersion());
  101. }
  102. QString ClientModel::formatBuildDate() const
  103. {
  104. return QString::fromStdString(CLIENT_DATE);
  105. }
  106. QString ClientModel::clientName() const
  107. {
  108. return QString::fromStdString(CLIENT_NAME);
  109. }
  110. QString ClientModel::formatClientStartupTime() const
  111. {
  112. return QDateTime::fromTime_t(nClientStartupTime).toString();
  113. }
  114. // Handlers for core signals
  115. static void NotifyBlocksChanged(ClientModel *clientmodel)
  116. {
  117. // Don't remove it, though, as it might be useful later.
  118. }
  119. static void NotifyNumConnectionsChanged(ClientModel *clientmodel, int newNumConnections)
  120. {
  121. QMetaObject::invokeMethod(clientmodel, "updateNumConnections", Qt::QueuedConnection,
  122. Q_ARG(int, newNumConnections));
  123. }
  124. static void NotifyAlertChanged(ClientModel *clientmodel, const uint256 &hash, ChangeType status)
  125. {
  126. OutputDebugStringF("NotifyAlertChanged %s status=%i\n", hash.GetHex().c_str(), status);
  127. QMetaObject::invokeMethod(clientmodel, "updateAlert", Qt::QueuedConnection,
  128. Q_ARG(QString, QString::fromStdString(hash.GetHex())),
  129. Q_ARG(int, status));
  130. }
  131. void ClientModel::subscribeToCoreSignals()
  132. {
  133. // Connect signals to client
  134. uiInterface.NotifyBlocksChanged.connect(boost::bind(NotifyBlocksChanged, this));
  135. uiInterface.NotifyNumConnectionsChanged.connect(boost::bind(NotifyNumConnectionsChanged, this, boost::placeholders::_1));
  136. uiInterface.NotifyAlertChanged.connect(boost::bind(NotifyAlertChanged, this, boost::placeholders::_1, boost::placeholders::_2));
  137. }
  138. void ClientModel::unsubscribeFromCoreSignals()
  139. {
  140. // Disconnect signals from client
  141. uiInterface.NotifyBlocksChanged.disconnect(boost::bind(NotifyBlocksChanged, this));
  142. uiInterface.NotifyNumConnectionsChanged.disconnect(boost::bind(NotifyNumConnectionsChanged, this, boost::placeholders::_1));
  143. uiInterface.NotifyAlertChanged.disconnect(boost::bind(NotifyAlertChanged, this, boost::placeholders::_1, boost::placeholders::_2));
  144. }