1 #include "clientmodel.h"
2 #include "guiconstants.h"
3 #include "optionsmodel.h"
4 #include "addresstablemodel.h"
5 #include "transactiontablemodel.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),
21 cachedNumBlocks(0), cachedNumBlocksOfPeers(0), pollTimer(0)
23 numBlocksAtStartup = -1;
25 pollTimer = new QTimer(this);
26 pollTimer->setInterval(MODEL_UPDATE_DELAY);
28 connect(pollTimer, SIGNAL(timeout()), this, SLOT(updateTimer()));
30 subscribeToCoreSignals();
33 ClientModel::~ClientModel()
35 unsubscribeFromCoreSignals();
38 double ClientModel::getPoSKernelPS()
40 return GetPoSKernelPS();
43 double ClientModel::getDifficulty(bool fProofofStake)
46 return GetDifficulty(GetLastBlockIndex(pindexBest,true));
48 return GetDifficulty(GetLastBlockIndex(pindexBest,false));
51 int ClientModel::getNumConnections(uint8_t flags) const
54 if (flags == CONNECTIONS_ALL) // Shortcut if we want total
55 return (int)(vNodes.size());
58 BOOST_FOREACH(CNode* pnode, vNodes)
59 if (flags & (pnode->fInbound ? CONNECTIONS_IN : CONNECTIONS_OUT))
65 int ClientModel::getNumBlocks() const
70 int ClientModel::getNumBlocksAtStartup()
72 if (numBlocksAtStartup == -1) numBlocksAtStartup = getNumBlocks();
73 return numBlocksAtStartup;
76 quint64 ClientModel::getTotalBytesRecv() const
78 return CNode::GetTotalBytesRecv();
81 quint64 ClientModel::getTotalBytesSent() const
83 return CNode::GetTotalBytesSent();
86 QDateTime ClientModel::getLastBlockDate() const
89 return QDateTime::fromTime_t(pindexBest->GetBlockTime());
91 return QDateTime::fromTime_t(1360105017); // Genesis block's time
94 void ClientModel::updateTimer()
96 // Some quantities (such as number of blocks) change so fast that we don't want to be notified for each change.
97 // Periodically check and update with a timer.
98 int newNumBlocks = getNumBlocks();
99 int newNumBlocksOfPeers = getNumBlocksOfPeers();
101 if(cachedNumBlocks != newNumBlocks || cachedNumBlocksOfPeers != newNumBlocksOfPeers)
103 cachedNumBlocks = newNumBlocks;
104 cachedNumBlocksOfPeers = newNumBlocksOfPeers;
106 emit numBlocksChanged(newNumBlocks, newNumBlocksOfPeers);
109 emit bytesChanged(getTotalBytesRecv(), getTotalBytesSent());
112 void ClientModel::updateNumConnections(int numConnections)
114 emit numConnectionsChanged(numConnections);
117 void ClientModel::updateAlert(const QString &hash, int status)
119 // Show error message notification for new alert
123 hash_256.SetHex(hash.toStdString());
124 CAlert alert = CAlert::getAlertByHash(hash_256);
127 emit error(tr("Network Alert"), QString::fromStdString(alert.strStatusBar), false);
131 // Emit a numBlocksChanged when the status message changes,
132 // so that the view recomputes and updates the status bar.
133 emit numBlocksChanged(getNumBlocks(), getNumBlocksOfPeers());
136 bool ClientModel::isTestNet() const
141 bool ClientModel::inInitialBlockDownload() const
143 return IsInitialBlockDownload();
146 int ClientModel::getNumBlocksOfPeers() const
148 return GetNumBlocksOfPeers();
151 QString ClientModel::getStatusBarWarnings() const
153 return QString::fromStdString(GetWarnings("statusbar"));
156 OptionsModel *ClientModel::getOptionsModel()
161 QString ClientModel::formatFullVersion() const
163 return QString::fromStdString(FormatFullVersion());
166 QString ClientModel::formatBuildDate() const
168 return QString::fromStdString(CLIENT_DATE);
171 QString ClientModel::clientName() const
173 return QString::fromStdString(CLIENT_NAME);
176 QString ClientModel::formatClientStartupTime() const
178 return QDateTime::fromTime_t(nClientStartupTime).toString();
181 // Handlers for core signals
182 static void NotifyBlocksChanged(ClientModel *clientmodel)
184 // This notification is too frequent. Don't trigger a signal.
185 // Don't remove it, though, as it might be useful later.
188 static void NotifyNumConnectionsChanged(ClientModel *clientmodel, int newNumConnections)
190 // Too noisy: OutputDebugStringF("NotifyNumConnectionsChanged %i\n", newNumConnections);
191 QMetaObject::invokeMethod(clientmodel, "updateNumConnections", Qt::QueuedConnection,
192 Q_ARG(int, newNumConnections));
195 static void NotifyAlertChanged(ClientModel *clientmodel, const uint256 &hash, ChangeType status)
197 OutputDebugStringF("NotifyAlertChanged %s status=%i\n", hash.GetHex().c_str(), status);
198 QMetaObject::invokeMethod(clientmodel, "updateAlert", Qt::QueuedConnection,
199 Q_ARG(QString, QString::fromStdString(hash.GetHex())),
203 void ClientModel::subscribeToCoreSignals()
205 // Connect signals to client
206 uiInterface.NotifyBlocksChanged.connect(boost::bind(NotifyBlocksChanged, this));
207 uiInterface.NotifyNumConnectionsChanged.connect(boost::bind(NotifyNumConnectionsChanged, this, boost::placeholders::_1));
208 uiInterface.NotifyAlertChanged.connect(boost::bind(NotifyAlertChanged, this, boost::placeholders::_1, boost::placeholders::_2));
211 void ClientModel::unsubscribeFromCoreSignals()
213 // Disconnect signals from client
214 uiInterface.NotifyBlocksChanged.disconnect(boost::bind(NotifyBlocksChanged, this));
215 uiInterface.NotifyNumConnectionsChanged.disconnect(boost::bind(NotifyNumConnectionsChanged, this, boost::placeholders::_1));
216 uiInterface.NotifyAlertChanged.disconnect(boost::bind(NotifyAlertChanged, this, boost::placeholders::_1, boost::placeholders::_2));