6e7606549ae52ea5eae611e065d3a7226cfeb38e
[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 "editaddressdialog.h"
15 #include "optionsmodel.h"
16 #include "transactiondescdialog.h"
17 #include "addresstablemodel.h"
18 #include "transactionview.h"
19 #include "overviewpage.h"
20 #include "bitcoinunits.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(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     // Clicking on a transaction simply sends you to transaction history page
158     connect(overviewPage, SIGNAL(transactionClicked(QModelIndex)), this, SLOT(gotoHistoryPage()));
159
160     gotoOverviewPage();    
161 }
162
163 void BitcoinGUI::createActions()
164 {
165     QActionGroup *tabGroup = new QActionGroup(this);
166
167     overviewAction = new QAction(QIcon(":/icons/overview"), tr("&Overview"), this);
168     overviewAction->setToolTip(tr("Show general overview of wallet"));
169     overviewAction->setCheckable(true);
170     tabGroup->addAction(overviewAction);
171
172     historyAction = new QAction(QIcon(":/icons/history"), tr("&Transactions"), this);
173     historyAction->setToolTip(tr("Browse transaction history"));
174     historyAction->setCheckable(true);
175     tabGroup->addAction(historyAction);
176
177     addressBookAction = new QAction(QIcon(":/icons/address-book"), tr("&Address Book"), this);
178     addressBookAction->setToolTip(tr("Edit the list of stored addresses and labels"));
179     addressBookAction->setCheckable(true);
180     tabGroup->addAction(addressBookAction);
181
182     receiveCoinsAction = new QAction(QIcon(":/icons/receiving_addresses"), tr("&Receive coins"), this);
183     receiveCoinsAction->setToolTip(tr("Show the list of addresses for receiving payments"));
184     receiveCoinsAction->setCheckable(true);
185     tabGroup->addAction(receiveCoinsAction);
186
187     sendCoinsAction = new QAction(QIcon(":/icons/send"), tr("&Send coins"), this);
188     sendCoinsAction->setToolTip(tr("Send coins to a bitcoin address"));
189     sendCoinsAction->setCheckable(true);
190     tabGroup->addAction(sendCoinsAction);
191
192     connect(overviewAction, SIGNAL(triggered()), this, SLOT(gotoOverviewPage()));
193     connect(historyAction, SIGNAL(triggered()), this, SLOT(gotoHistoryPage()));
194     connect(addressBookAction, SIGNAL(triggered()), this, SLOT(gotoAddressBookPage()));
195     connect(receiveCoinsAction, SIGNAL(triggered()), this, SLOT(gotoReceiveCoinsPage()));
196     connect(sendCoinsAction, SIGNAL(triggered()), this, SLOT(gotoSendCoinsPage()));
197
198     quitAction = new QAction(QIcon(":/icons/quit"), tr("&Exit"), this);
199     quitAction->setToolTip(tr("Quit application"));
200     aboutAction = new QAction(QIcon(":/icons/bitcoin"), tr("&About"), this);
201     aboutAction->setToolTip(tr("Show information about Bitcoin"));
202     optionsAction = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
203     optionsAction->setToolTip(tr("Modify configuration options for bitcoin"));
204     openBitcoinAction = new QAction(QIcon(":/icons/bitcoin"), tr("Open &Bitcoin"), this);
205     openBitcoinAction->setToolTip(tr("Show the Bitcoin window"));
206     exportAction = new QAction(QIcon(":/icons/export"), tr("&Export..."), this);
207     exportAction->setToolTip(tr("Export the current view to a file"));
208
209     connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
210     connect(optionsAction, SIGNAL(triggered()), this, SLOT(optionsClicked()));
211     connect(aboutAction, SIGNAL(triggered()), this, SLOT(aboutClicked()));
212     connect(openBitcoinAction, SIGNAL(triggered()), this, SLOT(show()));
213 }
214
215 void BitcoinGUI::setClientModel(ClientModel *clientModel)
216 {
217     this->clientModel = clientModel;
218
219     if(clientModel->isTestNet())
220     {
221         QString title_testnet = windowTitle() + QString(" ") + tr("[testnet]");
222         setWindowTitle(title_testnet);
223         setWindowIcon(QIcon(":icons/bitcoin_testnet"));
224         if(trayIcon)
225         {
226             trayIcon->setToolTip(title_testnet);
227             trayIcon->setIcon(QIcon(":/icons/toolbar_testnet"));
228         }
229     }
230
231     // Keep up to date with client
232     setNumConnections(clientModel->getNumConnections());
233     connect(clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
234
235     setNumBlocks(clientModel->getNumBlocks());
236     connect(clientModel, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
237
238     // Report errors from network/worker thread
239     connect(clientModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
240 }
241
242 void BitcoinGUI::setWalletModel(WalletModel *walletModel)
243 {
244     this->walletModel = walletModel;
245
246     // Report errors from wallet thread
247     connect(walletModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
248
249     // Put transaction list in tabs
250     transactionView->setModel(walletModel);
251
252     overviewPage->setModel(walletModel);
253     addressBookPage->setModel(walletModel->getAddressTableModel());
254     receiveCoinsPage->setModel(walletModel->getAddressTableModel());
255     sendCoinsPage->setModel(walletModel);
256
257     // Balloon popup for new transaction
258     connect(walletModel->getTransactionTableModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
259             this, SLOT(incomingTransaction(QModelIndex,int,int)));
260 }
261
262 void BitcoinGUI::createTrayIcon()
263 {
264     QMenu *trayIconMenu = new QMenu(this);
265     trayIconMenu->addAction(openBitcoinAction);
266     trayIconMenu->addAction(sendCoinsAction);
267     trayIconMenu->addAction(optionsAction);
268     trayIconMenu->addSeparator();
269     trayIconMenu->addAction(quitAction);
270
271     trayIcon = new QSystemTrayIcon(this);
272     trayIcon->setContextMenu(trayIconMenu);
273     trayIcon->setToolTip("Bitcoin client");
274     trayIcon->setIcon(QIcon(":/icons/toolbar"));
275     connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
276             this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
277     trayIcon->show();
278 }
279
280 void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
281 {
282     if(reason == QSystemTrayIcon::DoubleClick)
283     {
284         // Doubleclick on system tray icon triggers "open bitcoin"
285         openBitcoinAction->trigger();
286     }
287 }
288
289 void BitcoinGUI::optionsClicked()
290 {
291     OptionsDialog dlg;
292     dlg.setModel(clientModel->getOptionsModel());
293     dlg.exec();
294 }
295
296 void BitcoinGUI::aboutClicked()
297 {
298     AboutDialog dlg;
299     dlg.setModel(clientModel);
300     dlg.exec();
301 }
302
303 void BitcoinGUI::setNumConnections(int count)
304 {
305     QString icon;
306     switch(count)
307     {
308     case 0: icon = ":/icons/connect_0"; break;
309     case 1: case 2: case 3: icon = ":/icons/connect_1"; break;
310     case 4: case 5: case 6: icon = ":/icons/connect_2"; break;
311     case 7: case 8: case 9: icon = ":/icons/connect_3"; break;
312     default: icon = ":/icons/connect_4"; break;
313     }
314     labelConnectionsIcon->setPixmap(QIcon(icon).pixmap(16,16));
315     labelConnections->setText(tr("%n connection(s)", "", count));
316 }
317
318 void BitcoinGUI::setNumBlocks(int count)
319 {
320     int total = clientModel->getTotalBlocksEstimate();
321     QString tooltip;
322
323     if(count < total)
324     {
325         progressBarLabel->setVisible(true);
326         progressBar->setVisible(true);
327         progressBar->setMaximum(total);
328         progressBar->setValue(count);
329         tooltip = tr("Downloaded %1 of %2 blocks of transaction history.").arg(count).arg(total);
330     }
331     else
332     {
333         progressBarLabel->setVisible(false);
334         progressBar->setVisible(false);
335         tooltip = tr("Downloaded %1 blocks of transaction history.").arg(count);
336     }
337
338     QDateTime now = QDateTime::currentDateTime();
339     QDateTime lastBlockDate = clientModel->getLastBlockDate();
340     int secs = lastBlockDate.secsTo(now);
341     QString text;
342
343     // Represent time from last generated block in human readable text
344     if(secs < 60)
345     {
346         text = tr("%n second(s) ago","",secs);
347     }
348     else if(secs < 60*60)
349     {
350         text = tr("%n minute(s) ago","",secs/60);
351     }
352     else if(secs < 24*60*60)
353     {
354         text = tr("%n hour(s) ago","",secs/(60*60));
355     }
356     else
357     {
358         text = tr("%n day(s) ago","",secs/(60*60*24));
359     }
360
361     // In the label we want to be less specific
362     QString labelText = text;
363     bool spinning = true;
364     if(secs < 30*60)
365     {
366         labelText = "Up to date";
367         spinning = false;
368     }
369
370     tooltip += QString("\n");
371     tooltip += tr("Last received block was generated %1.").arg(text);
372
373     if(spinning)
374     {
375         labelBlocksIcon->setMovie(syncIconMovie);
376         syncIconMovie->start();
377     }
378     else
379     {
380         labelBlocksIcon->setPixmap(QIcon(":/icons/synced").pixmap(16,16));
381     }
382     labelBlocks->setText(labelText);
383
384     labelBlocksIcon->setToolTip(tooltip);
385     labelBlocks->setToolTip(tooltip);
386     progressBarLabel->setToolTip(tooltip);
387     progressBar->setToolTip(tooltip);
388 }
389
390 void BitcoinGUI::error(const QString &title, const QString &message)
391 {
392     // Report errors from network/worker thread
393     if(trayIcon->supportsMessages())
394     {
395         // Show as "balloon" message if possible
396         trayIcon->showMessage(title, message, QSystemTrayIcon::Critical);
397     }
398     else
399     {
400         // Fall back to old fashioned popup dialog if not
401         QMessageBox::critical(this, title,
402             message,
403             QMessageBox::Ok, QMessageBox::Ok);
404     }
405 }
406
407 void BitcoinGUI::changeEvent(QEvent *e)
408 {
409     if (e->type() == QEvent::WindowStateChange)
410     {
411         if(clientModel->getOptionsModel()->getMinimizeToTray())
412         {
413             if (isMinimized())
414             {
415                 hide();
416                 e->ignore();
417             }
418             else
419             {
420                 e->accept();
421             }
422         }
423     }
424     QMainWindow::changeEvent(e);
425 }
426
427 void BitcoinGUI::closeEvent(QCloseEvent *event)
428 {
429     if(!clientModel->getOptionsModel()->getMinimizeToTray() &&
430        !clientModel->getOptionsModel()->getMinimizeOnClose())
431     {
432         qApp->quit();
433     }
434     QMainWindow::closeEvent(event);
435 }
436
437 void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
438 {
439     QString strMessage =
440         tr("This transaction is over the size limit.  You can still send it for a fee of %1, "
441           "which goes to the nodes that process your transaction and helps to support the network.  "
442           "Do you want to pay the fee?").arg(
443                 BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, nFeeRequired));
444     QMessageBox::StandardButton retval = QMessageBox::question(
445           this, tr("Sending..."), strMessage,
446           QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
447     *payFee = (retval == QMessageBox::Yes);
448 }
449
450 void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int end)
451 {
452     TransactionTableModel *ttm = walletModel->getTransactionTableModel();
453     qint64 amount = ttm->index(start, TransactionTableModel::Amount, parent)
454                     .data(Qt::EditRole).toULongLong();
455     if(!clientModel->inInitialBlockDownload())
456     {
457         // On incoming transaction, make an info balloon
458         // Unless the initial block download is in progress, to prevent balloon-spam
459         QString date = ttm->index(start, TransactionTableModel::Date, parent)
460                         .data().toString();
461         QString type = ttm->index(start, TransactionTableModel::Type, parent)
462                         .data().toString();
463         QString address = ttm->index(start, TransactionTableModel::ToAddress, parent)
464                         .data().toString();
465
466         trayIcon->showMessage((amount)<0 ? tr("Sent transaction") :
467                                            tr("Incoming transaction"),
468                               tr("Date: ") + date + "\n" +
469                               tr("Amount: ") + BitcoinUnits::formatWithUnit(walletModel->getOptionsModel()->getDisplayUnit(), amount, true) + "\n" +
470                               tr("Type: ") + type + "\n" +
471                               tr("Address: ") + address + "\n",
472                               QSystemTrayIcon::Information);
473     }
474 }
475
476 void BitcoinGUI::gotoOverviewPage()
477 {
478     overviewAction->setChecked(true);
479     centralWidget->setCurrentWidget(overviewPage);
480
481     exportAction->setEnabled(false);
482     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
483 }
484
485 void BitcoinGUI::gotoHistoryPage()
486 {
487     historyAction->setChecked(true);
488     centralWidget->setCurrentWidget(transactionsPage);
489
490     exportAction->setEnabled(true);
491     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
492     connect(exportAction, SIGNAL(triggered()), transactionView, SLOT(exportClicked()));
493 }
494
495 void BitcoinGUI::gotoAddressBookPage()
496 {
497     addressBookAction->setChecked(true);
498     centralWidget->setCurrentWidget(addressBookPage);
499
500     exportAction->setEnabled(true);
501     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
502     connect(exportAction, SIGNAL(triggered()), addressBookPage, SLOT(exportClicked()));
503 }
504
505 void BitcoinGUI::gotoReceiveCoinsPage()
506 {
507     receiveCoinsAction->setChecked(true);
508     centralWidget->setCurrentWidget(receiveCoinsPage);
509
510     exportAction->setEnabled(true);
511     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
512     connect(exportAction, SIGNAL(triggered()), receiveCoinsPage, SLOT(exportClicked()));
513 }
514
515 void BitcoinGUI::gotoSendCoinsPage()
516 {
517     sendCoinsAction->setChecked(true);
518     sendCoinsPage->clear();
519     centralWidget->setCurrentWidget(sendCoinsPage);
520
521     exportAction->setEnabled(false);
522     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
523 }
524