Hide window from taskbar when "minimize to tray" active by making window into Tool...
[novacoin.git] / src / qt / bitcoingui.cpp
1 /*
2  * Qt4 bitcoin GUI.
3  *
4  * W.J. van der Laan 20011-2012
5  * The Bitcoin Developers 20011-2012
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(showNormal()));
209     connect(overviewAction, SIGNAL(triggered()), this, SLOT(gotoOverviewPage()));
210     connect(historyAction, SIGNAL(triggered()), this, SLOT(showNormal()));
211     connect(historyAction, SIGNAL(triggered()), this, SLOT(gotoHistoryPage()));
212     connect(addressBookAction, SIGNAL(triggered()), this, SLOT(showNormal()));
213     connect(addressBookAction, SIGNAL(triggered()), this, SLOT(gotoAddressBookPage()));
214     connect(receiveCoinsAction, SIGNAL(triggered()), this, SLOT(showNormal()));
215     connect(receiveCoinsAction, SIGNAL(triggered()), this, SLOT(gotoReceiveCoinsPage()));
216     connect(sendCoinsAction, SIGNAL(triggered()), this, SLOT(showNormal()));
217     connect(sendCoinsAction, SIGNAL(triggered()), this, SLOT(gotoSendCoinsPage()));
218     connect(messageAction, SIGNAL(triggered()), this, SLOT(showNormal()));
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 data in the current tab 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     file->addAction(exportAction);
266 #ifndef FIRST_CLASS_MESSAGING
267     file->addAction(messageAction);
268     file->addSeparator();
269 #endif
270     file->addAction(quitAction);
271
272     QMenu *settings = appMenuBar->addMenu(tr("&Settings"));
273     settings->addAction(encryptWalletAction);
274     settings->addAction(changePassphraseAction);
275     settings->addSeparator();
276     settings->addAction(optionsAction);
277
278     QMenu *help = appMenuBar->addMenu(tr("&Help"));
279     help->addAction(aboutAction);
280     help->addAction(aboutQtAction);
281 }
282
283 void BitcoinGUI::createToolBars()
284 {
285     QToolBar *toolbar = addToolBar(tr("Tabs toolbar"));
286     toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
287     toolbar->addAction(overviewAction);
288     toolbar->addAction(sendCoinsAction);
289     toolbar->addAction(receiveCoinsAction);
290     toolbar->addAction(historyAction);
291     toolbar->addAction(addressBookAction);
292 #ifdef FIRST_CLASS_MESSAGING
293     toolbar->addAction(messageAction);
294 #endif
295
296     QToolBar *toolbar2 = addToolBar(tr("Actions toolbar"));
297     toolbar2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
298     toolbar2->addAction(exportAction);
299 }
300
301 void BitcoinGUI::setClientModel(ClientModel *clientModel)
302 {
303     this->clientModel = clientModel;
304     if(clientModel)
305     {
306         if(clientModel->isTestNet())
307         {
308             QString title_testnet = windowTitle() + QString(" ") + tr("[testnet]");
309             setWindowTitle(title_testnet);
310 #ifndef Q_WS_MAC
311             setWindowIcon(QIcon(":icons/bitcoin_testnet"));
312 #else
313             MacDockIconHandler::instance()->setIcon(QIcon(":icons/bitcoin_testnet"));
314 #endif
315             if(trayIcon)
316             {
317                 trayIcon->setToolTip(title_testnet);
318                 trayIcon->setIcon(QIcon(":/icons/toolbar_testnet"));
319             }
320         }
321
322         // Keep up to date with client
323         setNumConnections(clientModel->getNumConnections());
324         connect(clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
325
326         setNumBlocks(clientModel->getNumBlocks());
327         connect(clientModel, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
328
329         // Report errors from network/worker thread
330         connect(clientModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
331     }
332 }
333
334 void BitcoinGUI::setWalletModel(WalletModel *walletModel)
335 {
336     this->walletModel = walletModel;
337     if(walletModel)
338     {
339         // Report errors from wallet thread
340         connect(walletModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
341
342         // Put transaction list in tabs
343         transactionView->setModel(walletModel);
344
345         overviewPage->setModel(walletModel);
346         addressBookPage->setModel(walletModel->getAddressTableModel());
347         receiveCoinsPage->setModel(walletModel->getAddressTableModel());
348         sendCoinsPage->setModel(walletModel);
349         messagePage->setModel(walletModel);
350
351         setEncryptionStatus(walletModel->getEncryptionStatus());
352         connect(walletModel, SIGNAL(encryptionStatusChanged(int)), this, SLOT(setEncryptionStatus(int)));
353
354         // Balloon popup for new transaction
355         connect(walletModel->getTransactionTableModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
356                 this, SLOT(incomingTransaction(QModelIndex,int,int)));
357
358         // Ask for passphrase if needed
359         connect(walletModel, SIGNAL(requireUnlock()), this, SLOT(unlockWallet()));
360     }
361 }
362
363 void BitcoinGUI::createTrayIcon()
364 {
365     QMenu *trayIconMenu;
366 #ifndef Q_WS_MAC
367     trayIcon = new QSystemTrayIcon(this);
368     trayIconMenu = new QMenu(this);
369     trayIcon->setContextMenu(trayIconMenu);
370     trayIcon->setToolTip("Bitcoin client");
371     trayIcon->setIcon(QIcon(":/icons/toolbar"));
372     connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
373             this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
374     trayIcon->show();
375 #else
376     // Note: On Mac, the dock icon is used to provide the tray's functionality.
377     MacDockIconHandler *dockIconHandler = MacDockIconHandler::instance();
378     connect(dockIconHandler, SIGNAL(dockIconClicked()), openBitcoinAction, SLOT(trigger()));
379     trayIconMenu = dockIconHandler->dockMenu();
380 #endif
381
382     // Configuration of the tray icon (or dock icon) icon menu
383     trayIconMenu->addAction(openBitcoinAction);
384     trayIconMenu->addSeparator();
385     trayIconMenu->addAction(messageAction);
386 #ifndef FIRST_CLASS_MESSAGING
387     trayIconMenu->addSeparator();
388 #endif
389     trayIconMenu->addAction(receiveCoinsAction);
390     trayIconMenu->addAction(sendCoinsAction);
391     trayIconMenu->addSeparator();
392     trayIconMenu->addAction(optionsAction);
393 #ifndef Q_WS_MAC // This is built-in on Mac
394     trayIconMenu->addSeparator();
395     trayIconMenu->addAction(quitAction);
396 #endif
397
398     notificator = new Notificator(tr("bitcoin-qt"), trayIcon);
399 }
400
401 #ifndef Q_WS_MAC
402 void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
403 {
404     if(reason == QSystemTrayIcon::Trigger)
405     {
406         // Click on system tray icon triggers "open bitcoin"
407         openBitcoinAction->trigger();
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             QWindowStateChangeEvent *wsevt = static_cast<QWindowStateChangeEvent*>(e);
552             bool wasMinimized = wsevt->oldState() & Qt::WindowMinimized;
553             bool isMinimized = windowState() & Qt::WindowMinimized;
554             if(!wasMinimized && isMinimized)
555             {
556                 // Minimized, hide the window from taskbar
557                 setWindowFlags(windowFlags() | Qt::Tool);
558                 return;
559             }
560             else if(wasMinimized && !isMinimized)
561             {
562                 // Unminimized, show the window in taskbar
563                 setWindowFlags(windowFlags() &~ Qt::Tool);
564             }
565         }
566     }
567 #endif
568     QMainWindow::changeEvent(e);
569 }
570
571 void BitcoinGUI::closeEvent(QCloseEvent *event)
572 {
573     if(clientModel)
574     {
575 #ifndef Q_WS_MAC // Ignored on Mac
576         if(!clientModel->getOptionsModel()->getMinimizeToTray() &&
577            !clientModel->getOptionsModel()->getMinimizeOnClose())
578         {
579             qApp->quit();
580         }
581 #endif
582     }
583     QMainWindow::closeEvent(event);
584 }
585
586 void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
587 {
588     QString strMessage =
589         tr("This transaction is over the size limit.  You can still send it for a fee of %1, "
590           "which goes to the nodes that process your transaction and helps to support the network.  "
591           "Do you want to pay the fee?").arg(
592                 BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, nFeeRequired));
593     QMessageBox::StandardButton retval = QMessageBox::question(
594           this, tr("Sending..."), strMessage,
595           QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
596     *payFee = (retval == QMessageBox::Yes);
597 }
598
599 void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int end)
600 {
601     if(!walletModel || !clientModel)
602         return;
603     TransactionTableModel *ttm = walletModel->getTransactionTableModel();
604     qint64 amount = ttm->index(start, TransactionTableModel::Amount, parent)
605                     .data(Qt::EditRole).toULongLong();
606     if(!clientModel->inInitialBlockDownload())
607     {
608         // On new transaction, make an info balloon
609         // Unless the initial block download is in progress, to prevent balloon-spam
610         QString date = ttm->index(start, TransactionTableModel::Date, parent)
611                         .data().toString();
612         QString type = ttm->index(start, TransactionTableModel::Type, parent)
613                         .data().toString();
614         QString address = ttm->index(start, TransactionTableModel::ToAddress, parent)
615                         .data().toString();
616         QIcon icon = qvariant_cast<QIcon>(ttm->index(start,
617                             TransactionTableModel::ToAddress, parent)
618                         .data(Qt::DecorationRole));
619
620         notificator->notify(Notificator::Information,
621                             (amount)<0 ? tr("Sent transaction") :
622                                          tr("Incoming transaction"),
623                               tr("Date: %1\n"
624                                  "Amount: %2\n"
625                                  "Type: %3\n"
626                                  "Address: %4\n")
627                               .arg(date)
628                               .arg(BitcoinUnits::formatWithUnit(walletModel->getOptionsModel()->getDisplayUnit(), amount, true))
629                               .arg(type)
630                               .arg(address), icon);
631     }
632 }
633
634 void BitcoinGUI::gotoOverviewPage()
635 {
636     overviewAction->setChecked(true);
637     centralWidget->setCurrentWidget(overviewPage);
638
639     exportAction->setEnabled(false);
640     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
641 }
642
643 void BitcoinGUI::gotoHistoryPage()
644 {
645     historyAction->setChecked(true);
646     centralWidget->setCurrentWidget(transactionsPage);
647
648     exportAction->setEnabled(true);
649     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
650     connect(exportAction, SIGNAL(triggered()), transactionView, SLOT(exportClicked()));
651 }
652
653 void BitcoinGUI::gotoAddressBookPage()
654 {
655     addressBookAction->setChecked(true);
656     centralWidget->setCurrentWidget(addressBookPage);
657
658     exportAction->setEnabled(true);
659     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
660     connect(exportAction, SIGNAL(triggered()), addressBookPage, SLOT(exportClicked()));
661 }
662
663 void BitcoinGUI::gotoReceiveCoinsPage()
664 {
665     receiveCoinsAction->setChecked(true);
666     centralWidget->setCurrentWidget(receiveCoinsPage);
667
668     exportAction->setEnabled(true);
669     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
670     connect(exportAction, SIGNAL(triggered()), receiveCoinsPage, SLOT(exportClicked()));
671 }
672
673 void BitcoinGUI::gotoSendCoinsPage()
674 {
675     sendCoinsAction->setChecked(true);
676     centralWidget->setCurrentWidget(sendCoinsPage);
677
678     exportAction->setEnabled(false);
679     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
680 }
681
682 void BitcoinGUI::gotoMessagePage()
683 {
684 #ifdef FIRST_CLASS_MESSAGING
685     messageAction->setChecked(true);
686     centralWidget->setCurrentWidget(messagePage);
687
688     exportAction->setEnabled(false);
689     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
690 #else
691     messagePage->show();
692     messagePage->setFocus();
693 #endif
694 }
695
696 void BitcoinGUI::gotoMessagePage(QString addr)
697 {
698     gotoMessagePage();
699     messagePage->setAddress(addr);
700 }
701
702 void BitcoinGUI::dragEnterEvent(QDragEnterEvent *event)
703 {
704     // Accept only URLs
705     if(event->mimeData()->hasUrls())
706         event->acceptProposedAction();
707 }
708
709 void BitcoinGUI::dropEvent(QDropEvent *event)
710 {
711     if(event->mimeData()->hasUrls())
712     {
713         gotoSendCoinsPage();
714         QList<QUrl> urls = event->mimeData()->urls();
715         foreach(const QUrl &url, urls)
716         {
717             sendCoinsPage->handleURL(&url);
718         }
719     }
720
721     event->acceptProposedAction();
722 }
723
724 void BitcoinGUI::handleURL(QString strURL)
725 {
726     gotoSendCoinsPage();
727     QUrl url = QUrl(strURL);
728     sendCoinsPage->handleURL(&url);
729 }
730
731 void BitcoinGUI::setEncryptionStatus(int status)
732 {
733     switch(status)
734     {
735     case WalletModel::Unencrypted:
736         labelEncryptionIcon->hide();
737         encryptWalletAction->setChecked(false);
738         changePassphraseAction->setEnabled(false);
739         encryptWalletAction->setEnabled(true);
740         break;
741     case WalletModel::Unlocked:
742         labelEncryptionIcon->show();
743         labelEncryptionIcon->setPixmap(QIcon(":/icons/lock_open").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
744         labelEncryptionIcon->setToolTip(tr("Wallet is <b>encrypted</b> and currently <b>unlocked</b>"));
745         encryptWalletAction->setChecked(true);
746         changePassphraseAction->setEnabled(true);
747         encryptWalletAction->setEnabled(false); // TODO: decrypt currently not supported
748         break;
749     case WalletModel::Locked:
750         labelEncryptionIcon->show();
751         labelEncryptionIcon->setPixmap(QIcon(":/icons/lock_closed").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
752         labelEncryptionIcon->setToolTip(tr("Wallet is <b>encrypted</b> and currently <b>locked</b>"));
753         encryptWalletAction->setChecked(true);
754         changePassphraseAction->setEnabled(true);
755         encryptWalletAction->setEnabled(false); // TODO: decrypt currently not supported
756         break;
757     }
758 }
759
760 void BitcoinGUI::encryptWallet(bool status)
761 {
762     if(!walletModel)
763         return;
764     AskPassphraseDialog dlg(status ? AskPassphraseDialog::Encrypt:
765                                      AskPassphraseDialog::Decrypt, this);
766     dlg.setModel(walletModel);
767     dlg.exec();
768
769     setEncryptionStatus(walletModel->getEncryptionStatus());
770 }
771
772 void BitcoinGUI::changePassphrase()
773 {
774     AskPassphraseDialog dlg(AskPassphraseDialog::ChangePass, this);
775     dlg.setModel(walletModel);
776     dlg.exec();
777 }
778
779 void BitcoinGUI::unlockWallet()
780 {
781     if(!walletModel)
782         return;
783     // Unlock wallet when requested by wallet model
784     if(walletModel->getEncryptionStatus() == WalletModel::Locked)
785     {
786         AskPassphraseDialog dlg(AskPassphraseDialog::Unlock, this);
787         dlg.setModel(walletModel);
788         dlg.exec();
789     }
790 }