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