]> Git Repo - VerusCoin.git/blob - src/qt/clientmodel.cpp
[Qt] remove unused getNumBlocksAtStartup() from ClientModel
[VerusCoin.git] / src / qt / clientmodel.cpp
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.
4
5 #include "clientmodel.h"
6
7 #include "guiconstants.h"
8 #include "peertablemodel.h"
9
10 #include "alert.h"
11 #include "chainparams.h"
12 #include "checkpoints.h"
13 #include "clientversion.h"
14 #include "main.h"
15 #include "net.h"
16 #include "ui_interface.h"
17 #include "util.h"
18
19 #include <stdint.h>
20
21 #include <QDateTime>
22 #include <QDebug>
23 #include <QTimer>
24
25 static const int64_t nClientStartupTime = GetTime();
26
27 ClientModel::ClientModel(OptionsModel *optionsModel, QObject *parent) :
28     QObject(parent),
29     optionsModel(optionsModel),
30     peerTableModel(0),
31     cachedNumBlocks(0),
32     cachedReindexing(0),
33     cachedImporting(0),
34     pollTimer(0)
35 {
36     peerTableModel = new PeerTableModel(this);
37     pollTimer = new QTimer(this);
38     connect(pollTimer, SIGNAL(timeout()), this, SLOT(updateTimer()));
39     pollTimer->start(MODEL_UPDATE_DELAY);
40
41     subscribeToCoreSignals();
42 }
43
44 ClientModel::~ClientModel()
45 {
46     unsubscribeFromCoreSignals();
47 }
48
49 int ClientModel::getNumConnections(unsigned int flags) const
50 {
51     LOCK(cs_vNodes);
52     if (flags == CONNECTIONS_ALL) // Shortcut if we want total
53         return vNodes.size();
54
55     int nNum = 0;
56     BOOST_FOREACH(CNode* pnode, vNodes)
57     if (flags & (pnode->fInbound ? CONNECTIONS_IN : CONNECTIONS_OUT))
58         nNum++;
59
60     return nNum;
61 }
62
63 int ClientModel::getNumBlocks() const
64 {
65     LOCK(cs_main);
66     return chainActive.Height();
67 }
68
69 quint64 ClientModel::getTotalBytesRecv() const
70 {
71     return CNode::GetTotalBytesRecv();
72 }
73
74 quint64 ClientModel::getTotalBytesSent() const
75 {
76     return CNode::GetTotalBytesSent();
77 }
78
79 QDateTime ClientModel::getLastBlockDate() const
80 {
81     LOCK(cs_main);
82     if (chainActive.Tip())
83         return QDateTime::fromTime_t(chainActive.Tip()->GetBlockTime());
84     else
85         return QDateTime::fromTime_t(Params().GenesisBlock().GetBlockTime()); // Genesis block's time of current network
86 }
87
88 double ClientModel::getVerificationProgress() const
89 {
90     LOCK(cs_main);
91     return Checkpoints::GuessVerificationProgress(chainActive.Tip());
92 }
93
94 void ClientModel::updateTimer()
95 {
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);
100     if(!lockMain)
101         return;
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();
105
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)
109     {
110         cachedNumBlocks = newNumBlocks;
111         cachedReindexing = fReindex;
112         cachedImporting = fImporting;
113
114         emit numBlocksChanged(newNumBlocks);
115     }
116
117     emit bytesChanged(getTotalBytesRecv(), getTotalBytesSent());
118 }
119
120 void ClientModel::updateNumConnections(int numConnections)
121 {
122     emit numConnectionsChanged(numConnections);
123 }
124
125 void ClientModel::updateAlert(const QString &hash, int status)
126 {
127     // Show error message notification for new alert
128     if(status == CT_NEW)
129     {
130         uint256 hash_256;
131         hash_256.SetHex(hash.toStdString());
132         CAlert alert = CAlert::getAlertByHash(hash_256);
133         if(!alert.IsNull())
134         {
135             emit message(tr("Network Alert"), QString::fromStdString(alert.strStatusBar), CClientUIInterface::ICON_ERROR);
136         }
137     }
138
139     emit alertsChanged(getStatusBarWarnings());
140 }
141
142 bool ClientModel::inInitialBlockDownload() const
143 {
144     return IsInitialBlockDownload();
145 }
146
147 enum BlockSource ClientModel::getBlockSource() const
148 {
149     if (fReindex)
150         return BLOCK_SOURCE_REINDEX;
151     else if (fImporting)
152         return BLOCK_SOURCE_DISK;
153     else if (getNumConnections() > 0)
154         return BLOCK_SOURCE_NETWORK;
155
156     return BLOCK_SOURCE_NONE;
157 }
158
159 QString ClientModel::getStatusBarWarnings() const
160 {
161     return QString::fromStdString(GetWarnings("statusbar"));
162 }
163
164 OptionsModel *ClientModel::getOptionsModel()
165 {
166     return optionsModel;
167 }
168
169 PeerTableModel *ClientModel::getPeerTableModel()
170 {
171     return peerTableModel;
172 }
173
174 QString ClientModel::formatFullVersion() const
175 {
176     return QString::fromStdString(FormatFullVersion());
177 }
178
179 QString ClientModel::formatBuildDate() const
180 {
181     return QString::fromStdString(CLIENT_DATE);
182 }
183
184 bool ClientModel::isReleaseVersion() const
185 {
186     return CLIENT_VERSION_IS_RELEASE;
187 }
188
189 QString ClientModel::clientName() const
190 {
191     return QString::fromStdString(CLIENT_NAME);
192 }
193
194 QString ClientModel::formatClientStartupTime() const
195 {
196     return QDateTime::fromTime_t(nClientStartupTime).toString();
197 }
198
199 // Handlers for core signals
200 static void ShowProgress(ClientModel *clientmodel, const std::string &title, int nProgress)
201 {
202     // emits signal "showProgress"
203     QMetaObject::invokeMethod(clientmodel, "showProgress", Qt::QueuedConnection,
204                               Q_ARG(QString, QString::fromStdString(title)),
205                               Q_ARG(int, nProgress));
206 }
207
208 static void NotifyNumConnectionsChanged(ClientModel *clientmodel, int newNumConnections)
209 {
210     // Too noisy: qDebug() << "NotifyNumConnectionsChanged: " + QString::number(newNumConnections);
211     QMetaObject::invokeMethod(clientmodel, "updateNumConnections", Qt::QueuedConnection,
212                               Q_ARG(int, newNumConnections));
213 }
214
215 static void NotifyAlertChanged(ClientModel *clientmodel, const uint256 &hash, ChangeType status)
216 {
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())),
220                               Q_ARG(int, status));
221 }
222
223 void ClientModel::subscribeToCoreSignals()
224 {
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));
229 }
230
231 void ClientModel::unsubscribeFromCoreSignals()
232 {
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));
237 }
This page took 0.037869 seconds and 4 git commands to generate.