Feathercoin  0.5.0
P2P Digital Currency
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
clientmodel.cpp
Go to the documentation of this file.
1 #include "clientmodel.h"
2 
3 #include "guiconstants.h"
4 #include "optionsmodel.h"
5 #include "addresstablemodel.h"
7 
8 #include "alert.h"
9 #include "main.h"
10 #include "checkpoints.h"
11 #include "ui_interface.h"
12 
13 #include <QDateTime>
14 #include <QTimer>
15 
16 static const int64 nClientStartupTime = GetTime();
17 
18 ClientModel::ClientModel(OptionsModel *optionsModel, QObject *parent) :
19  QObject(parent), optionsModel(optionsModel),
20  cachedNumBlocks(0), cachedNumBlocksOfPeers(0),
21  cachedReindexing(0), cachedImporting(0),
22  numBlocksAtStartup(-1), pollTimer(0)
23 {
24  pollTimer = new QTimer(this);
25  pollTimer->setInterval(MODEL_UPDATE_DELAY);
26  pollTimer->start();
27  connect(pollTimer, SIGNAL(timeout()), this, SLOT(updateTimer()));
28 
30 }
31 
33 {
35 }
36 
38 {
39  return vNodes.size();
40 }
41 
43 {
44  return nBestHeight;
45 }
46 
48 {
50  return numBlocksAtStartup;
51 }
52 
54 {
55  if (pindexBest)
56  return QDateTime::fromTime_t(pindexBest->GetBlockTime());
57  else if(!isTestNet())
58  return QDateTime::fromTime_t(1231006505); // Genesis block's time
59  else
60  return QDateTime::fromTime_t(1296688602); // Genesis block's time (testnet)
61 }
62 
64 {
66 }
67 
69 {
70  // Some quantities (such as number of blocks) change so fast that we don't want to be notified for each change.
71  // Periodically check and update with a timer.
72  int newNumBlocks = getNumBlocks();
73  int newNumBlocksOfPeers = getNumBlocksOfPeers();
74 
75  // check for changed number of blocks we have, number of blocks peers claim to have, reindexing state and importing state
76  if (cachedNumBlocks != newNumBlocks || cachedNumBlocksOfPeers != newNumBlocksOfPeers ||
78  {
79  cachedNumBlocks = newNumBlocks;
80  cachedNumBlocksOfPeers = newNumBlocksOfPeers;
83 
84  // ensure we return the maximum of newNumBlocksOfPeers and newNumBlocks to not create weird displays in the GUI
85  emit numBlocksChanged(newNumBlocks, std::max(newNumBlocksOfPeers, newNumBlocks));
86  }
87 }
88 
89 void ClientModel::updateNumConnections(int numConnections)
90 {
91  emit numConnectionsChanged(numConnections);
92 }
93 
94 void ClientModel::updateAlert(const QString &hash, int status)
95 {
96  // Show error message notification for new alert
97  if(status == CT_NEW)
98  {
99  uint256 hash_256;
100  hash_256.SetHex(hash.toStdString());
101  CAlert alert = CAlert::getAlertByHash(hash_256);
102  if(!alert.IsNull())
103  {
104  emit message(tr("Network Alert"), QString::fromStdString(alert.strStatusBar), CClientUIInterface::ICON_ERROR);
105  }
106  }
107 
109 }
110 
112 {
113  return fTestNet;
114 }
115 
117 {
118  return IsInitialBlockDownload();
119 }
120 
122 {
123  if (fReindex)
124  return BLOCK_SOURCE_REINDEX;
125  else if (fImporting)
126  return BLOCK_SOURCE_DISK;
127  else if (getNumConnections() > 0)
128  return BLOCK_SOURCE_NETWORK;
129 
130  return BLOCK_SOURCE_NONE;
131 }
132 
134 {
135  return GetNumBlocksOfPeers();
136 }
137 
139 {
140  return QString::fromStdString(GetWarnings("statusbar"));
141 }
142 
144 {
145  return optionsModel;
146 }
147 
149 {
150  return QString::fromStdString(FormatFullVersion());
151 }
152 
154 {
155  return QString::fromStdString(CLIENT_DATE);
156 }
157 
159 {
161 }
162 
163 QString ClientModel::clientName() const
164 {
165  return QString::fromStdString(CLIENT_NAME);
166 }
167 
169 {
170  return QDateTime::fromTime_t(nClientStartupTime).toString();
171 }
172 
173 // Handlers for core signals
174 static void NotifyBlocksChanged(ClientModel *clientmodel)
175 {
176  // This notification is too frequent. Don't trigger a signal.
177  // Don't remove it, though, as it might be useful later.
178 }
179 
180 static void NotifyNumConnectionsChanged(ClientModel *clientmodel, int newNumConnections)
181 {
182  // Too noisy: OutputDebugStringF("NotifyNumConnectionsChanged %i\n", newNumConnections);
183  QMetaObject::invokeMethod(clientmodel, "updateNumConnections", Qt::QueuedConnection,
184  Q_ARG(int, newNumConnections));
185 }
186 
187 static void NotifyAlertChanged(ClientModel *clientmodel, const uint256 &hash, ChangeType status)
188 {
189  OutputDebugStringF("NotifyAlertChanged %s status=%i\n", hash.GetHex().c_str(), status);
190  QMetaObject::invokeMethod(clientmodel, "updateAlert", Qt::QueuedConnection,
191  Q_ARG(QString, QString::fromStdString(hash.GetHex())),
192  Q_ARG(int, status));
193 }
194 
196 {
197  // Connect signals to client
198  uiInterface.NotifyBlocksChanged.connect(boost::bind(NotifyBlocksChanged, this));
199  uiInterface.NotifyNumConnectionsChanged.connect(boost::bind(NotifyNumConnectionsChanged, this, _1));
200  uiInterface.NotifyAlertChanged.connect(boost::bind(NotifyAlertChanged, this, _1, _2));
201 }
202 
204 {
205  // Disconnect signals from client
206  uiInterface.NotifyBlocksChanged.disconnect(boost::bind(NotifyBlocksChanged, this));
207  uiInterface.NotifyNumConnectionsChanged.disconnect(boost::bind(NotifyNumConnectionsChanged, this, _1));
208  uiInterface.NotifyAlertChanged.disconnect(boost::bind(NotifyAlertChanged, this, _1, _2));
209 }
void SetHex(const char *psz)
Definition: uint256.h:306
void message(const QString &title, const QString &message, unsigned int style)
Asynchronous message notification.
bool fImporting
Definition: main.cpp:49
int64 GetBlockTime() const
Definition: main.h:1749
const std::string CLIENT_NAME
string GetWarnings(string strFor)
Definition: main.cpp:3075
OptionsModel * getOptionsModel()
QString formatClientStartupTime() const
int getNumBlocksOfPeers() const
Return conservative estimate of total number of blocks, or 0 if unknown.
bool fReindex
Definition: main.cpp:50
QString getStatusBarWarnings() const
Return warnings to be displayed in status bar.
std::string strStatusBar
Definition: alert.h:39
void numConnectionsChanged(int count)
bool isReleaseVersion() const
void numBlocksChanged(int count, int countOfPeers)
void alertsChanged(const QString &warnings)
vector< CNode * > vNodes
Definition: net.cpp:56
int64 GetTime()
Definition: util.cpp:1298
bool IsNull() const
Definition: alert.cpp:92
#define CLIENT_VERSION_IS_RELEASE
Definition: clientversion.h:15
bool inInitialBlockDownload() const
Return true if core is doing initial block download.
int getNumConnections() const
Definition: clientmodel.cpp:37
bool cachedImporting
Definition: clientmodel.h:64
double getVerificationProgress() const
Definition: clientmodel.cpp:63
bool fTestNet
Definition: util.cpp:81
ChangeType
General change type (added, updated, removed).
Definition: ui_interface.h:18
bool IsInitialBlockDownload()
Check whether we are doing an initial block download (synchronizing from disk or network) ...
Definition: main.cpp:1276
An alert is a combination of a serialized CUnsignedAlert and a signature.
Definition: alert.h:68
QDateTime getLastBlockDate() const
Definition: clientmodel.cpp:53
BlockSource
Definition: clientmodel.h:16
int GetNumBlocksOfPeers()
Get the number of active peers.
Definition: main.cpp:1271
void updateAlert(const QString &hash, int status)
Definition: clientmodel.cpp:94
CClientUIInterface uiInterface
Definition: init.cpp:32
void unsubscribeFromCoreSignals()
QString clientName() const
void subscribeToCoreSignals()
QString formatBuildDate() const
std::string GetHex() const
Definition: uint256.h:298
Model for Bitcoin network client.
Definition: clientmodel.h:24
int cachedNumBlocks
Definition: clientmodel.h:61
int OutputDebugStringF(const char *pszFormat,...)
Definition: util.cpp:237
QTimer * pollTimer
Definition: clientmodel.h:68
int getNumBlocks() const
Definition: clientmodel.cpp:42
boost::signals2::signal< void(int newNumConnections)> NotifyNumConnectionsChanged
Number of network connections changed.
Definition: ui_interface.h:89
int cachedNumBlocksOfPeers
Definition: clientmodel.h:62
256-bit unsigned integer
Definition: uint256.h:537
int numBlocksAtStartup
Definition: clientmodel.h:66
bool cachedReindexing
Definition: clientmodel.h:63
ClientModel(OptionsModel *optionsModel, QObject *parent=0)
Definition: clientmodel.cpp:18
boost::signals2::signal< void(const uint256 &hash, ChangeType status)> NotifyAlertChanged
New, updated or cancelled alert.
Definition: ui_interface.h:95
Interface from Qt to configuration data structure for Bitcoin client.
Definition: optionsmodel.h:12
void updateTimer()
Definition: clientmodel.cpp:68
static CAlert getAlertByHash(const uint256 &hash)
Definition: alert.cpp:157
string FormatFullVersion()
Definition: util.cpp:1404
bool isTestNet() const
Return true if client connected to testnet.
boost::signals2::signal< void()> NotifyBlocksChanged
Block chain changed.
Definition: ui_interface.h:86
OptionsModel * optionsModel
Definition: clientmodel.h:59
CBlockIndex * pindexBest
Definition: main.cpp:45
int getNumBlocksAtStartup()
Definition: clientmodel.cpp:47
void updateNumConnections(int numConnections)
Definition: clientmodel.cpp:89
int nBestHeight
Definition: main.cpp:41
QString formatFullVersion() const
double GuessVerificationProgress(CBlockIndex *pindex)
Definition: checkpoints.cpp:75
uint32_t hash
Definition: cache.cc:34
enum BlockSource getBlockSource() const
Return true if core is importing blocks.
const std::string CLIENT_DATE
long long int64
Definition: serialize.h:25