]> Git Repo - VerusCoin.git/blob - src/qt/clientmodel.cpp
komodo strings
[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 <QDebug>
22 #include <QTimer>
23
24 static const int64_t nClientStartupTime = GetTime();
25
26 ClientModel::ClientModel(OptionsModel *optionsModel, QObject *parent) :
27     QObject(parent),
28     optionsModel(optionsModel),
29     peerTableModel(0),
30     cachedNumBlocks(0),
31     cachedBlockDate(QDateTime()),
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
83     if (chainActive.Tip())
84         return QDateTime::fromTime_t(chainActive.Tip()->GetBlockTime());
85
86     return QDateTime::fromTime_t(Params().GenesisBlock().GetBlockTime()); // Genesis block's time of current network
87 }
88
89 double ClientModel::getVerificationProgress() const
90 {
91     LOCK(cs_main);
92     return Checkpoints::GuessVerificationProgress(Params().Checkpoints(), chainActive.Tip());
93 }
94
95 void ClientModel::updateTimer()
96 {
97     // Get required lock upfront. This avoids the GUI from getting stuck on
98     // periodical polls if the core is holding the locks for a longer time -
99     // for example, during a wallet rescan.
100     TRY_LOCK(cs_main, lockMain);
101     if (!lockMain)
102         return;
103
104     // Some quantities (such as number of blocks) change so fast that we don't want to be notified for each change.
105     // Periodically check and update with a timer.
106     int newNumBlocks = getNumBlocks();
107     QDateTime newBlockDate = getLastBlockDate();
108
109     // check for changed number of blocks we have, number of blocks peers claim to have, reindexing state and importing state
110     if (cachedNumBlocks != newNumBlocks ||
111         cachedBlockDate != newBlockDate ||
112         cachedReindexing != fReindex ||
113         cachedImporting != fImporting)
114     {
115         cachedNumBlocks = newNumBlocks;
116         cachedBlockDate = newBlockDate;
117         cachedReindexing = fReindex;
118         cachedImporting = fImporting;
119
120         Q_EMIT numBlocksChanged(newNumBlocks, newBlockDate);
121     }
122
123     Q_EMIT bytesChanged(getTotalBytesRecv(), getTotalBytesSent());
124 }
125
126 void ClientModel::updateNumConnections(int numConnections)
127 {
128     Q_EMIT numConnectionsChanged(numConnections);
129 }
130
131 void ClientModel::updateAlert(const QString &hash, int status)
132 {
133     // Show error message notification for new alert
134     if(status == CT_NEW)
135     {
136         uint256 hash_256;
137         hash_256.SetHex(hash.toStdString());
138         CAlert alert = CAlert::getAlertByHash(hash_256);
139         if(!alert.IsNull())
140         {
141             Q_EMIT message(tr("Network Alert"), QString::fromStdString(alert.strStatusBar), CClientUIInterface::ICON_ERROR);
142         }
143     }
144
145     Q_EMIT alertsChanged(getStatusBarWarnings());
146 }
147
148 bool ClientModel::inInitialBlockDownload() const
149 {
150     return IsInitialBlockDownload();
151 }
152
153 enum BlockSource ClientModel::getBlockSource() const
154 {
155     if (fReindex)
156         return BLOCK_SOURCE_REINDEX;
157     else if (fImporting)
158         return BLOCK_SOURCE_DISK;
159     else if (getNumConnections() > 0)
160         return BLOCK_SOURCE_NETWORK;
161
162     return BLOCK_SOURCE_NONE;
163 }
164
165 QString ClientModel::getStatusBarWarnings() const
166 {
167     return QString::fromStdString(GetWarnings("statusbar"));
168 }
169
170 OptionsModel *ClientModel::getOptionsModel()
171 {
172     return optionsModel;
173 }
174
175 PeerTableModel *ClientModel::getPeerTableModel()
176 {
177     return peerTableModel;
178 }
179
180 QString ClientModel::formatFullVersion() const
181 {
182     return QString::fromStdString(FormatFullVersion());
183 }
184
185 QString ClientModel::formatBuildDate() const
186 {
187     return QString::fromStdString(CLIENT_DATE);
188 }
189
190 bool ClientModel::isReleaseVersion() const
191 {
192     return CLIENT_VERSION_IS_RELEASE;
193 }
194
195 QString ClientModel::clientName() const
196 {
197     return QString::fromStdString(CLIENT_NAME);
198 }
199
200 QString ClientModel::formatClientStartupTime() const
201 {
202     return QDateTime::fromTime_t(nClientStartupTime).toString();
203 }
204
205 // Handlers for core signals
206 static void ShowProgress(ClientModel *clientmodel, const std::string &title, int nProgress)
207 {
208     // emits signal "showProgress"
209     QMetaObject::invokeMethod(clientmodel, "showProgress", Qt::QueuedConnection,
210                               Q_ARG(QString, QString::fromStdString(title)),
211                               Q_ARG(int, nProgress));
212 }
213
214 static void NotifyNumConnectionsChanged(ClientModel *clientmodel, int newNumConnections)
215 {
216     // Too noisy: qDebug() << "NotifyNumConnectionsChanged: " + QString::number(newNumConnections);
217     QMetaObject::invokeMethod(clientmodel, "updateNumConnections", Qt::QueuedConnection,
218                               Q_ARG(int, newNumConnections));
219 }
220
221 static void NotifyAlertChanged(ClientModel *clientmodel, const uint256 &hash, ChangeType status)
222 {
223     qDebug() << "NotifyAlertChanged: " + QString::fromStdString(hash.GetHex()) + " status=" + QString::number(status);
224     QMetaObject::invokeMethod(clientmodel, "updateAlert", Qt::QueuedConnection,
225                               Q_ARG(QString, QString::fromStdString(hash.GetHex())),
226                               Q_ARG(int, status));
227 }
228
229 void ClientModel::subscribeToCoreSignals()
230 {
231     // Connect signals to client
232     uiInterface.ShowProgress.connect(boost::bind(ShowProgress, this, _1, _2));
233     uiInterface.NotifyNumConnectionsChanged.connect(boost::bind(NotifyNumConnectionsChanged, this, _1));
234     uiInterface.NotifyAlertChanged.connect(boost::bind(NotifyAlertChanged, this, _1, _2));
235 }
236
237 void ClientModel::unsubscribeFromCoreSignals()
238 {
239     // Disconnect signals from client
240     uiInterface.ShowProgress.disconnect(boost::bind(ShowProgress, this, _1, _2));
241     uiInterface.NotifyNumConnectionsChanged.disconnect(boost::bind(NotifyNumConnectionsChanged, this, _1));
242     uiInterface.NotifyAlertChanged.disconnect(boost::bind(NotifyAlertChanged, this, _1, _2));
243 }
This page took 0.038154 seconds and 4 git commands to generate.