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