1 #include "clientmodel.h"
2 #include "guiconstants.h"
3 #include "optionsmodel.h"
4 #include "addresstablemodel.h"
5 #include "transactiontablemodel.h"
9 #include "ui_interface.h"
14 extern double GetPoSKernelPS();
15 extern double GetDifficulty(const CBlockIndex* blockindex);
17 static const int64_t nClientStartupTime = GetTime();
19 ClientModel::ClientModel(OptionsModel *optionsModel, QObject *parent) :
20 QObject(parent), optionsModel(optionsModel), cachedNumBlocks(0), pollTimer(0)
22 numBlocksAtStartup = -1;
24 pollTimer = new QTimer(this);
25 pollTimer->setInterval(MODEL_UPDATE_DELAY);
27 connect(pollTimer, SIGNAL(timeout()), this, SLOT(updateTimer()));
29 subscribeToCoreSignals();
32 ClientModel::~ClientModel()
34 unsubscribeFromCoreSignals();
37 double ClientModel::getPoSKernelPS()
39 return GetPoSKernelPS();
42 double ClientModel::getDifficulty(bool fProofofStake)
45 return GetDifficulty(GetLastBlockIndex(pindexBest,true));
47 return GetDifficulty(GetLastBlockIndex(pindexBest,false));
50 int ClientModel::getNumConnections(uint8_t flags) const
53 if (flags == CONNECTIONS_ALL) // Shortcut if we want total
54 return (int)(vNodes.size());
57 for(CNode* pnode : vNodes)
58 if (flags & (pnode->fInbound ? CONNECTIONS_IN : CONNECTIONS_OUT))
64 int ClientModel::getNumBlocks() const
69 int ClientModel::getNumBlocksAtStartup()
71 if (numBlocksAtStartup == -1) numBlocksAtStartup = getNumBlocks();
72 return numBlocksAtStartup;
75 quint64 ClientModel::getTotalBytesRecv() const
77 return CNode::GetTotalBytesRecv();
80 quint64 ClientModel::getTotalBytesSent() const
82 return CNode::GetTotalBytesSent();
85 QDateTime ClientModel::getLastBlockDate() const
88 return QDateTime::fromTime_t(pindexBest->GetBlockTime());
90 return QDateTime::fromTime_t(1360105017); // Genesis block's time
93 void ClientModel::updateTimer()
95 // Some quantities (such as number of blocks) change so fast that we don't want to be notified for each change.
96 // Periodically check and update with a timer.
97 int newNumBlocks = getNumBlocks();
99 if(cachedNumBlocks != newNumBlocks)
101 cachedNumBlocks = newNumBlocks;
102 emit numBlocksChanged(newNumBlocks);
105 emit bytesChanged(getTotalBytesRecv(), getTotalBytesSent());
108 void ClientModel::updateNumConnections(int numConnections)
110 emit numConnectionsChanged(numConnections);
113 void ClientModel::updateAlert(const QString &hash, int status)
115 // Show error message notification for new alert
119 hash_256.SetHex(hash.toStdString());
120 CAlert alert = CAlert::getAlertByHash(hash_256);
123 emit error(tr("Network Alert"), QString::fromStdString(alert.strStatusBar), false);
127 // Emit a numBlocksChanged when the status message changes,
128 // so that the view recomputes and updates the status bar.
129 emit numBlocksChanged(getNumBlocks());
132 bool ClientModel::isTestNet() const
137 bool ClientModel::inInitialBlockDownload() const
139 return IsInitialBlockDownload();
142 QString ClientModel::getStatusBarWarnings() const
144 return QString::fromStdString(GetWarnings("statusbar"));
147 OptionsModel *ClientModel::getOptionsModel()
152 QString ClientModel::formatFullVersion() const
154 return QString::fromStdString(FormatFullVersion());
157 QString ClientModel::formatBuildDate() const
159 return QString::fromStdString(CLIENT_DATE);
162 QString ClientModel::clientName() const
164 return QString::fromStdString(CLIENT_NAME);
167 QString ClientModel::formatClientStartupTime() const
169 return QDateTime::fromTime_t(nClientStartupTime).toString();
172 // Handlers for core signals
173 static void NotifyBlocksChanged(ClientModel *clientmodel)
175 // This notification is too frequent. Don't trigger a signal.
176 // Don't remove it, though, as it might be useful later.
179 static void NotifyNumConnectionsChanged(ClientModel *clientmodel, int newNumConnections)
181 // Too noisy: OutputDebugStringF("NotifyNumConnectionsChanged %i\n", newNumConnections);
182 QMetaObject::invokeMethod(clientmodel, "updateNumConnections", Qt::QueuedConnection,
183 Q_ARG(int, newNumConnections));
186 static void NotifyAlertChanged(ClientModel *clientmodel, const uint256 &hash, ChangeType status)
188 OutputDebugStringF("NotifyAlertChanged %s status=%i\n", hash.GetHex().c_str(), status);
189 QMetaObject::invokeMethod(clientmodel, "updateAlert", Qt::QueuedConnection,
190 Q_ARG(QString, QString::fromStdString(hash.GetHex())),
194 void ClientModel::subscribeToCoreSignals()
196 // Connect signals to client
197 uiInterface.NotifyBlocksChanged.connect(boost::bind(NotifyBlocksChanged, this));
198 uiInterface.NotifyNumConnectionsChanged.connect(boost::bind(NotifyNumConnectionsChanged, this, _1));
199 uiInterface.NotifyAlertChanged.connect(boost::bind(NotifyAlertChanged, this, _1, _2));
202 void ClientModel::unsubscribeFromCoreSignals()
204 // Disconnect signals from client
205 uiInterface.NotifyBlocksChanged.disconnect(boost::bind(NotifyBlocksChanged, this));
206 uiInterface.NotifyNumConnectionsChanged.disconnect(boost::bind(NotifyNumConnectionsChanged, this, _1));
207 uiInterface.NotifyAlertChanged.disconnect(boost::bind(NotifyAlertChanged, this, _1, _2));