6a6f3f32ecda7acae03feef1ffd9f3209f629384
[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&)), transactionView, SLOT(showDetails()));
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 }
188
189 void BitcoinGUI::setClientModel(ClientModel *clientModel)
190 {
191     this->clientModel = clientModel;
192
193     if(clientModel->isTestNet())
194     {
195         QString title_testnet = windowTitle() + QString(" ") + tr("[testnet]");
196         setWindowTitle(title_testnet);
197         setWindowIcon(QIcon(":icons/bitcoin_testnet"));
198         if(trayIcon)
199         {
200             trayIcon->setToolTip(title_testnet);
201             trayIcon->setIcon(QIcon(":/icons/toolbar_testnet"));
202         }
203     }
204
205     // Keep up to date with client
206     setNumConnections(clientModel->getNumConnections());
207     connect(clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
208
209     setNumBlocks(clientModel->getNumBlocks());
210     connect(clientModel, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
211
212     // Report errors from network/worker thread
213     connect(clientModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
214 }
215
216 void BitcoinGUI::setWalletModel(WalletModel *walletModel)
217 {
218     this->walletModel = walletModel;
219
220     // Report errors from wallet thread
221     connect(walletModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
222
223     // Put transaction list in tabs
224     transactionView->setModel(walletModel);
225
226     overviewPage->setModel(walletModel);
227     addressBookPage->setModel(walletModel->getAddressTableModel());
228     receiveCoinsPage->setModel(walletModel->getAddressTableModel());
229     sendCoinsPage->setModel(walletModel);
230
231     // Balloon popup for new transaction
232     connect(walletModel->getTransactionTableModel(), SIGNAL(rowsInserted(const QModelIndex &, int, int)),
233             this, SLOT(incomingTransaction(const QModelIndex &, int, int)));
234 }
235
236 void BitcoinGUI::createTrayIcon()
237 {
238     QMenu *trayIconMenu = new QMenu(this);
239     trayIconMenu->addAction(openBitcoinAction);
240     trayIconMenu->addAction(sendCoinsAction);
241     trayIconMenu->addAction(optionsAction);
242     trayIconMenu->addSeparator();
243     trayIconMenu->addAction(quitAction);
244
245     trayIcon = new QSystemTrayIcon(this);
246     trayIcon->setContextMenu(trayIconMenu);
247     trayIcon->setToolTip("Bitcoin client");
248     trayIcon->setIcon(QIcon(":/icons/toolbar"));
249     connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
250             this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
251     trayIcon->show();
252 }
253
254 void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
255 {
256     if(reason == QSystemTrayIcon::DoubleClick)
257     {
258         // Doubleclick on system tray icon triggers "open bitcoin"
259         openBitcoinAction->trigger();
260     }
261 }
262
263 void BitcoinGUI::optionsClicked()
264 {
265     OptionsDialog dlg;
266     dlg.setModel(clientModel->getOptionsModel());
267     dlg.exec();
268 }
269
270 void BitcoinGUI::aboutClicked()
271 {
272     AboutDialog dlg;
273     dlg.setModel(clientModel);
274     dlg.exec();
275 }
276
277 void BitcoinGUI::setNumConnections(int count)
278 {
279     QString icon;
280     switch(count)
281     {
282     case 0: icon = ":/icons/connect_0"; break;
283     case 1: case 2: case 3: icon = ":/icons/connect_1"; break;
284     case 4: case 5: case 6: icon = ":/icons/connect_2"; break;
285     case 7: case 8: case 9: icon = ":/icons/connect_3"; break;
286     default: icon = ":/icons/connect_4"; break;
287     }
288     labelConnections->setTextFormat(Qt::RichText);
289     labelConnections->setText("<img src=\""+icon+"\"> " + tr("%n connection(s)", "", count));
290 }
291
292 void BitcoinGUI::setNumBlocks(int count)
293 {
294     int total = clientModel->getTotalBlocksEstimate();
295     QString tooltip;
296
297     if(count < total)
298     {
299         progressBarLabel->setVisible(true);
300         progressBar->setVisible(true);
301         progressBar->setMaximum(total);
302         progressBar->setValue(count);
303         tooltip = tr("Downloaded %1 of %2 blocks of transaction history.").arg(count).arg(total);
304     }
305     else
306     {
307         progressBarLabel->setVisible(false);
308         progressBar->setVisible(false);
309         tooltip = tr("Downloaded %1 blocks of transaction history.").arg(count);
310     }
311
312     QDateTime now = QDateTime::currentDateTime();
313     QDateTime lastBlockDate = clientModel->getLastBlockDate();
314     int secs = lastBlockDate.secsTo(now);
315     QString text;
316     QString icon = ":/icons/notsynced";
317
318     // "Up to date" icon, and outdated icon
319     if(secs < 30*60)
320     {
321         text = "Up to date";
322         icon = ":/icons/synced";
323     }
324     else if(secs < 60*60)
325     {
326         text = tr("%n minute(s) ago","",secs/60);
327     }
328     else if(secs < 24*60*60)
329     {
330         text = tr("%n hour(s) ago","",secs/(60*60));
331     }
332     else
333     {
334         text = tr("%n day(s) ago","",secs/(60*60*24));
335     }
336     tooltip += QString("\n");
337     tooltip += tr("Last block was generated %1.").arg(QLocale::system().toString(lastBlockDate));
338
339     labelBlocks->setText("<img src=\""+icon+"\"> " + text);
340     labelBlocks->setToolTip(tooltip);
341     progressBarLabel->setToolTip(tooltip);
342     progressBar->setToolTip(tooltip);
343 }
344
345 void BitcoinGUI::error(const QString &title, const QString &message)
346 {
347     // Report errors from network/worker thread
348     if(trayIcon->supportsMessages())
349     {
350         // Show as "balloon" message if possible
351         trayIcon->showMessage(title, message, QSystemTrayIcon::Critical);
352     }
353     else
354     {
355         // Fall back to old fashioned popup dialog if not
356         QMessageBox::critical(this, title,
357             message,
358             QMessageBox::Ok, QMessageBox::Ok);
359     }
360 }
361
362 void BitcoinGUI::changeEvent(QEvent *e)
363 {
364     if (e->type() == QEvent::WindowStateChange)
365     {
366         if(clientModel->getOptionsModel()->getMinimizeToTray())
367         {
368             if (isMinimized())
369             {
370                 hide();
371                 e->ignore();
372             }
373             else
374             {
375                 e->accept();
376             }
377         }
378     }
379     QMainWindow::changeEvent(e);
380 }
381
382 void BitcoinGUI::closeEvent(QCloseEvent *event)
383 {
384     if(!clientModel->getOptionsModel()->getMinimizeToTray() &&
385        !clientModel->getOptionsModel()->getMinimizeOnClose())
386     {
387         qApp->quit();
388     }
389     QMainWindow::closeEvent(event);
390 }
391
392 void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
393 {
394     QString strMessage =
395         tr("This transaction is over the size limit.  You can still send it for a fee of %1, "
396           "which goes to the nodes that process your transaction and helps to support the network.  "
397           "Do you want to pay the fee?").arg(GUIUtil::formatMoney(nFeeRequired));
398     QMessageBox::StandardButton retval = QMessageBox::question(
399           this, tr("Sending..."), strMessage,
400           QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
401     *payFee = (retval == QMessageBox::Yes);
402 }
403
404 void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int end)
405 {
406     TransactionTableModel *ttm = walletModel->getTransactionTableModel();
407     qint64 amount = ttm->index(start, TransactionTableModel::Amount, parent)
408                     .data(Qt::EditRole).toULongLong();
409     if(!clientModel->inInitialBlockDownload())
410     {
411         // On incoming transaction, make an info balloon
412         // Unless the initial block download is in progress, to prevent balloon-spam
413         QString date = ttm->index(start, TransactionTableModel::Date, parent)
414                         .data().toString();
415         QString type = ttm->index(start, TransactionTableModel::Type, parent)
416                         .data().toString();
417         QString address = ttm->index(start, TransactionTableModel::ToAddress, parent)
418                         .data().toString();
419
420         trayIcon->showMessage((amount)<0 ? tr("Sent transaction") :
421                                            tr("Incoming transaction"),
422                               tr("Date: ") + date + "\n" +
423                               tr("Amount: ") + GUIUtil::formatMoney(amount, true) + "\n" +
424                               tr("Type: ") + type + "\n" +
425                               tr("Address: ") + address + "\n",
426                               QSystemTrayIcon::Information);
427     }
428 }
429
430 void BitcoinGUI::gotoOverviewPage()
431 {
432     overviewAction->setChecked(true);
433     centralWidget->setCurrentWidget(overviewPage);
434
435     exportAction->setEnabled(false);
436     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
437 }
438
439 void BitcoinGUI::gotoHistoryPage()
440 {
441     historyAction->setChecked(true);
442     centralWidget->setCurrentWidget(transactionsPage);
443
444     exportAction->setEnabled(true);
445     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
446     connect(exportAction, SIGNAL(triggered()), transactionView, SLOT(exportClicked()));
447 }
448
449 void BitcoinGUI::gotoAddressBookPage()
450 {
451     addressBookAction->setChecked(true);
452     centralWidget->setCurrentWidget(addressBookPage);
453
454     exportAction->setEnabled(true);
455     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
456     connect(exportAction, SIGNAL(triggered()), addressBookPage, SLOT(exportClicked()));
457 }
458
459 void BitcoinGUI::gotoReceiveCoinsPage()
460 {
461     receiveCoinsAction->setChecked(true);
462     centralWidget->setCurrentWidget(receiveCoinsPage);
463
464     exportAction->setEnabled(true);
465     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
466     connect(exportAction, SIGNAL(triggered()), receiveCoinsPage, SLOT(exportClicked()));
467 }
468
469 void BitcoinGUI::gotoSendCoinsPage()
470 {
471     sendCoinsAction->setChecked(true);
472     sendCoinsPage->clear();
473     centralWidget->setCurrentWidget(sendCoinsPage);
474
475     exportAction->setEnabled(false);
476     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
477 }
478