064882be32535ab2a14d81c7fab82815101815ca
[novacoin.git] / src / qt / clientmodel.cpp
1 #include "clientmodel.h"
2 #include "guiconstants.h"
3 #include "optionsmodel.h"
4 #include "addresstablemodel.h"
5 #include "transactiontablemodel.h"
6
7 #include "alert.h"
8 #include "main.h"
9 #include "ui_interface.h"
10
11 #include <QDateTime>
12 #include <QTimer>
13
14 extern double GetPoSKernelPS();
15 extern double GetDifficulty(const CBlockIndex* blockindex);
16
17 static const int64 nClientStartupTime = GetTime();
18
19 ClientModel::ClientModel(OptionsModel *optionsModel, QObject *parent) :
20     QObject(parent), optionsModel(optionsModel),
21     cachedNumBlocks(0), cachedNumBlocksOfPeers(0), pollTimer(0)
22 {
23     numBlocksAtStartup = -1;
24
25     pollTimer = new QTimer(this);
26     pollTimer->setInterval(MODEL_UPDATE_DELAY);
27     pollTimer->start();
28     connect(pollTimer, SIGNAL(timeout()), this, SLOT(updateTimer()));
29
30     subscribeToCoreSignals();
31 }
32
33 ClientModel::~ClientModel()
34 {
35     unsubscribeFromCoreSignals();
36 }
37
38 double ClientModel::getPoSKernelPS()
39 {
40     return GetPoSKernelPS();
41 }
42
43 double ClientModel::getDifficulty(bool fProofofStake)
44 {
45     if (fProofofStake)
46        return GetDifficulty(GetLastBlockIndex(pindexBest,true));
47     else
48        return GetDifficulty(GetLastBlockIndex(pindexBest,false));
49 }
50
51 int ClientModel::getNumConnections() const
52 {
53     return vNodes.size();
54 }
55
56 int ClientModel::getNumBlocks() const
57 {
58     return nBestHeight;
59 }
60
61 int ClientModel::getNumBlocksAtStartup()
62 {
63     if (numBlocksAtStartup == -1) numBlocksAtStartup = getNumBlocks();
64     return numBlocksAtStartup;
65 }
66
67 QDateTime ClientModel::getLastBlockDate() const
68 {
69     if (pindexBest)
70         return QDateTime::fromTime_t(pindexBest->GetBlockTime());
71     else
72         return QDateTime::fromTime_t(1360105017); // Genesis block's time
73 }
74
75 void ClientModel::updateTimer()
76 {
77     // Some quantities (such as number of blocks) change so fast that we don't want to be notified for each change.
78     // Periodically check and update with a timer.
79     int newNumBlocks = getNumBlocks();
80     int newNumBlocksOfPeers = getNumBlocksOfPeers();
81
82     if(cachedNumBlocks != newNumBlocks || cachedNumBlocksOfPeers != newNumBlocksOfPeers)
83     {
84         cachedNumBlocks = newNumBlocks;
85         cachedNumBlocksOfPeers = newNumBlocksOfPeers;
86
87         emit numBlocksChanged(newNumBlocks, newNumBlocksOfPeers);
88     }
89 }
90
91 void ClientModel::updateNumConnections(int numConnections)
92 {
93     emit numConnectionsChanged(numConnections);
94 }
95
96 void ClientModel::updateAlert(const QString &hash, int status)
97 {
98     // Show error message notification for new alert
99     if(status == CT_NEW)
100     {
101         uint256 hash_256;
102         hash_256.SetHex(hash.toStdString());
103         CAlert alert = CAlert::getAlertByHash(hash_256);
104         if(!alert.IsNull())
105         {
106             emit error(tr("Network Alert"), QString::fromStdString(alert.strStatusBar), false);
107         }
108     }
109
110     // Emit a numBlocksChanged when the status message changes,
111     // so that the view recomputes and updates the status bar.
112     emit numBlocksChanged(getNumBlocks(), getNumBlocksOfPeers());
113 }
114
115 bool ClientModel::isTestNet() const
116 {
117     return fTestNet;
118 }
119
120 bool ClientModel::inInitialBlockDownload() const
121 {
122     return IsInitialBlockDownload();
123 }
124
125 int ClientModel::getNumBlocksOfPeers() const
126 {
127     return GetNumBlocksOfPeers();
128 }
129
130 QString ClientModel::getStatusBarWarnings() const
131 {
132     return QString::fromStdString(GetWarnings("statusbar"));
133 }
134
135 OptionsModel *ClientModel::getOptionsModel()
136 {
137     return optionsModel;
138 }
139
140 QString ClientModel::formatFullVersion() const
141 {
142     return QString::fromStdString(FormatFullVersion());
143 }
144
145 QString ClientModel::formatBuildDate() const
146 {
147     return QString::fromStdString(CLIENT_DATE);
148 }
149
150 QString ClientModel::clientName() const
151 {
152     return QString::fromStdString(CLIENT_NAME);
153 }
154
155 QString ClientModel::formatClientStartupTime() const
156 {
157     return QDateTime::fromTime_t(nClientStartupTime).toString();
158 }
159
160 // Handlers for core signals
161 static void NotifyBlocksChanged(ClientModel *clientmodel)
162 {
163     // This notification is too frequent. Don't trigger a signal.
164     // Don't remove it, though, as it might be useful later.
165 }
166
167 static void NotifyNumConnectionsChanged(ClientModel *clientmodel, int newNumConnections)
168 {
169     // Too noisy: OutputDebugStringF("NotifyNumConnectionsChanged %i\n", newNumConnections);
170     QMetaObject::invokeMethod(clientmodel, "updateNumConnections", Qt::QueuedConnection,
171                               Q_ARG(int, newNumConnections));
172 }
173
174 static void NotifyAlertChanged(ClientModel *clientmodel, const uint256 &hash, ChangeType status)
175 {
176     OutputDebugStringF("NotifyAlertChanged %s status=%i\n", hash.GetHex().c_str(), status);
177     QMetaObject::invokeMethod(clientmodel, "updateAlert", Qt::QueuedConnection,
178                               Q_ARG(QString, QString::fromStdString(hash.GetHex())),
179                               Q_ARG(int, status));
180 }
181
182 void ClientModel::subscribeToCoreSignals()
183 {
184     // Connect signals to client
185     uiInterface.NotifyBlocksChanged.connect(boost::bind(NotifyBlocksChanged, this));
186     uiInterface.NotifyNumConnectionsChanged.connect(boost::bind(NotifyNumConnectionsChanged, this, _1));
187     uiInterface.NotifyAlertChanged.connect(boost::bind(NotifyAlertChanged, this, _1, _2));
188 }
189
190 void ClientModel::unsubscribeFromCoreSignals()
191 {
192     // Disconnect signals from client
193     uiInterface.NotifyBlocksChanged.disconnect(boost::bind(NotifyBlocksChanged, this));
194     uiInterface.NotifyNumConnectionsChanged.disconnect(boost::bind(NotifyNumConnectionsChanged, this, _1));
195     uiInterface.NotifyAlertChanged.disconnect(boost::bind(NotifyAlertChanged, this, _1, _2));
196 }