Bitcoin-Qt signmessage GUI (pull request #582)
[novacoin.git] / src / qt / bitcoingui.cpp
1 /*
2  * Qt4 bitcoin GUI.
3  *
4  * W.J. van der Laan 2011
5  * The Bitcoin Developers 2011
6  */
7 #include "bitcoingui.h"
8 #include "transactiontablemodel.h"
9 #include "addressbookpage.h"
10 #include "sendcoinsdialog.h"
11 #include "messagepage.h"
12 #include "optionsdialog.h"
13 #include "aboutdialog.h"
14 #include "clientmodel.h"
15 #include "walletmodel.h"
16 #include "editaddressdialog.h"
17 #include "optionsmodel.h"
18 #include "transactiondescdialog.h"
19 #include "addresstablemodel.h"
20 #include "transactionview.h"
21 #include "overviewpage.h"
22 #include "bitcoinunits.h"
23 #include "guiconstants.h"
24 #include "askpassphrasedialog.h"
25 #include "notificator.h"
26
27 #ifdef Q_WS_MAC
28 #include "macdockiconhandler.h"
29 #endif
30
31 #include <QApplication>
32 #include <QMainWindow>
33 #include <QMenuBar>
34 #include <QMenu>
35 #include <QIcon>
36 #include <QTabWidget>
37 #include <QVBoxLayout>
38 #include <QToolBar>
39 #include <QStatusBar>
40 #include <QLabel>
41 #include <QLineEdit>
42 #include <QPushButton>
43 #include <QLocale>
44 #include <QMessageBox>
45 #include <QProgressBar>
46 #include <QStackedWidget>
47 #include <QDateTime>
48 #include <QMovie>
49
50 #include <QDragEnterEvent>
51 #include <QUrl>
52
53 #include <iostream>
54
55 BitcoinGUI::BitcoinGUI(QWidget *parent):
56     QMainWindow(parent),
57     clientModel(0),
58     walletModel(0),
59     encryptWalletAction(0),
60     changePassphraseAction(0),
61     aboutQtAction(0),
62     trayIcon(0),
63     notificator(0)
64 {
65     resize(850, 550);
66     setWindowTitle(tr("Bitcoin Wallet"));
67 #ifndef Q_WS_MAC
68     setWindowIcon(QIcon(":icons/bitcoin"));
69 #else
70     setUnifiedTitleAndToolBarOnMac(true);
71     QApplication::setAttribute(Qt::AA_DontShowIconsInMenus);
72 #endif
73     // Accept D&D of URIs
74     setAcceptDrops(true);
75
76     // Create actions for the toolbar, menu bar and tray/dock icon
77     createActions();
78
79     // Create application menu bar
80     createMenuBar();
81
82     // Create the toolbars
83     createToolBars();
84
85     // Create the tray icon (or setup the dock icon)
86     createTrayIcon();
87
88     // Create tabs
89     overviewPage = new OverviewPage();
90
91     transactionsPage = new QWidget(this);
92     QVBoxLayout *vbox = new QVBoxLayout();
93     transactionView = new TransactionView(this);
94     vbox->addWidget(transactionView);
95     transactionsPage->setLayout(vbox);
96
97     addressBookPage = new AddressBookPage(AddressBookPage::ForEditing, AddressBookPage::SendingTab);
98
99     receiveCoinsPage = new AddressBookPage(AddressBookPage::ForEditing, AddressBookPage::ReceivingTab);
100
101     sendCoinsPage = new SendCoinsDialog(this);
102
103     messagePage = new MessagePage(this);
104
105     centralWidget = new QStackedWidget(this);
106     centralWidget->addWidget(overviewPage);
107     centralWidget->addWidget(transactionsPage);
108     centralWidget->addWidget(addressBookPage);
109     centralWidget->addWidget(receiveCoinsPage);
110     centralWidget->addWidget(sendCoinsPage);
111 #ifdef FIRST_CLASS_MESSAGING
112     centralWidget->addWidget(messagePage);
113 #endif
114     setCentralWidget(centralWidget);
115
116     // Create status bar
117     statusBar();
118
119     // Status bar notification icons
120     QFrame *frameBlocks = new QFrame();
121     frameBlocks->setContentsMargins(0,0,0,0);
122     frameBlocks->setMinimumWidth(56);
123     frameBlocks->setMaximumWidth(56);
124     QHBoxLayout *frameBlocksLayout = new QHBoxLayout(frameBlocks);
125     frameBlocksLayout->setContentsMargins(3,0,3,0);
126     frameBlocksLayout->setSpacing(3);
127     labelEncryptionIcon = new QLabel();
128     labelConnectionsIcon = new QLabel();
129     labelBlocksIcon = new QLabel();
130     frameBlocksLayout->addStretch();
131     frameBlocksLayout->addWidget(labelEncryptionIcon);
132     frameBlocksLayout->addStretch();
133     frameBlocksLayout->addWidget(labelConnectionsIcon);
134     frameBlocksLayout->addStretch();
135     frameBlocksLayout->addWidget(labelBlocksIcon);
136     frameBlocksLayout->addStretch();
137
138     // Progress bar for blocks download
139     progressBarLabel = new QLabel(tr("Synchronizing with network..."));
140     progressBarLabel->setVisible(false);
141     progressBar = new QProgressBar();
142     progressBar->setToolTip(tr("Block chain synchronization in progress"));
143     progressBar->setVisible(false);
144
145     statusBar()->addWidget(progressBarLabel);
146     statusBar()->addWidget(progressBar);
147     statusBar()->addPermanentWidget(frameBlocks);
148
149     syncIconMovie = new QMovie(":/movies/update_spinner", "mng", this);
150
151     // Clicking on a transaction on the overview page simply sends you to transaction history page
152     connect(overviewPage, SIGNAL(transactionClicked(QModelIndex)), this, SLOT(gotoHistoryPage()));
153
154     // Doubleclicking on a transaction on the transaction history page shows details
155     connect(transactionView, SIGNAL(doubleClicked(QModelIndex)), transactionView, SLOT(showDetails()));
156
157     gotoOverviewPage();
158 }
159
160 BitcoinGUI::~BitcoinGUI()
161 {
162 #ifdef Q_WS_MAC
163     delete appMenuBar;
164 #endif
165 }
166
167 void BitcoinGUI::createActions()
168 {
169     QActionGroup *tabGroup = new QActionGroup(this);
170
171     overviewAction = new QAction(QIcon(":/icons/overview"), tr("&Overview"), this);
172     overviewAction->setToolTip(tr("Show general overview of wallet"));
173     overviewAction->setCheckable(true);
174     overviewAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_1));
175     tabGroup->addAction(overviewAction);
176
177     historyAction = new QAction(QIcon(":/icons/history"), tr("&Transactions"), this);
178     historyAction->setToolTip(tr("Browse transaction history"));
179     historyAction->setCheckable(true);
180     historyAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_4));
181     tabGroup->addAction(historyAction);
182
183     addressBookAction = new QAction(QIcon(":/icons/address-book"), tr("&Address Book"), this);
184     addressBookAction->setToolTip(tr("Edit the list of stored addresses and labels"));
185     addressBookAction->setCheckable(true);
186     addressBookAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_5));
187     tabGroup->addAction(addressBookAction);
188
189     receiveCoinsAction = new QAction(QIcon(":/icons/receiving_addresses"), tr("&Receive coins"), this);
190     receiveCoinsAction->setToolTip(tr("Show the list of addresses for receiving payments"));
191     receiveCoinsAction->setCheckable(true);
192     receiveCoinsAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_3));
193     tabGroup->addAction(receiveCoinsAction);
194
195     sendCoinsAction = new QAction(QIcon(":/icons/send"), tr("&Send coins"), this);
196     sendCoinsAction->setToolTip(tr("Send coins to a bitcoin address"));
197     sendCoinsAction->setCheckable(true);
198     sendCoinsAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_2));
199     tabGroup->addAction(sendCoinsAction);
200
201     messageAction = new QAction(QIcon(":/icons/edit"), tr("Sign &message"), this);
202     messageAction->setToolTip(tr("Prove you control an address"));
203 #ifdef FIRST_CLASS_MESSAGING
204     messageAction->setCheckable(true);
205 #endif
206     tabGroup->addAction(messageAction);
207
208     connect(overviewAction, SIGNAL(triggered()), this, SLOT(show()));
209     connect(overviewAction, SIGNAL(triggered()), this, SLOT(gotoOverviewPage()));
210     connect(historyAction, SIGNAL(triggered()), this, SLOT(show()));
211     connect(historyAction, SIGNAL(triggered()), this, SLOT(gotoHistoryPage()));
212     connect(addressBookAction, SIGNAL(triggered()), this, SLOT(show()));
213     connect(addressBookAction, SIGNAL(triggered()), this, SLOT(gotoAddressBookPage()));
214     connect(receiveCoinsAction, SIGNAL(triggered()), this, SLOT(show()));
215     connect(receiveCoinsAction, SIGNAL(triggered()), this, SLOT(gotoReceiveCoinsPage()));
216     connect(sendCoinsAction, SIGNAL(triggered()), this, SLOT(show()));
217     connect(sendCoinsAction, SIGNAL(triggered()), this, SLOT(gotoSendCoinsPage()));
218     connect(messageAction, SIGNAL(triggered()), this, SLOT(show()));
219     connect(messageAction, SIGNAL(triggered()), this, SLOT(gotoMessagePage()));
220
221     quitAction = new QAction(QIcon(":/icons/quit"), tr("E&xit"), this);
222     quitAction->setToolTip(tr("Quit application"));
223     quitAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
224     quitAction->setMenuRole(QAction::QuitRole);
225     aboutAction = new QAction(QIcon(":/icons/bitcoin"), tr("&About %1").arg(qApp->applicationName()), this);
226     aboutAction->setToolTip(tr("Show information about Bitcoin"));
227     aboutAction->setMenuRole(QAction::AboutRole);
228     aboutQtAction = new QAction(tr("About &Qt"), this);
229     aboutQtAction->setToolTip(tr("Show information about Qt"));
230     aboutQtAction->setMenuRole(QAction::AboutQtRole);
231     optionsAction = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
232     optionsAction->setToolTip(tr("Modify configuration options for bitcoin"));
233     optionsAction->setMenuRole(QAction::PreferencesRole);
234     openBitcoinAction = new QAction(QIcon(":/icons/bitcoin"), tr("Open &Bitcoin"), this);
235     openBitcoinAction->setToolTip(tr("Show the Bitcoin window"));
236     exportAction = new QAction(QIcon(":/icons/export"), tr("&Export..."), this);
237     exportAction->setToolTip(tr("Export the current view to a file"));
238     encryptWalletAction = new QAction(QIcon(":/icons/lock_closed"), tr("&Encrypt Wallet"), this);
239     encryptWalletAction->setToolTip(tr("Encrypt or decrypt wallet"));
240     encryptWalletAction->setCheckable(true);
241     changePassphraseAction = new QAction(QIcon(":/icons/key"), tr("&Change Passphrase"), this);
242     changePassphraseAction->setToolTip(tr("Change the passphrase used for wallet encryption"));
243
244     connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
245     connect(optionsAction, SIGNAL(triggered()), this, SLOT(optionsClicked()));
246     connect(aboutAction, SIGNAL(triggered()), this, SLOT(aboutClicked()));
247     connect(aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
248     connect(openBitcoinAction, SIGNAL(triggered()), this, SLOT(showNormal()));
249     connect(encryptWalletAction, SIGNAL(triggered(bool)), this, SLOT(encryptWallet(bool)));
250     connect(changePassphraseAction, SIGNAL(triggered()), this, SLOT(changePassphrase()));
251 }
252
253 void BitcoinGUI::createMenuBar()
254 {
255 #ifdef Q_WS_MAC
256     // Create a decoupled menu bar on Mac which stays even if the window is closed
257     appMenuBar = new QMenuBar();
258 #else
259     // Get the main window's menu bar on other platforms
260     appMenuBar = menuBar();
261 #endif
262
263     // Configure the menus
264     QMenu *file = appMenuBar->addMenu(tr("&File"));
265 #ifndef FIRST_CLASS_MESSAGING
266     file->addAction(messageAction);
267     file->addSeparator();
268 #endif
269     file->addAction(quitAction);
270
271     QMenu *settings = appMenuBar->addMenu(tr("&Settings"));
272     settings->addAction(encryptWalletAction);
273     settings->addAction(changePassphraseAction);
274     settings->addSeparator();
275     settings->addAction(optionsAction);
276
277     QMenu *help = appMenuBar->addMenu(tr("&Help"));
278     help->addAction(aboutAction);
279     help->addAction(aboutQtAction);
280 }
281
282 void BitcoinGUI::createToolBars()
283 {
284     QToolBar *toolbar = addToolBar(tr("Tabs toolbar"));
285     toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
286     toolbar->addAction(overviewAction);
287     toolbar->addAction(sendCoinsAction);
288     toolbar->addAction(receiveCoinsAction);
289     toolbar->addAction(historyAction);
290     toolbar->addAction(addressBookAction);
291 #ifdef FIRST_CLASS_MESSAGING
292     toolbar->addAction(messageAction);
293 #endif
294
295     QToolBar *toolbar2 = addToolBar(tr("Actions toolbar"));
296     toolbar2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
297     toolbar2->addAction(exportAction);
298 }
299
300 void BitcoinGUI::setClientModel(ClientModel *clientModel)
301 {
302     this->clientModel = clientModel;
303     if(clientModel)
304     {
305         if(clientModel->isTestNet())
306         {
307             QString title_testnet = windowTitle() + QString(" ") + tr("[testnet]");
308             setWindowTitle(title_testnet);
309 #ifndef Q_WS_MAC
310             setWindowIcon(QIcon(":icons/bitcoin_testnet"));
311 #else
312             MacDockIconHandler::instance()->setIcon(QIcon(":icons/bitcoin_testnet"));
313 #endif
314             if(trayIcon)
315             {
316                 trayIcon->setToolTip(title_testnet);
317                 trayIcon->setIcon(QIcon(":/icons/toolbar_testnet"));
318             }
319         }
320
321         // Keep up to date with client
322         setNumConnections(clientModel->getNumConnections());
323         connect(clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
324
325         setNumBlocks(clientModel->getNumBlocks());
326         connect(clientModel, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
327
328         // Report errors from network/worker thread
329         connect(clientModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
330     }
331 }
332
333 void BitcoinGUI::setWalletModel(WalletModel *walletModel)
334 {
335     this->walletModel = walletModel;
336     if(walletModel)
337     {
338         // Report errors from wallet thread
339         connect(walletModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
340
341         // Put transaction list in tabs
342         transactionView->setModel(walletModel);
343
344         overviewPage->setModel(walletModel);
345         addressBookPage->setModel(walletModel->getAddressTableModel());
346         receiveCoinsPage->setModel(walletModel->getAddressTableModel());
347         sendCoinsPage->setModel(walletModel);
348         messagePage->setModel(walletModel);
349
350         setEncryptionStatus(walletModel->getEncryptionStatus());
351         connect(walletModel, SIGNAL(encryptionStatusChanged(int)), this, SLOT(setEncryptionStatus(int)));
352
353         // Balloon popup for new transaction
354         connect(walletModel->getTransactionTableModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
355                 this, SLOT(incomingTransaction(QModelIndex,int,int)));
356
357         // Ask for passphrase if needed
358         connect(walletModel, SIGNAL(requireUnlock()), this, SLOT(unlockWallet()));
359     }
360 }
361
362 void BitcoinGUI::createTrayIcon()
363 {
364     QMenu *trayIconMenu;
365 #ifndef Q_WS_MAC
366     trayIcon = new QSystemTrayIcon(this);
367     trayIconMenu = new QMenu(this);
368     trayIcon->setContextMenu(trayIconMenu);
369     trayIcon->setToolTip("Bitcoin client");
370     trayIcon->setIcon(QIcon(":/icons/toolbar"));
371     connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
372             this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
373     trayIcon->show();
374 #else
375     // Note: On Mac, the dock icon is used to provide the tray's functionality.
376     MacDockIconHandler *dockIconHandler = MacDockIconHandler::instance();
377     connect(dockIconHandler, SIGNAL(dockIconClicked()), openBitcoinAction, SLOT(trigger()));
378     trayIconMenu = dockIconHandler->dockMenu();
379 #endif
380
381     // Configuration of the tray icon (or dock icon) icon menu
382     trayIconMenu->addAction(openBitcoinAction);
383     trayIconMenu->addSeparator();
384     trayIconMenu->addAction(messageAction);
385 #ifndef FIRST_CLASS_MESSAGING
386     trayIconMenu->addSeparator();
387 #endif
388     trayIconMenu->addAction(receiveCoinsAction);
389     trayIconMenu->addAction(sendCoinsAction);
390     trayIconMenu->addSeparator();
391     trayIconMenu->addAction(optionsAction);
392 #ifndef Q_WS_MAC // This is built-in on Mac
393     trayIconMenu->addSeparator();
394     trayIconMenu->addAction(quitAction);
395 #endif
396
397     notificator = new Notificator(tr("bitcoin-qt"), trayIcon);
398 }
399
400 #ifndef Q_WS_MAC
401 void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
402 {
403     if(reason == QSystemTrayIcon::Trigger)
404     {
405         // Click on system tray icon triggers "open bitcoin"
406         openBitcoinAction->trigger();
407     }
408
409 }
410 #endif
411
412 void BitcoinGUI::optionsClicked()
413 {
414     if(!clientModel || !clientModel->getOptionsModel())
415         return;
416     OptionsDialog dlg;
417     dlg.setModel(clientModel->getOptionsModel());
418     dlg.exec();
419 }
420
421 void BitcoinGUI::aboutClicked()
422 {
423     AboutDialog dlg;
424     dlg.setModel(clientModel);
425     dlg.exec();
426 }
427
428 void BitcoinGUI::setNumConnections(int count)
429 {
430     QString icon;
431     switch(count)
432     {
433     case 0: icon = ":/icons/connect_0"; break;
434     case 1: case 2: case 3: icon = ":/icons/connect_1"; break;
435     case 4: case 5: case 6: icon = ":/icons/connect_2"; break;
436     case 7: case 8: case 9: icon = ":/icons/connect_3"; break;
437     default: icon = ":/icons/connect_4"; break;
438     }
439     labelConnectionsIcon->setPixmap(QIcon(icon).pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
440     labelConnectionsIcon->setToolTip(tr("%n active connection(s) to Bitcoin network", "", count));
441 }
442
443 void BitcoinGUI::setNumBlocks(int count)
444 {
445     if(!clientModel)
446         return;
447     int total = clientModel->getNumBlocksOfPeers();
448     QString tooltip;
449
450     if(count < total)
451     {
452         if (clientModel->getStatusBarWarnings() == "")
453         {
454             progressBarLabel->setVisible(true);
455             progressBarLabel->setText(tr("Synchronizing with network..."));
456             progressBar->setVisible(true);
457             progressBar->setMaximum(total);
458             progressBar->setValue(count);
459         }
460         else
461         {
462             progressBarLabel->setText(clientModel->getStatusBarWarnings());
463             progressBarLabel->setVisible(true);
464             progressBar->setVisible(false);
465         }
466         tooltip = tr("Downloaded %1 of %2 blocks of transaction history.").arg(count).arg(total);
467     }
468     else
469     {
470         if (clientModel->getStatusBarWarnings() == "")
471             progressBarLabel->setVisible(false);
472         else
473         {
474             progressBarLabel->setText(clientModel->getStatusBarWarnings());
475             progressBarLabel->setVisible(true);
476         }
477         progressBar->setVisible(false);
478         tooltip = tr("Downloaded %1 blocks of transaction history.").arg(count);
479     }
480
481     QDateTime now = QDateTime::currentDateTime();
482     QDateTime lastBlockDate = clientModel->getLastBlockDate();
483     int secs = lastBlockDate.secsTo(now);
484     QString text;
485
486     // Represent time from last generated block in human readable text
487     if(secs < 60)
488     {
489         text = tr("%n second(s) ago","",secs);
490     }
491     else if(secs < 60*60)
492     {
493         text = tr("%n minute(s) ago","",secs/60);
494     }
495     else if(secs < 24*60*60)
496     {
497         text = tr("%n hour(s) ago","",secs/(60*60));
498     }
499     else
500     {
501         text = tr("%n day(s) ago","",secs/(60*60*24));
502     }
503
504     // Set icon state: spinning if catching up, tick otherwise
505     if(secs < 30*60)
506     {
507         tooltip = tr("Up to date") + QString("\n") + tooltip;
508         labelBlocksIcon->setPixmap(QIcon(":/icons/synced").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
509     }
510     else
511     {
512         tooltip = tr("Catching up...") + QString("\n") + tooltip;
513         labelBlocksIcon->setMovie(syncIconMovie);
514         syncIconMovie->start();
515     }
516
517     tooltip += QString("\n");
518     tooltip += tr("Last received block was generated %1.").arg(text);
519
520     labelBlocksIcon->setToolTip(tooltip);
521     progressBarLabel->setToolTip(tooltip);
522     progressBar->setToolTip(tooltip);
523 }
524
525 void BitcoinGUI::refreshStatusBar()
526 {
527     /* Might display multiple times in the case of multiple alerts
528     static QString prevStatusBar;
529     QString newStatusBar = clientModel->getStatusBarWarnings();
530     if (prevStatusBar != newStatusBar)
531     {
532         prevStatusBar = newStatusBar;
533         error(tr("Network Alert"), newStatusBar);
534     }*/
535     setNumBlocks(clientModel->getNumBlocks());
536 }
537
538 void BitcoinGUI::error(const QString &title, const QString &message)
539 {
540     // Report errors from network/worker thread
541     notificator->notify(Notificator::Critical, title, message);
542 }
543
544 void BitcoinGUI::changeEvent(QEvent *e)
545 {
546 #ifndef Q_WS_MAC // Ignored on Mac
547     if(e->type() == QEvent::WindowStateChange)
548     {
549         if(clientModel && clientModel->getOptionsModel()->getMinimizeToTray())
550         {
551             if(isMinimized())
552             {
553                 hide();
554                 e->ignore();
555             }
556             else
557             {
558                 show();
559                 e->accept();
560             }
561         }
562     }
563 #endif
564     QMainWindow::changeEvent(e);
565 }
566
567 void BitcoinGUI::closeEvent(QCloseEvent *event)
568 {
569     if(clientModel)
570     {
571 #ifndef Q_WS_MAC // Ignored on Mac
572         if(!clientModel->getOptionsModel()->getMinimizeToTray() &&
573            !clientModel->getOptionsModel()->getMinimizeOnClose())
574         {
575             qApp->quit();
576         }
577 #endif
578     }
579     QMainWindow::closeEvent(event);
580 }
581
582 void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
583 {
584     QString strMessage =
585         tr("This transaction is over the size limit.  You can still send it for a fee of %1, "
586           "which goes to the nodes that process your transaction and helps to support the network.  "
587           "Do you want to pay the fee?").arg(
588                 BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, nFeeRequired));
589     QMessageBox::StandardButton retval = QMessageBox::question(
590           this, tr("Sending..."), strMessage,
591           QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
592     *payFee = (retval == QMessageBox::Yes);
593 }
594
595 void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int end)
596 {
597     if(!walletModel || !clientModel)
598         return;
599     TransactionTableModel *ttm = walletModel->getTransactionTableModel();
600     qint64 amount = ttm->index(start, TransactionTableModel::Amount, parent)
601                     .data(Qt::EditRole).toULongLong();
602     if(!clientModel->inInitialBlockDownload())
603     {
604         // On new transaction, make an info balloon
605         // Unless the initial block download is in progress, to prevent balloon-spam
606         QString date = ttm->index(start, TransactionTableModel::Date, parent)
607                         .data().toString();
608         QString type = ttm->index(start, TransactionTableModel::Type, parent)
609                         .data().toString();
610         QString address = ttm->index(start, TransactionTableModel::ToAddress, parent)
611                         .data().toString();
612         QIcon icon = qvariant_cast<QIcon>(ttm->index(start,
613                             TransactionTableModel::ToAddress, parent)
614                         .data(Qt::DecorationRole));
615
616         notificator->notify(Notificator::Information,
617                             (amount)<0 ? tr("Sent transaction") :
618                                          tr("Incoming transaction"),
619                               tr("Date: %1\n"
620                                  "Amount: %2\n"
621                                  "Type: %3\n"
622                                  "Address: %4\n")
623                               .arg(date)
624                               .arg(BitcoinUnits::formatWithUnit(walletModel->getOptionsModel()->getDisplayUnit(), amount, true))
625                               .arg(type)
626                               .arg(address), icon);
627     }
628 }
629
630 void BitcoinGUI::gotoOverviewPage()
631 {
632     overviewAction->setChecked(true);
633     centralWidget->setCurrentWidget(overviewPage);
634
635     exportAction->setEnabled(false);
636     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
637 }
638
639 void BitcoinGUI::gotoHistoryPage()
640 {
641     historyAction->setChecked(true);
642     centralWidget->setCurrentWidget(transactionsPage);
643
644     exportAction->setEnabled(true);
645     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
646     connect(exportAction, SIGNAL(triggered()), transactionView, SLOT(exportClicked()));
647 }
648
649 void BitcoinGUI::gotoAddressBookPage()
650 {
651     addressBookAction->setChecked(true);
652     centralWidget->setCurrentWidget(addressBookPage);
653
654     exportAction->setEnabled(true);
655     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
656     connect(exportAction, SIGNAL(triggered()), addressBookPage, SLOT(exportClicked()));
657 }
658
659 void BitcoinGUI::gotoReceiveCoinsPage()
660 {
661     receiveCoinsAction->setChecked(true);
662     centralWidget->setCurrentWidget(receiveCoinsPage);
663
664     exportAction->setEnabled(true);
665     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
666     connect(exportAction, SIGNAL(triggered()), receiveCoinsPage, SLOT(exportClicked()));
667 }
668
669 void BitcoinGUI::gotoSendCoinsPage()
670 {
671     sendCoinsAction->setChecked(true);
672     centralWidget->setCurrentWidget(sendCoinsPage);
673
674     exportAction->setEnabled(false);
675     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
676 }
677
678 void BitcoinGUI::gotoMessagePage()
679 {
680 #ifdef FIRST_CLASS_MESSAGING
681     messageAction->setChecked(true);
682     centralWidget->setCurrentWidget(messagePage);
683
684     exportAction->setEnabled(false);
685     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
686 #else
687     messagePage->show();
688     messagePage->setFocus();
689 #endif
690 }
691
692 void BitcoinGUI::gotoMessagePage(QString addr)
693 {
694     gotoMessagePage();
695     messagePage->setAddress(addr);
696 }
697
698 void BitcoinGUI::dragEnterEvent(QDragEnterEvent *event)
699 {
700     // Accept only URLs
701     if(event->mimeData()->hasUrls())
702         event->acceptProposedAction();
703 }
704
705 void BitcoinGUI::dropEvent(QDropEvent *event)
706 {
707     if(event->mimeData()->hasUrls())
708     {
709         gotoSendCoinsPage();
710         QList<QUrl> urls = event->mimeData()->urls();
711         foreach(const QUrl &url, urls)
712         {
713             sendCoinsPage->handleURL(&url);
714         }
715     }
716
717     event->acceptProposedAction();
718 }
719
720 void BitcoinGUI::handleURL(QString strURL)
721 {
722     gotoSendCoinsPage();
723     QUrl url = QUrl(strURL);
724     sendCoinsPage->handleURL(&url);
725 }
726
727 void BitcoinGUI::setEncryptionStatus(int status)
728 {
729     switch(status)
730     {
731     case WalletModel::Unencrypted:
732         labelEncryptionIcon->hide();
733         encryptWalletAction->setChecked(false);
734         changePassphraseAction->setEnabled(false);
735         encryptWalletAction->setEnabled(true);
736         break;
737     case WalletModel::Unlocked:
738         labelEncryptionIcon->show();
739         labelEncryptionIcon->setPixmap(QIcon(":/icons/lock_open").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
740         labelEncryptionIcon->setToolTip(tr("Wallet is <b>encrypted</b> and currently <b>unlocked</b>"));
741         encryptWalletAction->setChecked(true);
742         changePassphraseAction->setEnabled(true);
743         encryptWalletAction->setEnabled(false); // TODO: decrypt currently not supported
744         break;
745     case WalletModel::Locked:
746         labelEncryptionIcon->show();
747         labelEncryptionIcon->setPixmap(QIcon(":/icons/lock_closed").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
748         labelEncryptionIcon->setToolTip(tr("Wallet is <b>encrypted</b> and currently <b>locked</b>"));
749         encryptWalletAction->setChecked(true);
750         changePassphraseAction->setEnabled(true);
751         encryptWalletAction->setEnabled(false); // TODO: decrypt currently not supported
752         break;
753     }
754 }
755
756 void BitcoinGUI::encryptWallet(bool status)
757 {
758     if(!walletModel)
759         return;
760     AskPassphraseDialog dlg(status ? AskPassphraseDialog::Encrypt:
761                                      AskPassphraseDialog::Decrypt, this);
762     dlg.setModel(walletModel);
763     dlg.exec();
764
765     setEncryptionStatus(walletModel->getEncryptionStatus());
766 }
767
768 void BitcoinGUI::changePassphrase()
769 {
770     AskPassphraseDialog dlg(AskPassphraseDialog::ChangePass, this);
771     dlg.setModel(walletModel);
772     dlg.exec();
773 }
774
775 void BitcoinGUI::unlockWallet()
776 {
777     if(!walletModel)
778         return;
779     // Unlock wallet when requested by wallet model
780     if(walletModel->getEncryptionStatus() == WalletModel::Locked)
781     {
782         AskPassphraseDialog dlg(AskPassphraseDialog::Unlock, this);
783         dlg.setModel(walletModel);
784         dlg.exec();
785     }
786 }