1 #include "clientmodel.h"
2 #include "guiconstants.h"
3 #include "optionsmodel.h"
4 #include "addresstablemodel.h"
5 #include "transactiontablemodel.h"
8 #include "ui_interface.h"
13 static const int64 nClientStartupTime = GetTime();
15 ClientModel::ClientModel(OptionsModel *optionsModel, QObject *parent) :
16 QObject(parent), optionsModel(optionsModel),
17 cachedNumBlocks(0), cachedNumBlocksOfPeers(0), pollTimer(0)
19 numBlocksAtStartup = -1;
21 pollTimer = new QTimer();
22 pollTimer->setInterval(MODEL_UPDATE_DELAY);
24 connect(pollTimer, SIGNAL(timeout()), this, SLOT(updateTimer()));
26 subscribeToCoreSignals();
29 ClientModel::~ClientModel()
31 unsubscribeFromCoreSignals();
34 int ClientModel::getNumConnections() const
39 int ClientModel::getNumBlocks() const
44 int ClientModel::getNumBlocksAtStartup()
46 if (numBlocksAtStartup == -1) numBlocksAtStartup = getNumBlocks();
47 return numBlocksAtStartup;
50 QDateTime ClientModel::getLastBlockDate() const
52 return QDateTime::fromTime_t(pindexBest->GetBlockTime());
55 void ClientModel::updateTimer()
57 // Some quantities (such as number of blocks) change so fast that we don't want to be notified for each change.
58 // Periodically check and update with a timer.
59 int newNumBlocks = getNumBlocks();
60 int newNumBlocksOfPeers = getNumBlocksOfPeers();
62 if(cachedNumBlocks != newNumBlocks || cachedNumBlocksOfPeers != newNumBlocksOfPeers)
63 emit numBlocksChanged(newNumBlocks, newNumBlocksOfPeers);
65 cachedNumBlocks = newNumBlocks;
66 cachedNumBlocksOfPeers = newNumBlocksOfPeers;
69 void ClientModel::updateNumConnections(int numConnections)
71 emit numConnectionsChanged(numConnections);
74 void ClientModel::updateAlert(const QString &hash, int status)
76 // Show error message notification for new alert
80 hash_256.SetHex(hash.toStdString());
81 CAlert alert = CAlert::getAlertByHash(hash_256);
84 emit error(tr("Network Alert"), QString::fromStdString(alert.strStatusBar), false);
88 // Emit a numBlocksChanged when the status message changes,
89 // so that the view recomputes and updates the status bar.
90 emit numBlocksChanged(getNumBlocks(), getNumBlocksOfPeers());
93 bool ClientModel::isTestNet() const
98 bool ClientModel::inInitialBlockDownload() const
100 return IsInitialBlockDownload();
103 int ClientModel::getNumBlocksOfPeers() const
105 return GetNumBlocksOfPeers();
108 QString ClientModel::getStatusBarWarnings() const
110 return QString::fromStdString(GetWarnings("statusbar"));
113 OptionsModel *ClientModel::getOptionsModel()
118 QString ClientModel::formatFullVersion() const
120 return QString::fromStdString(FormatFullVersion());
123 QString ClientModel::formatBuildDate() const
125 return QString::fromStdString(CLIENT_DATE);
128 QString ClientModel::clientName() const
130 return QString::fromStdString(CLIENT_NAME);
133 QDateTime ClientModel::formatClientStartupTime() const
135 return QDateTime::fromTime_t(nClientStartupTime);
138 // Handlers for core signals
139 static void NotifyBlocksChanged(ClientModel *clientmodel)
141 // This notification is too frequent. Don't trigger a signal.
142 // Don't remove it, though, as it might be useful later.
145 static void NotifyNumConnectionsChanged(ClientModel *clientmodel, int newNumConnections)
147 // Too noisy: OutputDebugStringF("NotifyNumConnectionsChanged %i\n", newNumConnections);
148 QMetaObject::invokeMethod(clientmodel, "updateNumConnections", Qt::QueuedConnection,
149 Q_ARG(int, newNumConnections));
152 static void NotifyAlertChanged(ClientModel *clientmodel, const uint256 &hash, ChangeType status)
154 OutputDebugStringF("NotifyAlertChanged %s status=%i\n", hash.GetHex().c_str(), status);
155 QMetaObject::invokeMethod(clientmodel, "updateAlert", Qt::QueuedConnection,
156 Q_ARG(QString, QString::fromStdString(hash.GetHex())),
160 void ClientModel::subscribeToCoreSignals()
162 // Connect signals to client
163 uiInterface.NotifyBlocksChanged.connect(boost::bind(NotifyBlocksChanged, this));
164 uiInterface.NotifyNumConnectionsChanged.connect(boost::bind(NotifyNumConnectionsChanged, this, _1));
165 uiInterface.NotifyAlertChanged.connect(boost::bind(NotifyAlertChanged, this, _1, _2));
168 void ClientModel::unsubscribeFromCoreSignals()
170 // Disconnect signals from client
171 uiInterface.NotifyBlocksChanged.disconnect(boost::bind(NotifyBlocksChanged, this));
172 uiInterface.NotifyNumConnectionsChanged.disconnect(boost::bind(NotifyNumConnectionsChanged, this, _1));
173 uiInterface.NotifyAlertChanged.disconnect(boost::bind(NotifyAlertChanged, this, _1, _2));