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 static const int64 nClientStartupTime = GetTime();
16 ClientModel::ClientModel(OptionsModel *optionsModel, QObject *parent) :
17 QObject(parent), optionsModel(optionsModel),
18 cachedNumBlocks(0), cachedNumBlocksOfPeers(0), pollTimer(0)
20 numBlocksAtStartup = -1;
22 pollTimer = new QTimer(this);
23 pollTimer->setInterval(MODEL_UPDATE_DELAY);
25 connect(pollTimer, SIGNAL(timeout()), this, SLOT(updateTimer()));
27 subscribeToCoreSignals();
30 ClientModel::~ClientModel()
32 unsubscribeFromCoreSignals();
35 int ClientModel::getNumConnections() const
40 int ClientModel::getNumBlocks() const
45 int ClientModel::getNumBlocksAtStartup()
47 if (numBlocksAtStartup == -1) numBlocksAtStartup = getNumBlocks();
48 return numBlocksAtStartup;
51 QDateTime ClientModel::getLastBlockDate() const
54 return QDateTime::fromTime_t(pindexBest->GetBlockTime());
56 return QDateTime::fromTime_t(1360105017); // Genesis block's time
59 void ClientModel::updateTimer()
61 // Some quantities (such as number of blocks) change so fast that we don't want to be notified for each change.
62 // Periodically check and update with a timer.
63 int newNumBlocks = getNumBlocks();
64 int newNumBlocksOfPeers = getNumBlocksOfPeers();
66 if(cachedNumBlocks != newNumBlocks || cachedNumBlocksOfPeers != newNumBlocksOfPeers)
68 cachedNumBlocks = newNumBlocks;
69 cachedNumBlocksOfPeers = newNumBlocksOfPeers;
71 emit numBlocksChanged(newNumBlocks, newNumBlocksOfPeers);
75 void ClientModel::updateNumConnections(int numConnections)
77 emit numConnectionsChanged(numConnections);
80 void ClientModel::updateAlert(const QString &hash, int status)
82 // Show error message notification for new alert
86 hash_256.SetHex(hash.toStdString());
87 CAlert alert = CAlert::getAlertByHash(hash_256);
90 emit error(tr("Network Alert"), QString::fromStdString(alert.strStatusBar), false);
94 // Emit a numBlocksChanged when the status message changes,
95 // so that the view recomputes and updates the status bar.
96 emit numBlocksChanged(getNumBlocks(), getNumBlocksOfPeers());
99 bool ClientModel::isTestNet() const
104 bool ClientModel::inInitialBlockDownload() const
106 return IsInitialBlockDownload();
109 int ClientModel::getNumBlocksOfPeers() const
111 return GetNumBlocksOfPeers();
114 QString ClientModel::getStatusBarWarnings() const
116 return QString::fromStdString(GetWarnings("statusbar"));
119 OptionsModel *ClientModel::getOptionsModel()
124 QString ClientModel::formatFullVersion() const
126 return QString::fromStdString(FormatFullVersion());
129 QString ClientModel::formatBuildDate() const
131 return QString::fromStdString(CLIENT_DATE);
134 QString ClientModel::clientName() const
136 return QString::fromStdString(CLIENT_NAME);
139 QString ClientModel::formatClientStartupTime() const
141 return QDateTime::fromTime_t(nClientStartupTime).toString();
144 // Handlers for core signals
145 static void NotifyBlocksChanged(ClientModel *clientmodel)
147 // This notification is too frequent. Don't trigger a signal.
148 // Don't remove it, though, as it might be useful later.
151 static void NotifyNumConnectionsChanged(ClientModel *clientmodel, int newNumConnections)
153 // Too noisy: OutputDebugStringF("NotifyNumConnectionsChanged %i\n", newNumConnections);
154 QMetaObject::invokeMethod(clientmodel, "updateNumConnections", Qt::QueuedConnection,
155 Q_ARG(int, newNumConnections));
158 static void NotifyAlertChanged(ClientModel *clientmodel, const uint256 &hash, ChangeType status)
160 OutputDebugStringF("NotifyAlertChanged %s status=%i\n", hash.GetHex().c_str(), status);
161 QMetaObject::invokeMethod(clientmodel, "updateAlert", Qt::QueuedConnection,
162 Q_ARG(QString, QString::fromStdString(hash.GetHex())),
166 void ClientModel::subscribeToCoreSignals()
168 // Connect signals to client
169 uiInterface.NotifyBlocksChanged.connect(boost::bind(NotifyBlocksChanged, this));
170 uiInterface.NotifyNumConnectionsChanged.connect(boost::bind(NotifyNumConnectionsChanged, this, _1));
171 uiInterface.NotifyAlertChanged.connect(boost::bind(NotifyAlertChanged, this, _1, _2));
174 void ClientModel::unsubscribeFromCoreSignals()
176 // Disconnect signals from client
177 uiInterface.NotifyBlocksChanged.disconnect(boost::bind(NotifyBlocksChanged, this));
178 uiInterface.NotifyNumConnectionsChanged.disconnect(boost::bind(NotifyNumConnectionsChanged, this, _1));
179 uiInterface.NotifyAlertChanged.disconnect(boost::bind(NotifyAlertChanged, this, _1, _2));