Fix "Last received block was generated Up to date"
[novacoin.git] / src / qt / bitcoingui.cpp
1 /*
2  * Qt4 bitcoin GUI.
3  *
4  * W.J. van der Laan 2011
5  */
6 #include "bitcoingui.h"
7 #include "transactiontablemodel.h"
8 #include "addressbookpage.h"
9 #include "sendcoinsdialog.h"
10 #include "optionsdialog.h"
11 #include "aboutdialog.h"
12 #include "clientmodel.h"
13 #include "walletmodel.h"
14 #include "guiutil.h"
15 #include "editaddressdialog.h"
16 #include "optionsmodel.h"
17 #include "transactiondescdialog.h"
18 #include "addresstablemodel.h"
19 #include "transactionview.h"
20 #include "overviewpage.h"
21
22 #include <QApplication>
23 #include <QMainWindow>
24 #include <QMenuBar>
25 #include <QMenu>
26 #include <QIcon>
27 #include <QTabWidget>
28 #include <QVBoxLayout>
29 #include <QToolBar>
30 #include <QStatusBar>
31 #include <QLabel>
32 #include <QLineEdit>
33 #include <QPushButton>
34 #include <QLocale>
35 #include <QMessageBox>
36 #include <QProgressBar>
37 #include <QStackedWidget>
38 #include <QDateTime>
39 #include <QMovie>
40
41 #include <QDebug>
42
43 #include <iostream>
44
45 BitcoinGUI::BitcoinGUI(QWidget *parent):
46     QMainWindow(parent),
47     clientModel(0),
48     walletModel(0),
49     trayIcon(0)
50 {
51     resize(850, 550);
52     setWindowTitle(tr("Bitcoin Wallet"));
53     setWindowIcon(QIcon(":icons/bitcoin"));
54
55     createActions();
56
57     // Menus
58     QMenu *file = menuBar()->addMenu("&File");
59     file->addAction(sendCoinsAction);
60     file->addAction(receiveCoinsAction);
61     file->addSeparator();
62     file->addAction(quitAction);
63     
64     QMenu *settings = menuBar()->addMenu("&Settings");
65     settings->addAction(optionsAction);
66
67     QMenu *help = menuBar()->addMenu("&Help");
68     help->addAction(aboutAction);
69     
70     // Toolbar
71     QToolBar *toolbar = addToolBar("Main toolbar");
72     toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
73     toolbar->addAction(overviewAction);
74     toolbar->addAction(sendCoinsAction);
75     toolbar->addAction(receiveCoinsAction);
76     toolbar->addAction(historyAction);
77     toolbar->addAction(addressBookAction);
78
79     QToolBar *toolbar2 = addToolBar("Transactions toolbar");
80     toolbar2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
81     toolbar2->addAction(exportAction);
82
83     // Overview page
84     overviewPage = new OverviewPage();
85     QVBoxLayout *vbox = new QVBoxLayout();
86
87     transactionView = new TransactionView(this);
88     connect(transactionView, SIGNAL(doubleClicked(const QModelIndex&)), transactionView, SLOT(showDetails()));
89     vbox->addWidget(transactionView);
90
91     transactionsPage = new QWidget(this);
92     transactionsPage->setLayout(vbox);
93
94     addressBookPage = new AddressBookPage(AddressBookPage::ForEditing, AddressBookPage::SendingTab);
95
96     receiveCoinsPage = new AddressBookPage(AddressBookPage::ForEditing, AddressBookPage::ReceivingTab);
97
98     sendCoinsPage = new SendCoinsDialog(this);
99
100     centralWidget = new QStackedWidget(this);
101     centralWidget->addWidget(overviewPage);
102     centralWidget->addWidget(transactionsPage);
103     centralWidget->addWidget(addressBookPage);
104     centralWidget->addWidget(receiveCoinsPage);
105     centralWidget->addWidget(sendCoinsPage);
106     setCentralWidget(centralWidget);
107     
108     // Create status bar
109     statusBar();
110
111     // Status bar "Connections" notification
112     QFrame *frameConnections = new QFrame();
113     frameConnections->setFrameStyle(QFrame::Panel | QFrame::Sunken);
114     frameConnections->setMinimumWidth(150);
115     frameConnections->setMaximumWidth(150);
116     QHBoxLayout *frameConnectionsLayout = new QHBoxLayout(frameConnections);
117     frameConnectionsLayout->setContentsMargins(3,0,3,0);
118     frameConnectionsLayout->setSpacing(3);
119     labelConnectionsIcon = new QLabel();
120     labelConnectionsIcon->setToolTip(tr("Number of connections to other clients"));
121     frameConnectionsLayout->addWidget(labelConnectionsIcon);
122     labelConnections = new QLabel();
123     labelConnections->setToolTip(tr("Number of connections to other clients"));
124     frameConnectionsLayout->addWidget(labelConnections);
125     frameConnectionsLayout->addStretch();
126
127     // Status bar "Blocks" notification
128     QFrame *frameBlocks = new QFrame();
129     frameBlocks->setFrameStyle(QFrame::Panel | QFrame::Sunken);
130     frameBlocks->setMinimumWidth(150);
131     frameBlocks->setMaximumWidth(150);
132     QHBoxLayout *frameBlocksLayout = new QHBoxLayout(frameBlocks);
133     frameBlocksLayout->setContentsMargins(3,0,3,0);
134     frameBlocksLayout->setSpacing(3);
135     labelBlocksIcon = new QLabel();
136     frameBlocksLayout->addWidget(labelBlocksIcon);
137     labelBlocks = new QLabel();
138     frameBlocksLayout->addWidget(labelBlocks);
139     frameBlocksLayout->addStretch();
140
141     // Progress bar for blocks download
142     progressBarLabel = new QLabel(tr("Synchronizing with network..."));
143     progressBarLabel->setVisible(false);
144     progressBar = new QProgressBar();
145     progressBar->setToolTip(tr("Block chain synchronization in progress"));
146     progressBar->setVisible(false);
147
148     statusBar()->addWidget(progressBarLabel);
149     statusBar()->addWidget(progressBar);
150     statusBar()->addPermanentWidget(frameConnections);
151     statusBar()->addPermanentWidget(frameBlocks);
152
153     createTrayIcon();
154
155     syncIconMovie = new QMovie(":/movies/update_spinner", "mng", this);
156
157     gotoOverviewPage();
158 }
159
160 void BitcoinGUI::createActions()
161 {
162     QActionGroup *tabGroup = new QActionGroup(this);
163
164     overviewAction = new QAction(QIcon(":/icons/overview"), tr("&Overview"), this);
165     overviewAction->setCheckable(true);
166     tabGroup->addAction(overviewAction);
167
168     historyAction = new QAction(QIcon(":/icons/history"), tr("&Transactions"), this);
169     historyAction->setCheckable(true);
170     tabGroup->addAction(historyAction);
171
172     addressBookAction = new QAction(QIcon(":/icons/address-book"), tr("&Address Book"), this);
173     addressBookAction->setToolTip(tr("Edit the list of stored addresses and labels"));
174     addressBookAction->setCheckable(true);
175     tabGroup->addAction(addressBookAction);
176
177     receiveCoinsAction = new QAction(QIcon(":/icons/receiving_addresses"), tr("&Receive coins"), this);
178     receiveCoinsAction->setToolTip(tr("Show the list of addresses for receiving payments"));
179     receiveCoinsAction->setCheckable(true);
180     tabGroup->addAction(receiveCoinsAction);
181
182     sendCoinsAction = new QAction(QIcon(":/icons/send"), tr("&Send coins"), this);
183     sendCoinsAction->setToolTip(tr("Send coins to a bitcoin address"));
184     sendCoinsAction->setCheckable(true);
185     tabGroup->addAction(sendCoinsAction);
186
187     connect(overviewAction, SIGNAL(triggered()), this, SLOT(gotoOverviewPage()));
188     connect(historyAction, SIGNAL(triggered()), this, SLOT(gotoHistoryPage()));
189     connect(addressBookAction, SIGNAL(triggered()), this, SLOT(gotoAddressBookPage()));
190     connect(receiveCoinsAction, SIGNAL(triggered()), this, SLOT(gotoReceiveCoinsPage()));
191     connect(sendCoinsAction, SIGNAL(triggered()), this, SLOT(gotoSendCoinsPage()));
192
193     quitAction = new QAction(QIcon(":/icons/quit"), tr("&Exit"), this);
194     quitAction->setToolTip(tr("Quit application"));
195     aboutAction = new QAction(QIcon(":/icons/bitcoin"), tr("&About"), this);
196     aboutAction->setToolTip(tr("Show information about Bitcoin"));
197     optionsAction = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
198     optionsAction->setToolTip(tr("Modify configuration options for bitcoin"));
199     openBitcoinAction = new QAction(QIcon(":/icons/bitcoin"), tr("Open &Bitcoin"), this);
200     openBitcoinAction->setToolTip(tr("Show the Bitcoin window"));
201     exportAction = new QAction(QIcon(":/icons/export"), tr("&Export..."), this);
202     exportAction->setToolTip(tr("Export data in current view to a file"));
203
204     connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
205     connect(optionsAction, SIGNAL(triggered()), this, SLOT(optionsClicked()));
206     connect(aboutAction, SIGNAL(triggered()), this, SLOT(aboutClicked()));
207     connect(openBitcoinAction, SIGNAL(triggered()), this, SLOT(show()));
208 }
209
210 void BitcoinGUI::setClientModel(ClientModel *clientModel)
211 {
212     this->clientModel = clientModel;
213
214     if(clientModel->isTestNet())
215     {
216         QString title_testnet = windowTitle() + QString(" ") + tr("[testnet]");
217         setWindowTitle(title_testnet);
218         setWindowIcon(QIcon(":icons/bitcoin_testnet"));
219         if(trayIcon)
220         {
221             trayIcon->setToolTip(title_testnet);
222             trayIcon->setIcon(QIcon(":/icons/toolbar_testnet"));
223         }
224     }
225
226     // Keep up to date with client
227     setNumConnections(clientModel->getNumConnections());
228     connect(clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
229
230     setNumBlocks(clientModel->getNumBlocks());
231     connect(clientModel, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
232
233     // Report errors from network/worker thread
234     connect(clientModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
235 }
236
237 void BitcoinGUI::setWalletModel(WalletModel *walletModel)
238 {
239     this->walletModel = walletModel;
240
241     // Report errors from wallet thread
242     connect(walletModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
243
244     // Put transaction list in tabs
245     transactionView->setModel(walletModel);
246
247     overviewPage->setModel(walletModel);
248     addressBookPage->setModel(walletModel->getAddressTableModel());
249     receiveCoinsPage->setModel(walletModel->getAddressTableModel());
250     sendCoinsPage->setModel(walletModel);
251
252     // Balloon popup for new transaction
253     connect(walletModel->getTransactionTableModel(), SIGNAL(rowsInserted(const QModelIndex &, int, int)),
254             this, SLOT(incomingTransaction(const QModelIndex &, int, int)));
255 }
256
257 void BitcoinGUI::createTrayIcon()
258 {
259     QMenu *trayIconMenu = new QMenu(this);
260     trayIconMenu->addAction(openBitcoinAction);
261     trayIconMenu->addAction(sendCoinsAction);
262     trayIconMenu->addAction(optionsAction);
263     trayIconMenu->addSeparator();
264     trayIconMenu->addAction(quitAction);
265
266     trayIcon = new QSystemTrayIcon(this);
267     trayIcon->setContextMenu(trayIconMenu);
268     trayIcon->setToolTip("Bitcoin client");
269     trayIcon->setIcon(QIcon(":/icons/toolbar"));
270     connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
271             this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
272     trayIcon->show();
273 }
274
275 void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
276 {
277     if(reason == QSystemTrayIcon::DoubleClick)
278     {
279         // Doubleclick on system tray icon triggers "open bitcoin"
280         openBitcoinAction->trigger();
281     }
282 }
283
284 void BitcoinGUI::optionsClicked()
285 {
286     OptionsDialog dlg;
287     dlg.setModel(clientModel->getOptionsModel());
288     dlg.exec();
289 }
290
291 void BitcoinGUI::aboutClicked()
292 {
293     AboutDialog dlg;
294     dlg.setModel(clientModel);
295     dlg.exec();
296 }
297
298 void BitcoinGUI::setNumConnections(int count)
299 {
300     QString icon;
301     switch(count)
302     {
303     case 0: icon = ":/icons/connect_0"; break;
304     case 1: case 2: case 3: icon = ":/icons/connect_1"; break;
305     case 4: case 5: case 6: icon = ":/icons/connect_2"; break;
306     case 7: case 8: case 9: icon = ":/icons/connect_3"; break;
307     default: icon = ":/icons/connect_4"; break;
308     }
309     labelConnectionsIcon->setPixmap(QIcon(icon).pixmap(16,16));
310     labelConnections->setText(tr("%n connection(s)", "", count));
311 }
312
313 void BitcoinGUI::setNumBlocks(int count)
314 {
315     int total = clientModel->getTotalBlocksEstimate();
316     QString tooltip;
317
318     if(count < total)
319     {
320         progressBarLabel->setVisible(true);
321         progressBar->setVisible(true);
322         progressBar->setMaximum(total);
323         progressBar->setValue(count);
324         tooltip = tr("Downloaded %1 of %2 blocks of transaction history.").arg(count).arg(total);
325     }
326     else
327     {
328         progressBarLabel->setVisible(false);
329         progressBar->setVisible(false);
330         tooltip = tr("Downloaded %1 blocks of transaction history.").arg(count);
331     }
332
333     QDateTime now = QDateTime::currentDateTime();
334     QDateTime lastBlockDate = clientModel->getLastBlockDate();
335     int secs = lastBlockDate.secsTo(now);
336     QString text;
337
338     // Represent time from last generated block in human readable text
339     if(secs < 60)
340     {
341         text = tr("%n second(s) ago","",secs);
342     }
343     else if(secs < 60*60)
344     {
345         text = tr("%n minute(s) ago","",secs/60);
346     }
347     else if(secs < 24*60*60)
348     {
349         text = tr("%n hour(s) ago","",secs/(60*60));
350     }
351     else
352     {
353         text = tr("%n day(s) ago","",secs/(60*60*24));
354     }
355
356     // In the label we want to be less specific
357     QString labelText = text;
358     bool spinning = true;
359     if(secs < 30*60)
360     {
361         labelText = "Up to date";
362         spinning = false;
363     }
364
365     tooltip += QString("\n");
366     tooltip += tr("Last received block was generated %1.").arg(text);
367
368     if(spinning)
369     {
370         labelBlocksIcon->setMovie(syncIconMovie);
371         syncIconMovie->start();
372     }
373     else
374     {
375         labelBlocksIcon->setPixmap(QIcon(":/icons/synced").pixmap(16,16));
376     }
377     labelBlocks->setText(labelText);
378
379     labelBlocksIcon->setToolTip(tooltip);
380     labelBlocks->setToolTip(tooltip);
381     progressBarLabel->setToolTip(tooltip);
382     progressBar->setToolTip(tooltip);
383 }
384
385 void BitcoinGUI::error(const QString &title, const QString &message)
386 {
387     // Report errors from network/worker thread
388     if(trayIcon->supportsMessages())
389     {
390         // Show as "balloon" message if possible
391         trayIcon->showMessage(title, message, QSystemTrayIcon::Critical);
392     }
393     else
394     {
395         // Fall back to old fashioned popup dialog if not
396         QMessageBox::critical(this, title,
397             message,
398             QMessageBox::Ok, QMessageBox::Ok);
399     }
400 }
401
402 void BitcoinGUI::changeEvent(QEvent *e)
403 {
404     if (e->type() == QEvent::WindowStateChange)
405     {
406         if(clientModel->getOptionsModel()->getMinimizeToTray())
407         {
408             if (isMinimized())
409             {
410                 hide();
411                 e->ignore();
412             }
413             else
414             {
415                 e->accept();
416             }
417         }
418     }
419     QMainWindow::changeEvent(e);
420 }
421
422 void BitcoinGUI::closeEvent(QCloseEvent *event)
423 {
424     if(!clientModel->getOptionsModel()->getMinimizeToTray() &&
425        !clientModel->getOptionsModel()->getMinimizeOnClose())
426     {
427         qApp->quit();
428     }
429     QMainWindow::closeEvent(event);
430 }
431
432 void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
433 {
434     QString strMessage =
435         tr("This transaction is over the size limit.  You can still send it for a fee of %1, "
436           "which goes to the nodes that process your transaction and helps to support the network.  "
437           "Do you want to pay the fee?").arg(GUIUtil::formatMoney(nFeeRequired));
438     QMessageBox::StandardButton retval = QMessageBox::question(
439           this, tr("Sending..."), strMessage,
440           QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
441     *payFee = (retval == QMessageBox::Yes);
442 }
443
444 void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int end)
445 {
446     TransactionTableModel *ttm = walletModel->getTransactionTableModel();
447     qint64 amount = ttm->index(start, TransactionTableModel::Amount, parent)
448                     .data(Qt::EditRole).toULongLong();
449     if(!clientModel->inInitialBlockDownload())
450     {
451         // On incoming transaction, make an info balloon
452         // Unless the initial block download is in progress, to prevent balloon-spam
453         QString date = ttm->index(start, TransactionTableModel::Date, parent)
454                         .data().toString();
455         QString type = ttm->index(start, TransactionTableModel::Type, parent)
456                         .data().toString();
457         QString address = ttm->index(start, TransactionTableModel::ToAddress, parent)
458                         .data().toString();
459
460         trayIcon->showMessage((amount)<0 ? tr("Sent transaction") :
461                                            tr("Incoming transaction"),
462                               tr("Date: ") + date + "\n" +
463                               tr("Amount: ") + GUIUtil::formatMoney(amount, true) + "\n" +
464                               tr("Type: ") + type + "\n" +
465                               tr("Address: ") + address + "\n",
466                               QSystemTrayIcon::Information);
467     }
468 }
469
470 void BitcoinGUI::gotoOverviewPage()
471 {
472     overviewAction->setChecked(true);
473     centralWidget->setCurrentWidget(overviewPage);
474
475     exportAction->setEnabled(false);
476     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
477 }
478
479 void BitcoinGUI::gotoHistoryPage()
480 {
481     historyAction->setChecked(true);
482     centralWidget->setCurrentWidget(transactionsPage);
483
484     exportAction->setEnabled(true);
485     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
486     connect(exportAction, SIGNAL(triggered()), transactionView, SLOT(exportClicked()));
487 }
488
489 void BitcoinGUI::gotoAddressBookPage()
490 {
491     addressBookAction->setChecked(true);
492     centralWidget->setCurrentWidget(addressBookPage);
493
494     exportAction->setEnabled(true);
495     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
496     connect(exportAction, SIGNAL(triggered()), addressBookPage, SLOT(exportClicked()));
497 }
498
499 void BitcoinGUI::gotoReceiveCoinsPage()
500 {
501     receiveCoinsAction->setChecked(true);
502     centralWidget->setCurrentWidget(receiveCoinsPage);
503
504     exportAction->setEnabled(true);
505     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
506     connect(exportAction, SIGNAL(triggered()), receiveCoinsPage, SLOT(exportClicked()));
507 }
508
509 void BitcoinGUI::gotoSendCoinsPage()
510 {
511     sendCoinsAction->setChecked(true);
512     sendCoinsPage->clear();
513     centralWidget->setCurrentWidget(sendCoinsPage);
514
515     exportAction->setEnabled(false);
516     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
517 }
518