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 QDateTime ClientModel::getLastBlockDate() const
70 return QDateTime::fromTime_t(pindexBest->GetBlockTime());
72 return QDateTime::fromTime_t(1360105017); // Genesis block's time
75 void ClientModel::updateTimer()
77 // Some quantities (such as number of blocks) change so fast that we don't want to be notified for each change.
78 // Periodically check and update with a timer.
79 int newNumBlocks = getNumBlocks();
80 int newNumBlocksOfPeers = getNumBlocksOfPeers();
82 if(cachedNumBlocks != newNumBlocks || cachedNumBlocksOfPeers != newNumBlocksOfPeers)
84 cachedNumBlocks = newNumBlocks;
85 cachedNumBlocksOfPeers = newNumBlocksOfPeers;
87 emit numBlocksChanged(newNumBlocks, newNumBlocksOfPeers);
91 void ClientModel::updateNumConnections(int numConnections)
93 emit numConnectionsChanged(numConnections);
96 void ClientModel::updateAlert(const QString &hash, int status)
98 // Show error message notification for new alert
102 hash_256.SetHex(hash.toStdString());
103 CAlert alert = CAlert::getAlertByHash(hash_256);
106 emit error(tr("Network Alert"), QString::fromStdString(alert.strStatusBar), false);
110 // Emit a numBlocksChanged when the status message changes,
111 // so that the view recomputes and updates the status bar.
112 emit numBlocksChanged(getNumBlocks(), getNumBlocksOfPeers());
115 bool ClientModel::isTestNet() const
120 bool ClientModel::inInitialBlockDownload() const
122 return IsInitialBlockDownload();
125 int ClientModel::getNumBlocksOfPeers() const
127 return GetNumBlocksOfPeers();
130 QString ClientModel::getStatusBarWarnings() const
132 return QString::fromStdString(GetWarnings("statusbar"));
135 OptionsModel *ClientModel::getOptionsModel()
140 QString ClientModel::formatFullVersion() const
142 return QString::fromStdString(FormatFullVersion());
145 QString ClientModel::formatBuildDate() const
147 return QString::fromStdString(CLIENT_DATE);
150 QString ClientModel::clientName() const
152 return QString::fromStdString(CLIENT_NAME);
155 QString ClientModel::formatClientStartupTime() const
157 return QDateTime::fromTime_t(nClientStartupTime).toString();
160 // Handlers for core signals
161 static void NotifyBlocksChanged(ClientModel *clientmodel)
163 // This notification is too frequent. Don't trigger a signal.
164 // Don't remove it, though, as it might be useful later.
167 static void NotifyNumConnectionsChanged(ClientModel *clientmodel, int newNumConnections)
169 // Too noisy: OutputDebugStringF("NotifyNumConnectionsChanged %i\n", newNumConnections);
170 QMetaObject::invokeMethod(clientmodel, "updateNumConnections", Qt::QueuedConnection,
171 Q_ARG(int, newNumConnections));
174 static void NotifyAlertChanged(ClientModel *clientmodel, const uint256 &hash, ChangeType status)
176 OutputDebugStringF("NotifyAlertChanged %s status=%i\n", hash.GetHex().c_str(), status);
177 QMetaObject::invokeMethod(clientmodel, "updateAlert", Qt::QueuedConnection,
178 Q_ARG(QString, QString::fromStdString(hash.GetHex())),
182 void ClientModel::subscribeToCoreSignals()
184 // Connect signals to client
185 uiInterface.NotifyBlocksChanged.connect(boost::bind(NotifyBlocksChanged, this));
186 uiInterface.NotifyNumConnectionsChanged.connect(boost::bind(NotifyNumConnectionsChanged, this, _1));
187 uiInterface.NotifyAlertChanged.connect(boost::bind(NotifyAlertChanged, this, _1, _2));
190 void ClientModel::unsubscribeFromCoreSignals()
192 // Disconnect signals from client
193 uiInterface.NotifyBlocksChanged.disconnect(boost::bind(NotifyBlocksChanged, this));
194 uiInterface.NotifyNumConnectionsChanged.disconnect(boost::bind(NotifyNumConnectionsChanged, this, _1));
195 uiInterface.NotifyAlertChanged.disconnect(boost::bind(NotifyAlertChanged, this, _1, _2));