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 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() const
56 int ClientModel::getNumBlocks() const
61 int ClientModel::getNumBlocksAtStartup()
63 if (numBlocksAtStartup == -1) numBlocksAtStartup = getNumBlocks();
64 return numBlocksAtStartup;
67 quint64 ClientModel::getTotalBytesRecv() const
69 return CNode::GetTotalBytesRecv();
72 quint64 ClientModel::getTotalBytesSent() const
74 return CNode::GetTotalBytesSent();
77 QDateTime ClientModel::getLastBlockDate() const
80 return QDateTime::fromTime_t(pindexBest->GetBlockTime());
82 return QDateTime::fromTime_t(1360105017); // Genesis block's time
85 void ClientModel::updateTimer()
87 // Some quantities (such as number of blocks) change so fast that we don't want to be notified for each change.
88 // Periodically check and update with a timer.
89 int newNumBlocks = getNumBlocks();
90 int newNumBlocksOfPeers = getNumBlocksOfPeers();
92 if(cachedNumBlocks != newNumBlocks || cachedNumBlocksOfPeers != newNumBlocksOfPeers)
94 cachedNumBlocks = newNumBlocks;
95 cachedNumBlocksOfPeers = newNumBlocksOfPeers;
97 emit numBlocksChanged(newNumBlocks, newNumBlocksOfPeers);
100 emit bytesChanged(getTotalBytesRecv(), getTotalBytesSent());
103 void ClientModel::updateNumConnections(int numConnections)
105 emit numConnectionsChanged(numConnections);
108 void ClientModel::updateAlert(const QString &hash, int status)
110 // Show error message notification for new alert
114 hash_256.SetHex(hash.toStdString());
115 CAlert alert = CAlert::getAlertByHash(hash_256);
118 emit error(tr("Network Alert"), QString::fromStdString(alert.strStatusBar), false);
122 // Emit a numBlocksChanged when the status message changes,
123 // so that the view recomputes and updates the status bar.
124 emit numBlocksChanged(getNumBlocks(), getNumBlocksOfPeers());
127 bool ClientModel::isTestNet() const
132 bool ClientModel::inInitialBlockDownload() const
134 return IsInitialBlockDownload();
137 int ClientModel::getNumBlocksOfPeers() const
139 return GetNumBlocksOfPeers();
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));