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"
25 static const int64_t nClientStartupTime = GetTime();
27 ClientModel::ClientModel(OptionsModel *optionsModel, QObject *parent) :
29 optionsModel(optionsModel),
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
82 if (chainActive.Tip())
83 return QDateTime::fromTime_t(chainActive.Tip()->GetBlockTime());
85 return QDateTime::fromTime_t(Params().GenesisBlock().GetBlockTime()); // Genesis block's time of current network
88 double ClientModel::getVerificationProgress() const
91 return Checkpoints::GuessVerificationProgress(chainActive.Tip());
94 void ClientModel::updateTimer()
96 // Get required lock upfront. This avoids the GUI from getting stuck on
97 // periodical polls if the core is holding the locks for a longer time -
98 // for example, during a wallet rescan.
99 TRY_LOCK(cs_main, lockMain);
102 // Some quantities (such as number of blocks) change so fast that we don't want to be notified for each change.
103 // Periodically check and update with a timer.
104 int newNumBlocks = getNumBlocks();
106 // check for changed number of blocks we have, number of blocks peers claim to have, reindexing state and importing state
107 if (cachedNumBlocks != newNumBlocks ||
108 cachedReindexing != fReindex || cachedImporting != fImporting)
110 cachedNumBlocks = newNumBlocks;
111 cachedReindexing = fReindex;
112 cachedImporting = fImporting;
114 emit numBlocksChanged(newNumBlocks);
117 emit bytesChanged(getTotalBytesRecv(), getTotalBytesSent());
120 void ClientModel::updateNumConnections(int numConnections)
122 emit numConnectionsChanged(numConnections);
125 void ClientModel::updateAlert(const QString &hash, int status)
127 // Show error message notification for new alert
131 hash_256.SetHex(hash.toStdString());
132 CAlert alert = CAlert::getAlertByHash(hash_256);
135 emit message(tr("Network Alert"), QString::fromStdString(alert.strStatusBar), CClientUIInterface::ICON_ERROR);
139 emit alertsChanged(getStatusBarWarnings());
142 bool ClientModel::inInitialBlockDownload() const
144 return IsInitialBlockDownload();
147 enum BlockSource ClientModel::getBlockSource() const
150 return BLOCK_SOURCE_REINDEX;
152 return BLOCK_SOURCE_DISK;
153 else if (getNumConnections() > 0)
154 return BLOCK_SOURCE_NETWORK;
156 return BLOCK_SOURCE_NONE;
159 QString ClientModel::getStatusBarWarnings() const
161 return QString::fromStdString(GetWarnings("statusbar"));
164 OptionsModel *ClientModel::getOptionsModel()
169 PeerTableModel *ClientModel::getPeerTableModel()
171 return peerTableModel;
174 QString ClientModel::formatFullVersion() const
176 return QString::fromStdString(FormatFullVersion());
179 QString ClientModel::formatBuildDate() const
181 return QString::fromStdString(CLIENT_DATE);
184 bool ClientModel::isReleaseVersion() const
186 return CLIENT_VERSION_IS_RELEASE;
189 QString ClientModel::clientName() const
191 return QString::fromStdString(CLIENT_NAME);
194 QString ClientModel::formatClientStartupTime() const
196 return QDateTime::fromTime_t(nClientStartupTime).toString();
199 // Handlers for core signals
200 static void ShowProgress(ClientModel *clientmodel, const std::string &title, int nProgress)
202 // emits signal "showProgress"
203 QMetaObject::invokeMethod(clientmodel, "showProgress", Qt::QueuedConnection,
204 Q_ARG(QString, QString::fromStdString(title)),
205 Q_ARG(int, nProgress));
208 static void NotifyNumConnectionsChanged(ClientModel *clientmodel, int newNumConnections)
210 // Too noisy: qDebug() << "NotifyNumConnectionsChanged: " + QString::number(newNumConnections);
211 QMetaObject::invokeMethod(clientmodel, "updateNumConnections", Qt::QueuedConnection,
212 Q_ARG(int, newNumConnections));
215 static void NotifyAlertChanged(ClientModel *clientmodel, const uint256 &hash, ChangeType status)
217 qDebug() << "NotifyAlertChanged: " + QString::fromStdString(hash.GetHex()) + " status=" + QString::number(status);
218 QMetaObject::invokeMethod(clientmodel, "updateAlert", Qt::QueuedConnection,
219 Q_ARG(QString, QString::fromStdString(hash.GetHex())),
223 void ClientModel::subscribeToCoreSignals()
225 // Connect signals to client
226 uiInterface.ShowProgress.connect(boost::bind(ShowProgress, this, _1, _2));
227 uiInterface.NotifyNumConnectionsChanged.connect(boost::bind(NotifyNumConnectionsChanged, this, _1));
228 uiInterface.NotifyAlertChanged.connect(boost::bind(NotifyAlertChanged, this, _1, _2));
231 void ClientModel::unsubscribeFromCoreSignals()
233 // Disconnect signals from client
234 uiInterface.ShowProgress.disconnect(boost::bind(ShowProgress, this, _1, _2));
235 uiInterface.NotifyNumConnectionsChanged.disconnect(boost::bind(NotifyNumConnectionsChanged, this, _1));
236 uiInterface.NotifyAlertChanged.disconnect(boost::bind(NotifyAlertChanged, this, _1, _2));