1 // Copyright (c) 2011-2013 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #include "clientmodel.h"
7 #include "guiconstants.h"
8 #include "peertablemodel.h"
11 #include "chainparams.h"
12 #include "checkpoints.h"
13 #include "clientversion.h"
16 #include "ui_interface.h"
24 static const int64_t nClientStartupTime = GetTime();
26 ClientModel::ClientModel(OptionsModel *optionsModel, QObject *parent) :
28 optionsModel(optionsModel),
31 cachedBlockDate(QDateTime()),
36 peerTableModel = new PeerTableModel(this);
37 pollTimer = new QTimer(this);
38 connect(pollTimer, SIGNAL(timeout()), this, SLOT(updateTimer()));
39 pollTimer->start(MODEL_UPDATE_DELAY);
41 subscribeToCoreSignals();
44 ClientModel::~ClientModel()
46 unsubscribeFromCoreSignals();
49 int ClientModel::getNumConnections(unsigned int flags) const
52 if (flags == CONNECTIONS_ALL) // Shortcut if we want total
56 BOOST_FOREACH(CNode* pnode, vNodes)
57 if (flags & (pnode->fInbound ? CONNECTIONS_IN : CONNECTIONS_OUT))
63 int ClientModel::getNumBlocks() const
66 return chainActive.Height();
69 quint64 ClientModel::getTotalBytesRecv() const
71 return CNode::GetTotalBytesRecv();
74 quint64 ClientModel::getTotalBytesSent() const
76 return CNode::GetTotalBytesSent();
79 QDateTime ClientModel::getLastBlockDate() const
83 if (chainActive.Tip())
84 return QDateTime::fromTime_t(chainActive.Tip()->GetBlockTime());
86 return QDateTime::fromTime_t(Params().GenesisBlock().GetBlockTime()); // Genesis block's time of current network
89 double ClientModel::getVerificationProgress() const
92 return Checkpoints::GuessVerificationProgress(Params().Checkpoints(), chainActive.Tip());
95 void ClientModel::updateTimer()
97 // Get required lock upfront. This avoids the GUI from getting stuck on
98 // periodical polls if the core is holding the locks for a longer time -
99 // for example, during a wallet rescan.
100 TRY_LOCK(cs_main, lockMain);
104 // Some quantities (such as number of blocks) change so fast that we don't want to be notified for each change.
105 // Periodically check and update with a timer.
106 int newNumBlocks = getNumBlocks();
107 QDateTime newBlockDate = getLastBlockDate();
109 // check for changed number of blocks we have, number of blocks peers claim to have, reindexing state and importing state
110 if (cachedNumBlocks != newNumBlocks ||
111 cachedBlockDate != newBlockDate ||
112 cachedReindexing != fReindex ||
113 cachedImporting != fImporting)
115 cachedNumBlocks = newNumBlocks;
116 cachedBlockDate = newBlockDate;
117 cachedReindexing = fReindex;
118 cachedImporting = fImporting;
120 Q_EMIT numBlocksChanged(newNumBlocks, newBlockDate);
123 Q_EMIT bytesChanged(getTotalBytesRecv(), getTotalBytesSent());
126 void ClientModel::updateNumConnections(int numConnections)
128 Q_EMIT numConnectionsChanged(numConnections);
131 void ClientModel::updateAlert(const QString &hash, int status)
133 // Show error message notification for new alert
137 hash_256.SetHex(hash.toStdString());
138 CAlert alert = CAlert::getAlertByHash(hash_256);
141 Q_EMIT message(tr("Network Alert"), QString::fromStdString(alert.strStatusBar), CClientUIInterface::ICON_ERROR);
145 Q_EMIT alertsChanged(getStatusBarWarnings());
148 bool ClientModel::inInitialBlockDownload() const
150 return IsInitialBlockDownload();
153 enum BlockSource ClientModel::getBlockSource() const
156 return BLOCK_SOURCE_REINDEX;
158 return BLOCK_SOURCE_DISK;
159 else if (getNumConnections() > 0)
160 return BLOCK_SOURCE_NETWORK;
162 return BLOCK_SOURCE_NONE;
165 QString ClientModel::getStatusBarWarnings() const
167 return QString::fromStdString(GetWarnings("statusbar"));
170 OptionsModel *ClientModel::getOptionsModel()
175 PeerTableModel *ClientModel::getPeerTableModel()
177 return peerTableModel;
180 QString ClientModel::formatFullVersion() const
182 return QString::fromStdString(FormatFullVersion());
185 QString ClientModel::formatBuildDate() const
187 return QString::fromStdString(CLIENT_DATE);
190 bool ClientModel::isReleaseVersion() const
192 return CLIENT_VERSION_IS_RELEASE;
195 QString ClientModel::clientName() const
197 return QString::fromStdString(CLIENT_NAME);
200 QString ClientModel::formatClientStartupTime() const
202 return QDateTime::fromTime_t(nClientStartupTime).toString();
205 // Handlers for core signals
206 static void ShowProgress(ClientModel *clientmodel, const std::string &title, int nProgress)
208 // emits signal "showProgress"
209 QMetaObject::invokeMethod(clientmodel, "showProgress", Qt::QueuedConnection,
210 Q_ARG(QString, QString::fromStdString(title)),
211 Q_ARG(int, nProgress));
214 static void NotifyNumConnectionsChanged(ClientModel *clientmodel, int newNumConnections)
216 // Too noisy: qDebug() << "NotifyNumConnectionsChanged: " + QString::number(newNumConnections);
217 QMetaObject::invokeMethod(clientmodel, "updateNumConnections", Qt::QueuedConnection,
218 Q_ARG(int, newNumConnections));
221 static void NotifyAlertChanged(ClientModel *clientmodel, const uint256 &hash, ChangeType status)
223 qDebug() << "NotifyAlertChanged: " + QString::fromStdString(hash.GetHex()) + " status=" + QString::number(status);
224 QMetaObject::invokeMethod(clientmodel, "updateAlert", Qt::QueuedConnection,
225 Q_ARG(QString, QString::fromStdString(hash.GetHex())),
229 void ClientModel::subscribeToCoreSignals()
231 // Connect signals to client
232 uiInterface.ShowProgress.connect(boost::bind(ShowProgress, this, _1, _2));
233 uiInterface.NotifyNumConnectionsChanged.connect(boost::bind(NotifyNumConnectionsChanged, this, _1));
234 uiInterface.NotifyAlertChanged.connect(boost::bind(NotifyAlertChanged, this, _1, _2));
237 void ClientModel::unsubscribeFromCoreSignals()
239 // Disconnect signals from client
240 uiInterface.ShowProgress.disconnect(boost::bind(ShowProgress, this, _1, _2));
241 uiInterface.NotifyNumConnectionsChanged.disconnect(boost::bind(NotifyNumConnectionsChanged, this, _1));
242 uiInterface.NotifyAlertChanged.disconnect(boost::bind(NotifyAlertChanged, this, _1, _2));